1 /*
2 * Copyright (c) 2021 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 namespace OHOS {
20 namespace Rosen {
21 std::once_flag RSTransactionProxy::flag_;
22 RSTransactionProxy* RSTransactionProxy::instance_ = nullptr;
23
GetInstance()24 RSTransactionProxy* RSTransactionProxy::GetInstance()
25 {
26 std::call_once(flag_, &RSTransactionProxy::Init);
27 return instance_;
28 }
29
RSTransactionProxy()30 RSTransactionProxy::RSTransactionProxy()
31 {
32 }
33
~RSTransactionProxy()34 RSTransactionProxy::~RSTransactionProxy()
35 {
36 }
37
Init()38 void RSTransactionProxy::Init()
39 {
40 instance_ = new RSTransactionProxy();
41 ::atexit(&RSTransactionProxy::Destory);
42 }
43
Destory()44 void RSTransactionProxy::Destory()
45 {
46 instance_ = nullptr;
47 }
48
SetRenderThreadClient(std::unique_ptr<RSIRenderClient> & renderThreadClient)49 void RSTransactionProxy::SetRenderThreadClient(std::unique_ptr<RSIRenderClient>& renderThreadClient)
50 {
51 if (renderThreadClient != nullptr) {
52 renderThreadClient_ = std::move(renderThreadClient);
53 }
54 }
55
SetRenderServiceClient(const std::shared_ptr<RSIRenderClient> & renderServiceClient)56 void RSTransactionProxy::SetRenderServiceClient(const std::shared_ptr<RSIRenderClient>& renderServiceClient)
57 {
58 if (renderServiceClient != nullptr) {
59 renderServiceClient_ = renderServiceClient;
60 }
61 }
62
AddCommand(std::unique_ptr<RSCommand> & command,bool isRenderServiceCommand)63 void RSTransactionProxy::AddCommand(std::unique_ptr<RSCommand>& command, bool isRenderServiceCommand)
64 {
65 if ((renderServiceClient_ == nullptr && renderThreadClient_ == nullptr) || command == nullptr) {
66 return;
67 }
68
69 std::unique_lock<std::mutex> cmdLock(mutex_);
70
71 if (renderThreadClient_ == nullptr || isRenderServiceCommand) {
72 AddRemoteCommand(command);
73 return;
74 }
75
76 if (renderServiceClient_ == nullptr || !isRenderServiceCommand) {
77 AddCommonCommand(command);
78 return;
79 }
80 }
81
AddCommandFromRT(std::unique_ptr<RSCommand> & command)82 void RSTransactionProxy::AddCommandFromRT(std::unique_ptr<RSCommand>& command)
83 {
84 if (renderServiceClient_ == nullptr || command == nullptr) {
85 return;
86 }
87
88 {
89 std::unique_lock<std::mutex> cmdLock(mutexForRT_);
90 implicitTransactionDataFromRT_->AddCommand(command);
91 }
92 }
93
ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask> & task,bool isRenderServiceTask)94 void RSTransactionProxy::ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task, bool isRenderServiceTask)
95 {
96 if ((renderServiceClient_ == nullptr && renderThreadClient_ == nullptr) || task == nullptr) {
97 return;
98 }
99
100 if (renderThreadClient_ != nullptr && !isRenderServiceTask) {
101 renderThreadClient_->ExecuteSynchronousTask(task);
102 return;
103 }
104
105 if (renderServiceClient_ != nullptr && isRenderServiceTask) {
106 renderServiceClient_->ExecuteSynchronousTask(task);
107 return;
108 }
109 }
110
FlushImplicitTransaction()111 void RSTransactionProxy::FlushImplicitTransaction()
112 {
113 std::unique_lock<std::mutex> cmdLock(mutex_);
114 if (renderThreadClient_ != nullptr && !implicitCommonTransactionData_->IsEmpty()) {
115 renderThreadClient_->CommitTransaction(implicitCommonTransactionData_);
116 implicitCommonTransactionData_ = std::make_unique<RSTransactionData>();
117 }
118 if (renderServiceClient_ != nullptr && !implicitRemoteTransactionData_->IsEmpty()) {
119 renderServiceClient_->CommitTransaction(implicitRemoteTransactionData_);
120 implicitRemoteTransactionData_ = std::make_unique<RSTransactionData>();
121 }
122 }
123
FlushImplicitTransactionFromRT()124 void RSTransactionProxy::FlushImplicitTransactionFromRT()
125 {
126 std::unique_lock<std::mutex> cmdLock(mutexForRT_);
127 if (renderServiceClient_ != nullptr && !implicitTransactionDataFromRT_->IsEmpty()) {
128 renderServiceClient_->CommitTransaction(implicitTransactionDataFromRT_);
129 implicitTransactionDataFromRT_ = std::make_unique<RSTransactionData>();
130 }
131 }
132
AddCommonCommand(std::unique_ptr<RSCommand> & command)133 void RSTransactionProxy::AddCommonCommand(std::unique_ptr<RSCommand> &command)
134 {
135 implicitCommonTransactionData_->AddCommand(command);
136 }
137
AddRemoteCommand(std::unique_ptr<RSCommand> & command)138 void RSTransactionProxy::AddRemoteCommand(std::unique_ptr<RSCommand>& command)
139 {
140 implicitRemoteTransactionData_->AddCommand(command);
141 }
142
143 } // namespace Rosen
144 } // namespace OHOS
145