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