1 /* 2 * Copyright (c) 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 CMD_LIST_H 17 #define CMD_LIST_H 18 19 #include <map> 20 #include <optional> 21 #include <vector> 22 23 #include "draw/canvas.h" 24 #include "recording/op_item.h" 25 #include "recording/mem_allocator.h" 26 #include "recording/recording_handle.h" 27 #include "utils/drawing_macros.h" 28 #include "utils/extend_object.h" 29 #ifdef ROSEN_OHOS 30 #include "surface_buffer.h" 31 #include "sync_fence.h" 32 #endif 33 34 namespace OHOS { 35 namespace Rosen { 36 namespace Drawing { 37 using CmdListData = std::pair<const void*, size_t>; 38 using NodeId = uint64_t; 39 40 class DRAWING_API ExtendImageObject { 41 public: 42 virtual ~ExtendImageObject() = default; 43 virtual void Playback(Canvas& canvas, const Rect& rect, 44 const SamplingOptions& sampling, bool isBackground = false) = 0; SetNodeId(NodeId id)45 virtual void SetNodeId(NodeId id) {}; SetPaint(Paint paint)46 virtual void SetPaint(Paint paint) {}; Purge()47 virtual void Purge() {}; 48 }; 49 50 class DRAWING_API ExtendImageBaseObj { 51 public: 52 virtual ~ExtendImageBaseObj() = default; 53 virtual void Playback(Canvas& canvas, const Rect& rect, const SamplingOptions& sampling, 54 SrcRectConstraint constraint = SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT) = 0; SetNodeId(NodeId id)55 virtual void SetNodeId(NodeId id) {}; Purge()56 virtual void Purge() {}; 57 }; 58 59 class DRAWING_API ExtendDrawFuncObj { 60 public: 61 virtual ~ExtendDrawFuncObj () = default; 62 virtual void Playback(Canvas* canvas, const Rect* rect) = 0; 63 }; 64 65 #ifdef ROSEN_OHOS 66 struct SurfaceBufferEntry { 67 SurfaceBufferEntry() = default; SurfaceBufferEntrySurfaceBufferEntry68 SurfaceBufferEntry(const sptr<SurfaceBuffer> surfaceBuffer, const sptr<SyncFence> acquireFence) 69 : surfaceBuffer_(surfaceBuffer), acquireFence_(acquireFence) 70 {} 71 sptr<SurfaceBuffer> surfaceBuffer_ = nullptr; 72 sptr<SyncFence> acquireFence_ = nullptr; 73 }; 74 #endif 75 76 class DRAWING_API CmdList { 77 public: 78 enum Type : uint32_t { 79 CMD_LIST = 0, 80 DRAW_CMD_LIST, 81 MASK_CMD_LIST, 82 }; 83 84 CmdList() = default; 85 explicit CmdList(const CmdListData& cmdListData); 86 virtual ~CmdList(); 87 GetType()88 virtual uint32_t GetType() const 89 { 90 return Type::CMD_LIST; 91 } 92 93 /** 94 * @brief Add OpItem to CmdList. 95 * @param T The name of OpItem class. 96 * @param Args Constructs arguments to the OpItem. 97 */ 98 template<typename T, typename... Args> AddOp(Args &&...args)99 void AddOp(Args&&... args) 100 { 101 std::lock_guard<std::recursive_mutex> lock(mutex_); 102 T* op = opAllocator_.Allocate<T>(std::forward<Args>(args)...); 103 if (op == nullptr) { 104 return; 105 } 106 107 size_t offset = opAllocator_.AddrToOffset(op); 108 if (lastOpItemOffset_.has_value()) { 109 #ifdef CROSS_PLATFORM 110 auto* lastOpItem = static_cast<OpItem*>( 111 opAllocator_.OffsetToAddr(lastOpItemOffset_.__get(), sizeof(OpItem))); 112 #else 113 auto* lastOpItem = static_cast<OpItem*>( 114 opAllocator_.OffsetToAddr(lastOpItemOffset_.value(), sizeof(OpItem))); 115 #endif 116 if (lastOpItem != nullptr) { 117 lastOpItem->SetNextOpItemOffset(offset); 118 } 119 } 120 lastOpItemOffset_.emplace(offset); 121 opCnt_++; 122 } 123 124 /** 125 * @brief Add a contiguous buffers to the CmdList. 126 * @param data A contiguous buffers. 127 * @return Returns the offset of the contiguous buffers and CmdList head point. 128 */ 129 size_t AddCmdListData(const CmdListData& data); 130 131 const void* GetCmdListData(size_t offset, size_t size) const; 132 133 /** 134 * @brief Gets the contiguous buffers of CmdList. 135 */ 136 CmdListData GetData() const; 137 138 // using for recording, should to remove after using shared memory 139 bool SetUpImageData(const void* data, size_t size); 140 size_t AddImageData(const void* data, size_t size); 141 const void* GetImageData(size_t offset, size_t size) const; 142 CmdListData GetAllImageData() const; 143 144 OpDataHandle AddImage(const Image& image); 145 std::shared_ptr<Image> GetImage(const OpDataHandle& imageHandle); 146 147 size_t AddBitmapData(const void* data, size_t size); 148 const void* GetBitmapData(size_t offset, size_t size) const; 149 bool SetUpBitmapData(const void* data, size_t size); 150 CmdListData GetAllBitmapData() const; 151 152 /* 153 * @brief return ExtendObject index. UINT32_MAX is error. 154 */ 155 uint32_t AddExtendObject(const std::shared_ptr<ExtendObject>& object); 156 157 /* 158 * @brief get ExtendObject by index. 159 */ 160 std::shared_ptr<ExtendObject> GetExtendObject(uint32_t index); 161 162 /* 163 * @brief return ExtendObject size, 0 is no ExtendObject. 164 */ 165 uint32_t GetAllExtendObject(std::vector<std::shared_ptr<ExtendObject>>& objectList); 166 167 /* 168 * @brief return real setup ExtendObject size. 169 */ 170 uint32_t SetupExtendObject(const std::vector<std::shared_ptr<ExtendObject>>& objectList); 171 172 /* 173 * @brief return RecordCmd index. UINT32_MAX is error. 174 */ 175 uint32_t AddRecordCmd(const std::shared_ptr<RecordCmd>& recordCmd); 176 177 /* 178 * @brief get RecordCmd by index. 179 */ 180 std::shared_ptr<RecordCmd> GetRecordCmd(uint32_t index); 181 182 /* 183 * @brief return real setup RecordCmd size. 184 */ 185 uint32_t SetupRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList); 186 187 /* 188 * @brief return RecordCmd size, 0 is no RecordCmd. 189 */ 190 uint32_t GetAllRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList); 191 192 /* 193 * @brief return imageObject index, negative is error. 194 */ 195 uint32_t AddImageObject(const std::shared_ptr<ExtendImageObject>& object); 196 197 /* 198 * @brief get imageObject by index. 199 */ 200 std::shared_ptr<ExtendImageObject> GetImageObject(uint32_t id); 201 202 /* 203 * @brief return imageObject size, 0 is no imageObject. 204 */ 205 uint32_t GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>>& objectList); 206 207 /* 208 * @brief return real setup imageObject size. 209 */ 210 uint32_t SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>>& objectList); 211 212 /* 213 * @brief return imageBaseObj index, negative is error. 214 */ 215 uint32_t AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj>& object); 216 217 /* 218 * @brief get imageBaseObj by index. 219 */ 220 std::shared_ptr<ExtendImageBaseObj> GetImageBaseObj(uint32_t id); 221 222 /* 223 * @brief return imageBaseObj size, 0 is no imageBaseObj. 224 */ 225 uint32_t GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList); 226 227 /* 228 * @brief return real setup imageBaseObj size. 229 */ 230 uint32_t SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList); 231 232 /* 233 * @brief return DrawFuncObj index, negative is error. 234 */ 235 uint32_t AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj>& object); 236 237 /* 238 * @brief get DrawFuncObj by index. 239 */ 240 std::shared_ptr<ExtendDrawFuncObj> GetDrawFuncObj(uint32_t id); 241 242 /* 243 * @brief copy object vec to another CmdList. 244 */ 245 void CopyObjectTo(CmdList& other) const; 246 247 /* 248 * @brief return recording op count. 249 */ 250 uint32_t GetOpCnt() const; 251 252 CmdList(CmdList&&) = delete; 253 CmdList(const CmdList&) = delete; 254 CmdList& operator=(CmdList&&) = delete; 255 CmdList& operator=(const CmdList&) = delete; 256 257 #ifdef ROSEN_OHOS 258 /* 259 * @brief return surfaceBufferEntry index, negative is error. 260 */ 261 uint32_t AddSurfaceBufferEntry(const std::shared_ptr<SurfaceBufferEntry>& surfaceBufferEntry); 262 263 /* 264 * @brief get surfaceBufferEntry by index. 265 */ 266 std::shared_ptr<SurfaceBufferEntry> GetSurfaceBufferEntry(uint32_t id); 267 268 /* 269 * @brief return surfaceBufferEntry size, 0 is no surfaceBuffer. 270 */ 271 uint32_t GetAllSurfaceBufferEntry(std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList); 272 273 /* 274 * @brief return real setup surfaceBufferEntry size. 275 */ 276 uint32_t SetupSurfaceBufferEntry(const std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList); 277 #endif 278 279 protected: 280 MemAllocator opAllocator_; 281 MemAllocator imageAllocator_; 282 MemAllocator bitmapAllocator_; 283 std::optional<size_t> lastOpItemOffset_ = std::nullopt; 284 std::recursive_mutex mutex_; 285 std::map<size_t, std::shared_ptr<Image>> imageMap_; 286 std::vector<std::pair<size_t, OpDataHandle>> imageHandleVec_; 287 uint32_t opCnt_ = 0; 288 289 std::vector<std::shared_ptr<RecordCmd>> recordCmdVec_; 290 std::mutex recordCmdMutex_; 291 std::vector<std::shared_ptr<ExtendImageObject>> imageObjectVec_; 292 std::mutex imageObjectMutex_; 293 std::vector<std::shared_ptr<ExtendImageBaseObj>> imageBaseObjVec_; 294 std::mutex imageBaseObjMutex_; 295 std::vector<std::shared_ptr<ExtendObject>> extendObjectVec_; 296 std::mutex extendObjectMutex_; 297 #ifdef ROSEN_OHOS 298 std::vector<std::shared_ptr<SurfaceBufferEntry>> surfaceBufferEntryVec_; 299 std::mutex surfaceBufferEntryMutex_; 300 #endif 301 std::vector<std::shared_ptr<ExtendDrawFuncObj>> drawFuncObjVec_; 302 std::mutex drawFuncObjMutex_; 303 }; 304 } // namespace Drawing 305 } // namespace Rosen 306 } // namespace OHOS 307 #endif