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_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 "text/hm_symbol.h" 25 #include "text/text_blob.h" 26 #include "utils/log.h" 27 #include "utils/vertices.h" 28 29 namespace OHOS { 30 namespace Media { 31 class PixelMap; 32 } 33 namespace Rosen { 34 namespace Drawing { 35 class DrawOpItem; 36 class DRAWING_API CmdListHelper { 37 public: 38 CmdListHelper() = default; 39 ~CmdListHelper() = default; 40 41 static OpDataHandle AddImageToCmdList(CmdList& cmdList, const Image& image); 42 static OpDataHandle AddImageToCmdList(CmdList& cmdList, const std::shared_ptr<Image>& image); 43 static std::shared_ptr<Image> GetImageFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle); 44 static OpDataHandle AddVerticesToCmdList(CmdList& cmdList, const Vertices& vertices); 45 static std::shared_ptr<Vertices> GetVerticesFromCmdList(const CmdList& cmdList, 46 const OpDataHandle& opDataHandle); 47 static ImageHandle AddBitmapToCmdList(CmdList& cmdList, const Bitmap& bitmap); 48 static std::shared_ptr<Bitmap> GetBitmapFromCmdList(const CmdList& cmdList, const ImageHandle& bitmapHandle); 49 static OpDataHandle AddPixelMapToCmdList(CmdList& cmdList, const std::shared_ptr<Media::PixelMap>& pixelMap); 50 static std::shared_ptr<Media::PixelMap> GetPixelMapFromCmdList( 51 const CmdList& cmdList, const OpDataHandle& pixelMapHandle); 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 AddPictureToCmdList(CmdList& cmdList, const Picture& picture); 61 static std::shared_ptr<Picture> GetPictureFromCmdList(const CmdList& cmdList, const OpDataHandle& pictureHandle); 62 static OpDataHandle AddCompressDataToCmdList(CmdList& cmdList, const std::shared_ptr<Data>& data); 63 static std::shared_ptr<Data> GetCompressDataFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle); 64 65 static OpDataHandle DRAWING_API AddDrawFuncObjToCmdList( 66 CmdList& cmdList, const std::shared_ptr<ExtendDrawFuncObj>& object); 67 static std::shared_ptr<ExtendDrawFuncObj> GetDrawFuncObjFromCmdList( 68 const CmdList& cmdList, const OpDataHandle& objectHandle); 69 70 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const CommonType & recorded)71 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const CommonType& recorded) 72 { 73 if (recorded.GetDrawingType() != DrawingType::RECORDING) { 74 return { 0 }; 75 } 76 77 auto recording = static_cast<const RecordingType&>(recorded); 78 if (recording.GetCmdList() == nullptr) { 79 return { 0 }; 80 } 81 82 return AddChildToCmdList(cmdList, recording.GetCmdList()); 83 } 84 85 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const std::shared_ptr<CommonType> & recorded)86 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const std::shared_ptr<CommonType>& recorded) 87 { 88 if (recorded == nullptr) { 89 return { 0 }; 90 } 91 if (recorded->GetDrawingType() != DrawingType::RECORDING) { 92 return { 0 }; 93 } 94 95 auto recording = std::static_pointer_cast<RecordingType>(recorded); 96 if (recording->GetCmdList() == nullptr) { 97 return { 0 }; 98 } 99 100 return AddChildToCmdList(cmdList, recording->GetCmdList()); 101 } 102 103 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const CommonType * recorded)104 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const CommonType* recorded) 105 { 106 if (recorded == nullptr) { 107 return { 0 }; 108 } 109 if (recorded->GetDrawingType() != DrawingType::RECORDING) { 110 return { 0 }; 111 } 112 113 auto recording = static_cast<const RecordingType*>(recorded); 114 if (recording->GetCmdList() == nullptr) { 115 return { 0 }; 116 } 117 118 return AddChildToCmdList(cmdList, recording->GetCmdList()); 119 } 120 121 template<typename CmdListType, typename Type> GetFromCmdList(const CmdList & cmdList,const CmdListHandle & handler)122 static std::shared_ptr<Type> GetFromCmdList(const CmdList& cmdList, const CmdListHandle& handler) 123 { 124 auto childCmdList = GetChildFromCmdList<CmdListType>(cmdList, handler); 125 if (childCmdList == nullptr) { 126 return nullptr; 127 } 128 129 return childCmdList->Playback(); 130 } 131 132 template<typename Type> AddVectorToCmdList(CmdList & cmdList,const std::vector<Type> & vec)133 static std::pair<uint32_t, size_t> AddVectorToCmdList(CmdList& cmdList, const std::vector<Type>& vec) 134 { 135 std::pair<uint32_t, size_t> ret(0, 0); 136 if (!vec.empty()) { 137 const void* data = static_cast<const void*>(vec.data()); 138 size_t size = vec.size() * sizeof(Type); 139 auto offset = cmdList.AddCmdListData(std::make_pair(data, size)); 140 ret = { offset, size }; 141 } 142 143 return ret; 144 } 145 146 template<typename Type> GetVectorFromCmdList(const CmdList & cmdList,std::pair<uint32_t,size_t> info)147 static std::vector<Type> GetVectorFromCmdList(const CmdList& cmdList, std::pair<uint32_t, size_t> info) 148 { 149 std::vector<Type> ret; 150 const auto* values = static_cast<const Type*>(cmdList.GetCmdListData(info.first)); 151 auto size = info.second / sizeof(Type); 152 if (values != nullptr && size > 0) { 153 for (size_t i = 0; i < size; i++) { 154 ret.push_back(*values); 155 values++; 156 } 157 } 158 return ret; 159 } 160 161 static CmdListHandle AddChildToCmdList(CmdList& cmdList, const std::shared_ptr<CmdList>& child); 162 163 template<typename CmdListType> GetChildFromCmdList(const CmdList & cmdList,const CmdListHandle & childHandle)164 static std::shared_ptr<CmdListType> GetChildFromCmdList(const CmdList& cmdList, const CmdListHandle& childHandle) 165 { 166 if (childHandle.size == 0) { 167 return std::make_shared<CmdListType>(); 168 } 169 170 const void* childData = cmdList.GetCmdListData(childHandle.offset); 171 if (childData == nullptr) { 172 return nullptr; 173 } 174 175 auto childCmdList = CmdListType::CreateFromData({ childData, childHandle.size }); 176 if (childCmdList == nullptr) { 177 return nullptr; 178 } 179 180 if (childHandle.imageSize > 0 && cmdList.GetImageData(childHandle.imageOffset) != nullptr) { 181 if (!childCmdList->SetUpImageData(cmdList.GetImageData(childHandle.imageOffset), childHandle.imageSize)) { 182 LOGD("set up child image data failed!"); 183 } 184 } 185 186 return childCmdList; 187 } 188 189 static OpDataHandle AddTextBlobToCmdList(CmdList& cmdList, const TextBlob* textBlob); 190 static std::shared_ptr<TextBlob> GetTextBlobFromCmdList(const CmdList& cmdList, const OpDataHandle& textBlobHandle); 191 192 static OpDataHandle AddDataToCmdList(CmdList& cmdList, const Data* data); 193 static std::shared_ptr<Data> GetDataFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle); 194 195 static OpDataHandle AddPathToCmdList(CmdList& cmdList, const Path& path); 196 static std::shared_ptr<Path> GetPathFromCmdList(const CmdList& cmdList, const OpDataHandle& pathHandle); 197 198 static OpDataHandle AddRegionToCmdList(CmdList& cmdList, const Region& region); 199 static std::shared_ptr<Region> GetRegionFromCmdList(const CmdList& cmdList, const OpDataHandle& regionHandle); 200 201 static OpDataHandle AddColorSpaceToCmdList(CmdList& cmdList, const std::shared_ptr<ColorSpace> colorSpace); 202 static std::shared_ptr<ColorSpace> GetColorSpaceFromCmdList(const CmdList& cmdList, 203 const OpDataHandle& imageHandle); 204 205 static FlattenableHandle AddShaderEffectToCmdList(CmdList& cmdList, std::shared_ptr<ShaderEffect> shaderEffect); 206 static std::shared_ptr<ShaderEffect> GetShaderEffectFromCmdList(const CmdList& cmdList, 207 const FlattenableHandle& shaderEffectHandle); 208 209 static FlattenableHandle AddPathEffectToCmdList(CmdList& cmdList, std::shared_ptr<PathEffect> pathEffect); 210 static std::shared_ptr<PathEffect> GetPathEffectFromCmdList(const CmdList& cmdList, 211 const FlattenableHandle& pathEffectHandle); 212 213 static FlattenableHandle AddMaskFilterToCmdList(CmdList& cmdList, std::shared_ptr<MaskFilter> maskFilter); 214 static std::shared_ptr<MaskFilter> GetMaskFilterFromCmdList(const CmdList& cmdList, 215 const FlattenableHandle& maskFilterHandle); 216 217 static FlattenableHandle AddColorFilterToCmdList(CmdList& cmdList, std::shared_ptr<ColorFilter> colorFilter); 218 static std::shared_ptr<ColorFilter> GetColorFilterFromCmdList(const CmdList& cmdList, 219 const FlattenableHandle& colorFilterHandle); 220 221 static FlattenableHandle AddImageFilterToCmdList(CmdList& cmdList, const ImageFilter* imageFilter); 222 static FlattenableHandle AddImageFilterToCmdList(CmdList& cmdList, std::shared_ptr<ImageFilter> imageFilter); 223 static std::shared_ptr<ImageFilter> GetImageFilterFromCmdList(const CmdList& cmdList, 224 const FlattenableHandle& imageFilterHandle); 225 226 static SymbolOpHandle AddSymbolToCmdList(CmdList& cmdList, const DrawingHMSymbolData& symbol); 227 static DrawingHMSymbolData GetSymbolFromCmdList(const CmdList& cmdList, const SymbolOpHandle& symbolHandle); 228 229 static SymbolLayersHandle AddSymbolLayersToCmdList(CmdList& cmdList, const DrawingSymbolLayers& symbolLayers); 230 static DrawingSymbolLayers GetSymbolLayersFromCmdList(const CmdList& cmdList, 231 const SymbolLayersHandle& symbolLayersHandle); 232 233 static RenderGroupHandle AddRenderGroupToCmdList(CmdList& cmdList, const DrawingRenderGroup& group); 234 static DrawingRenderGroup GetRenderGroupFromCmdList(const CmdList& cmdList, 235 const RenderGroupHandle& renderGroupHandle); 236 237 static GroupInfoHandle AddGroupInfoToCmdList(CmdList& cmdList, const DrawingGroupInfo& groupInfo); 238 static DrawingGroupInfo GetGroupInfoFromCmdList(const CmdList& cmdList, const GroupInfoHandle& groupInfoHandle); 239 #ifdef ROSEN_OHOS 240 static uint32_t AddSurfaceBufferToCmdList(CmdList& cmdList, const sptr<SurfaceBuffer>& imageFilter); 241 static sptr<SurfaceBuffer> GetSurfaceBufferFromCmdList( 242 const CmdList& cmdList, uint32_t imageFilterHandle); 243 #endif 244 }; 245 } // namespace Drawing 246 } // namespace Rosen 247 } // namespace OHOS 248 #endif