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 enum RSCommandPermissionType : uint16_t { 49 PERMISSION_APP, 50 PERMISSION_SYSTEM, 51 }; 52 53 // [attention] 54 // RSCommand object is serializable, when use ADD_COMMAND macro to define a new RSCommand type, 55 // use POD types or type with marshalling & unmarshalling func defined in RSMarshallingHelper for construction 56 // parameters, otherwise you should define marshalling & unmarshalling func for your param type. 57 // Especially, don't use enum class type directly, convert it to int 58 class RSCommand : public Parcelable { 59 public: 60 RSCommand() = default; 61 RSCommand(const RSCommand&) = delete; 62 RSCommand(const RSCommand&&) = delete; 63 RSCommand& operator=(const RSCommand&) = delete; 64 RSCommand& operator=(const RSCommand&&) = delete; 65 ~RSCommand() noexcept override = default; 66 67 virtual void Process(RSContext& context) = 0; 68 GetAccessPermission()69 virtual RSCommandPermissionType GetAccessPermission() const 70 { 71 return RSCommandPermissionType::PERMISSION_SYSTEM; 72 } 73 GetType()74 virtual uint16_t GetType() const 75 { 76 return 0; 77 } 78 GetSubType()79 virtual uint16_t GetSubType() const 80 { 81 return 0; 82 } 83 GetDrawCmdList()84 virtual std::shared_ptr<Drawing::DrawCmdList> GetDrawCmdList() const 85 { 86 return nullptr; 87 } 88 GetUniqueType()89 std::pair<uint16_t, uint16_t> GetUniqueType() const 90 { 91 return std::make_pair(GetType(), GetSubType()); 92 } 93 GetNodeId()94 virtual NodeId GetNodeId() const 95 { 96 return 0; 97 } 98 GetToken()99 virtual uint64_t GetToken() const 100 { 101 return 0; 102 } 103 PrintType()104 std::string PrintType() const 105 { 106 return "commandType:[" + std::to_string(GetType()) + ", " + std::to_string(GetSubType()) + "], "; 107 } 108 SetCallingPidValid(bool isCallingPidValid)109 void SetCallingPidValid(bool isCallingPidValid) 110 { 111 if (isCallingPidValid_.load() != isCallingPidValid) { 112 isCallingPidValid_.store(isCallingPidValid); 113 } 114 } 115 IsCallingPidValid()116 bool IsCallingPidValid() const 117 { 118 return isCallingPidValid_.load(); 119 } 120 121 private: 122 size_t indexVerifier_ = 0; 123 std::atomic_bool isCallingPidValid_ = true; 124 friend class RSTransactionData; 125 #ifdef RS_PROFILER_ENABLED 126 protected: 127 using PatchFunction = NodeId (*)(NodeId); 128 129 private: 130 friend class RSProfiler; Patch(PatchFunction function)131 virtual void Patch(PatchFunction function) {}; 132 #endif 133 }; 134 135 class RSSyncTask : public RSCommand { 136 public: RSSyncTask(uint64_t timeoutNS)137 RSSyncTask(uint64_t timeoutNS) : timeoutNS_(timeoutNS) {} 138 139 virtual bool CheckHeader(Parcel& parcel) const = 0; 140 virtual bool ReadFromParcel(Parcel& parcel) = 0; IsCallingPidValid(pid_t callingPid,const RSRenderNodeMap & nodeMap)141 virtual bool IsCallingPidValid(pid_t callingPid, const RSRenderNodeMap& nodeMap) const 142 { 143 return true; 144 } GetType()145 virtual uint16_t GetType() const override 146 { 147 return 0; 148 } 149 GetTimeout()150 inline uint64_t GetTimeout() const 151 { 152 return timeoutNS_; 153 } IsSuccess()154 inline bool IsSuccess() const 155 { 156 return success_; 157 } 158 159 protected: 160 uint64_t timeoutNS_ = 0; 161 bool success_ = false; 162 }; 163 164 } // namespace Rosen 165 } // namespace OHOS 166 167 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_H 168