• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
18 #include "gtest/gtest.h"
19 #include "skia_adapter/skia_canvas.h"
20 #include "skia_adapter/skia_gpu_context.h"
21 #include "skia_adapter/skia_image.h"
22 #include "skia_adapter/skia_surface.h"
23 
24 #include "draw/surface.h"
25 #include "image/image.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace Drawing {
33 class SkiaImageTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp() override;
38     void TearDown() override;
39 };
40 
SetUpTestCase()41 void SkiaImageTest::SetUpTestCase() {}
TearDownTestCase()42 void SkiaImageTest::TearDownTestCase() {}
SetUp()43 void SkiaImageTest::SetUp() {}
TearDown()44 void SkiaImageTest::TearDown() {}
45 
46 /**
47  * @tc.name: BuildFromBitmap001
48  * @tc.desc:
49  * @tc.type: FUNC
50  * @tc.author:
51  */
52 HWTEST_F(SkiaImageTest, BuildFromBitmap001, TestSize.Level1)
53 {
54     Bitmap bitmap;
55     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
56     auto image = skiaImage->BuildFromBitmap(bitmap);
57     EXPECT_EQ(image, false);
58 }
59 
60 /**
61  * @tc.name: MakeRasterData001
62  * @tc.desc: Test MakeRasterData
63  * @tc.type: FUNC
64  * @tc.require: I91EH1
65  */
66 HWTEST_F(SkiaImageTest, MakeRasterData001, TestSize.Level1)
67 {
68     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
69     ImageInfo imageInfo;
70     auto image = skiaImage->MakeRasterData(imageInfo, nullptr, 0);
71     EXPECT_TRUE(image == nullptr);
72     std::shared_ptr<Data> data = std::make_shared<Data>();
73     data->BuildUninitialized(100);
74     auto image2 = skiaImage->MakeRasterData(imageInfo, data, 100);
75     EXPECT_TRUE(image2 == nullptr);
76 }
77 
78 #ifdef ACE_ENABLE_GPU
79 /**
80  * @tc.name: MakeFromEncoded001
81  * @tc.desc: Test MakeFromEncoded
82  * @tc.type: FUNC
83  * @tc.require: I91EH1
84  */
85 HWTEST_F(SkiaImageTest, MakeFromEncoded001, TestSize.Level1)
86 {
87     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
88     auto ret = skiaImage->MakeFromEncoded(nullptr);
89     EXPECT_TRUE(!ret);
90 }
91 
92 /**
93  * @tc.name: BuildSubset001
94  * @tc.desc: Test BuildSubset
95  * @tc.type: FUNC
96  * @tc.require: I91EH1
97  */
98 HWTEST_F(SkiaImageTest, BuildSubset001, TestSize.Level1)
99 {
100     auto surface = std::make_unique<SkiaSurface>();
101     surface->FlushAndSubmit(true);
102     Bitmap bitmap;
103     BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
104     bitmap.Build(100, 100, bitmapFormat);
105     ASSERT_TRUE(surface->Bind(bitmap));
106 
107     auto image = surface->GetImageSnapshot();
108     GPUContext context;
109 #ifdef NEW_SKIA
110     GrMockOptions options;
111     context.GetImpl<SkiaGPUContext>()->SetGrContext(GrDirectContext::MakeMock(&options));
112 #endif
113     auto skiaImage = image->GetImpl<SkiaImage>();
114     if (skiaImage) {
115         RectI rect;
116         GPUContext context;
117         skiaImage->BuildSubset(nullptr, rect, context);
118     }
119 }
120 
121 /**
122  * @tc.name: BuildSubset002
123  * @tc.desc: Test BuildSubset002
124  * @tc.type: FUNC
125  * @tc.require: I91EH1
126  */
127 HWTEST_F(SkiaImageTest, BuildSubset002, TestSize.Level1)
128 {
129     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
130     std::unique_ptr<Bitmap> bitmap = std::make_unique<Bitmap>();
131     ASSERT_TRUE(bitmap != nullptr);
132     EXPECT_TRUE(bitmap->Build(151, 150, { ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE }));
133     std::shared_ptr<Image> image = bitmap->MakeImage();
134     ASSERT_TRUE(image != nullptr);
135     RectI rect;
136     GPUContext context;
137     bool result = skiaImage->BuildSubset(image, rect, context);
138     ASSERT_FALSE(result);
139 }
140 
141 /**
142  * @tc.name: BuildFromTexture001
143  * @tc.desc: Test BuildFromTexture
144  * @tc.type: FUNC
145  * @tc.require: I91EH1
146  */
147 HWTEST_F(SkiaImageTest, BuildFromTexture001, TestSize.Level1)
148 {
149     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
150     GPUContext context;
151     TextureInfo textureInfo;
152     textureInfo.SetWidth(10);
153     BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
154     skiaImage->BuildFromTexture(context, textureInfo, TextureOrigin::TOP_LEFT, bitmapFormat, nullptr, nullptr, nullptr);
155     auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
156     skiaImage->BuildFromTexture(
157         context, textureInfo, TextureOrigin::TOP_LEFT, bitmapFormat, colorSpace, nullptr, nullptr);
158 }
159 
160 /**
161  * @tc.name: BuildFromTexture002
162  * @tc.desc: Test BuildFromTexture
163  * @tc.type: FUNC
164  * @tc.require: I91EH1
165  */
166 HWTEST_F(SkiaImageTest, BuildFromTexture002, TestSize.Level1)
167 {
168     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
169     GPUContext context;
170     TextureInfo textureInfo;
171     textureInfo.SetWidth(10);
172     BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
173     bool buildFromTexture = skiaImage->BuildFromTexture(
174         context, textureInfo, TextureOrigin::TOP_LEFT, bitmapFormat, nullptr, nullptr, nullptr);
175     ASSERT_FALSE(buildFromTexture);
176 }
177 
178 /**
179  * @tc.name: BuildFromSurface001
180  * @tc.desc: Test BuildFromSurface
181  * @tc.type: FUNC
182  * @tc.require: I91EH1
183  */
184 HWTEST_F(SkiaImageTest, BuildFromSurface001, TestSize.Level1)
185 {
186     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
187     auto skiaCanvas = std::make_shared<SkiaCanvas>();
188     auto gpuContext = skiaCanvas->GetGPUContext();
189     auto surface = Surface::MakeRasterN32Premul(800, 800);
190     BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
191     auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
192     auto ret = skiaImage->BuildFromSurface(*gpuContext, *surface, TextureOrigin::TOP_LEFT, bitmapFormat, colorSpace);
193     ASSERT_TRUE(!ret);
194     ASSERT_TRUE(skiaImage->GetColorSpace() == nullptr);
195 }
196 
197 /**
198  * @tc.name: BuildFromSurface002
199  * @tc.desc: Test BuildFromSurface
200  * @tc.type: FUNC
201  * @tc.require: I91EH1
202  */
203 HWTEST_F(SkiaImageTest, BuildFromSurface002, TestSize.Level1)
204 {
205     auto skiaCanvas = std::make_shared<SkiaCanvas>();
206     auto gpuContext = skiaCanvas->GetGPUContext();
207     BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
208     auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
209     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
210     Surface surFace;
211     auto ret = skiaImage->BuildFromSurface(*gpuContext, surFace, TextureOrigin::TOP_LEFT, bitmapFormat, colorSpace);
212     ASSERT_FALSE(ret);
213 }
214 
215 /**
216  * @tc.name: IsTextureBacked002
217  * @tc.desc: Test GetBackendTexture
218  * @tc.type: FUNC
219  * @tc.require: I91EH1
220  */
221 HWTEST_F(SkiaImageTest, IsTextureBacked002, TestSize.Level1)
222 {
223     auto surface = std::make_unique<SkiaSurface>();
224     surface->FlushAndSubmit(true);
225     Bitmap bitmap;
226     BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
227     bitmap.Build(100, 100, bitmapFormat);
228     ASSERT_TRUE(surface->Bind(bitmap));
229 
230     auto image = surface->GetImageSnapshot();
231     if (image) {
232         auto skiaImage = image->GetImpl<SkiaImage>();
233         if (skiaImage) {
234             ASSERT_TRUE(skiaImage->IsValid(nullptr));
235             ASSERT_TRUE(!skiaImage->IsTextureBacked());
236             ASSERT_TRUE(!skiaImage->IsLazyGenerated());
237             ASSERT_TRUE(skiaImage->CanPeekPixels());
238             ASSERT_TRUE(skiaImage->IsOpaque());
239         }
240     }
241 }
242 
243 /**
244  * @tc.name: IsValid001
245  * @tc.desc: Test IsValid
246  * @tc.type: FUNC
247  * @tc.require: I91EH1
248  */
249 HWTEST_F(SkiaImageTest, IsValid001, TestSize.Level1)
250 {
251     Bitmap bitmap;
252     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
253     ASSERT_TRUE(!skiaImage->IsValid(nullptr));
254     auto ret = skiaImage->BuildFromBitmap(bitmap);
255     if (ret) {
256         ASSERT_TRUE(!skiaImage->IsValid(nullptr));
257         GPUContext context;
258         ASSERT_TRUE(skiaImage->IsValid(&context));
259     }
260 }
261 
262 /**
263  * @tc.name: MakeFromYUVAPixmaps
264  * @tc.desc: Test MakeFromYUVAPixmaps
265  * @tc.type: FUNC
266  * @tc.require: I91EH1
267  */
268 HWTEST_F(SkiaImageTest, MakeFromYUVAPixmaps, TestSize.Level1)
269 {
270     Bitmap bitmap;
271     GPUContext gpuContext;
272     YUVInfo info(100, 100, YUVInfo::PlaneConfig::Y_UV, YUVInfo::SubSampling::K420,
273         YUVInfo::YUVColorSpace::JPEG_FULL_YUVCOLORSPACE);
274     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
275     std::shared_ptr<Image> image = skiaImage->MakeFromYUVAPixmaps(gpuContext, info, nullptr);
276     ASSERT_TRUE(image == nullptr);
277 }
278 
279 
280 /**
281  * @tc.name: BuildFromCompressed
282  * @tc.desc: Test BuildFromCompressed
283  * @tc.type: FUNC
284  * @tc.require: I91EH1
285  */
286 HWTEST_F(SkiaImageTest, BuildFromCompressed, TestSize.Level1)
287 {
288     const int height = 100;
289     const int width = 100;
290     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
291     GPUContext context;
292     bool result = skiaImage->BuildFromCompressed(context, nullptr, width, height, CompressedType::ETC2_RGB8_UNORM);
293     ASSERT_FALSE(result);
294 }
295 
296 /**
297  * @tc.name: SetGrBackendTexture
298  * @tc.desc: Test SetGrBackendTexture
299  * @tc.type: FUNC
300  * @tc.require: I91EH1
301  */
302 HWTEST_F(SkiaImageTest, SetGrBackendTexture, TestSize.Level1)
303 {
304     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
305     ASSERT_TRUE(skiaImage != nullptr);
306     GrBackendTexture skBackendTexture;
307     skiaImage->SetGrBackendTexture(skBackendTexture);
308 }
309 #endif
310 
311 /**
312  * @tc.name: IsLazyGenerated001
313  * @tc.desc: Test IsLazyGenerated
314  * @tc.type: FUNC
315  * @tc.require: I91EH1
316  */
317 HWTEST_F(SkiaImageTest, IsLazyGenerated001, TestSize.Level1)
318 {
319     auto surface = std::make_unique<SkiaSurface>();
320     surface->FlushAndSubmit(true);
321     Bitmap bitmap;
322     BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
323     bitmap.Build(100, 100, bitmapFormat);
324     ASSERT_TRUE(surface->Bind(bitmap));
325 
326     auto image = surface->GetImageSnapshot();
327     auto skiaImage = image->GetImpl<SkiaImage>();
328     ASSERT_TRUE(skiaImage->GetImage() != nullptr);
329     skiaImage->GetColorType();
330     skiaImage->GetAlphaType();
331     ASSERT_TRUE(skiaImage->EncodeToData(EncodedImageFormat::JPEG, 1) != nullptr);
332     ASSERT_TRUE(skiaImage->GetColorSpace() == nullptr);
333     ASSERT_TRUE(skiaImage->MakeRasterImage() != nullptr);
334     ASSERT_TRUE(!skiaImage->IsTextureBacked());
335     ASSERT_TRUE(!skiaImage->IsLazyGenerated());
336     ASSERT_TRUE(skiaImage->CanPeekPixels());
337     ASSERT_TRUE(skiaImage->IsOpaque());
338     Bitmap bitmap2;
339     ASSERT_TRUE(skiaImage->GetROPixels(bitmap2));
340     Pixmap pixmap;
341     skiaImage->ReadPixels(pixmap, 100, 100);
342     Bitmap bitmap3;
343     CubicResampler cubicResampler;
344     SamplingOptions options1 { cubicResampler };
345     ASSERT_TRUE(!skiaImage->ScalePixels(bitmap3, options1, false));
346 }
347 
348 /**
349  * @tc.name: IsLazyGenerated002
350  * @tc.desc: Test IsLazyGenerated
351  * @tc.type: FUNC
352  * @tc.require: I91EH1
353  */
354 HWTEST_F(SkiaImageTest, IsLazyGenerated002, TestSize.Level1)
355 {
356     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
357     ASSERT_TRUE(!skiaImage->IsLazyGenerated());
358 }
359 
360 /**
361  * @tc.name: AsLegacyBitmap001
362  * @tc.desc: Test AsLegacyBitmap
363  * @tc.type: FUNC
364  * @tc.require: I91EH1
365  */
366 HWTEST_F(SkiaImageTest, AsLegacyBitmap001, TestSize.Level1)
367 {
368     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
369     Bitmap bitmap;
370     ASSERT_TRUE(!skiaImage->AsLegacyBitmap(bitmap));
371 }
372 
373 /**
374  * @tc.name: IsOpaque001
375  * @tc.desc: Test IsOpaque
376  * @tc.type: FUNC
377  * @tc.require: I91EH1
378  */
379 HWTEST_F(SkiaImageTest, IsOpaque001, TestSize.Level1)
380 {
381     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
382     ASSERT_TRUE(!skiaImage->IsOpaque());
383 }
384 
385 /**
386  * @tc.name: GetAlphaType001
387  * @tc.desc: Test GetAlphaType
388  * @tc.type: FUNC
389  * @tc.require: I91EH1
390  */
391 HWTEST_F(SkiaImageTest, GetAlphaType001, TestSize.Level1)
392 {
393     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
394     skiaImage->GetAlphaType();
395 }
396 
397 /**
398  * @tc.name: GetColorType001
399  * @tc.desc: Test GetColorType
400  * @tc.type: FUNC
401  * @tc.require: I91EH1
402  */
403 HWTEST_F(SkiaImageTest, GetColorType001, TestSize.Level1)
404 {
405     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
406     skiaImage->GetColorType();
407 }
408 
409 /**
410  * @tc.name: GetColorSpace001
411  * @tc.desc: Test GetColorSpace
412  * @tc.type: FUNC
413  * @tc.require: I91EH1
414  */
415 HWTEST_F(SkiaImageTest, GetColorSpace001, TestSize.Level1)
416 {
417     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
418     ASSERT_TRUE(skiaImage->GetColorSpace() == nullptr);
419 }
420 
421 /**
422  * @tc.name: GetImageInfo001
423  * @tc.desc: Test GetImageInfo
424  * @tc.type: FUNC
425  * @tc.require: I91EH1
426  */
427 HWTEST_F(SkiaImageTest, GetImageInfo001, TestSize.Level1)
428 {
429     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
430     skiaImage->GetImageInfo();
431 }
432 
433 /**
434  * @tc.name: IsTextureBacked001
435  * @tc.desc: Test IsTextureBacked
436  * @tc.type: FUNC
437  * @tc.require: I91EH1
438  */
439 HWTEST_F(SkiaImageTest, IsTextureBacked001, TestSize.Level1)
440 {
441     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
442     ASSERT_TRUE(!skiaImage->IsTextureBacked());
443 }
444 
445 /**
446  * @tc.name: ReadPixels001
447  * @tc.desc: Test ReadPixels
448  * @tc.type: FUNC
449  * @tc.require: I91EH1
450  */
451 HWTEST_F(SkiaImageTest, ReadPixels001, TestSize.Level1)
452 {
453     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
454     Pixmap pixmap;
455     skiaImage->ReadPixels(pixmap, 0, 0);
456     ImageInfo dstInfo = ImageInfo::MakeN32Premul(100, 100); // 100: width, height
457     skiaImage->ReadPixels(dstInfo, nullptr, 100, 100, 100); // 100: dstRowBytes, srcX, srcY
458 }
459 
460 /**
461  * @tc.name: ScalePixels001
462  * @tc.desc: Test ScalePixels
463  * @tc.type: FUNC
464  * @tc.require: I91EH1
465  */
466 HWTEST_F(SkiaImageTest, ScalePixels001, TestSize.Level1)
467 {
468     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
469     Bitmap bitmap;
470     CubicResampler cubicResampler;
471     SamplingOptions options1 { cubicResampler };
472     ASSERT_TRUE(!skiaImage->ScalePixels(bitmap, options1, false));
473     SamplingOptions options2;
474     ASSERT_TRUE(!skiaImage->ScalePixels(bitmap, options2, true));
475 }
476 
477 /**
478  * @tc.name: EncodeToData001
479  * @tc.desc: Test EncodeToData
480  * @tc.type: FUNC
481  * @tc.require: I91EH1
482  */
483 HWTEST_F(SkiaImageTest, EncodeToData001, TestSize.Level1)
484 {
485     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
486     ASSERT_TRUE(skiaImage->EncodeToData(EncodedImageFormat::JPEG, 1) == nullptr);
487 }
488 
489 /**
490  * @tc.name: MakeRasterImage001
491  * @tc.desc: Test MakeRasterImage
492  * @tc.type: FUNC
493  * @tc.require: I91EH1
494  */
495 HWTEST_F(SkiaImageTest, MakeRasterImage001, TestSize.Level1)
496 {
497     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
498     ASSERT_TRUE(skiaImage->MakeRasterImage() == nullptr);
499 }
500 
501 /**
502  * @tc.name: GetROPixels001
503  * @tc.desc: Test GetROPixels
504  * @tc.type: FUNC
505  * @tc.require: I91EH1
506  */
507 HWTEST_F(SkiaImageTest, GetROPixels001, TestSize.Level1)
508 {
509     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
510     Bitmap bitmap;
511     ASSERT_TRUE(!skiaImage->GetROPixels(bitmap));
512 }
513 
514 /**
515  * @tc.name: CanPeekPixels001
516  * @tc.desc: Test CanPeekPixels
517  * @tc.type: FUNC
518  * @tc.require: I91EH1
519  */
520 HWTEST_F(SkiaImageTest, CanPeekPixels001, TestSize.Level1)
521 {
522     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
523     ASSERT_TRUE(!skiaImage->CanPeekPixels());
524 }
525 
526 /**
527  * @tc.name: GetImage001
528  * @tc.desc: Test GetImage
529  * @tc.type: FUNC
530  * @tc.require: I91EH1
531  */
532 HWTEST_F(SkiaImageTest, GetImage001, TestSize.Level1)
533 {
534     sk_sp<SkImage> img;
535     SkiaImage skiaImage;
536     skiaImage.SetSkImage(img);
537     ASSERT_TRUE(skiaImage.GetImage() == img);
538 }
539 
540 /**
541  * @tc.name: MakeFromRaster
542  * @tc.desc: Test MakeFromRaster
543  * @tc.type: FUNC
544  * @tc.require: I91EH1
545  */
546 HWTEST_F(SkiaImageTest, MakeFromRaster, TestSize.Level1)
547 {
548     Pixmap pixmap;
549     std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
550     std::shared_ptr<Image> image = skiaImage->MakeFromRaster(pixmap, nullptr, nullptr);
551     ASSERT_TRUE(image == nullptr);
552 }
553 
554 } // namespace Drawing
555 } // namespace Rosen
556 } // namespace OHOS