1 /*
2 * Copyright (c) 2024 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 <cstddef>
17 #include "gtest/gtest.h"
18 #include "draw/surface.h"
19 #include "effect/color_space.h"
20 #include "image/bitmap.h"
21 #include "image/image_info.h"
22 #include "include/core/SkBitmap.h"
23 #include "include/core/SkImageInfo.h"
24 #include "skia_adapter/skia_bitmap.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace Drawing {
32 class SkiaBitmapTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 };
39
SetUpTestCase()40 void SkiaBitmapTest::SetUpTestCase() {}
TearDownTestCase()41 void SkiaBitmapTest::TearDownTestCase() {}
SetUp()42 void SkiaBitmapTest::SetUp() {}
TearDown()43 void SkiaBitmapTest::TearDown() {}
44
45 /**
46 * @tc.name: Build001
47 * @tc.desc: Test Build
48 * @tc.type: FUNC
49 * @tc.require: I91F9L
50 */
51 HWTEST_F(SkiaBitmapTest, Build001, TestSize.Level1)
52 {
53 int width = 100;
54 int height = 50;
55 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888,
56 AlphaType::ALPHATYPE_PREMUL};
57 SkiaBitmap skiaBitmap;
58 ASSERT_TRUE(skiaBitmap.Build(width, height, bitmapFormat, 0));
59 }
60
61 /**
62 * @tc.name: Build002
63 * @tc.desc: Test Build
64 * @tc.type: FUNC
65 * @tc.require: I91F9L
66 */
67 HWTEST_F(SkiaBitmapTest, Build002, TestSize.Level1)
68 {
69 int width = 100;
70 int height = 50;
71 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888,
72 ALPHATYPE_PREMUL, nullptr};
73 SkiaBitmap skiaBitmap;
74 ASSERT_TRUE(skiaBitmap.Build(offscreenInfo, 0));
75 }
76
77 /**
78 * @tc.name: GetWidth001
79 * @tc.desc: Test GetWidth
80 * @tc.type: FUNC
81 * @tc.require: I91F9L
82 */
83 HWTEST_F(SkiaBitmapTest, GetWidth001, TestSize.Level1)
84 {
85 SkiaBitmap skiaBitmap;
86 ASSERT_TRUE(skiaBitmap.GetWidth() >= 0);
87 }
88
89 /**
90 * @tc.name: GetHeight001
91 * @tc.desc: Test GetHeight
92 * @tc.type: FUNC
93 * @tc.require: I91F9L
94 */
95 HWTEST_F(SkiaBitmapTest, GetHeight001, TestSize.Level1)
96 {
97 SkiaBitmap skiaBitmap;
98 ASSERT_TRUE(skiaBitmap.GetHeight() >= 0);
99 }
100
101 /**
102 * @tc.name: GetRowBytes001
103 * @tc.desc: Test GetRowBytes
104 * @tc.type: FUNC
105 * @tc.require: I91F9L
106 */
107 HWTEST_F(SkiaBitmapTest, GetRowBytes001, TestSize.Level1)
108 {
109 SkiaBitmap skiaBitmap;
110 ASSERT_TRUE(skiaBitmap.GetRowBytes() >= 0);
111 }
112
113 /**
114 * @tc.name: GetColorType001
115 * @tc.desc: Test GetColorType
116 * @tc.type: FUNC
117 * @tc.require: I91F9L
118 */
119 HWTEST_F(SkiaBitmapTest, GetColorType001, TestSize.Level1)
120 {
121 SkiaBitmap skiaBitmap;
122 ASSERT_TRUE(skiaBitmap.GetColorType() == ColorType::COLORTYPE_UNKNOWN);
123 }
124
125 /**
126 * @tc.name: GetAlphaType001
127 * @tc.desc: Test GetAlphaType
128 * @tc.type: FUNC
129 * @tc.require: I91F9L
130 */
131 HWTEST_F(SkiaBitmapTest, GetAlphaType001, TestSize.Level1)
132 {
133 SkiaBitmap skiaBitmap;
134 ASSERT_TRUE(skiaBitmap.GetAlphaType() == AlphaType::ALPHATYPE_UNKNOWN);
135 }
136
137 /**
138 * @tc.name: ExtractSubset001
139 * @tc.desc: Test ExtractSubset
140 * @tc.type: FUNC
141 * @tc.require: I91F9L
142 */
143 HWTEST_F(SkiaBitmapTest, ExtractSubset001, TestSize.Level1)
144 {
145 Bitmap left;
146 Rect subset = Rect(0, 0, 50, 50); // 50: right, bottom
147 SkiaBitmap skiaBitmap;
148 ASSERT_TRUE(!skiaBitmap.ExtractSubset(left, subset));
149 }
150
151 /**
152 * @tc.name: GetPixels001
153 * @tc.desc: Test GetPixels
154 * @tc.type: FUNC
155 * @tc.require: I91F9L
156 */
157 HWTEST_F(SkiaBitmapTest, GetPixels001, TestSize.Level1)
158 {
159 SkiaBitmap skiaBitmap;
160 ASSERT_TRUE(skiaBitmap.GetPixels() == nullptr);
161 }
162
163 /**
164 * @tc.name: PeekPixels001
165 * @tc.desc: Test PeekPixels
166 * @tc.type: FUNC
167 * @tc.require: I91F9L
168 */
169 HWTEST_F(SkiaBitmapTest, PeekPixels001, TestSize.Level1)
170 {
171 Pixmap pixmap;
172 SkiaBitmap skiaBitmap;
173 ASSERT_TRUE(!skiaBitmap.PeekPixels(pixmap));
174 }
175
176 /**
177 * @tc.name: ExportSkiaBitmap001
178 * @tc.desc: Test ExportSkiaBitmap
179 * @tc.type: FUNC
180 * @tc.require: I91F9L
181 */
182 HWTEST_F(SkiaBitmapTest, ExportSkiaBitmap001, TestSize.Level1)
183 {
184 SkiaBitmap skiaBitmap;
185 ASSERT_TRUE(skiaBitmap.ExportSkiaBitmap().empty());
186 }
187
188 /**
189 * @tc.name: IsImmutable001
190 * @tc.desc: Test IsImmutable
191 * @tc.type: FUNC
192 * @tc.require: I91F9L
193 */
194 HWTEST_F(SkiaBitmapTest, IsImmutable001, TestSize.Level1)
195 {
196 SkiaBitmap skiaBitmap;
197 ASSERT_TRUE(!skiaBitmap.IsImmutable());
198 }
199
200 /**
201 * @tc.name: SetImmutable001
202 * @tc.desc: Test SetImmutable
203 * @tc.type: FUNC
204 * @tc.require: I91F9L
205 */
206 HWTEST_F(SkiaBitmapTest, SetImmutable001, TestSize.Level1)
207 {
208 SkiaBitmap skiaBitmap;
209 skiaBitmap.SetImmutable();
210 ASSERT_TRUE(!skiaBitmap.IsImmutable());
211 }
212
213 /**
214 * @tc.name: GetColor001
215 * @tc.desc: Test GetColor
216 * @tc.type: FUNC
217 * @tc.require: I91F9L
218 */
219 HWTEST_F(SkiaBitmapTest, GetColor001, TestSize.Level1)
220 {
221 int width = 100;
222 int height = 50;
223 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888,
224 AlphaType::ALPHATYPE_PREMUL};
225 SkiaBitmap skiaBitmap;
226 skiaBitmap.Build(width, height, bitmapFormat, 0);
227 skiaBitmap.ClearWithColor(Color::COLOR_BLACK);
228 ASSERT_TRUE(skiaBitmap.GetColor(0, 0) == Color::COLOR_BLACK);
229 }
230
231 /**
232 * @tc.name: Free001
233 * @tc.desc: Test Free
234 * @tc.type: FUNC
235 * @tc.require: I91F9L
236 */
237 HWTEST_F(SkiaBitmapTest, Free001, TestSize.Level1)
238 {
239 SkiaBitmap skiaBitmap;
240 skiaBitmap.Free();
241 ASSERT_TRUE(skiaBitmap.IsValid());
242 }
243
244 /**
245 * @tc.name: IsValid001
246 * @tc.desc: Test IsValid
247 * @tc.type: FUNC
248 * @tc.require: I91F9L
249 */
250 HWTEST_F(SkiaBitmapTest, IsValid001, TestSize.Level1)
251 {
252 SkiaBitmap skiaBitmap;
253 ASSERT_TRUE(skiaBitmap.IsValid());
254 }
255
256 /**
257 * @tc.name: IsEmpty001
258 * @tc.desc: Test IsEmpty
259 * @tc.type: FUNC
260 * @tc.require: I91F9L
261 */
262 HWTEST_F(SkiaBitmapTest, IsEmpty001, TestSize.Level1)
263 {
264 SkiaBitmap skiaBitmap;
265 ASSERT_TRUE(skiaBitmap.IsEmpty());
266 }
267
268 /**
269 * @tc.name: GetPixmap001
270 * @tc.desc: Test GetPixmap
271 * @tc.type: FUNC
272 * @tc.require: I91F9L
273 */
274 HWTEST_F(SkiaBitmapTest, GetPixmap001, TestSize.Level1)
275 {
276 SkiaBitmap skiaBitmap;
277 ASSERT_TRUE(skiaBitmap.GetPixmap().GetWidth() == 0);
278 }
279
280 /**
281 * @tc.name: TryAllocPixels001
282 * @tc.desc: Test TryAllocPixels
283 * @tc.type: FUNC
284 * @tc.require: I91F9L
285 */
286 HWTEST_F(SkiaBitmapTest, TryAllocPixels001, TestSize.Level1)
287 {
288 int width = 100;
289 int height = 50;
290 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888,
291 ALPHATYPE_PREMUL, nullptr};
292 SkiaBitmap skiaBitmap;
293 skiaBitmap.TryAllocPixels(offscreenInfo);
294 ASSERT_TRUE(!skiaBitmap.IsValid());
295 }
296
297 /**
298 * @tc.name: Serialize001
299 * @tc.desc: Test Serialize
300 * @tc.type: FUNC
301 * @tc.require: I91F9L
302 */
303 HWTEST_F(SkiaBitmapTest, Serialize001, TestSize.Level1)
304 {
305 SkiaBitmap skiaBitmap;
306 skiaBitmap.Serialize();
307 skiaBitmap.Deserialize(nullptr);
308 skiaBitmap.Deserialize(std::make_shared<Data>());
309
310 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888,
311 AlphaType::ALPHATYPE_PREMUL};
312 skiaBitmap.Build(100, 50, bitmapFormat, 0); // 100:width, 50:height
313 skiaBitmap.ClearWithColor(0xFF000000);
314 ASSERT_TRUE(skiaBitmap.Serialize() != nullptr);
315 skiaBitmap.Deserialize(std::make_shared<Data>());
316 }
317
318 /**
319 * @tc.name: Build003
320 * @tc.desc: Test Build
321 * @tc.type: FUNC
322 * @tc.require: I91F9L
323 */
324 HWTEST_F(SkiaBitmapTest, Build003, TestSize.Level1)
325 {
326 const int width = -100;
327 const int height = -50;
328 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL};
329 SkiaBitmap skiaBitmap;
330 bool build = skiaBitmap.Build(width, height, bitmapFormat, 0);
331 ASSERT_FALSE(build);
332 }
333
334 /**
335 * @tc.name: Build004
336 * @tc.desc: Test Build
337 * @tc.type: FUNC
338 * @tc.require: I91F9L
339 */
340 HWTEST_F(SkiaBitmapTest, Build004, TestSize.Level1)
341 {
342 const int width = -100;
343 const int height = -50;
344 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, nullptr};
345 SkiaBitmap skiaBitmap;
346 bool build = skiaBitmap.Build(offscreenInfo, 0);
347 ASSERT_FALSE(build);
348 }
349
350 /**
351 * @tc.name: SetInfo
352 * @tc.desc: Test SetInfo
353 * @tc.type: FUNC
354 * @tc.require: I91F9L
355 */
356 HWTEST_F(SkiaBitmapTest, SetInfo, TestSize.Level1)
357 {
358 const int width = 100;
359 const int height = 50;
360 SkiaBitmap skiaBitmap;
361 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, nullptr};
362 skiaBitmap.SetInfo(offscreenInfo);
363 ASSERT_EQ(ColorType::COLORTYPE_RGBA_8888, skiaBitmap.GetColorType());
364 ASSERT_EQ(AlphaType::ALPHATYPE_PREMUL, skiaBitmap.GetAlphaType());
365 }
366
367 /**
368 * @tc.name: ComputeByteSize
369 * @tc.desc: Test ComputeByteSize
370 * @tc.type: FUNC
371 * @tc.require: I91F9L
372 */
373 HWTEST_F(SkiaBitmapTest, ComputeByteSize, TestSize.Level1)
374 {
375 const int width = 100;
376 const int height = 50;
377 SkiaBitmap skiaBitmap;
378 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL };
379 bool build = skiaBitmap.Build(width, height, bitmapFormat, 0);
380 ASSERT_TRUE(build);
381 size_t byteSize = skiaBitmap.ComputeByteSize();
382 ASSERT_TRUE(byteSize >= 0);
383 }
384
385 /**
386 * @tc.name: CopyPixels
387 * @tc.desc: Test CopyPixels
388 * @tc.type: FUNC
389 * @tc.require: I91F9L
390 */
391 HWTEST_F(SkiaBitmapTest, CopyPixels, TestSize.Level1)
392 {
393 const int srcWidth = 100;
394 const int srcHight = 50;
395 std::unique_ptr<Bitmap> dstBitmap = std::make_unique<Bitmap>();
396 ASSERT_TRUE(dstBitmap != nullptr);
397 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL };
398 SkiaBitmap skiaBitmap;
399 skiaBitmap.Build(srcWidth, srcHight, bitmapFormat, 0);
400 skiaBitmap.CopyPixels(*dstBitmap, 0, 0);
401 void* pixels = dstBitmap->GetPixels();
402 ASSERT_TRUE(pixels != skiaBitmap.GetPixels());
403 }
404
405 /**
406 * @tc.name: MakeImage
407 * @tc.desc: Test MakeImage
408 * @tc.type: FUNC
409 * @tc.require: I91F9L
410 */
411 HWTEST_F(SkiaBitmapTest, MakeImage, TestSize.Level1)
412 {
413 SkBitmap skBitmap;
414 SkiaBitmap skiaBitmap;
415 skiaBitmap.SetSkBitmap(skBitmap);
416 std::shared_ptr<Image> result = skiaBitmap.MakeImage();
417 ASSERT_TRUE(result == nullptr);
418 }
419
420 /**
421 * @tc.name: Serialize002
422 * @tc.desc: Test Serialize
423 * @tc.type: FUNC
424 * @tc.require: I91F9L
425 */
426 HWTEST_F(SkiaBitmapTest, Serialize002, TestSize.Level1)
427 {
428 const int width = 100;
429 const int height = 50;
430 SkiaBitmap skiaBitmap;
431 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
432 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, colorSpace};
433 bool build = skiaBitmap.Build(offscreenInfo, 0);
434 ASSERT_TRUE(build);
435 std::shared_ptr<Data> result = skiaBitmap.Serialize();
436 ASSERT_TRUE(result != nullptr);
437 }
438
439 /**
440 * @tc.name: Serialize003
441 * @tc.desc: Test Serialize
442 * @tc.type: FUNC
443 * @tc.require: I91F9L
444 */
445 HWTEST_F(SkiaBitmapTest, Serialize003, TestSize.Level1)
446 {
447 const int width = 100;
448 const int height = 50;
449 SkiaBitmap skiaBitmap;
450 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
451 ImageInfo offscreenInfo = { width, height, COLORTYPE_RGBA_8888, ALPHATYPE_PREMUL, colorSpace};
452 bool build = skiaBitmap.Build(offscreenInfo, 0);
453 ASSERT_TRUE(build);
454 std::shared_ptr<Data> result = skiaBitmap.Serialize();
455 ASSERT_TRUE(result->GetSize() == 20104);
456 }
457
458 /**
459 * @tc.name: Deserialize
460 * @tc.desc: Test Deserialize
461 * @tc.type: FUNC
462 * @tc.require: I91F9L
463 */
464 HWTEST_F(SkiaBitmapTest, Deserialize, TestSize.Level1)
465 {
466 const int width = 100;
467 const int height = 50;
468 SkiaBitmap skiaBitmap;
469 BitmapFormat bitmapFormat = { ColorType::COLORTYPE_BGRA_8888, AlphaType::ALPHATYPE_PREMUL};
470 skiaBitmap.Build(width, height, bitmapFormat, 0);
471 std::shared_ptr<Data> data = skiaBitmap.Serialize();
472 ASSERT_TRUE(data != nullptr);
473 bool ret = skiaBitmap.Deserialize(data);
474 ASSERT_TRUE(ret);
475 }
476 } // namespace Drawing
477 } // namespace Rosen
478 } // namespace OHOS