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_COMMAND_RS_COMMAND_H
17 #define ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_H
18 
19 #include <parcel.h>
20 #include <refbase.h>
21 
22 #include "common/rs_common_def.h"
23 #include "pipeline/rs_context.h"
24 #include "recording/draw_cmd_list.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 
29 enum RSCommandType : uint16_t {
30     // node commands
31     BASE_NODE,
32     RS_NODE,     // formerly RSPropertyNode
33     CANVAS_NODE, // formerly RSNode
34     SURFACE_NODE,
35     PROXY_NODE,
36     ROOT_NODE,
37     DISPLAY_NODE,
38     EFFECT_NODE,
39     CANVAS_DRAWING_NODE,
40     // animation commands
41     ANIMATION,
42     // read showing properties (deprecated, will be removed later)
43     RS_NODE_SYNCHRONOUS_READ_PROPERTY,
44     RS_NODE_SYNCHRONOUS_GET_VALUE_FRACTION,
45     FRAME_RATE_LINKER,
46 };
47 
48 // [attention]
49 // RSCommand object is serializable, when use ADD_COMMAND macro to define a new RSCommand type,
50 // use POD types or type with marshalling & unmarshalling func defined in RSMarshallingHelper for construction
51 // parameters, otherwise you should define marshalling & unmarshalling func for your param type.
52 // Especially, don't use enum class type directly, convert it to int
53 class RSCommand : public Parcelable {
54 public:
55     RSCommand() = default;
56     RSCommand(const RSCommand&) = delete;
57     RSCommand(const RSCommand&&) = delete;
58     RSCommand& operator=(const RSCommand&) = delete;
59     RSCommand& operator=(const RSCommand&&) = delete;
60     ~RSCommand() noexcept override = default;
61 
62     virtual void Process(RSContext& context) = 0;
63 
GetType()64     virtual uint16_t GetType() const
65     {
66         return 0;
67     }
68 
GetSubType()69     virtual uint16_t GetSubType() const
70     {
71         return 0;
72     }
73 
GetDrawCmdList()74     virtual std::shared_ptr<Drawing::DrawCmdList> GetDrawCmdList() const
75     {
76         return nullptr;
77     }
78 
GetUniqueType()79     std::pair<uint16_t, uint16_t> GetUniqueType() const
80     {
81         return std::make_pair(GetType(), GetSubType());
82     }
83 
GetNodeId()84     virtual NodeId GetNodeId() const
85     {
86         return 0;
87     }
88 
PrintType()89     std::string PrintType() const
90     {
91         return "commandType:[" + std::to_string(GetType()) + ", " + std::to_string(GetSubType()) + "], ";
92     }
93 
SetCallingPidValid(bool isCallingPidValid)94     void SetCallingPidValid(bool isCallingPidValid)
95     {
96         if (isCallingPidValid_.load() != isCallingPidValid) {
97             isCallingPidValid_.store(isCallingPidValid);
98         }
99     }
100 
IsCallingPidValid()101     bool IsCallingPidValid() const
102     {
103         return isCallingPidValid_.load();
104     }
105 
106 private:
107     size_t indexVerifier_ = 0;
108     std::atomic_bool isCallingPidValid_ = true;
109     friend class RSTransactionData;
110 #ifdef RS_PROFILER_ENABLED
111 protected:
112     using PatchFunction = NodeId (*)(NodeId);
113 
114 private:
115     friend class RSProfiler;
Patch(PatchFunction function)116     virtual void Patch(PatchFunction function) {};
117 #endif
118 };
119 
120 class RSSyncTask : public RSCommand {
121 public:
RSSyncTask(uint64_t timeoutNS)122     RSSyncTask(uint64_t timeoutNS) : timeoutNS_(timeoutNS) {}
123 
124     virtual bool CheckHeader(Parcel& parcel) const = 0;
125     virtual bool ReadFromParcel(Parcel& parcel) = 0;
126 
GetTimeout()127     inline uint64_t GetTimeout() const
128     {
129         return timeoutNS_;
130     }
IsSuccess()131     inline bool IsSuccess() const
132     {
133         return success_;
134     }
135 
136 protected:
137     uint64_t timeoutNS_ = 0;
138     bool success_ = false;
139 };
140 
141 } // namespace Rosen
142 } // namespace OHOS
143 
144 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_H
145