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_command.h"
19 #include "command/rs_command_factory.h"
20 #include "platform/common/rs_log.h"
21 #include "platform/common/rs_system_properties.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 static constexpr size_t PARCEL_MAX_CPACITY = 2000 * 1024; // upper bound of parcel capacity
27 static constexpr size_t PARCEL_SPLIT_THRESHOLD = 1800 * 1024; // should be < PARCEL_MAX_CPACITY
28 }
29
Unmarshalling(Parcel & parcel)30 RSTransactionData* RSTransactionData::Unmarshalling(Parcel& parcel)
31 {
32 auto transactionData = new RSTransactionData();
33 if (transactionData->UnmarshallingCommand(parcel)) {
34 return transactionData;
35 }
36 ROSEN_LOGE("RSTransactionData Unmarshalling Failed");
37 delete transactionData;
38 return nullptr;
39 }
40
~RSTransactionData()41 RSTransactionData::~RSTransactionData()
42 {
43 Clear();
44 }
45
Marshalling(Parcel & parcel) const46 bool RSTransactionData::Marshalling(Parcel& parcel) const
47 {
48 bool success = true;
49 parcel.SetMaxCapacity(PARCEL_MAX_CPACITY);
50 // to correct actual marshaled command size later, record its position in parcel
51 size_t recordPosition = parcel.GetWritePosition();
52 std::unique_lock<std::mutex> lock(commandMutex_);
53 success = success && parcel.WriteInt32(static_cast<int32_t>(payload_.size()));
54 size_t marshaledSize = 0;
55 static bool isUniRender = RSSystemProperties::GetUniRenderEnabled();
56 success = success && parcel.WriteBool(isUniRender);
57 while (marshallingIndex_ < payload_.size()) {
58 auto& [nodeId, followType, command] = payload_[marshallingIndex_];
59 if (!isUniRender) {
60 success = success && parcel.WriteUint64(nodeId);
61 success = success && parcel.WriteUint8(static_cast<uint8_t>(followType));
62 }
63 success = success && command->Marshalling(parcel);
64 if (!success) {
65 ROSEN_LOGE("failed RSTransactionData::Marshalling type:%s", command->PrintType().c_str());
66 return false;
67 }
68 ++marshallingIndex_;
69 ++marshaledSize;
70 if (parcel.GetDataSize() > PARCEL_SPLIT_THRESHOLD) {
71 break;
72 }
73 }
74 if (marshaledSize < payload_.size()) {
75 // correct command size recorded in Parcel
76 *reinterpret_cast<int32_t*>(parcel.GetData() + recordPosition) = static_cast<int32_t>(marshaledSize);
77 ROSEN_LOGW("RSTransactionData::Marshalling data split to several parcels"
78 ", marshaledSize:%zu, marshallingIndex_:%zu, total count:%zu"
79 ", parcel size:%zu, threshold:%zu",
80 marshaledSize, marshallingIndex_, payload_.size(), parcel.GetDataSize(), PARCEL_SPLIT_THRESHOLD);
81 }
82 success = success && parcel.WriteBool(needSync_);
83 success = success && parcel.WriteBool(needCloseSync_);
84 success = success && parcel.WriteInt32(syncTransactionCount_);
85 success = success && parcel.WriteUint64(timestamp_);
86 success = success && parcel.WriteInt32(pid_);
87 success = success && parcel.WriteUint64(index_);
88 success = success && parcel.WriteUint64(syncId_);
89 return success;
90 }
91
Process(RSContext & context)92 void RSTransactionData::Process(RSContext& context)
93 {
94 std::unique_lock<std::mutex> lock(commandMutex_);
95 for (auto& [nodeId, followType, command] : payload_) {
96 if (command != nullptr) {
97 command->Process(context);
98 }
99 }
100 }
101
Clear()102 void RSTransactionData::Clear()
103 {
104 std::unique_lock<std::mutex> lock(commandMutex_);
105 payload_.clear();
106 timestamp_ = 0;
107 }
108
AddCommand(std::unique_ptr<RSCommand> & command,NodeId nodeId,FollowType followType)109 void RSTransactionData::AddCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType)
110 {
111 std::unique_lock<std::mutex> lock(commandMutex_);
112 payload_.emplace_back(nodeId, followType, std::move(command));
113 }
114
AddCommand(std::unique_ptr<RSCommand> && command,NodeId nodeId,FollowType followType)115 void RSTransactionData::AddCommand(std::unique_ptr<RSCommand>&& command, NodeId nodeId, FollowType followType)
116 {
117 std::unique_lock<std::mutex> lock(commandMutex_);
118 payload_.emplace_back(nodeId, followType, std::move(command));
119 }
120
UnmarshallingCommand(Parcel & parcel)121 bool RSTransactionData::UnmarshallingCommand(Parcel& parcel)
122 {
123 Clear();
124
125 int commandSize = 0;
126 if (!parcel.ReadInt32(commandSize)) {
127 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read commandSize");
128 return false;
129 }
130 uint8_t followType = 0;
131 NodeId nodeId = 0;
132 uint16_t commandType = 0;
133 uint16_t commandSubType = 0;
134
135 size_t readableSize = parcel.GetReadableBytes();
136 size_t len = static_cast<size_t>(commandSize);
137 if (len > readableSize || len > payload_.max_size()) {
138 ROSEN_LOGE("RSTransactionData UnmarshallingCommand Failed read vector, size:%zu, readAbleSize:%zu",
139 len, readableSize);
140 return false;
141 }
142
143 bool isUniRender = false;
144 if (!parcel.ReadBool(isUniRender)) {
145 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read isUniRender");
146 return false;
147 }
148 std::unique_lock<std::mutex> payloadLock(commandMutex_, std::defer_lock);
149 for (size_t i = 0; i < len; i++) {
150 if (!isUniRender) {
151 if (!parcel.ReadUint64(nodeId)) {
152 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read nodeId");
153 return false;
154 }
155 if (!parcel.ReadUint8(followType)) {
156 ROSEN_LOGE("RSTransactionData::UnmarshallingCommand cannot read followType");
157 return false;
158 }
159 }
160
161 if (!(parcel.ReadUint16(commandType) && parcel.ReadUint16(commandSubType))) {
162 return false;
163 }
164 auto func = RSCommandFactory::Instance().GetUnmarshallingFunc(commandType, commandSubType);
165 if (func == nullptr) {
166 return false;
167 }
168 auto command = (*func)(parcel);
169 if (command == nullptr) {
170 ROSEN_LOGE("failed RSTransactionData::UnmarshallingCommand, type=%d subtype=%d", commandType,
171 commandSubType);
172 return false;
173 }
174 payloadLock.lock();
175 payload_.emplace_back(nodeId, static_cast<FollowType>(followType), std::move(command));
176 payloadLock.unlock();
177 }
178 int32_t pid;
179 return parcel.ReadBool(needSync_) && parcel.ReadBool(needCloseSync_) && parcel.ReadInt32(syncTransactionCount_) &&
180 parcel.ReadUint64(timestamp_) && parcel.ReadInt32(pid) && ({pid_ = pid; true;}) &&
181 parcel.ReadUint64(index_) && parcel.ReadUint64(syncId_);
182 }
183
184
185 } // namespace Rosen
186 } // namespace OHOS
187