1 /* 2 * Copyright (c) 2021-2022 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_XCOMPONENT_XCOMPONENT_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_H 18 19 #include <string> 20 21 #include "base/geometry/size.h" 22 #include "base/utils/utils.h" 23 #include "core/components/declaration/xcomponent/xcomponent_declaration.h" 24 #include "core/components/xcomponent/native_interface_xcomponent_impl.h" 25 #include "core/components/xcomponent/resource/native_texture.h" 26 #include "core/pipeline/base/element.h" 27 28 namespace OHOS::Ace { 29 class XComponentDelegate; 30 class RenderXComponent; 31 class XComponentComponent; 32 33 using TaskFunction = std::function<void(RenderXComponent&, const Offset&)>; 34 using PushTaskFunction = std::function<void(const TaskFunction&)>; 35 36 class ACE_EXPORT XComponentTaskPool : virtual public AceType { 37 DECLARE_ACE_TYPE(XComponentTaskPool, AceType); 38 39 public: PushTask(const TaskFunction & task)40 void PushTask(const TaskFunction& task) 41 { 42 if (pushToRenderNodeFunc_) { 43 pushToRenderNodeFunc_(task); 44 } else { 45 tasks_.emplace_back(task); 46 } 47 } 48 SetPushToRenderNodeFunc(const PushTaskFunction & pushTaskFunc)49 void SetPushToRenderNodeFunc(const PushTaskFunction& pushTaskFunc) 50 { 51 pushToRenderNodeFunc_ = pushTaskFunc; 52 } 53 GetTasks()54 const std::list<TaskFunction>& GetTasks() const 55 { 56 return tasks_; 57 } 58 ClearTasks()59 void ClearTasks() 60 { 61 tasks_.clear(); 62 } 63 64 void SetRenderNode(const WeakPtr<RenderXComponent>& renderNode); 65 void NativeXComponentInit( 66 OH_NativeXComponent* nativeXComponent, 67 WeakPtr<NativeXComponentImpl> nativeXComponentImpl); 68 69 WeakPtr<XComponentComponent> component_; 70 71 private: 72 PushTaskFunction pushToRenderNodeFunc_; 73 std::list<TaskFunction> tasks_; 74 WeakPtr<RenderXComponent> renderNode_; 75 }; 76 77 class XComponentController : public virtual AceType { 78 DECLARE_ACE_TYPE(XComponentController, AceType); 79 80 public: 81 using ConfigSurfaceImpl = std::function<void(uint32_t, uint32_t)>; 82 GetSurfaceId()83 uint64_t GetSurfaceId() 84 { 85 return surfaceId_; 86 } 87 ConfigSurface(uint32_t surfaceWidth,uint32_t surfaceHeight)88 void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight) 89 { 90 if (ConfigSurfaceImpl_) { 91 ConfigSurfaceImpl_(surfaceWidth, surfaceHeight); 92 } 93 } 94 SetConfigSurfaceImpl(ConfigSurfaceImpl && ConfigSurfaceImpl)95 void SetConfigSurfaceImpl(ConfigSurfaceImpl&& ConfigSurfaceImpl) 96 { 97 ConfigSurfaceImpl_ = std::move(ConfigSurfaceImpl); 98 } 99 100 public: AddXComponentController(const RefPtr<XComponentController> & xcomponentController)101 void AddXComponentController(const RefPtr<XComponentController>& xcomponentController) 102 { 103 auto it = std::find(controllers_.begin(), controllers_.end(), xcomponentController); 104 if (it != controllers_.end()) { 105 LOGW("Controller is already existed"); 106 return; 107 } 108 controllers_.emplace_back(xcomponentController); 109 } 110 111 uint64_t surfaceId_ = 0; 112 RemoveXComponentController(const RefPtr<XComponentController> & xcomponentController)113 void RemoveXComponentController(const RefPtr<XComponentController>& xcomponentController) 114 { 115 if (xcomponentController) { 116 controllers_.remove(xcomponentController); 117 } 118 } 119 Clear()120 void Clear() 121 { 122 controllers_.clear(); 123 } 124 125 private: 126 std::list<RefPtr<XComponentController>> controllers_; 127 ConfigSurfaceImpl ConfigSurfaceImpl_; 128 }; 129 130 // A component can show different native view. 131 class ACE_EXPORT XComponentComponent : public RenderComponent { 132 DECLARE_ACE_TYPE(XComponentComponent, RenderComponent); 133 134 public: 135 using CreatedCallback = std::function<void()>; 136 using ReleasedCallback = std::function<void(bool)>; 137 using ErrorCallback = std::function<void(const std::string&, const std::string&)>; 138 using MethodCall = std::function<void(const std::string&)>; 139 using Method = std::string; 140 141 XComponentComponent() = default; 142 explicit XComponentComponent(const std::string& type); 143 ~XComponentComponent() override = default; 144 145 RefPtr<RenderNode> CreateRenderNode() override; 146 RefPtr<Element> CreateElement() override; 147 SetType(const std::string & type)148 void SetType(const std::string& type) 149 { 150 type_ = type; 151 } 152 GetType()153 const std::string& GetType() const 154 { 155 return type_; 156 } 157 SetName(const std::string & name)158 void SetName(const std::string& name) 159 { 160 declaration_->SetXComponentName(name); 161 } 162 GetName()163 const std::string& GetName() const 164 { 165 return declaration_->GetXComponentName(); 166 } 167 SetId(const std::string & id)168 void SetId(const std::string& id) 169 { 170 declaration_->SetXComponentId(id); 171 } 172 GetId()173 const std::string& GetId() const 174 { 175 return declaration_->GetXComponentId(); 176 } 177 SetXComponentType(const std::string & type)178 void SetXComponentType(const std::string& type) 179 { 180 declaration_->SetXComponentType(type); 181 } 182 GetXComponentType()183 const std::string& GetXComponentType() const 184 { 185 return declaration_->GetXComponentType(); 186 } 187 SetLibraryName(const std::string & libraryName)188 void SetLibraryName(const std::string& libraryName) 189 { 190 declaration_->SetXComponentLibraryName(libraryName); 191 } 192 GetLibraryName()193 const std::string& GetLibraryName() const 194 { 195 return declaration_->GetXComponentLibraryName(); 196 } 197 SetXComponentInitEventId(const EventMarker & xComponentInitEventId)198 void SetXComponentInitEventId(const EventMarker& xComponentInitEventId) 199 { 200 declaration_->SetXComponentInitEventId(xComponentInitEventId); 201 } 202 GetXComponentInitEventId()203 const EventMarker& GetXComponentInitEventId() const 204 { 205 return declaration_->GetXComponentInitEventId(); 206 } 207 SetXComponentDestroyEventId(const EventMarker & xComponentDestroyEventId)208 void SetXComponentDestroyEventId(const EventMarker& xComponentDestroyEventId) 209 { 210 declaration_->SetXComponentDestroyEventId(xComponentDestroyEventId); 211 } 212 GetXComponentDestroyEventId()213 const EventMarker& GetXComponentDestroyEventId() const 214 { 215 return declaration_->GetXComponentDestroyEventId(); 216 } 217 SetDeclaration(const RefPtr<XComponentDeclaration> & declaration)218 void SetDeclaration(const RefPtr<XComponentDeclaration>& declaration) 219 { 220 if (declaration) { 221 declaration_ = declaration; 222 } 223 } 224 GetTaskPool()225 const RefPtr<XComponentTaskPool>& GetTaskPool() const 226 { 227 return pool_; 228 } 229 GetTextureId()230 int64_t GetTextureId() const 231 { 232 return textureId_; 233 } 234 SetTextureId(int64_t textureId)235 void SetTextureId(int64_t textureId) 236 { 237 textureId_ = textureId; 238 } 239 GetNodeId()240 int32_t GetNodeId() const 241 { 242 return nodeId_; 243 } 244 SetNodeId(int32_t nodeId)245 void SetNodeId(int32_t nodeId) 246 { 247 nodeId_ = nodeId; 248 } 249 GetTexture()250 RefPtr<NativeTexture> GetTexture() const 251 { 252 return texture_; 253 } 254 SetTexture(RefPtr<NativeTexture> texture)255 void SetTexture(RefPtr<NativeTexture> texture) 256 { 257 texture_ = texture; 258 } 259 GetXComponentController()260 RefPtr<XComponentController> GetXComponentController() const 261 { 262 return xcomponentController_; 263 } 264 SetXComponentController(const RefPtr<XComponentController> & xcomponentController)265 void SetXComponentController(const RefPtr<XComponentController>& xcomponentController) 266 { 267 xcomponentController_ = xcomponentController; 268 } 269 270 #ifdef OHOS_STANDARD_SYSTEM SetNativeWindow(void * nativeWindow)271 void SetNativeWindow(void* nativeWindow) 272 { 273 nativeWindow_ = nativeWindow; 274 } 275 GetNativeWindow()276 void* GetNativeWindow() 277 { 278 return nativeWindow_; 279 } 280 #endif 281 282 private: 283 RefPtr<XComponentDeclaration> declaration_; 284 CreatedCallback createdCallback_ = nullptr; 285 ReleasedCallback releasedCallback_ = nullptr; 286 ErrorCallback errorCallback_ = nullptr; 287 RefPtr<XComponentDelegate> delegate_; 288 RefPtr<XComponentTaskPool> pool_; 289 RefPtr<XComponentController> xcomponentController_; 290 std::string type_; 291 int64_t textureId_ = -1; 292 int32_t nodeId_ = -1; 293 RefPtr<NativeTexture> texture_; 294 #ifdef OHOS_STANDARD_SYSTEM 295 void* nativeWindow_ = nullptr; 296 #endif 297 }; 298 } // namespace OHOS::Ace 299 300 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_H 301