1 /*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "EGL/egl.h"
17 #include "EGL/eglext.h"
18 #include "drawing_bitmap.h"
19 #include "drawing_error_code.h"
20 #include "drawing_gpu_context.h"
21 #include "drawing_surface.h"
22 #include "drawing_canvas.h"
23 #include "drawing_rect.h"
24 #include "gtest/gtest.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace Drawing {
32 class DrawingNativeSurFaceTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38
39 protected:
40 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
41 EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
42 EGLContext eglContext_ = EGL_NO_CONTEXT;
43 EGLSurface eglSurface_ = EGL_NO_SURFACE;
44 OH_Drawing_GpuContext *gpuContext_ = nullptr;
45 OH_Drawing_Surface *surface_ = nullptr;
46 OH_Drawing_Canvas *canvas_ = nullptr;
47 };
48
SetUpTestCase()49 void DrawingNativeSurFaceTest::SetUpTestCase() {}
TearDownTestCase()50 void DrawingNativeSurFaceTest::TearDownTestCase() {}
SetUp()51 void DrawingNativeSurFaceTest::SetUp()
52 {
53 eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
54 EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
55
56 EGLint eglMajVers;
57 EGLint eglMinVers;
58 EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
59 EXPECT_EQ(ret, EGL_TRUE);
60
61 EGLint count;
62 EGLint configAttribs[] = {
63 EGL_SURFACE_TYPE,
64 EGL_WINDOW_BIT,
65 EGL_RED_SIZE,
66 8,
67 EGL_GREEN_SIZE,
68 8,
69 EGL_BLUE_SIZE,
70 8,
71 EGL_ALPHA_SIZE,
72 8,
73 EGL_RENDERABLE_TYPE,
74 EGL_OPENGL_ES3_BIT,
75 EGL_NONE,
76 };
77 ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
78 EXPECT_EQ(ret, EGL_TRUE);
79 EXPECT_GE(count, 1);
80
81 const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
82 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
83 EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
84
85 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
86 eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
87 EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
88
89 ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
90 EXPECT_EQ(ret, EGL_TRUE);
91 // 初始化errorCode
92 std::cout << "DrawingNativeSurFaceTest Setup code called before each test case." << std::endl;
93 OH_Drawing_ErrorCodeReset();
94 std::cout << "DrawingNativeSurFaceTest errorCodeReset before each test case." << std::endl;
95 }
96
TearDown()97 void DrawingNativeSurFaceTest::TearDown()
98 {
99 EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
100 EXPECT_EQ(ret, EGL_TRUE);
101
102 ret = eglDestroyContext(eglDisplay_, eglContext_);
103 EXPECT_EQ(ret, EGL_TRUE);
104
105 ret = eglTerminate(eglDisplay_);
106 EXPECT_EQ(ret, EGL_TRUE);
107
108 eglSurface_ = EGL_NO_SURFACE;
109 eglContext_ = EGL_NO_CONTEXT;
110 eglDisplay_ = EGL_NO_DISPLAY;
111 std::cout << "DrawingNativeSurFaceTest Setup code called after each test case." << std::endl;
112 OH_Drawing_ErrorCodeReset();
113 std::cout << "DrawingNativeSurFaceTest errorCodeReset after each test case." << std::endl;
114 }
115
116 /*
117 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0100
118 * @tc.name: testSurfaceCreateFromGpuContextNormal
119 * @tc.desc: test for testSurfaceCreateFromGpuContextNormal.
120 * @tc.size : SmallTest
121 * @tc.type : Function
122 * @tc.level : Level 0
123 */
124 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateFromGpuContextNormal, TestSize.Level0) {
125 OH_Drawing_GpuContextOptions options{true};
126 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
127 EXPECT_NE(gpuContext_, nullptr);
128 const int32_t width = 500;
129 const int32_t height = 500;
130 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
131 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
132 // add assert
133 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_SUCCESS);
134 EXPECT_NE(surface_, nullptr);
135 OH_Drawing_SurfaceDestroy(surface_);
136 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo);
137 // add assert
138 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_SUCCESS);
139 EXPECT_NE(surface_, nullptr);
140 OH_Drawing_SurfaceDestroy(surface_);
141 }
142
143 /*
144 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0101
145 * @tc.name: testSurfaceCreateFromGpuContextNull
146 * @tc.desc: test for testSurfaceCreateFromGpuContextNull.
147 * @tc.size : SmallTest
148 * @tc.type : Function
149 * @tc.level : Level 3
150 */
151 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateFromGpuContextNull, TestSize.Level3) {
152 OH_Drawing_GpuContextOptions options{true};
153 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
154 EXPECT_NE(gpuContext_, nullptr);
155 const int32_t width = 500;
156 const int32_t height = 500;
157 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
158 // OH_Drawing_GpuContext is NULL, check error code using OH_Drawing_ErrorCodeGet
159 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(nullptr, true, imageInfo);
160 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
161 // Exceptional parameter passing for OH_Drawing_Image_Info
162 OH_Drawing_Image_Info imageInfo2 = {0, 0, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
163 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo2);
164 EXPECT_EQ(surface_, nullptr);
165 OH_Drawing_SurfaceDestroy(surface_);
166 }
167
168 /*
169 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0102
170 * @tc.name: testSurfaceCreateFromGpuContextBoundary
171 * @tc.desc: test for testSurfaceCreateFromGpuContextBoundary.
172 * @tc.size : SmallTest
173 * @tc.type : Function
174 * @tc.level : Level 0
175 */
176 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateFromGpuContextBoundary, TestSize.Level0) {
177 OH_Drawing_GpuContextOptions options{true};
178 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
179 EXPECT_NE(gpuContext_, nullptr);
180 const int32_t width = 4096;
181 const int32_t height = 2160;
182 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
183 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
184 EXPECT_NE(surface_, nullptr);
185 OH_Drawing_SurfaceDestroy(surface_);
186 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo);
187 EXPECT_NE(surface_, nullptr);
188 OH_Drawing_SurfaceDestroy(surface_);
189 }
190
191 /*
192 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0200
193 * @tc.name: testSurfaceDestroyNormal
194 * @tc.desc: test for testSurfaceDestroyNormal.
195 * @tc.size : SmallTest
196 * @tc.type : Function
197 * @tc.level : Level 0
198 */
199 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceDestroyNormal, TestSize.Level0) {
200 OH_Drawing_GpuContextOptions options{true};
201 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
202 EXPECT_NE(gpuContext_, nullptr);
203 const int32_t width = 500;
204 const int32_t height = 500;
205 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
206 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
207 EXPECT_NE(surface_, nullptr);
208 OH_Drawing_SurfaceDestroy(surface_);
209 }
210
211 /*
212 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0201
213 * @tc.name: testSurfaceDestroyNull
214 * @tc.desc: test for testSurfaceDestroyNull.
215 * @tc.size : SmallTest
216 * @tc.type : Function
217 * @tc.level : Level 3
218 */
219 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceDestroyNull, TestSize.Level3) {
220 // free
221 OH_Drawing_SurfaceDestroy(nullptr);
222 // add assert
223 EXPECT_TRUE(true);
224 }
225
226 /*
227 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0300
228 * @tc.name: testSurfaceGetCanvasNormal
229 * @tc.desc: test for testSurfaceGetCanvasNormal.
230 * @tc.size : SmallTest
231 * @tc.type : Function
232 * @tc.level : Level 0
233 */
234 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceGetCanvasNormal, TestSize.Level0) {
235 OH_Drawing_GpuContextOptions options{true};
236 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
237 EXPECT_NE(gpuContext_, nullptr);
238 // 1. OH_Drawing_SurfaceCreateFromGpuContext
239 const int32_t width = 500;
240 const int32_t height = 500;
241 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
242 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
243 EXPECT_NE(surface_, nullptr);
244 //2.OH_Drawing_SurfaceGetCanvas, get the canvas object from the surface object,a pointer to the surface object
245 // and call the drawing interface
246 canvas_ = OH_Drawing_SurfaceGetCanvas(surface_);
247 // add assert
248 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_SUCCESS);
249 EXPECT_NE(canvas_, nullptr);
250 // 3. Free memory
251 OH_Drawing_SurfaceDestroy(surface_);
252 }
253
254 /*
255 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0301
256 * @tc.name: testSurfaceGetCanvasNull
257 * @tc.desc: test for testSurfaceGetCanvasNull.
258 * @tc.size : SmallTest
259 * @tc.type : Function
260 * @tc.level : Level 3
261 */
262 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceGetCanvasNull, TestSize.Level3) {
263 OH_Drawing_GpuContextOptions options{true};
264 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
265 EXPECT_NE(gpuContext_, nullptr);
266 // 1. OH_Drawing_SurfaceCreateFromGpuContext
267 const int32_t width = 500;
268 const int32_t height = 500;
269 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
270 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
271 EXPECT_NE(surface_, nullptr);
272 // 2. OH_Drawing_SurfaceGetCanvas with null parameter, check error code using OH_Drawing_ErrorCodeGet
273 canvas_ = OH_Drawing_SurfaceGetCanvas(nullptr);
274 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
275 OH_Drawing_SurfaceDestroy(surface_);
276 }
277
278 /*
279 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0302
280 * @tc.name: testSurfaceGetCanvasBoundary
281 * @tc.desc: test for testSurfaceGetCanvasBoundary.
282 * @tc.size : SmallTest
283 * @tc.type : Function
284 * @tc.level : Level 0
285 */
286 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceGetCanvasBoundary, TestSize.Level0) {
287 OH_Drawing_GpuContextOptions options{true};
288 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
289 EXPECT_NE(gpuContext_, nullptr);
290 // 1. OH_Drawing_SurfaceCreateFromGpuContext
291 const int32_t width = 4096;
292 const int32_t height = 2160;
293 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
294 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
295 EXPECT_NE(surface_, nullptr);
296 //2. OH_Drawing_SurfaceGetCanvas, get the canvas object from the surface object,a pointer to the surface object
297 // and call the drawing interface
298 canvas_ = OH_Drawing_SurfaceGetCanvas(surface_);
299 EXPECT_NE(canvas_, nullptr);
300 // 3. Free memory
301 OH_Drawing_SurfaceDestroy(surface_);
302 }
303
304 /*
305 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0400
306 * @tc.name: testSurfaceCreateOnScreenNormal
307 * @tc.desc: test for testSurfaceCreateOnScreenNormal.window
308 * @tc.size : SmallTest
309 * @tc.type : Function
310 * @tc.level : Level 0
311 */
312 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateOnScreenNormal, TestSize.Level0) {
313 // 1、OH_Drawing_SurfaceCreateOnScreen正常入参调用
314 gpuContext_ = OH_Drawing_GpuContextCreate();
315 EXPECT_NE(gpuContext_, nullptr);
316 const int32_t width = 4096;
317 const int32_t height = 2160;
318 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
319 // 1. OH_Drawing_SurfaceCreateOnScreen
320 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
321 canvas_ = OH_Drawing_SurfaceGetCanvas(surface_);
322 // 2. Free memory
323 OH_Drawing_SurfaceDestroy(surface_);
324 }
325
326 /*
327 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0401
328 * @tc.name: testSurfaceCreateOnScreenNull
329 * @tc.desc: test for testSurfaceCreateOnScreenNull.
330 * @tc.size : SmallTest
331 * @tc.type : Function
332 * @tc.level : Level 3
333 */
334 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateOnScreenNull, TestSize.Level3) {
335 // 1. OH_Drawing_SurfaceCreateOnScreen第一个参数传空
336 const int32_t width = 4096;
337 const int32_t height = 2160;
338 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
339 surface_ = OH_Drawing_SurfaceCreateOnScreen(nullptr, imageInfo, nullptr);
340 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
341 // 2. Free memory
342 OH_Drawing_SurfaceDestroy(surface_);
343 }
344
345 /*
346 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0402
347 * @tc.name: testSurfaceCreateOnScreenMultipleCalls
348 * @tc.desc: test for testSurfaceCreateOnScreenMultipleCalls.
349 * @tc.size : SmallTest
350 * @tc.type : Function
351 * @tc.level : Level 3
352 */
353 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateOnScreenMultipleCalls, TestSize.Level3) {
354 gpuContext_ = OH_Drawing_GpuContextCreate();
355 EXPECT_NE(gpuContext_, nullptr);
356 const int32_t width = 4096;
357 const int32_t height = 2160;
358 OH_Drawing_ColorFormat formats[] = {
359 COLOR_FORMAT_ALPHA_8, COLOR_FORMAT_RGB_565,
360 COLOR_FORMAT_ARGB_4444, COLOR_FORMAT_RGBA_8888, COLOR_FORMAT_BGRA_8888,
361 };
362 OH_Drawing_AlphaFormat alphaFormats[] = {
363 ALPHA_FORMAT_OPAQUE,
364 ALPHA_FORMAT_PREMUL,
365 ALPHA_FORMAT_UNPREMUL,
366 };
367
368 std::vector<OH_Drawing_Image_Info> imageInfos;
369 // Loop to create different imageInfo structures
370 for (int i = 0; i < 5; ++i) { // Loop through formats
371 for (int j = 0; j < 3; ++j) { // Loop through alphaFormats
372 OH_Drawing_Image_Info imageInfo = {width, height, formats[i], alphaFormats[j]};
373 imageInfos.push_back(imageInfo);
374 }
375 }
376
377 for (int index = 0; index < 10; ++index) {
378 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfos[index], nullptr);
379 EXPECT_EQ(surface_, nullptr);
380 // Free memory
381 OH_Drawing_SurfaceDestroy(surface_);
382 }
383 }
384
385 /*
386 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0500
387 * @tc.name: testSurfaceFlushNormal
388 * @tc.desc: test for testSurfaceFlushNormal.
389 * @tc.size : SmallTest
390 * @tc.type : Function
391 * @tc.level : Level 0
392 */
393 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceFlushNormal, TestSize.Level0) {
394 gpuContext_ = OH_Drawing_GpuContextCreate();
395 EXPECT_NE(gpuContext_, nullptr);
396 // 1. OH_Drawing_SurfaceCreateFromGpuContext
397 const int32_t width = 4096;
398 const int32_t height = 2160;
399 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
400 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
401 EXPECT_EQ(surface_, nullptr);
402 // 2. OH_Drawing_SurfaceFlush
403 auto result = OH_Drawing_SurfaceFlush(surface_);
404 EXPECT_EQ(result, OH_DRAWING_ERROR_INVALID_PARAMETER);
405 // 3. Free memory
406 OH_Drawing_SurfaceDestroy(surface_);
407 }
408
409 /*
410 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0501
411 * @tc.name: testSurfaceFlushNull
412 * @tc.desc: test for testSurfaceFlushNull.
413 * @tc.size : SmallTest
414 * @tc.type : Function
415 * @tc.level : Level 3
416 */
417 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceFlushNull, TestSize.Level3) {
418 OH_Drawing_ErrorCode errorCode = OH_Drawing_SurfaceFlush(nullptr);
419 EXPECT_EQ(errorCode, OH_DRAWING_ERROR_INVALID_PARAMETER);
420 }
421
422 /*
423 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0502
424 * @tc.name: testSurfaceFlushAbnormal
425 * @tc.desc: test for testSurfaceFlushAbnormal.
426 * @tc.size : SmallTest
427 * @tc.type : Function
428 * @tc.level : Level 3
429 */
430 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceFlushAbnormal, TestSize.Level3) {
431 OH_Drawing_GpuContextOptions options{true};
432 // 1. OH_Drawing_GPUContextCreateFromGL
433 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
434 EXPECT_NE(gpuContext_, nullptr);
435 const int32_t width = 4096;
436 const int32_t height = 2160;
437 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
438 // 2. OH_Drawing_SurfaceCreateOnScreen
439 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
440 // 3. OH_Drawing_SurfaceFlush
441 auto result = OH_Drawing_SurfaceFlush(surface_);
442 EXPECT_NE(result, OH_DRAWING_SUCCESS);
443 // 4. Free memory
444 OH_Drawing_SurfaceDestroy(surface_);
445 }
446
447 /*
448 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0503
449 * @tc.name: testSurfaceFlushMultipleCalls
450 * @tc.desc: test for testSurfaceFlushMultipleCalls.
451 * @tc.size : SmallTest
452 * @tc.type : Function
453 * @tc.level : Level 3
454 */
455 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceFlushMultipleCalls, TestSize.Level3) {
456 gpuContext_ = OH_Drawing_GpuContextCreate();
457 EXPECT_NE(gpuContext_, nullptr);
458 const int32_t width = 4096;
459 const int32_t height = 2160;
460 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
461 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
462
463 OH_Drawing_Canvas *canvas = OH_Drawing_CanvasCreate();
464 EXPECT_NE(canvas, nullptr);
465 OH_Drawing_Rect *rect = OH_Drawing_RectCreate(10, 100, 200, 300);
466 EXPECT_NE(rect, nullptr);
467
468 // 1. OH_Drawing_CanvasDrawRect-OH_Drawing_SurfaceFlush循环调用10次
469 for (int i = 0; i < 10; ++i) {
470 OH_Drawing_CanvasDrawRect(canvas, rect);
471 auto result1 = OH_Drawing_SurfaceFlush(surface_);
472 EXPECT_EQ(result1, OH_DRAWING_ERROR_INVALID_PARAMETER);
473 }
474
475 // 2. OH_Drawing_SurfaceFlush直接循环调用10次
476 for (int i = 0; i < 10; ++i) {
477 auto result2 = OH_Drawing_SurfaceFlush(surface_);
478 EXPECT_EQ(result2, OH_DRAWING_ERROR_INVALID_PARAMETER);
479 }
480 // 3. 创建不同surface(通过创建不同imageinfo)-OH_Drawing_SurfaceFlush循环调用10次
481 OH_Drawing_ColorFormat formats[] = {
482 COLOR_FORMAT_ALPHA_8, COLOR_FORMAT_RGB_565,
483 COLOR_FORMAT_ARGB_4444, COLOR_FORMAT_RGBA_8888, COLOR_FORMAT_BGRA_8888,
484 };
485 OH_Drawing_AlphaFormat alphaFormats[] = {
486 ALPHA_FORMAT_OPAQUE,
487 ALPHA_FORMAT_PREMUL,
488 ALPHA_FORMAT_UNPREMUL,
489 };
490
491 std::vector<OH_Drawing_Image_Info> imageInfos;
492 // Loop to create different imageInfo structures
493 for (int i = 0; i < 5; ++i) { // Loop through formats
494 for (int j = 0; j < 3; ++j) { // Loop through alphaFormats
495 OH_Drawing_Image_Info imageInfo = {width, height, formats[i], alphaFormats[j]};
496 imageInfos.push_back(imageInfo);
497 }
498 }
499
500 for (int index = 0; index < 10; ++index) {
501 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfos[index], nullptr);
502 auto result3 = OH_Drawing_SurfaceFlush(surface_);
503 EXPECT_EQ(result3, OH_DRAWING_ERROR_INVALID_PARAMETER);
504 }
505
506 OH_Drawing_SurfaceDestroy(surface_);
507 }
508
509 /*
510 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0504
511 * @tc.name: testSurfaceFlushTiming
512 * @tc.desc: test for testSurfaceFlushTiming.
513 * @tc.size : SmallTest
514 * @tc.type : Function
515 * @tc.level : Level 3
516 */
517 HWTEST_F(DrawingNativeSurFaceTest, testSurfaceFlushTiming, TestSize.Level3) {
518 OH_Drawing_GpuContextOptions options{true};
519 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
520 EXPECT_NE(gpuContext_, nullptr);
521 const int32_t width = 4096;
522 const int32_t height = 2160;
523 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888,
524 ALPHA_FORMAT_OPAQUE};
525
526 OH_Drawing_Canvas *canvas = OH_Drawing_CanvasCreate();
527 EXPECT_NE(canvas, nullptr);
528 OH_Drawing_Rect *rect = OH_Drawing_RectCreate(10, 100, 200, 300);
529 EXPECT_NE(rect, nullptr);
530 // 1. OH_Drawing_SurfaceCreateOnScreen-OH_Drawing_CanvasDrawRect-OH_Drawing_SurfaceFlush正常时序
531 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
532 OH_Drawing_CanvasDrawRect(canvas, rect);
533 OH_Drawing_SurfaceFlush(surface_);
534
535 // 2. OH_Drawing_SurfaceCreateOnScreen-OH_Drawing_SurfaceFlush创建surface之后直接调用flush
536 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
537 OH_Drawing_SurfaceFlush(surface_);
538
539 // 3. 创建两个canvas-创建两个surface-各自进行绘制-各自flush
540 OH_Drawing_Canvas *canvas1 = OH_Drawing_CanvasCreate();
541 OH_Drawing_Rect *rect1 = OH_Drawing_RectCreate(10, 100, 200, 300);
542 OH_Drawing_CanvasDrawRect(canvas1, rect1);
543 OH_Drawing_Surface* surface1 = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
544 OH_Drawing_Canvas *canvas2 = OH_Drawing_CanvasCreate();
545 OH_Drawing_Rect *rect2 = OH_Drawing_RectCreate(10, 100, 200, 300);
546 OH_Drawing_CanvasDrawRect(canvas2, rect2);
547 OH_Drawing_Surface* surface2 = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
548
549 OH_Drawing_SurfaceDestroy(surface_);
550 OH_Drawing_SurfaceDestroy(surface1);
551 OH_Drawing_SurfaceDestroy(surface2);
552 }
553
554 } // namespace Drawing
555 } // namespace Rosen
556 } // namespace OHOS