• 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 
64     void OnAttachToMainTree() override;
65     void OnDetachFromMainTree() override;
66 
IsAtomicNode()67     bool IsAtomicNode() const override
68     {
69         return type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE || type_ == XComponentType::NODE;
70     }
71 
CreateLayoutProperty()72     RefPtr<LayoutProperty> CreateLayoutProperty() override
73     {
74         return MakeRefPtr<XComponentLayoutProperty>();
75     }
76 
CreateEventHub()77     RefPtr<EventHub> CreateEventHub() override
78     {
79         return MakeRefPtr<XComponentEventHub>();
80     }
81 
CreateLayoutAlgorithm()82     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
83     {
84         return MakeRefPtr<XComponentLayoutAlgorithm>();
85     }
86 
CreateNodePaintMethod()87     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
88     {
89         if (type_ == XComponentType::TEXTURE) {
90             auto paint = MakeRefPtr<XComponentPaintMethod>(renderSurface_, AceType::Claim(this));
91             return paint;
92         }
93         return nullptr;
94     }
95 
GetFocusPattern()96     FocusPattern GetFocusPattern() const override
97     {
98         if (type_ == XComponentType::NODE) {
99             return { FocusType::SCOPE, true };
100         }
101         FocusPattern focusPattern = { FocusType::NODE, false };
102         focusPattern.SetIsFocusActiveWhenFocused(true);
103         return focusPattern;
104     }
105 
NeedSoftKeyboard()106     bool NeedSoftKeyboard() const override
107     {
108         return nativeXComponentImpl_ ? nativeXComponentImpl_->IsNeedSoftKeyboard() : false;
109     }
110 
GetNativeXComponent()111     std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, std::weak_ptr<OH_NativeXComponent>> GetNativeXComponent()
112     {
113         if (!nativeXComponent_ || !nativeXComponentImpl_) {
114             // for XComponentType::NODE
115             nativeXComponentImpl_ = AceType::MakeRefPtr<NativeXComponentImpl>();
116             nativeXComponent_ = std::make_shared<OH_NativeXComponent>(AceType::RawPtr(nativeXComponentImpl_));
117         }
118         return std::make_pair(nativeXComponentImpl_, nativeXComponent_);
119     }
120 
121     void NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent,
122         const std::vector<XComponentTouchPoint>& xComponentTouchPoints);
123     void NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent);
124     void NativeXComponentDispatchAxisEvent(AxisEvent* axisEvent);
125 
126     void InitXComponent();
127     void InitNativeXComponent();
128     void InitNativeWindow(float textureWidth, float textureHeight);
129     void XComponentSizeInit();
130     void XComponentSizeChange(const RectF& surfaceRect, bool needFireNativeEvent);
NativeXComponentInit()131     void NativeXComponentInit()
132     {
133         if (!isTypedNode_) {
134             OnSurfaceCreated();
135         }
136     }
137 
GetId()138     std::string GetId() const
139     {
140         if (id_.has_value()) {
141             return id_.value();
142         }
143         auto host = GetHost();
144         return "nodeId:" + (host ? std::to_string(host->GetId()) : "-1");
145     }
146 
SetId(const std::string & id)147     void SetId(const std::string& id)
148     {
149         id_ = id;
150     }
151 
GetLibraryName()152     const std::optional<std::string>& GetLibraryName() const
153     {
154         return libraryname_;
155     }
156 
SetLibraryName(const std::optional<std::string> & libraryname)157     void SetLibraryName(const std::optional<std::string>& libraryname)
158     {
159         libraryname_ = libraryname;
160     }
161 
GetSoPath()162     const std::optional<std::string>& GetSoPath() const
163     {
164         return soPath_;
165     }
166 
SetSoPath(const std::string & soPath)167     void SetSoPath(const std::string& soPath)
168     {
169         soPath_ = soPath;
170     }
171 
GetType()172     XComponentType GetType()
173     {
174         return type_;
175     }
176 
SetType(XComponentType type)177     void SetType(XComponentType type)
178     {
179         type_ = type;
180     }
181 
GetDrawSize()182     const SizeF& GetDrawSize() const
183     {
184         return drawSize_;
185     }
186 
GetSurfaceSize()187     const SizeF& GetSurfaceSize() const
188     {
189         return surfaceSize_;
190     }
191 
GetLocalPosition()192     const OffsetF& GetLocalPosition() const
193     {
194         return localPosition_;
195     }
196 
197     OffsetF GetOffsetRelativeToWindow();
198 
GetRenderContextForSurface()199     const RefPtr<RenderContext>& GetRenderContextForSurface()
200     {
201         return renderContextForSurface_;
202     }
203 
204     void SetSurfaceRotation(bool isLock);
205 
GetSurfaceRotation()206     bool GetSurfaceRotation()
207     {
208         return isSurfaceLock_;
209     }
210 
SetIsTypeNode(bool isTypeNode)211     void SetIsTypeNode(bool isTypeNode)
212     {
213         isTypedNode_ = isTypeNode;
214     }
215 
GetXComponentController()216     std::shared_ptr<InnerXComponentController> GetXComponentController()
217     {
218         return xcomponentController_;
219     }
220 
221     void SetHandlingRenderContextForSurface(const RefPtr<RenderContext>& otherRenderContext);
222 
223     void RestoreHandlingRenderContextForSurface();
224 
225     XComponentControllerErrorCode SetExtController(const RefPtr<XComponentPattern>& extPattern);
226     XComponentControllerErrorCode ResetExtController(const RefPtr<XComponentPattern>& extPattern);
227 
SetExpectedRateRangeInit()228     void SetExpectedRateRangeInit()
229     {
230         CHECK_NULL_VOID(nativeXComponentImpl_);
231         nativeXComponentImpl_->SetExpectedRateRangeEventCallback([weak = AceType::WeakClaim(this)]() {
232             auto xComponentPattern = weak.Upgrade();
233             CHECK_NULL_VOID(xComponentPattern);
234             xComponentPattern->HandleSetExpectedRateRangeEvent();
235         });
236     }
237 
OnFrameEventInit()238     void OnFrameEventInit()
239     {
240         CHECK_NULL_VOID(nativeXComponentImpl_);
241         nativeXComponentImpl_->SetOnFrameEventCallback([weak = AceType::WeakClaim(this)]() {
242             auto xComponentPattern = weak.Upgrade();
243             CHECK_NULL_VOID(xComponentPattern);
244             xComponentPattern->HandleOnFrameEvent();
245         });
246     }
247 
UnregisterOnFrameEventInit()248     void UnregisterOnFrameEventInit()
249     {
250         CHECK_NULL_VOID(nativeXComponentImpl_);
251         nativeXComponentImpl_->SetUnregisterOnFrameEventCallback([weak = AceType::WeakClaim(this)]() {
252             auto xComponentPattern = weak.Upgrade();
253             CHECK_NULL_VOID(xComponentPattern);
254             xComponentPattern->HandleUnregisterOnFrameEvent();
255         });
256     }
257 
SetXcomponentInit(bool isInit)258     void SetXcomponentInit(bool isInit)
259     {
260         hasXComponentInit_ = isInit;
261     }
262 
263     bool ChangeRenderType(NodeRenderType renderType);
264 
SetRenderType(NodeRenderType renderType)265     void SetRenderType(NodeRenderType renderType)
266     {
267         renderType_ = renderType;
268     }
269 
UpdateTransformHintChangedCallbackId(std::optional<int32_t> id)270     void UpdateTransformHintChangedCallbackId(std::optional<int32_t> id)
271     {
272         transformHintChangedCallbackId_ = id;
273     }
274 
HasTransformHintChangedCallbackId()275     bool HasTransformHintChangedCallbackId()
276     {
277         return transformHintChangedCallbackId_.has_value();
278     }
279 
NeedTriggerLoadEventImmediately()280     bool NeedTriggerLoadEventImmediately() const
281     {
282         return isTypedNode_ && isNativeXComponent_ && hasLoadNativeDone_;
283     }
284 
285     void SetExportTextureSurfaceId(const std::string& surfaceId);
286     void FireExternalEvent(RefPtr<NG::PipelineContext> context,
287         const std::string& componentId, const uint32_t nodeId, const bool isDestroy);
288     void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight);
289 
290     // accessibility
291     void InitializeAccessibility();
292     void UninitializeAccessibility();
293     bool OnAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId);
294     bool OnAccessibilityChildTreeDeregister();
295     void OnSetAccessibilityChildTree(int32_t childWindowId, int32_t childTreeId);
SetAccessibilityState(bool state)296     void SetAccessibilityState(bool state) {}
297     RefPtr<AccessibilitySessionAdapter> GetAccessibilitySessionAdapter() override;
298     void InitializeAccessibilityCallback();
299     void HandleRegisterAccessibilityEvent(bool isRegister);
300 
301     void SetIdealSurfaceWidth(float surfaceWidth);
302     void SetIdealSurfaceHeight(float surfaceHeight);
303     void SetIdealSurfaceOffsetX(float offsetX);
304     void SetIdealSurfaceOffsetY(float offsetY);
305     void ClearIdealSurfaceOffset(bool isXAxis);
306     std::tuple<bool, bool, bool> UpdateSurfaceRect();
307     void HandleSurfaceChangeEvent(bool needForceRender, bool offsetChanged, bool sizeChanged, bool needFireNativeEvent,
308         bool frameOffsetChange = false);
309     void EnableAnalyzer(bool enable);
310     void SetImageAIOptions(void* options);
311     void StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed);
312     void StopImageAnalyzer();
313     RectF AdjustPaintRect(float positionX, float positionY, float width, float height, bool isRound);
314     float RoundValueToPixelGrid(float value, bool isRound, bool forceCeil, bool forceFloor);
315     void OnSurfaceDestroyed();
316     void SetRenderFit(RenderFit renderFit);
317     void EnableSecure(bool isSecure);
318 
319 private:
320     void OnAttachToFrameNode() override;
321     void OnDetachFromFrameNode(FrameNode* frameNode) override;
322     void BeforeSyncGeometryProperties(const DirtySwapConfig& config) override;
323     void OnRebuildFrame() override;
324     void OnAreaChangedInner() override;
325     void OnWindowHide() override;
326     void OnWindowShow() override;
327     void OnModifyDone() override;
328     void DumpInfo() override;
329     void DumpAdvanceInfo() override;
330     void OnAttachContext(PipelineContext *context) override;
331     void OnDetachContext(PipelineContext *context) override;
332     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
333 
334     void NativeXComponentOffset(double x, double y);
335 
336     void LoadNative();
337     void OnNativeLoad(FrameNode* frameNode);
338     void OnNativeUnload(FrameNode* frameNode);
339 
340     void OnSurfaceCreated();
341     void OnSurfaceChanged(const RectF& surfaceRect, bool needResizeNativeWindow);
342 
343     void NativeSurfaceShow();
344     void NativeSurfaceHide();
345 
346     void Initialize();
347     void InitController();
348     void InitSurface();
349     void InitNativeNodeCallbacks();
350     void InitEvent();
351     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
352     void InitOnTouchIntercept(const RefPtr<GestureEventHub>& gestureHub);
353     void InitAxisEvent(const RefPtr<InputEventHub>& inputHub);
354     void HandleTouchEvent(const TouchEventInfo& info);
355     void InitMouseEvent(const RefPtr<InputEventHub>& inputHub);
356     void HandleMouseEvent(const MouseInfo& info);
357     void HandleAxisEvent(const AxisInfo& info);
358     void InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub);
359     void HandleMouseHoverEvent(bool isHover);
360     void InitFocusEvent(const RefPtr<FocusHub>& focusHub);
361     void HandleFocusEvent();
362     bool HandleKeyEvent(const KeyEvent& event);
363     void HandleBlurEvent();
364     ExternalEvent CreateExternalEvent();
365 
366     void SetTouchPoint(
367         const std::list<TouchLocationInfo>& touchInfoList, int64_t timeStamp, const TouchType& touchType);
368     void HandleSetExpectedRateRangeEvent();
369     void HandleOnFrameEvent();
370     void HandleUnregisterOnFrameEvent();
371     bool ExportTextureAvailable();
372     void AddAfterLayoutTaskForExportTexture();
373     bool DoTextureExport();
374     bool StopTextureExport();
375     void InitializeRenderContext();
376     void SetSurfaceNodeToGraphic();
377     bool IsSupportImageAnalyzerFeature();
378     void CreateAnalyzerOverlay();
379     void DestroyAnalyzerOverlay();
380     void UpdateAnalyzerOverlay();
381     void UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode);
382     void ReleaseImageAnalyzer();
383     void UpdateTransformHint();
384     void SetRotation(uint32_t rotation);
385 
386 #ifdef RENDER_EXTRACT_SUPPORTED
387     RenderSurface::RenderSurfaceType CovertToRenderSurfaceType(const XComponentType& hostType);
388     void RegisterRenderContextCallBack();
389     void RequestFocus();
390 #endif
391 
392     std::vector<OH_NativeXComponent_HistoricalPoint> SetHistoryPoint(const std::list<TouchLocationInfo>& touchInfoList);
393     std::optional<std::string> id_;
394     XComponentType type_;
395     std::optional<std::string> libraryname_;
396     std::shared_ptr<InnerXComponentController> xcomponentController_;
397     std::optional<std::string> soPath_;
398 
399     RefPtr<RenderSurface> renderSurface_;
400     RefPtr<RenderContext> renderContextForSurface_;
401     RefPtr<RenderContext> handlingSurfaceRenderContext_;
402     WeakPtr<XComponentPattern> extPattern_;
403 
404     std::shared_ptr<OH_NativeXComponent> nativeXComponent_;
405     RefPtr<NativeXComponentImpl> nativeXComponentImpl_;
406 
407     bool hasXComponentInit_ = false;
408 
409     RefPtr<TouchEventImpl> touchEvent_;
410     OH_NativeXComponent_TouchEvent touchEventPoint_ = {};
411     RefPtr<InputEvent> mouseEvent_;
412     RefPtr<InputEvent> axisEvent_;
413     RefPtr<InputEvent> mouseHoverEvent_;
414     std::vector<XComponentTouchPoint> nativeXComponentTouchPoints_;
415     RefPtr<XComponentExtSurfaceCallbackClient> extSurfaceClient_;
416     SizeF initSize_;
417     OffsetF localPosition_;
418     OffsetF globalPosition_;
419     SizeF drawSize_;
420     SizeF surfaceSize_;
421     RefPtr<UIDisplaySync> displaySync_ = AceType::MakeRefPtr<UIDisplaySync>(UIObjectType::DISPLAYSYNC_XCOMPONENT);
422 
423     std::optional<float> selfIdealSurfaceWidth_;
424     std::optional<float> selfIdealSurfaceHeight_;
425     std::optional<float> selfIdealSurfaceOffsetX_;
426     std::optional<float> selfIdealSurfaceOffsetY_;
427     std::string surfaceId_;
428     void* nativeWindow_ = nullptr;
429 
430     bool isSurfaceLock_ = false;
431     uint32_t windowId_ = 0;
432     int32_t treeId_ = 0;
433     std::shared_ptr<AccessibilityChildTreeCallback> accessibilityChildTreeCallback_;
434     RefPtr<XComponentAccessibilityProvider> accessibilityProvider_;
435     RefPtr<AccessibilitySessionAdapter> accessibilitySessionAdapter_;
436 
437     // for export texture
438     NodeRenderType renderType_ = NodeRenderType::RENDER_TYPE_DISPLAY;
439     uint64_t exportTextureSurfaceId_ = 0U;
440     bool hasReleasedSurface_ = false;
441     std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_;
442     bool isEnableAnalyzer_ = false;
443     std::optional<int32_t> transformHintChangedCallbackId_;
444     uint32_t rotation_ = 0;
445     bool isTypedNode_ = false;
446     bool isNativeXComponent_ = false;
447     bool hasLoadNativeDone_ = false;
448     bool isEnableSecure_ = false;
449 };
450 } // namespace OHOS::Ace::NG
451 
452 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H
453