• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     surface->Flush(nullptr);
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;
__anon4f6de05e0102(void *context) 285     drawingFlushInfo.finishedProc = [](void *context) {
286         int* count = reinterpret_cast<int *>(context);
287         (*count)++;
288     };
289     drawingFlushInfo.submittedContext = &count2;
__anon4f6de05e0202(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     surface->Flush(&drawingFlushInfo);
296     ASSERT_EQ(count1, 1); // finishedProc excute 1 time
297     ASSERT_EQ(count2, 1); // submittedProc excute 1 time
298 }
299 
300 /**
301  * @tc.name: Width001
302  * @tc.desc: Test Width
303  * @tc.type: FUNC
304  * @tc.require:I91EDT
305  */
306 HWTEST_F(SkiaSurfaceTest, Width001, TestSize.Level1)
307 {
308     auto sf = SkiaSurface::MakeRasterN32Premul(800, 800); // 800: width, height
309     ASSERT_TRUE(sf != nullptr);
310     ASSERT_TRUE(sf->Width() == 800);
311 }
312 
313 /**
314  * @tc.name: Height001
315  * @tc.desc: Test Height
316  * @tc.type: FUNC
317  * @tc.require:I91EDT
318  */
319 HWTEST_F(SkiaSurfaceTest, Height001, TestSize.Level1)
320 {
321     auto sf = SkiaSurface::MakeRasterN32Premul(800, 800); // 800: width, height
322     ASSERT_TRUE(sf != nullptr);
323     ASSERT_TRUE(sf->Height() == 800);
324 }
325 
326 /**
327  * @tc.name: GetCanvas001
328  * @tc.desc: Test GetCanvas
329  * @tc.type: FUNC
330  * @tc.require:I91EDT
331  */
332 HWTEST_F(SkiaSurfaceTest, GetCanvas001, TestSize.Level1)
333 {
334     sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
335     SkiaSurface skiaSurface;
336     skiaSurface.SetSkSurface(skSurface);
337     auto canvas = skiaSurface.GetCanvas();
338     ASSERT_TRUE(canvas != nullptr);
339 }
340 
341 /**
342  * @tc.name: GetImageSnapshot001
343  * @tc.desc: Test GetImageSnapshot
344  * @tc.type: FUNC
345  * @tc.require:I91EDT
346  */
347 HWTEST_F(SkiaSurfaceTest, GetImageSnapshot001, TestSize.Level1)
348 {
349     sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
350     SkiaSurface skiaSurface;
351     skiaSurface.SetSkSurface(skSurface);
352     auto snapshot = skiaSurface.GetImageSnapshot();
353     ASSERT_TRUE(snapshot != nullptr);
354 }
355 
356 /**
357  * @tc.name: GetImageSnapshot001
358  * @tc.desc: Test GetImageSnapshot
359  * @tc.type: FUNC
360  * @tc.require:I91EDT
361  */
362 HWTEST_F(SkiaSurfaceTest, GetImageSnapshot002, TestSize.Level1)
363 {
364     sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
365     SkiaSurface skiaSurface;
366     ImageInfo imageInfo;
367     imageInfo.SetWidth(100);
368     imageInfo.SetHeight(200);
369     RectI rectI;
370     rectI = imageInfo.GetBound();
371     skiaSurface.SetSkSurface(skSurface);
372     auto snapshot = skiaSurface.GetImageSnapshot(rectI);
373     ASSERT_TRUE(snapshot != nullptr);
374 }
375 
376 /**
377  * @tc.name: SetSkSurface001
378  * @tc.desc: Test SetSkSurface
379  * @tc.type: FUNC
380  * @tc.require:I91EDT
381  */
382 HWTEST_F(SkiaSurfaceTest, SetSkSurface001, TestSize.Level1)
383 {
384     sk_sp<SkSurface> skSurface = SkSurface::MakeRasterN32Premul(100, 100);
385     SkiaSurface skiaSurface;
386     skiaSurface.SetSkSurface(skSurface);
387     ASSERT_TRUE(skSurface == skiaSurface.GetSkSurface());
388 }
389 } // namespace Drawing
390 } // namespace Rosen
391 } // namespace OHOS
392