• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_v2.h"
17 
18 #include "base/log/dump_log.h"
19 #include "base/utils/multi_thread.h"
20 #include "base/utils/utils.h"
21 #include "core/accessibility/accessibility_session_adapter.h"
22 #include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_child_tree_callback.h"
23 #include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_session_adapter.h"
24 #include "core/components_ng/pattern/xcomponent/xcomponent_ext_surface_callback_client.h"
25 #include "core/components_ng/pattern/xcomponent/xcomponent_inner_surface_controller.h"
26 #ifdef ENABLE_ROSEN_BACKEND
27 #include "transaction/rs_transaction.h"
28 #include "transaction/rs_transaction_handler.h"
29 #include "transaction/rs_transaction_proxy.h"
30 #include "transaction/rs_sync_transaction_controller.h"
31 #include "transaction/rs_sync_transaction_handler.h"
32 #include "ui/rs_ui_context.h"
33 #include "ui/rs_ui_director.h"
34 #endif
35 
36 namespace OHOS::Ace::NG {
37 
38 namespace {
39 const std::string BUFFER_USAGE_XCOMPONENT = "xcomponent";
40 
BoolToString(bool value)41 inline std::string BoolToString(bool value)
42 {
43     return value ? "true" : "false";
44 }
45 } // namespace
46 
XComponentPatternV2(XComponentType type,XComponentNodeType nodeType)47 XComponentPatternV2::XComponentPatternV2(XComponentType type, XComponentNodeType nodeType)
48     : XComponentPattern((nodeType == XComponentNodeType::CNODE) ? std::make_optional<std::string>("") : std::nullopt,
49           type, (nodeType == XComponentNodeType::CNODE) ? std::make_optional<std::string>("") : std::nullopt, nullptr)
50 {
51     nodeType_ = nodeType;
52     if (nodeType == XComponentNodeType::CNODE) {
53         isCNode_ = true;
54     }
55 }
56 
SetSurfaceHolder(OH_ArkUI_SurfaceHolder * surfaceHolder)57 void XComponentPatternV2::SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder)
58 {
59     surfaceHolder_ = surfaceHolder;
60     if (surfaceHolder_) {
61         surfaceHolder_->nativeWindow_ = reinterpret_cast<OHNativeWindow*>(nativeWindow_);
62         hasGotSurfaceHolder_ = true;
63     }
64 }
65 
GetSurfaceHolder()66 OH_ArkUI_SurfaceHolder* XComponentPatternV2::GetSurfaceHolder()
67 {
68     return surfaceHolder_;
69 }
70 
OnAttachToFrameNode()71 void XComponentPatternV2::OnAttachToFrameNode()
72 {
73     if (isCNode_) {
74         XComponentPattern::OnAttachToFrameNode();
75         return;
76     }
77     auto host = GetHost();
78     CHECK_NULL_VOID(host);
79     auto renderContext = host->GetRenderContext();
80     CHECK_NULL_VOID(renderContext);
81 
82     renderContext->SetClipToFrame(true);
83     renderContext->SetClipToBounds(true);
84     InitSurface();
85     if (FrameReport::GetInstance().GetEnable()) {
86         FrameReport::GetInstance().EnableSelfRender();
87     }
88     UpdateTransformHint();
89 }
90 
OnAttachToMainTree()91 void XComponentPatternV2::OnAttachToMainTree()
92 {
93     UpdateUsesSuperMethod();
94     if (usesSuperMethod_) {
95         XComponentPattern::OnAttachToMainTree();
96         return;
97     }
98     auto host = GetHost();
99     THREAD_SAFE_NODE_CHECK(host, OnAttachToMainTree, host);
100     isOnTree_ = true;
101     if (autoInitialize_) {
102         HandleSurfaceCreated();
103     }
104     if (needRecoverDisplaySync_ && displaySync_ && !displaySync_->IsOnPipeline()) {
105         TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "OnAttachToMainTree:recover displaySync: "
106             "%{public}s(%{public}" PRIu64 ")", GetId().c_str(), displaySync_->GetId());
107         displaySync_->AddToPipelineOnContainer();
108         needRecoverDisplaySync_ = false;
109     }
110     displaySync_->NotifyXComponentExpectedFrameRate(GetId());
111 }
112 
BeforeSyncGeometryProperties(const DirtySwapConfig & config)113 void XComponentPatternV2::BeforeSyncGeometryProperties(const DirtySwapConfig& config)
114 {
115     UpdateUsesSuperMethod();
116     if (usesSuperMethod_) {
117         XComponentPattern::BeforeSyncGeometryProperties(config);
118         return;
119     }
120     CHECK_EQUAL_VOID(config.skipMeasure, true);
121     auto host = GetHost();
122     CHECK_NULL_VOID(host);
123     auto geometryNode = host->GetGeometryNode();
124     CHECK_NULL_VOID(geometryNode);
125     drawSize_ = geometryNode->GetContentSize();
126     if (!drawSize_.IsPositive()) {
127         TAG_LOGW(
128             AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s]'s size is not positive", GetId().c_str());
129         return;
130     }
131     localPosition_ = geometryNode->GetContentOffset();
132     if (IsSupportImageAnalyzerFeature()) {
133         UpdateAnalyzerUIConfig(geometryNode);
134     }
135     const auto& [offsetChanged, sizeChanged] = UpdateSurfaceRect();
136     HandleSurfaceChangeEvent(offsetChanged, sizeChanged, config.frameOffsetChange);
137     AddAfterLayoutTaskForExportTexture();
138     host->MarkNeedSyncRenderTree();
139 }
140 
UpdateSurfaceRect()141 std::pair<bool, bool> XComponentPatternV2::UpdateSurfaceRect()
142 {
143     if (!drawSize_.IsPositive()) {
144         return { false, false };
145     }
146     auto preSurfaceSize = surfaceSize_;
147     auto preSurfaceOffset = surfaceOffset_;
148 
149     surfaceSize_ = drawSize_;
150     surfaceOffset_ = localPosition_;
151     auto offsetChanged = preSurfaceOffset != surfaceOffset_;
152     auto sizeChanged = preSurfaceSize != surfaceSize_;
153     if (offsetChanged || sizeChanged) {
154         paintRect_ = AdjustPaintRect(
155             surfaceOffset_.GetX(), surfaceOffset_.GetY(), surfaceSize_.Width(), surfaceSize_.Height(), true);
156     }
157     return { offsetChanged, sizeChanged };
158 }
159 
HandleSurfaceChangeEvent(bool offsetChanged,bool sizeChanged,bool frameOffsetChange)160 void XComponentPatternV2::HandleSurfaceChangeEvent(bool offsetChanged, bool sizeChanged, bool frameOffsetChange)
161 {
162     if (!drawSize_.IsPositive()) {
163         return;
164     }
165     if (sizeChanged) {
166         XComponentSizeChange(paintRect_);
167     }
168     if (renderContextForSurface_) {
169         renderContextForSurface_->SetBounds(
170             paintRect_.GetX(), paintRect_.GetY(), paintRect_.Width(), paintRect_.Height());
171     }
172     if (renderSurface_) {
173         renderSurface_->SetSurfaceDefaultSize(
174             static_cast<int32_t>(paintRect_.Width()), static_cast<int32_t>(paintRect_.Height()));
175     }
176 }
177 
XComponentSizeChange(const RectF & surfaceRect)178 void XComponentPatternV2::XComponentSizeChange(const RectF& surfaceRect)
179 {
180     auto host = GetHost();
181     CHECK_NULL_VOID(host);
182     auto context = host->GetContextRefPtr();
183     CHECK_NULL_VOID(context);
184     auto viewScale = context->GetViewScale();
185     CHECK_NULL_VOID(renderSurface_);
186     auto width = surfaceRect.Width();
187     auto height = surfaceRect.Height();
188     needNotifySizeChanged_ = true;
189     renderSurface_->UpdateSurfaceSizeInUserData(
190         static_cast<uint32_t>(width), static_cast<uint32_t>(height));
191     renderSurface_->AdjustNativeWindowSize(
192         static_cast<uint32_t>(width * viewScale), static_cast<uint32_t>(height * viewScale));
193     OnSurfaceChanged(surfaceRect);
194 }
195 
OnSurfaceChanged(const RectF & surfaceRect)196 void XComponentPatternV2::OnSurfaceChanged(const RectF& surfaceRect)
197 {
198     CHECK_RUN_ON(UI);
199     CHECK_NULL_VOID(surfaceHolder_);
200     CHECK_EQUAL_VOID(isInitialized_, false);
201     ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceChanged", GetId().c_str());
202     auto callbackList = surfaceHolder_->surfaceCallbackList_;
203     for (const auto& iter : callbackList) {
204         auto callback = iter->OnSurfaceChanged;
205         if (callback) {
206             callback(surfaceHolder_, surfaceRect.Width(), surfaceRect.Height());
207         }
208     }
209     needNotifySizeChanged_ = false;
210 }
211 
OnDetachFromMainTree()212 void XComponentPatternV2::OnDetachFromMainTree()
213 {
214     if (usesSuperMethod_) {
215         XComponentPattern::OnDetachFromMainTree();
216         return;
217     }
218     auto host = GetHost();
219     THREAD_SAFE_NODE_CHECK(host, OnDetachFromMainTree, host);
220     isOnTree_ = false;
221     if (autoInitialize_) {
222         HandleSurfaceDestroyed();
223     }
224     if (displaySync_ && displaySync_->IsOnPipeline()) {
225         TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "OnDetachFromMainTree:remove displaySync: "
226             "%{public}s(%{public}" PRIu64 ")", GetId().c_str(), displaySync_->GetId());
227         displaySync_->DelFromPipelineOnContainer();
228         needRecoverDisplaySync_ = true;
229     }
230     displaySync_->NotifyXComponentExpectedFrameRate(GetId(), 0);
231 }
232 
OnDetachFromFrameNode(FrameNode * frameNode)233 void XComponentPatternV2::OnDetachFromFrameNode(FrameNode* frameNode)
234 {
235     CHECK_NULL_VOID(frameNode);
236     UpdateUsesSuperMethod();
237     if (usesSuperMethod_) {
238         XComponentPattern::OnDetachFromFrameNode(frameNode);
239         return;
240     }
241     THREAD_SAFE_NODE_CHECK(frameNode, OnDetachFromFrameNode);
242     auto id = frameNode->GetId();
243     auto pipeline = frameNode->GetContextRefPtr();
244     CHECK_NULL_VOID(pipeline);
245     pipeline->RemoveWindowStateChangedCallback(id);
246     if (HasTransformHintChangedCallbackId()) {
247         pipeline->UnregisterTransformHintChangedCallback(transformHintChangedCallbackId_.value_or(-1));
248     }
249     if (FrameReport::GetInstance().GetEnable()) {
250         FrameReport::GetInstance().DisableSelfRender();
251     }
252     HandleSurfaceDestroyed();
253 
254     CHECK_NULL_VOID(surfaceHolder_);
255     surfaceHolder_->nativeWindow_ = nullptr;
256     surfaceHolder_->node_ = nullptr;
257 }
258 
InitSurface()259 void XComponentPatternV2::InitSurface()
260 {
261     if (renderSurface_) {
262         return;
263     }
264     auto host = GetHost();
265     CHECK_NULL_VOID(host);
266     FREE_NODE_CHECK(host, InitSurface, host);
267     auto renderContext = host->GetRenderContext();
268     CHECK_NULL_VOID(renderContext);
269 
270     renderSurface_ = RenderSurface::Create();
271     renderSurface_->SetInstanceId(GetHostInstanceId());
272     std::string xComponentType = GetType() == XComponentType::SURFACE ? "s" : "t";
273     renderSurface_->SetBufferUsage(BUFFER_USAGE_XCOMPONENT + "-" + xComponentType + "-" + GetId());
274     if (type_ == XComponentType::SURFACE) {
275         InitializeRenderContext();
276         renderSurface_->SetRenderContext(renderContextForSurface_);
277         renderContext->AddChild(renderContextForSurface_, 0);
278     } else if (type_ == XComponentType::TEXTURE) {
279         renderSurface_->SetRenderContext(renderContext);
280         renderSurface_->SetIsTexture(true);
281         renderContext->OnNodeNameUpdate(GetId());
282     }
283     renderSurface_->InitSurface();
284     renderSurface_->UpdateSurfaceConfig();
285     if (type_ == XComponentType::TEXTURE) {
286         renderSurface_->RegisterBufferCallback();
287     }
288     auto width = paintRect_.Width();
289     auto height = paintRect_.Height();
290     if (!paintRect_.IsEmpty()) {
291         renderSurface_->UpdateSurfaceSizeInUserData(
292             static_cast<uint32_t>(width), static_cast<uint32_t>(height));
293         renderSurface_->SetSurfaceDefaultSize(
294             static_cast<int32_t>(width), static_cast<int32_t>(height));
295     }
296     renderSurface_->RegisterSurface();
297     InitNativeWindow(width, height);
298     if (surfaceHolder_) {
299         surfaceHolder_->nativeWindow_ = reinterpret_cast<OHNativeWindow*>(nativeWindow_);
300     }
301     surfaceId_ = renderSurface_->GetUniqueId();
302     if (type_ == XComponentType::SURFACE) {
303         XComponentInnerSurfaceController::RegisterNode(surfaceId_, WeakPtr(host));
304     }
305 }
306 
DisposeSurface()307 void XComponentPatternV2::DisposeSurface()
308 {
309     if (type_ == XComponentType::SURFACE) {
310         XComponentInnerSurfaceController::UnregisterNode(surfaceId_);
311         surfaceId_ = "";
312     }
313     if (renderSurface_) {
314         renderSurface_->ReleaseSurfaceBuffers();
315         renderSurface_->UnregisterSurface();
316         renderSurface_ = nullptr;
317     }
318     if (surfaceHolder_) {
319         surfaceHolder_->nativeWindow_ = nullptr;
320     }
321     nativeWindow_ = nullptr;
322     auto host = GetHost();
323     CHECK_NULL_VOID(host);
324     auto renderContext = host->GetRenderContext();
325     CHECK_NULL_VOID(renderContext);
326     // for surface type
327     CHECK_NULL_VOID(renderContextForSurface_);
328     renderContext->RemoveChild(renderContextForSurface_);
329     renderContextForSurface_ = nullptr;
330 #ifdef ENABLE_ROSEN_BACKEND
331     FlushImplicitTransaction(host);
332 #endif
333 }
334 
GetRSTransactionHandler(const RefPtr<FrameNode> & frameNode)335 std::shared_ptr<Rosen::RSTransactionHandler> XComponentPatternV2::GetRSTransactionHandler(
336     const RefPtr<FrameNode>& frameNode)
337 {
338 #ifdef ENABLE_ROSEN_BACKEND
339     if (!SystemProperties::GetMultiInstanceEnabled()) {
340         return nullptr;
341     }
342     auto rsUIContext = GetRSUIContext(frameNode);
343     CHECK_NULL_RETURN(rsUIContext, nullptr);
344     return rsUIContext->GetRSTransaction();
345 #endif
346     return nullptr;
347 }
348 
GetRSUIContext(const RefPtr<FrameNode> & frameNode)349 std::shared_ptr<Rosen::RSUIContext> XComponentPatternV2::GetRSUIContext(const RefPtr<FrameNode>& frameNode)
350 {
351 #ifdef ENABLE_ROSEN_BACKEND
352     CHECK_NULL_RETURN(frameNode, nullptr);
353     auto pipeline = frameNode->GetContext();
354     CHECK_NULL_RETURN(pipeline, nullptr);
355     auto window = pipeline->GetWindow();
356     CHECK_NULL_RETURN(window, nullptr);
357     auto rsUIDirector = window->GetRSUIDirector();
358     CHECK_NULL_RETURN(rsUIDirector, nullptr);
359     auto rsUIContext = rsUIDirector->GetRSUIContext();
360     return rsUIContext;
361 #endif
362     return nullptr;
363 }
364 
FlushImplicitTransaction(const RefPtr<FrameNode> & frameNode)365 void XComponentPatternV2::FlushImplicitTransaction(const RefPtr<FrameNode>& frameNode)
366 {
367 #ifdef ENABLE_ROSEN_BACKEND
368     if (auto transactionHandler = GetRSTransactionHandler(frameNode)) {
369         transactionHandler->FlushImplicitTransaction();
370     } else {
371         Rosen::RSTransactionProxy::GetInstance()->FlushImplicitTransaction();
372     }
373 #endif
374 }
375 
HandleSurfaceCreated()376 int32_t XComponentPatternV2::HandleSurfaceCreated()
377 {
378     CHECK_EQUAL_RETURN(isInitialized_, true, ERROR_CODE_XCOMPONENT_STATE_INVALID);
379     InitSurface();
380     CHECK_NULL_RETURN(renderSurface_, ERROR_CODE_PARAM_INVALID);
381     isInitialized_ = true;
382     if (surfaceHolder_) {
383         auto callbackList = surfaceHolder_->surfaceCallbackList_;
384         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceCreated", GetId().c_str());
385         ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceCreated", GetId().c_str());
386         for (const auto& iter : callbackList) {
387             auto callback = iter->OnSurfaceCreated;
388             if (callback) {
389                 callback(surfaceHolder_);
390             }
391         }
392     }
393     if (needNotifySizeChanged_) {
394         OnSurfaceChanged(paintRect_);
395     }
396     return ERROR_CODE_NO_ERROR;
397 }
398 
HandleSurfaceDestroyed()399 int32_t XComponentPatternV2::HandleSurfaceDestroyed()
400 {
401     CHECK_EQUAL_RETURN(isInitialized_, false, ERROR_CODE_XCOMPONENT_STATE_INVALID);
402     isInitialized_ = false;
403     if (surfaceHolder_) {
404         auto callbackList = surfaceHolder_->surfaceCallbackList_;
405         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceDestroyed", GetId().c_str());
406         ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceDestroyed", GetId().c_str());
407         for (const auto& iter : callbackList) {
408             auto callback = iter->OnSurfaceDestroyed;
409             if (callback) {
410                 callback(surfaceHolder_);
411             }
412         }
413     }
414     DisposeSurface();
415     return ERROR_CODE_NO_ERROR;
416 }
417 
Initialize()418 int32_t XComponentPatternV2::Initialize()
419 {
420     CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID);
421     isLifecycleInterfaceCalled_ = true;
422     return HandleSurfaceCreated();
423 }
424 
Finalize()425 int32_t XComponentPatternV2::Finalize()
426 {
427     CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID);
428     isLifecycleInterfaceCalled_ = true;
429     return HandleSurfaceDestroyed();
430 }
431 
SetAutoInitialize(bool autoInitialize)432 int32_t XComponentPatternV2::SetAutoInitialize(bool autoInitialize)
433 {
434     CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID);
435     isLifecycleInterfaceCalled_ = true;
436     autoInitialize_ = autoInitialize;
437     return ERROR_CODE_NO_ERROR;
438 }
439 
IsInitialized(bool & isInitialized)440 int32_t XComponentPatternV2::IsInitialized(bool& isInitialized)
441 {
442     CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID);
443     isLifecycleInterfaceCalled_ = true;
444     isInitialized = isInitialized_;
445     return ERROR_CODE_NO_ERROR;
446 }
447 
OnWindowHide()448 void XComponentPatternV2::OnWindowHide()
449 {
450     if (usesSuperMethod_) {
451         XComponentPattern::OnWindowHide();
452         return;
453     }
454     CHECK_EQUAL_VOID(hasReleasedSurface_, true);
455     if (renderSurface_) {
456         renderSurface_->OnWindowStateChange(false);
457     }
458     OnSurfaceHide();
459     hasReleasedSurface_ = true;
460 }
461 
OnWindowShow()462 void XComponentPatternV2::OnWindowShow()
463 {
464     if (usesSuperMethod_) {
465         XComponentPattern::OnWindowShow();
466         return;
467     }
468     CHECK_EQUAL_VOID(hasReleasedSurface_, false);
469     if (renderSurface_) {
470         renderSurface_->OnWindowStateChange(true);
471     }
472     OnSurfaceShow();
473     hasReleasedSurface_ = false;
474 }
475 
OnRebuildFrame()476 void XComponentPatternV2::OnRebuildFrame()
477 {
478     if (usesSuperMethod_) {
479         XComponentPattern::OnRebuildFrame();
480         return;
481     }
482     if (type_ != XComponentType::SURFACE) {
483         return;
484     }
485     CHECK_NULL_VOID(renderSurface_);
486     if (!renderSurface_->IsSurfaceValid()) {
487         return;
488     }
489     auto host = GetHost();
490     CHECK_NULL_VOID(host);
491     auto renderContext = host->GetRenderContext();
492     CHECK_NULL_VOID(renderContext);
493     CHECK_NULL_VOID(renderContextForSurface_);
494     renderContext->AddChild(renderContextForSurface_, 0);
495 }
496 
InitializeRenderContext()497 void XComponentPatternV2::InitializeRenderContext()
498 {
499     if (renderContextForSurface_) {
500         return;
501     }
502     renderContextForSurface_ = RenderContext::Create();
503     RenderContext::ContextParam param = { RenderContext::ContextType::HARDWARE_SURFACE, GetId() + "Surface",
504         RenderContext::PatternType::XCOM };
505     renderContextForSurface_->InitContext(false, param);
506     if (!paintRect_.IsEmpty()) {
507         renderContextForSurface_->SetBounds(
508             paintRect_.GetX(), paintRect_.GetY(), paintRect_.Width(), paintRect_.Height());
509     }
510     renderContextForSurface_->UpdateBackgroundColor(bkColor_);
511     renderContextForSurface_->SetHDRBrightness(hdrBrightness_);
512     renderContextForSurface_->SetTransparentLayer(isTransparentLayer_);
513     renderContextForSurface_->SetSecurityLayer(isEnableSecure_);
514     renderContextForSurface_->SetSurfaceRotation(isSurfaceLock_);
515     renderContextForSurface_->SetRenderFit(renderFit_);
516 }
517 
OnModifyDone()518 void XComponentPatternV2::OnModifyDone()
519 {
520     if (usesSuperMethod_) {
521         XComponentPattern::OnModifyDone();
522         return;
523     }
524     Pattern::OnModifyDone();
525     auto host = GetHost();
526     CHECK_NULL_VOID(host);
527     auto renderContext = host->GetRenderContext();
528     CHECK_NULL_VOID(renderContext);
529     CHECK_NULL_VOID(renderContextForSurface_);
530     auto bkColor = renderContext->GetBackgroundColor();
531     if (bkColor.has_value()) {
532         bool isTransparent = bkColor.value().GetAlpha() < UINT8_MAX;
533         bkColor_ = isTransparent ? Color::TRANSPARENT : bkColor.value();
534     }
535     renderContextForSurface_->UpdateBackgroundColor(bkColor_);
536 }
537 
DumpInfo()538 void XComponentPatternV2::DumpInfo()
539 {
540     if (usesSuperMethod_) {
541         XComponentPattern::DumpInfo();
542         return;
543     }
544     DumpLog::GetInstance().AddDesc(std::string("autoInitialize: ").append(BoolToString(autoInitialize_)));
545     DumpLog::GetInstance().AddDesc(std::string("isInitialized: ").append(BoolToString(isInitialized_)));
546     DumpLog::GetInstance().AddDesc(
547         std::string("xcomponentNodeType: ").append(XComponentPattern::XComponentNodeTypeToString(nodeType_)));
548     DumpLog::GetInstance().AddDesc(
549         std::string("xcomponentType: ").append(XComponentPattern::XComponentTypeToString(type_)));
550     DumpLog::GetInstance().AddDesc(std::string("surfaceId: ").append(surfaceId_));
551     DumpLog::GetInstance().AddDesc(std::string("surfaceRect: ").append(paintRect_.ToString()));
552 }
553 
SetExpectedRateRange(int32_t min,int32_t max,int32_t expected)554 void XComponentPatternV2::SetExpectedRateRange(int32_t min, int32_t max, int32_t expected)
555 {
556     if (hasGotNativeXComponent_) {
557         return;
558     }
559     isNativeXComponentDisabled_ = true;
560     CHECK_NULL_VOID(displaySync_);
561     FrameRateRange frameRateRange;
562     frameRateRange.Set(min, max, expected);
563     displaySync_->NotifyXComponentExpectedFrameRate(GetId(), isOnTree_, frameRateRange);
564     TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " NotifyXComponentExpectedFrameRate"
565         "{%{public}d, %{public}d, %{public}d}", displaySync_->GetId(), min, max, expected);
566 }
567 
UpdateOnFrameEvent(void (* callback)(void *,uint64_t,uint64_t),void * arkuiNode)568 void XComponentPatternV2::UpdateOnFrameEvent(void(*callback)(void*, uint64_t, uint64_t), void* arkuiNode)
569 {
570     if (hasGotNativeXComponent_) {
571         return;
572     }
573     isNativeXComponentDisabled_ = true;
574     CHECK_NULL_VOID(displaySync_);
575     displaySync_->RegisterOnFrameWithData([weak = AceType::WeakClaim(this),
576         callback, arkuiNode](RefPtr<DisplaySyncData> displaySyncData) {
577         auto xComponentPattern = weak.Upgrade();
578         CHECK_NULL_VOID(xComponentPattern);
579         CHECK_NULL_VOID(callback);
580         CHECK_NULL_VOID(arkuiNode);
581         CHECK_NULL_VOID(displaySyncData);
582         callback(arkuiNode, displaySyncData->GetTimestamp(), displaySyncData->GetTargetTimestamp());
583     });
584     TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " RegisterOnFrame",
585         displaySync_->GetId());
586     displaySync_->AddToPipelineOnContainer();
587 }
588 
UnregisterOnFrameEvent()589 void XComponentPatternV2::UnregisterOnFrameEvent()
590 {
591     if (hasGotNativeXComponent_) {
592         return;
593     }
594     isNativeXComponentDisabled_ = true;
595     CHECK_NULL_VOID(displaySync_);
596     displaySync_->UnregisterOnFrame();
597     TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " UnregisterOnFrame",
598         displaySync_->GetId());
599     displaySync_->DelFromPipelineOnContainer();
600     needRecoverDisplaySync_ = false;
601 }
602 
SetNeedSoftKeyboard(bool isNeedSoftKeyboard)603 void XComponentPatternV2::SetNeedSoftKeyboard(bool isNeedSoftKeyboard)
604 {
605     if (hasGotNativeXComponent_) {
606         return;
607     }
608     isNativeXComponentDisabled_ = true;
609     isNeedSoftKeyboard_ = isNeedSoftKeyboard;
610 }
611 
OnSurfaceShow()612 void XComponentPatternV2::OnSurfaceShow()
613 {
614     CHECK_RUN_ON(UI);
615     CHECK_NULL_VOID(surfaceHolder_);
616     auto callbackList = surfaceHolder_->surfaceCallbackList_;
617     for (const auto& iter : callbackList) {
618         auto callback = iter->onSurfaceShow;
619         if (callback) {
620             callback(surfaceHolder_);
621         }
622     }
623 }
624 
OnSurfaceHide()625 void XComponentPatternV2::OnSurfaceHide()
626 {
627     CHECK_RUN_ON(UI);
628     CHECK_NULL_VOID(surfaceHolder_);
629     auto callbackList = surfaceHolder_->surfaceCallbackList_;
630     bool hasCallback = false;
631     for (const auto& iter : callbackList) {
632         auto callback = iter->onSurfaceHide;
633         if (callback) {
634             callback(surfaceHolder_);
635             hasCallback = true;
636         }
637     }
638     if (hasCallback) {
639         CHECK_NULL_VOID(renderSurface_);
640         renderSurface_->ReleaseSurfaceBuffers();
641     }
642 }
643 
ResetAndInitializeNodeHandleAccessibility()644 void XComponentPatternV2::ResetAndInitializeNodeHandleAccessibility()
645 {
646     auto host = GetHost();
647     CHECK_NULL_VOID(host);
648     auto pipeline = host->GetContextRefPtr();
649     CHECK_NULL_VOID(pipeline);
650     auto accessibilityManager = pipeline->GetAccessibilityManager();
651     CHECK_NULL_VOID(accessibilityManager);
652     if (accessibilityChildTreeCallback_) {
653         accessibilityManager->DeregisterAccessibilityChildTreeCallback(
654             accessibilityChildTreeCallback_->GetAccessibilityId()
655         );
656         accessibilityChildTreeCallback_.reset();
657         accessibilityChildTreeCallback_ = nullptr;
658     }
659 
660     TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "InitializeNodeHandleAccessibility");
661     CHECK_NULL_VOID(arkuiAccessibilityProvider_);
662     arkuiAccessibilityProvider_->SetRegisterCallback(
663         [weak = WeakClaim(this)] (bool isRegister) {
664             auto pattern = weak.Upgrade();
665             CHECK_NULL_VOID(pattern);
666             pattern->HandleRegisterAccessibilityEvent(isRegister);
667         });
668     int64_t accessibilityId = host->GetAccessibilityId();
669     TAG_LOGI(AceLogTag::ACE_XCOMPONENT,
670         "InitializeNodeHandleAccessibility accessibilityId: %{public}" PRId64 "", accessibilityId);
671     accessibilityChildTreeCallback_ = std::make_shared<XComponentAccessibilityChildTreeCallback>(
672         WeakClaim(this), host->GetAccessibilityId());
673     accessibilityManager->RegisterAccessibilityChildTreeCallback(
674         accessibilityId, accessibilityChildTreeCallback_);
675     if (accessibilityManager->IsRegister()) {
676         accessibilityChildTreeCallback_->OnRegister(
677             pipeline->GetWindowId(), accessibilityManager->GetTreeId());
678     }
679 }
680 
CreateAccessibilityProvider()681 ArkUI_AccessibilityProvider* XComponentPatternV2::CreateAccessibilityProvider()
682 {
683     if (hasGotNativeXComponent_) {
684         return nullptr;
685     }
686     isNativeXComponentDisabled_ = true;
687     useNodeHandleAccessibilityProvider_ = true;
688     if (arkuiAccessibilityProvider_) {
689         return arkuiAccessibilityProvider_;
690     }
691     auto host = GetHost();
692     CHECK_NULL_RETURN(host, nullptr);
693     arkuiAccessibilityProvider_ = new ArkUI_AccessibilityProvider();
694     XComponentPatternV2::XComponentAccessibilityProviderMap[arkuiAccessibilityProvider_] = WeakPtr(host);
695     ResetAndInitializeNodeHandleAccessibility();
696     return arkuiAccessibilityProvider_;
697 }
698 
DisposeAccessibilityProvider(ArkUI_AccessibilityProvider * provider)699 void XComponentPatternV2::DisposeAccessibilityProvider(ArkUI_AccessibilityProvider* provider)
700 {
701     if (hasGotNativeXComponent_ || (provider != arkuiAccessibilityProvider_)) {
702         return;
703     }
704     CHECK_NULL_VOID(arkuiAccessibilityProvider_);
705     isNativeXComponentDisabled_ = true;
706     XComponentPatternV2::XComponentAccessibilityProviderMap.erase(arkuiAccessibilityProvider_);
707     auto host = GetHost();
708     UninitializeAccessibility(AceType::RawPtr(host));
709     delete arkuiAccessibilityProvider_;
710     arkuiAccessibilityProvider_ = nullptr;
711 }
712 
713 std::unordered_map<void*, WeakPtr<FrameNode>> XComponentPatternV2::XComponentAccessibilityProviderMap;
714 
QueryAccessibilityProviderHost(void * provider,bool & isProviderValied)715 FrameNode* XComponentPatternV2::QueryAccessibilityProviderHost(void* provider, bool& isProviderValied)
716 {
717     auto it = XComponentAccessibilityProviderMap.find(provider);
718     if (it == XComponentAccessibilityProviderMap.end()) {
719         isProviderValied = false;
720         return nullptr;
721     }
722     isProviderValied = true;
723     auto weakHost = it->second;
724     auto host = weakHost.Upgrade();
725     CHECK_NULL_RETURN(host, nullptr);
726     return AceType::RawPtr(host);
727 }
728 } // namespace OHOS::Ace::NG