1 /*
2  * Copyright (c) 2021-2023 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 ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H
17 #define ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <queue>
22 #include <stack>
23 
24 #include "command/rs_command.h"
25 #include "command/rs_node_showing_command.h"
26 #include "common/rs_macros.h"
27 #include "common/rs_singleton.h"
28 #include "common/rs_macros.h"
29 #include "transaction/rs_irender_client.h"
30 #include "transaction/rs_transaction_data.h"
31 
32 namespace OHOS {
33 namespace Rosen {
34 class RSSyncTask;
35 using FlushEmptyCallback = std::function<bool(const uint64_t)>;
36 class RSB_EXPORT RSTransactionProxy final {
37 public:
38     static RSB_EXPORT RSTransactionProxy* GetInstance();
39     void SetRenderThreadClient(std::unique_ptr<RSIRenderClient>& renderThreadClient);
40     void SetRenderServiceClient(const std::shared_ptr<RSIRenderClient>& renderServiceClient);
41 
42     void AddCommand(std::unique_ptr<RSCommand>& command, bool isRenderServiceCommand = false,
43                     FollowType followType = FollowType::NONE, NodeId nodeId = 0);
44     void AddCommandFromRT(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType = FollowType::FOLLOW_TO_PARENT);
45 
46     void FlushImplicitTransaction(uint64_t timestamp = 0, const std::string& abilityName = "");
47     void FlushImplicitTransactionFromRT(uint64_t timestamp);
48 
49     void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task, bool isRenderServiceTask = false);
50 
51     void Begin();
52     void Commit(uint64_t timestamp = 0);
53     void CommitSyncTransaction(uint64_t timestamp = 0, const std::string& abilityName = "");
54     void MarkTransactionNeedSync();
55     void MarkTransactionNeedCloseSync(const int32_t transactionCount);
56     void SetSyncTransactionNum(const int32_t transactionCount);
57 
58     void StartSyncTransaction();
59     void CloseSyncTransaction();
SetFlushEmptyCallback(FlushEmptyCallback flushEmptyCallback)60     void SetFlushEmptyCallback(FlushEmptyCallback flushEmptyCallback)
61     {
62         flushEmptyCallback_ = flushEmptyCallback;
63     }
64 
SetSyncId(const uint64_t syncId)65     void SetSyncId(const uint64_t syncId)
66     {
67         syncId_ = syncId;
68     }
69 
70     void SetParentPid(const int32_t parentPid);
71 
72     uint32_t GetTransactionDataIndex() const;
73 
74     bool IsEmpty() const;
75 
76     void StartCloseSyncTransactionFallbackTask(std::shared_ptr<AppExecFwk::EventHandler> handler, bool isOpen);
77 
78 private:
79     RSTransactionProxy();
80     virtual ~RSTransactionProxy();
81     static void Init();
82     static void Destroy();
83 
84     RSTransactionProxy(const RSTransactionProxy&) = delete;
85     RSTransactionProxy(const RSTransactionProxy&&) = delete;
86     RSTransactionProxy& operator=(const RSTransactionProxy&) = delete;
87     RSTransactionProxy& operator=(const RSTransactionProxy&&) = delete;
88 
89     void AddCommonCommand(std::unique_ptr<RSCommand>& command);
90     void AddRemoteCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType);
91 
92     // Command Transaction Triggered by UI Thread.
93     mutable std::mutex mutex_;
94     std::unique_ptr<RSTransactionData> implicitCommonTransactionData_{std::make_unique<RSTransactionData>()};
95     std::unique_ptr<RSTransactionData> implicitRemoteTransactionData_{std::make_unique<RSTransactionData>()};
96 
97     std::stack<std::unique_ptr<RSTransactionData>> implicitCommonTransactionDataStack_;
98     std::stack<std::unique_ptr<RSTransactionData>> implicitRemoteTransactionDataStack_;
99 
100     // Command Transaction Triggered by Render Thread which is definitely send to Render Service.
101     std::mutex mutexForRT_;
102     std::unique_ptr<RSTransactionData> implicitTransactionDataFromRT_{std::make_unique<RSTransactionData>()};
103 
104     std::shared_ptr<RSIRenderClient> renderServiceClient_ = RSIRenderClient::CreateRenderServiceClient();
105     std::unique_ptr<RSIRenderClient> renderThreadClient_ = nullptr;
106     uint64_t timestamp_ = 0;
107     static std::once_flag flag_;
108     static RSTransactionProxy* instance_;
109     bool needSync_ { false };
110     uint64_t syncId_ { 0 };
111     FlushEmptyCallback flushEmptyCallback_ = nullptr;
112     uint32_t transactionDataIndex_ = 0;
113     std::queue<std::string> taskNames_ {};
114 };
115 } // namespace Rosen
116 } // namespace OHOS
117 
118 #endif // ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H
119