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 GetSurfaceOffset()192 const OffsetF& GetSurfaceOffset() const 193 { 194 return surfaceOffset_; 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 HandleSurfaceCreated(); 318 void HandleSurfaceDestroyed(); ChangeSurfaceCallbackMode(SurfaceCallbackMode mode)319 void ChangeSurfaceCallbackMode(SurfaceCallbackMode mode) 320 { 321 if (surfaceCallbackModeChangeEvent_) { 322 surfaceCallbackModeChangeEvent_(mode); 323 } 324 } 325 void OnSurfaceCallbackModeChange(SurfaceCallbackMode mode); 326 void EnableSecure(bool isSecure); 327 328 private: 329 void OnAttachToFrameNode() override; 330 void OnDetachFromFrameNode(FrameNode* frameNode) override; 331 void BeforeSyncGeometryProperties(const DirtySwapConfig& config) override; 332 void OnRebuildFrame() override; 333 void OnAreaChangedInner() override; 334 void OnWindowHide() override; 335 void OnWindowShow() override; 336 void OnModifyDone() override; 337 void DumpInfo() override; 338 void DumpAdvanceInfo() override; 339 void OnAttachContext(PipelineContext *context) override; 340 void OnDetachContext(PipelineContext *context) override; 341 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 342 343 void NativeXComponentOffset(double x, double y); 344 345 void LoadNative(); 346 void OnNativeLoad(FrameNode* frameNode); 347 void OnNativeUnload(FrameNode* frameNode); 348 349 void OnSurfaceCreated(); 350 void OnSurfaceChanged(const RectF& surfaceRect, bool needResizeNativeWindow); 351 352 void NativeSurfaceShow(); 353 void NativeSurfaceHide(); 354 355 void Initialize(); 356 void InitController(); 357 void InitSurface(); 358 void InitNativeNodeCallbacks(); 359 void InitEvent(); 360 void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub); 361 void InitOnTouchIntercept(const RefPtr<GestureEventHub>& gestureHub); 362 void InitAxisEvent(const RefPtr<InputEventHub>& inputHub); 363 void HandleTouchEvent(const TouchEventInfo& info); 364 void InitMouseEvent(const RefPtr<InputEventHub>& inputHub); 365 void HandleMouseEvent(const MouseInfo& info); 366 void HandleAxisEvent(const AxisInfo& info); 367 void InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub); 368 void HandleMouseHoverEvent(bool isHover); 369 void InitFocusEvent(const RefPtr<FocusHub>& focusHub); 370 void HandleFocusEvent(); 371 bool HandleKeyEvent(const KeyEvent& event); 372 void HandleBlurEvent(); 373 ExternalEvent CreateExternalEvent(); 374 375 void SetTouchPoint( 376 const std::list<TouchLocationInfo>& touchInfoList, int64_t timeStamp, const TouchType& touchType); 377 void HandleSetExpectedRateRangeEvent(); 378 void HandleOnFrameEvent(); 379 void HandleUnregisterOnFrameEvent(); 380 bool ExportTextureAvailable(); 381 void AddAfterLayoutTaskForExportTexture(); 382 bool DoTextureExport(); 383 bool StopTextureExport(); 384 void InitializeRenderContext(); 385 void SetSurfaceNodeToGraphic(); 386 bool IsSupportImageAnalyzerFeature(); 387 void CreateAnalyzerOverlay(); 388 void DestroyAnalyzerOverlay(); 389 void UpdateAnalyzerOverlay(); 390 void UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode); 391 void ReleaseImageAnalyzer(); 392 void UpdateTransformHint(); 393 void SetRotation(uint32_t rotation); 394 void RegisterSurfaceCallbackModeEvent(); 395 396 #ifdef RENDER_EXTRACT_SUPPORTED 397 RenderSurface::RenderSurfaceType CovertToRenderSurfaceType(const XComponentType& hostType); 398 void RegisterRenderContextCallBack(); 399 void RequestFocus(); 400 #endif 401 402 std::vector<OH_NativeXComponent_HistoricalPoint> SetHistoryPoint(const std::list<TouchLocationInfo>& touchInfoList); 403 std::optional<std::string> id_; 404 XComponentType type_; 405 std::optional<std::string> libraryname_; 406 std::shared_ptr<InnerXComponentController> xcomponentController_; 407 std::optional<std::string> soPath_; 408 409 RefPtr<RenderSurface> renderSurface_; 410 RefPtr<RenderContext> renderContextForSurface_; 411 RefPtr<RenderContext> handlingSurfaceRenderContext_; 412 WeakPtr<XComponentPattern> extPattern_; 413 414 std::shared_ptr<OH_NativeXComponent> nativeXComponent_; 415 RefPtr<NativeXComponentImpl> nativeXComponentImpl_; 416 417 bool hasXComponentInit_ = false; 418 419 RefPtr<TouchEventImpl> touchEvent_; 420 OH_NativeXComponent_TouchEvent touchEventPoint_ = {}; 421 RefPtr<InputEvent> mouseEvent_; 422 RefPtr<InputEvent> axisEvent_; 423 RefPtr<InputEvent> mouseHoverEvent_; 424 std::vector<XComponentTouchPoint> nativeXComponentTouchPoints_; 425 RefPtr<XComponentExtSurfaceCallbackClient> extSurfaceClient_; 426 SizeF initSize_; 427 OffsetF localPosition_; 428 OffsetF globalPosition_; 429 OffsetF surfaceOffset_; 430 SizeF drawSize_; 431 SizeF surfaceSize_; 432 RectF paintRect_; 433 RefPtr<UIDisplaySync> displaySync_ = AceType::MakeRefPtr<UIDisplaySync>(UIObjectType::DISPLAYSYNC_XCOMPONENT); 434 435 std::optional<float> selfIdealSurfaceWidth_; 436 std::optional<float> selfIdealSurfaceHeight_; 437 std::optional<float> selfIdealSurfaceOffsetX_; 438 std::optional<float> selfIdealSurfaceOffsetY_; 439 std::string surfaceId_; 440 void* nativeWindow_ = nullptr; 441 442 bool isSurfaceLock_ = false; 443 uint32_t windowId_ = 0; 444 int32_t treeId_ = 0; 445 std::shared_ptr<AccessibilityChildTreeCallback> accessibilityChildTreeCallback_; 446 RefPtr<XComponentAccessibilityProvider> accessibilityProvider_; 447 RefPtr<AccessibilitySessionAdapter> accessibilitySessionAdapter_; 448 449 // for export texture 450 NodeRenderType renderType_ = NodeRenderType::RENDER_TYPE_DISPLAY; 451 uint64_t exportTextureSurfaceId_ = 0U; 452 bool hasReleasedSurface_ = false; 453 std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_; 454 bool isEnableAnalyzer_ = false; 455 std::optional<int32_t> transformHintChangedCallbackId_; 456 uint32_t rotation_ = 0; 457 bool isTypedNode_ = false; 458 bool isNativeXComponent_ = false; 459 bool hasLoadNativeDone_ = false; 460 bool isEnableSecure_ = false; 461 SurfaceCallbackMode surfaceCallbackMode_ = SurfaceCallbackMode::DEFAULT; 462 std::function<void(SurfaceCallbackMode)> surfaceCallbackModeChangeEvent_; 463 }; 464 } // namespace OHOS::Ace::NG 465 466 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H 467