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 #include "recording/cmd_list_helper.h"
17
18 #ifdef SUPPORT_OHOS_PIXMAP
19 #include "pixel_map.h"
20 #endif
21 #include "recording/color_filter_cmd_list.h"
22 #include "recording/color_space_cmd_list.h"
23 #include "recording/draw_cmd_list.h"
24 #include "recording/image_filter_cmd_list.h"
25 #include "recording/mask_filter_cmd_list.h"
26 #include "recording/path_cmd_list.h"
27 #include "recording/path_effect_cmd_list.h"
28 #include "recording/region_cmd_list.h"
29 #include "recording/shader_effect_cmd_list.h"
30 #include "utils/log.h"
31
32 #include "skia_adapter/skia_picture.h"
33
34 namespace OHOS {
35 namespace Rosen {
36 namespace Drawing {
ColorTypeToBytesPerPixel(ColorType colorType)37 static int ColorTypeToBytesPerPixel(ColorType colorType)
38 {
39 // returns the number of bytes per pixel: 1byte, 2bytes, 4bytes
40 switch (colorType) {
41 case ColorType::COLORTYPE_ALPHA_8:
42 return 1;
43 case ColorType::COLORTYPE_RGB_565:
44 case ColorType::COLORTYPE_ARGB_4444:
45 return 2;
46 case ColorType::COLORTYPE_RGBA_8888:
47 case ColorType::COLORTYPE_BGRA_8888:
48 case ColorType::COLORTYPE_N32:
49 return 4;
50 case ColorType::COLORTYPE_UNKNOWN:
51 default:
52 return 0;
53 }
54 }
55
AddImageToCmdList(CmdList & cmdList,const Image & image)56 ImageHandle CmdListHelper::AddImageToCmdList(CmdList& cmdList, const Image& image)
57 {
58 auto data = image.Serialize();
59 if (data == nullptr || data->GetSize() == 0) {
60 LOGE("image is valid!");
61 return { 0 };
62 }
63
64 auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
65 return { offset, data->GetSize() };
66 }
67
AddImageToCmdList(CmdList & cmdList,const std::shared_ptr<Image> & image)68 ImageHandle CmdListHelper::AddImageToCmdList(CmdList& cmdList, const std::shared_ptr<Image>& image)
69 {
70 if (image == nullptr) {
71 LOGE("image is nullptr!");
72 return { 0 };
73 }
74 return CmdListHelper::AddImageToCmdList(cmdList, *image);
75 }
76
GetImageFromCmdList(const CmdList & cmdList,const ImageHandle & imageHandle)77 std::shared_ptr<Image> CmdListHelper::GetImageFromCmdList(const CmdList& cmdList, const ImageHandle& imageHandle)
78 {
79 const void* ptr = cmdList.GetImageData(imageHandle.offset);
80 if (imageHandle.size == 0 || ptr == nullptr) {
81 LOGE("get image data failed!");
82 return nullptr;
83 }
84
85 auto imageData = std::make_shared<Data>();
86 imageData->BuildWithoutCopy(ptr, imageHandle.size);
87 auto image = std::make_shared<Image>();
88 if (image->Deserialize(imageData) == false) {
89 LOGE("image deserialize failed!");
90 return nullptr;
91 }
92 return image;
93 }
94
AddBitmapToCmdList(CmdList & cmdList,const Bitmap & bitmap)95 ImageHandle CmdListHelper::AddBitmapToCmdList(CmdList& cmdList, const Bitmap& bitmap)
96 {
97 auto format = bitmap.GetFormat();
98 auto bpp = ColorTypeToBytesPerPixel(format.colorType);
99 auto bitmapSize = bitmap.GetHeight() * bitmap.GetWidth() * bpp;
100 if (bitmapSize == 0) {
101 LOGE("bitmap is valid!");
102 return { 0 };
103 }
104
105 auto offset = cmdList.AddImageData(bitmap.GetPixels(), bitmapSize);
106 return { offset, bitmapSize, bitmap.GetWidth(), bitmap.GetHeight(), format.colorType, format.alphaType };
107 }
108
GetBitmapFromCmdList(const CmdList & cmdList,const ImageHandle & bitmapHandle)109 std::shared_ptr<Bitmap> CmdListHelper::GetBitmapFromCmdList(const CmdList& cmdList, const ImageHandle& bitmapHandle)
110 {
111 const void* ptr = cmdList.GetImageData(bitmapHandle.offset);
112 if (bitmapHandle.size == 0 || ptr == nullptr) {
113 LOGE("get bitmap data failed!");
114 return nullptr;
115 }
116
117 BitmapFormat format = { bitmapHandle.colorType, bitmapHandle.alphaType };
118 auto bitmap = std::make_shared<Bitmap>();
119 bitmap->Build(bitmapHandle.width, bitmapHandle.height, format);
120 bitmap->SetPixels(const_cast<void*>(cmdList.GetImageData(bitmapHandle.offset)));
121
122 return bitmap;
123 }
124
AddPixelMapToCmdList(CmdList & cmdList,const std::shared_ptr<Media::PixelMap> & pixelMap)125 ImageHandle CmdListHelper::AddPixelMapToCmdList(CmdList& cmdList, const std::shared_ptr<Media::PixelMap>& pixelMap)
126 {
127 #ifdef SUPPORT_OHOS_PIXMAP
128 auto index = cmdList.AddPixelMap(pixelMap);
129 return { index };
130 #else
131 LOGE("Not support drawing Media::PixelMap");
132 return { 0 };
133 #endif
134 }
135
GetPixelMapFromCmdList(const CmdList & cmdList,const ImageHandle & pixelMapHandle)136 std::shared_ptr<Media::PixelMap> CmdListHelper::GetPixelMapFromCmdList(
137 const CmdList& cmdList, const ImageHandle& pixelMapHandle)
138 {
139 #ifdef SUPPORT_OHOS_PIXMAP
140 return (const_cast<CmdList&>(cmdList)).GetPixelMap(pixelMapHandle.offset);
141 #else
142 LOGE("Not support drawing Media::PixelMap");
143 return nullptr;
144 #endif
145 }
146
AddPictureToCmdList(CmdList & cmdList,const Picture & picture)147 ImageHandle CmdListHelper::AddPictureToCmdList(CmdList& cmdList, const Picture& picture)
148 {
149 auto data = picture.GetImpl<SkiaPicture>()->Serialize();
150 if (data == nullptr || data->GetSize() == 0) {
151 LOGE("picture is valid!");
152 return { 0 };
153 }
154
155 auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
156 return { offset, data->GetSize() };
157 }
158
GetPictureFromCmdList(const CmdList & cmdList,const ImageHandle & pictureHandle)159 std::shared_ptr<Picture> CmdListHelper::GetPictureFromCmdList(const CmdList& cmdList, const ImageHandle& pictureHandle)
160 {
161 const void* ptr = cmdList.GetImageData(pictureHandle.offset);
162 if (ptr == nullptr) {
163 LOGE("get picture data failed!");
164 return nullptr;
165 }
166
167 auto pictureData = std::make_shared<Data>();
168 pictureData->BuildWithoutCopy(ptr, pictureHandle.size);
169 auto picture = std::make_shared<Picture>();
170 if (picture->GetImpl<SkiaPicture>()->Deserialize(pictureData) == false) {
171 LOGE("picture deserialize failed!");
172 return nullptr;
173 }
174 return picture;
175 }
176
AddCompressDataToCmdList(CmdList & cmdList,const std::shared_ptr<Data> & data)177 ImageHandle CmdListHelper::AddCompressDataToCmdList(CmdList& cmdList, const std::shared_ptr<Data>& data)
178 {
179 if (data == nullptr || data->GetSize() == 0) {
180 LOGE("data is valid!");
181 return { 0 };
182 }
183
184 auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
185 return { offset, data->GetSize() };
186 }
187
GetCompressDataFromCmdList(const CmdList & cmdList,const ImageHandle & imageHandle)188 std::shared_ptr<Data> CmdListHelper::GetCompressDataFromCmdList(const CmdList& cmdList, const ImageHandle& imageHandle)
189 {
190 const void* ptr = cmdList.GetImageData(imageHandle.offset);
191 if (imageHandle.size == 0 || ptr == nullptr) {
192 LOGE("get image data failed!");
193 return nullptr;
194 }
195
196 auto imageData = std::make_shared<Data>();
197 imageData->BuildWithoutCopy(ptr, imageHandle.size);
198 return imageData;
199 }
200
AddChildToCmdList(CmdList & cmdList,const std::shared_ptr<CmdList> & child)201 CmdListHandle CmdListHelper::AddChildToCmdList(CmdList& cmdList, const std::shared_ptr<CmdList>& child)
202 {
203 CmdListHandle childHandle = { 0 };
204
205 if (child == nullptr) {
206 LOGE("child is invalid!");
207 return childHandle;
208 }
209
210 childHandle.type = child->GetType();
211
212 auto childData = child->GetData();
213 if (childData.first != nullptr && childData.second != 0) {
214 childHandle.offset = cmdList.AddCmdListData(childData);
215 childHandle.size = childData.second;
216 } else {
217 return childHandle;
218 }
219
220 auto childImageData = child->GetAllImageData();
221 if (childImageData.first != nullptr && childImageData.second != 0) {
222 childHandle.imageOffset = cmdList.AddImageData(childImageData.first, childImageData.second);
223 childHandle.imageSize = childImageData.second;
224 }
225
226 return childHandle;
227 }
228 } // namespace Drawing
229 } // namespace Rosen
230 } // namespace OHOS
231