• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_ROSEN_RS_ADAPTER_H
17 #define OHOS_ROSEN_RS_ADAPTER_H
18 
19 #include <initializer_list>
20 #include <unordered_set>
21 
22 #include <transaction/rs_sync_transaction_controller.h>
23 #include <transaction/rs_sync_transaction_handler.h>
24 #include <transaction/rs_transaction.h>
25 #include <ui/rs_base_node.h>
26 #include <ui/rs_node.h>
27 #include <ui/rs_ui_context.h>
28 #include <ui/rs_ui_director.h>
29 
30 #define RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(...)      \
31     do {                                                      \
32         if (!RSAdapterUtil::IsClientMultiInstanceEnabled()) { \
33             return __VA_ARGS__;                               \
34         }                                                     \
35     } while (false)                                           \
36 
37 namespace OHOS {
38 namespace Rosen {
39 enum class InvokerType {
40     RS_TRANSACTION_PROXY = 0,
41     RS_TRANSACTION_HANDLER,
42     RS_SYNC_TRANSACTION_CONTROLLER,
43     RS_SYNC_TRANSACTION_HANDLER,
44     NONE,
45 };
46 
47 class RSTransactionAdapter {
48 public:
49     explicit RSTransactionAdapter(const std::shared_ptr<RSUIContext>& rsUIContext);
50     explicit RSTransactionAdapter(const std::shared_ptr<RSUIDirector>& rsUIDirector);
51     explicit RSTransactionAdapter(const std::shared_ptr<RSNode>& rsNode);
52     virtual ~RSTransactionAdapter() = default;
53 
54     std::shared_ptr<RSUIContext> GetRSUIContext() const;
55     virtual void Begin();
56     virtual void Commit(uint64_t timestamp = 0);
57     void FlushImplicitTransaction(uint64_t timestamp = 0, const std::string& abilityName = "");
58 
59     static void FlushImplicitTransaction(const std::shared_ptr<RSUIContext>& rsUIContext,
60                                          uint64_t timestamp = 0, const std::string& abilityName = "");
61     static void FlushImplicitTransaction(const std::unordered_set<std::shared_ptr<RSUIContext>>& rsUIContexts,
62                                          uint64_t timestamp = 0, const std::string& abilityName = "");
63     static void FlushImplicitTransaction(std::initializer_list<std::shared_ptr<RSUIContext>> rsUIContexts,
64                                          uint64_t timestamp = 0, const std::string& abilityName = "");
65     static void FlushImplicitTransaction(const std::shared_ptr<RSUIDirector>& rsUIDirector,
66                                          uint64_t timestamp = 0, const std::string& abilityName = "");
67     static void FlushImplicitTransaction(std::initializer_list<std::shared_ptr<RSUIDirector>> rsUIDirectors,
68                                          uint64_t timestamp = 0, const std::string& abilityName = "");
69     static void FlushImplicitTransaction(const std::shared_ptr<RSNode>& rsNode,
70                                          uint64_t timestamp = 0, const std::string& abilityName = "");
71     static void FlushImplicitTransaction(std::initializer_list<std::shared_ptr<RSNode>> rsNodes,
72                                          uint64_t timestamp = 0, const std::string& abilityName = "");
73 
74 private:
75     template<typename Func>
76     void InvokeTransaction(Func&& func, const char* caller);
77 
78     static InvokerType invokerType_;
79     std::shared_ptr<RSUIContext> rsUIContext_;
80     RSTransactionProxy* rsTransProxy_;
81     std::shared_ptr<RSTransactionHandler> rsTransHandler_;
82 };
83 
84 class AutoRSTransaction {
85 public:
86     explicit AutoRSTransaction(const std::shared_ptr<RSUIContext>& rsUIContext, bool enable = true);
87     explicit AutoRSTransaction(const std::shared_ptr<RSNode>& rsNode, bool enable = true);
88     explicit AutoRSTransaction(const std::shared_ptr<RSTransactionAdapter>& rsTransAdapter, bool enable = true);
89     ~AutoRSTransaction();
90     AutoRSTransaction(const AutoRSTransaction&) = delete;
91     AutoRSTransaction& operator=(const AutoRSTransaction&) = delete;
92     AutoRSTransaction(AutoRSTransaction&&) = delete;
93     AutoRSTransaction& operator=(AutoRSTransaction&&) = delete;
94 
95 private:
96     std::shared_ptr<RSTransactionAdapter> rsTransAdapter_;
97 };
98 
99 class RSSyncTransactionAdapter {
100 public:
101     explicit RSSyncTransactionAdapter(const std::shared_ptr<RSUIContext>& rsUIContext);
102     explicit RSSyncTransactionAdapter(const std::shared_ptr<RSNode>& rsNode);
103     virtual ~RSSyncTransactionAdapter() = default;
104 
105     std::shared_ptr<RSUIContext> GetRSUIContext() const;
106     std::shared_ptr<RSTransaction> GetRSTransaction();
107     virtual void OpenSyncTransaction(const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
108     virtual void CloseSyncTransaction(const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
109 
110     static std::shared_ptr<RSTransaction> GetRSTransaction(const std::shared_ptr<RSUIContext>& rsUIContext);
111     static std::shared_ptr<RSTransaction> GetRSTransaction(const std::shared_ptr<RSNode>& rsNode);
112     static void OpenSyncTransaction(const std::shared_ptr<RSUIContext>& rsUIContext,
113                                     const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
114     static void OpenSyncTransaction(const std::shared_ptr<RSNode>& rsNode,
115                                     const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
116     static void CloseSyncTransaction(const std::shared_ptr<RSUIContext>& rsUIContext,
117                                      const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
118     static void CloseSyncTransaction(const std::shared_ptr<RSNode>& rsNode,
119                                      const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
120 
121 private:
122     template<typename ReturnType, typename Func>
123     ReturnType InvokeSyncTransaction(Func&& func, const char* caller);
124 
125     template<typename ReturnType, typename Func>
126     static ReturnType InvokeSyncTransaction(
127         const std::shared_ptr<RSUIContext>& rsUIContext, Func&& func, const char* caller);
128 
129     static InvokerType invokerType_;
130     std::shared_ptr<RSUIContext> rsUIContext_;
131     RSSyncTransactionController* rsSyncTransController_;
132     std::shared_ptr<RSSyncTransactionHandler> rsSyncTransHandler_;
133 };
134 
135 class AutoRSSyncTransaction {
136 public:
137     explicit AutoRSSyncTransaction(const std::shared_ptr<RSUIContext>& rsUIContext,
138                                    bool needFlushImplicitTransaction = true,
139                                    const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
140     explicit AutoRSSyncTransaction(const std::shared_ptr<RSNode>& rsNode,
141                                    bool needFlushImplicitTransaction = true,
142                                    const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
143     explicit AutoRSSyncTransaction(const std::shared_ptr<RSSyncTransactionAdapter>& rsSyncTransAdapter,
144                                    bool needFlushImplicitTransaction = true,
145                                    const std::shared_ptr<AppExecFwk::EventHandler>& handler = nullptr);
146     ~AutoRSSyncTransaction();
147     AutoRSSyncTransaction(const AutoRSSyncTransaction&) = delete;
148     AutoRSSyncTransaction& operator=(const AutoRSSyncTransaction&) = delete;
149     AutoRSSyncTransaction(AutoRSSyncTransaction&&) = delete;
150     AutoRSSyncTransaction& operator=(AutoRSSyncTransaction&&) = delete;
151 
152 private:
153     std::shared_ptr<RSSyncTransactionAdapter> rsSyncTransAdapter_;
154     std::shared_ptr<AppExecFwk::EventHandler> handler_;
155 };
156 
157 class AllowInMultiThreadGuard {
158 public:
159     explicit AllowInMultiThreadGuard(const std::shared_ptr<RSNode>& rsNode);
160     ~AllowInMultiThreadGuard();
161     AllowInMultiThreadGuard(const AllowInMultiThreadGuard&) = delete;
162     AllowInMultiThreadGuard& operator=(const AllowInMultiThreadGuard&) = delete;
163     AllowInMultiThreadGuard(AllowInMultiThreadGuard&&) = delete;
164     AllowInMultiThreadGuard& operator=(AllowInMultiThreadGuard&&) = delete;
165 
166 private:
167     std::shared_ptr<RSNode> rsNode_;
168 };
169 
170 class RSAdapterUtil {
171 public:
172     static bool IsClientMultiInstanceEnabled();
173     static void InitRSUIDirector(std::shared_ptr<RSUIDirector>& rsUIDirector,
174                                  bool shouldCreateRenderThread = true,
175                                  bool isMultiInstance = false);
176     static void SetRSUIContext(const std::shared_ptr<RSNode>& rsNode,
177                                const std::shared_ptr<RSUIContext>& rsUIContext,
178                                bool skipCheckInMultiInstance = false);
179     static void SetRSTransactionHandler(const std::shared_ptr<RSTransaction>& rsTransaction,
180                                         const std::shared_ptr<RSUIContext>& rsUIContext);
181     static void SetSkipCheckInMultiInstance(const std::shared_ptr<RSNode>& rsNode,
182                                             bool skipCheckInMultiInstance);
183     static const std::shared_ptr<RSBaseNode> GetRSNode(
184         const std::shared_ptr<RSUIContext>& rsUIContext, NodeId id);
185     static std::string RSUIContextToStr(const std::shared_ptr<RSUIContext>& rsUIContext);
186     static std::string RSNodeToStr(const std::shared_ptr<RSNode>& rsNode);
187     static std::string RSUIDirectorToStr(const std::shared_ptr<RSUIDirector>& rsUIDirector);
188 };
189 
RunIfRSClientMultiInstanceEnabled(const std::function<void ()> & action)190 inline void RunIfRSClientMultiInstanceEnabled(const std::function<void()>& action)
191 {
192     RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED();
193     action();
194 }
195 } // namespace Rosen
196 } // namespace OHOS
197 
198 #endif // OHOS_ROSEN_RS_ADAPTER_H