• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H
18 
19 #include <optional>
20 #include <string>
21 #include <utility>
22 
23 #include "base/geometry/dimension.h"
24 #include "base/geometry/ng/offset_t.h"
25 #include "base/geometry/ng/rect_t.h"
26 #include "base/geometry/ng/size_t.h"
27 #include "base/geometry/size.h"
28 #include "base/memory/referenced.h"
29 #include "base/utils/utils.h"
30 #include "core/common/thread_checker.h"
31 #include "core/components/common/layout/constants.h"
32 #include "core/components/xcomponent/native_interface_xcomponent_impl.h"
33 #include "core/components_ng/event/focus_hub.h"
34 #include "core/components_ng/event/input_event.h"
35 #include "core/components_ng/pattern/pattern.h"
36 #include "core/components_ng/pattern/xcomponent/inner_xcomponent_controller.h"
37 #include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_provider.h"
38 #include "core/components_ng/pattern/xcomponent/xcomponent_event_hub.h"
39 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h"
40 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_property.h"
41 #include "core/components_ng/pattern/xcomponent/xcomponent_paint_method.h"
42 #include "core/components_ng/property/property.h"
43 #include "core/components_ng/render/render_surface.h"
44 #include "core/pipeline_ng/pipeline_context.h"
45 #include "core/components_ng/manager/display_sync/ui_display_sync.h"
46 #include "core/gestures/velocity.h"
47 
48 namespace OHOS::Ace {
49 class ImageAnalyzerManager;
50 }
51 namespace OHOS::Ace::NG {
52 class XComponentExtSurfaceCallbackClient;
53 class XComponentPattern : public Pattern {
54     DECLARE_ACE_TYPE(XComponentPattern, Pattern);
55 
56 public:
57     XComponentPattern() = default;
58     XComponentPattern(const std::optional<std::string>& id, XComponentType type,
59         const std::optional<std::string>& libraryname,
60         const std::shared_ptr<InnerXComponentController>& xcomponentController, float initWidth = 0.0f,
61         float initHeight = 0.0f, bool isTypedNode = false);
62     ~XComponentPattern() override = default;
63 
IsAtomicNode()64     bool IsAtomicNode() const override
65     {
66         return type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE || type_ == XComponentType::NODE;
67     }
68 
CreateLayoutProperty()69     RefPtr<LayoutProperty> CreateLayoutProperty() override
70     {
71         return MakeRefPtr<XComponentLayoutProperty>();
72     }
73 
CreateEventHub()74     RefPtr<EventHub> CreateEventHub() override
75     {
76         return MakeRefPtr<XComponentEventHub>();
77     }
78 
CreateLayoutAlgorithm()79     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
80     {
81         return MakeRefPtr<XComponentLayoutAlgorithm>();
82     }
83 
CreateNodePaintMethod()84     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
85     {
86         if (type_ == XComponentType::TEXTURE) {
87             auto paint = MakeRefPtr<XComponentPaintMethod>(renderSurface_, AceType::Claim(this));
88             return paint;
89         }
90         return nullptr;
91     }
92 
GetFocusPattern()93     FocusPattern GetFocusPattern() const override
94     {
95         if (type_ == XComponentType::NODE) {
96             return { FocusType::SCOPE, true };
97         }
98         FocusPattern focusPattern = { FocusType::NODE, false };
99         focusPattern.SetIsFocusActiveWhenFocused(true);
100         return focusPattern;
101     }
102 
NeedSoftKeyboard()103     bool NeedSoftKeyboard() const override
104     {
105         return nativeXComponentImpl_ ? nativeXComponentImpl_->IsNeedSoftKeyboard() : false;
106     }
107 
GetNativeXComponent()108     std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, std::weak_ptr<OH_NativeXComponent>> GetNativeXComponent()
109     {
110         if (!nativeXComponent_ || !nativeXComponentImpl_) {
111             // for XComponentType::NODE
112             nativeXComponentImpl_ = AceType::MakeRefPtr<NativeXComponentImpl>();
113             nativeXComponent_ = std::make_shared<OH_NativeXComponent>(AceType::RawPtr(nativeXComponentImpl_));
114         }
115         hasGotNativeXComponent_ = true;
116         return std::make_pair(nativeXComponentImpl_, nativeXComponent_);
117     }
118 
119     void NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent,
120         const std::vector<XComponentTouchPoint>& xComponentTouchPoints);
121     void NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent);
122     void NativeXComponentDispatchAxisEvent(AxisEvent* axisEvent);
123 
124     void InitXComponent();
125     void InitNativeXComponent();
126     void InitNativeWindow(float textureWidth, float textureHeight);
127     void XComponentSizeInit();
128     void XComponentSizeChange(const RectF& surfaceRect, bool needFireNativeEvent);
NativeXComponentInit()129     void NativeXComponentInit()
130     {
131         if (!isTypedNode_) {
132             OnSurfaceCreated();
133         }
134     }
135 
GetId()136     std::string GetId() const
137     {
138         if (id_.has_value()) {
139             return id_.value();
140         }
141         auto host = GetHost();
142         return "nodeId:" + (host ? std::to_string(host->GetId()) : "-1");
143     }
144 
SetId(const std::string & id)145     void SetId(const std::string& id)
146     {
147         id_ = id;
148     }
149 
GetLibraryName()150     const std::optional<std::string>& GetLibraryName() const
151     {
152         return libraryname_;
153     }
154 
SetLibraryName(const std::optional<std::string> & libraryname)155     void SetLibraryName(const std::optional<std::string>& libraryname)
156     {
157         libraryname_ = libraryname;
158     }
159 
GetSoPath()160     const std::optional<std::string>& GetSoPath() const
161     {
162         return soPath_;
163     }
164 
SetSoPath(const std::string & soPath)165     void SetSoPath(const std::string& soPath)
166     {
167         soPath_ = soPath;
168     }
169 
GetType()170     XComponentType GetType()
171     {
172         return type_;
173     }
174 
SetType(XComponentType type)175     void SetType(XComponentType type)
176     {
177         type_ = type;
178     }
179 
GetDrawSize()180     const SizeF& GetDrawSize() const
181     {
182         return drawSize_;
183     }
184 
GetSurfaceSize()185     const SizeF& GetSurfaceSize() const
186     {
187         return surfaceSize_;
188     }
189 
GetSurfaceOffset()190     const OffsetF& GetSurfaceOffset() const
191     {
192         return surfaceOffset_;
193     }
194 
195     OffsetF GetOffsetRelativeToWindow();
196 
GetRenderContextForSurface()197     const RefPtr<RenderContext>& GetRenderContextForSurface()
198     {
199         return renderContextForSurface_;
200     }
201 
202     void SetSurfaceRotation(bool isLock);
203 
GetSurfaceRotation()204     bool GetSurfaceRotation()
205     {
206         return isSurfaceLock_;
207     }
208 
SetIsTypeNode(bool isTypeNode)209     void SetIsTypeNode(bool isTypeNode)
210     {
211         isTypedNode_ = isTypeNode;
212     }
213 
GetXComponentController()214     std::shared_ptr<InnerXComponentController> GetXComponentController()
215     {
216         return xcomponentController_;
217     }
218 
219     void SetHandlingRenderContextForSurface(const RefPtr<RenderContext>& otherRenderContext);
220 
221     void RestoreHandlingRenderContextForSurface();
222 
223     XComponentControllerErrorCode SetExtController(const RefPtr<XComponentPattern>& extPattern);
224     XComponentControllerErrorCode ResetExtController(const RefPtr<XComponentPattern>& extPattern);
225 
SetExpectedRateRangeInit()226     void SetExpectedRateRangeInit()
227     {
228         CHECK_NULL_VOID(nativeXComponentImpl_);
229         nativeXComponentImpl_->SetExpectedRateRangeEventCallback([weak = AceType::WeakClaim(this)]() {
230             auto xComponentPattern = weak.Upgrade();
231             CHECK_NULL_VOID(xComponentPattern);
232             xComponentPattern->HandleSetExpectedRateRangeEvent();
233         });
234     }
235 
OnFrameEventInit()236     void OnFrameEventInit()
237     {
238         CHECK_NULL_VOID(nativeXComponentImpl_);
239         nativeXComponentImpl_->SetOnFrameEventCallback([weak = AceType::WeakClaim(this)]() {
240             auto xComponentPattern = weak.Upgrade();
241             CHECK_NULL_VOID(xComponentPattern);
242             xComponentPattern->HandleOnFrameEvent();
243         });
244     }
245 
UnregisterOnFrameEventInit()246     void UnregisterOnFrameEventInit()
247     {
248         CHECK_NULL_VOID(nativeXComponentImpl_);
249         nativeXComponentImpl_->SetUnregisterOnFrameEventCallback([weak = AceType::WeakClaim(this)]() {
250             auto xComponentPattern = weak.Upgrade();
251             CHECK_NULL_VOID(xComponentPattern);
252             xComponentPattern->HandleUnregisterOnFrameEvent();
253         });
254     }
255 
SetXcomponentInit(bool isInit)256     void SetXcomponentInit(bool isInit)
257     {
258         hasXComponentInit_ = isInit;
259     }
260 
261     bool ChangeRenderType(NodeRenderType renderType);
262 
SetRenderType(NodeRenderType renderType)263     void SetRenderType(NodeRenderType renderType)
264     {
265         renderType_ = renderType;
266     }
267 
UpdateTransformHintChangedCallbackId(std::optional<int32_t> id)268     void UpdateTransformHintChangedCallbackId(std::optional<int32_t> id)
269     {
270         transformHintChangedCallbackId_ = id;
271     }
272 
HasTransformHintChangedCallbackId()273     bool HasTransformHintChangedCallbackId()
274     {
275         return transformHintChangedCallbackId_.has_value();
276     }
277 
NeedTriggerLoadEventImmediately()278     bool NeedTriggerLoadEventImmediately() const
279     {
280         return isTypedNode_ && isNativeXComponent_ && hasLoadNativeDone_;
281     }
282 
HasGotSurfaceHolder()283     bool HasGotSurfaceHolder() const
284     {
285         return hasGotSurfaceHolder_;
286     }
287 
HasGotNativeXComponent()288     bool HasGotNativeXComponent() const
289     {
290         return hasGotNativeXComponent_;
291     }
292 
IsBindNative()293     virtual bool IsBindNative()
294     {
295         return false;
296     }
297 
298     void SetExportTextureSurfaceId(const std::string& surfaceId);
299     void FireExternalEvent(RefPtr<NG::PipelineContext> context,
300         const std::string& componentId, const uint32_t nodeId, const bool isDestroy);
301     void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight);
302 
303     // accessibility
304     void InitializeAccessibility();
305     void UninitializeAccessibility();
306     bool OnAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId);
307     bool OnAccessibilityChildTreeDeregister();
308     void OnSetAccessibilityChildTree(int32_t childWindowId, int32_t childTreeId);
SetAccessibilityState(bool state)309     void SetAccessibilityState(bool state) {}
310     RefPtr<AccessibilitySessionAdapter> GetAccessibilitySessionAdapter() override;
311     void InitializeAccessibilityCallback();
312     void HandleRegisterAccessibilityEvent(bool isRegister);
313 
314     void SetIdealSurfaceWidth(float surfaceWidth);
315     void SetIdealSurfaceHeight(float surfaceHeight);
316     void SetIdealSurfaceOffsetX(float offsetX);
317     void SetIdealSurfaceOffsetY(float offsetY);
318     void ClearIdealSurfaceOffset(bool isXAxis);
319     std::tuple<bool, bool, bool> UpdateSurfaceRect();
320     void HandleSurfaceChangeEvent(bool needForceRender, bool offsetChanged, bool sizeChanged, bool needFireNativeEvent,
321         bool frameOffsetChange = false);
322     void EnableAnalyzer(bool enable);
323     void SetImageAIOptions(void* options);
324     void StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed);
325     void StopImageAnalyzer();
326     RectF AdjustPaintRect(float positionX, float positionY, float width, float height, bool isRound);
327     float RoundValueToPixelGrid(float value, bool isRound, bool forceCeil, bool forceFloor);
328     void OnSurfaceDestroyed();
329     void SetRenderFit(RenderFit renderFit);
330     void SetScreenId(uint64_t screenId);
331     void HandleSurfaceCreated();
332     void HandleSurfaceDestroyed();
ChangeSurfaceCallbackMode(SurfaceCallbackMode mode)333     void ChangeSurfaceCallbackMode(SurfaceCallbackMode mode)
334     {
335         if (surfaceCallbackModeChangeEvent_) {
336             surfaceCallbackModeChangeEvent_(mode);
337         }
338     }
339     void OnSurfaceCallbackModeChange(SurfaceCallbackMode mode);
340     void EnableSecure(bool isSecure);
341     void HdrBrightness(float hdrBrightness);
342     void EnableTransparentLayer(bool isTransparentLayer);
343     RenderFit GetSurfaceRenderFit() const;
344     bool GetEnableAnalyzer();
345     void NativeStartImageAnalyzer(std::function<void(int32_t)>& callback);
346 
347 protected:
348     void OnAttachToMainTree() override;
349     void OnDetachFromMainTree() override;
350     void OnAttachToFrameNode() override;
351     void OnDetachFromFrameNode(FrameNode* frameNode) override;
352     void BeforeSyncGeometryProperties(const DirtySwapConfig& config) override;
353     void OnRebuildFrame() override;
354     void OnWindowHide() override;
355     void OnWindowShow() override;
356     void OnModifyDone() override;
357     void AddAfterLayoutTaskForExportTexture();
358     void UpdateTransformHint();
359     void DumpInfo() override;
360     static std::string XComponentTypeToString(XComponentType type);
361     static std::string XComponentNodeTypeToString(XComponentNodeType type);
362     void AdjustNativeWindowSize(float width, float height);
363     bool IsSupportImageAnalyzerFeature();
364     void UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode);
365 
366     std::optional<std::string> id_;
367     XComponentType type_;
368     bool hasGotSurfaceHolder_ = false;
369     bool hasGotNativeXComponent_ = false;
370     bool isCNode_ = false;
371     RefPtr<RenderSurface> renderSurface_;
372     OffsetF localPosition_;
373     OffsetF surfaceOffset_;
374     SizeF drawSize_;
375     SizeF surfaceSize_;
376     RectF paintRect_;
377     void* nativeWindow_ = nullptr;
378     bool hasReleasedSurface_ = false;
379     RefPtr<RenderContext> renderContextForSurface_;
380     std::optional<int32_t> transformHintChangedCallbackId_;
381     std::string surfaceId_;
382     bool isOnTree_ = false;
383 
384 private:
385     void OnAreaChangedInner() override;
DumpSimplifyInfo(std::unique_ptr<JsonValue> & json)386     void DumpSimplifyInfo(std::unique_ptr<JsonValue>& json) override {}
387     void DumpInfo(std::unique_ptr<JsonValue>& json) override;
388     void DumpAdvanceInfo() override;
389     void DumpAdvanceInfo(std::unique_ptr<JsonValue>& json) override;
390     void OnAttachContext(PipelineContext *context) override;
391     void OnDetachContext(PipelineContext *context) override;
392     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
393 
394     void NativeXComponentOffset(double x, double y);
395 
396     void LoadNative();
397     void OnNativeLoad(FrameNode* frameNode);
398     void OnNativeUnload(FrameNode* frameNode);
399 
400     void OnSurfaceCreated();
401     void OnSurfaceChanged(const RectF& surfaceRect, bool needResizeNativeWindow);
402 
403     void NativeSurfaceShow();
404     void NativeSurfaceHide();
405 
406     void Initialize();
407     void InitController();
408     void InitSurface();
409     void InitNativeNodeCallbacks();
410     void InitEvent();
411     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
412     void InitOnTouchIntercept(const RefPtr<GestureEventHub>& gestureHub);
413     void InitAxisEvent(const RefPtr<InputEventHub>& inputHub);
414     void HandleTouchEvent(const TouchEventInfo& info);
415     void InitMouseEvent(const RefPtr<InputEventHub>& inputHub);
416     void HandleMouseEvent(const MouseInfo& info);
417     void HandleAxisEvent(const AxisInfo& info);
418     void InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub);
419     void HandleMouseHoverEvent(bool isHover);
420     void InitFocusEvent(const RefPtr<FocusHub>& focusHub);
421     void HandleFocusEvent();
422     bool HandleKeyEvent(const KeyEvent& event);
423     void HandleBlurEvent();
424     ExternalEvent CreateExternalEvent();
425 
426     void SetTouchPoint(
427         const std::list<TouchLocationInfo>& touchInfoList, int64_t timeStamp, const TouchType& touchType);
428     void HandleSetExpectedRateRangeEvent();
429     void HandleOnFrameEvent();
430     void HandleUnregisterOnFrameEvent();
431     bool ExportTextureAvailable();
432     bool DoTextureExport();
433     bool StopTextureExport();
434     void InitializeRenderContext();
435     void SetSurfaceNodeToGraphic();
436     void CreateAnalyzerOverlay();
437     void DestroyAnalyzerOverlay();
438     void UpdateAnalyzerOverlay();
439     void ReleaseImageAnalyzer();
440     void SetRotation(uint32_t rotation);
441     void RegisterSurfaceCallbackModeEvent();
442 
443 #ifdef RENDER_EXTRACT_SUPPORTED
444     RenderSurface::RenderSurfaceType CovertToRenderSurfaceType(const XComponentType& hostType);
445     void RegisterRenderContextCallBack();
446     void RequestFocus();
447 #endif
448 
449     std::vector<OH_NativeXComponent_HistoricalPoint> SetHistoryPoint(const std::list<TouchLocationInfo>& touchInfoList);
450     std::optional<std::string> libraryname_;
451     std::shared_ptr<InnerXComponentController> xcomponentController_;
452     std::optional<std::string> soPath_;
453     std::optional<uint64_t> screenId_;
454 
455     RefPtr<RenderContext> handlingSurfaceRenderContext_;
456     WeakPtr<XComponentPattern> extPattern_;
457 
458     std::shared_ptr<OH_NativeXComponent> nativeXComponent_;
459     RefPtr<NativeXComponentImpl> nativeXComponentImpl_;
460 
461     bool hasXComponentInit_ = false;
462 
463     RefPtr<TouchEventImpl> touchEvent_;
464     OH_NativeXComponent_TouchEvent touchEventPoint_ = {};
465     RefPtr<InputEvent> mouseEvent_;
466     RefPtr<InputEvent> axisEvent_;
467     RefPtr<InputEvent> mouseHoverEvent_;
468     std::vector<XComponentTouchPoint> nativeXComponentTouchPoints_;
469     RefPtr<XComponentExtSurfaceCallbackClient> extSurfaceClient_;
470     SizeF initSize_;
471     OffsetF globalPosition_;
472     RefPtr<UIDisplaySync> displaySync_ = AceType::MakeRefPtr<UIDisplaySync>(UIObjectType::DISPLAYSYNC_XCOMPONENT);
473 
474     std::optional<float> selfIdealSurfaceWidth_;
475     std::optional<float> selfIdealSurfaceHeight_;
476     std::optional<float> selfIdealSurfaceOffsetX_;
477     std::optional<float> selfIdealSurfaceOffsetY_;
478 
479     bool isSurfaceLock_ = false;
480     uint32_t windowId_ = 0;
481     int32_t treeId_ = 0;
482     std::shared_ptr<AccessibilityChildTreeCallback> accessibilityChildTreeCallback_;
483     RefPtr<XComponentAccessibilityProvider> accessibilityProvider_;
484     RefPtr<AccessibilitySessionAdapter> accessibilitySessionAdapter_;
485 
486     // for export texture
487     NodeRenderType renderType_ = NodeRenderType::RENDER_TYPE_DISPLAY;
488     uint64_t exportTextureSurfaceId_ = 0U;
489     std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_;
490     bool isEnableAnalyzer_ = false;
491     uint32_t rotation_ = 0;
492     bool isTypedNode_ = false;
493     bool isNativeXComponent_ = false;
494     bool hasLoadNativeDone_ = false;
495     bool isEnableSecure_ = false;
496     float hdrBrightness_ = 1.0f;
497     bool isTransparentLayer_ = false;
498     SurfaceCallbackMode surfaceCallbackMode_ = SurfaceCallbackMode::DEFAULT;
499     std::function<void(SurfaceCallbackMode)> surfaceCallbackModeChangeEvent_;
500     // record displaySync_->DelFromPipelineOnContainer() from OnDetachFromMainTree
501     bool needRecoverDisplaySync_ = false;
502     bool isNativeImageAnalyzing_ = false;
503 };
504 } // namespace OHOS::Ace::NG
505 
506 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H
507