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 <memory>
17 #include "gtest/gtest.h"
18 #include "effect/color_space.h"
19 #include "image/gpu_context.h"
20 #include "skia_adapter/skia_canvas.h"
21 #include "skia_adapter/skia_gpu_context.h"
22 #include "skia_adapter/skia_surface.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 class SkiaSurfaceTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void SkiaSurfaceTest::SetUpTestCase() {}
TearDownTestCase()39 void SkiaSurfaceTest::TearDownTestCase() {}
SetUp()40 void SkiaSurfaceTest::SetUp() {}
TearDown()41 void SkiaSurfaceTest::TearDown() {}
42
43 #ifdef RS_ENABLE_GPU
44 /**
45 * @tc.name: Bind001
46 * @tc.desc: Test Bind
47 * @tc.type: FUNC
48 * @tc.require:I91EDT
49 */
50 HWTEST_F(SkiaSurfaceTest, Bind001, TestSize.Level1)
51 {
52 auto surface = std::make_unique<SkiaSurface>();
53 ASSERT_TRUE(surface != nullptr);
54 surface->FlushAndSubmit(true);
55 Bitmap bitmap;
56 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
57 bitmap.Build(100, 100, bitmapFormat); // 100: width, height
58 ASSERT_TRUE(surface->Bind(bitmap));
59
60 auto image = surface->GetImageSnapshot();
61 GPUContext context;
62 #ifdef NEW_SKIA
63 GrMockOptions options;
64 context.GetImpl<SkiaGPUContext>()->SetGrContext(GrDirectContext::MakeMock(&options));
65 #endif
66 Bitmap bitmap2;
67 bitmap2.Build(10, 10, bitmapFormat); // 10: width, height
68 image->BuildFromBitmap(context, bitmap2);
69 auto surface2 = std::make_unique<SkiaSurface>();
70 ASSERT_TRUE(surface2 != nullptr);
71 ASSERT_TRUE(!surface2->Bind(*image));
72 }
73
74 #ifdef RS_ENABLE_VK
75 /**
76 * @tc.name: MakeFromBackendRenderTarget001
77 * @tc.desc: Test MakeFromBackendRenderTarget
78 * @tc.type: FUNC
79 * @tc.require:I91EDT
80 */
81 HWTEST_F(SkiaSurfaceTest, MakeFromBackendRenderTarget001, TestSize.Level1)
82 {
83 auto skiaCanvas = std::make_shared<SkiaCanvas>();
84 ASSERT_TRUE(skiaCanvas != nullptr);
85 skiaCanvas->ImportSkCanvas(nullptr);
86 auto gpuContext = skiaCanvas->GetGPUContext();
87 TextureInfo info;
88 info.SetWidth(10);
89 auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
90 auto surface1 = SkiaSurface::MakeFromBackendRenderTarget(gpuContext.get(), info, TextureOrigin::TOP_LEFT,
91 ColorType::COLORTYPE_RGBA_8888, colorSpace, nullptr, nullptr);
92 auto surface2 = SkiaSurface::MakeFromBackendRenderTarget(gpuContext.get(), info, TextureOrigin::TOP_LEFT,
93 ColorType::COLORTYPE_RGBA_8888, nullptr, nullptr, nullptr);
94 ASSERT_TRUE(surface1 == nullptr);
95 ASSERT_TRUE(surface2 == nullptr);
96 }
97
98 /**
99 * @tc.name: MakeFromBackendTexture001
100 * @tc.desc: Test MakeFromBackendTexture
101 * @tc.type: FUNC
102 * @tc.require:I91EDT
103 */
104 HWTEST_F(SkiaSurfaceTest, MakeFromBackendTexture001, TestSize.Level1)
105 {
106 auto skiaCanvas = std::make_shared<SkiaCanvas>();
107 ASSERT_TRUE(skiaCanvas != nullptr);
108 skiaCanvas->ImportSkCanvas(nullptr);
109 auto gpuContext = skiaCanvas->GetGPUContext();
110 TextureInfo info;
111 info.SetWidth(10);
112 auto surface = SkiaSurface::MakeFromBackendTexture(nullptr, info, TextureOrigin::TOP_LEFT, 1,
113 ColorType::COLORTYPE_RGBA_8888, nullptr, nullptr, nullptr);
114 ASSERT_TRUE(surface == nullptr);
115 auto surface = SkiaSurface::MakeFromBackendTexture(gpuContext.get(), info, TextureOrigin::TOP_LEFT, 1,
116 ColorType::COLORTYPE_RGBA_8888, nullptr, nullptr, nullptr);
117 ASSERT_TRUE(surface == nullptr);
118 }
119
120 /**
121 * @tc.name: Wait001
122 * @tc.desc: Test Wait
123 * @tc.type: FUNC
124 * @tc.require:I91EDT
125 */
126 HWTEST_F(SkiaSurfaceTest, Wait001, TestSize.Level1)
127 {
128 auto surface = std::make_unique<SkiaSurface>();
129 ASSERT_TRUE(surface != nullptr);
130 VkSemaphore semaphore;
131 surface->Wait(5, semaphore); // 5: time
132 Bitmap bitmap;
133 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
134 bitmap.Build(10, 10, bitmapFormat); // 10: width, height
135 ASSERT_TRUE(surface->Bind(bitmap));
136 surface->Wait(1000, semaphore);
137 }
138
139 /**
140 * @tc.name: SetDrawingArea001
141 * @tc.desc: Test SetDrawingArea
142 * @tc.type: FUNC
143 * @tc.require:I91EDT
144 */
145 HWTEST_F(SkiaSurfaceTest, SetDrawingArea001, TestSize.Level1)
146 {
147 auto surface = std::make_unique<SkiaSurface>();
148 ASSERT_TRUE(surface != nullptr);
149 const std::vector<RectI>& rects;
150 surface->SetDrawingArea(rects);
151 }
152
153 /**
154 * @tc.name: ClearDrawingArea001
155 * @tc.desc: Test ClearDrawingArea
156 * @tc.type: FUNC
157 * @tc.require:I91EDT
158 */
159 HWTEST_F(SkiaSurfaceTest, ClearDrawingArea001, TestSize.Level1)
160 {
161 auto surface = std::make_unique<SkiaSurface>();
162 ASSERT_TRUE(surface != nullptr);
163 surface->ClearDrawingArea();
164 }
165 #endif
166 #endif
167
168 /**
169 * @tc.name: MakeRenderTarget001
170 * @tc.desc: Test MakeRenderTarget
171 * @tc.type: FUNC
172 * @tc.require:I91EDT
173 */
174 HWTEST_F(SkiaSurfaceTest, MakeRenderTarget001, TestSize.Level1)
175 {
176 auto skiaCanvas = std::make_shared<SkiaCanvas>();
177 ASSERT_TRUE(skiaCanvas != nullptr);
178 skiaCanvas->ImportSkCanvas(nullptr);
179 auto gpuContext = skiaCanvas->GetGPUContext();
180 ImageInfo imageInfo;
181 auto surface1 = SkiaSurface::MakeRenderTarget(gpuContext.get(), true, imageInfo);
182 auto surface2 = SkiaSurface::MakeRenderTarget(nullptr, true, imageInfo);
183 ASSERT_TRUE(surface1 == nullptr);
184 ASSERT_TRUE(surface2 == nullptr);
185 }
186
187 /**
188 * @tc.name: MakeRaster001
189 * @tc.desc: Test MakeRaster
190 * @tc.type: FUNC
191 * @tc.require:I91EDT
192 */
193 HWTEST_F(SkiaSurfaceTest, MakeRaster001, TestSize.Level1)
194 {
195 ImageInfo info{-1, -1, ColorType::COLORTYPE_ALPHA_8, AlphaType::ALPHATYPE_OPAQUE};
196 auto surface = SkiaSurface::MakeRaster(info);
197 ASSERT_TRUE(surface == nullptr);
198 }
199
200 /**
201 * @tc.name: MakeRasterN32Premul001
202 * @tc.desc: Test MakeRasterN32Premul
203 * @tc.type: FUNC
204 * @tc.require:I91EDT
205 */
206 HWTEST_F(SkiaSurfaceTest, MakeRasterN32Premul001, TestSize.Level1)
207 {
208 auto surface = SkiaSurface::MakeRasterN32Premul(800, 800); // 800: width, height
209 ASSERT_TRUE(surface != nullptr);
210 auto surface2 = SkiaSurface::MakeRasterN32Premul(-1, -1); // -1: width, height
211 ASSERT_TRUE(surface2 == nullptr);
212 }
213
214 /**
215 * @tc.name: MakeSurface001
216 * @tc.desc: Test MakeSurface
217 * @tc.type: FUNC
218 * @tc.require:I91EDT
219 */
220 HWTEST_F(SkiaSurfaceTest, MakeSurface001, TestSize.Level1)
221 {
222 auto surface = std::make_unique<SkiaSurface>();
223 ASSERT_TRUE(surface != nullptr);
224 auto surface2 = surface->MakeSurface(800, 800); // 800: width, height
225 ASSERT_TRUE(surface2 == nullptr);
226 }
227
228 /**
229 * @tc.name: GetSkSurface001
230 * @tc.desc: Test GetSkSurface
231 * @tc.type: FUNC
232 * @tc.require:I91EDT
233 */
234 HWTEST_F(SkiaSurfaceTest, GetSkSurface001, TestSize.Level1)
235 {
236 auto surface = std::make_unique<SkiaSurface>();
237 ASSERT_TRUE(surface->GetSkSurface() == nullptr);
238 }
239
240 /**
241 * @tc.name: FlushAndSubmit001
242 * @tc.desc: Test FlushAndSubmit
243 * @tc.type: FUNC
244 * @tc.require:I91EDT
245 */
246 HWTEST_F(SkiaSurfaceTest, FlushAndSubmit001, TestSize.Level1)
247 {
248 auto surface = std::make_unique<SkiaSurface>();
249 ASSERT_TRUE(surface != nullptr);
250 surface->FlushAndSubmit(false);
251 Bitmap bitmap;
252 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
253 bitmap.Build(10, 10, bitmapFormat); // 10: width, height
254 ASSERT_TRUE(surface->Bind(bitmap));
255 surface->FlushAndSubmit(true);
256 }
257
258 /**
259 * @tc.name: Flush001
260 * @tc.desc: Test Flush
261 * @tc.type: FUNC
262 * @tc.require:I91EDT
263 */
264 HWTEST_F(SkiaSurfaceTest, Flush001, TestSize.Level1)
265 {
266 auto surface = std::make_unique<SkiaSurface>();
267 ASSERT_TRUE(surface != nullptr);
268 EXPECT_EQ(surface->Flush(nullptr), SemaphoresSubmited::DRAWING_SUBMIT_NO);
269 }
270
271 /**
272 * @tc.name: Flush002
273 * @tc.desc: Test Flush
274 * @tc.type: FUNC
275 * @tc.require:I91EDT
276 */
277 HWTEST_F(SkiaSurfaceTest, Flush002, TestSize.Level1)
278 {
279 auto surface = std::make_unique<SkiaSurface>();
280 ASSERT_TRUE(surface != nullptr);
281 FlushInfo drawingFlushInfo = { 0 };
282 int count1 = 0;
283 int count2 = 0;
284 drawingFlushInfo.finishedContext = &count1;
__anoneab9d9400102(void *context) 285 drawingFlushInfo.finishedProc = [](void *context) {
286 int* count = reinterpret_cast<int *>(context);
287 (*count)++;
288 };
289 drawingFlushInfo.submittedContext = &count2;
__anoneab9d9400202(void *context, bool success) 290 drawingFlushInfo.submittedProc = [](void *context, bool success) {
291 int* count = reinterpret_cast<int *>(context);
292 (*count)++;
293 ASSERT_TRUE(success == false);
294 };
295 EXPECT_EQ(surface->Flush(&drawingFlushInfo), SemaphoresSubmited::DRAWING_SUBMIT_YES);
296 ASSERT_EQ(count1, 1); // finishedProc excute 1 time
297 ASSERT_EQ(count2, 1); // submittedProc excute 1 time
298 }
299
300 /**
301 * @tc.name: Flush003
302 * @tc.desc: Test Flush
303 * @tc.type: FUNC
304 * @tc.require:ICQM91
305 */
306 HWTEST_F(SkiaSurfaceTest, Flush003, TestSize.Level1)
307 {
308 auto surface = std::make_unique<SkiaSurface>();
309 EXPECT_TRUE(surface != nullptr);
310 Bitmap bitmap;
311 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
312 bitmap.Build(10, 10, bitmapFormat); // 10: width, height
313 EXPECT_TRUE(surface->Bind(bitmap));
314 FlushInfo drawingFlushInfo = { 0 };
315 int count1 = 0;
316 int count2 = 0;
317 drawingFlushInfo.finishedContext = &count1;
__anoneab9d9400302(void *context) 318 drawingFlushInfo.finishedProc = [](void *context) {
319 int* count = reinterpret_cast<int *>(context);
320 (*count)++;
321 };
322 drawingFlushInfo.submittedContext = &count2;
__anoneab9d9400402(void *context, bool success) 323 drawingFlushInfo.submittedProc = [](void *context, bool success) {
324 int* count = reinterpret_cast<int *>(context);
325 (*count)++;
326 EXPECT_TRUE(success == false);
327 };
328 EXPECT_EQ(surface->Flush(&drawingFlushInfo), SemaphoresSubmited::DRAWING_ENGINE_SUBMIT_NO);
329 EXPECT_EQ(count1, 0); // finishedProc excute 1 time
330 EXPECT_EQ(count2, 0); // submittedProc excute 1 time
331 }
332
333 /**
334 * @tc.name: Flush004
335 * @tc.desc: Test Flush
336 * @tc.type: FUNC
337 * @tc.require:ICQM91
338 */
339 HWTEST_F(SkiaSurfaceTest, Flush004, TestSize.Level1)
340 {
341 auto surface = std::make_unique<SkiaSurface>();
342 EXPECT_TRUE(surface != nullptr);
343 Bitmap bitmap;
344 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
345 bitmap.Build(10, 10, bitmapFormat); // 10: width, height
346 EXPECT_TRUE(surface->Bind(bitmap));
347 EXPECT_EQ(surface->Flush(nullptr), SemaphoresSubmited::DRAWING_ENGINE_YES);
348 }
349
350 /**
351 * @tc.name: Width001
352 * @tc.desc: Test Width
353 * @tc.type: FUNC
354 * @tc.require:I91EDT
355 */
356 HWTEST_F(SkiaSurfaceTest, Width001, TestSize.Level1)
357 {
358 auto sf = SkiaSurface::MakeRasterN32Premul(800, 800); // 800: width, height
359 ASSERT_TRUE(sf != nullptr);
360 ASSERT_TRUE(sf->Width() == 800);
361 }
362
363 /**
364 * @tc.name: Height001
365 * @tc.desc: Test Height
366 * @tc.type: FUNC
367 * @tc.require:I91EDT
368 */
369 HWTEST_F(SkiaSurfaceTest, Height001, TestSize.Level1)
370 {
371 auto sf = SkiaSurface::MakeRasterN32Premul(800, 800); // 800: width, height
372 ASSERT_TRUE(sf != nullptr);
373 ASSERT_TRUE(sf->Height() == 800);
374 }
375
376 /**
377 * @tc.name: GetCanvas001
378 * @tc.desc: Test GetCanvas
379 * @tc.type: FUNC
380 * @tc.require:I91EDT
381 */
382 HWTEST_F(SkiaSurfaceTest, GetCanvas001, TestSize.Level1)
383 {
384 #ifdef USE_M133_SKIA
385 sk_sp<SkSurface> skSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
386 #else
387 sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
388 #endif
389 SkiaSurface skiaSurface;
390 skiaSurface.SetSkSurface(skSurface);
391 auto canvas = skiaSurface.GetCanvas();
392 ASSERT_TRUE(canvas != nullptr);
393 }
394
395 /**
396 * @tc.name: GetImageSnapshot001
397 * @tc.desc: Test GetImageSnapshot
398 * @tc.type: FUNC
399 * @tc.require:I91EDT
400 */
401 HWTEST_F(SkiaSurfaceTest, GetImageSnapshot001, TestSize.Level1)
402 {
403 #ifdef USE_M133_SKIA
404 sk_sp<SkSurface> skSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
405 #else
406 sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
407 #endif
408 SkiaSurface skiaSurface;
409 skiaSurface.SetSkSurface(skSurface);
410 auto snapshot = skiaSurface.GetImageSnapshot();
411 ASSERT_TRUE(snapshot != nullptr);
412 }
413
414 /**
415 * @tc.name: GetImageSnapshot001
416 * @tc.desc: Test GetImageSnapshot
417 * @tc.type: FUNC
418 * @tc.require:I91EDT
419 */
420 HWTEST_F(SkiaSurfaceTest, GetImageSnapshot002, TestSize.Level1)
421 {
422 #ifdef USE_M133_SKIA
423 sk_sp<SkSurface> skSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
424 #else
425 sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
426 #endif
427 SkiaSurface skiaSurface;
428 ImageInfo imageInfo;
429 imageInfo.SetWidth(100);
430 imageInfo.SetHeight(200);
431 RectI rectI;
432 rectI = imageInfo.GetBound();
433 skiaSurface.SetSkSurface(skSurface);
434 auto snapshot = skiaSurface.GetImageSnapshot(rectI);
435 ASSERT_TRUE(snapshot != nullptr);
436 }
437
438 /**
439 * @tc.name: SetSkSurface001
440 * @tc.desc: Test SetSkSurface
441 * @tc.type: FUNC
442 * @tc.require:I91EDT
443 */
444 HWTEST_F(SkiaSurfaceTest, SetSkSurface001, TestSize.Level1)
445 {
446 #ifdef USE_M133_SKIA
447 sk_sp<SkSurface> skSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
448 #else
449 sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
450 #endif
451 SkiaSurface skiaSurface;
452 skiaSurface.SetSkSurface(skSurface);
453 ASSERT_TRUE(skSurface == skiaSurface.GetSkSurface());
454 }
455 } // namespace Drawing
456 } // namespace Rosen
457 } // namespace OHOS
458