1 /*
2 * Copyright (c) 2022-2023 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 "base/geometry/ng/size_t.h"
19 #include "base/log/log_wrapper.h"
20 #include "base/ressched/ressched_report.h"
21 #include "base/utils/system_properties.h"
22 #include "base/utils/utils.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/pattern/xcomponent/xcomponent_controller_ng.h"
25 #ifdef NG_BUILD
26 #include "bridge/declarative_frontend/ng/declarative_frontend_ng.h"
27 #else
28 #include "bridge/declarative_frontend/declarative_frontend.h"
29 #endif
30 #ifdef ENABLE_ROSEN_BACKEND
31 #include "transaction/rs_transaction_proxy.h"
32 #endif
33
34 #include "core/components_ng/event/input_event.h"
35 #include "core/components_ng/pattern/xcomponent/xcomponent_event_hub.h"
36 #include "core/components_ng/pattern/xcomponent/xcomponent_ext_surface_callback_client.h"
37 #include "core/event/key_event.h"
38 #include "core/event/mouse_event.h"
39 #include "core/event/touch_event.h"
40 #include "core/pipeline_ng/pipeline_context.h"
41
42 namespace OHOS::Ace::NG {
43 namespace {
44 #ifdef OHOS_PLATFORM
45 constexpr int64_t INCREASE_CPU_TIME_ONCE = 4000000000; // 4s(unit: ns)
46 #endif
ConvertNativeXComponentTouchEvent(const TouchType & touchType)47 OH_NativeXComponent_TouchEventType ConvertNativeXComponentTouchEvent(const TouchType& touchType)
48 {
49 switch (touchType) {
50 case TouchType::DOWN:
51 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_DOWN;
52 case TouchType::UP:
53 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UP;
54 case TouchType::MOVE:
55 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_MOVE;
56 case TouchType::CANCEL:
57 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_CANCEL;
58 default:
59 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UNKNOWN;
60 }
61 }
62
ConvertNativeXComponentTouchToolType(const SourceTool & toolType)63 OH_NativeXComponent_TouchPointToolType ConvertNativeXComponentTouchToolType(const SourceTool& toolType)
64 {
65 switch (toolType) {
66 case SourceTool::FINGER:
67 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_FINGER;
68 case SourceTool::PEN:
69 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_PEN;
70 case SourceTool::RUBBER:
71 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_RUBBER;
72 case SourceTool::BRUSH:
73 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_BRUSH;
74 case SourceTool::PENCIL:
75 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_PENCIL;
76 case SourceTool::AIRBRUSH:
77 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_AIRBRUSH;
78 case SourceTool::MOUSE:
79 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_MOUSE;
80 case SourceTool::LENS:
81 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_LENS;
82 default:
83 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN;
84 }
85 }
86
ConvertNativeXComponentKeyAction(const KeyAction & keyAction)87 OH_NativeXComponent_KeyAction ConvertNativeXComponentKeyAction(const KeyAction& keyAction)
88 {
89 switch (keyAction) {
90 case KeyAction::DOWN:
91 return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_DOWN;
92 case KeyAction::UP:
93 return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UP;
94 default:
95 return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UNKNOWN;
96 }
97 }
98
ConvertNativeXComponentEventSourceType(const SourceType & sourceType)99 OH_NativeXComponent_EventSourceType ConvertNativeXComponentEventSourceType(const SourceType& sourceType)
100 {
101 switch (sourceType) {
102 case SourceType::MOUSE:
103 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_MOUSE;
104 case SourceType::TOUCH:
105 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_TOUCHSCREEN;
106 case SourceType::TOUCH_PAD:
107 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_TOUCHPAD;
108 case SourceType::KEYBOARD:
109 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_KEYBOARD;
110 default:
111 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_UNKNOWN;
112 }
113 }
114
ConvertNativeXComponentKeyEvent(const KeyEvent & event)115 OH_NativeXComponent_KeyEvent ConvertNativeXComponentKeyEvent(const KeyEvent& event)
116 {
117 OH_NativeXComponent_KeyEvent nativeKeyEvent;
118 nativeKeyEvent.action = ConvertNativeXComponentKeyAction(event.action);
119 nativeKeyEvent.code = static_cast<OH_NativeXComponent_KeyCode>(event.code);
120 nativeKeyEvent.sourceType = ConvertNativeXComponentEventSourceType(event.sourceType);
121 nativeKeyEvent.deviceId = event.deviceId;
122 nativeKeyEvent.timestamp = event.timeStamp.time_since_epoch().count();
123 return nativeKeyEvent;
124 }
125 } // namespace
126
XComponentPattern(const std::string & id,XComponentType type,const std::string & libraryname,const std::shared_ptr<InnerXComponentController> & xcomponentController,float initWidth,float initHeight)127 XComponentPattern::XComponentPattern(const std::string& id, XComponentType type, const std::string& libraryname,
128 const std::shared_ptr<InnerXComponentController>& xcomponentController, float initWidth, float initHeight)
129 : id_(id), type_(type), libraryname_(libraryname), xcomponentController_(xcomponentController)
130 {
131 initSize_.SetWidth(initWidth);
132 initSize_.SetHeight(initHeight);
133 }
134
Initialize(int32_t instanceId)135 void XComponentPattern::Initialize(int32_t instanceId)
136 {
137 auto host = GetHost();
138 CHECK_NULL_VOID(host);
139 instanceId_ = (instanceId <= 0) ? Container::CurrentId() : instanceId;
140 auto renderContext = host->GetRenderContext();
141 if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) {
142 renderContext->SetClipToFrame(true);
143 renderContext->SetClipToBounds(true);
144 renderSurface_ = RenderSurface::Create();
145 renderSurface_->SetInstanceId(instanceId_);
146 if (type_ == XComponentType::SURFACE) {
147 renderContextForSurface_ = RenderContext::Create();
148 static RenderContext::ContextParam param = { RenderContext::ContextType::HARDWARE_SURFACE,
149 id_ + "Surface" };
150 renderContextForSurface_->InitContext(false, param);
151 renderContextForSurface_->UpdateBackgroundColor(Color::BLACK);
152 if (!SystemProperties::GetExtSurfaceEnabled()) {
153 renderSurface_->SetRenderContext(renderContextForSurface_);
154 } else {
155 auto pipelineContext = PipelineContext::GetCurrentContext();
156 CHECK_NULL_VOID(pipelineContext);
157 pipelineContext->AddOnAreaChangeNode(host->GetId());
158 extSurfaceClient_ = MakeRefPtr<XComponentExtSurfaceCallbackClient>(WeakClaim(this));
159 renderSurface_->SetExtSurfaceCallback(extSurfaceClient_);
160 }
161 handlingSurfaceRenderContext_ = renderContextForSurface_;
162 auto* controllerNG = static_cast<XComponentControllerNG*>(xcomponentController_.get());
163 if (controllerNG) {
164 controllerNG->SetPattern(AceType::Claim(this));
165 }
166 } else if (type_ == XComponentType::TEXTURE) {
167 renderSurface_->SetRenderContext(renderContext);
168 renderSurface_->SetIsTexture(true);
169 }
170
171 renderSurface_->InitSurface();
172 renderSurface_->UpdateXComponentConfig();
173 InitEvent();
174 SetMethodCall();
175 } else if (type_ == XComponentType::NODE) {
176 auto context = PipelineContext::GetCurrentContext();
177 if (context) {
178 FireExternalEvent(context, id_, host->GetId(), false);
179 InitNativeNodeCallbacks();
180 }
181 }
182 }
183
OnAttachToFrameNode()184 void XComponentPattern::OnAttachToFrameNode()
185 {
186 instanceId_ = Container::CurrentId();
187 Initialize(instanceId_);
188 }
189
OnModifyDone()190 void XComponentPattern::OnModifyDone()
191 {
192 auto host = GetHost();
193 CHECK_NULL_VOID(host);
194 auto renderContext = host->GetRenderContext();
195 CHECK_NULL_VOID(renderContext);
196 auto bkColor = renderContext->GetBackgroundColor();
197 if (bkColor.has_value() && handlingSurfaceRenderContext_) {
198 handlingSurfaceRenderContext_->UpdateBackgroundColor(Color::TRANSPARENT);
199 }
200 }
201
OnAreaChangedInner()202 void XComponentPattern::OnAreaChangedInner()
203 {
204 if (SystemProperties::GetExtSurfaceEnabled()) {
205 auto host = GetHost();
206 CHECK_NULL_VOID(host);
207 auto geometryNode = host->GetGeometryNode();
208 CHECK_NULL_VOID(geometryNode);
209 auto xcomponentNodeSize = geometryNode->GetContentSize();
210 auto xcomponentNodeOffset = geometryNode->GetContentOffset();
211 auto transformRelativeOffset = host->GetTransformRelativeOffset();
212 renderSurface_->SetExtSurfaceBounds(transformRelativeOffset.GetX() + xcomponentNodeOffset.GetX(),
213 transformRelativeOffset.GetY() + xcomponentNodeOffset.GetY(), xcomponentNodeSize.Width(),
214 xcomponentNodeSize.Height());
215 }
216 }
217
OnRebuildFrame()218 void XComponentPattern::OnRebuildFrame()
219 {
220 if (type_ != XComponentType::SURFACE) {
221 return;
222 }
223 if (!renderSurface_->IsSurfaceValid()) {
224 return;
225 }
226 auto host = GetHost();
227 CHECK_NULL_VOID(host);
228 auto renderContext = host->GetRenderContext();
229 CHECK_NULL_VOID(renderContext);
230 CHECK_NULL_VOID(handlingSurfaceRenderContext_);
231 renderContext->AddChild(handlingSurfaceRenderContext_, 0);
232 }
233
OnDetachFromFrameNode(FrameNode * frameNode)234 void XComponentPattern::OnDetachFromFrameNode(FrameNode* frameNode)
235 {
236 CHECK_NULL_VOID(frameNode);
237 if (!hasXComponentInit_) {
238 return;
239 }
240 if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) {
241 NativeXComponentDestroy();
242 auto eventHub = frameNode->GetEventHub<XComponentEventHub>();
243 CHECK_NULL_VOID(eventHub);
244 eventHub->FireDestroyEvent();
245 eventHub->FireDetachEvent(id_);
246 }
247 }
248
SetMethodCall()249 void XComponentPattern::SetMethodCall()
250 {
251 CHECK_NULL_VOID(xcomponentController_);
252 auto pipelineContext = PipelineContext::GetCurrentContext();
253 CHECK_NULL_VOID(pipelineContext);
254 auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(), TaskExecutor::TaskType::UI);
255 xcomponentController_->SetConfigSurfaceImpl(
256 [weak = WeakClaim(this), uiTaskExecutor](uint32_t surfaceWidth, uint32_t surfaceHeight) {
257 uiTaskExecutor.PostSyncTask([weak, surfaceWidth, surfaceHeight]() {
258 auto pattern = weak.Upgrade();
259 CHECK_NULL_VOID(pattern);
260 pattern->ConfigSurface(surfaceWidth, surfaceHeight);
261 });
262 });
263
264 xcomponentController_->SetSurfaceId(renderSurface_->GetUniqueId());
265 }
266
ConfigSurface(uint32_t surfaceWidth,uint32_t surfaceHeight)267 void XComponentPattern::ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight)
268 {
269 renderSurface_->ConfigSurface(surfaceWidth, surfaceHeight);
270 }
271
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)272 bool XComponentPattern::OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config)
273 {
274 if (type_ == XComponentType::COMPONENT || type_ == XComponentType::NODE
275 || config.skipMeasure || dirty->SkipMeasureContent()) {
276 return false;
277 }
278 auto geometryNode = dirty->GetGeometryNode();
279 CHECK_NULL_RETURN(geometryNode, false);
280 drawSize_ = geometryNode->GetContentSize();
281 if (drawSize_.IsNonPositive()) {
282 return false;
283 }
284
285 if (!hasXComponentInit_) {
286 initSize_ = drawSize_;
287 globalPosition_ = geometryNode->GetContentOffset() + geometryNode->GetFrameOffset();
288 if (!SystemProperties::GetExtSurfaceEnabled()) {
289 XComponentSizeInit();
290 }
291 NativeXComponentOffset(globalPosition_.GetX(), globalPosition_.GetY());
292 hasXComponentInit_ = true;
293 } else {
294 if (config.frameOffsetChange || config.contentOffsetChange) {
295 globalPosition_ = geometryNode->GetContentOffset() + geometryNode->GetFrameOffset();
296 NativeXComponentOffset(globalPosition_.GetX(), globalPosition_.GetY());
297 }
298 if (config.contentSizeChange) {
299 if (!SystemProperties::GetExtSurfaceEnabled()) {
300 XComponentSizeChange(drawSize_.Width(), drawSize_.Height());
301 }
302 }
303 }
304 localposition_ = geometryNode->GetContentOffset();
305 if (SystemProperties::GetExtSurfaceEnabled()) {
306 auto host = GetHost();
307 CHECK_NULL_RETURN(host, false);
308 auto transformRelativeOffset = host->GetTransformRelativeOffset();
309 renderSurface_->SetExtSurfaceBounds(
310 static_cast<int32_t>(transformRelativeOffset.GetX() + localposition_.GetX()),
311 static_cast<int32_t>(transformRelativeOffset.GetY() + localposition_.GetY()),
312 static_cast<int32_t>(drawSize_.Width()), static_cast<int32_t>(drawSize_.Height()));
313 }
314 if (handlingSurfaceRenderContext_) {
315 handlingSurfaceRenderContext_->SetBounds(
316 localposition_.GetX(), localposition_.GetY(), drawSize_.Width(), drawSize_.Height());
317 #ifdef ENABLE_ROSEN_BACKEND
318 auto* transactionProxy = Rosen::RSTransactionProxy::GetInstance();
319 if (transactionProxy != nullptr) {
320 transactionProxy->FlushImplicitTransaction();
321 }
322 #endif
323 }
324 // XComponentType::SURFACE has set surface default size in RSSurfaceNode->SetBounds()
325 if (type_ == XComponentType::TEXTURE) {
326 renderSurface_->SetSurfaceDefaultSize(
327 static_cast<int32_t>(drawSize_.Width()), static_cast<int32_t>(drawSize_.Height()));
328 }
329 if (type_ == XComponentType::SURFACE && renderType_ == NodeRenderType::RENDER_TYPE_TEXTURE) {
330 AddAfterLayoutTaskForExportTexture();
331 }
332 auto host = GetHost();
333 CHECK_NULL_RETURN(host, false);
334 host->MarkNeedSyncRenderTree();
335 return false;
336 }
337
OnPaint()338 void XComponentPattern::OnPaint()
339 {
340 auto host = GetHost();
341 CHECK_NULL_VOID(host);
342 auto renderContext = host->GetRenderContext();
343 renderContext->UpdateBackgroundColor(Color::BLACK);
344 }
345
NativeXComponentChange(float width,float height)346 void XComponentPattern::NativeXComponentChange(float width, float height)
347 {
348 CHECK_RUN_ON(UI);
349 CHECK_NULL_VOID(nativeXComponent_);
350 CHECK_NULL_VOID(nativeXComponentImpl_);
351 nativeXComponentImpl_->SetXComponentWidth(static_cast<int32_t>(width));
352 nativeXComponentImpl_->SetXComponentHeight(static_cast<int32_t>(height));
353 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
354 const auto* callback = nativeXComponentImpl_->GetCallback();
355 CHECK_NULL_VOID(callback);
356 CHECK_NULL_VOID(callback->OnSurfaceChanged);
357 callback->OnSurfaceChanged(nativeXComponent_.get(), surface);
358 }
359
NativeXComponentDestroy()360 void XComponentPattern::NativeXComponentDestroy()
361 {
362 CHECK_RUN_ON(UI);
363 CHECK_NULL_VOID(nativeXComponent_);
364 CHECK_NULL_VOID(nativeXComponentImpl_);
365 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
366 const auto* callback = nativeXComponentImpl_->GetCallback();
367 CHECK_NULL_VOID(callback);
368 CHECK_NULL_VOID(callback->OnSurfaceDestroyed);
369 callback->OnSurfaceDestroyed(nativeXComponent_.get(), surface);
370 }
371
NativeXComponentOffset(double x,double y)372 void XComponentPattern::NativeXComponentOffset(double x, double y)
373 {
374 CHECK_RUN_ON(UI);
375 CHECK_NULL_VOID(nativeXComponent_);
376 CHECK_NULL_VOID(nativeXComponentImpl_);
377 auto pipelineContext = PipelineContext::GetCurrentContext();
378 CHECK_NULL_VOID(pipelineContext);
379 float scale = pipelineContext->GetViewScale();
380 nativeXComponentImpl_->SetXComponentOffsetX(x * scale);
381 nativeXComponentImpl_->SetXComponentOffsetY(y * scale);
382 }
383
NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent & touchEvent,const std::vector<XComponentTouchPoint> & xComponentTouchPoints)384 void XComponentPattern::NativeXComponentDispatchTouchEvent(
385 const OH_NativeXComponent_TouchEvent& touchEvent, const std::vector<XComponentTouchPoint>& xComponentTouchPoints)
386 {
387 CHECK_RUN_ON(UI);
388 CHECK_NULL_VOID(nativeXComponent_);
389 CHECK_NULL_VOID(nativeXComponentImpl_);
390 nativeXComponentImpl_->SetTouchEvent(touchEvent);
391 nativeXComponentImpl_->SetTouchPoint(xComponentTouchPoints);
392 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
393 const auto* callback = nativeXComponentImpl_->GetCallback();
394 CHECK_NULL_VOID(callback);
395 CHECK_NULL_VOID(callback->DispatchTouchEvent);
396 callback->DispatchTouchEvent(nativeXComponent_.get(), surface);
397 }
398
InitNativeWindow(float textureWidth,float textureHeight)399 void XComponentPattern::InitNativeWindow(float textureWidth, float textureHeight)
400 {
401 auto context = PipelineContext::GetCurrentContext();
402 CHECK_NULL_VOID(context);
403 if (renderSurface_->IsSurfaceValid() && (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE)) {
404 float viewScale = context->GetViewScale();
405 renderSurface_->CreateNativeWindow();
406 renderSurface_->AdjustNativeWindowSize(
407 static_cast<uint32_t>(textureWidth * viewScale), static_cast<uint32_t>(textureHeight * viewScale));
408 }
409 }
410
XComponentSizeInit()411 void XComponentPattern::XComponentSizeInit()
412 {
413 CHECK_RUN_ON(UI);
414 ContainerScope scope(instanceId_);
415 auto context = PipelineContext::GetCurrentContext();
416 CHECK_NULL_VOID(context);
417 InitNativeWindow(initSize_.Width(), initSize_.Height());
418 auto host = GetHost();
419 CHECK_NULL_VOID(host);
420 auto eventHub = host->GetEventHub<XComponentEventHub>();
421 CHECK_NULL_VOID(eventHub);
422 eventHub->FireSurfaceInitEvent(id_, host->GetId());
423 eventHub->FireLoadEvent(id_);
424 }
425
XComponentSizeChange(float textureWidth,float textureHeight)426 void XComponentPattern::XComponentSizeChange(float textureWidth, float textureHeight)
427 {
428 ContainerScope scope(instanceId_);
429 auto context = PipelineContext::GetCurrentContext();
430 CHECK_NULL_VOID(context);
431 auto viewScale = context->GetViewScale();
432 renderSurface_->AdjustNativeWindowSize(
433 static_cast<uint32_t>(textureWidth * viewScale), static_cast<uint32_t>(textureHeight * viewScale));
434 NativeXComponentChange(textureWidth, textureHeight);
435 }
436
InitNativeNodeCallbacks()437 void XComponentPattern::InitNativeNodeCallbacks()
438 {
439 CHECK_NULL_VOID(nativeXComponent_);
440 CHECK_NULL_VOID(nativeXComponentImpl_);
441
442 auto host = GetHost();
443 CHECK_NULL_VOID(host);
444 nativeXComponentImpl_->registerContaner(AceType::RawPtr(host));
445
446 auto OnAttachRootNativeNode = [](void* container, void* root) {
447 auto node = AceType::Claim(reinterpret_cast<NG::FrameNode*>(root));
448 CHECK_NULL_VOID(node);
449 auto host = AceType::Claim(reinterpret_cast<NG::FrameNode*>(container));
450 CHECK_NULL_VOID(host);
451 host->AddChild(node);
452 host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
453 };
454
455 auto OnDetachRootNativeNode = [](void* container, void* root) {
456 auto node = AceType::Claim(reinterpret_cast<NG::FrameNode*>(root));
457 CHECK_NULL_VOID(node);
458 auto host = AceType::Claim(reinterpret_cast<NG::FrameNode*>(container));
459 CHECK_NULL_VOID(host);
460 host->RemoveChild(node);
461 };
462
463 nativeXComponentImpl_->registerNativeNodeCallbacks(std::move(OnAttachRootNativeNode),
464 std::move(OnDetachRootNativeNode));
465 }
466
InitEvent()467 void XComponentPattern::InitEvent()
468 {
469 auto host = GetHost();
470 CHECK_NULL_VOID(host);
471 auto eventHub = host->GetEventHub<XComponentEventHub>();
472 CHECK_NULL_VOID(eventHub);
473 eventHub->SetOnSurfaceInitEvent(CreateExternalEvent());
474 auto gestureHub = eventHub->GetOrCreateGestureEventHub();
475 CHECK_NULL_VOID(gestureHub);
476 InitTouchEvent(gestureHub);
477 auto inputHub = eventHub->GetOrCreateInputEventHub();
478 InitMouseEvent(inputHub);
479 InitMouseHoverEvent(inputHub);
480 auto focusHub = host->GetOrCreateFocusHub();
481 CHECK_NULL_VOID(focusHub);
482 InitFocusEvent(focusHub);
483 }
484
InitFocusEvent(const RefPtr<FocusHub> & focusHub)485 void XComponentPattern::InitFocusEvent(const RefPtr<FocusHub>& focusHub)
486 {
487 auto onFocusEvent = [weak = WeakClaim(this)]() {
488 auto pattern = weak.Upgrade();
489 CHECK_NULL_VOID(pattern);
490 return pattern->HandleFocusEvent();
491 };
492 focusHub->SetOnFocusInternal(std::move(onFocusEvent));
493
494 auto onKeyEvent = [weak = WeakClaim(this)](const KeyEvent& event) -> bool {
495 auto pattern = weak.Upgrade();
496 CHECK_NULL_RETURN(pattern, false);
497 return pattern->HandleKeyEvent(event);
498 };
499 focusHub->SetOnKeyEventInternal(std::move(onKeyEvent));
500
501 auto onBlurEvent = [weak = WeakClaim(this)]() {
502 auto pattern = weak.Upgrade();
503 CHECK_NULL_VOID(pattern);
504 return pattern->HandleBlurEvent();
505 };
506 focusHub->SetOnBlurInternal(std::move(onBlurEvent));
507 }
HandleFocusEvent()508 void XComponentPattern::HandleFocusEvent()
509 {
510 CHECK_NULL_VOID(nativeXComponent_);
511 CHECK_NULL_VOID(nativeXComponentImpl_);
512 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
513 const auto focusEventCallback = nativeXComponentImpl_->GetFocusEventCallback();
514 CHECK_NULL_VOID(focusEventCallback);
515 focusEventCallback(nativeXComponent_.get(), surface);
516 }
517
HandleKeyEvent(const KeyEvent & event)518 bool XComponentPattern::HandleKeyEvent(const KeyEvent& event)
519 {
520 CHECK_NULL_RETURN(nativeXComponent_, false);
521 CHECK_NULL_RETURN(nativeXComponentImpl_, false);
522
523 OH_NativeXComponent_KeyEvent keyEvent = ConvertNativeXComponentKeyEvent(event);
524 nativeXComponentImpl_->SetKeyEvent(keyEvent);
525
526 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
527 const auto keyEventCallback = nativeXComponentImpl_->GetKeyEventCallback();
528 CHECK_NULL_RETURN(keyEventCallback, false);
529 keyEventCallback(nativeXComponent_.get(), surface);
530 return false;
531 }
532
HandleBlurEvent()533 void XComponentPattern::HandleBlurEvent()
534 {
535 CHECK_NULL_VOID(nativeXComponent_);
536 CHECK_NULL_VOID(nativeXComponentImpl_);
537 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
538 const auto blurEventCallback = nativeXComponentImpl_->GetBlurEventCallback();
539 CHECK_NULL_VOID(blurEventCallback);
540 blurEventCallback(nativeXComponent_.get(), surface);
541 }
542
InitTouchEvent(const RefPtr<GestureEventHub> & gestureHub)543 void XComponentPattern::InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub)
544 {
545 CHECK_NULL_VOID(!touchEvent_);
546
547 auto touchTask = [weak = WeakClaim(this)](const TouchEventInfo& info) {
548 auto pattern = weak.Upgrade();
549 CHECK_NULL_VOID(pattern);
550 pattern->HandleTouchEvent(info);
551 };
552
553 touchEvent_ = MakeRefPtr<TouchEventImpl>(std::move(touchTask));
554 gestureHub->AddTouchEvent(touchEvent_);
555 }
556
InitMouseEvent(const RefPtr<InputEventHub> & inputHub)557 void XComponentPattern::InitMouseEvent(const RefPtr<InputEventHub>& inputHub)
558 {
559 CHECK_NULL_VOID(!mouseEvent_);
560
561 auto mouseTask = [weak = WeakClaim(this)](const MouseInfo& info) {
562 auto pattern = weak.Upgrade();
563 CHECK_NULL_VOID(pattern);
564 pattern->HandleMouseEvent(info);
565 };
566
567 mouseEvent_ = MakeRefPtr<InputEvent>(std::move(mouseTask));
568 inputHub->AddOnMouseEvent(mouseEvent_);
569 }
570
InitMouseHoverEvent(const RefPtr<InputEventHub> & inputHub)571 void XComponentPattern::InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub)
572 {
573 CHECK_NULL_VOID(!mouseHoverEvent_);
574 auto mouseHoverTask = [weak = WeakClaim(this)](bool isHover) {
575 auto pattern = weak.Upgrade();
576 CHECK_NULL_VOID(pattern);
577 pattern->HandleMouseHoverEvent(isHover);
578 };
579 mouseHoverEvent_ = MakeRefPtr<InputEvent>(std::move(mouseHoverTask));
580 inputHub->AddOnHoverEvent(mouseHoverEvent_);
581 }
582
HandleTouchEvent(const TouchEventInfo & info)583 void XComponentPattern::HandleTouchEvent(const TouchEventInfo& info)
584 {
585 auto touchInfoList = info.GetChangedTouches();
586 if (touchInfoList.empty()) {
587 return;
588 }
589 const auto& locationInfo = touchInfoList.front();
590 const auto& screenOffset = locationInfo.GetGlobalLocation();
591 const auto& localOffset = locationInfo.GetLocalLocation();
592 touchEventPoint_.id = locationInfo.GetFingerId();
593 touchEventPoint_.screenX = static_cast<float>(screenOffset.GetX());
594 touchEventPoint_.screenY = static_cast<float>(screenOffset.GetY());
595 touchEventPoint_.x = static_cast<float>(localOffset.GetX());
596 touchEventPoint_.y = static_cast<float>(localOffset.GetY());
597 touchEventPoint_.size = locationInfo.GetSize();
598 touchEventPoint_.force = locationInfo.GetForce();
599 touchEventPoint_.deviceId = locationInfo.GetTouchDeviceId();
600 const auto timeStamp = info.GetTimeStamp().time_since_epoch().count();
601 touchEventPoint_.timeStamp = timeStamp;
602 auto touchType = touchInfoList.front().GetTouchType();
603 touchEventPoint_.type = ConvertNativeXComponentTouchEvent(touchType);
604 #ifdef OHOS_PLATFORM
605 // increase cpu frequency
606 if (touchType == TouchType::MOVE) {
607 auto currentTime = GetSysTimestamp();
608 auto increaseCpuTime = currentTime - startIncreaseTime_;
609 if (increaseCpuTime >= INCREASE_CPU_TIME_ONCE) {
610 startIncreaseTime_ = currentTime;
611 ResSchedReport::GetInstance().ResSchedDataReport("slide_on");
612 }
613 } else if (touchType == TouchType::UP) {
614 startIncreaseTime_ = 0;
615 ResSchedReport::GetInstance().ResSchedDataReport("slide_off");
616 }
617 #endif
618 SetTouchPoint(info.GetTouches(), timeStamp, touchType);
619
620 if (nativeXComponent_ && nativeXComponentImpl_) {
621 nativeXComponentImpl_->SetHistoricalPoint(SetHistoryPoint(info.GetHistory()));
622 }
623 NativeXComponentDispatchTouchEvent(touchEventPoint_, nativeXComponentTouchPoints_);
624 }
625
HandleMouseEvent(const MouseInfo & info)626 void XComponentPattern::HandleMouseEvent(const MouseInfo& info)
627 {
628 OH_NativeXComponent_MouseEvent mouseEventPoint;
629 mouseEventPoint.x = static_cast<float>(info.GetLocalLocation().GetX());
630 mouseEventPoint.y = static_cast<float>(info.GetLocalLocation().GetY());
631 mouseEventPoint.screenX = static_cast<float>(info.GetScreenLocation().GetX());
632 mouseEventPoint.screenY = static_cast<float>(info.GetScreenLocation().GetY());
633 switch (info.GetAction()) {
634 case MouseAction::PRESS:
635 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_PRESS;
636 break;
637 case MouseAction::RELEASE:
638 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_RELEASE;
639 break;
640 case MouseAction::MOVE:
641 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_MOVE;
642 break;
643 default:
644 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_NONE;
645 break;
646 }
647 switch (info.GetButton()) {
648 case MouseButton::LEFT_BUTTON:
649 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_LEFT_BUTTON;
650 break;
651 case MouseButton::RIGHT_BUTTON:
652 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_RIGHT_BUTTON;
653 break;
654 case MouseButton::MIDDLE_BUTTON:
655 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_MIDDLE_BUTTON;
656 break;
657 case MouseButton::BACK_BUTTON:
658 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_BACK_BUTTON;
659 break;
660 case MouseButton::FORWARD_BUTTON:
661 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_FORWARD_BUTTON;
662 break;
663 default:
664 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_NONE_BUTTON;
665 break;
666 }
667 mouseEventPoint.timestamp = info.GetTimeStamp().time_since_epoch().count();
668 NativeXComponentDispatchMouseEvent(mouseEventPoint);
669 }
670
HandleMouseHoverEvent(bool isHover)671 void XComponentPattern::HandleMouseHoverEvent(bool isHover)
672 {
673 CHECK_RUN_ON(UI);
674 CHECK_NULL_VOID(nativeXComponent_);
675 CHECK_NULL_VOID(nativeXComponentImpl_);
676 const auto* callback = nativeXComponentImpl_->GetMouseEventCallback();
677 CHECK_NULL_VOID(callback);
678 CHECK_NULL_VOID(callback->DispatchHoverEvent);
679 callback->DispatchHoverEvent(nativeXComponent_.get(), isHover);
680 }
681
NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent & mouseEvent)682 void XComponentPattern::NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent)
683 {
684 CHECK_RUN_ON(UI);
685 CHECK_NULL_VOID(nativeXComponent_);
686 CHECK_NULL_VOID(nativeXComponentImpl_);
687 nativeXComponentImpl_->SetMouseEvent(mouseEvent);
688 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
689 const auto* callback = nativeXComponentImpl_->GetMouseEventCallback();
690 CHECK_NULL_VOID(callback);
691 CHECK_NULL_VOID(callback->DispatchMouseEvent);
692 callback->DispatchMouseEvent(nativeXComponent_.get(), surface);
693 }
694
SetTouchPoint(const std::list<TouchLocationInfo> & touchInfoList,const int64_t timeStamp,const TouchType & touchType)695 void XComponentPattern::SetTouchPoint(
696 const std::list<TouchLocationInfo>& touchInfoList, const int64_t timeStamp, const TouchType& touchType)
697 {
698 touchEventPoint_.numPoints =
699 touchInfoList.size() <= OH_MAX_TOUCH_POINTS_NUMBER ? touchInfoList.size() : OH_MAX_TOUCH_POINTS_NUMBER;
700 nativeXComponentTouchPoints_.clear();
701 uint32_t index = 0;
702 for (auto iterator = touchInfoList.begin(); iterator != touchInfoList.end() && index < OH_MAX_TOUCH_POINTS_NUMBER;
703 iterator++) {
704 OH_NativeXComponent_TouchPoint ohTouchPoint;
705 const auto& pointTouchInfo = *iterator;
706 const auto& pointScreenOffset = pointTouchInfo.GetGlobalLocation();
707 const auto& pointLocalOffset = pointTouchInfo.GetLocalLocation();
708 ohTouchPoint.id = pointTouchInfo.GetFingerId();
709 ohTouchPoint.screenX = static_cast<float>(pointScreenOffset.GetX());
710 ohTouchPoint.screenY = static_cast<float>(pointScreenOffset.GetY());
711 ohTouchPoint.x = static_cast<float>(pointLocalOffset.GetX());
712 ohTouchPoint.y = static_cast<float>(pointLocalOffset.GetY());
713 ohTouchPoint.type = ConvertNativeXComponentTouchEvent(touchType);
714 ohTouchPoint.size = pointTouchInfo.GetSize();
715 ohTouchPoint.force = pointTouchInfo.GetForce();
716 ohTouchPoint.timeStamp = timeStamp;
717 ohTouchPoint.isPressed = (touchType == TouchType::DOWN);
718 touchEventPoint_.touchPoints[index++] = ohTouchPoint;
719 // set tiltX, tiltY and sourceToolType
720 XComponentTouchPoint xcomponentTouchPoint;
721 xcomponentTouchPoint.tiltX = pointTouchInfo.GetTiltX().value_or(0.0f);
722 xcomponentTouchPoint.tiltY = pointTouchInfo.GetTiltY().value_or(0.0f);
723 xcomponentTouchPoint.sourceToolType = ConvertNativeXComponentTouchToolType(pointTouchInfo.GetSourceTool());
724 nativeXComponentTouchPoints_.emplace_back(xcomponentTouchPoint);
725 }
726 while (index < OH_MAX_TOUCH_POINTS_NUMBER) {
727 OH_NativeXComponent_TouchPoint ohTouchPoint;
728 ohTouchPoint.id = 0;
729 ohTouchPoint.screenX = 0;
730 ohTouchPoint.screenY = 0;
731 ohTouchPoint.x = 0;
732 ohTouchPoint.y = 0;
733 ohTouchPoint.type = OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UNKNOWN;
734 ohTouchPoint.size = 0;
735 ohTouchPoint.force = 0;
736 ohTouchPoint.timeStamp = 0;
737 ohTouchPoint.isPressed = false;
738 touchEventPoint_.touchPoints[index++] = ohTouchPoint;
739 }
740 }
741
SetHistoryPoint(const std::list<TouchLocationInfo> & touchInfoList)742 std::vector<OH_NativeXComponent_HistoricalPoint> XComponentPattern::SetHistoryPoint(
743 const std::list<TouchLocationInfo>& touchInfoList)
744 {
745 std::vector<OH_NativeXComponent_HistoricalPoint> historicalPoints;
746 for (auto&& item : touchInfoList) {
747 OH_NativeXComponent_HistoricalPoint point;
748 point.id = item.GetFingerId();
749 point.x = item.GetLocalLocation().GetX();
750 point.y = item.GetLocalLocation().GetY();
751 point.screenX = item.GetScreenLocation().GetX();
752 point.screenY = item.GetScreenLocation().GetY();
753 point.type = static_cast<OH_NativeXComponent_TouchEventType>(item.GetTouchType());
754 point.size = item.GetSize();
755 point.force = item.GetForce();
756 point.timeStamp = item.GetTimeStamp().time_since_epoch().count();
757 point.titlX = item.GetTiltX().value_or(0.0f);
758 point.titlY = item.GetTiltY().value_or(0.0f);
759 point.sourceTool = static_cast<OH_NativeXComponent_TouchEvent_SourceTool>(item.GetSourceTool());
760
761 historicalPoints.push_back(point);
762 }
763 return historicalPoints;
764 }
765
766
FireExternalEvent(RefPtr<NG::PipelineContext> context,const std::string & componentId,const uint32_t nodeId,const bool isDestroy)767 void XComponentPattern::FireExternalEvent(RefPtr<NG::PipelineContext> context,
768 const std::string& componentId, const uint32_t nodeId, const bool isDestroy)
769 {
770 CHECK_NULL_VOID(context);
771 #ifdef NG_BUILD
772 auto frontEnd = AceType::DynamicCast<DeclarativeFrontendNG>(context->GetFrontend());
773 #else
774 auto frontEnd = AceType::DynamicCast<DeclarativeFrontend>(context->GetFrontend());
775 #endif
776 CHECK_NULL_VOID(frontEnd);
777 auto jsEngine = frontEnd->GetJsEngine();
778 jsEngine->FireExternalEvent(componentId, nodeId, isDestroy);
779 }
780
CreateExternalEvent()781 ExternalEvent XComponentPattern::CreateExternalEvent()
782 {
783 return [weak = AceType::WeakClaim(this), instanceId = instanceId_](
784 const std::string& componentId, const uint32_t nodeId, const bool isDestroy) {
785 ContainerScope scope(instanceId);
786 auto context = PipelineContext::GetCurrentContext();
787 CHECK_NULL_VOID(context);
788 auto pattern = weak.Upgrade();
789 CHECK_NULL_VOID(pattern);
790 pattern->FireExternalEvent(context, componentId, nodeId, isDestroy);
791 };
792 }
793
SetHandlingRenderContextForSurface(const RefPtr<RenderContext> & otherRenderContext)794 void XComponentPattern::SetHandlingRenderContextForSurface(const RefPtr<RenderContext>& otherRenderContext)
795 {
796 CHECK_NULL_VOID(otherRenderContext);
797 handlingSurfaceRenderContext_ = otherRenderContext;
798 auto host = GetHost();
799 CHECK_NULL_VOID(host);
800 auto renderContext = host->GetRenderContext();
801 renderContext->ClearChildren();
802 renderContext->AddChild(handlingSurfaceRenderContext_, 0);
803 handlingSurfaceRenderContext_->SetBounds(
804 localposition_.GetX(), localposition_.GetY(), drawSize_.Width(), drawSize_.Height());
805 }
806
GetOffsetRelativeToWindow()807 OffsetF XComponentPattern::GetOffsetRelativeToWindow()
808 {
809 auto host = GetHost();
810 CHECK_NULL_RETURN(host, OffsetF());
811 return host->GetTransformRelativeOffset();
812 }
813
RestoreHandlingRenderContextForSurface()814 void XComponentPattern::RestoreHandlingRenderContextForSurface()
815 {
816 SetHandlingRenderContextForSurface(renderContextForSurface_);
817 }
818
SetExtController(const RefPtr<XComponentPattern> & extPattern)819 XComponentControllerErrorCode XComponentPattern::SetExtController(const RefPtr<XComponentPattern>& extPattern)
820 {
821 if (!extPattern) {
822 return XCOMPONENT_CONTROLLER_BAD_PARAMETER;
823 }
824 if (extPattern_.Upgrade()) {
825 return XCOMPONENT_CONTROLLER_REPEAT_SET;
826 }
827 extPattern->SetHandlingRenderContextForSurface(handlingSurfaceRenderContext_);
828 extPattern_ = extPattern;
829 handlingSurfaceRenderContext_.Reset();
830 return XCOMPONENT_CONTROLLER_NO_ERROR;
831 }
832
ResetExtController(const RefPtr<XComponentPattern> & extPattern)833 XComponentControllerErrorCode XComponentPattern::ResetExtController(const RefPtr<XComponentPattern>& extPattern)
834 {
835 if (!extPattern) {
836 return XCOMPONENT_CONTROLLER_BAD_PARAMETER;
837 }
838 auto curExtPattern = extPattern_.Upgrade();
839 if (!curExtPattern || curExtPattern != extPattern) {
840 return XCOMPONENT_CONTROLLER_RESET_ERROR;
841 }
842 RestoreHandlingRenderContextForSurface();
843 extPattern->RestoreHandlingRenderContextForSurface();
844 extPattern_.Reset();
845 return XCOMPONENT_CONTROLLER_NO_ERROR;
846 }
847
HandleSetExpectedRateRangeEvent()848 void XComponentPattern::HandleSetExpectedRateRangeEvent()
849 {
850 CHECK_NULL_VOID(nativeXComponent_);
851 CHECK_NULL_VOID(nativeXComponentImpl_);
852 CHECK_NULL_VOID(displaySync_);
853 OH_NativeXComponent_ExpectedRateRange* range = nativeXComponentImpl_->GetRateRange();
854 CHECK_NULL_VOID(range);
855 FrameRateRange frameRateRange;
856 frameRateRange.preferred_ = range->expected;
857 frameRateRange.max_ = range->max;
858 frameRateRange.min_ = range->min;
859 displaySync_->SetExpectedFrameRateRange(std::move(frameRateRange));
860 }
861
HandleOnFrameEvent()862 void XComponentPattern::HandleOnFrameEvent()
863 {
864 CHECK_NULL_VOID(nativeXComponent_);
865 CHECK_NULL_VOID(nativeXComponentImpl_);
866 CHECK_NULL_VOID(displaySync_);
867 displaySync_->RegisterOnFrameWithData([weak = AceType::WeakClaim(this)](RefPtr<DisplaySyncData> displaySyncData) {
868 auto xComponentPattern = weak.Upgrade();
869 CHECK_NULL_VOID(xComponentPattern);
870 CHECK_NULL_VOID(xComponentPattern->nativeXComponentImpl_->GetOnFrameCallback());
871 xComponentPattern->nativeXComponentImpl_->GetOnFrameCallback()(xComponentPattern->nativeXComponent_.get(),
872 displaySyncData->GetTimestamp(), displaySyncData->GetTargetTimestamp());
873 });
874 displaySync_->AddToPipelineOnContainer();
875 }
876
HandleUnregisterOnFrameEvent()877 void XComponentPattern::HandleUnregisterOnFrameEvent()
878 {
879 CHECK_NULL_VOID(nativeXComponent_);
880 CHECK_NULL_VOID(nativeXComponentImpl_);
881 CHECK_NULL_VOID(displaySync_);
882 displaySync_->UnRegisterOnFrame();
883 displaySync_->DelFromPipelineOnContainer();
884 }
885
DoTextureExport()886 bool XComponentPattern::DoTextureExport()
887 {
888 CHECK_NULL_RETURN(handlingSurfaceRenderContext_, false);
889 if (!ExportTextureAvailable()) {
890 return false;
891 }
892 if (!handlingSurfaceRenderContext_->DoTextureExport(exportTextureSurfaceId_)) {
893 TAG_LOGW(AceLogTag::ACE_XCOMPONENT, "DoTextureExport fail");
894 return false;
895 }
896 auto host = GetHost();
897 CHECK_NULL_RETURN(host, false);
898 auto renderContext = host->GetRenderContext();
899 CHECK_NULL_RETURN(renderContext, false);
900 renderContext->SetIsNeedRebuildRSTree(false);
901 return true;
902 }
903
StopTextureExport()904 bool XComponentPattern::StopTextureExport()
905 {
906 CHECK_NULL_RETURN(handlingSurfaceRenderContext_, false);
907 if (!handlingSurfaceRenderContext_->StopTextureExport()) {
908 TAG_LOGW(AceLogTag::ACE_XCOMPONENT, "StopTextureExport fail");
909 return false;
910 }
911 auto host = GetHost();
912 CHECK_NULL_RETURN(host, false);
913 auto renderContext = host->GetRenderContext();
914 CHECK_NULL_RETURN(renderContext, false);
915 renderContext->ClearChildren();
916 renderContext->AddChild(handlingSurfaceRenderContext_, 0);
917 renderContext->SetIsNeedRebuildRSTree(true);
918 return true;
919 }
920
AddAfterLayoutTaskForExportTexture()921 void XComponentPattern::AddAfterLayoutTaskForExportTexture()
922 {
923 auto context = PipelineContext::GetCurrentContext();
924 CHECK_NULL_VOID(context);
925 context->AddAfterLayoutTask([weak = WeakClaim(this)]() {
926 auto pattern = weak.Upgrade();
927 CHECK_NULL_VOID(pattern);
928 pattern->DoTextureExport();
929 });
930 }
931
ExportTextureAvailable()932 bool XComponentPattern::ExportTextureAvailable()
933 {
934 auto host = GetHost();
935 auto parnetNodeContainer = host->GetNodeContainer();
936 CHECK_NULL_RETURN(parnetNodeContainer, false);
937 auto parent = parnetNodeContainer->GetAncestorNodeOfFrame();
938 CHECK_NULL_RETURN(parent, false);
939 auto ancestorNodeContainer = parent->GetNodeContainer();
940 CHECK_NULL_RETURN(ancestorNodeContainer, true);
941 auto ancestorViewNode = ancestorNodeContainer->GetChildAtIndex(0);
942 CHECK_NULL_RETURN(ancestorViewNode, true);
943 auto parnetExportTextureInfo = ancestorViewNode->GetExportTextureInfo();
944 CHECK_NULL_RETURN(parnetExportTextureInfo, true);
945 return parnetExportTextureInfo->GetCurrentRenderType() != NodeRenderType::RENDER_TYPE_TEXTURE;
946 }
947
ChangeRenderType(NodeRenderType renderType)948 bool XComponentPattern::ChangeRenderType(NodeRenderType renderType)
949 {
950 if (type_ != XComponentType::SURFACE) {
951 return renderType == NodeRenderType::RENDER_TYPE_DISPLAY;
952 }
953 auto host = GetHost();
954 CHECK_NULL_RETURN(host, false);
955 if (!host->GetNodeContainer()) {
956 renderType_ = renderType;
957 return true;
958 }
959 auto renderContext = host->GetRenderContext();
960 CHECK_NULL_RETURN(renderContext, false);
961 if (renderType == NodeRenderType::RENDER_TYPE_TEXTURE) {
962 if (DoTextureExport()) {
963 renderType_ = renderType;
964 return true;
965 }
966 } else {
967 if (StopTextureExport()) {
968 renderType_ = renderType;
969 return true;
970 }
971 }
972 return false;
973 }
974
SetExportTextureSurfaceId(const std::string & surfaceId)975 void XComponentPattern::SetExportTextureSurfaceId(const std::string& surfaceId)
976 {
977 exportTextureSurfaceId_ = StringUtils::StringToLongUint(surfaceId);
978 }
979
SetSurfaceSize(uint32_t surfaceWidth,uint32_t surfaceHeight)980 void XComponentPattern::SetSurfaceSize(uint32_t surfaceWidth, uint32_t surfaceHeight)
981 {
982 CHECK_NULL_VOID(xcomponentController_);
983 xcomponentController_->ConfigSurface(surfaceWidth, surfaceHeight);
984 }
985 } // namespace OHOS::Ace::NG
986