/* * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ROSEN_RS_ADAPTER_H #define OHOS_ROSEN_RS_ADAPTER_H #include #include #include #include #include #include #include #include #include #define RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(...) \ do { \ if (!RSAdapterUtil::IsClientMultiInstanceEnabled()) { \ return __VA_ARGS__; \ } \ } while (false) \ namespace OHOS { namespace Rosen { enum class InvokerType { RS_TRANSACTION_PROXY = 0, RS_TRANSACTION_HANDLER, RS_SYNC_TRANSACTION_CONTROLLER, RS_SYNC_TRANSACTION_HANDLER, NONE, }; class RSTransactionAdapter { public: explicit RSTransactionAdapter(const std::shared_ptr& rsUIContext); explicit RSTransactionAdapter(const std::shared_ptr& rsUIDirector); explicit RSTransactionAdapter(const std::shared_ptr& rsNode); virtual ~RSTransactionAdapter() = default; std::shared_ptr GetRSUIContext() const; virtual void Begin(); virtual void Commit(uint64_t timestamp = 0); void FlushImplicitTransaction(uint64_t timestamp = 0, const std::string& abilityName = ""); static void FlushImplicitTransaction(const std::shared_ptr& rsUIContext, uint64_t timestamp = 0, const std::string& abilityName = ""); static void FlushImplicitTransaction(const std::unordered_set>& rsUIContexts, uint64_t timestamp = 0, const std::string& abilityName = ""); static void FlushImplicitTransaction(std::initializer_list> rsUIContexts, uint64_t timestamp = 0, const std::string& abilityName = ""); static void FlushImplicitTransaction(const std::shared_ptr& rsUIDirector, uint64_t timestamp = 0, const std::string& abilityName = ""); static void FlushImplicitTransaction(std::initializer_list> rsUIDirectors, uint64_t timestamp = 0, const std::string& abilityName = ""); static void FlushImplicitTransaction(const std::shared_ptr& rsNode, uint64_t timestamp = 0, const std::string& abilityName = ""); static void FlushImplicitTransaction(std::initializer_list> rsNodes, uint64_t timestamp = 0, const std::string& abilityName = ""); private: template void InvokeTransaction(Func&& func, const char* caller); static InvokerType invokerType_; std::shared_ptr rsUIContext_; RSTransactionProxy* rsTransProxy_; std::shared_ptr rsTransHandler_; }; class AutoRSTransaction { public: explicit AutoRSTransaction(const std::shared_ptr& rsUIContext, bool enable = true); explicit AutoRSTransaction(const std::shared_ptr& rsNode, bool enable = true); explicit AutoRSTransaction(const std::shared_ptr& rsTransAdapter, bool enable = true); ~AutoRSTransaction(); AutoRSTransaction(const AutoRSTransaction&) = delete; AutoRSTransaction& operator=(const AutoRSTransaction&) = delete; AutoRSTransaction(AutoRSTransaction&&) = delete; AutoRSTransaction& operator=(AutoRSTransaction&&) = delete; private: std::shared_ptr rsTransAdapter_; }; class RSSyncTransactionAdapter { public: explicit RSSyncTransactionAdapter(const std::shared_ptr& rsUIContext); explicit RSSyncTransactionAdapter(const std::shared_ptr& rsNode); virtual ~RSSyncTransactionAdapter() = default; std::shared_ptr GetRSUIContext() const; std::shared_ptr GetRSTransaction(); virtual void OpenSyncTransaction(const std::shared_ptr& handler = nullptr); virtual void CloseSyncTransaction(const std::shared_ptr& handler = nullptr); static std::shared_ptr GetRSTransaction(const std::shared_ptr& rsUIContext); static std::shared_ptr GetRSTransaction(const std::shared_ptr& rsNode); static void OpenSyncTransaction(const std::shared_ptr& rsUIContext, const std::shared_ptr& handler = nullptr); static void OpenSyncTransaction(const std::shared_ptr& rsNode, const std::shared_ptr& handler = nullptr); static void CloseSyncTransaction(const std::shared_ptr& rsUIContext, const std::shared_ptr& handler = nullptr); static void CloseSyncTransaction(const std::shared_ptr& rsNode, const std::shared_ptr& handler = nullptr); private: template ReturnType InvokeSyncTransaction(Func&& func, const char* caller); template static ReturnType InvokeSyncTransaction( const std::shared_ptr& rsUIContext, Func&& func, const char* caller); static InvokerType invokerType_; std::shared_ptr rsUIContext_; RSSyncTransactionController* rsSyncTransController_; std::shared_ptr rsSyncTransHandler_; }; class AutoRSSyncTransaction { public: explicit AutoRSSyncTransaction(const std::shared_ptr& rsUIContext, bool needFlushImplicitTransaction = true, const std::shared_ptr& handler = nullptr); explicit AutoRSSyncTransaction(const std::shared_ptr& rsNode, bool needFlushImplicitTransaction = true, const std::shared_ptr& handler = nullptr); explicit AutoRSSyncTransaction(const std::shared_ptr& rsSyncTransAdapter, bool needFlushImplicitTransaction = true, const std::shared_ptr& handler = nullptr); ~AutoRSSyncTransaction(); AutoRSSyncTransaction(const AutoRSSyncTransaction&) = delete; AutoRSSyncTransaction& operator=(const AutoRSSyncTransaction&) = delete; AutoRSSyncTransaction(AutoRSSyncTransaction&&) = delete; AutoRSSyncTransaction& operator=(AutoRSSyncTransaction&&) = delete; private: std::shared_ptr rsSyncTransAdapter_; std::shared_ptr handler_; }; class AllowInMultiThreadGuard { public: explicit AllowInMultiThreadGuard(const std::shared_ptr& rsNode); ~AllowInMultiThreadGuard(); AllowInMultiThreadGuard(const AllowInMultiThreadGuard&) = delete; AllowInMultiThreadGuard& operator=(const AllowInMultiThreadGuard&) = delete; AllowInMultiThreadGuard(AllowInMultiThreadGuard&&) = delete; AllowInMultiThreadGuard& operator=(AllowInMultiThreadGuard&&) = delete; private: std::shared_ptr rsNode_; }; class RSAdapterUtil { public: static bool IsClientMultiInstanceEnabled(); static void InitRSUIDirector(std::shared_ptr& rsUIDirector, bool shouldCreateRenderThread = true, bool isMultiInstance = false); static void SetRSUIContext(const std::shared_ptr& rsNode, const std::shared_ptr& rsUIContext, bool skipCheckInMultiInstance = false); static void SetRSTransactionHandler(const std::shared_ptr& rsTransaction, const std::shared_ptr& rsUIContext); static void SetSkipCheckInMultiInstance(const std::shared_ptr& rsNode, bool skipCheckInMultiInstance); static const std::shared_ptr GetRSNode( const std::shared_ptr& rsUIContext, NodeId id); static std::string RSUIContextToStr(const std::shared_ptr& rsUIContext); static std::string RSNodeToStr(const std::shared_ptr& rsNode); static std::string RSUIDirectorToStr(const std::shared_ptr& rsUIDirector); }; inline void RunIfRSClientMultiInstanceEnabled(const std::function& action) { RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(); action(); } } // namespace Rosen } // namespace OHOS #endif // OHOS_ROSEN_RS_ADAPTER_H