• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cmath>
17 #include <cstdlib>
18 #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h"
19 
20 #include "interfaces/native/event/ui_input_event_impl.h"
21 #include "interfaces/native/ui_input_event.h"
22 
23 #include "base/geometry/ng/point_t.h"
24 #include "base/geometry/ng/size_t.h"
25 #include "base/log/dump_log.h"
26 #include "base/log/frame_report.h"
27 #include "base/log/log_wrapper.h"
28 #include "base/memory/ace_type.h"
29 #include "base/ressched/ressched_report.h"
30 #include "base/utils/system_properties.h"
31 #include "base/utils/utils.h"
32 #include "core/common/ace_engine.h"
33 #include "core/common/ace_view.h"
34 #include "core/common/ai/image_analyzer_manager.h"
35 #include "core/components/common/layout/constants.h"
36 #include "core/components_ng/event/gesture_event_hub.h"
37 #include "core/components_ng/pattern/xcomponent/xcomponent_controller_ng.h"
38 #include "core/event/axis_event.h"
39 #ifdef NG_BUILD
40 #include "bridge/declarative_frontend/ng/declarative_frontend_ng.h"
41 #else
42 #include "bridge/declarative_frontend/declarative_frontend.h"
43 #endif
44 #ifdef ENABLE_ROSEN_BACKEND
45 #include "feature/anco_manager/rs_ext_node_operation.h"
46 #include "core/components_ng/render/adapter/rosen_render_context.h"
47 #endif
48 
49 #include "core/components_ng/event/input_event.h"
50 #include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_child_tree_callback.h"
51 #include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_session_adapter.h"
52 #include "core/components_ng/pattern/xcomponent/xcomponent_event_hub.h"
53 #include "core/components_ng/pattern/xcomponent/xcomponent_ext_surface_callback_client.h"
54 #include "core/event/event_info_convertor.h"
55 #include "core/event/key_event.h"
56 #include "core/event/mouse_event.h"
57 #include "core/event/touch_event.h"
58 #include "core/pipeline_ng/pipeline_context.h"
59 
60 namespace OHOS::Ace::NG {
61 namespace {
62 const std::string BUFFER_USAGE_XCOMPONENT = "xcomponent";
63 
XComponentRenderFitToString(RenderFit renderFit)64 std::string XComponentRenderFitToString(RenderFit renderFit)
65 {
66     static const std::string renderFitStyles[] = { "RenderFit.CENTER", "RenderFit.TOP", "RenderFit.BOTTOM",
67         "RenderFit.LEFT", "RenderFit.RIGHT", "RenderFit.TOP_LEFT", "RenderFit.TOP_RIGHT", "RenderFit.BOTTOM_LEFT",
68         "RenderFit.BOTTOM_RIGHT", "RenderFit.RESIZE_FILL", "RenderFit.RESIZE_CONTAIN",
69         "RenderFit.RESIZE_CONTAIN_TOP_LEFT", "RenderFit.RESIZE_CONTAIN_BOTTOM_RIGHT", "RenderFit.RESIZE_COVER",
70         "RenderFit.RESIZE_COVER_TOP_LEFT", "RenderFit.RESIZE_COVER_BOTTOM_RIGHT" };
71     return renderFitStyles[static_cast<int>(renderFit)];
72 }
73 
ConvertNativeXComponentTouchEvent(const TouchType & touchType)74 OH_NativeXComponent_TouchEventType ConvertNativeXComponentTouchEvent(const TouchType& touchType)
75 {
76     switch (touchType) {
77         case TouchType::DOWN:
78             return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_DOWN;
79         case TouchType::UP:
80             return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UP;
81         case TouchType::MOVE:
82             return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_MOVE;
83         case TouchType::CANCEL:
84             return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_CANCEL;
85         default:
86             return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UNKNOWN;
87     }
88 }
89 
ConvertNativeXComponentTouchToolType(const SourceTool & toolType)90 OH_NativeXComponent_TouchPointToolType ConvertNativeXComponentTouchToolType(const SourceTool& toolType)
91 {
92     switch (toolType) {
93         case SourceTool::FINGER:
94             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_FINGER;
95         case SourceTool::PEN:
96             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_PEN;
97         case SourceTool::RUBBER:
98             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_RUBBER;
99         case SourceTool::BRUSH:
100             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_BRUSH;
101         case SourceTool::PENCIL:
102             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_PENCIL;
103         case SourceTool::AIRBRUSH:
104             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_AIRBRUSH;
105         case SourceTool::MOUSE:
106             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_MOUSE;
107         case SourceTool::LENS:
108             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_LENS;
109         default:
110             return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN;
111     }
112 }
113 
ConvertNativeXComponentKeyAction(const KeyAction & keyAction)114 OH_NativeXComponent_KeyAction ConvertNativeXComponentKeyAction(const KeyAction& keyAction)
115 {
116     switch (keyAction) {
117         case KeyAction::DOWN:
118             return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_DOWN;
119         case KeyAction::UP:
120             return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UP;
121         default:
122             return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UNKNOWN;
123     }
124 }
125 
ConvertNativeXComponentEventSourceType(const SourceType & sourceType)126 OH_NativeXComponent_EventSourceType ConvertNativeXComponentEventSourceType(const SourceType& sourceType)
127 {
128     switch (sourceType) {
129         case SourceType::MOUSE:
130             return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_MOUSE;
131         case SourceType::TOUCH:
132             return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_TOUCHSCREEN;
133         case SourceType::TOUCH_PAD:
134             return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_TOUCHPAD;
135         case SourceType::KEYBOARD:
136             return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_KEYBOARD;
137         default:
138             return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_UNKNOWN;
139     }
140 }
141 
ConvertNativeXComponentKeyEvent(const KeyEvent & event)142 OH_NativeXComponent_KeyEvent ConvertNativeXComponentKeyEvent(const KeyEvent& event)
143 {
144     OH_NativeXComponent_KeyEvent nativeKeyEvent;
145     nativeKeyEvent.action = ConvertNativeXComponentKeyAction(event.action);
146     nativeKeyEvent.code = static_cast<OH_NativeXComponent_KeyCode>(event.code);
147     nativeKeyEvent.sourceType = ConvertNativeXComponentEventSourceType(event.sourceType);
148     nativeKeyEvent.deviceId = event.deviceId;
149     nativeKeyEvent.timestamp = event.timeStamp.time_since_epoch().count();
150     return nativeKeyEvent;
151 }
152 
ConvertNativeXComponentAnalyzerStatus(const ImageAnalyzerState state)153 ArkUI_XComponent_ImageAnalyzerState ConvertNativeXComponentAnalyzerStatus(const ImageAnalyzerState state)
154 {
155     switch (state) {
156         case ImageAnalyzerState::UNSUPPORTED:
157             return ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_UNSUPPORTED;
158         case ImageAnalyzerState::ONGOING:
159             return ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_ONGOING;
160         case ImageAnalyzerState::STOPPED:
161             return ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_STOPPED;
162         case ImageAnalyzerState::FINISHED:
163             return ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_FINISHED;
164         default:
165             return ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_DISABLED;
166     }
167 }
168 } // namespace
169 
XComponentPattern(const std::optional<std::string> & id,XComponentType type,const std::optional<std::string> & libraryname,const std::shared_ptr<InnerXComponentController> & xcomponentController,float initWidth,float initHeight,bool isTypedNode)170 XComponentPattern::XComponentPattern(const std::optional<std::string>& id, XComponentType type,
171     const std::optional<std::string>& libraryname,
172     const std::shared_ptr<InnerXComponentController>& xcomponentController, float initWidth, float initHeight,
173     bool isTypedNode)
174     : id_(id), type_(type), xcomponentController_(xcomponentController), initSize_(initWidth, initHeight),
175       isTypedNode_(isTypedNode)
176 {
177     SetLibraryName(libraryname);
178     if (!isTypedNode_) {
179         InitNativeXComponent();
180     }
181     RegisterSurfaceCallbackModeEvent();
182 }
183 
XComponentTypeToString(XComponentType type)184 std::string XComponentPattern::XComponentTypeToString(XComponentType type)
185 {
186     switch (type) {
187         case XComponentType::UNKNOWN:
188             return "unknown";
189         case XComponentType::SURFACE:
190             return "surface";
191         case XComponentType::COMPONENT:
192             return "component";
193         case XComponentType::TEXTURE:
194             return "texture";
195         case XComponentType::NODE:
196             return "node";
197         default:
198             return "unknown";
199     }
200 }
201 
XComponentNodeTypeToString(XComponentNodeType type)202 std::string XComponentPattern::XComponentNodeTypeToString(XComponentNodeType type)
203 {
204     switch (type) {
205         case XComponentNodeType::UNKNOWN:
206             return "unknown";
207         case XComponentNodeType::TYPE_NODE:
208             return "type_node";
209         case XComponentNodeType::DECLARATIVE_NODE:
210             return "declarative_node";
211         case XComponentNodeType::CNODE:
212             return "cnode";
213         default:
214             return "unknown";
215     }
216 }
217 
AdjustNativeWindowSize(float width,float height)218 void XComponentPattern::AdjustNativeWindowSize(float width, float height)
219 {
220     auto host = GetHost();
221     CHECK_NULL_VOID(host);
222     auto context = host->GetContextRefPtr();
223     CHECK_NULL_VOID(context);
224     auto viewScale = context->GetViewScale();
225     CHECK_NULL_VOID(renderSurface_);
226     renderSurface_->AdjustNativeWindowSize(
227         static_cast<uint32_t>(width * viewScale), static_cast<uint32_t>(height * viewScale));
228 }
229 
InitNativeXComponent()230 void XComponentPattern::InitNativeXComponent()
231 {
232     if ((type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) && libraryname_.has_value()) {
233         isNativeXComponent_ = true;
234         nativeXComponentImpl_ = AceType::MakeRefPtr<NativeXComponentImpl>();
235         nativeXComponent_ = std::make_shared<OH_NativeXComponent>(AceType::RawPtr(nativeXComponentImpl_));
236     }
237 }
238 
InitXComponent()239 void XComponentPattern::InitXComponent()
240 {
241     // used for TypedNode, not for declareative
242     if (isTypedNode_) {
243         InitNativeXComponent();
244         if (isNativeXComponent_) {
245             LoadNative();
246         }
247     }
248 }
249 
InitSurface()250 void XComponentPattern::InitSurface()
251 {
252     auto host = GetHost();
253     CHECK_NULL_VOID(host);
254     auto renderContext = host->GetRenderContext();
255     CHECK_NULL_VOID(renderContext);
256 
257     // only xcomponent created by capi will set successfully, others will be set in FireExternalEvent
258     SetExpectedRateRangeInit();
259     OnFrameEventInit();
260     UnregisterOnFrameEventInit();
261 
262     renderContext->SetClipToFrame(true);
263     renderContext->SetClipToBounds(true);
264 #ifdef RENDER_EXTRACT_SUPPORTED
265     renderSurface_ = RenderSurface::Create(CovertToRenderSurfaceType(type_));
266 #else
267     renderSurface_ = RenderSurface::Create();
268 #endif
269     renderSurface_->SetInstanceId(GetHostInstanceId());
270     renderSurface_->SetBufferUsage(BUFFER_USAGE_XCOMPONENT);
271     if (type_ == XComponentType::SURFACE) {
272         InitializeRenderContext();
273         if (!SystemProperties::GetExtSurfaceEnabled()) {
274             renderSurface_->SetRenderContext(renderContextForSurface_);
275         } else {
276             auto pipelineContext = host->GetContextRefPtr();
277             CHECK_NULL_VOID(pipelineContext);
278             pipelineContext->AddOnAreaChangeNode(host->GetId());
279             extSurfaceClient_ = MakeRefPtr<XComponentExtSurfaceCallbackClient>(WeakClaim(this));
280             renderSurface_->SetExtSurfaceCallback(extSurfaceClient_);
281 #ifdef RENDER_EXTRACT_SUPPORTED
282             RegisterRenderContextCallBack();
283 #endif
284         }
285         handlingSurfaceRenderContext_ = renderContextForSurface_;
286     } else if (type_ == XComponentType::TEXTURE) {
287         renderSurface_->SetRenderContext(renderContext);
288         renderSurface_->SetIsTexture(true);
289         renderContext->OnNodeNameUpdate(GetId());
290     }
291     renderSurface_->InitSurface();
292     renderSurface_->UpdateSurfaceConfig();
293     if (type_ == XComponentType::TEXTURE) {
294         renderSurface_->RegisterBufferCallback();
295     }
296     if (isTypedNode_ || isCNode_) {
297         InitNativeWindow(initSize_.Width(), initSize_.Height());
298     }
299     surfaceId_ = renderSurface_->GetUniqueId();
300 
301     UpdateTransformHint();
302 }
303 
UpdateTransformHint()304 void XComponentPattern::UpdateTransformHint()
305 {
306     auto host = GetHost();
307     CHECK_NULL_VOID(host);
308     auto pipelineContext = host->GetContextRefPtr();
309     CHECK_NULL_VOID(pipelineContext);
310     pipelineContext->AddWindowStateChangedCallback(host->GetId());
311     SetRotation(pipelineContext->GetTransformHint());
312     auto callbackId =
313         pipelineContext->RegisterTransformHintChangeCallback([weak = WeakClaim(this)](uint32_t transform) {
314             auto pattern = weak.Upgrade();
315             if (pattern) {
316                 pattern->SetRotation(transform);
317             }
318         });
319     UpdateTransformHintChangedCallbackId(callbackId);
320 }
321 
Initialize()322 void XComponentPattern::Initialize()
323 {
324     if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) {
325         InitSurface();
326         InitEvent();
327         InitController();
328     } else if (type_ == XComponentType::NODE && id_.has_value()) {
329         auto host = GetHost();
330         CHECK_NULL_VOID(host);
331         auto context = host->GetContextRefPtr();
332         if (context) {
333             FireExternalEvent(context, id_.value(), host->GetId(), false);
334             InitNativeNodeCallbacks();
335         }
336     }
337 
338     InitializeAccessibility();
339 }
340 
OnAttachToMainTree()341 void XComponentPattern::OnAttachToMainTree()
342 {
343     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] AttachToMainTree", GetId().c_str());
344     ACE_SCOPED_TRACE("XComponent[%s] AttachToMainTree", GetId().c_str());
345     isOnTree_ = true;
346     if (isTypedNode_ && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) {
347         HandleSurfaceCreated();
348     }
349     auto host = GetHost();
350     CHECK_NULL_VOID(host);
351     if (host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) {
352         if (needRecoverDisplaySync_ && displaySync_ && !displaySync_->IsOnPipeline()) {
353             TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "OnAttachToMainTree:recover displaySync: "
354                 "%{public}s(%{public}" PRIu64 ")", GetId().c_str(), displaySync_->GetId());
355             displaySync_->AddToPipelineOnContainer();
356             needRecoverDisplaySync_ = false;
357         }
358     }
359 }
360 
OnDetachFromMainTree()361 void XComponentPattern::OnDetachFromMainTree()
362 {
363     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] DetachFromMainTree", GetId().c_str());
364     ACE_SCOPED_TRACE("XComponent[%s] DetachFromMainTree", GetId().c_str());
365     isOnTree_ = false;
366     if (isTypedNode_ && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) {
367         HandleSurfaceDestroyed();
368     }
369     auto host = GetHost();
370     CHECK_NULL_VOID(host);
371     if (host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) {
372         if (displaySync_ && displaySync_->IsOnPipeline()) {
373             TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "OnDetachFromMainTree:remove displaySync: "
374                 "%{public}s(%{public}" PRIu64 ")", GetId().c_str(), displaySync_->GetId());
375             displaySync_->DelFromPipelineOnContainer();
376             needRecoverDisplaySync_ = true;
377         }
378     }
379 }
380 
InitializeRenderContext()381 void XComponentPattern::InitializeRenderContext()
382 {
383     renderContextForSurface_ = RenderContext::Create();
384 #ifdef RENDER_EXTRACT_SUPPORTED
385     auto contextType = type_ == XComponentType::TEXTURE ? RenderContext::ContextType::HARDWARE_TEXTURE
386                                                         : RenderContext::ContextType::HARDWARE_SURFACE;
387     RenderContext::ContextParam param = { contextType, GetId() + "Surface", RenderContext::PatternType::XCOM };
388 #else
389     RenderContext::ContextParam param = { RenderContext::ContextType::HARDWARE_SURFACE,
390                                           GetId() + "Surface", RenderContext::PatternType::XCOM };
391 #endif
392 
393     renderContextForSurface_->InitContext(false, param);
394 
395     renderContextForSurface_->UpdateBackgroundColor(Color::BLACK);
396 }
397 
398 #ifdef RENDER_EXTRACT_SUPPORTED
CovertToRenderSurfaceType(const XComponentType & hostType)399 RenderSurface::RenderSurfaceType XComponentPattern::CovertToRenderSurfaceType(const XComponentType& hostType)
400 {
401     switch (hostType) {
402         case XComponentType::SURFACE:
403             return RenderSurface::RenderSurfaceType::SURFACE;
404         case XComponentType::TEXTURE:
405             return RenderSurface::RenderSurfaceType::TEXTURE;
406         default:
407             return RenderSurface::RenderSurfaceType::UNKNOWN;
408     }
409 }
410 
RegisterRenderContextCallBack()411 void XComponentPattern::RegisterRenderContextCallBack()
412 {
413     CHECK_NULL_VOID(renderContextForSurface_);
414     auto OnAreaChangedCallBack = [weak = WeakClaim(this)](float x, float y, float w, float h) mutable {
415         auto pattern = weak.Upgrade();
416         CHECK_NULL_VOID(pattern);
417         auto host = pattern->GetHost();
418         CHECK_NULL_VOID(host);
419         auto geometryNode = host->GetGeometryNode();
420         CHECK_NULL_VOID(geometryNode);
421         auto xcomponentNodeSize = geometryNode->GetContentSize();
422         auto xcomponentNodeOffset = geometryNode->GetContentOffset();
423         auto transformRelativeOffset = host->GetTransformRelativeOffset();
424         Rect rect = Rect(transformRelativeOffset.GetX() + xcomponentNodeOffset.GetX(),
425             transformRelativeOffset.GetY() + xcomponentNodeOffset.GetY(), xcomponentNodeSize.Width(),
426             xcomponentNodeSize.Height());
427         if (pattern->renderSurface_) {
428             pattern->renderSurface_->SetExtSurfaceBoundsSync(rect.Left(), rect.Top(),
429                 rect.Width(), rect.Height());
430         }
431     };
432     renderContextForSurface_->SetSurfaceChangedCallBack(OnAreaChangedCallBack);
433 }
434 
RequestFocus()435 void XComponentPattern::RequestFocus()
436 {
437     auto host = GetHost();
438     CHECK_NULL_VOID(host);
439     auto eventHub = host->GetEventHub<XComponentEventHub>();
440     CHECK_NULL_VOID(eventHub);
441     auto focusHub = eventHub->GetOrCreateFocusHub();
442     CHECK_NULL_VOID(focusHub);
443 
444     focusHub->RequestFocusImmediately();
445 }
446 #endif
447 
OnAttachToFrameNode()448 void XComponentPattern::OnAttachToFrameNode()
449 {
450     Initialize();
451     if (FrameReport::GetInstance().GetEnable()) {
452         FrameReport::GetInstance().EnableSelfRender();
453     }
454 }
455 
OnModifyDone()456 void XComponentPattern::OnModifyDone()
457 {
458     Pattern::OnModifyDone();
459     // if surface has been reset by pip, do not set backgourndColor
460     if (handlingSurfaceRenderContext_ != renderContextForSurface_) {
461         return;
462     }
463     auto host = GetHost();
464     CHECK_NULL_VOID(host);
465     auto renderContext = host->GetRenderContext();
466     CHECK_NULL_VOID(renderContext);
467     CHECK_NULL_VOID(handlingSurfaceRenderContext_);
468     auto bkColor = renderContext->GetBackgroundColor();
469     if (bkColor.has_value()) {
470         bool isTransparent = bkColor.value().GetAlpha() < UINT8_MAX;
471         handlingSurfaceRenderContext_->UpdateBackgroundColor(isTransparent ? Color::TRANSPARENT : bkColor.value());
472     } else {
473         handlingSurfaceRenderContext_->UpdateBackgroundColor(Color::BLACK);
474     }
475 }
476 
OnAreaChangedInner()477 void XComponentPattern::OnAreaChangedInner()
478 {
479 #ifndef RENDER_EXTRACT_SUPPORTED
480     if (SystemProperties::GetExtSurfaceEnabled()) {
481         auto host = GetHost();
482         CHECK_NULL_VOID(host);
483         auto geometryNode = host->GetGeometryNode();
484         CHECK_NULL_VOID(geometryNode);
485         auto xcomponentNodeSize = geometryNode->GetContentSize();
486         auto xcomponentNodeOffset = geometryNode->GetContentOffset();
487         auto transformRelativeOffset = host->GetTransformRelativeOffset();
488         renderSurface_->SetExtSurfaceBounds(transformRelativeOffset.GetX() + xcomponentNodeOffset.GetX(),
489             transformRelativeOffset.GetY() + xcomponentNodeOffset.GetY(), xcomponentNodeSize.Width(),
490             xcomponentNodeSize.Height());
491     }
492 #endif
493 }
494 
SetSurfaceNodeToGraphic()495 void XComponentPattern::SetSurfaceNodeToGraphic()
496 {
497 #ifdef ENABLE_ROSEN_BACKEND
498     if (type_ != XComponentType::SURFACE || !id_.has_value() ||
499         !Rosen::RSExtNodeOperation::GetInstance().CheckNeedToProcess(id_.value())) {
500         return;
501     }
502     auto host = GetHost();
503     CHECK_NULL_VOID(host);
504     auto renderContext = host->GetRenderContext();
505     CHECK_NULL_VOID(renderContext);
506     auto rosenRenderContext = AceType::DynamicCast<NG::RosenRenderContext>(renderContext);
507     CHECK_NULL_VOID(rosenRenderContext);
508     std::shared_ptr<Rosen::RSNode> parentNode = rosenRenderContext->GetRSNode();
509     CHECK_NULL_VOID(parentNode);
510     RectF canvasRect = rosenRenderContext->GetPropertyOfPosition();
511 
512     CHECK_NULL_VOID(renderContextForSurface_);
513     auto context = AceType::DynamicCast<NG::RosenRenderContext>(renderContextForSurface_);
514     CHECK_NULL_VOID(context);
515     std::shared_ptr<Rosen::RSNode> rsNode = context->GetRSNode();
516     CHECK_NULL_VOID(rsNode);
517     std::shared_ptr<Rosen::RSSurfaceNode> rsSurfaceNode = std::static_pointer_cast<Rosen::RSSurfaceNode>(rsNode);
518     CHECK_NULL_VOID(rsSurfaceNode);
519 
520     Rosen::RSExtNodeOperation::GetInstance().ProcessRSExtNode(GetId(), parentNode->GetId(),
521         canvasRect.GetX(), canvasRect.GetY(), rsSurfaceNode);
522 #endif
523 }
524 
OnRebuildFrame()525 void XComponentPattern::OnRebuildFrame()
526 {
527     if (type_ != XComponentType::SURFACE) {
528         return;
529     }
530     if (!renderSurface_->IsSurfaceValid()) {
531         return;
532     }
533     auto host = GetHost();
534     CHECK_NULL_VOID(host);
535     auto renderContext = host->GetRenderContext();
536     CHECK_NULL_VOID(renderContext);
537     CHECK_NULL_VOID(handlingSurfaceRenderContext_);
538     renderContext->AddChild(handlingSurfaceRenderContext_, 0);
539     SetSurfaceNodeToGraphic();
540 }
541 
OnDetachFromFrameNode(FrameNode * frameNode)542 void XComponentPattern::OnDetachFromFrameNode(FrameNode* frameNode)
543 {
544     CHECK_NULL_VOID(frameNode);
545     UninitializeAccessibility();
546     if (isTypedNode_) {
547         if (surfaceCallbackMode_ == SurfaceCallbackMode::PIP) {
548             HandleSurfaceDestroyed();
549         }
550         if (isNativeXComponent_) {
551             OnNativeUnload(frameNode);
552         }
553     } else {
554         if (!hasXComponentInit_) {
555             return;
556         }
557         if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) {
558             OnSurfaceDestroyed();
559             auto eventHub = frameNode->GetEventHub<XComponentEventHub>();
560             CHECK_NULL_VOID(eventHub);
561             {
562                 ACE_SCOPED_TRACE("XComponent[%s] FireDestroyEvent", GetId().c_str());
563                 eventHub->FireDestroyEvent(GetId());
564             }
565             if (id_.has_value()) {
566                 eventHub->FireDetachEvent(id_.value());
567             }
568             {
569                 ACE_SCOPED_TRACE("XComponent[%s] FireControllerDestroyedEvent", GetId().c_str());
570                 eventHub->FireControllerDestroyedEvent(surfaceId_, GetId());
571             }
572 #ifdef RENDER_EXTRACT_SUPPORTED
573             if (renderContextForSurface_) {
574                 renderContextForSurface_->RemoveSurfaceChangedCallBack();
575             }
576 #endif
577         }
578     }
579 
580     auto id = frameNode->GetId();
581     auto pipeline = frameNode->GetContextRefPtr();
582     CHECK_NULL_VOID(pipeline);
583     pipeline->RemoveWindowStateChangedCallback(id);
584     if (HasTransformHintChangedCallbackId()) {
585         pipeline->UnregisterTransformHintChangedCallback(transformHintChangedCallbackId_.value_or(-1));
586     }
587     if (FrameReport::GetInstance().GetEnable()) {
588         FrameReport::GetInstance().DisableSelfRender();
589     }
590 }
591 
InitController()592 void XComponentPattern::InitController()
593 {
594     CHECK_NULL_VOID(xcomponentController_);
595     auto host = GetHost();
596     CHECK_NULL_VOID(host);
597     auto pipelineContext = host->GetContextRefPtr();
598     CHECK_NULL_VOID(pipelineContext);
599     auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(), TaskExecutor::TaskType::UI);
600     auto* controllerNG = static_cast<XComponentControllerNG*>(xcomponentController_.get());
601     if (controllerNG) {
602         controllerNG->SetPattern(AceType::Claim(this));
603     }
604     xcomponentController_->SetConfigSurfaceImpl(
605         [weak = WeakClaim(this), uiTaskExecutor](uint32_t surfaceWidth, uint32_t surfaceHeight) {
606             uiTaskExecutor.PostSyncTask(
607                 [weak, surfaceWidth, surfaceHeight]() {
608                     auto pattern = weak.Upgrade();
609                     CHECK_NULL_VOID(pattern);
610                     pattern->ConfigSurface(surfaceWidth, surfaceHeight);
611                 },
612                 "ArkUIXComponentSurfaceConfigChange");
613         });
614     if (!isTypedNode_) {
615         xcomponentController_->SetSurfaceId(surfaceId_);
616     }
617 }
618 
ConfigSurface(uint32_t surfaceWidth,uint32_t surfaceHeight)619 void XComponentPattern::ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight)
620 {
621     renderSurface_->ConfigSurface(surfaceWidth, surfaceHeight);
622 }
623 
OnAttachContext(PipelineContext * context)624 void XComponentPattern::OnAttachContext(PipelineContext* context)
625 {
626     CHECK_NULL_VOID(context);
627     auto host = GetHost();
628     CHECK_NULL_VOID(host);
629     context->AddWindowStateChangedCallback(host->GetId());
630     CHECK_NULL_VOID(renderSurface_);
631     renderSurface_->SetInstanceId(context->GetInstanceId());
632 }
633 
OnDetachContext(PipelineContext * context)634 void XComponentPattern::OnDetachContext(PipelineContext* context)
635 {
636     CHECK_NULL_VOID(context);
637     auto host = GetHost();
638     CHECK_NULL_VOID(host);
639     context->RemoveWindowStateChangedCallback(host->GetId());
640 }
641 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const642 void XComponentPattern::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
643 {
644     Pattern::ToJsonValue(json, filter);
645     if (filter.IsFastFilter()) {
646         return;
647     }
648     json->PutExtAttr("enableAnalyzer", isEnableAnalyzer_ ? "true" : "false", filter);
649     json->PutExtAttr("enableSecure", isEnableSecure_ ? "true" : "false", filter);
650     json->PutExtAttr("hdrBrightness", std::to_string(hdrBrightness_).c_str(), filter);
651     json->PutExtAttr("enableTransparentLayer", isTransparentLayer_ ? "true" : "false", filter);
652     json->PutExtAttr("screenId", screenId_.has_value() ? std::to_string(screenId_.value()).c_str() : "", filter);
653     if (type_ == XComponentType::SURFACE) {
654         json->PutExtAttr("renderFit", XComponentRenderFitToString(GetSurfaceRenderFit()).c_str(), filter);
655     }
656 }
657 
SetRotation(uint32_t rotation)658 void XComponentPattern::SetRotation(uint32_t rotation)
659 {
660     if (type_ != XComponentType::SURFACE || isSurfaceLock_ || rotation_ == rotation) {
661         return;
662     }
663     rotation_ = rotation;
664     CHECK_NULL_VOID(renderSurface_);
665     renderSurface_->SetTransformHint(rotation);
666 }
667 
BeforeSyncGeometryProperties(const DirtySwapConfig & config)668 void XComponentPattern::BeforeSyncGeometryProperties(const DirtySwapConfig& config)
669 {
670     if (type_ == XComponentType::COMPONENT || type_ == XComponentType::NODE || config.skipMeasure) {
671         return;
672     }
673     auto host = GetHost();
674     CHECK_NULL_VOID(host);
675     auto geometryNode = host->GetGeometryNode();
676     CHECK_NULL_VOID(geometryNode);
677     drawSize_ = geometryNode->GetContentSize();
678     if (!drawSize_.IsPositive()) {
679         TAG_LOGW(
680             AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s]'s size is not positive", GetId().c_str());
681         return;
682     }
683     globalPosition_ = geometryNode->GetFrameOffset();
684     localPosition_ = geometryNode->GetContentOffset();
685 
686     if (IsSupportImageAnalyzerFeature()) {
687         UpdateAnalyzerUIConfig(geometryNode);
688     }
689     const auto& [offsetChanged, sizeChanged, needFireNativeEvent] = UpdateSurfaceRect();
690     if (!hasXComponentInit_) {
691         initSize_ = paintRect_.GetSize();
692         if (!SystemProperties::GetExtSurfaceEnabled() && !isTypedNode_) {
693             XComponentSizeInit();
694         }
695         auto offset = globalPosition_ + paintRect_.GetOffset();
696         NativeXComponentOffset(offset.GetX(), offset.GetY());
697         hasXComponentInit_ = true;
698     }
699 #ifndef RENDER_EXTRACT_SUPPORTED
700     if (SystemProperties::GetExtSurfaceEnabled()) {
701         auto transformRelativeOffset = host->GetTransformRelativeOffset();
702         renderSurface_->SetExtSurfaceBounds(
703             static_cast<int32_t>(transformRelativeOffset.GetX() + localPosition_.GetX()),
704             static_cast<int32_t>(transformRelativeOffset.GetY() + localPosition_.GetY()),
705             static_cast<int32_t>(drawSize_.Width()), static_cast<int32_t>(drawSize_.Height()));
706     }
707 #endif
708     HandleSurfaceChangeEvent(false, offsetChanged, sizeChanged, needFireNativeEvent, config.frameOffsetChange);
709     if (type_ == XComponentType::SURFACE && renderType_ == NodeRenderType::RENDER_TYPE_TEXTURE) {
710         AddAfterLayoutTaskForExportTexture();
711     }
712     host->MarkNeedSyncRenderTree();
713 }
714 
DumpInfo()715 void XComponentPattern::DumpInfo()
716 {
717     DumpLog::GetInstance().AddDesc(std::string("xcomponentId: ").append(id_.value_or("no id")));
718     DumpLog::GetInstance().AddDesc(std::string("xcomponentType: ").append(XComponentTypeToString(type_)));
719     DumpLog::GetInstance().AddDesc(std::string("libraryName: ").append(libraryname_.value_or("no library name")));
720     DumpLog::GetInstance().AddDesc(std::string("surfaceId: ").append(surfaceId_));
721     DumpLog::GetInstance().AddDesc(std::string("surfaceRect: ").append(paintRect_.ToString()));
722 }
723 
DumpAdvanceInfo()724 void XComponentPattern::DumpAdvanceInfo()
725 {
726     DumpLog::GetInstance().AddDesc(std::string("enableSecure: ").append(isEnableSecure_ ? "true" : "false"));
727     DumpLog::GetInstance().AddDesc(std::string("hdrBrightness: ").append(std::to_string(hdrBrightness_).c_str()));
728     DumpLog::GetInstance().AddDesc(
729         std::string("enableTransparentLayer: ").append(isTransparentLayer_ ? "true" : "false"));
730     DumpLog::GetInstance().AddDesc(
731         std::string("screenId: ").append(screenId_.has_value() ? std::to_string(screenId_.value()).c_str() : ""));
732     if (renderSurface_) {
733         renderSurface_->DumpInfo();
734     }
735 }
736 
NativeXComponentOffset(double x,double y)737 void XComponentPattern::NativeXComponentOffset(double x, double y)
738 {
739     CHECK_RUN_ON(UI);
740     CHECK_NULL_VOID(nativeXComponent_);
741     CHECK_NULL_VOID(nativeXComponentImpl_);
742     auto host = GetHost();
743     CHECK_NULL_VOID(host);
744     auto pipelineContext = host->GetContextRefPtr();
745     CHECK_NULL_VOID(pipelineContext);
746     float scale = pipelineContext->GetViewScale();
747     nativeXComponentImpl_->SetXComponentOffsetX(x * scale);
748     nativeXComponentImpl_->SetXComponentOffsetY(y * scale);
749 }
750 
NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent & touchEvent,const std::vector<XComponentTouchPoint> & xComponentTouchPoints)751 void XComponentPattern::NativeXComponentDispatchTouchEvent(
752     const OH_NativeXComponent_TouchEvent& touchEvent, const std::vector<XComponentTouchPoint>& xComponentTouchPoints)
753 {
754     CHECK_RUN_ON(UI);
755     CHECK_NULL_VOID(nativeXComponent_);
756     CHECK_NULL_VOID(nativeXComponentImpl_);
757     nativeXComponentImpl_->SetTouchEvent(touchEvent);
758     nativeXComponentImpl_->SetTouchPoint(xComponentTouchPoints);
759     auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
760     const auto* callback = nativeXComponentImpl_->GetCallback();
761     CHECK_NULL_VOID(callback);
762     CHECK_NULL_VOID(callback->DispatchTouchEvent);
763     callback->DispatchTouchEvent(nativeXComponent_.get(), surface);
764 }
765 
InitNativeWindow(float textureWidth,float textureHeight)766 void XComponentPattern::InitNativeWindow(float textureWidth, float textureHeight)
767 {
768     auto host = GetHost();
769     CHECK_NULL_VOID(host);
770     auto context = host->GetContextRefPtr();
771     CHECK_NULL_VOID(context);
772     CHECK_NULL_VOID(renderSurface_);
773     if (renderSurface_->GetNativeWindow()) {
774         return;
775     }
776     if (renderSurface_->IsSurfaceValid() && (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE)) {
777         float viewScale = context->GetViewScale();
778         renderSurface_->CreateNativeWindow();
779         renderSurface_->AdjustNativeWindowSize(
780             static_cast<uint32_t>(textureWidth * viewScale), static_cast<uint32_t>(textureHeight * viewScale));
781         nativeWindow_ = renderSurface_->GetNativeWindow();
782     }
783 }
784 
XComponentSizeInit()785 void XComponentPattern::XComponentSizeInit()
786 {
787     CHECK_RUN_ON(UI);
788     auto host = GetHost();
789     CHECK_NULL_VOID(host);
790     if (!isCNode_) {
791         InitNativeWindow(initSize_.Width(), initSize_.Height());
792     } else {
793         AdjustNativeWindowSize(initSize_.Width(), initSize_.Height());
794     }
795 
796 #ifdef RENDER_EXTRACT_SUPPORTED
797     if (xcomponentController_ && renderSurface_) {
798         xcomponentController_->SetSurfaceId(renderSurface_->GetUniqueId());
799     }
800 #endif
801     auto eventHub = host->GetEventHub<XComponentEventHub>();
802     CHECK_NULL_VOID(eventHub);
803     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] triggers onLoad and OnSurfaceCreated callback",
804         GetId().c_str());
805     if (id_.has_value()) {
806         eventHub->FireSurfaceInitEvent(id_.value(), host->GetId());
807     }
808     {
809         ACE_SCOPED_TRACE("XComponent[%s] FireLoadEvent", GetId().c_str());
810         eventHub->FireLoadEvent(GetId());
811     }
812     {
813         ACE_SCOPED_TRACE("XComponent[%s] FireControllerCreatedEvent", GetId().c_str());
814         eventHub->FireControllerCreatedEvent(surfaceId_, GetId());
815     }
816 }
817 
XComponentSizeChange(const RectF & surfaceRect,bool needFireNativeEvent)818 void XComponentPattern::XComponentSizeChange(const RectF& surfaceRect, bool needFireNativeEvent)
819 {
820     auto host = GetHost();
821     CHECK_NULL_VOID(host);
822     renderSurface_->UpdateSurfaceSizeInUserData(
823         static_cast<uint32_t>(surfaceRect.Width()), static_cast<uint32_t>(surfaceRect.Height()));
824 
825     // In declarative mode: Native onSurfaceCreated callback is triggred
826     // when the component finish it's first layout, so do not trigger the native onSurfaceChanged callback
827     if (!isTypedNode_ && isNativeXComponent_ && !needFireNativeEvent) {
828         return;
829     }
830     // When creating the surface for the first time, needFireNativeEvent = false, other time needFireNativeEvent = true
831     // the first time change size no need to resize nativeWindow
832     OnSurfaceChanged(surfaceRect, needFireNativeEvent);
833 }
834 
GetAccessibilitySessionAdapter()835 RefPtr<AccessibilitySessionAdapter> XComponentPattern::GetAccessibilitySessionAdapter()
836 {
837     return accessibilitySessionAdapter_;
838 }
839 
InitializeAccessibility()840 void XComponentPattern::InitializeAccessibility()
841 {
842     if (accessibilityChildTreeCallback_) {
843         return;
844     }
845 
846     InitializeAccessibilityCallback();
847     auto host = GetHost();
848     CHECK_NULL_VOID(host);
849     int64_t accessibilityId = host->GetAccessibilityId();
850     TAG_LOGI(AceLogTag::ACE_XCOMPONENT,
851         "InitializeAccessibility accessibilityId: %{public}" PRId64 "", accessibilityId);
852     auto pipeline = host->GetContextRefPtr();
853     CHECK_NULL_VOID(pipeline);
854     auto accessibilityManager = pipeline->GetAccessibilityManager();
855     CHECK_NULL_VOID(accessibilityManager);
856     accessibilityChildTreeCallback_ = std::make_shared<XComponentAccessibilityChildTreeCallback>(
857         WeakClaim(this), host->GetAccessibilityId());
858     accessibilityManager->RegisterAccessibilityChildTreeCallback(
859         accessibilityId, accessibilityChildTreeCallback_);
860     if (accessibilityManager->IsRegister()) {
861         accessibilityChildTreeCallback_->OnRegister(
862             pipeline->GetWindowId(), accessibilityManager->GetTreeId());
863     }
864 }
865 
UninitializeAccessibility()866 void XComponentPattern::UninitializeAccessibility()
867 {
868     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "UninitializeAccessibility");
869     auto host = GetHost();
870     CHECK_NULL_VOID(host);
871     int64_t accessibilityId = host->GetAccessibilityId();
872     auto pipeline = host->GetContextRefPtr();
873     CHECK_NULL_VOID(pipeline);
874     auto accessibilityManager = pipeline->GetAccessibilityManager();
875     CHECK_NULL_VOID(accessibilityManager);
876     if (accessibilityManager->IsRegister() && accessibilityChildTreeCallback_) {
877         accessibilityChildTreeCallback_->OnDeregister();
878     }
879     accessibilityManager->DeregisterAccessibilityChildTreeCallback(accessibilityId);
880     accessibilityChildTreeCallback_ = nullptr;
881 }
882 
OnAccessibilityChildTreeRegister(uint32_t windowId,int32_t treeId)883 bool XComponentPattern::OnAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId)
884 {
885     auto host = GetHost();
886     CHECK_NULL_RETURN(host, false);
887     auto pipeline = host->GetContextRefPtr();
888     CHECK_NULL_RETURN(pipeline, false);
889     auto accessibilityManager = pipeline->GetAccessibilityManager();
890     CHECK_NULL_RETURN(accessibilityManager, false);
891     if (accessibilityProvider_ == nullptr) {
892         accessibilityProvider_ =
893             AceType::MakeRefPtr<XComponentAccessibilityProvider>(WeakClaim(this));
894     }
895 
896     auto pair = GetNativeXComponent();
897     auto nativeXComponentImpl = pair.first;
898     CHECK_NULL_RETURN(nativeXComponentImpl, false);
899     auto nativeProvider = nativeXComponentImpl->GetAccessbilityProvider();
900     CHECK_NULL_RETURN(nativeProvider, false);
901     if (!nativeProvider->IsRegister()) {
902         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "Not register native accessibility");
903         return false;
904     }
905 
906     nativeProvider->SetInnerAccessibilityProvider(accessibilityProvider_);
907     if (accessibilitySessionAdapter_ == nullptr) {
908         accessibilitySessionAdapter_ =
909             AceType::MakeRefPtr<XcomponentAccessibilitySessionAdapter>(host);
910     }
911     Registration registration;
912     registration.windowId = windowId;
913     registration.parentWindowId = windowId;
914     registration.parentTreeId = treeId;
915     registration.elementId = host->GetAccessibilityId();
916     registration.operatorType = OperatorType::JS_THIRD_PROVIDER;
917     registration.hostNode = WeakClaim(RawPtr(host));
918     registration.accessibilityProvider = WeakClaim(RawPtr(accessibilityProvider_));
919     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "OnAccessibilityChildTreeRegister, "
920         "windowId: %{public}d, treeId: %{public}d.", windowId, treeId);
921     return accessibilityManager->RegisterInteractionOperationAsChildTree(registration);
922 }
923 
OnAccessibilityChildTreeDeregister()924 bool XComponentPattern::OnAccessibilityChildTreeDeregister()
925 {
926     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "OnAccessibilityChildTreeDeregister, "
927         "windowId: %{public}u, treeId: %{public}d.", windowId_, treeId_);
928     auto host = GetHost();
929     CHECK_NULL_RETURN(host, false);
930     auto pipeline = host->GetContextRefPtr();
931     CHECK_NULL_RETURN(pipeline, false);
932     auto accessibilityManager = pipeline->GetAccessibilityManager();
933     CHECK_NULL_RETURN(accessibilityManager, false);
934     auto pair = GetNativeXComponent();
935     auto nativeXComponentImpl = pair.first;
936     CHECK_NULL_RETURN(nativeXComponentImpl, false);
937     auto nativeProvider = nativeXComponentImpl->GetAccessbilityProvider();
938     CHECK_NULL_RETURN(nativeProvider, false);
939     nativeProvider->SetInnerAccessibilityProvider(nullptr);
940     accessibilitySessionAdapter_ = nullptr;
941     accessibilityProvider_ = nullptr;
942     return accessibilityManager->DeregisterInteractionOperationAsChildTree(windowId_, treeId_);
943 }
944 
OnSetAccessibilityChildTree(int32_t childWindowId,int32_t childTreeId)945 void XComponentPattern::OnSetAccessibilityChildTree(
946     int32_t childWindowId, int32_t childTreeId)
947 {
948     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "OnAccessibilityChildTreeDeregister, "
949         "windowId: %{public}d, treeId: %{public}d.", childWindowId, childTreeId);
950     windowId_ = static_cast<uint32_t>(childWindowId);
951     treeId_ = childTreeId;
952     auto host = GetHost();
953     CHECK_NULL_VOID(host);
954     auto accessibilityProperty = host->GetAccessibilityProperty<AccessibilityProperty>();
955     CHECK_NULL_VOID(accessibilityProperty);
956     accessibilityProperty->SetChildWindowId(childWindowId);
957     accessibilityProperty->SetChildTreeId(childTreeId);
958 }
959 
InitializeAccessibilityCallback()960 void XComponentPattern::InitializeAccessibilityCallback()
961 {
962     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "InitializeAccessibilityCallback");
963     CHECK_NULL_VOID(nativeXComponentImpl_);
964     auto nativeProvider = nativeXComponentImpl_->GetAccessbilityProvider();
965     CHECK_NULL_VOID(nativeProvider);
966     nativeProvider->SetRegisterCallback(
967         [weak = WeakClaim(this)] (bool isRegister) {
968             auto pattern = weak.Upgrade();
969             CHECK_NULL_VOID(pattern);
970             pattern->HandleRegisterAccessibilityEvent(isRegister);
971         });
972 }
973 
HandleRegisterAccessibilityEvent(bool isRegister)974 void XComponentPattern::HandleRegisterAccessibilityEvent(bool isRegister)
975 {
976     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "HandleRegisterAccessibilityEvent, "
977         "isRegister: %{public}d.", isRegister);
978     CHECK_NULL_VOID(accessibilityChildTreeCallback_);
979     auto host = GetHost();
980     CHECK_NULL_VOID(host);
981     auto pipeline = host->GetContextRefPtr();
982     CHECK_NULL_VOID(pipeline);
983     auto accessibilityManager = pipeline->GetAccessibilityManager();
984     CHECK_NULL_VOID(accessibilityManager);
985     if (!isRegister) {
986         accessibilityChildTreeCallback_->OnDeregister();
987         return;
988     }
989 
990     if (accessibilityManager->IsRegister()) {
991         accessibilityChildTreeCallback_->OnRegister(
992             pipeline->GetWindowId(), accessibilityManager->GetTreeId());
993     }
994 }
995 
InitNativeNodeCallbacks()996 void XComponentPattern::InitNativeNodeCallbacks()
997 {
998     CHECK_NULL_VOID(nativeXComponent_);
999     CHECK_NULL_VOID(nativeXComponentImpl_);
1000 
1001     auto host = GetHost();
1002     CHECK_NULL_VOID(host);
1003     nativeXComponentImpl_->registerContaner(AceType::RawPtr(host));
1004 
1005     auto OnAttachRootNativeNode = [](void* container, void* root) {
1006         ContainerScope scope(Container::CurrentIdSafely());
1007         auto node = AceType::Claim(reinterpret_cast<NG::FrameNode*>(root));
1008         CHECK_NULL_VOID(node);
1009         auto host = AceType::Claim(reinterpret_cast<NG::FrameNode*>(container));
1010         CHECK_NULL_VOID(host);
1011         host->AddChild(node);
1012         host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
1013     };
1014 
1015     auto OnDetachRootNativeNode = [](void* container, void* root) {
1016         ContainerScope scope(Container::CurrentIdSafely());
1017         auto node = AceType::Claim(reinterpret_cast<NG::FrameNode*>(root));
1018         CHECK_NULL_VOID(node);
1019         auto host = AceType::Claim(reinterpret_cast<NG::FrameNode*>(container));
1020         CHECK_NULL_VOID(host);
1021         host->RemoveChild(node);
1022     };
1023 
1024     nativeXComponentImpl_->registerNativeNodeCallbacks(std::move(OnAttachRootNativeNode),
1025         std::move(OnDetachRootNativeNode));
1026 }
1027 
InitEvent()1028 void XComponentPattern::InitEvent()
1029 {
1030     auto host = GetHost();
1031     CHECK_NULL_VOID(host);
1032     auto eventHub = host->GetEventHub<XComponentEventHub>();
1033     CHECK_NULL_VOID(eventHub);
1034     if (id_.has_value()) {
1035         eventHub->SetOnSurfaceInitEvent(CreateExternalEvent());
1036     }
1037     auto gestureHub = eventHub->GetOrCreateGestureEventHub();
1038     CHECK_NULL_VOID(gestureHub);
1039     InitTouchEvent(gestureHub);
1040     InitOnTouchIntercept(gestureHub);
1041     auto inputHub = eventHub->GetOrCreateInputEventHub();
1042     InitMouseEvent(inputHub);
1043     InitAxisEvent(inputHub);
1044     InitMouseHoverEvent(inputHub);
1045     auto focusHub = host->GetOrCreateFocusHub();
1046     CHECK_NULL_VOID(focusHub);
1047     InitFocusEvent(focusHub);
1048 }
1049 
InitFocusEvent(const RefPtr<FocusHub> & focusHub)1050 void XComponentPattern::InitFocusEvent(const RefPtr<FocusHub>& focusHub)
1051 {
1052 #ifdef RENDER_EXTRACT_SUPPORTED
1053     focusHub->SetFocusable(true);
1054 #endif
1055 
1056     auto onFocusEvent = [weak = WeakClaim(this)]() {
1057         auto pattern = weak.Upgrade();
1058         CHECK_NULL_VOID(pattern);
1059         return pattern->HandleFocusEvent();
1060     };
1061     focusHub->SetOnFocusInternal(std::move(onFocusEvent));
1062 
1063     auto onKeyEvent = [weak = WeakClaim(this)](const KeyEvent& event) -> bool {
1064         TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "HandleKeyEvent[%{public}d,%{public}d,%{public}d,%{public}" PRId64 "]",
1065             event.action, event.code, event.sourceType, event.deviceId);
1066         auto pattern = weak.Upgrade();
1067         CHECK_NULL_RETURN(pattern, false);
1068         return pattern->HandleKeyEvent(event);
1069     };
1070     focusHub->SetOnKeyEventInternal(std::move(onKeyEvent));
1071 
1072     auto onBlurEvent = [weak = WeakClaim(this)]() {
1073         auto pattern = weak.Upgrade();
1074         CHECK_NULL_VOID(pattern);
1075         return pattern->HandleBlurEvent();
1076     };
1077     focusHub->SetOnBlurInternal(std::move(onBlurEvent));
1078 }
HandleFocusEvent()1079 void XComponentPattern::HandleFocusEvent()
1080 {
1081     CHECK_NULL_VOID(nativeXComponent_);
1082     CHECK_NULL_VOID(nativeXComponentImpl_);
1083     auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1084     const auto focusEventCallback = nativeXComponentImpl_->GetFocusEventCallback();
1085     CHECK_NULL_VOID(focusEventCallback);
1086     focusEventCallback(nativeXComponent_.get(), surface);
1087 }
1088 
HandleKeyEvent(const KeyEvent & event)1089 bool XComponentPattern::HandleKeyEvent(const KeyEvent& event)
1090 {
1091     CHECK_NULL_RETURN(nativeXComponent_, false);
1092     CHECK_NULL_RETURN(nativeXComponentImpl_, false);
1093 
1094     OH_NativeXComponent_KeyEvent keyEvent = ConvertNativeXComponentKeyEvent(event);
1095     nativeXComponentImpl_->SetKeyEvent(keyEvent);
1096 
1097     auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1098     const auto keyEventCallbackWithResult = nativeXComponentImpl_->GetKeyEventCallbackWithResult();
1099     if (keyEventCallbackWithResult) {
1100         return keyEventCallbackWithResult(nativeXComponent_.get(), surface);
1101     }
1102     const auto keyEventCallback = nativeXComponentImpl_->GetKeyEventCallback();
1103     CHECK_NULL_RETURN(keyEventCallback, false);
1104     keyEventCallback(nativeXComponent_.get(), surface);
1105     return false;
1106 }
1107 
HandleBlurEvent()1108 void XComponentPattern::HandleBlurEvent()
1109 {
1110     CHECK_NULL_VOID(nativeXComponent_);
1111     CHECK_NULL_VOID(nativeXComponentImpl_);
1112     auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1113     const auto blurEventCallback = nativeXComponentImpl_->GetBlurEventCallback();
1114     CHECK_NULL_VOID(blurEventCallback);
1115     blurEventCallback(nativeXComponent_.get(), surface);
1116 }
1117 
InitTouchEvent(const RefPtr<GestureEventHub> & gestureHub)1118 void XComponentPattern::InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub)
1119 {
1120     CHECK_NULL_VOID(!touchEvent_);
1121 
1122     auto touchTask = [weak = WeakClaim(this)](const TouchEventInfo& info) {
1123         if (EventInfoConvertor::IsTouchEventNeedAbandoned(info)) {
1124             return;
1125         }
1126         auto pattern = weak.Upgrade();
1127         CHECK_NULL_VOID(pattern);
1128         pattern->HandleTouchEvent(info);
1129     };
1130 
1131     touchEvent_ = MakeRefPtr<TouchEventImpl>(std::move(touchTask));
1132     gestureHub->AddTouchEvent(touchEvent_);
1133 }
1134 
InitAxisEvent(const RefPtr<InputEventHub> & inputHub)1135 void XComponentPattern::InitAxisEvent(const RefPtr<InputEventHub>& inputHub)
1136 {
1137     CHECK_NULL_VOID(!axisEvent_);
1138 
1139     auto axisTask = [weak = WeakClaim(this)](const AxisInfo& info) {
1140         auto pattern = weak.Upgrade();
1141         CHECK_NULL_VOID(pattern);
1142         pattern->HandleAxisEvent(info);
1143     };
1144 
1145     axisEvent_ = MakeRefPtr<InputEvent>(std::move(axisTask));
1146     inputHub->AddOnAxisEvent(axisEvent_);
1147 }
1148 
InitOnTouchIntercept(const RefPtr<GestureEventHub> & gestureHub)1149 void XComponentPattern::InitOnTouchIntercept(const RefPtr<GestureEventHub>& gestureHub)
1150 {
1151     gestureHub->SetOnTouchIntercept(
1152         [weak = WeakClaim(this)](
1153             const TouchEventInfo& touchEvent) -> HitTestMode {
1154             auto pattern = weak.Upgrade();
1155             CHECK_NULL_RETURN(pattern, NG::HitTestMode::HTMDEFAULT);
1156             auto hostNode = pattern->GetHost();
1157             CHECK_NULL_RETURN(hostNode, NG::HitTestMode::HTMDEFAULT);
1158             CHECK_NULL_RETURN(pattern->nativeXComponentImpl_, hostNode->GetHitTestMode());
1159             const auto onTouchInterceptCallback = pattern->nativeXComponentImpl_->GetOnTouchInterceptCallback();
1160             CHECK_NULL_RETURN(onTouchInterceptCallback, hostNode->GetHitTestMode());
1161             auto event = touchEvent.ConvertToTouchEvent();
1162             ArkUI_UIInputEvent uiEvent { ARKUI_UIINPUTEVENT_TYPE_TOUCH, TOUCH_EVENT_ID, &event };
1163             return static_cast<NG::HitTestMode>(onTouchInterceptCallback(pattern->nativeXComponent_.get(), &uiEvent));
1164         });
1165 }
1166 
InitMouseEvent(const RefPtr<InputEventHub> & inputHub)1167 void XComponentPattern::InitMouseEvent(const RefPtr<InputEventHub>& inputHub)
1168 {
1169     CHECK_NULL_VOID(!mouseEvent_);
1170 
1171     auto mouseTask = [weak = WeakClaim(this)](const MouseInfo& info) {
1172         auto pattern = weak.Upgrade();
1173         CHECK_NULL_VOID(pattern);
1174         TouchEventInfo touchEventInfo("touchEvent");
1175         if (EventInfoConvertor::ConvertMouseToTouchIfNeeded(info, touchEventInfo)) {
1176             pattern->HandleTouchEvent(touchEventInfo);
1177             return;
1178         }
1179         TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "HandleMouseEvent[%{public}f,%{public}f,%{public}d,%{public}d]",
1180             info.GetLocalLocation().GetX(), info.GetLocalLocation().GetY(), info.GetAction(), info.GetButton());
1181         pattern->HandleMouseEvent(info);
1182     };
1183 
1184     mouseEvent_ = MakeRefPtr<InputEvent>(std::move(mouseTask));
1185     inputHub->AddOnMouseEvent(mouseEvent_);
1186 }
1187 
InitMouseHoverEvent(const RefPtr<InputEventHub> & inputHub)1188 void XComponentPattern::InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub)
1189 {
1190     CHECK_NULL_VOID(!mouseHoverEvent_);
1191     auto mouseHoverTask = [weak = WeakClaim(this)](bool isHover) {
1192         auto pattern = weak.Upgrade();
1193         CHECK_NULL_VOID(pattern);
1194         pattern->HandleMouseHoverEvent(isHover);
1195     };
1196     mouseHoverEvent_ = MakeRefPtr<InputEvent>(std::move(mouseHoverTask));
1197     inputHub->AddOnHoverEvent(mouseHoverEvent_);
1198 }
1199 
HandleTouchEvent(const TouchEventInfo & info)1200 void XComponentPattern::HandleTouchEvent(const TouchEventInfo& info)
1201 {
1202     auto touchInfoList = info.GetChangedTouches();
1203     if (touchInfoList.empty()) {
1204         return;
1205     }
1206     const auto& touchInfo = touchInfoList.front();
1207     const auto& screenOffset = touchInfo.GetGlobalLocation();
1208     const auto& localOffset = touchInfo.GetLocalLocation();
1209     touchEventPoint_.id = touchInfo.GetFingerId();
1210     touchEventPoint_.screenX = static_cast<float>(screenOffset.GetX());
1211     touchEventPoint_.screenY = static_cast<float>(screenOffset.GetY());
1212     touchEventPoint_.x = static_cast<float>(localOffset.GetX());
1213     touchEventPoint_.y = static_cast<float>(localOffset.GetY());
1214     touchEventPoint_.size = touchInfo.GetSize();
1215     touchEventPoint_.force = touchInfo.GetForce();
1216     touchEventPoint_.deviceId = touchInfo.GetTouchDeviceId();
1217     const auto timeStamp = info.GetTimeStamp().time_since_epoch().count();
1218     touchEventPoint_.timeStamp = timeStamp;
1219     auto touchType = touchInfoList.front().GetTouchType();
1220     touchEventPoint_.type = ConvertNativeXComponentTouchEvent(touchType);
1221     TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "HandleTouchEvent[%{public}f,%{public}f,%{public}d,%{public}zu,%{public}u]",
1222         localOffset.GetX(), localOffset.GetY(), touchInfo.GetFingerId(), touchInfoList.front().GetTouchType(),
1223         static_cast<uint32_t>(touchInfo.GetSize()));
1224     SetTouchPoint(info.GetTouches(), timeStamp, touchType);
1225 
1226     if (nativeXComponent_ && nativeXComponentImpl_) {
1227         nativeXComponentImpl_->SetHistoricalPoint(SetHistoryPoint(info.GetHistory()));
1228         nativeXComponentImpl_->SetCurrentSourceType(
1229             { touchInfo.GetFingerId(), ConvertNativeXComponentEventSourceType(info.GetSourceDevice()) });
1230     }
1231     NativeXComponentDispatchTouchEvent(touchEventPoint_, nativeXComponentTouchPoints_);
1232 
1233 #ifdef RENDER_EXTRACT_SUPPORTED
1234     if (touchType == TouchType::DOWN) {
1235         RequestFocus();
1236     }
1237 #endif
1238 }
1239 
HandleMouseEvent(const MouseInfo & info)1240 void XComponentPattern::HandleMouseEvent(const MouseInfo& info)
1241 {
1242     OH_NativeXComponent_MouseEvent mouseEventPoint;
1243     mouseEventPoint.x = static_cast<float>(info.GetLocalLocation().GetX());
1244     mouseEventPoint.y = static_cast<float>(info.GetLocalLocation().GetY());
1245     mouseEventPoint.screenX = static_cast<float>(info.GetScreenLocation().GetX());
1246     mouseEventPoint.screenY = static_cast<float>(info.GetScreenLocation().GetY());
1247     switch (info.GetAction()) {
1248         case MouseAction::PRESS:
1249             mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_PRESS;
1250             break;
1251         case MouseAction::RELEASE:
1252             mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_RELEASE;
1253             break;
1254         case MouseAction::MOVE:
1255             mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_MOVE;
1256             break;
1257         case MouseAction::CANCEL:
1258             mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_CANCEL;
1259             break;
1260         default:
1261             mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_NONE;
1262             break;
1263     }
1264     switch (info.GetButton()) {
1265         case MouseButton::LEFT_BUTTON:
1266             mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_LEFT_BUTTON;
1267             break;
1268         case MouseButton::RIGHT_BUTTON:
1269             mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_RIGHT_BUTTON;
1270             break;
1271         case MouseButton::MIDDLE_BUTTON:
1272             mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_MIDDLE_BUTTON;
1273             break;
1274         case MouseButton::BACK_BUTTON:
1275             mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_BACK_BUTTON;
1276             break;
1277         case MouseButton::FORWARD_BUTTON:
1278             mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_FORWARD_BUTTON;
1279             break;
1280         default:
1281             mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_NONE_BUTTON;
1282             break;
1283     }
1284     mouseEventPoint.timestamp = info.GetTimeStamp().time_since_epoch().count();
1285     NativeXComponentDispatchMouseEvent(mouseEventPoint);
1286 }
1287 
HandleAxisEvent(const AxisInfo & info)1288 void XComponentPattern::HandleAxisEvent(const AxisInfo& info)
1289 {
1290     auto axisEvent = info.ConvertToAxisEvent();
1291     NativeXComponentDispatchAxisEvent(&axisEvent);
1292 }
1293 
HandleMouseHoverEvent(bool isHover)1294 void XComponentPattern::HandleMouseHoverEvent(bool isHover)
1295 {
1296     CHECK_RUN_ON(UI);
1297     CHECK_NULL_VOID(nativeXComponent_);
1298     CHECK_NULL_VOID(nativeXComponentImpl_);
1299     const auto* callback = nativeXComponentImpl_->GetMouseEventCallback();
1300     CHECK_NULL_VOID(callback);
1301     CHECK_NULL_VOID(callback->DispatchHoverEvent);
1302     callback->DispatchHoverEvent(nativeXComponent_.get(), isHover);
1303 }
1304 
NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent & mouseEvent)1305 void XComponentPattern::NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent)
1306 {
1307     CHECK_RUN_ON(UI);
1308     CHECK_NULL_VOID(nativeXComponent_);
1309     CHECK_NULL_VOID(nativeXComponentImpl_);
1310     nativeXComponentImpl_->SetMouseEvent(mouseEvent);
1311     auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1312     const auto* callback = nativeXComponentImpl_->GetMouseEventCallback();
1313     CHECK_NULL_VOID(callback);
1314     CHECK_NULL_VOID(callback->DispatchMouseEvent);
1315     callback->DispatchMouseEvent(nativeXComponent_.get(), surface);
1316 }
1317 
NativeXComponentDispatchAxisEvent(AxisEvent * axisEvent)1318 void XComponentPattern::NativeXComponentDispatchAxisEvent(AxisEvent* axisEvent)
1319 {
1320     CHECK_RUN_ON(UI);
1321     CHECK_NULL_VOID(nativeXComponent_);
1322     CHECK_NULL_VOID(nativeXComponentImpl_);
1323     const auto callback = nativeXComponentImpl_->GetUIAxisEventCallback();
1324     CHECK_NULL_VOID(callback);
1325     ArkUI_UIInputEvent uiEvent { ARKUI_UIINPUTEVENT_TYPE_AXIS, AXIS_EVENT_ID, axisEvent };
1326     callback(nativeXComponent_.get(), &uiEvent, ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS);
1327 }
1328 
SetTouchPoint(const std::list<TouchLocationInfo> & touchInfoList,const int64_t timeStamp,const TouchType & touchType)1329 void XComponentPattern::SetTouchPoint(
1330     const std::list<TouchLocationInfo>& touchInfoList, const int64_t timeStamp, const TouchType& touchType)
1331 {
1332     touchEventPoint_.numPoints =
1333         touchInfoList.size() <= OH_MAX_TOUCH_POINTS_NUMBER ? touchInfoList.size() : OH_MAX_TOUCH_POINTS_NUMBER;
1334     nativeXComponentTouchPoints_.clear();
1335     uint32_t index = 0;
1336     for (auto iterator = touchInfoList.begin(); iterator != touchInfoList.end() && index < OH_MAX_TOUCH_POINTS_NUMBER;
1337          iterator++) {
1338         OH_NativeXComponent_TouchPoint ohTouchPoint;
1339         const auto& pointTouchInfo = *iterator;
1340         const auto& pointWindowOffset = pointTouchInfo.GetGlobalLocation();
1341         const auto& pointLocalOffset = pointTouchInfo.GetLocalLocation();
1342         const auto& pointDisplayOffset = pointTouchInfo.GetScreenLocation();
1343         ohTouchPoint.id = pointTouchInfo.GetFingerId();
1344         // screenX and screenY implementation wrong but should not modify for maintaining compatibility
1345         ohTouchPoint.screenX = static_cast<float>(pointWindowOffset.GetX());
1346         ohTouchPoint.screenY = static_cast<float>(pointWindowOffset.GetY());
1347         ohTouchPoint.x = static_cast<float>(pointLocalOffset.GetX());
1348         ohTouchPoint.y = static_cast<float>(pointLocalOffset.GetY());
1349         ohTouchPoint.type = ConvertNativeXComponentTouchEvent(touchType);
1350         ohTouchPoint.size = pointTouchInfo.GetSize();
1351         ohTouchPoint.force = pointTouchInfo.GetForce();
1352         ohTouchPoint.timeStamp = timeStamp;
1353         ohTouchPoint.isPressed = (touchType == TouchType::DOWN);
1354         touchEventPoint_.touchPoints[index++] = ohTouchPoint;
1355         // set tiltX, tiltY, windowX, windowY, displayX, displayY and sourceToolType
1356         XComponentTouchPoint xcomponentTouchPoint;
1357         xcomponentTouchPoint.tiltX = pointTouchInfo.GetTiltX().value_or(0.0f);
1358         xcomponentTouchPoint.tiltY = pointTouchInfo.GetTiltY().value_or(0.0f);
1359         xcomponentTouchPoint.windowX = static_cast<float>(pointWindowOffset.GetX());
1360         xcomponentTouchPoint.windowY = static_cast<float>(pointWindowOffset.GetY());
1361         xcomponentTouchPoint.displayX = static_cast<float>(pointDisplayOffset.GetX());
1362         xcomponentTouchPoint.displayY = static_cast<float>(pointDisplayOffset.GetY());
1363         xcomponentTouchPoint.sourceToolType = ConvertNativeXComponentTouchToolType(pointTouchInfo.GetSourceTool());
1364         nativeXComponentTouchPoints_.emplace_back(xcomponentTouchPoint);
1365     }
1366     while (index < OH_MAX_TOUCH_POINTS_NUMBER) {
1367         OH_NativeXComponent_TouchPoint ohTouchPoint;
1368         ohTouchPoint.id = 0;
1369         ohTouchPoint.screenX = 0;
1370         ohTouchPoint.screenY = 0;
1371         ohTouchPoint.x = 0;
1372         ohTouchPoint.y = 0;
1373         ohTouchPoint.type = OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UNKNOWN;
1374         ohTouchPoint.size = 0;
1375         ohTouchPoint.force = 0;
1376         ohTouchPoint.timeStamp = 0;
1377         ohTouchPoint.isPressed = false;
1378         touchEventPoint_.touchPoints[index++] = ohTouchPoint;
1379     }
1380 }
1381 
SetHistoryPoint(const std::list<TouchLocationInfo> & touchInfoList)1382 std::vector<OH_NativeXComponent_HistoricalPoint> XComponentPattern::SetHistoryPoint(
1383     const std::list<TouchLocationInfo>& touchInfoList)
1384 {
1385     std::vector<OH_NativeXComponent_HistoricalPoint> historicalPoints;
1386     for (auto&& item : touchInfoList) {
1387         OH_NativeXComponent_HistoricalPoint point;
1388         point.id = item.GetFingerId();
1389         point.x = item.GetLocalLocation().GetX();
1390         point.y = item.GetLocalLocation().GetY();
1391         point.screenX = item.GetScreenLocation().GetX();
1392         point.screenY = item.GetScreenLocation().GetY();
1393         point.type = static_cast<OH_NativeXComponent_TouchEventType>(item.GetTouchType());
1394         point.size = item.GetSize();
1395         point.force = item.GetForce();
1396         point.timeStamp = item.GetTimeStamp().time_since_epoch().count();
1397         point.titlX = item.GetTiltX().value_or(0.0f);
1398         point.titlY = item.GetTiltY().value_or(0.0f);
1399         point.sourceTool = static_cast<OH_NativeXComponent_TouchEvent_SourceTool>(item.GetSourceTool());
1400 
1401         historicalPoints.push_back(point);
1402     }
1403     return historicalPoints;
1404 }
1405 
FireExternalEvent(RefPtr<NG::PipelineContext> context,const std::string & componentId,const uint32_t nodeId,const bool isDestroy)1406 void XComponentPattern::FireExternalEvent(RefPtr<NG::PipelineContext> context,
1407     const std::string& componentId, const uint32_t nodeId, const bool isDestroy)
1408 {
1409     CHECK_NULL_VOID(context);
1410 #ifdef NG_BUILD
1411     auto frontEnd = AceType::DynamicCast<DeclarativeFrontendNG>(context->GetFrontend());
1412 #else
1413     auto frontEnd = AceType::DynamicCast<DeclarativeFrontend>(context->GetFrontend());
1414 #endif
1415     CHECK_NULL_VOID(frontEnd);
1416     auto jsEngine = frontEnd->GetJsEngine();
1417     jsEngine->FireExternalEvent(componentId, nodeId, isDestroy);
1418 }
1419 
CreateExternalEvent()1420 ExternalEvent XComponentPattern::CreateExternalEvent()
1421 {
1422     return
1423         [weak = AceType::WeakClaim(this)](const std::string& componentId, const uint32_t nodeId, const bool isDestroy) {
1424             auto pattern = weak.Upgrade();
1425             CHECK_NULL_VOID(pattern);
1426             auto host = pattern->GetHost();
1427             CHECK_NULL_VOID(host);
1428             auto context = host->GetContextRefPtr();
1429             CHECK_NULL_VOID(context);
1430             pattern->FireExternalEvent(context, componentId, nodeId, isDestroy);
1431         };
1432 }
1433 
SetHandlingRenderContextForSurface(const RefPtr<RenderContext> & otherRenderContext)1434 void XComponentPattern::SetHandlingRenderContextForSurface(const RefPtr<RenderContext>& otherRenderContext)
1435 {
1436     CHECK_NULL_VOID(otherRenderContext);
1437     handlingSurfaceRenderContext_ = otherRenderContext;
1438     auto host = GetHost();
1439     CHECK_NULL_VOID(host);
1440     auto renderContext = host->GetRenderContext();
1441     renderContext->ClearChildren();
1442     renderContext->AddChild(handlingSurfaceRenderContext_, 0);
1443     handlingSurfaceRenderContext_->SetBounds(
1444         paintRect_.GetX(), paintRect_.GetY(), paintRect_.Width(), paintRect_.Height());
1445     host->MarkModifyDone();
1446 }
1447 
GetOffsetRelativeToWindow()1448 OffsetF XComponentPattern::GetOffsetRelativeToWindow()
1449 {
1450     auto host = GetHost();
1451     CHECK_NULL_RETURN(host, OffsetF());
1452     return host->GetTransformRelativeOffset();
1453 }
1454 
RestoreHandlingRenderContextForSurface()1455 void XComponentPattern::RestoreHandlingRenderContextForSurface()
1456 {
1457     SetHandlingRenderContextForSurface(renderContextForSurface_);
1458 }
1459 
SetExtController(const RefPtr<XComponentPattern> & extPattern)1460 XComponentControllerErrorCode XComponentPattern::SetExtController(const RefPtr<XComponentPattern>& extPattern)
1461 {
1462     CHECK_NULL_RETURN(extPattern, XCOMPONENT_CONTROLLER_BAD_PARAMETER);
1463     if (extPattern_.Upgrade()) {
1464         return XCOMPONENT_CONTROLLER_REPEAT_SET;
1465     }
1466     if (handlingSurfaceRenderContext_) {
1467         // backgroundColor of pip's surface is black
1468         handlingSurfaceRenderContext_->UpdateBackgroundColor(Color::BLACK);
1469     }
1470     extPattern->SetHandlingRenderContextForSurface(handlingSurfaceRenderContext_);
1471     extPattern_ = extPattern;
1472     handlingSurfaceRenderContext_.Reset();
1473     return XCOMPONENT_CONTROLLER_NO_ERROR;
1474 }
1475 
ResetExtController(const RefPtr<XComponentPattern> & extPattern)1476 XComponentControllerErrorCode XComponentPattern::ResetExtController(const RefPtr<XComponentPattern>& extPattern)
1477 {
1478     if (!extPattern) {
1479         return XCOMPONENT_CONTROLLER_BAD_PARAMETER;
1480     }
1481     auto curExtPattern = extPattern_.Upgrade();
1482     if (!curExtPattern || curExtPattern != extPattern) {
1483         return XCOMPONENT_CONTROLLER_RESET_ERROR;
1484     }
1485     RestoreHandlingRenderContextForSurface();
1486     extPattern->RestoreHandlingRenderContextForSurface();
1487     extPattern_.Reset();
1488     return XCOMPONENT_CONTROLLER_NO_ERROR;
1489 }
1490 
HandleSetExpectedRateRangeEvent()1491 void XComponentPattern::HandleSetExpectedRateRangeEvent()
1492 {
1493     CHECK_NULL_VOID(nativeXComponent_);
1494     CHECK_NULL_VOID(nativeXComponentImpl_);
1495     CHECK_NULL_VOID(displaySync_);
1496     OH_NativeXComponent_ExpectedRateRange* range = nativeXComponentImpl_->GetRateRange();
1497     CHECK_NULL_VOID(range);
1498     FrameRateRange frameRateRange;
1499     frameRateRange.Set(range->min, range->max, range->expected);
1500     displaySync_->SetExpectedFrameRateRange(frameRateRange);
1501     TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " SetExpectedFrameRateRange"
1502         "{%{public}d, %{public}d, %{public}d}", displaySync_->GetId(), range->min, range->max, range->expected);
1503 }
1504 
HandleOnFrameEvent()1505 void XComponentPattern::HandleOnFrameEvent()
1506 {
1507     CHECK_NULL_VOID(nativeXComponent_);
1508     CHECK_NULL_VOID(nativeXComponentImpl_);
1509     CHECK_NULL_VOID(displaySync_);
1510     displaySync_->RegisterOnFrameWithData([weak = AceType::WeakClaim(this)](RefPtr<DisplaySyncData> displaySyncData) {
1511         auto xComponentPattern = weak.Upgrade();
1512         CHECK_NULL_VOID(xComponentPattern);
1513         CHECK_NULL_VOID(xComponentPattern->nativeXComponentImpl_->GetOnFrameCallback());
1514         xComponentPattern->nativeXComponentImpl_->GetOnFrameCallback()(xComponentPattern->nativeXComponent_.get(),
1515             displaySyncData->GetTimestamp(), displaySyncData->GetTargetTimestamp());
1516     });
1517     TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " RegisterOnFrame",
1518         displaySync_->GetId());
1519     displaySync_->AddToPipelineOnContainer();
1520 }
1521 
HandleUnregisterOnFrameEvent()1522 void XComponentPattern::HandleUnregisterOnFrameEvent()
1523 {
1524     CHECK_NULL_VOID(nativeXComponent_);
1525     CHECK_NULL_VOID(nativeXComponentImpl_);
1526     CHECK_NULL_VOID(displaySync_);
1527     displaySync_->UnregisterOnFrame();
1528     TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " UnregisterOnFrame",
1529         displaySync_->GetId());
1530     displaySync_->DelFromPipelineOnContainer();
1531     needRecoverDisplaySync_ = false;
1532 }
1533 
DoTextureExport()1534 bool XComponentPattern::DoTextureExport()
1535 {
1536     CHECK_NULL_RETURN(handlingSurfaceRenderContext_, false);
1537     if (!ExportTextureAvailable()) {
1538         return false;
1539     }
1540     if (!handlingSurfaceRenderContext_->DoTextureExport(exportTextureSurfaceId_)) {
1541         TAG_LOGW(AceLogTag::ACE_XCOMPONENT, "DoTextureExport fail");
1542         return false;
1543     }
1544     auto host = GetHost();
1545     CHECK_NULL_RETURN(host, false);
1546     auto renderContext = host->GetRenderContext();
1547     CHECK_NULL_RETURN(renderContext, false);
1548     renderContext->SetIsNeedRebuildRSTree(false);
1549     return true;
1550 }
1551 
StopTextureExport()1552 bool XComponentPattern::StopTextureExport()
1553 {
1554     CHECK_NULL_RETURN(handlingSurfaceRenderContext_, false);
1555     if (!handlingSurfaceRenderContext_->StopTextureExport()) {
1556         TAG_LOGW(AceLogTag::ACE_XCOMPONENT, "StopTextureExport fail");
1557         return false;
1558     }
1559     auto host = GetHost();
1560     CHECK_NULL_RETURN(host, false);
1561     auto renderContext = host->GetRenderContext();
1562     CHECK_NULL_RETURN(renderContext, false);
1563     renderContext->ClearChildren();
1564     renderContext->AddChild(handlingSurfaceRenderContext_, 0);
1565     renderContext->SetIsNeedRebuildRSTree(true);
1566     return true;
1567 }
1568 
AddAfterLayoutTaskForExportTexture()1569 void XComponentPattern::AddAfterLayoutTaskForExportTexture()
1570 {
1571     auto host = GetHost();
1572     CHECK_NULL_VOID(host);
1573     auto context = host->GetContextRefPtr();
1574     CHECK_NULL_VOID(context);
1575     context->AddAfterLayoutTask([weak = WeakClaim(this)]() {
1576         auto pattern = weak.Upgrade();
1577         CHECK_NULL_VOID(pattern);
1578         pattern->DoTextureExport();
1579     });
1580 }
1581 
ExportTextureAvailable()1582 bool XComponentPattern::ExportTextureAvailable()
1583 {
1584     auto host = GetHost();
1585     auto parnetNodeContainer = host->GetNodeContainer();
1586     CHECK_NULL_RETURN(parnetNodeContainer, false);
1587     auto parent = parnetNodeContainer->GetAncestorNodeOfFrame(false);
1588     CHECK_NULL_RETURN(parent, false);
1589     auto ancestorNodeContainer = parent->GetNodeContainer();
1590     CHECK_NULL_RETURN(ancestorNodeContainer, true);
1591     auto ancestorViewNode = ancestorNodeContainer->GetChildAtIndex(0);
1592     CHECK_NULL_RETURN(ancestorViewNode, true);
1593     auto parnetExportTextureInfo = ancestorViewNode->GetExportTextureInfo();
1594     CHECK_NULL_RETURN(parnetExportTextureInfo, true);
1595     return parnetExportTextureInfo->GetCurrentRenderType() != NodeRenderType::RENDER_TYPE_TEXTURE;
1596 }
1597 
ChangeRenderType(NodeRenderType renderType)1598 bool XComponentPattern::ChangeRenderType(NodeRenderType renderType)
1599 {
1600     if (type_ != XComponentType::SURFACE) {
1601         return renderType == NodeRenderType::RENDER_TYPE_DISPLAY;
1602     }
1603     auto host = GetHost();
1604     CHECK_NULL_RETURN(host, false);
1605     if (!host->GetNodeContainer()) {
1606         renderType_ = renderType;
1607         return true;
1608     }
1609     auto renderContext = host->GetRenderContext();
1610     CHECK_NULL_RETURN(renderContext, false);
1611     if (renderType == NodeRenderType::RENDER_TYPE_TEXTURE) {
1612         if (DoTextureExport()) {
1613             renderType_ = renderType;
1614             return true;
1615         }
1616     } else {
1617         if (StopTextureExport()) {
1618             renderType_ = renderType;
1619             return true;
1620         }
1621     }
1622     return false;
1623 }
1624 
SetExportTextureSurfaceId(const std::string & surfaceId)1625 void XComponentPattern::SetExportTextureSurfaceId(const std::string& surfaceId)
1626 {
1627     exportTextureSurfaceId_ = StringUtils::StringToLongUint(surfaceId);
1628 }
1629 
SetIdealSurfaceWidth(float surfaceWidth)1630 void XComponentPattern::SetIdealSurfaceWidth(float surfaceWidth)
1631 {
1632     selfIdealSurfaceWidth_ = surfaceWidth;
1633 }
1634 
SetIdealSurfaceHeight(float surfaceHeight)1635 void XComponentPattern::SetIdealSurfaceHeight(float surfaceHeight)
1636 {
1637     selfIdealSurfaceHeight_ = surfaceHeight;
1638 }
1639 
SetIdealSurfaceOffsetX(float offsetX)1640 void XComponentPattern::SetIdealSurfaceOffsetX(float offsetX)
1641 {
1642     selfIdealSurfaceOffsetX_ = offsetX;
1643 }
1644 
SetIdealSurfaceOffsetY(float offsetY)1645 void XComponentPattern::SetIdealSurfaceOffsetY(float offsetY)
1646 {
1647     selfIdealSurfaceOffsetY_ = offsetY;
1648 }
1649 
ClearIdealSurfaceOffset(bool isXAxis)1650 void XComponentPattern::ClearIdealSurfaceOffset(bool isXAxis)
1651 {
1652     if (isXAxis) {
1653         selfIdealSurfaceOffsetX_.reset();
1654     } else {
1655         selfIdealSurfaceOffsetY_.reset();
1656     }
1657 }
1658 
HandleSurfaceChangeEvent(bool needForceRender,bool offsetChanged,bool sizeChanged,bool needFireNativeEvent,bool frameOffsetChange)1659 void XComponentPattern::HandleSurfaceChangeEvent(
1660     bool needForceRender, bool offsetChanged, bool sizeChanged, bool needFireNativeEvent, bool frameOffsetChange)
1661 {
1662     if (!drawSize_.IsPositive()) {
1663         return;
1664     }
1665     if (frameOffsetChange || offsetChanged) {
1666         auto offset = globalPosition_ + paintRect_.GetOffset();
1667         NativeXComponentOffset(offset.GetX(), offset.GetY());
1668     }
1669     if (sizeChanged) {
1670         XComponentSizeChange(paintRect_, needFireNativeEvent);
1671     }
1672     if (handlingSurfaceRenderContext_) {
1673         handlingSurfaceRenderContext_->SetBounds(
1674             paintRect_.GetX(), paintRect_.GetY(), paintRect_.Width(), paintRect_.Height());
1675     }
1676     if (renderSurface_) {
1677         renderSurface_->SetSurfaceDefaultSize(
1678             static_cast<int32_t>(paintRect_.Width()), static_cast<int32_t>(paintRect_.Height()));
1679     }
1680     if (needForceRender) {
1681         auto host = GetHost();
1682         CHECK_NULL_VOID(host);
1683         host->MarkNeedRenderOnly();
1684     }
1685 }
1686 
UpdateSurfaceRect()1687 std::tuple<bool, bool, bool> XComponentPattern::UpdateSurfaceRect()
1688 {
1689     if (!drawSize_.IsPositive()) {
1690         return { false, false, false };
1691     }
1692     auto preSurfaceSize = surfaceSize_;
1693     auto preSurfaceOffset = surfaceOffset_;
1694     if (selfIdealSurfaceWidth_.has_value() && Positive(selfIdealSurfaceWidth_.value()) &&
1695         selfIdealSurfaceHeight_.has_value() && Positive(selfIdealSurfaceHeight_.value())) {
1696         surfaceOffset_.SetX(selfIdealSurfaceOffsetX_.has_value()
1697                                 ? selfIdealSurfaceOffsetX_.value()
1698                                 : (drawSize_.Width() - selfIdealSurfaceWidth_.value()) / 2.0f);
1699 
1700         surfaceOffset_.SetY(selfIdealSurfaceOffsetY_.has_value()
1701                                 ? selfIdealSurfaceOffsetY_.value()
1702                                 : (drawSize_.Height() - selfIdealSurfaceHeight_.value()) / 2.0f);
1703         surfaceSize_ = { selfIdealSurfaceWidth_.value(), selfIdealSurfaceHeight_.value() };
1704     } else {
1705         surfaceSize_ = drawSize_;
1706         surfaceOffset_ = localPosition_;
1707     }
1708     auto offsetChanged = preSurfaceOffset != surfaceOffset_;
1709     auto sizeChanged = preSurfaceSize != surfaceSize_;
1710     if (offsetChanged || sizeChanged) {
1711         paintRect_ = { surfaceOffset_, surfaceSize_ };
1712         if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
1713             paintRect_ = AdjustPaintRect(
1714                 surfaceOffset_.GetX(), surfaceOffset_.GetY(), surfaceSize_.Width(), surfaceSize_.Height(), true);
1715         }
1716     }
1717     return { offsetChanged, sizeChanged, preSurfaceSize.IsPositive() };
1718 }
1719 
LoadNative()1720 void XComponentPattern::LoadNative()
1721 {
1722     auto host = GetHost();
1723     CHECK_NULL_VOID(host);
1724     auto eventHub = host->GetEventHub<XComponentEventHub>();
1725     CHECK_NULL_VOID(eventHub);
1726     eventHub->FireSurfaceInitEvent(id_.value_or(""), host->GetId());
1727     OnNativeLoad(reinterpret_cast<FrameNode*>(AceType::RawPtr(host)));
1728 }
1729 
OnNativeLoad(FrameNode * frameNode)1730 void XComponentPattern::OnNativeLoad(FrameNode* frameNode)
1731 {
1732     hasLoadNativeDone_ = true;
1733     CHECK_NULL_VOID(frameNode);
1734     auto eventHub = frameNode->GetEventHub<XComponentEventHub>();
1735     CHECK_NULL_VOID(eventHub);
1736     {
1737         ACE_SCOPED_TRACE("XComponent[%s] FireLoadEvent", GetId().c_str());
1738         eventHub->FireLoadEvent(GetId());
1739     }
1740 }
1741 
OnNativeUnload(FrameNode * frameNode)1742 void XComponentPattern::OnNativeUnload(FrameNode* frameNode)
1743 {
1744     hasLoadNativeDone_ = false;
1745     CHECK_NULL_VOID(frameNode);
1746     auto eventHub = frameNode->GetEventHub<XComponentEventHub>();
1747     CHECK_NULL_VOID(eventHub);
1748     {
1749         ACE_SCOPED_TRACE("XComponent[%s] FireDestroyEvent", GetId().c_str());
1750         eventHub->FireDestroyEvent(GetId());
1751     }
1752 }
1753 
OnSurfaceCreated()1754 void XComponentPattern::OnSurfaceCreated()
1755 {
1756     CHECK_RUN_ON(UI);
1757     auto width = initSize_.Width();
1758     auto height = initSize_.Height();
1759     if (isNativeXComponent_) {
1760         CHECK_NULL_VOID(nativeXComponentImpl_);
1761         CHECK_NULL_VOID(nativeXComponent_);
1762         nativeXComponentImpl_->SetXComponentWidth(static_cast<int32_t>(width));
1763         nativeXComponentImpl_->SetXComponentHeight(static_cast<int32_t>(height));
1764         nativeXComponentImpl_->SetSurface(nativeWindow_);
1765         const auto* callback = nativeXComponentImpl_->GetCallback();
1766         CHECK_NULL_VOID(callback);
1767         CHECK_NULL_VOID(callback->OnSurfaceCreated);
1768         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] native OnSurfaceCreated", GetId().c_str());
1769         ACE_SCOPED_TRACE("XComponent[%s] NativeSurfaceCreated[w:%f,h:%f]", GetId().c_str(), width, height);
1770         callback->OnSurfaceCreated(nativeXComponent_.get(), nativeWindow_);
1771     } else {
1772         auto host = GetHost();
1773         CHECK_NULL_VOID(host);
1774         auto eventHub = host->GetEventHub<XComponentEventHub>();
1775         CHECK_NULL_VOID(eventHub);
1776         {
1777             ACE_SCOPED_TRACE("XComponent[%s] FireControllerCreatedEvent", GetId().c_str());
1778             eventHub->FireControllerCreatedEvent(surfaceId_, GetId());
1779         }
1780     }
1781 }
1782 
OnSurfaceChanged(const RectF & surfaceRect,bool needResizeNativeWindow)1783 void XComponentPattern::OnSurfaceChanged(const RectF& surfaceRect, bool needResizeNativeWindow)
1784 {
1785     CHECK_RUN_ON(UI);
1786     auto host = GetHost();
1787     CHECK_NULL_VOID(host);
1788     auto width = surfaceRect.Width();
1789     auto height = surfaceRect.Height();
1790     if (needResizeNativeWindow) {
1791         AdjustNativeWindowSize(width, height);
1792     }
1793     if (isNativeXComponent_) {
1794         CHECK_NULL_VOID(nativeXComponent_);
1795         CHECK_NULL_VOID(nativeXComponentImpl_);
1796         nativeXComponentImpl_->SetXComponentWidth(static_cast<int32_t>(width));
1797         nativeXComponentImpl_->SetXComponentHeight(static_cast<int32_t>(height));
1798         auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1799         const auto* callback = nativeXComponentImpl_->GetCallback();
1800         CHECK_NULL_VOID(callback);
1801         CHECK_NULL_VOID(callback->OnSurfaceChanged);
1802         {
1803             ACE_SCOPED_TRACE("XComponent[%s] native OnSurfaceChanged[w:%f,h:%f]", GetId().c_str(), width, height);
1804             callback->OnSurfaceChanged(nativeXComponent_.get(), surface);
1805         }
1806     } else {
1807         auto eventHub = host->GetEventHub<XComponentEventHub>();
1808         CHECK_NULL_VOID(eventHub);
1809         {
1810             ACE_SCOPED_TRACE("XComponent[%s] FireControllerChangedEvent[w:%f,h:%f]", GetId().c_str(), width, height);
1811             eventHub->FireControllerChangedEvent(surfaceId_, surfaceRect);
1812         }
1813     }
1814 }
1815 
OnSurfaceDestroyed()1816 void XComponentPattern::OnSurfaceDestroyed()
1817 {
1818     if (isNativeXComponent_) {
1819         CHECK_RUN_ON(UI);
1820         CHECK_NULL_VOID(nativeXComponent_);
1821         CHECK_NULL_VOID(nativeXComponentImpl_);
1822         auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1823         const auto* callback = nativeXComponentImpl_->GetCallback();
1824         CHECK_NULL_VOID(callback);
1825         CHECK_NULL_VOID(callback->OnSurfaceDestroyed);
1826         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] native OnSurfaceDestroyed", GetId().c_str());
1827         ACE_SCOPED_TRACE("XComponent[%s] native OnSurfaceDestroyed", GetId().c_str());
1828         callback->OnSurfaceDestroyed(nativeXComponent_.get(), surface);
1829         nativeXComponentImpl_->SetSurface(nullptr);
1830     } else {
1831         auto host = GetHost();
1832         CHECK_NULL_VOID(host);
1833         auto eventHub = host->GetEventHub<XComponentEventHub>();
1834         CHECK_NULL_VOID(eventHub);
1835         {
1836             ACE_SCOPED_TRACE("XComponent[%s] FireControllerDestroyedEvent", GetId().c_str());
1837             eventHub->FireControllerDestroyedEvent(surfaceId_, GetId());
1838         }
1839     }
1840 }
1841 
RegisterSurfaceCallbackModeEvent()1842 void XComponentPattern::RegisterSurfaceCallbackModeEvent()
1843 {
1844     if (isTypedNode_ && !surfaceCallbackModeChangeEvent_) {
1845         surfaceCallbackModeChangeEvent_ = [weak = WeakClaim(this)](SurfaceCallbackMode mode) {
1846             auto xcPattern = weak.Upgrade();
1847             CHECK_NULL_VOID(xcPattern);
1848             xcPattern->OnSurfaceCallbackModeChange(mode);
1849         };
1850     }
1851 }
1852 
OnSurfaceCallbackModeChange(SurfaceCallbackMode mode)1853 void XComponentPattern::OnSurfaceCallbackModeChange(SurfaceCallbackMode mode)
1854 {
1855     CHECK_EQUAL_VOID(surfaceCallbackMode_, mode);
1856     surfaceCallbackMode_ = mode;
1857     auto host = GetHost();
1858     CHECK_NULL_VOID(host);
1859     if (!host->IsOnMainTree()) {
1860         if (surfaceCallbackMode_ == SurfaceCallbackMode::PIP) {
1861             HandleSurfaceCreated();
1862         } else if (surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) {
1863             HandleSurfaceDestroyed();
1864         }
1865     }
1866 }
1867 
HandleSurfaceCreated()1868 void XComponentPattern::HandleSurfaceCreated()
1869 {
1870     CHECK_NULL_VOID(renderSurface_);
1871     renderSurface_->RegisterSurface();
1872     surfaceId_ = screenId_.has_value() ? "" : renderSurface_->GetUniqueId();
1873     CHECK_NULL_VOID(xcomponentController_);
1874     xcomponentController_->SetSurfaceId(surfaceId_);
1875     OnSurfaceCreated();
1876 }
1877 
HandleSurfaceDestroyed()1878 void XComponentPattern::HandleSurfaceDestroyed()
1879 {
1880     CHECK_NULL_VOID(renderSurface_);
1881     renderSurface_->ReleaseSurfaceBuffers();
1882     renderSurface_->UnregisterSurface();
1883     CHECK_NULL_VOID(xcomponentController_);
1884     OnSurfaceDestroyed();
1885     xcomponentController_->SetSurfaceId("");
1886 }
1887 
NativeSurfaceShow()1888 void XComponentPattern::NativeSurfaceShow()
1889 {
1890     CHECK_RUN_ON(UI);
1891     CHECK_NULL_VOID(nativeXComponentImpl_);
1892     CHECK_NULL_VOID(nativeXComponent_);
1893     auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1894     const auto surfaceShowCallback = nativeXComponentImpl_->GetSurfaceShowCallback();
1895     CHECK_NULL_VOID(surfaceShowCallback);
1896     surfaceShowCallback(nativeXComponent_.get(), surface);
1897 }
1898 
NativeSurfaceHide()1899 void XComponentPattern::NativeSurfaceHide()
1900 {
1901     CHECK_RUN_ON(UI);
1902     CHECK_NULL_VOID(nativeXComponent_);
1903     CHECK_NULL_VOID(nativeXComponentImpl_);
1904     auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
1905     const auto surfaceHideCallback = nativeXComponentImpl_->GetSurfaceHideCallback();
1906     CHECK_NULL_VOID(surfaceHideCallback);
1907     surfaceHideCallback(nativeXComponent_.get(), surface);
1908     CHECK_NULL_VOID(renderSurface_);
1909     renderSurface_->ReleaseSurfaceBuffers();
1910 }
1911 
OnWindowHide()1912 void XComponentPattern::OnWindowHide()
1913 {
1914     if (!hasXComponentInit_ || hasReleasedSurface_ ||
1915         (type_ != XComponentType::SURFACE && type_ != XComponentType::TEXTURE)) {
1916         return;
1917     }
1918     if (renderSurface_) {
1919         renderSurface_->OnWindowStateChange(false);
1920     }
1921     NativeSurfaceHide();
1922     hasReleasedSurface_ = true;
1923 }
1924 
OnWindowShow()1925 void XComponentPattern::OnWindowShow()
1926 {
1927     if (!hasXComponentInit_ || !hasReleasedSurface_ ||
1928         (type_ != XComponentType::SURFACE && type_ != XComponentType::TEXTURE)) {
1929         return;
1930     }
1931     if (renderSurface_) {
1932         renderSurface_->OnWindowStateChange(true);
1933     }
1934     NativeSurfaceShow();
1935     hasReleasedSurface_ = false;
1936 }
1937 
EnableAnalyzer(bool enable)1938 void XComponentPattern::EnableAnalyzer(bool enable)
1939 {
1940     isEnableAnalyzer_ = enable;
1941     if (!isEnableAnalyzer_) {
1942         DestroyAnalyzerOverlay();
1943         return;
1944     }
1945 
1946     CHECK_NULL_VOID(!imageAnalyzerManager_);
1947     auto host = GetHost();
1948     CHECK_NULL_VOID(host);
1949     imageAnalyzerManager_ = std::make_shared<ImageAnalyzerManager>(host, ImageAnalyzerHolder::XCOMPONENT);
1950 }
1951 
SetImageAIOptions(void * options)1952 void XComponentPattern::SetImageAIOptions(void *options)
1953 {
1954     if (!imageAnalyzerManager_) {
1955         imageAnalyzerManager_ = std::make_shared<ImageAnalyzerManager>(GetHost(), ImageAnalyzerHolder::XCOMPONENT);
1956     }
1957     CHECK_NULL_VOID(imageAnalyzerManager_);
1958     imageAnalyzerManager_->SetImageAIOptions(options);
1959 }
1960 
StartImageAnalyzer(void * config,OnAnalyzedCallback & onAnalyzed)1961 void XComponentPattern::StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed)
1962 {
1963     if (!IsSupportImageAnalyzerFeature()) {
1964         CHECK_NULL_VOID(onAnalyzed);
1965         (onAnalyzed.value())(ImageAnalyzerState::UNSUPPORTED);
1966         return;
1967     }
1968 
1969     CHECK_NULL_VOID(imageAnalyzerManager_);
1970     imageAnalyzerManager_->SetImageAnalyzerConfig(config);
1971     imageAnalyzerManager_->SetImageAnalyzerCallback(onAnalyzed);
1972 
1973     auto host = GetHost();
1974     CHECK_NULL_VOID(host);
1975     auto* context = host->GetContext();
1976     CHECK_NULL_VOID(context);
1977     auto uiTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::UI);
1978     uiTaskExecutor.PostTask(
1979         [weak = WeakClaim(this)] {
1980             auto pattern = weak.Upgrade();
1981             CHECK_NULL_VOID(pattern);
1982             pattern->CreateAnalyzerOverlay();
1983         },
1984         "ArkUIXComponentCreateAnalyzerOverlay");
1985 }
1986 
StopImageAnalyzer()1987 void XComponentPattern::StopImageAnalyzer()
1988 {
1989     DestroyAnalyzerOverlay();
1990 }
1991 
IsSupportImageAnalyzerFeature()1992 bool XComponentPattern::IsSupportImageAnalyzerFeature()
1993 {
1994     return isEnableAnalyzer_ && imageAnalyzerManager_ && imageAnalyzerManager_->IsSupportImageAnalyzerFeature();
1995 }
1996 
CreateAnalyzerOverlay()1997 void XComponentPattern::CreateAnalyzerOverlay()
1998 {
1999     auto host = GetHost();
2000     CHECK_NULL_VOID(host);
2001     host->SetOverlayNode(nullptr);
2002 
2003     auto layoutProperty = GetLayoutProperty<XComponentLayoutProperty>();
2004     CHECK_NULL_VOID(layoutProperty);
2005     auto padding  = layoutProperty->CreatePaddingAndBorder();
2006     Rect contentRect = { padding.left.value_or(0), padding.top.value_or(0), drawSize_.Width(), drawSize_.Height() };
2007     auto context = host->GetRenderContext();
2008     CHECK_NULL_VOID(context);
2009     auto nailPixelMap = context->GetThumbnailPixelMap();
2010     CHECK_NULL_VOID(nailPixelMap);
2011     auto pixelMap = nailPixelMap->GetCropPixelMap(contentRect);
2012     CHECK_NULL_VOID(pixelMap);
2013 
2014     CHECK_NULL_VOID(imageAnalyzerManager_);
2015     imageAnalyzerManager_->CreateAnalyzerOverlay(pixelMap);
2016 }
2017 
UpdateAnalyzerOverlay()2018 void XComponentPattern::UpdateAnalyzerOverlay()
2019 {
2020     auto context = GetHost()->GetRenderContext();
2021     CHECK_NULL_VOID(context);
2022     auto pixelMap = context->GetThumbnailPixelMap();
2023     CHECK_NULL_VOID(pixelMap);
2024     CHECK_NULL_VOID(imageAnalyzerManager_);
2025     imageAnalyzerManager_->UpdateAnalyzerOverlay(pixelMap);
2026 }
2027 
UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode> & geometryNode)2028 void XComponentPattern::UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode)
2029 {
2030     if (IsSupportImageAnalyzerFeature()) {
2031         CHECK_NULL_VOID(imageAnalyzerManager_);
2032         imageAnalyzerManager_->UpdateAnalyzerUIConfig(geometryNode);
2033     }
2034 }
2035 
DestroyAnalyzerOverlay()2036 void XComponentPattern::DestroyAnalyzerOverlay()
2037 {
2038     CHECK_NULL_VOID(imageAnalyzerManager_);
2039     imageAnalyzerManager_->DestroyAnalyzerOverlay();
2040 }
2041 
ReleaseImageAnalyzer()2042 void XComponentPattern::ReleaseImageAnalyzer()
2043 {
2044     CHECK_NULL_VOID(imageAnalyzerManager_);
2045     imageAnalyzerManager_->ReleaseImageAnalyzer();
2046 }
2047 
AdjustPaintRect(float positionX,float positionY,float width,float height,bool isRound)2048 RectF XComponentPattern::AdjustPaintRect(float positionX, float positionY, float width, float height, bool isRound)
2049 {
2050     RectF rect;
2051     float relativeLeft = positionX;
2052     float relativeTop = positionY;
2053     float nodeWidth = width;
2054     float nodeHeight = height;
2055     float absoluteRight = relativeLeft + nodeWidth;
2056     float absoluteBottom = relativeTop + nodeHeight;
2057     float roundToPixelErrorX = 0;
2058     float roundToPixelErrorY = 0;
2059 
2060     float nodeLeftI = RoundValueToPixelGrid(relativeLeft, isRound, false, false);
2061     float nodeTopI = RoundValueToPixelGrid(relativeTop, isRound, false, false);
2062     roundToPixelErrorX += nodeLeftI - relativeLeft;
2063     roundToPixelErrorY += nodeTopI - relativeTop;
2064     rect.SetLeft(nodeLeftI);
2065     rect.SetTop(nodeTopI);
2066 
2067     float nodeWidthI = RoundValueToPixelGrid(absoluteRight, isRound, false, false) - nodeLeftI;
2068     float nodeWidthTemp = RoundValueToPixelGrid(nodeWidth, isRound, false, false);
2069     roundToPixelErrorX += nodeWidthI - nodeWidth;
2070     if (roundToPixelErrorX > 0.5f) {
2071         nodeWidthI -= 1.0f;
2072         roundToPixelErrorX -= 1.0f;
2073     }
2074     if (roundToPixelErrorX < -0.5f) {
2075         nodeWidthI += 1.0f;
2076         roundToPixelErrorX += 1.0f;
2077     }
2078     if (nodeWidthI < nodeWidthTemp) {
2079         roundToPixelErrorX += nodeWidthTemp - nodeWidthI;
2080         nodeWidthI = nodeWidthTemp;
2081     }
2082 
2083     float nodeHeightI = RoundValueToPixelGrid(absoluteBottom, isRound, false, false) - nodeTopI;
2084     float nodeHeightTemp = RoundValueToPixelGrid(nodeHeight, isRound, false, false);
2085     roundToPixelErrorY += nodeHeightI - nodeHeight;
2086     if (roundToPixelErrorY > 0.5f) {
2087         nodeHeightI -= 1.0f;
2088         roundToPixelErrorY -= 1.0f;
2089     }
2090     if (roundToPixelErrorY < -0.5f) {
2091         nodeHeightI += 1.0f;
2092         roundToPixelErrorY += 1.0f;
2093     }
2094     if (nodeHeightI < nodeHeightTemp) {
2095         roundToPixelErrorY += nodeHeightTemp - nodeHeightI;
2096         nodeHeightI = nodeHeightTemp;
2097     }
2098 
2099     rect.SetWidth(nodeWidthI);
2100     rect.SetHeight(nodeHeightI);
2101     return rect;
2102 }
2103 
RoundValueToPixelGrid(float value,bool isRound,bool forceCeil,bool forceFloor)2104 float XComponentPattern::RoundValueToPixelGrid(float value, bool isRound, bool forceCeil, bool forceFloor)
2105 {
2106     float fractials = fmod(value, 1.0f);
2107     if (fractials < 0.0f) {
2108         ++fractials;
2109     }
2110     if (forceCeil) {
2111         return (value - fractials + 1.0f);
2112     } else if (forceFloor) {
2113         return (value - fractials);
2114     } else if (isRound) {
2115         if (NearEqual(fractials, 1.0f) || GreatOrEqual(fractials, 0.50f)) {
2116             return (value - fractials + 1.0f);
2117         } else {
2118             return (value - fractials);
2119         }
2120     }
2121     return value;
2122 }
2123 
SetSurfaceRotation(bool isLock)2124 void XComponentPattern::SetSurfaceRotation(bool isLock)
2125 {
2126     if (type_ != XComponentType::SURFACE) {
2127         return;
2128     }
2129     isSurfaceLock_ = isLock;
2130 
2131     CHECK_NULL_VOID(handlingSurfaceRenderContext_);
2132     handlingSurfaceRenderContext_->SetSurfaceRotation(isLock);
2133 }
2134 
DumpInfo(std::unique_ptr<JsonValue> & json)2135 void XComponentPattern::DumpInfo(std::unique_ptr<JsonValue>& json)
2136 {
2137     json->Put("xcomponentId", id_.value_or("no id").c_str());
2138     json->Put("xcomponentType", XComponentTypeToString(type_).c_str());
2139     json->Put("libraryName", libraryname_.value_or("no library name").c_str());
2140     json->Put("surfaceId", surfaceId_.c_str());
2141     json->Put("surfaceRect", paintRect_.ToString().c_str());
2142 }
2143 
DumpAdvanceInfo(std::unique_ptr<JsonValue> & json)2144 void XComponentPattern::DumpAdvanceInfo(std::unique_ptr<JsonValue>& json)
2145 {
2146     if (renderSurface_) {
2147         renderSurface_->DumpInfo(json);
2148     }
2149 }
2150 
SetRenderFit(RenderFit renderFit)2151 void XComponentPattern::SetRenderFit(RenderFit renderFit)
2152 {
2153     CHECK_NULL_VOID(handlingSurfaceRenderContext_);
2154     handlingSurfaceRenderContext_->SetRenderFit(renderFit);
2155 }
2156 
SetScreenId(uint64_t screenId)2157 void XComponentPattern::SetScreenId(uint64_t screenId)
2158 {
2159     screenId_ = screenId;
2160     renderContextForSurface_->SetScreenId(screenId);
2161     surfaceId_ = "";
2162     xcomponentController_->SetSurfaceId(surfaceId_);
2163 }
2164 
EnableSecure(bool isSecure)2165 void XComponentPattern::EnableSecure(bool isSecure)
2166 {
2167     if (type_ != XComponentType::SURFACE) {
2168         return;
2169     }
2170     CHECK_NULL_VOID(renderContextForSurface_);
2171     renderContextForSurface_->SetSecurityLayer(isSecure);
2172     isEnableSecure_ = isSecure;
2173 }
2174 
HdrBrightness(float hdrBrightness)2175 void XComponentPattern::HdrBrightness(float hdrBrightness)
2176 {
2177     if (type_ != XComponentType::SURFACE) {
2178         return;
2179     }
2180     CHECK_NULL_VOID(renderContextForSurface_);
2181     renderContextForSurface_->SetHDRBrightness(std::clamp(hdrBrightness, 0.0f, 1.0f));
2182     hdrBrightness_ = std::clamp(hdrBrightness, 0.0f, 1.0f);
2183 }
2184 
EnableTransparentLayer(bool isTransparentLayer)2185 void XComponentPattern::EnableTransparentLayer(bool isTransparentLayer)
2186 {
2187     if (type_ != XComponentType::SURFACE) {
2188         return;
2189     }
2190     CHECK_NULL_VOID(renderContextForSurface_);
2191     renderContextForSurface_->SetTransparentLayer(isTransparentLayer);
2192     isTransparentLayer_ = isTransparentLayer;
2193 }
2194 
GetSurfaceRenderFit() const2195 RenderFit XComponentPattern::GetSurfaceRenderFit() const
2196 {
2197     CHECK_NULL_RETURN(handlingSurfaceRenderContext_, RenderFit::RESIZE_FILL);
2198     return handlingSurfaceRenderContext_->GetRenderFit().value_or(RenderFit::RESIZE_FILL);
2199 }
2200 
GetEnableAnalyzer()2201 bool XComponentPattern::GetEnableAnalyzer()
2202 {
2203     return isEnableAnalyzer_;
2204 }
2205 
NativeStartImageAnalyzer(std::function<void (int32_t)> & callback)2206 void XComponentPattern::NativeStartImageAnalyzer(std::function<void(int32_t)>& callback)
2207 {
2208     CHECK_NULL_VOID(callback);
2209     if (!isOnTree_ || !isEnableAnalyzer_) {
2210         return callback(ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_DISABLED);
2211     }
2212     if (!IsSupportImageAnalyzerFeature()) {
2213         return callback(ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_UNSUPPORTED);
2214     }
2215     if (isNativeImageAnalyzing_) {
2216         return callback(ArkUI_XComponent_ImageAnalyzerState::ARKUI_XCOMPONENT_AI_ANALYSIS_ONGOING);
2217     }
2218     isNativeImageAnalyzing_ = true;
2219 
2220     OnAnalyzedCallback nativeOnAnalyzed = [weak = WeakClaim(this),
2221         callback](ImageAnalyzerState state) {
2222         CHECK_NULL_VOID(callback);
2223         auto pattern = weak.Upgrade();
2224         CHECK_NULL_VOID(pattern);
2225         auto statusCode = ConvertNativeXComponentAnalyzerStatus(state);
2226         callback(statusCode);
2227         pattern->isNativeImageAnalyzing_ = false;
2228     };
2229 
2230     CHECK_NULL_VOID(imageAnalyzerManager_);
2231     imageAnalyzerManager_->SetImageAnalyzerCallback(nativeOnAnalyzed);
2232 
2233     auto host = GetHost();
2234     CHECK_NULL_VOID(host);
2235     auto* context = host->GetContext();
2236     CHECK_NULL_VOID(context);
2237     auto uiTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::UI);
2238     uiTaskExecutor.PostTask(
2239         [weak = WeakClaim(this)] {
2240             auto pattern = weak.Upgrade();
2241             CHECK_NULL_VOID(pattern);
2242             pattern->CreateAnalyzerOverlay();
2243         },
2244         "ArkUIXComponentCreateAnalyzerOverlay", PriorityType::VIP);
2245 }
2246 } // namespace OHOS::Ace::NG
2247