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 #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/size_t.h" 26 #include "base/geometry/size.h" 27 #include "base/memory/referenced.h" 28 #include "base/utils/utils.h" 29 #include "core/common/thread_checker.h" 30 #include "core/components/common/layout/constants.h" 31 #include "core/components/xcomponent/native_interface_xcomponent_impl.h" 32 #include "core/components_ng/event/focus_hub.h" 33 #include "core/components_ng/event/input_event.h" 34 #include "core/components_ng/pattern/pattern.h" 35 #include "core/components_ng/pattern/xcomponent/inner_xcomponent_controller.h" 36 #include "core/components_ng/pattern/xcomponent/xcomponent_event_hub.h" 37 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" 38 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_property.h" 39 #include "core/components_ng/pattern/xcomponent/xcomponent_paint_method.h" 40 #include "core/components_ng/property/property.h" 41 #include "core/components_ng/render/render_surface.h" 42 #include "core/pipeline_ng/pipeline_context.h" 43 #include "core/components_ng/manager/display_sync/ui_display_sync.h" 44 45 namespace OHOS::Ace::NG { 46 class XComponentExtSurfaceCallbackClient; 47 class XComponentPattern : public Pattern { 48 DECLARE_ACE_TYPE(XComponentPattern, Pattern); 49 50 public: 51 XComponentPattern() = default; 52 XComponentPattern(const std::string& id, XComponentType type, const std::string& libraryname, 53 const std::shared_ptr<InnerXComponentController>& xcomponentController, float initWidth = 0.0f, 54 float initHeight = 0.0f); 55 ~XComponentPattern() override = default; 56 IsAtomicNode()57 bool IsAtomicNode() const override 58 { 59 return type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE || type_ == XComponentType::NODE; 60 } 61 CreateLayoutProperty()62 RefPtr<LayoutProperty> CreateLayoutProperty() override 63 { 64 return MakeRefPtr<XComponentLayoutProperty>(); 65 } 66 CreateEventHub()67 RefPtr<EventHub> CreateEventHub() override 68 { 69 return MakeRefPtr<XComponentEventHub>(); 70 } 71 CreateLayoutAlgorithm()72 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 73 { 74 return MakeRefPtr<XComponentLayoutAlgorithm>(); 75 } 76 CreateNodePaintMethod()77 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 78 { 79 if (type_ == XComponentType::TEXTURE) { 80 auto paint = MakeRefPtr<XComponentPaintMethod>(renderSurface_); 81 return paint; 82 } 83 return nullptr; 84 } 85 GetFocusPattern()86 FocusPattern GetFocusPattern() const override 87 { 88 return { FocusType::NODE, false }; 89 } 90 GetNativeXComponent()91 std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, std::weak_ptr<OH_NativeXComponent>> GetNativeXComponent() 92 { 93 if (!nativeXComponent_ || !nativeXComponentImpl_) { 94 nativeXComponentImpl_ = AceType::MakeRefPtr<NativeXComponentImpl>(); 95 nativeXComponent_ = std::make_shared<OH_NativeXComponent>(AceType::RawPtr(nativeXComponentImpl_)); 96 } 97 return std::make_pair(nativeXComponentImpl_, nativeXComponent_); 98 } 99 NativeXComponentInit()100 void NativeXComponentInit() 101 { 102 CHECK_RUN_ON(UI); 103 CHECK_NULL_VOID(nativeXComponentImpl_); 104 CHECK_NULL_VOID(nativeXComponent_); 105 auto host = GetHost(); 106 CHECK_NULL_VOID(host); 107 SetExpectedRateRangeInit(); 108 OnFrameEventInit(); 109 UnregisterOnFrameEventInit(); 110 auto width = initSize_.Width(); 111 auto height = initSize_.Height(); 112 nativeXComponentImpl_->SetXComponentWidth(static_cast<uint32_t>(width)); 113 nativeXComponentImpl_->SetXComponentHeight(static_cast<uint32_t>(height)); 114 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface()); 115 const auto* callback = nativeXComponentImpl_->GetCallback(); 116 if (callback && callback->OnSurfaceCreated != nullptr) { 117 callback->OnSurfaceCreated(nativeXComponent_.get(), surface); 118 } 119 } 120 121 void OnPaint(); 122 void NativeXComponentOffset(double x, double y); 123 void NativeXComponentChange(float width, float height); 124 void NativeXComponentDestroy(); 125 void NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent, 126 const std::vector<XComponentTouchPoint>& xComponentTouchPoints); 127 void NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent); 128 129 void InitNativeWindow(float textureWidth, float textureHeight); 130 void XComponentSizeInit(); 131 void XComponentSizeChange(float textureWidth, float textureHeight); 132 GetNativeWindow()133 void* GetNativeWindow() 134 { 135 return renderSurface_->GetNativeWindow(); 136 } 137 GetId()138 const std::string& GetId() const 139 { 140 return id_; 141 } 142 SetId(const std::string & id)143 void SetId(const std::string& id) 144 { 145 id_ = id; 146 } 147 GetLibraryName()148 const std::string& GetLibraryName() const 149 { 150 return libraryname_; 151 } 152 GetSoPath()153 const std::optional<std::string>& GetSoPath() const 154 { 155 return soPath_; 156 } 157 SetSoPath(const std::string & soPath)158 void SetSoPath(const std::string& soPath) 159 { 160 soPath_ = soPath; 161 } 162 GetType()163 XComponentType GetType() 164 { 165 return type_; 166 } 167 SetType(XComponentType type)168 void SetType(XComponentType type) 169 { 170 type_ = type; 171 } 172 GetDrawSize()173 SizeF GetDrawSize() 174 { 175 return drawSize_; 176 } 177 GetGlobalPosition()178 OffsetF GetGlobalPosition() 179 { 180 return globalPosition_; 181 } 182 183 OffsetF GetOffsetRelativeToWindow(); 184 GetRenderContextForSurface()185 const RefPtr<RenderContext>& GetRenderContextForSurface() 186 { 187 return renderContextForSurface_; 188 } 189 190 void SetHandlingRenderContextForSurface(const RefPtr<RenderContext>& otherRenderContext); 191 192 void RestoreHandlingRenderContextForSurface(); 193 194 XComponentControllerErrorCode SetExtController(const RefPtr<XComponentPattern>& extPattern); 195 XComponentControllerErrorCode ResetExtController(const RefPtr<XComponentPattern>& extPattern); 196 SetExpectedRateRangeInit()197 void SetExpectedRateRangeInit() 198 { 199 CHECK_NULL_VOID(nativeXComponentImpl_); 200 nativeXComponentImpl_->SetExpectedRateRangeEventCallback([weak = AceType::WeakClaim(this)]() { 201 auto xComponentPattern = weak.Upgrade(); 202 CHECK_NULL_VOID(xComponentPattern); 203 xComponentPattern->HandleSetExpectedRateRangeEvent(); 204 }); 205 } 206 OnFrameEventInit()207 void OnFrameEventInit() 208 { 209 CHECK_NULL_VOID(nativeXComponentImpl_); 210 nativeXComponentImpl_->SetOnFrameEventCallback([weak = AceType::WeakClaim(this)]() { 211 auto xComponentPattern = weak.Upgrade(); 212 CHECK_NULL_VOID(xComponentPattern); 213 xComponentPattern->HandleOnFrameEvent(); 214 }); 215 } 216 UnregisterOnFrameEventInit()217 void UnregisterOnFrameEventInit() 218 { 219 CHECK_NULL_VOID(nativeXComponentImpl_); 220 nativeXComponentImpl_->SetUnregisterOnFrameEventCallback([weak = AceType::WeakClaim(this)]() { 221 auto xComponentPattern = weak.Upgrade(); 222 CHECK_NULL_VOID(xComponentPattern); 223 xComponentPattern->HandleUnregisterOnFrameEvent(); 224 }); 225 } 226 SetXcomponentInit(bool isInit)227 void SetXcomponentInit(bool isInit) 228 { 229 hasXComponentInit_ = isInit; 230 } 231 232 void Initialize(int32_t instanceId = -1); 233 234 bool ChangeRenderType(NodeRenderType renderType); 235 SetRenderType(NodeRenderType renderType)236 void SetRenderType(NodeRenderType renderType) 237 { 238 renderType_ = renderType; 239 } 240 241 void SetExportTextureSurfaceId(const std::string& surfaceId); 242 void FireExternalEvent(RefPtr<NG::PipelineContext> context, 243 const std::string& componentId, const uint32_t nodeId, const bool isDestroy); 244 void SetSurfaceSize(uint32_t surfaceWidth, uint32_t surfaceHeight); 245 private: 246 void OnAttachToFrameNode() override; 247 void OnDetachFromFrameNode(FrameNode* frameNode) override; 248 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 249 void OnRebuildFrame() override; 250 void OnAreaChangedInner() override; 251 void OnModifyDone() override; 252 253 void InitNativeNodeCallbacks(); 254 void InitEvent(); 255 void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub); 256 void HandleTouchEvent(const TouchEventInfo& info); 257 void InitMouseEvent(const RefPtr<InputEventHub>& inputHub); 258 void HandleMouseEvent(const MouseInfo& info); 259 void InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub); 260 void HandleMouseHoverEvent(bool isHover); 261 void InitFocusEvent(const RefPtr<FocusHub>& focusHub); 262 void HandleFocusEvent(); 263 bool HandleKeyEvent(const KeyEvent& event); 264 void HandleBlurEvent(); 265 ExternalEvent CreateExternalEvent(); 266 void CreateSurface(); 267 void SetMethodCall(); 268 void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight); 269 void SetTouchPoint( 270 const std::list<TouchLocationInfo>& touchInfoList, int64_t timeStamp, const TouchType& touchType); 271 void HandleSetExpectedRateRangeEvent(); 272 void HandleOnFrameEvent(); 273 void HandleUnregisterOnFrameEvent(); 274 bool ExportTextureAvailable(); 275 void AddAfterLayoutTaskForExportTexture(); 276 bool DoTextureExport(); 277 bool StopTextureExport(); 278 279 std::vector<OH_NativeXComponent_HistoricalPoint> SetHistoryPoint(const std::list<TouchLocationInfo>& touchInfoList); 280 std::string id_; 281 XComponentType type_; 282 std::string libraryname_; 283 std::shared_ptr<InnerXComponentController> xcomponentController_; 284 std::optional<std::string> soPath_; 285 286 RefPtr<RenderSurface> renderSurface_; 287 RefPtr<RenderContext> renderContextForSurface_; 288 RefPtr<RenderContext> handlingSurfaceRenderContext_; 289 WeakPtr<XComponentPattern> extPattern_; 290 291 std::shared_ptr<OH_NativeXComponent> nativeXComponent_; 292 RefPtr<NativeXComponentImpl> nativeXComponentImpl_; 293 294 bool hasXComponentInit_ = false; 295 296 RefPtr<TouchEventImpl> touchEvent_; 297 OH_NativeXComponent_TouchEvent touchEventPoint_; 298 RefPtr<InputEvent> mouseEvent_; 299 RefPtr<InputEvent> mouseHoverEvent_; 300 std::vector<XComponentTouchPoint> nativeXComponentTouchPoints_; 301 RefPtr<XComponentExtSurfaceCallbackClient> extSurfaceClient_; 302 WeakPtr<NG::PipelineContext> context_; 303 int32_t instanceId_; 304 SizeF initSize_; 305 OffsetF localposition_; 306 OffsetF globalPosition_; 307 SizeF drawSize_; 308 RefPtr<UIDisplaySync> displaySync_ = AceType::MakeRefPtr<UIDisplaySync>(); 309 NodeRenderType renderType_ = NodeRenderType::RENDER_TYPE_DISPLAY; 310 uint64_t exportTextureSurfaceId_ = 0U; 311 #ifdef OHOS_PLATFORM 312 int64_t startIncreaseTime_ = 0; 313 #endif 314 }; 315 } // namespace OHOS::Ace::NG 316 317 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H 318