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 #ifndef ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_DATA_H
17 #define ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_DATA_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <vector>
22 
23 #include "command/rs_command.h"
24 #include "command/rs_node_showing_command.h"
25 #include "common/rs_macros.h"
26 #include "pipeline/rs_context.h"
27 
28 #include <parcel.h>
29 
30 namespace OHOS {
31 namespace Rosen {
32 class RSB_EXPORT RSTransactionData : public Parcelable {
33 public:
34     RSTransactionData() = default;
35     RSTransactionData(const RSTransactionData&) = delete;
36     RSTransactionData& operator=(const RSTransactionData&) = delete;
RSTransactionData(RSTransactionData && other)37     RSTransactionData(RSTransactionData&& other)
38         : payload_(std::move(other.payload_)), timestamp_(std::move(other.timestamp_)),
39           abilityName_(std::move(other.abilityName_)), pid_(other.pid_), index_(other.index_)
40     {}
41     ~RSTransactionData() override;
42 
43     [[nodiscard]] static RSTransactionData* Unmarshalling(Parcel& parcel);
44     bool Marshalling(Parcel& parcel) const override;
45     void AlarmRsNodeLog() const;
46 
GetCommandCount()47     unsigned long GetCommandCount() const
48     {
49         return payload_.size();
50     }
51 
IsEmpty()52     bool IsEmpty() const
53     {
54         return payload_.empty();
55     }
56 
57     void Process(RSContext& context);
58     void ProcessBySingleFrameComposer(RSContext& context);
59     static void AddAlarmLog(std::function<void(uint64_t, int, int)> func);
60 
61     void Clear();
62 
GetTimestamp()63     uint64_t GetTimestamp() const
64     {
65         return timestamp_;
66     }
67 
SetTimestamp(const uint64_t timestamp)68     void SetTimestamp(const uint64_t timestamp)
69     {
70         timestamp_ = timestamp;
71     }
72 
GetAbilityName()73     std::string GetAbilityName() const
74     {
75         return abilityName_;
76     }
77 
SetAbilityName(const std::string & abilityName)78     void SetAbilityName(const std::string& abilityName)
79     {
80         abilityName_ = abilityName;
81     }
82 
SetIsCached(bool isCached)83     void SetIsCached(bool isCached)
84     {
85         isCached_ = isCached;
86     }
87 
GetIsCached()88     bool GetIsCached()
89     {
90         return isCached_;
91     }
92 
SetSendingPid(pid_t pid)93     void SetSendingPid(pid_t pid)
94     {
95         pid_ = pid;
96     }
97 
GetSendingPid()98     pid_t GetSendingPid() const
99     {
100         return pid_;
101     }
102 
SetIndex(uint64_t index)103     void SetIndex(uint64_t index)
104     {
105         index_ = index;
106     }
107 
GetIndex()108     uint64_t GetIndex() const
109     {
110         return index_;
111     }
112 
GetMarshallingIndex()113     size_t GetMarshallingIndex() const
114     {
115         return marshallingIndex_;
116     }
117 
GetPayload()118     std::vector<std::tuple<NodeId, FollowType, std::unique_ptr<RSCommand>>>& GetPayload()
119     {
120         return payload_;
121     }
122 
IsNeedSync()123     bool IsNeedSync() const
124     {
125         return needSync_;
126     }
127 
IsNeedCloseSync()128     bool IsNeedCloseSync() const
129     {
130         return needCloseSync_;
131     }
132 
MarkNeedSync()133     void MarkNeedSync()
134     {
135         needSync_ = true;
136     }
137 
MarkNeedCloseSync()138     void MarkNeedCloseSync()
139     {
140         needCloseSync_ = true;
141     }
142 
SetSyncTransactionNum(const int32_t syncTransactionCount)143     void SetSyncTransactionNum(const int32_t syncTransactionCount)
144     {
145         syncTransactionCount_ = syncTransactionCount;
146     }
147 
GetSyncTransactionNum()148     int32_t GetSyncTransactionNum() const
149     {
150         return syncTransactionCount_;
151     }
152 
SetSyncId(const uint64_t syncId)153     void SetSyncId(const uint64_t syncId)
154     {
155         syncId_ = syncId;
156     }
157 
GetSyncId()158     uint64_t GetSyncId() const
159     {
160         return syncId_;
161     }
162 
SetParentPid(const int32_t parentPid)163     void SetParentPid(const int32_t parentPid)
164     {
165         parentPid_ = parentPid;
166     }
167 
GetParentPid()168     int32_t GetParentPid() const
169     {
170         return parentPid_;
171     }
172 
173     bool IsCallingPidValid(pid_t callingPid, const RSRenderNodeMap& nodeMap, pid_t& conflictCommandPid,
174         std::string& commandMapDesc) const;
175 
176 private:
177     void AddCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType);
178     void AddCommand(std::unique_ptr<RSCommand>&& command, NodeId nodeId, FollowType followType);
179 
180     bool UnmarshallingCommand(Parcel& parcel);
181 
182     std::string PrintCommandMapDesc(
183         const std::unordered_map<NodeId, std::set<std::pair<uint16_t, uint16_t>>>& commandTypeMap) const;
184 
185     std::vector<std::tuple<NodeId, FollowType, std::unique_ptr<RSCommand>>> payload_ = {};
186     uint64_t timestamp_ = 0;
187     std::string abilityName_;
188     pid_t pid_ = 0;
189     uint64_t index_ = 0;
190     mutable size_t marshallingIndex_ = 0;
191     bool needSync_ { false };
192     bool needCloseSync_ { false };
193     bool isCached_ { false };
194     int32_t syncTransactionCount_ { 0 };
195     int32_t parentPid_ { -1 };
196     uint64_t syncId_ { 0 };
197     static std::function<void(uint64_t, int, int)> alarmLogFunc;
198     mutable std::mutex commandMutex_;
199 
200     friend class RSTransactionProxy;
201     friend class RSMessageProcessor;
202     friend class RSMainThread;
203 };
204 using TransactionDataMap = std::unordered_map<pid_t, std::vector<std::unique_ptr<RSTransactionData>>>;
205 } // namespace Rosen
206 } // namespace OHOS
207 
208 #endif // ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_DATA_H
209