1 /*
2 * Copyright (c) 2021-2022 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 #include "transaction/rs_transaction_proxy.h"
17 #include <stdlib.h>
18
19 #include "platform/common/rs_log.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 std::once_flag RSTransactionProxy::flag_;
24 RSTransactionProxy* RSTransactionProxy::instance_ = nullptr;
25
GetInstance()26 RSTransactionProxy* RSTransactionProxy::GetInstance()
27 {
28 std::call_once(flag_, &RSTransactionProxy::Init);
29 return instance_;
30 }
31
RSTransactionProxy()32 RSTransactionProxy::RSTransactionProxy()
33 {
34 }
35
~RSTransactionProxy()36 RSTransactionProxy::~RSTransactionProxy()
37 {
38 }
39
Init()40 void RSTransactionProxy::Init()
41 {
42 instance_ = new RSTransactionProxy();
43 ::atexit(&RSTransactionProxy::Destroy);
44 }
45
Destroy()46 void RSTransactionProxy::Destroy()
47 {
48 instance_ = nullptr;
49 }
50
SetRenderThreadClient(std::unique_ptr<RSIRenderClient> & renderThreadClient)51 void RSTransactionProxy::SetRenderThreadClient(std::unique_ptr<RSIRenderClient>& renderThreadClient)
52 {
53 if (renderThreadClient != nullptr) {
54 renderThreadClient_ = std::move(renderThreadClient);
55 }
56 }
57
SetRenderServiceClient(const std::shared_ptr<RSIRenderClient> & renderServiceClient)58 void RSTransactionProxy::SetRenderServiceClient(const std::shared_ptr<RSIRenderClient>& renderServiceClient)
59 {
60 if (renderServiceClient != nullptr) {
61 renderServiceClient_ = renderServiceClient;
62 }
63 }
64
AddCommand(std::unique_ptr<RSCommand> & command,bool isRenderServiceCommand,FollowType followType,NodeId nodeId)65 void RSTransactionProxy::AddCommand(std::unique_ptr<RSCommand>& command, bool isRenderServiceCommand,
66 FollowType followType, NodeId nodeId)
67 {
68 if ((renderServiceClient_ == nullptr && renderThreadClient_ == nullptr) || command == nullptr) {
69 return;
70 }
71
72 std::unique_lock<std::mutex> cmdLock(mutex_);
73
74 if (renderServiceClient_ != nullptr && isRenderServiceCommand) {
75 AddRemoteCommand(command, nodeId, followType);
76 return;
77 }
78
79 if (renderThreadClient_ != nullptr && !isRenderServiceCommand) {
80 AddCommonCommand(command);
81 return;
82 }
83 ROSEN_LOGE("RSTransactionProxy::AddCommand failed, command type and client type not match !");
84 }
85
AddCommandFromRT(std::unique_ptr<RSCommand> & command,NodeId nodeId,FollowType followType)86 void RSTransactionProxy::AddCommandFromRT(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType)
87 {
88 if (renderServiceClient_ == nullptr || command == nullptr) {
89 return;
90 }
91
92 {
93 std::unique_lock<std::mutex> cmdLock(mutexForRT_);
94 implicitTransactionDataFromRT_->AddCommand(command, nodeId, followType);
95 }
96 }
97
ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask> & task,bool isRenderServiceTask)98 void RSTransactionProxy::ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task, bool isRenderServiceTask)
99 {
100 if ((renderServiceClient_ == nullptr && renderThreadClient_ == nullptr) || task == nullptr) {
101 return;
102 }
103
104 if (renderThreadClient_ != nullptr && !isRenderServiceTask) {
105 renderThreadClient_->ExecuteSynchronousTask(task);
106 return;
107 }
108
109 if (renderServiceClient_ != nullptr && isRenderServiceTask) {
110 renderServiceClient_->ExecuteSynchronousTask(task);
111 return;
112 }
113 }
114
FlushImplicitTransaction(uint64_t timestamp,const std::string & abilityName)115 void RSTransactionProxy::FlushImplicitTransaction(uint64_t timestamp, const std::string& abilityName)
116 {
117 std::unique_lock<std::mutex> cmdLock(mutex_);
118 timestamp_ = std::max(timestamp, timestamp_);
119 if (renderThreadClient_ != nullptr && !implicitCommonTransactionData_->IsEmpty()) {
120 implicitCommonTransactionData_->timestamp_ = timestamp_;
121 implicitCommonTransactionData_->abilityName_ = abilityName;
122 renderThreadClient_->CommitTransaction(implicitCommonTransactionData_);
123 implicitCommonTransactionData_ = std::make_unique<RSTransactionData>();
124 }
125 if (renderServiceClient_ != nullptr && !implicitRemoteTransactionData_->IsEmpty()) {
126 implicitRemoteTransactionData_->timestamp_ = timestamp_;
127 renderServiceClient_->CommitTransaction(implicitRemoteTransactionData_);
128 implicitRemoteTransactionData_ = std::make_unique<RSTransactionData>();
129 }
130 }
131
FlushImplicitTransactionFromRT(uint64_t timestamp)132 void RSTransactionProxy::FlushImplicitTransactionFromRT(uint64_t timestamp)
133 {
134 std::unique_lock<std::mutex> cmdLock(mutexForRT_);
135 if (renderServiceClient_ != nullptr && !implicitTransactionDataFromRT_->IsEmpty()) {
136 implicitTransactionDataFromRT_->timestamp_ = timestamp;
137 renderServiceClient_->CommitTransaction(implicitTransactionDataFromRT_);
138 implicitTransactionDataFromRT_ = std::make_unique<RSTransactionData>();
139 }
140 }
141
Begin()142 void RSTransactionProxy::Begin()
143 {
144 std::unique_lock<std::mutex> cmdLock(mutex_);
145 implicitCommonTransactionDataStack_.emplace(std::make_unique<RSTransactionData>());
146 implicitRemoteTransactionDataStack_.emplace(std::make_unique<RSTransactionData>());
147 }
148
Commit(uint64_t timestamp)149 void RSTransactionProxy::Commit(uint64_t timestamp)
150 {
151 std::unique_lock<std::mutex> cmdLock(mutex_);
152 if (!implicitCommonTransactionDataStack_.empty()) {
153 implicitCommonTransactionDataStack_.pop();
154 }
155
156 if (!implicitRemoteTransactionDataStack_.empty()) {
157 if (renderServiceClient_ != nullptr && !implicitRemoteTransactionDataStack_.top()->IsEmpty()) {
158 implicitRemoteTransactionDataStack_.top()->timestamp_ = timestamp;
159 renderServiceClient_->CommitTransaction(implicitRemoteTransactionDataStack_.top());
160 }
161 implicitRemoteTransactionDataStack_.pop();
162 }
163 }
164
AddCommonCommand(std::unique_ptr<RSCommand> & command)165 void RSTransactionProxy::AddCommonCommand(std::unique_ptr<RSCommand> &command)
166 {
167 if (!implicitCommonTransactionDataStack_.empty()) {
168 implicitCommonTransactionDataStack_.top()->AddCommand(command, 0, FollowType::NONE);
169 return;
170 }
171 implicitCommonTransactionData_->AddCommand(command, 0, FollowType::NONE);
172 }
173
AddRemoteCommand(std::unique_ptr<RSCommand> & command,NodeId nodeId,FollowType followType)174 void RSTransactionProxy::AddRemoteCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType)
175 {
176 if (!implicitRemoteTransactionDataStack_.empty()) {
177 implicitRemoteTransactionDataStack_.top()->AddCommand(command, nodeId, followType);
178 return;
179 }
180 implicitRemoteTransactionData_->AddCommand(command, nodeId, followType);
181 }
182
183 } // namespace Rosen
184 } // namespace OHOS
185