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 #include "draw/surface.h"
18 #include "gtest/gtest.h"
19 #include "image/image.h"
20 #include "skia_adapter/skia_image.h"
21 #include "skia_adapter/skia_canvas.h"
22 #include "skia_adapter/skia_gpu_context.h"
23 #include "skia_adapter/skia_surface.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Rosen {
30 namespace Drawing {
31 class SkiaImageTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37 };
38
SetUpTestCase()39 void SkiaImageTest::SetUpTestCase() {}
TearDownTestCase()40 void SkiaImageTest::TearDownTestCase() {}
SetUp()41 void SkiaImageTest::SetUp() {}
TearDown()42 void SkiaImageTest::TearDown() {}
43
44 /**
45 * @tc.name: BuildFromBitmap001
46 * @tc.desc:
47 * @tc.type: FUNC
48 * @tc.author:
49 */
50 HWTEST_F(SkiaImageTest, BuildFromBitmap001, TestSize.Level1)
51 {
52 Bitmap bitmap;
53 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
54 auto image = skiaImage->BuildFromBitmap(bitmap);
55 EXPECT_EQ(image, false);
56 }
57
58 /**
59 * @tc.name: MakeRasterData001
60 * @tc.desc: Test MakeRasterData
61 * @tc.type: FUNC
62 * @tc.require: I91EH1
63 */
64 HWTEST_F(SkiaImageTest, MakeRasterData001, TestSize.Level1)
65 {
66 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
67 ImageInfo imageInfo;
68 auto image = skiaImage->MakeRasterData(imageInfo, nullptr, 0);
69 EXPECT_TRUE(image == nullptr);
70 std::shared_ptr<Data> data = std::make_shared<Data>();
71 data->BuildUninitialized(100);
72 auto image2 = skiaImage->MakeRasterData(imageInfo, data, 100);
73 EXPECT_TRUE(image2 == nullptr);
74 }
75
76 #ifdef ACE_ENABLE_GPU
77 /**
78 * @tc.name: MakeFromEncoded001
79 * @tc.desc: Test MakeFromEncoded
80 * @tc.type: FUNC
81 * @tc.require: I91EH1
82 */
83 HWTEST_F(SkiaImageTest, MakeFromEncoded001, TestSize.Level1)
84 {
85 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
86 auto ret = skiaImage->MakeFromEncoded(nullptr);
87 EXPECT_TRUE(!ret);
88 }
89
90 /**
91 * @tc.name: BuildSubset001
92 * @tc.desc: Test BuildSubset
93 * @tc.type: FUNC
94 * @tc.require: I91EH1
95 */
96 HWTEST_F(SkiaImageTest, BuildSubset001, TestSize.Level1)
97 {
98 auto surface = std::make_unique<SkiaSurface>();
99 surface->FlushAndSubmit(true);
100 Bitmap bitmap;
101 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
102 bitmap.Build(100, 100, bitmapFormat);
103 ASSERT_TRUE(surface->Bind(bitmap));
104
105 auto image = surface->GetImageSnapshot();
106 GPUContext context;
107 #ifdef NEW_SKIA
108 GrMockOptions options;
109 context.GetImpl<SkiaGPUContext>()->SetGrContext(GrDirectContext::MakeMock(&options));
110 #endif
111 auto skiaImage = image->GetImpl<SkiaImage>();
112 if (skiaImage) {
113 RectI rect;
114 GPUContext context;
115 skiaImage->BuildSubset(nullptr, rect, context);
116 }
117 }
118
119 /**
120 * @tc.name: BuildFromTexture001
121 * @tc.desc: Test BuildFromTexture
122 * @tc.type: FUNC
123 * @tc.require: I91EH1
124 */
125 HWTEST_F(SkiaImageTest, BuildFromTexture001, TestSize.Level1)
126 {
127 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
128 GPUContext context;
129 #ifdef NEW_SKIA
130 GrMockOptions options;
131 context.GetImpl<SkiaGPUContext>()->SetGrContext(GrDirectContext::MakeMock(&options));
132 #endif
133 TextureInfo textureInfo;
134 BitmapFormat bitmapFormat;
135 skiaImage->BuildFromTexture(context, textureInfo, TextureOrigin::TOP_LEFT, bitmapFormat,
136 nullptr, nullptr, nullptr);
137 auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
138 skiaImage->BuildFromTexture(context, textureInfo, TextureOrigin::TOP_LEFT, bitmapFormat,
139 colorSpace, nullptr, nullptr);
140 }
141
142 /**
143 * @tc.name: BuildFromSurface001
144 * @tc.desc: Test BuildFromSurface
145 * @tc.type: FUNC
146 * @tc.require: I91EH1
147 */
148 HWTEST_F(SkiaImageTest, BuildFromSurface001, TestSize.Level1)
149 {
150 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
151 auto skiaCanvas = std::make_shared<SkiaCanvas>();
152 auto gpuContext = skiaCanvas->GetGPUContext();
153 auto surface = Surface::MakeRasterN32Premul(800, 800);
154 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
155 auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
156 auto ret = skiaImage->BuildFromSurface(*gpuContext, *surface, TextureOrigin::TOP_LEFT, bitmapFormat, colorSpace);
157 ASSERT_TRUE(!ret);
158 ASSERT_TRUE(skiaImage->GetColorSpace() == nullptr);
159 }
160
161 /**
162 * @tc.name: GetBackendTexture001
163 * @tc.desc: Test GetBackendTexture
164 * @tc.type: FUNC
165 * @tc.require: I91EH1
166 */
167 HWTEST_F(SkiaImageTest, GetBackendTexture001, TestSize.Level1)
168 {
169 auto surface = std::make_unique<SkiaSurface>();
170 surface->FlushAndSubmit(true);
171 Bitmap bitmap;
172 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
173 bitmap.Build(100, 100, bitmapFormat);
174 ASSERT_TRUE(surface->Bind(bitmap));
175
176 auto image = surface->GetImageSnapshot();
177 auto skiaImage = image->GetImpl<SkiaImage>();
178 ASSERT_TRUE(skiaImage->IsValid(nullptr));
179 TextureOrigin textureOrigin = TextureOrigin::TOP_LEFT;
180 BackendTexture backendTexture = skiaImage->GetBackendTexture(true, &textureOrigin);
181 ASSERT_TRUE(!backendTexture.IsValid());
182 BackendTexture backendTexture2 = skiaImage->GetBackendTexture(true, nullptr);
183 ASSERT_TRUE(!backendTexture2.IsValid());
184 }
185
186 /**
187 * @tc.name: IsValid001
188 * @tc.desc: Test IsValid
189 * @tc.type: FUNC
190 * @tc.require: I91EH1
191 */
192 HWTEST_F(SkiaImageTest, IsValid001, TestSize.Level1)
193 {
194 Bitmap bitmap;
195 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
196 ASSERT_TRUE(!skiaImage->IsValid(nullptr));
197 auto ret = skiaImage->BuildFromBitmap(bitmap);
198 if (ret) {
199 ASSERT_TRUE(!skiaImage->IsValid(nullptr));
200 GPUContext context;
201 ASSERT_TRUE(skiaImage->IsValid(&context));
202 }
203 }
204 #endif
205
206 /**
207 * @tc.name: IsLazyGenerated001
208 * @tc.desc: Test IsLazyGenerated
209 * @tc.type: FUNC
210 * @tc.require: I91EH1
211 */
212 HWTEST_F(SkiaImageTest, IsLazyGenerated001, TestSize.Level1)
213 {
214 auto surface = std::make_unique<SkiaSurface>();
215 surface->FlushAndSubmit(true);
216 Bitmap bitmap;
217 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
218 bitmap.Build(100, 100, bitmapFormat);
219 ASSERT_TRUE(surface->Bind(bitmap));
220
221 auto image = surface->GetImageSnapshot();
222 auto skiaImage = image->GetImpl<SkiaImage>();
223 ASSERT_TRUE(skiaImage->GetImage() != nullptr);
224 skiaImage->GetColorType();
225 skiaImage->GetAlphaType();
226 ASSERT_TRUE(skiaImage->EncodeToData(EncodedImageFormat::JPEG, 1) != nullptr);
227 ASSERT_TRUE(skiaImage->GetColorSpace() == nullptr);
228 ASSERT_TRUE(skiaImage->MakeRasterImage() != nullptr);
229 ASSERT_TRUE(!skiaImage->IsTextureBacked());
230 ASSERT_TRUE(!skiaImage->IsLazyGenerated());
231 ASSERT_TRUE(skiaImage->CanPeekPixels());
232 ASSERT_TRUE(skiaImage->IsOpaque());
233 Bitmap bitmap2;
234 ASSERT_TRUE(skiaImage->GetROPixels(bitmap2));
235 Pixmap pixmap;
236 skiaImage->ReadPixels(pixmap, 100, 100);
237 Bitmap bitmap3;
238 CubicResampler cubicResampler;
239 SamplingOptions options1{cubicResampler};
240 ASSERT_TRUE(!skiaImage->ScalePixels(bitmap3, options1, false));
241 }
242
243 /**
244 * @tc.name: IsLazyGenerated002
245 * @tc.desc: Test IsLazyGenerated
246 * @tc.type: FUNC
247 * @tc.require: I91EH1
248 */
249 HWTEST_F(SkiaImageTest, IsLazyGenerated002, TestSize.Level1)
250 {
251 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
252 ASSERT_TRUE(!skiaImage->IsLazyGenerated());
253 }
254
255 /**
256 * @tc.name: AsLegacyBitmap001
257 * @tc.desc: Test AsLegacyBitmap
258 * @tc.type: FUNC
259 * @tc.require: I91EH1
260 */
261 HWTEST_F(SkiaImageTest, AsLegacyBitmap001, TestSize.Level1)
262 {
263 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
264 Bitmap bitmap;
265 ASSERT_TRUE(!skiaImage->AsLegacyBitmap(bitmap));
266 }
267
268 /**
269 * @tc.name: IsOpaque001
270 * @tc.desc: Test IsOpaque
271 * @tc.type: FUNC
272 * @tc.require: I91EH1
273 */
274 HWTEST_F(SkiaImageTest, IsOpaque001, TestSize.Level1)
275 {
276 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
277 ASSERT_TRUE(!skiaImage->IsOpaque());
278 }
279
280 /**
281 * @tc.name: GetAlphaType001
282 * @tc.desc: Test GetAlphaType
283 * @tc.type: FUNC
284 * @tc.require: I91EH1
285 */
286 HWTEST_F(SkiaImageTest, GetAlphaType001, TestSize.Level1)
287 {
288 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
289 skiaImage->GetAlphaType();
290 }
291
292 /**
293 * @tc.name: GetColorType001
294 * @tc.desc: Test GetColorType
295 * @tc.type: FUNC
296 * @tc.require: I91EH1
297 */
298 HWTEST_F(SkiaImageTest, GetColorType001, TestSize.Level1)
299 {
300 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
301 skiaImage->GetColorType();
302 }
303
304 /**
305 * @tc.name: GetColorSpace001
306 * @tc.desc: Test GetColorSpace
307 * @tc.type: FUNC
308 * @tc.require: I91EH1
309 */
310 HWTEST_F(SkiaImageTest, GetColorSpace001, TestSize.Level1)
311 {
312 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
313 ASSERT_TRUE(skiaImage->GetColorSpace() == nullptr);
314 }
315
316 /**
317 * @tc.name: GetImageInfo001
318 * @tc.desc: Test GetImageInfo
319 * @tc.type: FUNC
320 * @tc.require: I91EH1
321 */
322 HWTEST_F(SkiaImageTest, GetImageInfo001, TestSize.Level1)
323 {
324 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
325 skiaImage->GetImageInfo();
326 }
327
328 /**
329 * @tc.name: IsTextureBacked001
330 * @tc.desc: Test IsTextureBacked
331 * @tc.type: FUNC
332 * @tc.require: I91EH1
333 */
334 HWTEST_F(SkiaImageTest, IsTextureBacked001, TestSize.Level1)
335 {
336 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
337 ASSERT_TRUE(!skiaImage->IsTextureBacked());
338 }
339
340 /**
341 * @tc.name: ReadPixels001
342 * @tc.desc: Test ReadPixels
343 * @tc.type: FUNC
344 * @tc.require: I91EH1
345 */
346 HWTEST_F(SkiaImageTest, ReadPixels001, TestSize.Level1)
347 {
348 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
349 Pixmap pixmap;
350 skiaImage->ReadPixels(pixmap, 0, 0);
351 ImageInfo dstInfo = ImageInfo::MakeN32Premul(100, 100); // 100: width, height
352 skiaImage->ReadPixels(dstInfo, nullptr, 100, 100, 100); // 100: dstRowBytes, srcX, srcY
353 }
354
355 /**
356 * @tc.name: ScalePixels001
357 * @tc.desc: Test ScalePixels
358 * @tc.type: FUNC
359 * @tc.require: I91EH1
360 */
361 HWTEST_F(SkiaImageTest, ScalePixels001, TestSize.Level1)
362 {
363 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
364 Bitmap bitmap;
365 CubicResampler cubicResampler;
366 SamplingOptions options1{cubicResampler};
367 ASSERT_TRUE(!skiaImage->ScalePixels(bitmap, options1, false));
368 SamplingOptions options2;
369 ASSERT_TRUE(!skiaImage->ScalePixels(bitmap, options2, true));
370 }
371
372 /**
373 * @tc.name: EncodeToData001
374 * @tc.desc: Test EncodeToData
375 * @tc.type: FUNC
376 * @tc.require: I91EH1
377 */
378 HWTEST_F(SkiaImageTest, EncodeToData001, TestSize.Level1)
379 {
380 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
381 ASSERT_TRUE(skiaImage->EncodeToData(EncodedImageFormat::JPEG, 1) == nullptr);
382 }
383
384 /**
385 * @tc.name: MakeRasterImage001
386 * @tc.desc: Test MakeRasterImage
387 * @tc.type: FUNC
388 * @tc.require: I91EH1
389 */
390 HWTEST_F(SkiaImageTest, MakeRasterImage001, TestSize.Level1)
391 {
392 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
393 ASSERT_TRUE(skiaImage->MakeRasterImage() == nullptr);
394 }
395
396 /**
397 * @tc.name: GetROPixels001
398 * @tc.desc: Test GetROPixels
399 * @tc.type: FUNC
400 * @tc.require: I91EH1
401 */
402 HWTEST_F(SkiaImageTest, GetROPixels001, TestSize.Level1)
403 {
404 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
405 Bitmap bitmap;
406 ASSERT_TRUE(!skiaImage->GetROPixels(bitmap));
407 }
408
409 /**
410 * @tc.name: CanPeekPixels001
411 * @tc.desc: Test CanPeekPixels
412 * @tc.type: FUNC
413 * @tc.require: I91EH1
414 */
415 HWTEST_F(SkiaImageTest, CanPeekPixels001, TestSize.Level1)
416 {
417 std::shared_ptr<SkiaImage> skiaImage = std::make_shared<SkiaImage>();
418 ASSERT_TRUE(!skiaImage->CanPeekPixels());
419 }
420
421 } // namespace Drawing
422 } // namespace Rosen
423 } // namespace OHOS