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_manager.h 25 * 26 * @brief Defines the properties and methods for RSUIContextManager class. 27 */ 28 29 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_CONTEXT_MANAGER_H 30 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_CONTEXT_MANAGER_H 31 32 #include "rs_ui_context.h" 33 34 #include "common/rs_macros.h" 35 36 namespace OHOS { 37 namespace Rosen { 38 /** 39 * @class RSUIContextManager 40 * 41 * @brief Manages the lifecycle and access to RSUIContext instances. 42 */ 43 class RSC_EXPORT RSUIContextManager final { 44 public: 45 46 /** 47 * @brief Provides a singleton instance of the RSUIContextManager. 48 * 49 * @return A constant reference to the singleton instance of RSUIContextManager. 50 */ 51 static const RSUIContextManager& Instance(); 52 53 /** 54 * @brief Provides a mutable singleton instance of the RSUIContextManager. 55 * 56 * This function will be called when there are multiple instances of arkui. 57 * 58 * @return A reference to the singleton instance of RSUIContextManager. 59 */ 60 static RSUIContextManager& MutableInstance(); 61 62 /** 63 * @brief Retrieves the RSUIContext associated with the given token. 64 * 65 * @param token The unique identifier for the RSUIContext. 66 * @return A shared pointer to the RSUIContext if found; otherwise, nullptr. 67 */ 68 const std::shared_ptr<RSUIContext> GetRSUIContext(uint64_t token) const; 69 70 /** 71 * @brief Creates a new RSUIContext. 72 * 73 * @return A shared pointer to the newly created RSUIContext. 74 */ 75 std::shared_ptr<RSUIContext> CreateRSUIContext(); 76 77 /** 78 * @brief Destroys the RSUIContext associated with the given token. 79 * 80 * @param token The unique identifier for the RSUIContext to be destroyed. 81 */ 82 void DestroyContext(uint64_t token); 83 84 private: 85 RSUIContextManager(); 86 ~RSUIContextManager() noexcept; 87 RSUIContextManager(const RSUIContextManager&) = delete; 88 RSUIContextManager(const RSUIContextManager&&) = delete; 89 RSUIContextManager& operator=(const RSUIContextManager&) = delete; 90 RSUIContextManager& operator=(const RSUIContextManager&&) = delete; 91 92 /** 93 * @brief Generates a unique token based on the provided thread ID (tid). 94 * 95 * @param tid The thread ID for which the token is to be generated. 96 * @return A 64-bit unsigned integer representing the generated token. 97 */ 98 uint64_t GenerateToken(int32_t tid); 99 100 /** 101 * @brief Get a random UI task runner context. 102 * 103 * @return A shared pointer to an RSUIContext. 104 */ 105 std::shared_ptr<RSUIContext> GetRandomUITaskRunnerCtx() const; 106 107 mutable std::mutex mutex_; 108 std::unordered_map<uint64_t, std::shared_ptr<RSUIContext>> rsUIContextMap_; 109 uint32_t instanceIdCounter_ = 0; 110 bool isMultiInstanceOpen_ = false; 111 112 friend class RSUIDirector; 113 friend class RSUIContext; 114 }; 115 } // namespace Rosen 116 } // namespace OHOS 117 118 /** @} */ 119 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_CONTEXT_MANAGER_H