1 /* 2 * Copyright (c) 2025 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 /** 17 * @addtogroup RenderNodeDisplay 18 * @{ 19 * 20 * @brief Display render nodes. 21 */ 22 23 /** 24 * @file rs_ui_context.h 25 * 26 * @brief Defines the properties and methods for RSUIContext class. 27 */ 28 29 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_CONTEXT_H 30 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_CONTEXT_H 31 32 #include <functional> 33 #include <memory> 34 #include <mutex> 35 36 #include "animation/rs_animation.h" 37 #include "animation/rs_implicit_animator.h" 38 #include "common/rs_common_def.h" 39 #include "modifier/rs_modifier_manager.h" 40 #include "pipeline/rs_node_map_v2.h" 41 #include "transaction/rs_transaction_handler.h" 42 43 namespace OHOS { 44 namespace Rosen { 45 using TaskRunner = std::function<void(const std::function<void()>&, uint32_t)>; 46 47 /** 48 * @class RSUIContext 49 * 50 * @brief Represents the context for UI operations in the rendering service. 51 */ 52 class RSC_EXPORT RSUIContext : public std::enable_shared_from_this<RSUIContext> { 53 public: 54 /** 55 * @brief Default constructor for RSUIContext. 56 */ 57 virtual ~RSUIContext(); 58 59 /** 60 * @brief Gets node map. 61 * 62 * This function will be called when there are multiple instances of arkui. 63 * 64 * @return A reference to the mutable RSNodeMapV2 instance. 65 */ GetMutableNodeMap()66 inline RSNodeMapV2& GetMutableNodeMap() 67 { 68 return nodeMap_; 69 } 70 71 /** 72 * @brief Gets node map. 73 * 74 * This function will be called when there is a single instance of arkui. 75 * 76 * @return A reference to the RSNodeMapV2 instance. 77 */ GetNodeMap()78 inline const RSNodeMapV2& GetNodeMap() const 79 { 80 return nodeMap_; 81 } 82 83 /** 84 * @brief Gets the RSTransactionHandler associated with this context. 85 * 86 * @return A shared pointer to the RSTransactionHandler instance. 87 */ GetRSTransaction()88 inline std::shared_ptr<RSTransactionHandler> GetRSTransaction() const 89 { 90 return rsTransactionHandler_; 91 } 92 93 /** 94 * @brief Gets the RSSyncTransactionHandler associated with this context. 95 * 96 * @return A shared pointer to the RSSyncTransactionHandler instance. 97 */ GetSyncTransactionHandler()98 inline std::shared_ptr<RSSyncTransactionHandler> GetSyncTransactionHandler() const 99 { 100 return rsSyncTransactionHandler_; 101 } 102 103 /** 104 * @brief Gets the RSImplicitAnimator associated with this context. 105 * 106 * @return A shared pointer to the RSImplicitAnimator instance. 107 */ 108 const std::shared_ptr<RSImplicitAnimator> GetRSImplicitAnimator(); 109 110 /** 111 * @brief Gets the RSModifierManager associated with this context. 112 * 113 * @return A shared pointer to the RSModifierManager instance. 114 */ 115 const std::shared_ptr<RSModifierManager> GetRSModifierManager(); 116 117 /** 118 * @brief Gets the RSImplicitAnimParam associated with this context. 119 * 120 * @return A shared pointer to the RSImplicitAnimParam instance. 121 */ 122 bool AnimationCallback(AnimationId animationId, AnimationCallbackEvent event); 123 124 /** 125 * @brief Adds an animation to the context. 126 * 127 * @param animation The shared pointer to the RSAnimation to be added. 128 */ 129 void AddAnimationInner(const std::shared_ptr<RSAnimation>& animation); 130 131 /** 132 * @brief Removes animations from the context. 133 */ 134 void RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation); 135 136 /** 137 * @brief Gets the token associated with the current context. 138 * 139 * @return The token as a 64-bit unsigned integer. 140 */ GetToken()141 inline uint64_t GetToken() const 142 { 143 return token_; 144 } 145 146 /** 147 * @brief Sets the task runner for the UI thread. 148 * 149 * @param uiTaskRunner The task runner to be set. 150 */ 151 void SetUITaskRunner(const TaskRunner& uiTaskRunner); 152 153 /** 154 * @brief Posts a task to the UI thread. 155 * 156 * @param task The task to be posted. 157 */ 158 void PostTask(const std::function<void()>& task); 159 160 /** 161 * @brief Posts a delayed task to the UI thread. 162 * 163 * @param task The task to be posted. 164 * @param delay The delay before executing the task. 165 */ 166 void PostDelayTask(const std::function<void()>& task, uint32_t delay); 167 168 /** 169 * @brief Checks if the task runner is set. 170 * 171 * @return true if the task runner is set; false otherwise. 172 */ HasTaskRunner()173 inline bool HasTaskRunner() 174 { 175 return bool(taskRunner_); 176 } 177 178 private: 179 RSUIContext(); 180 RSUIContext(uint64_t token); 181 RSUIContext(const RSUIContext&) = delete; 182 RSUIContext(const RSUIContext&&) = delete; 183 RSUIContext& operator=(const RSUIContext&) = delete; 184 RSUIContext& operator=(const RSUIContext&&) = delete; 185 186 void DumpNodeTreeProcessor(NodeId nodeId, pid_t pid, uint32_t taskId, std::string& out); 187 188 uint64_t token_; 189 190 RSNodeMapV2 nodeMap_; 191 std::shared_ptr<RSTransactionHandler> rsTransactionHandler_; 192 std::shared_ptr<RSSyncTransactionHandler> rsSyncTransactionHandler_; 193 std::unordered_map<pid_t, std::shared_ptr<RSImplicitAnimator>> rsImplicitAnimators_; 194 std::shared_ptr<RSModifierManager> rsModifierManager_; 195 196 std::unordered_map<AnimationId, std::shared_ptr<RSAnimation>> animations_; 197 std::unordered_map<PropertyId, uint32_t> animatingPropertyNum_; 198 std::recursive_mutex animationMutex_; 199 200 TaskRunner taskRunner_ = TaskRunner(); 201 std::mutex implicitAnimatorMutex_; 202 203 friend class RSUIContextManager; 204 friend class RSUIDirector; 205 }; 206 207 } // namespace Rosen 208 } // namespace OHOS 209 210 /** @} */ 211 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_CONTEXT_H