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