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