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 #include <gtest/gtest.h>
16 #include <iservice_registry.h>
17 #include <native_image.h>
18 #include <EGL/egl.h>
19 #include <EGL/eglext.h>
20 #include <sys/time.h>
21 #include <securec.h>
22 #include "graphic_common_c.h"
23 #include "surface_type.h"
24 #include "window.h"
25 #include "GLES/gl.h"
26 #include "buffer_log.h"
27 #include "surface.h"
28 #include "surface_image.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace std;
33
34 namespace OHOS::Rosen {
35 using GetPlatformDisplayExt = PFNEGLGETPLATFORMDISPLAYEXTPROC;
36 constexpr const char* EGL_EXT_PLATFORM_WAYLAND = "EGL_EXT_platform_wayland";
37 constexpr const char* EGL_KHR_PLATFORM_WAYLAND = "EGL_KHR_platform_wayland";
38 constexpr int32_t EGL_CONTEXT_CLIENT_VERSION_NUM = 2;
39 constexpr char CHARACTER_WHITESPACE = ' ';
40 constexpr const char* CHARACTER_STRING_WHITESPACE = " ";
41 constexpr const char* EGL_GET_PLATFORM_DISPLAY_EXT = "eglGetPlatformDisplayEXT";
42 constexpr int32_t MATRIX_SIZE = 16;
43
44 struct TEST_IMAGE {
45 int a;
46 bool b;
47 };
48
49 typedef struct OH_NativeImage_Tmp {
50 OHOS::sptr<OHOS::SurfaceImage> consumer;
51 OHOS::sptr<OHOS::IBufferProducer> producer;
52 OHOS::sptr<OHOS::Surface> pSurface = nullptr;
53 struct NativeWindow* nativeWindow = nullptr;
54 } OH_NativeImage_Tmp;
55
CheckEglExtension(const char * extensions,const char * extension)56 static bool CheckEglExtension(const char* extensions, const char* extension)
57 {
58 size_t extlen = strlen(extension);
59 const char* end = extensions + strlen(extensions);
60
61 while (extensions < end) {
62 size_t n = 0;
63 /* Skip whitespaces, if any */
64 if (*extensions == CHARACTER_WHITESPACE) {
65 extensions++;
66 continue;
67 }
68 n = strcspn(extensions, CHARACTER_STRING_WHITESPACE);
69 /* Compare strings */
70 if (n == extlen && strncmp(extension, extensions, n) == 0) {
71 return true; /* Found */
72 }
73 extensions += n;
74 }
75 /* Not found */
76 return false;
77 }
78
GetPlatformEglDisplay(EGLenum platform,void * nativeDisplay,const EGLint * attribList)79 static EGLDisplay GetPlatformEglDisplay(EGLenum platform, void* nativeDisplay, const EGLint* attribList)
80 {
81 static GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL;
82
83 if (!eglGetPlatformDisplayExt) {
84 const char* extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
85 if (extensions &&
86 (CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) ||
87 CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) {
88 eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT);
89 }
90 }
91
92 if (eglGetPlatformDisplayExt) {
93 return eglGetPlatformDisplayExt(platform, nativeDisplay, attribList);
94 }
95
96 return eglGetDisplay((EGLNativeDisplayType)nativeDisplay);
97 }
98
99 class NativeImageTest : public testing::Test {
100 public:
101 static void SetUpTestCase();
102 static void TearDownTestCase();
103
104 static void InitEglContext();
105 static void Deinit();
106
107 static inline OH_NativeImage* image = nullptr;
108 static inline OHNativeWindow* nativeWindow = nullptr;
109 static inline GLuint textureId = 0;
110 static inline GLuint textureId2 = 0;
111 static inline EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
112 static inline EGLContext eglContext_ = EGL_NO_CONTEXT;
113 static inline EGLConfig config_;
114 static void OnFrameAvailable(void *context);
115 };
116
OnFrameAvailable(void * context)117 void NativeImageTest::OnFrameAvailable(void *context)
118 {
119 (void) context;
120 cout << "OnFrameAvailable is called" << endl;
121 }
122
SetUpTestCase()123 void NativeImageTest::SetUpTestCase()
124 {
125 image = nullptr;
126 nativeWindow = nullptr;
127 glGenTextures(1, &textureId);
128 glGenTextures(1, &textureId2);
129 }
130
TearDownTestCase()131 void NativeImageTest::TearDownTestCase()
132 {
133 image = nullptr;
134 nativeWindow = nullptr;
135 Deinit();
136 }
137
InitEglContext()138 void NativeImageTest::InitEglContext()
139 {
140 if (eglContext_ != EGL_NO_DISPLAY) {
141 return;
142 }
143
144 BLOGI("Creating EGLContext!!!");
145 eglDisplay_ = GetPlatformEglDisplay(EGL_PLATFORM_OHOS_KHR, EGL_DEFAULT_DISPLAY, NULL);
146 if (eglDisplay_ == EGL_NO_DISPLAY) {
147 BLOGW("Failed to create EGLDisplay gl errno : %{public}x", eglGetError());
148 return;
149 }
150
151 EGLint major = 0;
152 EGLint minor = 0;
153 if (eglInitialize(eglDisplay_, &major, &minor) == EGL_FALSE) {
154 BLOGE("Failed to initialize EGLDisplay");
155 return;
156 }
157
158 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
159 BLOGE("Failed to bind OpenGL ES API");
160 return;
161 }
162
163 unsigned int ret;
164 EGLint count;
165 EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
166 EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
167
168 ret = eglChooseConfig(eglDisplay_, config_attribs, &config_, 1, &count);
169 if (!(ret && static_cast<unsigned int>(count) >= 1)) {
170 BLOGE("Failed to eglChooseConfig");
171 return;
172 }
173
174 static const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_NUM, EGL_NONE };
175
176 eglContext_ = eglCreateContext(eglDisplay_, config_, EGL_NO_CONTEXT, context_attribs);
177 if (eglContext_ == EGL_NO_CONTEXT) {
178 BLOGE("Failed to create egl context %{public}x", eglGetError());
179 return;
180 }
181
182 eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglContext_);
183
184 BLOGW("Create EGL context successfully, version %{public}d.%{public}d", major, minor);
185 }
186
Deinit()187 void NativeImageTest::Deinit()
188 {
189 if (eglDisplay_ == EGL_NO_DISPLAY) {
190 return;
191 }
192 eglDestroyContext(eglDisplay_, eglContext_);
193 eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
194 eglTerminate(eglDisplay_);
195 eglReleaseThread();
196
197 eglDisplay_ = EGL_NO_DISPLAY;
198 eglContext_ = EGL_NO_CONTEXT;
199 }
200
201 /*
202 * Function: OH_NativeImage_Create
203 * Type: Function
204 * Rank: Important(1)
205 * EnvConditions: N/A
206 * CaseDescription: 1. call OH_NativeImage_Create
207 * 2. check ret
208 * @tc.require: issueI5KG61
209 */
210 HWTEST_F(NativeImageTest, OHNativeImageCreate001, Function | MediumTest | Level1)
211 {
212 image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
213 ASSERT_NE(image, nullptr);
214 }
215
216 /*
217 * Function: OH_NativeImage_AcquireNativeWindow
218 * Type: Function
219 * Rank: Important(2)
220 * EnvConditions: N/A
221 * CaseDescription: 1. call OH_NativeImage_AcquireNativeWindow by abnormal input
222 * 2. check ret
223 * @tc.require: issueI5KG61
224 */
225 HWTEST_F(NativeImageTest, OHNativeImageAcquireNativeWindow001, Function | MediumTest | Level2)
226 {
227 nativeWindow = OH_NativeImage_AcquireNativeWindow(nullptr);
228 ASSERT_EQ(nativeWindow, nullptr);
229 }
230
231 /*
232 * Function: OH_NativeImage_AcquireNativeWindow
233 * Type: Function
234 * Rank: Important(1)
235 * EnvConditions: N/A
236 * CaseDescription: 1. call OH_NativeImage_AcquireNativeWindow
237 * 2. check ret
238 * @tc.require: issueI5KG61
239 */
240 HWTEST_F(NativeImageTest, OHNativeImageAcquireNativeWindow002, Function | MediumTest | Level1)
241 {
242 nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
243 ASSERT_NE(nativeWindow, nullptr);
244 }
245
246 /*
247 * Function: OH_NativeImage_AttachContext
248 * Type: Function
249 * Rank: Important(2)
250 * EnvConditions: N/A
251 * CaseDescription: 1. call OH_NativeImage_AttachContext by abnormal input
252 * 2. check ret
253 * @tc.require: issueI5KG61
254 */
255 HWTEST_F(NativeImageTest, OHNativeImageAttachContext001, Function | MediumTest | Level2)
256 {
257 int32_t ret = OH_NativeImage_AttachContext(nullptr, textureId);
258 ASSERT_NE(ret, SURFACE_ERROR_OK);
259 }
260
261 /*
262 * Function: OH_NativeImage_DetachContext
263 * Type: Function
264 * Rank: Important(2)
265 * EnvConditions: N/A
266 * CaseDescription: 1. call OH_NativeImage_DetachContext by abnormal input
267 * 2. check ret
268 * @tc.require: issueI5KG61
269 */
270 HWTEST_F(NativeImageTest, OHNativeImageDetachContext001, Function | MediumTest | Level2)
271 {
272 int32_t ret = OH_NativeImage_DetachContext(nullptr);
273 ASSERT_NE(ret, SURFACE_ERROR_OK);
274 }
275
276 /*
277 * Function: OH_NativeImage_DetachContext
278 * Type: Function
279 * Rank: Important(1)
280 * EnvConditions: N/A
281 * CaseDescription: 1. call OH_NativeImage_DetachContext
282 * 2. check ret
283 * @tc.require: issueI5KG61
284 */
285 HWTEST_F(NativeImageTest, OHNativeImageDetachContext002, Function | MediumTest | Level1)
286 {
287 int32_t ret = OH_NativeImage_DetachContext(image);
288 ASSERT_EQ(ret, SURFACE_ERROR_EGL_STATE_UNKONW);
289 }
290
291 /*
292 * Function: OH_NativeImage_DetachContext
293 * Type: Function
294 * Rank: Important(1)
295 * EnvConditions: N/A
296 * CaseDescription: 1. call OH_NativeImage_DetachContext
297 * 2. check ret
298 * @tc.require: issueI5KG61
299 */
300 HWTEST_F(NativeImageTest, OHNativeImageDetachContext003, Function | MediumTest | Level1)
301 {
302 InitEglContext();
303 int32_t ret = OH_NativeImage_DetachContext(image);
304 ASSERT_EQ(ret, SURFACE_ERROR_OK);
305 }
306
307 /*
308 * Function: OH_NativeImage_AttachContext
309 * Type: Function
310 * Rank: Important(1)
311 * EnvConditions: N/A
312 * CaseDescription: 1. call OH_NativeImage_AttachContext
313 * 2. check ret
314 * @tc.require: issueI5KG61
315 */
316 HWTEST_F(NativeImageTest, OHNativeImageAttachContext002, Function | MediumTest | Level1)
317 {
318 int32_t ret = OH_NativeImage_AttachContext(image, textureId);
319 ASSERT_EQ(ret, SURFACE_ERROR_OK);
320 }
321
322 /*
323 * Function: OH_NativeImage_UpdateSurfaceImage
324 * Type: Function
325 * Rank: Important(2)
326 * EnvConditions: N/A
327 * CaseDescription: 1. call OH_NativeImage_UpdateSurfaceImage by abnormal input
328 * 2. check ret
329 * @tc.require: issueI5KG61
330 */
331 HWTEST_F(NativeImageTest, OHNativeImageUpdateSurfaceImage001, Function | MediumTest | Level2)
332 {
333 int32_t ret = OH_NativeImage_UpdateSurfaceImage(nullptr);
334 ASSERT_NE(ret, SURFACE_ERROR_OK);
335 }
336
337 /*
338 * Function: OH_NativeImage_UpdateSurfaceImage
339 * Type: Function
340 * Rank: Important(1)
341 * EnvConditions: N/A
342 * CaseDescription: 1. call OH_NativeImage_UpdateSurfaceImage
343 * 2. check ret
344 * @tc.require: issueI5KG61
345 */
346 HWTEST_F(NativeImageTest, OHNativeImageUpdateSurfaceImage002, Function | MediumTest | Level1)
347 {
348 int32_t ret = OH_NativeImage_UpdateSurfaceImage(image);
349 ASSERT_NE(ret, SURFACE_ERROR_OK);
350 }
351
352 /*
353 * Function: OH_NativeImage_UpdateSurfaceImage
354 * Type: Function
355 * Rank: Important(1)
356 * EnvConditions: N/A
357 * CaseDescription: 1. call OH_NativeWindow_NativeWindowRequestBuffer
358 * 2. call OH_NativeWindow_NativeWindowFlushBuffer
359 * 3. OH_NativeImage_UpdateSurfaceImage
360 * 4. check ret
361 * @tc.require: issueI5KG61
362 */
363 HWTEST_F(NativeImageTest, OHNativeImageUpdateSurfaceImage003, Function | MediumTest | Level1)
364 {
365 int code = SET_USAGE;
366 uint64_t usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA;
367 int32_t ret = NativeWindowHandleOpt(nativeWindow, code, usage);
368 if (ret != GSERROR_OK) {
369 std::cout << "NativeWindowHandleOpt SET_USAGE faile" << std::endl;
370 }
371 code = SET_BUFFER_GEOMETRY;
372 int32_t width = 0x100;
373 int32_t height = 0x100;
374 ret = NativeWindowHandleOpt(nativeWindow, code, width, height);
375 if (ret != GSERROR_OK) {
376 std::cout << "NativeWindowHandleOpt SET_BUFFER_GEOMETRY failed" << std::endl;
377 }
378 code = SET_STRIDE;
379 int32_t stride = 0x8;
380 ret = NativeWindowHandleOpt(nativeWindow, code, stride);
381 if (ret != GSERROR_OK) {
382 std::cout << "NativeWindowHandleOpt SET_STRIDE failed" << std::endl;
383 }
384 code = SET_FORMAT;
385 int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
386 ret = NativeWindowHandleOpt(nativeWindow, code, format);
387 if (ret != GSERROR_OK) {
388 std::cout << "NativeWindowHandleOpt SET_FORMAT failed" << std::endl;
389 }
390
391 NativeWindowBuffer* nativeWindowBuffer = nullptr;
392 int fenceFd = -1;
393 struct Region *region = new Region();
394 struct Region::Rect *rect = new Region::Rect();
395 rect->x = 0x100;
396 rect->y = 0x100;
397 rect->w = 0x100;
398 rect->h = 0x100;
399 region->rects = rect;
400 for (int32_t i = 0; i < 2; i++) {
401 ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
402 ASSERT_EQ(ret, GSERROR_OK);
403 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
404 ASSERT_EQ(ret, GSERROR_OK);
405
406 ret = OH_NativeImage_UpdateSurfaceImage(image);
407 ASSERT_EQ(ret, SURFACE_ERROR_OK);
408 ASSERT_EQ(NativeWindowDisconnect(nativeWindow), SURFACE_ERROR_OK);
409 }
410 delete region;
411 }
412
413 /*
414 * Function: OH_NativeImage_UpdateSurfaceImage
415 * Type: Function
416 * Rank: Important(1)
417 * EnvConditions: N/A
418 * CaseDescription: 1. call OH_NativeImage_UpdateSurfaceImage
419 * 2. check ret
420 * @tc.require: issueI5KG61
421 */
422 HWTEST_F(NativeImageTest, OHNativeImageUpdateSurfaceImage006, Function | MediumTest | Level1)
423 {
424 OH_NativeImage* imageNew = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
425 ASSERT_NE(imageNew, nullptr);
426 memset_s(imageNew, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
427 int32_t ret = OH_NativeImage_UpdateSurfaceImage(imageNew);
428 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
429
430 OH_NativeImage_Destroy(&imageNew);
431 }
432
433 /*
434 * Function: OH_NativeImage_GetTimestamp
435 * Type: Function
436 * Rank: Important(2)
437 * EnvConditions: N/A
438 * CaseDescription: 1. call OH_NativeImage_GetTimestamp by abnormal input
439 * 2. check ret
440 * @tc.require: issueI5KG61
441 */
442 HWTEST_F(NativeImageTest, OHNativeImageGetTimestamp001, Function | MediumTest | Level2)
443 {
444 int64_t timeStamp = OH_NativeImage_GetTimestamp(nullptr);
445 ASSERT_EQ(timeStamp, -1);
446 }
447
448 /*
449 * Function: OH_NativeImage_GetTimestamp
450 * Type: Function
451 * Rank: Important(1)
452 * EnvConditions: N/A
453 * CaseDescription: 1. call OH_NativeImage_GetTimestamp
454 * 2. check ret
455 * @tc.require: issueI5KG61
456 */
457 HWTEST_F(NativeImageTest, OHNativeImageGetTimestamp002, Function | MediumTest | Level1)
458 {
459 int64_t timeStamp = OH_NativeImage_GetTimestamp(image);
460 ASSERT_NE(timeStamp, SURFACE_ERROR_ERROR);
461 }
462
463 /*
464 * Function: OH_NativeImage_GetTimestamp
465 * Type: Function
466 * Rank: Important(1)
467 * EnvConditions: N/A
468 * CaseDescription: 1. call OH_NativeImage_GetTimestamp
469 * 2. check ret
470 * @tc.require: issueI5KG61
471 */
472 HWTEST_F(NativeImageTest, OHNativeImageGetTimestamp003, Function | MediumTest | Level1)
473 {
474 OH_NativeImage* imageNew = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
475 ASSERT_NE(imageNew, nullptr);
476 memset_s(imageNew, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
477 int32_t ret = OH_NativeImage_GetTimestamp(imageNew);
478 ASSERT_EQ(ret, -1);
479
480 OH_NativeImage_Destroy(&imageNew);
481 }
482
483 /*
484 * Function: OH_NativeImage_GetTransformMatrix
485 * Type: Function
486 * Rank: Important(2)
487 * EnvConditions: N/A
488 * CaseDescription: 1. call OH_NativeImage_GetTransformMatrix by abnormal input
489 * 2. check ret
490 * @tc.require: issueI5KG61
491 */
492 HWTEST_F(NativeImageTest, OHNativeImageGetTransformMatrix001, Function | MediumTest | Level2)
493 {
494 float matrix[MATRIX_SIZE];
495 int32_t ret = OH_NativeImage_GetTransformMatrix(nullptr, matrix);
496 ASSERT_NE(ret, SURFACE_ERROR_OK);
497 }
498
499 /*
500 * Function: OH_NativeImage_GetTransformMatrix
501 * Type: Function
502 * Rank: Important(1)
503 * EnvConditions: N/A
504 * CaseDescription: 1. call OH_NativeImage_GetTransformMatrix
505 * 2. check ret
506 * @tc.require: issueI5KG61
507 */
508 HWTEST_F(NativeImageTest, OHNativeImageGetTransformMatrix002, Function | MediumTest | Level1)
509 {
510 float matrix[MATRIX_SIZE];
511 int32_t ret = OH_NativeImage_GetTransformMatrix(image, matrix);
512 ASSERT_EQ(ret, SURFACE_ERROR_OK);
513 }
514
CheckMatricIsSame(float matrixOld[MATRIX_SIZE],float matrixNew[MATRIX_SIZE])515 bool CheckMatricIsSame(float matrixOld[MATRIX_SIZE], float matrixNew[MATRIX_SIZE])
516 {
517 for (int32_t i = 0; i < MATRIX_SIZE; i++) {
518 if (fabs(matrixOld[i] - matrixNew[i]) > 1e-6) {
519 return false;
520 }
521 }
522 return true;
523 }
524
525 int32_t testType[] = {
526 GraphicTransformType::GRAPHIC_ROTATE_NONE, GraphicTransformType::GRAPHIC_ROTATE_90,
527 GraphicTransformType::GRAPHIC_ROTATE_180, GraphicTransformType::GRAPHIC_ROTATE_270,
528 GraphicTransformType::GRAPHIC_FLIP_H, GraphicTransformType::GRAPHIC_FLIP_V,
529 GraphicTransformType::GRAPHIC_FLIP_H_ROT90, GraphicTransformType::GRAPHIC_FLIP_V_ROT90,
530 GraphicTransformType::GRAPHIC_FLIP_H_ROT180, GraphicTransformType::GRAPHIC_FLIP_V_ROT180,
531 GraphicTransformType::GRAPHIC_FLIP_H_ROT270, GraphicTransformType::GRAPHIC_FLIP_V_ROT270,
532 };
533 float matrixArr[][MATRIX_SIZE] = {
534 {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
535 {0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
536 {-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
537 {0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
538 {-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
539 {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
540 {0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
541 {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
542 {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
543 {-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
544 {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
545 {0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
546 };
547
548 /*
549 * Function: OH_NativeImage_GetTransformMatrix
550 * Type: Function
551 * Rank: Important(1)
552 * EnvConditions: N/A
553 * CaseDescription: 1. call OH_NativeImage_GetTransformMatrix
554 * 2. check ret
555 * @tc.require: issueI5KG61
556 */
557 HWTEST_F(NativeImageTest, OHNativeImageGetTransformMatrix003, Function | MediumTest | Level1)
558 {
559 if (image == nullptr) {
560 image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
561 ASSERT_NE(image, nullptr);
562 }
563
564 if (nativeWindow == nullptr) {
565 nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
566 ASSERT_NE(nativeWindow, nullptr);
567 }
568
569 OH_OnFrameAvailableListener listener;
570 listener.context = this;
571 listener.onFrameAvailable = nullptr;
572 int32_t ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener);
573 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
574
575 listener.onFrameAvailable = NativeImageTest::OnFrameAvailable;
576 ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener);
577 ASSERT_EQ(ret, GSERROR_OK);
578
579 NativeWindowBuffer* nativeWindowBuffer = nullptr;
580 int fenceFd = -1;
581 struct Region *region = new Region();
582 struct Region::Rect *rect = new Region::Rect();
583
584 for (int32_t i = 0; i < sizeof(testType) / sizeof(int32_t); i++) {
585 int code = SET_TRANSFORM;
586 ret = NativeWindowHandleOpt(nativeWindow, code, testType[i]);
587 ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
588 ASSERT_EQ(ret, GSERROR_OK);
589
590 rect->x = 0x100;
591 rect->y = 0x100;
592 rect->w = 0x100;
593 rect->h = 0x100;
594 region->rects = rect;
595 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
596 ASSERT_EQ(ret, GSERROR_OK);
597
598 ret = OH_NativeImage_UpdateSurfaceImage(image);
599 ASSERT_EQ(ret, SURFACE_ERROR_OK);
600
601 float matrix[16];
602 int32_t ret = OH_NativeImage_GetTransformMatrix(image, matrix);
603 ASSERT_EQ(ret, SURFACE_ERROR_OK);
604
605 bool bRet = CheckMatricIsSame(matrix, matrixArr[i]);
606 ASSERT_EQ(bRet, true);
607 }
608 delete region;
609 }
610
611 float matrixArrV2[][MATRIX_SIZE] = {
612 {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1}, // 单位矩阵
613 {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, // 90度矩阵
614 {-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1}, // 180度矩阵
615 {0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1}, // 270度矩阵
616 {-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1}, // 水平翻转
617 {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, // 垂直翻转
618 {0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1}, // 水平*90
619 {0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1}, // 垂直*90
620 {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, // 水平*180
621 {-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1}, // 垂直*180
622 {0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1}, // 水平*270
623 {0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1}, // 垂直*270
624 };
625
626 /*
627 * Function: OH_NativeImage_GetTransformMatrix
628 * Type: Function
629 * Rank: Important(1)
630 * EnvConditions: N/A
631 * CaseDescription: 1. call OH_NativeImage_GetTransformMatrix
632 * 2. check ret
633 * @tc.require: issueI5KG61
634 */
635 HWTEST_F(NativeImageTest, OHNativeImageGetTransformMatrix004, Function | MediumTest | Level1)
636 {
637 if (image == nullptr) {
638 image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
639 ASSERT_NE(image, nullptr);
640 }
641
642 if (nativeWindow == nullptr) {
643 nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
644 ASSERT_NE(nativeWindow, nullptr);
645 }
646
647 OH_OnFrameAvailableListener listener;
648 listener.context = this;
649 listener.onFrameAvailable = NativeImageTest::OnFrameAvailable;
650 int32_t ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener);
651 ASSERT_EQ(ret, GSERROR_OK);
652
653 NativeWindowBuffer* nativeWindowBuffer = nullptr;
654 int fenceFd = -1;
655 struct Region *region = new Region();
656 struct Region::Rect *rect = new Region::Rect();
657
658 for (int32_t i = 0; i < sizeof(testType) / sizeof(int32_t); i++) {
659 int code = SET_TRANSFORM;
660 ret = NativeWindowHandleOpt(nativeWindow, code, testType[i]);
661 ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
662 ASSERT_EQ(ret, GSERROR_OK);
663
664 rect->x = 0x100;
665 rect->y = 0x100;
666 rect->w = 0x100;
667 rect->h = 0x100;
668 region->rects = rect;
669 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
670 ASSERT_EQ(ret, GSERROR_OK);
671
672 ret = OH_NativeImage_UpdateSurfaceImage(image);
673 ASSERT_EQ(ret, SURFACE_ERROR_OK);
674
675 float matrix[16];
676 int32_t ret = OH_NativeImage_GetTransformMatrixV2(image, matrix);
677 ASSERT_EQ(ret, SURFACE_ERROR_OK);
678
679 bool bRet = CheckMatricIsSame(matrix, matrixArrV2[i]);
680 ASSERT_EQ(bRet, true);
681 }
682 delete region;
683 }
684
685 /*
686 * Function: OH_NativeImage_GetTransformMatrix
687 * Type: Function
688 * Rank: Important(1)
689 * EnvConditions: N/A
690 * CaseDescription: 1. call OH_NativeImage_GetTransformMatrix
691 * 2. check ret
692 * @tc.require: issueI5KG61
693 */
694 HWTEST_F(NativeImageTest, OHNativeImageGetTransformMatrix005, Function | MediumTest | Level1)
695 {
696 OH_NativeImage* imageNew = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
697 ASSERT_NE(imageNew, nullptr);
698 int32_t ret = OH_NativeImage_GetTransformMatrixV2(nullptr, nullptr);
699 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
700 ret = OH_NativeImage_GetTransformMatrix(imageNew, nullptr);
701 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
702 ret = OH_NativeImage_GetTransformMatrixV2(imageNew, nullptr);
703 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
704 float matrix[16];
705 memset_s(imageNew, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
706 ret = OH_NativeImage_GetTransformMatrix(imageNew, matrix);
707 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
708 ret = OH_NativeImage_GetTransformMatrixV2(imageNew, matrix);
709 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
710
711 OH_NativeImage_Destroy(&imageNew);
712 }
713
714 /*
715 * Function: OH_NativeImage_AttachContext
716 * Type: Function
717 * Rank: Important(1)
718 * EnvConditions: N/A
719 * CaseDescription: 1. call OH_NativeImage_AttachContext with another texture
720 * 2. check ret
721 * @tc.require: issueI5KG61
722 */
723 HWTEST_F(NativeImageTest, OHNativeImageAttachContext003, Function | MediumTest | Level1)
724 {
725 int32_t ret = OH_NativeImage_AttachContext(image, textureId2);
726 ASSERT_EQ(ret, SURFACE_ERROR_OK);
727 }
728
729 /*
730 * Function: OH_NativeImage_UpdateSurfaceImage
731 * Type: Function
732 * Rank: Important(1)
733 * EnvConditions: N/A
734 * CaseDescription: 1. call OH_NativeWindow_NativeWindowRequestBuffer
735 * 2. call OH_NativeWindow_NativeWindowFlushBuffer
736 * 3. OH_NativeImage_UpdateSurfaceImage after the bound OPENGL ES texture changed
737 * 4. check ret
738 * @tc.require: issueI5KG61
739 */
740 HWTEST_F(NativeImageTest, OHNativeImageUpdateSurfaceImage004, Function | MediumTest | Level1)
741 {
742 NativeWindowBuffer* nativeWindowBuffer = nullptr;
743 int fenceFd = -1;
744 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
745 ASSERT_EQ(ret, GSERROR_OK);
746
747 struct Region *region = new Region();
748 struct Region::Rect *rect = new Region::Rect();
749 rect->x = 0x100;
750 rect->y = 0x100;
751 rect->w = 0x100;
752 rect->h = 0x100;
753 region->rects = rect;
754 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
755 ASSERT_EQ(ret, GSERROR_OK);
756 delete region;
757
758 ret = OH_NativeImage_UpdateSurfaceImage(image);
759 ASSERT_EQ(ret, SURFACE_ERROR_OK);
760 }
761
762 /*
763 * Function: OH_NativeImage_DetachContext
764 * Type: Function
765 * Rank: Important(1)
766 * EnvConditions: N/A
767 * CaseDescription: 1. call OH_NativeImage_DetachContext
768 * 2. check ret
769 * @tc.require: issueI5KG61
770 */
771 HWTEST_F(NativeImageTest, OHNativeImageDetachContext004, Function | MediumTest | Level1)
772 {
773 int32_t ret = OH_NativeImage_DetachContext(image);
774 ASSERT_EQ(ret, SURFACE_ERROR_OK);
775 }
776
777 /*
778 * Function: OH_NativeImage_AttachContext
779 * Type: Function
780 * Rank: Important(1)
781 * EnvConditions: N/A
782 * CaseDescription: 1. call OH_NativeImage_AttachContext after OH_NativeImage_DetachContext
783 * 2. check ret
784 * @tc.require: issueI5KG61
785 */
786 HWTEST_F(NativeImageTest, OHNativeImageAttachContext004, Function | MediumTest | Level1)
787 {
788 int32_t ret = OH_NativeImage_AttachContext(image, textureId2);
789 ASSERT_EQ(ret, SURFACE_ERROR_OK);
790 }
791
792 /*
793 * Function: OH_NativeImage_AttachContext
794 * Type: Function
795 * Rank: Important(1)
796 * EnvConditions: N/A
797 * CaseDescription: 1. call OH_NativeImage_AttachContext after OH_NativeImage_DetachContext
798 * 2. check ret
799 * @tc.require: issueI5KG61
800 */
801 HWTEST_F(NativeImageTest, OHNativeImageAttachContext005, Function | MediumTest | Level1)
802 {
803 OH_NativeImage* imageNew = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
804 ASSERT_NE(imageNew, nullptr);
805 memset_s(imageNew, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
806 int32_t ret = OH_NativeImage_AttachContext(imageNew, 0);
807 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
808 ret = OH_NativeImage_DetachContext(imageNew);
809 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
810
811 OH_NativeImage_Destroy(&imageNew);
812 }
813
814 /*
815 * Function: OH_NativeImage_UpdateSurfaceImage
816 * Type: Function
817 * Rank: Important(1)
818 * EnvConditions: N/A
819 * CaseDescription: 1. call OH_NativeWindow_NativeWindowRequestBuffer
820 * 2. call OH_NativeWindow_NativeWindowFlushBuffer
821 * 3. OH_NativeImage_UpdateSurfaceImage again
822 * 4. check ret
823 * @tc.require: issueI5KG61
824 */
825 HWTEST_F(NativeImageTest, OHNativeImageUpdateSurfaceImage005, Function | MediumTest | Level1)
826 {
827 NativeWindowBuffer* nativeWindowBuffer = nullptr;
828 int fenceFd = -1;
829 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
830 ASSERT_EQ(ret, GSERROR_OK);
831
832 struct Region *region = new Region();
833 struct Region::Rect *rect = new Region::Rect();
834 rect->x = 0x100;
835 rect->y = 0x100;
836 rect->w = 0x100;
837 rect->h = 0x100;
838 region->rects = rect;
839 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
840 ASSERT_EQ(ret, GSERROR_OK);
841 delete region;
842
843 ret = OH_NativeImage_UpdateSurfaceImage(image);
844 ASSERT_EQ(ret, SURFACE_ERROR_OK);
845 }
846
847 /*
848 * Function: OH_NativeImage_GetSurfaceId
849 * Type: Function
850 * Rank: Important(1)
851 * EnvConditions: N/A
852 * CaseDescription: 1. create image
853 * 2. GetSurfaceId
854 * 2. check ret
855 * @tc.require: issueI86VH2
856 */
857 HWTEST_F(NativeImageTest, OHNativeImageGetSurfaceId001, Function | MediumTest | Level1)
858 {
859 if (image == nullptr) {
860 image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
861 ASSERT_NE(image, nullptr);
862 }
863
864 uint64_t surfaceId;
865 int32_t ret = OH_NativeImage_GetSurfaceId(image, &surfaceId);
866 ASSERT_EQ(ret, SURFACE_ERROR_OK);
867 }
868
869 /*
870 * Function: OH_NativeImage_GetSurfaceId
871 * Type: Function
872 * Rank: Important(1)
873 * EnvConditions: N/A
874 * CaseDescription: 1. create image
875 * 2. GetSurfaceId
876 * 2. check ret
877 * @tc.require: issueI86VH2
878 */
879 HWTEST_F(NativeImageTest, OHNativeImageGetSurfaceId002, Function | MediumTest | Level1)
880 {
881 uint64_t surfaceId;
882 int32_t ret = OH_NativeImage_GetSurfaceId(nullptr, &surfaceId);
883 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
884
885 OH_NativeImage* imageNew = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
886 ASSERT_NE(imageNew, nullptr);
887
888 ret = OH_NativeImage_GetSurfaceId(imageNew, nullptr);
889 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
890
891 memset_s(imageNew, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
892 ret = OH_NativeImage_GetSurfaceId(imageNew, &surfaceId);
893 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
894
895 OH_NativeImage_Destroy(&imageNew);
896 }
897
898 /*
899 * Function: OH_NativeImage_SetOnFrameAvailableListener
900 * Type: Function
901 * Rank: Important(1)
902 * EnvConditions: N/A
903 * CaseDescription: 1. check image and nativeWindow
904 * 2. call OH_NativeImage_SetOnFrameAvailableListener
905 * 3. call OH_NativeWindow_NativeWindowFlushBuffer
906 * 4. check OnFrameAvailable is called
907 * @tc.require: issueI86VH2
908 */
909 HWTEST_F(NativeImageTest, OHNativeImageSetOnFrameAvailableListener001, Function | MediumTest | Level1)
910 {
911 if (image == nullptr) {
912 image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
913 ASSERT_NE(image, nullptr);
914 }
915
916 if (nativeWindow == nullptr) {
917 nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
918 ASSERT_NE(nativeWindow, nullptr);
919 }
920
921 OH_OnFrameAvailableListener listener;
922 listener.context = this;
923 listener.onFrameAvailable = NativeImageTest::OnFrameAvailable;
924 int32_t ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener);
925 ASSERT_EQ(ret, GSERROR_OK);
926
927 NativeWindowBuffer* nativeWindowBuffer = nullptr;
928 int fenceFd = -1;
929 ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
930 ASSERT_EQ(ret, GSERROR_OK);
931
932 struct Region *region = new Region();
933 struct Region::Rect *rect = new Region::Rect();
934 rect->x = 0x100;
935 rect->y = 0x100;
936 rect->w = 0x100;
937 rect->h = 0x100;
938 region->rects = rect;
939 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
940 ASSERT_EQ(ret, GSERROR_OK);
941 delete region;
942
943 ret = OH_NativeImage_UpdateSurfaceImage(image);
944 ASSERT_EQ(ret, SURFACE_ERROR_OK);
945 }
946
947 /*
948 * Function: OH_NativeImage_SetOnFrameAvailableListener
949 * Type: Function
950 * Rank: Important(1)
951 * EnvConditions: N/A
952 * CaseDescription: 1. check image and nativeWindow
953 * @tc.require: issueI86VH2
954 */
955 HWTEST_F(NativeImageTest, OHNativeImageSetOnFrameAvailableListener002, Function | MediumTest | Level1)
956 {
957 OH_OnFrameAvailableListener listener;
958 int32_t ret = OH_NativeImage_SetOnFrameAvailableListener(nullptr, listener);
959 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
960
961 OH_NativeImage* imageNew = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
962 ASSERT_NE(imageNew, nullptr);
963 memset_s(imageNew, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
964 ret = OH_NativeImage_SetOnFrameAvailableListener(imageNew, listener);
965 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
966
967 OH_NativeImage_Destroy(&imageNew);
968 }
969
970 /*
971 * Function: OH_NativeImage_UnsetOnFrameAvailableListener
972 * Type: Function
973 * Rank: Important(1)
974 * EnvConditions: N/A
975 * CaseDescription: 1. call OH_NativeImage_UnsetOnFrameAvailableListener
976 * 2. check ret
977 * @tc.require: issueI86VH2
978 */
979 HWTEST_F(NativeImageTest, OHNativeImageUnsetOnFrameAvailableListener001, Function | MediumTest | Level1)
980 {
981 int32_t ret = OH_NativeImage_UnsetOnFrameAvailableListener(image);
982 ASSERT_EQ(ret, SURFACE_ERROR_OK);
983 }
984
985 /*
986 * Function: OH_NativeImage_UnsetOnFrameAvailableListener
987 * Type: Function
988 * Rank: Important(1)
989 * EnvConditions: N/A
990 * CaseDescription: 1. call OH_NativeImage_UnsetOnFrameAvailableListener
991 * 2. check ret
992 * @tc.require: issueI86VH2
993 */
994 HWTEST_F(NativeImageTest, OHNativeImageUnsetOnFrameAvailableListener002, Function | MediumTest | Level1)
995 {
996 int32_t ret = OH_NativeImage_UnsetOnFrameAvailableListener(nullptr);
997 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
998
999 OH_NativeImage* imageNew = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
1000 ASSERT_NE(imageNew, nullptr);
1001 memset_s(imageNew, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
1002 ret = OH_NativeImage_UnsetOnFrameAvailableListener(imageNew);
1003 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1004
1005 OH_NativeImage_Destroy(&imageNew);
1006 }
1007
1008 /*
1009 * Function: OH_NativeImage_Destroy
1010 * Type: Function
1011 * Rank: Important(2)
1012 * EnvConditions: N/A
1013 * CaseDescription: 1. call OH_NativeImage_Destroy by abnormal input
1014 * 2. check ret
1015 * @tc.require: issueI5KG61
1016 */
1017 HWTEST_F(NativeImageTest, OHNativeImageDestroy001, Function | MediumTest | Level2)
1018 {
1019 OH_NativeImage_Destroy(nullptr);
1020 ASSERT_NE(image, nullptr);
1021 }
1022
1023 /*
1024 * Function: OH_NativeImage_Destroy
1025 * Type: Function
1026 * Rank: Important(1)
1027 * EnvConditions: N/A
1028 * CaseDescription: 1. call OH_NativeImage_Destroy
1029 * 2. check ret
1030 * @tc.require: issueI5KG61
1031 */
1032 HWTEST_F(NativeImageTest, OHNativeImageDestroy002, Function | MediumTest | Level1)
1033 {
1034 OH_NativeImage_Destroy(&image);
1035 ASSERT_EQ(image, nullptr);
1036 }
1037
1038 /*
1039 * Function: OH_NativeImage_AcquireNativeWindowBuffer
1040 * Type: Function
1041 * Rank: Important(1)
1042 * EnvConditions: N/A
1043 * CaseDescription: 1. call OH_NativeImage_AcquireNativeWindowBuffer
1044 * 2. check ret
1045 * 3. call OH_NativeImage_ReleaseNativeWindowBuffer
1046 * 4. check ret
1047 * @tc.require: issueI5KG61
1048 */
1049 HWTEST_F(NativeImageTest, OHNativeImageAcquireNativeWindowBuffer001, Function | MediumTest | Level1)
1050 {
1051 int32_t ret = OH_NativeImage_AcquireNativeWindowBuffer(nullptr, nullptr, nullptr);
1052 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1053 OH_NativeImage* newImage1 = OH_NativeImage_Create(0, 0);
1054 ret = OH_NativeImage_AcquireNativeWindowBuffer(newImage1, nullptr, nullptr);
1055 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1056 NativeWindowBuffer* nativeWindowBuffer = nullptr;
1057 ret = OH_NativeImage_AcquireNativeWindowBuffer(newImage1, &nativeWindowBuffer, nullptr);
1058 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1059
1060 ret = OH_NativeImage_ReleaseNativeWindowBuffer(nullptr, nullptr, 0);
1061 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1062 ret = OH_NativeImage_ReleaseNativeWindowBuffer(newImage1, nullptr, 0);
1063 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1064
1065 int32_t fenceFd;
1066 memset_s(newImage1, sizeof(OH_NativeImage_Tmp), 0, sizeof(OH_NativeImage_Tmp));
1067 ret = OH_NativeImage_AcquireNativeWindowBuffer(newImage1, &nativeWindowBuffer, &fenceFd);
1068 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1069
1070 NativeWindowBuffer nativeWindowBufferNew;
1071 ret = OH_NativeImage_ReleaseNativeWindowBuffer(newImage1, &nativeWindowBufferNew, 0);
1072 ASSERT_EQ(ret, SURFACE_ERROR_INVALID_PARAM);
1073
1074 OH_NativeImage_Destroy(&newImage1);
1075 }
1076
1077 /*
1078 * Function: OH_NativeImage_AcquireNativeWindowBuffer
1079 * Type: Function
1080 * Rank: Important(1)
1081 * EnvConditions: N/A
1082 * CaseDescription: 1. call OH_NativeImage_AcquireNativeWindowBuffer
1083 * 2. check ret
1084 * 3. call OH_NativeImage_ReleaseNativeWindowBuffer
1085 * 4. check ret
1086 * @tc.require: issueI5KG61
1087 */
1088 HWTEST_F(NativeImageTest, OHNativeImageAcquireNativeWindowBuffer002, Function | MediumTest | Level1)
1089 {
1090 OH_NativeImage* newImage = OH_NativeImage_Create(0, 0);
1091 ASSERT_NE(newImage, nullptr);
1092 OHNativeWindow* newNativeWindow = OH_NativeImage_AcquireNativeWindow(newImage);
1093 ASSERT_NE(newNativeWindow, nullptr);
1094
1095 int32_t code = SET_BUFFER_GEOMETRY;
1096 int32_t width = 0x100;
1097 int32_t height = 0x100;
1098 int32_t ret = NativeWindowHandleOpt(newNativeWindow, code, width, height);
1099 ASSERT_EQ(ret, GSERROR_OK);
1100
1101 NativeWindowBuffer* nativeWindowBuffer = nullptr;
1102 int fenceFd = -1;
1103 struct Region *region = new Region();
1104 struct Region::Rect *rect = new Region::Rect();
1105 rect->x = 0x100;
1106 rect->y = 0x100;
1107 rect->w = 0x100;
1108 rect->h = 0x100;
1109 region->rects = rect;
1110 for (int32_t i = 0; i < 100; i++) {
1111 ret = OH_NativeWindow_NativeWindowRequestBuffer(newNativeWindow, &nativeWindowBuffer, &fenceFd);
1112 ASSERT_EQ(ret, GSERROR_OK);
1113
1114 ret = OH_NativeWindow_NativeWindowFlushBuffer(newNativeWindow, nativeWindowBuffer, fenceFd, *region);
1115 ASSERT_EQ(ret, GSERROR_OK);
1116
1117 nativeWindowBuffer = nullptr;
1118 ret = OH_NativeImage_AcquireNativeWindowBuffer(newImage, &nativeWindowBuffer, &fenceFd);
1119 ASSERT_EQ(ret, GSERROR_OK);
1120 ASSERT_NE(nativeWindowBuffer, nullptr);
1121
1122 ret = OH_NativeImage_ReleaseNativeWindowBuffer(newImage, nativeWindowBuffer, fenceFd);
1123 ASSERT_EQ(ret, GSERROR_OK);
1124 }
1125
1126 delete rect;
1127 delete region;
1128 OH_NativeImage_Destroy(&newImage);
1129 }
1130
1131 /*
1132 * Function: OH_NativeImage_AcquireNativeWindowBuffer
1133 * Type: Function
1134 * Rank: Important(1)
1135 * EnvConditions: N/A
1136 * CaseDescription: 1. call OH_NativeImage_AcquireNativeWindowBuffer
1137 * 2. check ret
1138 * 3. call OH_NativeImage_ReleaseNativeWindowBuffer
1139 * 4. check ret
1140 * @tc.require: issueI5KG61
1141 */
1142 HWTEST_F(NativeImageTest, OHNativeImageAcquireNativeWindowBuffer003, Function | MediumTest | Level1)
1143 {
1144 OH_NativeImage* newImage = OH_NativeImage_Create(0, 0);
1145 ASSERT_NE(newImage, nullptr);
1146 OHNativeWindow* newNativeWindow = OH_NativeImage_AcquireNativeWindow(newImage);
1147 ASSERT_NE(newNativeWindow, nullptr);
1148
1149 int32_t code = SET_BUFFER_GEOMETRY;
1150 int32_t width = 0x100;
1151 int32_t height = 0x100;
1152 int32_t ret = NativeWindowHandleOpt(newNativeWindow, code, width, height);
1153 ASSERT_EQ(ret, GSERROR_OK);
1154 struct Region *region = new Region();
1155 struct Region::Rect *rect = new Region::Rect();
1156 rect->x = 0x100;
1157 rect->y = 0x100;
1158 rect->w = 0x100;
1159 rect->h = 0x100;
1160 region->rects = rect;
1161
1162 NativeWindowBuffer* nativeWindowBuffer = nullptr;
1163 int fenceFd = -1;
1164
1165 ret = OH_NativeImage_AcquireNativeWindowBuffer(newImage, &nativeWindowBuffer, &fenceFd);
1166 ASSERT_EQ(ret, SURFACE_ERROR_NO_BUFFER);
1167
1168 ret = OH_NativeWindow_NativeWindowRequestBuffer(newNativeWindow, &nativeWindowBuffer, &fenceFd);
1169 ASSERT_EQ(ret, GSERROR_OK);
1170
1171 ret = OH_NativeWindow_NativeWindowFlushBuffer(newNativeWindow, nativeWindowBuffer, fenceFd, *region);
1172 ASSERT_EQ(ret, GSERROR_OK);
1173
1174 ret = OH_NativeImage_ReleaseNativeWindowBuffer(newImage, nativeWindowBuffer, fenceFd);
1175 ASSERT_EQ(ret, SURFACE_ERROR_BUFFER_STATE_INVALID);
1176
1177 OH_NativeImage* newImage1 = OH_NativeImage_Create(0, 0);
1178 ASSERT_NE(newImage1, nullptr);
1179 OHNativeWindow* newNativeWindow1 = OH_NativeImage_AcquireNativeWindow(newImage1);
1180 ASSERT_NE(newNativeWindow1, nullptr);
1181 code = SET_BUFFER_GEOMETRY;
1182 width = 0x100;
1183 height = 0x100;
1184 ret = NativeWindowHandleOpt(newNativeWindow1, code, width, height);
1185 ASSERT_EQ(ret, GSERROR_OK);
1186
1187 NativeWindowBuffer* nativeWindowBuffer1 = nullptr;
1188 ret = OH_NativeWindow_NativeWindowRequestBuffer(newNativeWindow1, &nativeWindowBuffer1, &fenceFd);
1189 ASSERT_EQ(ret, GSERROR_OK);
1190 ret = OH_NativeWindow_NativeWindowFlushBuffer(newNativeWindow1, nativeWindowBuffer1, fenceFd, *region);
1191 ASSERT_EQ(ret, GSERROR_OK);
1192
1193 ret = OH_NativeImage_AcquireNativeWindowBuffer(newImage1, &nativeWindowBuffer1, &fenceFd);
1194 ASSERT_EQ(ret, GSERROR_OK);
1195 ret = OH_NativeImage_ReleaseNativeWindowBuffer(newImage, nativeWindowBuffer1, fenceFd);
1196 ASSERT_EQ(ret, SURFACE_ERROR_BUFFER_NOT_INCACHE);
1197
1198 delete rect;
1199 delete region;
1200 OH_NativeImage_Destroy(&newImage);
1201 OH_NativeImage_Destroy(&newImage1);
1202 }
1203
1204 /*
1205 * Function: OH_NativeImage_AcquireNativeWindowBuffer
1206 * Type: Function
1207 * Rank: Important(1)
1208 * EnvConditions: N/A
1209 * CaseDescription: 1. call OH_NativeImage_AcquireNativeWindowBuffer
1210 * 2. check ret
1211 * 3. call OH_NativeImage_ReleaseNativeWindowBuffer
1212 * 4. check ret
1213 * @tc.require: issueI5KG61
1214 */
1215 HWTEST_F(NativeImageTest, OHNativeImageAcquireNativeWindowBuffer004, Function | MediumTest | Level1)
1216 {
1217 OH_NativeImage* newImage = OH_NativeImage_Create(0, 0);
1218 ASSERT_NE(newImage, nullptr);
1219 OHNativeWindow* newNativeWindow = OH_NativeImage_AcquireNativeWindow(newImage);
1220 ASSERT_NE(newNativeWindow, nullptr);
1221
1222 int32_t code = SET_BUFFER_GEOMETRY;
1223 int32_t width = 0x100;
1224 int32_t height = 0x100;
1225 int32_t ret = NativeWindowHandleOpt(newNativeWindow, code, width, height);
1226 ASSERT_EQ(ret, GSERROR_OK);
1227
1228 NativeWindowBuffer* nativeWindowBuffer = nullptr;
1229 int fenceFd = -1;
1230 struct Region *region = new Region();
1231 struct Region::Rect *rect = new Region::Rect();
1232 rect->x = 0x100;
1233 rect->y = 0x100;
1234 rect->w = 0x100;
1235 rect->h = 0x100;
1236 region->rects = rect;
1237 struct timeval acquireStartTime;
1238 struct timeval acquireEndTime;
1239 struct timeval releaseStartTime;
1240 struct timeval releaseEndTime;
1241 uint64_t acquireTotalTime = 0;
1242 uint64_t releaseTotalTime = 0;
1243 for (int32_t i = 0; i < 10000; i++) {
1244 ret = OH_NativeWindow_NativeWindowRequestBuffer(newNativeWindow, &nativeWindowBuffer, &fenceFd);
1245 ASSERT_EQ(ret, GSERROR_OK);
1246
1247 ret = OH_NativeWindow_NativeWindowFlushBuffer(newNativeWindow, nativeWindowBuffer, fenceFd, *region);
1248 ASSERT_EQ(ret, GSERROR_OK);
1249
1250 nativeWindowBuffer = nullptr;
1251 gettimeofday(&acquireStartTime, nullptr);
1252 ret = OH_NativeImage_AcquireNativeWindowBuffer(newImage, &nativeWindowBuffer, &fenceFd);
1253 gettimeofday(&acquireEndTime, nullptr);
1254 acquireTotalTime += (1000000 * (acquireEndTime.tv_sec - acquireStartTime.tv_sec) +
1255 (acquireEndTime.tv_usec - acquireStartTime.tv_usec));
1256 ASSERT_EQ(ret, GSERROR_OK);
1257 ASSERT_NE(nativeWindowBuffer, nullptr);
1258
1259 gettimeofday(&releaseStartTime, nullptr);
1260 ret = OH_NativeImage_ReleaseNativeWindowBuffer(newImage, nativeWindowBuffer, fenceFd);
1261 gettimeofday(&releaseEndTime, nullptr);
1262 releaseTotalTime += (1000000 * (releaseEndTime.tv_sec - releaseStartTime.tv_sec) +
1263 (releaseEndTime.tv_usec - releaseStartTime.tv_usec));
1264 ASSERT_EQ(ret, GSERROR_OK);
1265 }
1266 std::cout << "10000 count total time, OH_NativeImage_AcquireNativeWindowBuffer: " << acquireTotalTime << " us" <<
1267 " OH_NativeImage_ReleaseNativeWindowBuffer: " << releaseTotalTime << " us" << std::endl;
1268
1269 delete rect;
1270 delete region;
1271 OH_NativeImage_Destroy(&newImage);
1272 }
1273
1274 /*
1275 * Function: OH_ConsumerSurface_Create
1276 * Type: Function
1277 * Rank: Important(1)
1278 * EnvConditions: N/A
1279 * CaseDescription: 1. call OH_ConsumerSurface_Create
1280 * 2. check ret
1281 * @tc.require: issueI5KG61
1282 */
1283 HWTEST_F(NativeImageTest, OH_ConsumerSurface_Create001, Function | MediumTest | Level1)
1284 {
1285 OH_NativeImage* consumerSurface = OH_ConsumerSurface_Create();
1286 ASSERT_NE(consumerSurface, nullptr);
1287 OHNativeWindow* newNativeWindow = OH_NativeImage_AcquireNativeWindow(consumerSurface);
1288 ASSERT_NE(newNativeWindow, nullptr);
1289
1290 int32_t code = SET_BUFFER_GEOMETRY;
1291 int32_t width = 0x100;
1292 int32_t height = 0x100;
1293 int32_t ret = NativeWindowHandleOpt(newNativeWindow, code, width, height);
1294 ASSERT_EQ(ret, GSERROR_OK);
1295
1296 NativeWindowBuffer* nativeWindowBuffer = nullptr;
1297 int fenceFd = -1;
1298 struct Region *region = new Region();
1299 struct Region::Rect *rect = new Region::Rect();
1300 rect->x = 0x100;
1301 rect->y = 0x100;
1302 rect->w = 0x100;
1303 rect->h = 0x100;
1304 region->rects = rect;
1305 for (int32_t i = 0; i < 10000; i++) {
1306 ret = OH_NativeWindow_NativeWindowRequestBuffer(newNativeWindow, &nativeWindowBuffer, &fenceFd);
1307 ASSERT_EQ(ret, GSERROR_OK);
1308
1309 ret = OH_NativeWindow_NativeWindowFlushBuffer(newNativeWindow, nativeWindowBuffer, fenceFd, *region);
1310 ASSERT_EQ(ret, GSERROR_OK);
1311
1312 nativeWindowBuffer = nullptr;
1313 ret = OH_NativeImage_AcquireNativeWindowBuffer(consumerSurface, &nativeWindowBuffer, &fenceFd);
1314 ASSERT_EQ(ret, GSERROR_OK);
1315 ASSERT_NE(nativeWindowBuffer, nullptr);
1316
1317 ret = OH_NativeImage_ReleaseNativeWindowBuffer(consumerSurface, nativeWindowBuffer, fenceFd);
1318 ASSERT_EQ(ret, GSERROR_OK);
1319 }
1320
1321 delete rect;
1322 delete region;
1323 OH_NativeImage_Destroy(&consumerSurface);
1324 }
1325
1326 /*
1327 * Function: OH_ConsumerSurface_Create
1328 * Type: Function
1329 * Rank: Important(1)
1330 * EnvConditions: N/A
1331 * CaseDescription: 1. call OH_ConsumerSurface_Create
1332 * 2. check ret
1333 * @tc.require: issueI5KG61
1334 */
1335 HWTEST_F(NativeImageTest, OH_ConsumerSurface_Create002, Function | MediumTest | Level1)
1336 {
1337 struct timeval startTime;
1338 struct timeval endTime;
1339 uint64_t totalTime = 0;
1340 OH_NativeImage* consumerSurface;
1341 for (int32_t i = 0; i < 10000; i++) {
1342 gettimeofday(&startTime, nullptr);
1343 consumerSurface = OH_ConsumerSurface_Create();
1344 gettimeofday(&endTime, nullptr);
1345 totalTime += (1000000 * (endTime.tv_sec - startTime.tv_sec) + (endTime.tv_usec - startTime.tv_usec));
1346 ASSERT_NE(consumerSurface, nullptr);
1347 OH_NativeImage_Destroy(&consumerSurface);
1348 }
1349 std::cout << "10000 count total time, OH_ConsumerSurface_Create: " << totalTime << " us" << std::endl;
1350 }
1351
1352 /*
1353 * Function: OH_ConsumerSurface_SetDefaultUsage
1354 * Type: Function
1355 * Rank: Important(1)
1356 * EnvConditions: N/A
1357 * CaseDescription: 1. call OH_ConsumerSurface_SetDefaultUsage
1358 * 2. check ret
1359 * @tc.require: issueI5KG61
1360 */
1361 HWTEST_F(NativeImageTest, OH_ConsumerSurface_SetDefaultUsage001, Function | MediumTest | Level1)
1362 {
1363 uint64_t usage = BUFFER_USAGE_CPU_READ;
1364 OH_NativeImage* consumerSurface = OH_ConsumerSurface_Create();
1365 ASSERT_NE(consumerSurface, nullptr);
1366 ASSERT_EQ(OH_ConsumerSurface_SetDefaultUsage(nullptr, usage), SURFACE_ERROR_INVALID_PARAM);
1367 ASSERT_EQ(OH_ConsumerSurface_SetDefaultUsage(consumerSurface, usage), GSERROR_OK);
1368 OH_NativeImage_Destroy(&consumerSurface);
1369 }
1370
1371 /*
1372 * Function: OH_ConsumerSurface_SetDefaultSize
1373 * Type: Function
1374 * Rank: Important(1)
1375 * EnvConditions: N/A
1376 * CaseDescription: 1. call OH_ConsumerSurface_SetDefaultSize
1377 * 2. check ret
1378 * @tc.require: issueI5KG61
1379 */
1380 HWTEST_F(NativeImageTest, OH_ConsumerSurface_SetDefaultSize001, Function | MediumTest | Level1)
1381 {
1382 int32_t width = 100;
1383 int32_t height = 100;
1384 OH_NativeImage* consumerSurface = OH_ConsumerSurface_Create();
1385 ASSERT_NE(consumerSurface, nullptr);
1386 ASSERT_EQ(OH_ConsumerSurface_SetDefaultSize(nullptr, 1, 1), SURFACE_ERROR_INVALID_PARAM);
1387 ASSERT_EQ(OH_ConsumerSurface_SetDefaultSize(consumerSurface, 0, -1), SURFACE_ERROR_INVALID_PARAM);
1388 ASSERT_EQ(OH_ConsumerSurface_SetDefaultSize(consumerSurface, 1, -1), SURFACE_ERROR_INVALID_PARAM);
1389 ASSERT_EQ(OH_ConsumerSurface_SetDefaultSize(consumerSurface, width, height), GSERROR_OK);
1390 OH_NativeImage_Destroy(&consumerSurface);
1391 }
1392 }