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 <optional> 20 #include <vector> 21 22 #include "draw/color.h" 23 #include "recording/op_item.h" 24 #include "recording/mem_allocator.h" 25 26 namespace OHOS { 27 namespace Media { 28 class PixelMap; 29 } 30 namespace Rosen { 31 namespace Drawing { 32 struct ImageHandle { 33 uint32_t offset; 34 size_t size; 35 int32_t width; 36 int32_t height; 37 ColorType colorType; 38 AlphaType alphaType; 39 }; 40 41 struct CmdListHandle { 42 uint32_t type; 43 uint32_t offset; 44 size_t size; 45 uint32_t imageOffset; 46 size_t imageSize; 47 }; 48 49 using CmdListData = std::pair<const void*, size_t>; 50 51 class CmdList { 52 public: 53 enum Type : uint32_t { 54 CMD_LIST = 0, 55 COLOR_FILTER_CMD_LIST, 56 COLOR_SPACE_CMD_LIST, 57 DRAW_CMD_LIST, 58 IMAGE_FILTER_CMD_LIST, 59 MASK_FILTER_CMD_LIST, 60 PATH_CMD_LIST, 61 PATH_EFFECT_CMD_LIST, 62 REGION_CMD_LIST, 63 SHADER_EFFECT_CMD_LIST, 64 }; 65 66 CmdList() = default; 67 explicit CmdList(const CmdListData& cmdListData); 68 virtual ~CmdList(); 69 GetType()70 virtual uint32_t GetType() const 71 { 72 return Type::CMD_LIST; 73 } 74 75 /* 76 * @brief Add OpItem to CmdList. 77 * @param T The name of OpItem class. 78 * @param Args Constructs arguments to the OpItem. 79 */ 80 template<typename T, typename... Args> AddOp(Args &&...args)81 void AddOp(Args&&... args) 82 { 83 std::lock_guard<std::mutex> lock(mutex_); 84 T* op = opAllocator_.Allocate<T>(std::forward<Args>(args)...); 85 if (op == nullptr) { 86 return; 87 } 88 89 uint32_t offset = opAllocator_.AddrToOffset(op); 90 if (lastOpItemOffset_.has_value()) { 91 auto* lastOpItem = static_cast<OpItem*>(opAllocator_.OffsetToAddr(lastOpItemOffset_.value())); 92 if (lastOpItem != nullptr) { 93 lastOpItem->SetNextOpItemOffset(offset); 94 } 95 } 96 lastOpItemOffset_.emplace(offset); 97 } 98 99 /* 100 * @brief Add a contiguous buffers to the CmdList. 101 * @param src A contiguous buffers. 102 * @return Returns the offset of the contiguous buffers and CmdList head point. 103 */ 104 uint32_t AddCmdListData(const CmdListData& data); 105 106 const void* GetCmdListData(uint32_t offset) const; 107 108 /* 109 * @brief Gets the contiguous buffers of CmdList. 110 */ 111 CmdListData GetData() const; 112 113 // using for recording, should to remove after using shared memory 114 bool SetUpImageData(const void* data, size_t size); 115 uint32_t AddImageData(const void* data, size_t size); 116 const void* GetImageData(uint32_t offset) const; 117 CmdListData GetAllImageData() const; 118 119 /* 120 * @brief return pixelmap index, negative is error. 121 */ 122 uint32_t AddPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap); 123 124 /* 125 * @brief get pixelmap by index. 126 */ 127 std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t id); 128 129 /* 130 * @brief return pixelmaplist size, 0 is no pixelmap. 131 */ 132 uint32_t GetAllPixelMap(std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList); 133 134 /* 135 * @brief return real setup pixelmap size. 136 */ 137 uint32_t SetupPixelMap(const std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList); 138 139 CmdList(CmdList&&) = delete; 140 CmdList(const CmdList&) = delete; 141 CmdList& operator=(CmdList&&) = delete; 142 CmdList& operator=(const CmdList&) = delete; 143 144 protected: 145 MemAllocator opAllocator_; 146 MemAllocator imageAllocator_; 147 std::optional<uint32_t> lastOpItemOffset_ = std::nullopt; 148 std::mutex mutex_; 149 #ifdef SUPPORT_OHOS_PIXMAP 150 std::vector<std::shared_ptr<Media::PixelMap>> pixelMapVec_; 151 std::mutex pixelMapMutex_; 152 #endif 153 }; 154 } // namespace Drawing 155 } // namespace Rosen 156 } // namespace OHOS 157 #endif