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 #include "transaction/rs_transaction_data.h"
17
18 #include "command/rs_canvas_node_command.h"
19 #include "command/rs_command.h"
20 #include "command/rs_command_factory.h"
21 #include "platform/common/rs_log.h"
22 #include "platform/common/rs_system_properties.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 static constexpr size_t PARCEL_MAX_CPACITY = 2000 * 1024; // upper bound of parcel capacity
28 static constexpr size_t PARCEL_SPLIT_THRESHOLD = 1800 * 1024; // should be < PARCEL_MAX_CPACITY
29 }
30
__anon646b9ff60202(uint64_t nodeId, int count, int num) 31 std::function<void(uint64_t, int, int)> RSTransactionData::alarmLogFunc = [](uint64_t nodeId, int count, int num) {
32 ROSEN_LOGW("rsNode:%{public}" PRId64 " send %{public}d commands, "
33 "total num of rsNode is %{public}d", nodeId, count, num);
34 };
35
Unmarshalling(Parcel & parcel)36 RSTransactionData* RSTransactionData::Unmarshalling(Parcel& parcel)
37 {
38 auto transactionData = new RSTransactionData();
39 if (transactionData->UnmarshallingCommand(parcel)) {
40 return transactionData;
41 }
42 ROSEN_LOGE("RSTransactionData Unmarshalling Failed");
43 delete transactionData;
44 return nullptr;
45 }
46
AddAlarmLog(std::function<void (uint64_t,int,int)> func)47 void RSTransactionData::AddAlarmLog(std::function<void(uint64_t, int, int)> func)
48 {
49 alarmLogFunc = func;
50 }
51
~RSTransactionData()52 RSTransactionData::~RSTransactionData()
53 {
54 Clear();
55 }
56
AlarmRsNodeLog() const57 void RSTransactionData::AlarmRsNodeLog() const
58 {
59 std::unordered_map<NodeId, int> commandNodeIdCount;
60 for (size_t countIndex = 0; countIndex < payload_.size();countIndex++) {
61 auto nodeId = std::get<0>(payload_[countIndex]);
62
63 if (commandNodeIdCount.count(nodeId)) {
64 commandNodeIdCount[nodeId] += 1;
65 } else {
66 commandNodeIdCount[nodeId] = 1;
67 }
68 }
69
70 int maxCount = 0;
71 NodeId maxNodeId = -1;
72 int rsNodeNum = 0;
73
74 for (auto it = commandNodeIdCount.begin(); it != commandNodeIdCount.end(); ++it) {
75 if (it->second > maxCount) {
76 maxNodeId = it->first;
77 maxCount = it->second;
78 }
79 rsNodeNum++;
80 }
81
82 RSTransactionData::alarmLogFunc(maxNodeId, maxCount, rsNodeNum);
83 }
84
Marshalling(Parcel & parcel) const85 bool RSTransactionData::Marshalling(Parcel& parcel) const
86 {
87 bool success = true;
88 parcel.SetMaxCapacity(PARCEL_MAX_CPACITY);
89 // to correct actual marshaled command size later, record its position in parcel
90 size_t recordPosition = parcel.GetWritePosition();
91 std::unique_lock<std::mutex> lock(commandMutex_);
92 success = success && parcel.WriteInt32(static_cast<int32_t>(payload_.size()));
93 size_t marshaledSize = 0;
94 static bool isUniRender = RSSystemProperties::GetUniRenderEnabled();
95 success = success && parcel.WriteBool(isUniRender);
96 while (marshallingIndex_ < payload_.size()) {
97 auto& [nodeId, followType, command] = payload_[marshallingIndex_];
98
99 if (!isUniRender) {
100 success = success && parcel.WriteUint64(nodeId);
101 success = success && parcel.WriteUint8(static_cast<uint8_t>(followType));
102 }
103 if (!command) {
104 parcel.WriteUint8(0);
105 RS_LOGW("failed RSTransactionData::Marshalling, command is nullptr");
106 } else {
107 parcel.WriteUint8(1);
108 success = success && command->Marshalling(parcel);
109 }
110 if (!success) {
111 ROSEN_LOGE("failed RSTransactionData::Marshalling type:%{public}s", command->PrintType().c_str());
112 return false;
113 }
114 ++marshallingIndex_;
115 ++marshaledSize;
116 if (parcel.GetDataSize() > PARCEL_SPLIT_THRESHOLD) {
117 break;
118 }
119 }
120 if (marshaledSize < payload_.size()) {
121 // correct command size recorded in Parcel
122 *reinterpret_cast<int32_t*>(parcel.GetData() + recordPosition) = static_cast<int32_t>(marshaledSize);
123 ROSEN_LOGW("RSTransactionData::Marshalling data split to several parcels"
124 ", marshaledSize:%{public}zu, marshallingIndex_:%{public}zu, total count:%{public}zu"
125 ", parcel size:%{public}zu, threshold:%{public}zu.",
126 marshaledSize, marshallingIndex_, payload_.size(), parcel.GetDataSize(), PARCEL_SPLIT_THRESHOLD);
127
128 AlarmRsNodeLog();
129 }
130 success = success && parcel.WriteBool(needSync_);
131 success = success && parcel.WriteBool(needCloseSync_);
132 success = success && parcel.WriteInt32(syncTransactionCount_);
133 success = success && parcel.WriteUint64(timestamp_);
134 success = success && parcel.WriteInt32(pid_);
135 success = success && parcel.WriteUint64(index_);
136 success = success && parcel.WriteUint64(syncId_);
137 return success;
138 }
139
ProcessBySingleFrameComposer(RSContext & context)140 void RSTransactionData::ProcessBySingleFrameComposer(RSContext& context)
141 {
142 std::unique_lock<std::mutex> lock(commandMutex_);
143 for (auto& [nodeId, followType, command] : payload_) {
144 if (command != nullptr &&
145 command->GetType() == RSCommandType::CANVAS_NODE &&
146 command->GetSubType() == RSCanvasNodeCommandType::CANVAS_NODE_UPDATE_RECORDING) {
147 command->Process(context);
148 }
149 }
150 }
151
Process(RSContext & context)152 void RSTransactionData::Process(RSContext& context)
153 {
154 std::unique_lock<std::mutex> lock(commandMutex_);
155 for (auto& [nodeId, followType, command] : payload_) {
156 if (command != nullptr) {
157 command->Process(context);
158 }
159 }
160 }
161
Clear()162 void RSTransactionData::Clear()
163 {
164 std::unique_lock<std::mutex> lock(commandMutex_);
165 payload_.clear();
166 timestamp_ = 0;
167 }
168
AddCommand(std::unique_ptr<RSCommand> & command,NodeId nodeId,FollowType followType)169 void RSTransactionData::AddCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType)
170 {
171 std::unique_lock<std::mutex> lock(commandMutex_);
172 if (command) {
173 payload_.emplace_back(nodeId, followType, std::move(command));
174 }
175 }
176
AddCommand(std::unique_ptr<RSCommand> && command,NodeId nodeId,FollowType followType)177 void RSTransactionData::AddCommand(std::unique_ptr<RSCommand>&& command, NodeId nodeId, FollowType followType)
178 {
179 std::unique_lock<std::mutex> lock(commandMutex_);
180 if (command) {
181 payload_.emplace_back(nodeId, followType, std::move(command));
182 }
183 }
184
UnmarshallingCommand(Parcel & parcel)185 bool RSTransactionData::UnmarshallingCommand(Parcel& parcel)
186 {
187 Clear();
188
189 int commandSize = 0;
190 if (!parcel.ReadInt32(commandSize)) {
191 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read commandSize");
192 return false;
193 }
194 uint8_t followType = 0;
195 NodeId nodeId = 0;
196 uint8_t hasCommand = 0;
197 uint16_t commandType = 0;
198 uint16_t commandSubType = 0;
199
200 size_t readableSize = parcel.GetReadableBytes();
201 size_t len = static_cast<size_t>(commandSize);
202 if (len > readableSize || len > payload_.max_size()) {
203 ROSEN_LOGE("RSTransactionData UnmarshallingCommand Failed read vector, size:%{public}zu,"
204 " readAbleSize:%{public}zu", len, readableSize);
205 return false;
206 }
207
208 bool isUniRender = false;
209 if (!parcel.ReadBool(isUniRender)) {
210 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read isUniRender");
211 return false;
212 }
213 std::unique_lock<std::mutex> payloadLock(commandMutex_, std::defer_lock);
214 for (size_t i = 0; i < len; i++) {
215 if (!isUniRender) {
216 if (!parcel.ReadUint64(nodeId)) {
217 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read nodeId");
218 return false;
219 }
220 if (!parcel.ReadUint8(followType)) {
221 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read followType");
222 return false;
223 }
224 }
225 if (!parcel.ReadUint8(hasCommand)) {
226 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read hasCommand");
227 return false;
228 }
229 if (hasCommand) {
230 if (!(parcel.ReadUint16(commandType) && parcel.ReadUint16(commandSubType))) {
231 return false;
232 }
233 auto func = RSCommandFactory::Instance().GetUnmarshallingFunc(commandType, commandSubType);
234 if (func == nullptr) {
235 return false;
236 }
237 auto command = (*func)(parcel);
238 if (command == nullptr) {
239 ROSEN_LOGE("failed RSTransactionData::UnmarshallingCommand, type=%{public}d subtype=%{public}d",
240 commandType, commandSubType);
241 return false;
242 }
243 payloadLock.lock();
244 payload_.emplace_back(nodeId, static_cast<FollowType>(followType), std::move(command));
245 payloadLock.unlock();
246 } else {
247 continue;
248 }
249 }
250 int32_t pid;
251 return parcel.ReadBool(needSync_) && parcel.ReadBool(needCloseSync_) && parcel.ReadInt32(syncTransactionCount_) &&
252 parcel.ReadUint64(timestamp_) && parcel.ReadInt32(pid) && ({pid_ = pid; true;}) &&
253 parcel.ReadUint64(index_) && parcel.ReadUint64(syncId_);
254 }
255
256
257 } // namespace Rosen
258 } // namespace OHOS
259