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/size.h" 25 #include "base/memory/referenced.h" 26 #include "base/utils/utils.h" 27 #include "core/common/thread_checker.h" 28 #include "core/components/common/layout/constants.h" 29 #include "core/components/xcomponent/native_interface_xcomponent_impl.h" 30 #include "core/components/xcomponent/resource/native_texture.h" 31 #include "core/components/xcomponent/xcomponent_controller.h" 32 #include "core/components_ng/event/input_event.h" 33 #include "core/components_ng/pattern/pattern.h" 34 #include "core/components_ng/pattern/xcomponent/xcomponent_event_hub.h" 35 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" 36 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_property.h" 37 #include "core/components_ng/property/property.h" 38 #include "core/components_ng/render/render_surface.h" 39 #include "core/pipeline_ng/pipeline_context.h" 40 41 namespace OHOS::Ace::NG { 42 class XComponentExtSurfaceCallbackClient; 43 class XComponentPattern : public Pattern { 44 DECLARE_ACE_TYPE(XComponentPattern, Pattern); 45 46 public: 47 XComponentPattern() = default; 48 XComponentPattern(const std::string& id, XComponentType type, const std::string& libraryname, 49 const RefPtr<XComponentController>& xcomponentController); 50 ~XComponentPattern() override = default; 51 IsAtomicNode()52 bool IsAtomicNode() const override 53 { 54 return type_ == XComponentType::SURFACE; 55 } 56 CreateLayoutProperty()57 RefPtr<LayoutProperty> CreateLayoutProperty() override 58 { 59 return MakeRefPtr<XComponentLayoutProperty>(); 60 } 61 CreateEventHub()62 RefPtr<EventHub> CreateEventHub() override 63 { 64 return MakeRefPtr<XComponentEventHub>(); 65 } 66 CreateLayoutAlgorithm()67 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 68 { 69 return MakeRefPtr<XComponentLayoutAlgorithm>(); 70 } 71 GetNativeXComponent()72 std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, std::weak_ptr<OH_NativeXComponent>> GetNativeXComponent() 73 { 74 if (!nativeXComponent_ || !nativeXComponentImpl_) { 75 nativeXComponentImpl_ = AceType::MakeRefPtr<NativeXComponentImpl>(); 76 nativeXComponent_ = std::make_shared<OH_NativeXComponent>(AceType::RawPtr(nativeXComponentImpl_)); 77 } 78 return std::make_pair(nativeXComponentImpl_, nativeXComponent_); 79 } 80 NativeXComponentInit()81 void NativeXComponentInit() 82 { 83 CHECK_RUN_ON(UI); 84 CHECK_NULL_VOID(nativeXComponentImpl_); 85 CHECK_NULL_VOID(nativeXComponent_); 86 auto host = GetHost(); 87 CHECK_NULL_VOID(host); 88 auto width = initSize_.Width(); 89 auto height = initSize_.Height(); 90 nativeXComponentImpl_->SetXComponentWidth(static_cast<uint32_t>(width)); 91 nativeXComponentImpl_->SetXComponentHeight(static_cast<uint32_t>(height)); 92 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface()); 93 const auto* callback = nativeXComponentImpl_->GetCallback(); 94 if (callback && callback->OnSurfaceCreated != nullptr) { 95 callback->OnSurfaceCreated(nativeXComponent_.get(), surface); 96 } 97 } 98 99 void OnPaint(); 100 void NativeXComponentOffset(double x, double y); 101 void NativeXComponentChange(float width, float height); 102 void NativeXComponentDestroy(); 103 void NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent, 104 const std::vector<XComponentTouchPoint>& xComponentTouchPoints); 105 void NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent); 106 107 void InitNativeWindow(float textureWidth, float textureHeight); 108 void XComponentSizeInit(); 109 void XComponentSizeChange(float textureWidth, float textureHeight); 110 GetNativeWindow()111 void* GetNativeWindow() 112 { 113 return renderSurface_->GetNativeWindow(); 114 } 115 GetId()116 const std::string& GetId() const 117 { 118 return id_; 119 } 120 GetLibraryName()121 const std::string& GetLibraryName() const 122 { 123 return libraryname_; 124 } 125 GetSoPath()126 const std::optional<std::string>& GetSoPath() const 127 { 128 return soPath_; 129 } 130 SetSoPath(const std::string & soPath)131 void SetSoPath(const std::string& soPath) 132 { 133 soPath_ = soPath; 134 } 135 136 private: 137 void OnAttachToFrameNode() override; 138 void OnDetachFromFrameNode(FrameNode* frameNode) override; 139 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 140 void OnRebuildFrame() override; 141 void OnAreaChangedInner() override; 142 143 void InitEvent(); 144 void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub); 145 void HandleTouchEvent(const TouchEventInfo& info); 146 void InitMouseEvent(const RefPtr<InputEventHub>& inputHub); 147 void HandleMouseEvent(const MouseInfo& info); 148 void InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub); 149 void HandleMouseHoverEvent(bool isHover); 150 ExternalEvent CreateExternalEvent(); 151 void CreateSurface(); 152 void SetMethodCall(); 153 void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight); 154 void SetTouchPoint( 155 const std::list<TouchLocationInfo>& touchInfoList, int64_t timeStamp, const TouchType& touchType); 156 157 std::string id_; 158 XComponentType type_; 159 std::string libraryname_; 160 RefPtr<XComponentController> xcomponentController_; 161 std::optional<std::string> soPath_; 162 163 RefPtr<RenderSurface> renderSurface_; 164 RefPtr<RenderContext> renderContextForSurface_; 165 166 std::shared_ptr<OH_NativeXComponent> nativeXComponent_; 167 RefPtr<NativeXComponentImpl> nativeXComponentImpl_; 168 169 bool hasXComponentInit_ = false; 170 171 RefPtr<TouchEventImpl> touchEvent_; 172 OH_NativeXComponent_TouchEvent touchEventPoint_; 173 RefPtr<InputEvent> mouseEvent_; 174 RefPtr<InputEvent> mouseHoverEvent_; 175 std::vector<XComponentTouchPoint> nativeXComponentTouchPoints_; 176 RefPtr<XComponentExtSurfaceCallbackClient> extSurfaceClient_; 177 WeakPtr<NG::PipelineContext> context_; 178 int32_t scopeId_; 179 SizeF initSize_; 180 #ifdef OHOS_PLATFORM 181 int64_t startIncreaseTime_ = 0; 182 #endif 183 }; 184 } // namespace OHOS::Ace::NG 185 186 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_H 187