1 /* 2 * Copyright (c) 2023-2025 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_HELPER_H 17 #define CMD_LIST_HELPER_H 18 19 #include <memory> 20 #include <utility> 21 22 #include "image/image.h" 23 #include "recording/cmd_list.h" 24 #include "recording/record_cmd.h" 25 #include "text/hm_symbol.h" 26 #include "text/text_blob.h" 27 #include "utils/log.h" 28 #include "utils/vertices.h" 29 30 namespace OHOS { 31 namespace Rosen { 32 namespace Drawing { 33 constexpr size_t MAX_VECTORSIZE = 170000; 34 class DrawOpItem; 35 class DRAWING_API CmdListHelper { 36 public: 37 CmdListHelper() = default; 38 ~CmdListHelper() = default; 39 40 static OpDataHandle AddImageToCmdList(CmdList& cmdList, const Image& image); 41 static OpDataHandle AddImageToCmdList(CmdList& cmdList, const std::shared_ptr<Image>& image); 42 static std::shared_ptr<Image> GetImageFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle); 43 static OpDataHandle AddVerticesToCmdList(CmdList& cmdList, const Vertices& vertices); 44 static std::shared_ptr<Vertices> GetVerticesFromCmdList(const CmdList& cmdList, 45 const OpDataHandle& opDataHandle); 46 static ImageHandle AddBitmapToCmdList(CmdList& cmdList, const Bitmap& bitmap); 47 static std::shared_ptr<Bitmap> GetBitmapFromCmdList(const CmdList& cmdList, const ImageHandle& bitmapHandle); 48 static OpDataHandle DRAWING_API AddRecordCmdToCmdList( 49 CmdList& cmdList, const std::shared_ptr<RecordCmd>& recordCmd); 50 static std::shared_ptr<RecordCmd> GetRecordCmdFromCmdList( 51 const CmdList& cmdList, const OpDataHandle& recordCmdHandle); 52 static OpDataHandle DRAWING_API AddImageObjectToCmdList( 53 CmdList& cmdList, const std::shared_ptr<ExtendImageObject>& object); 54 static std::shared_ptr<ExtendImageObject> GetImageObjectFromCmdList( 55 const CmdList& cmdList, const OpDataHandle& objectHandle); 56 static OpDataHandle DRAWING_API AddImageBaseObjToCmdList( 57 CmdList& cmdList, const std::shared_ptr<ExtendImageBaseObj>& object); 58 static std::shared_ptr<ExtendImageBaseObj> GetImageBaseObjFromCmdList( 59 const CmdList& cmdList, const OpDataHandle& objectHandle); 60 static OpDataHandle DRAWING_API AddImageNineObjecToCmdList( 61 CmdList& cmdList, const std::shared_ptr<ExtendImageNineObject>& object); 62 static std::shared_ptr<ExtendImageNineObject> GetImageNineObjecFromCmdList( 63 const CmdList& cmdList, const OpDataHandle& objectHandle); 64 static OpDataHandle DRAWING_API AddImageLatticeObjecToCmdList( 65 CmdList& cmdList, const std::shared_ptr<ExtendImageLatticeObject>& object); 66 static std::shared_ptr<ExtendImageLatticeObject> GetImageLatticeObjecFromCmdList( 67 const CmdList& cmdList, const OpDataHandle& objectHandle); 68 static OpDataHandle AddPictureToCmdList(CmdList& cmdList, const Picture& picture); 69 static std::shared_ptr<Picture> GetPictureFromCmdList(const CmdList& cmdList, const OpDataHandle& pictureHandle); 70 static OpDataHandle AddCompressDataToCmdList(CmdList& cmdList, const std::shared_ptr<Data>& data); 71 static std::shared_ptr<Data> GetCompressDataFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle); 72 73 static uint32_t DRAWING_API AddDrawFuncObjToCmdList( 74 CmdList& cmdList, const std::shared_ptr<ExtendDrawFuncObj>& object); 75 static std::shared_ptr<ExtendDrawFuncObj> GetDrawFuncObjFromCmdList( 76 const CmdList& cmdList, uint32_t objectHandle); 77 78 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const CommonType & recorded)79 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const CommonType& recorded) 80 { 81 if (recorded.GetDrawingType() != DrawingType::RECORDING) { 82 return { 0 }; 83 } 84 85 auto recording = static_cast<const RecordingType&>(recorded); 86 if (recording.GetCmdList() == nullptr) { 87 return { 0 }; 88 } 89 90 return AddChildToCmdList(cmdList, recording.GetCmdList()); 91 } 92 93 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const std::shared_ptr<CommonType> & recorded)94 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const std::shared_ptr<CommonType>& recorded) 95 { 96 if (recorded == nullptr) { 97 return { 0 }; 98 } 99 if (recorded->GetDrawingType() != DrawingType::RECORDING) { 100 return { 0 }; 101 } 102 103 auto recording = std::static_pointer_cast<RecordingType>(recorded); 104 if (recording->GetCmdList() == nullptr) { 105 return { 0 }; 106 } 107 108 return AddChildToCmdList(cmdList, recording->GetCmdList()); 109 } 110 111 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const CommonType * recorded)112 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const CommonType* recorded) 113 { 114 if (recorded == nullptr) { 115 return { 0 }; 116 } 117 if (recorded->GetDrawingType() != DrawingType::RECORDING) { 118 return { 0 }; 119 } 120 121 auto recording = static_cast<const RecordingType*>(recorded); 122 if (recording->GetCmdList() == nullptr) { 123 return { 0 }; 124 } 125 126 return AddChildToCmdList(cmdList, recording->GetCmdList()); 127 } 128 129 template<typename CmdListType, typename Type> GetFromCmdList(const CmdList & cmdList,const CmdListHandle & handler)130 static std::shared_ptr<Type> GetFromCmdList(const CmdList& cmdList, const CmdListHandle& handler) 131 { 132 auto childCmdList = GetChildFromCmdList<CmdListType>(cmdList, handler); 133 if (childCmdList == nullptr) { 134 return nullptr; 135 } 136 137 return childCmdList->Playback(); 138 } 139 140 template<typename Type> AddVectorToCmdList(CmdList & cmdList,const std::vector<Type> & vec)141 static std::pair<size_t, size_t> AddVectorToCmdList(CmdList& cmdList, const std::vector<Type>& vec) 142 { 143 std::pair<size_t, size_t> ret(0, 0); 144 if (!vec.empty()) { 145 const void* data = static_cast<const void*>(vec.data()); 146 size_t size = vec.size() * sizeof(Type); 147 auto offset = cmdList.AddCmdListData(std::make_pair(data, size)); 148 ret = { offset, size }; 149 } 150 151 return ret; 152 } 153 154 template<typename Type> GetVectorFromCmdList(const CmdList & cmdList,std::pair<size_t,size_t> info)155 static std::vector<Type> GetVectorFromCmdList(const CmdList& cmdList, std::pair<size_t, size_t> info) 156 { 157 std::vector<Type> ret; 158 const auto* values = static_cast<const Type*>(cmdList.GetCmdListData(info.first, info.second)); 159 auto size = info.second / sizeof(Type); 160 if (size > MAX_VECTORSIZE) { 161 LOGE("GetVectorFromCmdList size is too large, an empty vector is returned!"); 162 return ret; 163 } 164 if (values != nullptr && size > 0) { 165 for (size_t i = 0; i < size; i++) { 166 ret.push_back(*values); 167 values++; 168 } 169 } 170 return ret; 171 } 172 173 static CmdListHandle AddChildToCmdList(CmdList& cmdList, const std::shared_ptr<CmdList>& child); 174 175 template<typename CmdListType> GetChildFromCmdList(const CmdList & cmdList,const CmdListHandle & childHandle)176 static std::shared_ptr<CmdListType> GetChildFromCmdList(const CmdList& cmdList, const CmdListHandle& childHandle) 177 { 178 if (childHandle.size == 0) { 179 return std::make_shared<CmdListType>(); 180 } 181 182 const void* childData = cmdList.GetCmdListData(childHandle.offset, childHandle.size); 183 if (childData == nullptr) { 184 return nullptr; 185 } 186 187 auto childCmdList = CmdListType::CreateFromData({ childData, childHandle.size }); 188 if (childCmdList == nullptr) { 189 return nullptr; 190 } 191 192 const void* childImageData = cmdList.GetImageData(childHandle.imageOffset, childHandle.imageSize); 193 if (childHandle.imageSize > 0 && childImageData != nullptr) { 194 if (!childCmdList->SetUpImageData(childImageData, childHandle.imageSize)) { 195 LOGD("set up child image data failed!"); 196 } 197 } 198 199 return childCmdList; 200 } 201 202 static OpDataHandle AddTextBlobToCmdList(CmdList& cmdList, const TextBlob* textBlob, void* ctx = nullptr); 203 static std::shared_ptr<TextBlob> GetTextBlobFromCmdList(const CmdList& cmdList, 204 const OpDataHandle& textBlobHandle, uint64_t globalUniqueId = 0); 205 206 static OpDataHandle AddDataToCmdList(CmdList& cmdList, const Data* data); 207 static std::shared_ptr<Data> GetDataFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle); 208 209 static OpDataHandle AddPathToCmdList(CmdList& cmdList, const Path& path); 210 static std::shared_ptr<Path> GetPathFromCmdList(const CmdList& cmdList, const OpDataHandle& pathHandle); 211 212 static OpDataHandle AddRegionToCmdList(CmdList& cmdList, const Region& region); 213 static std::shared_ptr<Region> GetRegionFromCmdList(const CmdList& cmdList, const OpDataHandle& regionHandle); 214 215 static OpDataHandle AddColorSpaceToCmdList(CmdList& cmdList, const std::shared_ptr<ColorSpace> colorSpace); 216 static std::shared_ptr<ColorSpace> GetColorSpaceFromCmdList(const CmdList& cmdList, 217 const OpDataHandle& imageHandle); 218 219 static FlattenableHandle AddShaderEffectToCmdList(CmdList& cmdList, std::shared_ptr<ShaderEffect> shaderEffect); 220 static std::shared_ptr<ShaderEffect> GetShaderEffectFromCmdList(const CmdList& cmdList, 221 const FlattenableHandle& shaderEffectHandle); 222 223 static FlattenableHandle AddBlenderToCmdList(CmdList& cmdList, std::shared_ptr<Blender> pathEffect); 224 static std::shared_ptr<Blender> GetBlenderFromCmdList(const CmdList& cmdList, 225 const FlattenableHandle& blenderHandle); 226 227 static FlattenableHandle AddPathEffectToCmdList(CmdList& cmdList, std::shared_ptr<PathEffect> pathEffect); 228 static std::shared_ptr<PathEffect> GetPathEffectFromCmdList(const CmdList& cmdList, 229 const FlattenableHandle& pathEffectHandle); 230 231 static FlattenableHandle AddMaskFilterToCmdList(CmdList& cmdList, std::shared_ptr<MaskFilter> maskFilter); 232 static std::shared_ptr<MaskFilter> GetMaskFilterFromCmdList(const CmdList& cmdList, 233 const FlattenableHandle& maskFilterHandle); 234 235 static FlattenableHandle AddColorFilterToCmdList(CmdList& cmdList, std::shared_ptr<ColorFilter> colorFilter); 236 static std::shared_ptr<ColorFilter> GetColorFilterFromCmdList(const CmdList& cmdList, 237 const FlattenableHandle& colorFilterHandle); 238 239 static FlattenableHandle AddImageFilterToCmdList(CmdList& cmdList, const ImageFilter* imageFilter); 240 static FlattenableHandle AddImageFilterToCmdList(CmdList& cmdList, std::shared_ptr<ImageFilter> imageFilter); 241 static std::shared_ptr<ImageFilter> GetImageFilterFromCmdList(const CmdList& cmdList, 242 const FlattenableHandle& imageFilterHandle); 243 244 static OpDataHandle AddBlurDrawLooperToCmdList(CmdList& cmdList, std::shared_ptr<BlurDrawLooper> blurDrawLooper); 245 static std::shared_ptr<BlurDrawLooper> GetBlurDrawLooperFromCmdList(const CmdList& cmdList, 246 const OpDataHandle& blurDrawLooperHandle); 247 248 static LatticeHandle AddLatticeToCmdList(CmdList& cmdList, const Lattice& lattice); 249 static Lattice GetLatticeFromCmdList(const CmdList& cmdList, const LatticeHandle& latticeHandle); 250 251 static SymbolOpHandle AddSymbolToCmdList(CmdList& cmdList, const DrawingHMSymbolData& symbol); 252 static DrawingHMSymbolData GetSymbolFromCmdList(const CmdList& cmdList, const SymbolOpHandle& symbolHandle); 253 254 static SymbolLayersHandle AddSymbolLayersToCmdList(CmdList& cmdList, const DrawingSymbolLayers& symbolLayers); 255 static DrawingSymbolLayers GetSymbolLayersFromCmdList(const CmdList& cmdList, 256 const SymbolLayersHandle& symbolLayersHandle); 257 258 static RenderGroupHandle AddRenderGroupToCmdList(CmdList& cmdList, const DrawingRenderGroup& group); 259 static DrawingRenderGroup GetRenderGroupFromCmdList(const CmdList& cmdList, 260 const RenderGroupHandle& renderGroupHandle); 261 262 static GroupInfoHandle AddGroupInfoToCmdList(CmdList& cmdList, const DrawingGroupInfo& groupInfo); 263 static DrawingGroupInfo GetGroupInfoFromCmdList(const CmdList& cmdList, const GroupInfoHandle& groupInfoHandle); 264 #ifdef ROSEN_OHOS 265 static uint32_t AddSurfaceBufferEntryToCmdList( 266 CmdList& cmdList, const std::shared_ptr<SurfaceBufferEntry>& imageFilter); 267 static std::shared_ptr<SurfaceBufferEntry> GetSurfaceBufferEntryFromCmdList( 268 const CmdList& cmdList, uint32_t imageFilterHandle); 269 #endif 270 static uint32_t AddExtendObjectToCmdList(CmdList& cmdList, std::shared_ptr<ExtendObject>); 271 static std::shared_ptr<ExtendObject> GetExtendObjectFromCmdList(const CmdList& cmdList, uint32_t index); 272 273 static uint32_t AddDrawingObjectToCmdList(CmdList& cmdList, std::shared_ptr<Object>); 274 static std::shared_ptr<Object> GetDrawingObjectFromCmdList(const CmdList& cmdList, uint32_t index); 275 }; 276 } // namespace Drawing 277 } // namespace Rosen 278 } // namespace OHOS 279 #endif