1 /*
2 * Copyright (c) 2023 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 "napi/native_api.h"
17 #include <EGL/egl.h>
18 #include <EGL/eglext.h>
19 #include <GLES3/gl32.h>
20 #include <bits/alltypes.h>
21 #include <cstring>
22 #include <native_buffer/native_buffer.h>
23 #include <native_image/native_image.h>
24 #include <native_window/external_window.h>
25 #include <sys/mman.h>
26 #include <chrono>
27 #include <iostream>
28 #include <map>
29 #include <native_image/graphic_error_code.h>
30
31 #define SUCCESS 0
32 #define PARAM_0 0
33 #define PARAM_8 8
34 #define PARAM_1 1
35 #define PARAM_2 2
36 #define PARAM_3 3
37 #define PARAM_16 16
38 #define PARAM_800 800
39 #define PARAM_600 600
40 #define FAIL (-1)
41 #define ONEVAL 1
42 #define NUMMAX 16
43 #define TWOVAL 2
44 #define ARR_NUMBER_0 0
45 #define ARR_NUMBER_1 1
46 #define ARR_NUMBER_2 2
47 #define ARR_NUMBER_3 3
48 #define ARR_NUMBER_4 4
49 #define ARR_NUMBER_5 5
50 #define ARR_NUMBER_6 6
51 #define NUMBER_99999 99999
52 #define NUMBER_500 500
53 #define NUMBER_1000 1000
54 #define NUMBER_100000 100000
55
56 using GetPlatformDisplayExt = PFNEGLGETPLATFORMDISPLAYEXTPROC;
57 constexpr const char *EGL_EXT_PLATFORM_WAYLAND = "EGL_EXT_platform_wayland";
58 constexpr const char *EGL_KHR_PLATFORM_WAYLAND = "EGL_KHR_platform_wayland";
59 constexpr int32_t EGL_CONTEXT_CLIENT_VERSION_NUM = 2;
60 constexpr char CHARACTER_WHITESPACE = ' ';
61 constexpr const char *CHARACTER_STRING_WHITESPACE = " ";
62 constexpr const char *EGL_GET_PLATFORM_DISPLAY_EXT = "eglGetPlatformDisplayEXT";
63 EGLContext eglContext_ = EGL_NO_CONTEXT;
64 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
65 static inline EGLConfig config_;
66
67 class InitNativeWindow {
68 private:
69 int32_t width_ = 0x800;
70 int32_t height_ = 0x600;
71 int32_t usage = NATIVEBUFFER_USAGE_CPU_READ | NATIVEBUFFER_USAGE_CPU_WRITE | NATIVEBUFFER_USAGE_MEM_DMA;
72 OHNativeWindow *_nativeWindow = nullptr;
73 GLuint textureId = 0;
74 public:
InitNativeWindow(OH_NativeImage * _image)75 explicit InitNativeWindow(OH_NativeImage *_image)
76 {
77 if (_image != nullptr) {
78 _nativeWindow = OH_NativeImage_AcquireNativeWindow(_image);
79 if (_nativeWindow != nullptr) {
80 OH_NativeWindow_NativeWindowHandleOpt(_nativeWindow, SET_BUFFER_GEOMETRY, width_, height_);
81 OH_NativeWindow_NativeWindowHandleOpt(_nativeWindow, SET_USAGE, usage);
82 } else {
83 _nativeWindow = nullptr;
84 }
85 } else {
86 _image = nullptr;
87 _nativeWindow = nullptr;
88 }
89 }
90
~InitNativeWindow()91 ~InitNativeWindow()
92 {
93 _nativeWindow = nullptr;
94 }
95
returnNativeWindow()96 OHNativeWindow *returnNativeWindow()
97 {
98 if (_nativeWindow == nullptr) {
99 return nullptr;
100 } else {
101 return _nativeWindow;
102 }
103 };
104
OH_FlushBuffer()105 int32_t OH_FlushBuffer()
106 {
107 OHNativeWindowBuffer *Buffer = nullptr;
108 int fenceFd = -1;
109 struct Region *region = new Region();
110 struct Region::Rect *rect = new Region::Rect();
111 rect->x = 0x100;
112 rect->y = 0x100;
113 rect->w = 0x100;
114 rect->h = 0x100;
115 region->rects = rect;
116 int32_t ret = OH_NativeWindow_NativeWindowRequestBuffer(_nativeWindow, &Buffer, &fenceFd);
117 ret = OH_NativeWindow_NativeWindowFlushBuffer(_nativeWindow, Buffer, fenceFd, *region);
118 return ret;
119 };
120 };
121
ConsumerSurfaceBuffer(OHNativeWindow * window,OHNativeWindowBuffer ** nativeWindowBuffer,int fenceFd)122 int32_t ConsumerSurfaceBuffer(OHNativeWindow *window, OHNativeWindowBuffer **nativeWindowBuffer, int fenceFd)
123 {
124 OHNativeWindowBuffer *windowBuffer = nullptr;
125 struct Region *region = new Region();
126 struct Region::Rect *rect = new Region::Rect();
127 rect->x = 0x100;
128 rect->y = 0x100;
129 rect->w = 0x100;
130 rect->h = 0x100;
131 region->rects = rect;
132 int32_t ret1 = OH_NativeWindow_NativeWindowRequestBuffer(window, &windowBuffer, &fenceFd);
133 int32_t ret2 = OH_NativeWindow_NativeWindowFlushBuffer(window, windowBuffer, fenceFd, *region);
134 if (window != nullptr && ret1 == 0 && ret2 == 0) {
135 *nativeWindowBuffer = windowBuffer;
136 return SUCCESS;
137 } else {
138 return ret1;
139 }
140 }
141
CheckEglExtension(const char * extensions,const char * extension)142 static bool CheckEglExtension(const char *extensions, const char *extension)
143 {
144 size_t extlen = strlen(extension);
145 const char *end = extensions + strlen(extensions);
146
147 while (extensions < end) {
148 size_t n = PARAM_0;
149 if (*extensions == CHARACTER_WHITESPACE) {
150 extensions++;
151 continue;
152 }
153 n = strcspn(extensions, CHARACTER_STRING_WHITESPACE);
154 if (n == extlen && strncmp(extension, extensions, n) == PARAM_0) {
155 return true;
156 }
157 extensions += n;
158 }
159 return false;
160 }
161
GetPlatformEglDisplay(EGLenum platform,void * native_display,const EGLint * attrib_list)162 static EGLDisplay GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list)
163 {
164 static GetPlatformDisplayExt eglGetPlatformDisplayExt = nullptr;
165
166 if (!eglGetPlatformDisplayExt) {
167 const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
168 if (extensions && (CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) ||
169 CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) {
170 eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT);
171 }
172 }
173
174 if (eglGetPlatformDisplayExt) {
175 return eglGetPlatformDisplayExt(platform, native_display, attrib_list);
176 }
177
178 return eglGetDisplay((EGLNativeDisplayType)native_display);
179 }
180
InitEGLEnv()181 static void InitEGLEnv()
182 {
183 eglDisplay_ = GetPlatformEglDisplay(EGL_PLATFORM_OHOS_KHR, EGL_DEFAULT_DISPLAY, nullptr);
184 EGLint major, minor;
185 eglInitialize(eglDisplay_, &major, &minor);
186 eglBindAPI(EGL_OPENGL_ES_API);
187 unsigned int ret;
188 EGLint count;
189 EGLint config_attribs[] = {EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, PARAM_8,
190 EGL_GREEN_SIZE, PARAM_8, EGL_BLUE_SIZE, PARAM_8,
191 EGL_ALPHA_SIZE, PARAM_8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
192 EGL_NONE};
193
194 ret = eglChooseConfig(eglDisplay_, config_attribs, &config_, PARAM_1, &count);
195 static const EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_NUM, EGL_NONE};
196 eglContext_ = eglCreateContext(eglDisplay_, config_, EGL_NO_CONTEXT, context_attribs);
197 eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglContext_);
198 }
199
200 int32_t g_callBack = 0;
201
DealCallback(void * context)202 void DealCallback(void * context)
203 {
204 g_callBack++;
205 return;
206 }
207
OnFrameAvailable(void * context)208 static void OnFrameAvailable(void *context)
209 {
210 DealCallback(context);
211 return;
212 }
213
getNativeImage()214 static OH_NativeImage *getNativeImage()
215 {
216 GLuint textureId = SUCCESS;
217 glGenTextures(ONEVAL, &textureId);
218 OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
219 return image;
220 }
221
OHNativeImageCreate(napi_env env,napi_callback_info info)222 static napi_value OHNativeImageCreate(napi_env env, napi_callback_info info)
223 {
224 napi_value result = nullptr;
225 OH_NativeImage *image = getNativeImage();
226 int backInfo = FAIL;
227 if (image != nullptr) {
228 backInfo = SUCCESS;
229 OH_NativeImage_Destroy(&image);
230 }
231
232 napi_create_int32(env, backInfo, &result);
233 return result;
234 }
235
OHNativeImageAcquireNativeWindow(napi_env env,napi_callback_info info)236 static napi_value OHNativeImageAcquireNativeWindow(napi_env env, napi_callback_info info)
237 {
238 napi_value result = nullptr;
239 int backInfo = FAIL;
240 OH_NativeImage *image = getNativeImage();
241
242 if (image != nullptr) {
243 OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
244 if (nativeWindow != nullptr) {
245 backInfo = SUCCESS;
246 }
247 OH_NativeImage_Destroy(&image);
248 }
249 napi_create_int32(env, backInfo, &result);
250 return result;
251 }
252
OHNativeImageAcquireNativeWindowAbnormal(napi_env env,napi_callback_info info)253 static napi_value OHNativeImageAcquireNativeWindowAbnormal(napi_env env, napi_callback_info info)
254 {
255 napi_value result = nullptr;
256 int backInfo = FAIL;
257 OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(nullptr);
258 if (nativeWindow != nullptr) {
259 backInfo = SUCCESS;
260 }
261 napi_create_int32(env, backInfo, &result);
262 return result;
263 }
264
OHNativeImageAttachContext(napi_env env,napi_callback_info info)265 static napi_value OHNativeImageAttachContext(napi_env env, napi_callback_info info)
266 {
267 int backInfo = FAIL;
268 InitEGLEnv();
269 GLuint textureId;
270 glGenTextures(PARAM_1, &textureId);
271 OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
272 OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
273 int code = SET_BUFFER_GEOMETRY;
274 int32_t width = PARAM_800;
275 int32_t height = PARAM_600;
276 OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
277 OHNativeWindowBuffer *buffer = nullptr;
278 int fenceFd;
279 OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
280 BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
281 void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
282 static uint32_t value = 0x00;
283 value++;
284 uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
285 for (uint32_t x = PARAM_0; x < width; x++) {
286 for (uint32_t y = PARAM_0; y < height; y++) {
287 *pixel++ = value;
288 }
289 }
290 munmap(mappedAddr, handle->size);
291
292 Region region{nullptr, PARAM_0};
293 OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
294 OH_NativeImage_UpdateSurfaceImage(image);
295 OH_NativeImage_GetTimestamp(image);
296 float matrix[16];
297 OH_NativeImage_GetTransformMatrix(image, matrix);
298 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
299 OH_NativeImage_DetachContext(image);
300 GLuint textureId2;
301 glGenTextures(PARAM_1, &textureId2);
302 backInfo = OH_NativeImage_AttachContext(image, textureId2);
303 napi_value result = nullptr;
304 napi_create_int32(env, backInfo, &result);
305 OH_NativeImage_Destroy(&image);
306 return result;
307 }
308
OHNativeImageAttachContextAbnormal(napi_env env,napi_callback_info info)309 static napi_value OHNativeImageAttachContextAbnormal(napi_env env, napi_callback_info info)
310 {
311 GLuint textureId2;
312 glGenTextures(ONEVAL, &textureId2);
313 int backInfo = OH_NativeImage_AttachContext(nullptr, textureId2);
314 if (backInfo != SUCCESS) {
315 backInfo = FAIL;
316 }
317 napi_value result = nullptr;
318 napi_create_int32(env, backInfo, &result);
319 return result;
320 }
321
OHNativeImageDetachContext(napi_env env,napi_callback_info info)322 static napi_value OHNativeImageDetachContext(napi_env env, napi_callback_info info)
323 {
324 int backInfo = FAIL;
325 InitEGLEnv();
326 GLuint textureId;
327 glGenTextures(PARAM_1, &textureId);
328 OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
329 OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
330 int code = SET_BUFFER_GEOMETRY;
331 int32_t width = PARAM_800;
332 int32_t height = PARAM_600;
333 OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
334 OHNativeWindowBuffer *buffer = nullptr;
335 int fenceFd;
336 OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
337 BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
338 void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
339 static uint32_t value = 0x00;
340 value++;
341 uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
342 for (uint32_t x = PARAM_0; x < width; x++) {
343 for (uint32_t y = PARAM_0; y < height; y++) {
344 *pixel++ = value;
345 }
346 }
347 munmap(mappedAddr, handle->size);
348
349 Region region{nullptr, PARAM_0};
350 OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
351 OH_NativeImage_UpdateSurfaceImage(image);
352 OH_NativeImage_GetTimestamp(image);
353 float matrix[16];
354 OH_NativeImage_GetTransformMatrix(image, matrix);
355 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
356 backInfo = OH_NativeImage_DetachContext(image);
357 GLuint textureId2;
358 glGenTextures(PARAM_1, &textureId2);
359 OH_NativeImage_AttachContext(image, textureId2);
360 OH_NativeImage_Destroy(&image);
361 napi_value result = nullptr;
362 napi_create_int32(env, backInfo, &result);
363 return result;
364 }
365
OHNativeImageDetachContextAbnormal(napi_env env,napi_callback_info info)366 static napi_value OHNativeImageDetachContextAbnormal(napi_env env, napi_callback_info info)
367 {
368 int backInfo = OH_NativeImage_DetachContext(nullptr);
369 if (backInfo != SUCCESS) {
370 backInfo = FAIL;
371 }
372 napi_value result = nullptr;
373 napi_create_int32(env, backInfo, &result);
374 return result;
375 }
376
OHNativeImageUpdateSurfaceImage(napi_env env,napi_callback_info info)377 static napi_value OHNativeImageUpdateSurfaceImage(napi_env env, napi_callback_info info)
378 {
379 InitEGLEnv();
380 GLuint textureId;
381 glGenTextures(PARAM_1, &textureId);
382 OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
383 OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
384 int code = SET_BUFFER_GEOMETRY;
385 int32_t width = PARAM_800;
386 int32_t height = PARAM_600;
387 OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
388 OHNativeWindowBuffer *buffer = nullptr;
389 int fenceFd;
390 OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
391 BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
392 void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
393 static uint32_t value = 0x00;
394 value++;
395 uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
396 for (uint32_t x = PARAM_0; x < width; x++) {
397 for (uint32_t y = PARAM_0; y < height; y++) {
398 *pixel++ = value;
399 }
400 }
401 munmap(mappedAddr, handle->size);
402 Region region{nullptr, PARAM_0};
403 OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
404 int backInfo = OH_NativeImage_UpdateSurfaceImage(image);
405 OH_NativeImage_GetTimestamp(image);
406 float matrix[16];
407 OH_NativeImage_GetTransformMatrix(image, matrix);
408 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
409 OH_NativeImage_DetachContext(image);
410 GLuint textureId2;
411 glGenTextures(PARAM_1, &textureId2);
412 OH_NativeImage_AttachContext(image, textureId2);
413 OH_NativeImage_Destroy(&image);
414 napi_value result = nullptr;
415 napi_create_int32(env, backInfo, &result);
416 return result;
417 }
418
OHNativeImageUpdateSurfaceImageBoundary(napi_env env,napi_callback_info info)419 static napi_value OHNativeImageUpdateSurfaceImageBoundary(napi_env env, napi_callback_info info)
420 {
421 InitEGLEnv();
422 GLuint textureId;
423 glGenTextures(PARAM_1, &textureId);
424 OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
425 OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
426 int code = SET_BUFFER_GEOMETRY;
427 int32_t width = 4096;
428 int32_t height = 2160;
429 OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
430 OHNativeWindowBuffer *buffer = nullptr;
431 int fenceFd;
432 OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
433 BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
434 void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
435 static uint32_t value = 0x00;
436 value++;
437 uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
438 for (uint32_t x = PARAM_0; x < width; x++) {
439 for (uint32_t y = PARAM_0; y < height; y++) {
440 *pixel++ = value;
441 }
442 }
443 munmap(mappedAddr, handle->size);
444 Region region{nullptr, PARAM_0};
445 OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
446 int backInfo = OH_NativeImage_UpdateSurfaceImage(image);
447 OH_NativeImage_GetTimestamp(image);
448 float matrix[16];
449 OH_NativeImage_GetTransformMatrix(image, matrix);
450 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
451 OH_NativeImage_DetachContext(image);
452 GLuint textureId2;
453 glGenTextures(PARAM_1, &textureId2);
454 OH_NativeImage_AttachContext(image, textureId2);
455 OH_NativeImage_Destroy(&image);
456 napi_value result = nullptr;
457 napi_create_int32(env, backInfo, &result);
458 return result;
459 }
460
OHNativeImageUpdateSurfaceImageAbnormal(napi_env env,napi_callback_info info)461 static napi_value OHNativeImageUpdateSurfaceImageAbnormal(napi_env env, napi_callback_info info)
462 {
463 int backInfo = OH_NativeImage_UpdateSurfaceImage(nullptr);
464 if (backInfo != SUCCESS) {
465 backInfo = FAIL;
466 }
467 napi_value result = nullptr;
468 napi_create_int32(env, backInfo, &result);
469 return result;
470 }
471
OHNativeImageGetTimestamp(napi_env env,napi_callback_info info)472 static napi_value OHNativeImageGetTimestamp(napi_env env, napi_callback_info info)
473 {
474 int backInfo = FAIL;
475 OH_NativeImage *image = getNativeImage();
476 if (image != nullptr) {
477 backInfo = OH_NativeImage_GetTimestamp(image);
478 OH_NativeImage_Destroy(&image);
479 }
480 napi_value result = nullptr;
481 napi_create_int32(env, backInfo, &result);
482 return result;
483 }
484
OHNativeImageGetTransformMatrix(napi_env env,napi_callback_info info)485 static napi_value OHNativeImageGetTransformMatrix(napi_env env, napi_callback_info info)
486 {
487 int backInfo = FAIL;
488 OH_NativeImage *image = getNativeImage();
489 if (image != nullptr) {
490 float matrix[NUMMAX];
491 backInfo = OH_NativeImage_GetTransformMatrix(image, matrix);
492 OH_NativeImage_Destroy(&image);
493 }
494 napi_value result = nullptr;
495 napi_create_int32(env, backInfo, &result);
496 return result;
497 }
498
OHNativeImageGetTransformMatrixAbnormal(napi_env env,napi_callback_info info)499 static napi_value OHNativeImageGetTransformMatrixAbnormal(napi_env env, napi_callback_info info)
500 {
501 int backInfo = FAIL;
502 float matrix[NUMMAX];
503 int ret = OH_NativeImage_GetTransformMatrix(nullptr, matrix);
504 if (ret != SUCCESS) {
505 backInfo = FAIL;
506 }
507
508 napi_value result = nullptr;
509 napi_create_int32(env, backInfo, &result);
510 return result;
511 }
512
OHNativeImageDestroy(napi_env env,napi_callback_info info)513 static napi_value OHNativeImageDestroy(napi_env env, napi_callback_info info)
514 {
515 int backInfo = FAIL;
516 OH_NativeImage *image = getNativeImage();
517 OH_NativeImage_Destroy(&image);
518 if (image == nullptr) {
519 backInfo = SUCCESS;
520 }
521
522 napi_value result = nullptr;
523 napi_create_int32(env, backInfo, &result);
524 return result;
525 }
526
OHNativeImageCreateNormal(napi_env env,napi_callback_info info)527 static napi_value OHNativeImageCreateNormal(napi_env env, napi_callback_info info)
528 {
529 int backInfo = FAIL;
530 OH_NativeImage *image = nullptr;
531 InitEGLEnv();
532 GLuint textureId;
533 glGenTextures(PARAM_1, &textureId);
534 GLuint textureTarget = GL_TEXTURE_2D;
535 image = OH_NativeImage_Create(textureId, textureTarget);
536 if (image != nullptr) {
537 backInfo = SUCCESS;
538 }
539 napi_value result = nullptr;
540 napi_create_int32(env, backInfo, &result);
541 OH_NativeImage_Destroy(&image);
542 return result;
543 }
544
OHNativeImageCreateAbnormal(napi_env env,napi_callback_info info)545 static napi_value OHNativeImageCreateAbnormal(napi_env env, napi_callback_info info)
546 {
547 int backInfo = FAIL;
548
549 OH_NativeImage *image = OH_NativeImage_Create(0, 1);
550 if (image != nullptr) {
551 backInfo = SUCCESS;
552 }
553 napi_value result = nullptr;
554 napi_create_int32(env, backInfo, &result);
555 OH_NativeImage_Destroy(&image);
556 return result;
557 }
558
OHNativeImageDestroy1(napi_env env,napi_callback_info info)559 static napi_value OHNativeImageDestroy1(napi_env env, napi_callback_info info)
560 {
561 int backInfo = FAIL;
562 OH_NativeImage *image = nullptr;
563 GLenum nativeImageTexId_;
564 GLuint GL_TEXTURE_EXTERNAL_OES;
565 glGenTextures(1, &nativeImageTexId_);
566 glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
567 image = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
568 if (image != nullptr) {
569 OH_NativeImage_Destroy(&image);
570 if (image == nullptr) {
571 backInfo = SUCCESS;
572 }
573 }
574 napi_value result = nullptr;
575 napi_create_int32(env, backInfo, &result);
576 return result;
577 }
578
OHNativeImageCreateMuch(napi_env env,napi_callback_info info)579 static napi_value OHNativeImageCreateMuch(napi_env env, napi_callback_info info)
580 {
581 int backInfo = SUCCESS;
582 const int count = NUMBER_500;
583 OH_NativeImage *imageArray[500] = {nullptr};
584 for (int i = 0; i < count; ++i) {
585 GLuint GL_TEXTURE_EXTERNAL_OES;
586 glBindTexture(GL_TEXTURE_EXTERNAL_OES, 1);
587 imageArray[i] = OH_NativeImage_Create(1, GL_TEXTURE_EXTERNAL_OES);
588 if (imageArray[i] == nullptr) {
589 backInfo = FAIL;
590 break;
591 }
592 }
593
594 for (int i = 0; i < count; ++i) {
595 if (imageArray[i] != nullptr) {
596 OH_NativeImage_Destroy(&imageArray[i]); // 销毁单个图像
597 if (imageArray[i] != nullptr) {
598 backInfo = FAIL;
599 break;
600 }
601 }
602 }
603
604 napi_value result = nullptr;
605 if (backInfo == FAIL) {
606 napi_create_int32(env, backInfo, &result);
607 } else {
608 napi_create_int32(env, SUCCESS, &result);
609 }
610
611 return result;
612 }
613
OHNativeImageAcquireNativeWindowNullptr(napi_env env,napi_callback_info info)614 static napi_value OHNativeImageAcquireNativeWindowNullptr(napi_env env, napi_callback_info info)
615 {
616 OHNativeWindow *nativeWindow = nullptr;
617 nativeWindow = OH_NativeImage_AcquireNativeWindow(nullptr);
618 napi_value result = nullptr;
619 if (nativeWindow != nullptr) {
620 napi_create_int32(env, SUCCESS, &result);
621 } else {
622 napi_create_int32(env, FAIL, &result);
623 }
624 return result;
625 }
626
OHNativeImageAcquireNativeWindowNormal(napi_env env,napi_callback_info info)627 static napi_value OHNativeImageAcquireNativeWindowNormal(napi_env env, napi_callback_info info)
628 {
629 int backInfo = FAIL;
630 OHNativeWindow *nativeWindow1 = nullptr;
631 OHNativeWindow *nativeWindow2 = nullptr;
632 OH_NativeImage *nativeImage = getNativeImage();
633 nativeWindow1 = OH_NativeImage_AcquireNativeWindow(nativeImage);
634 if (nativeWindow1 != nullptr) {
635 nativeWindow2 = OH_NativeImage_AcquireNativeWindow(nativeImage);
636 if (nativeWindow1 == nativeWindow2) {
637 backInfo = SUCCESS;
638 }
639 }
640
641 napi_value result = nullptr;
642 napi_create_int32(env, backInfo, &result);
643 OH_NativeImage_Destroy(&nativeImage);
644 return result;
645 }
646
OHNativeImageAttachContextNullptr(napi_env env,napi_callback_info info)647 static napi_value OHNativeImageAttachContextNullptr(napi_env env, napi_callback_info info)
648 {
649 OH_NativeImage *nativeImage = getNativeImage();
650 InitEGLEnv();
651 GLuint textureId;
652 glGenTextures(PARAM_1, &textureId);
653 napi_value result = nullptr;
654 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
655 napi_value result1 = nullptr;
656 napi_value result2 = nullptr;
657 int res1 = OH_NativeImage_AttachContext(nullptr, textureId);
658 if (res1 != 0) {
659 napi_create_int32(env, FAIL, &result1);
660 } else {
661 napi_create_int32(env, SUCCESS, &result1);
662 }
663 napi_set_element(env, result, ARR_NUMBER_0, result1);
664
665 int res2 = OH_NativeImage_AttachContext(nativeImage, NULL);
666 if (res2 != 0) {
667 napi_create_int32(env, FAIL, &result2);
668 } else {
669 napi_create_int32(env, SUCCESS, &result2);
670 }
671 napi_set_element(env, result, ARR_NUMBER_1, result2);
672 OH_NativeImage_Destroy(&nativeImage);
673 return result;
674 }
675
OHNativeImageDetachContextNullptr(napi_env env,napi_callback_info info)676 static napi_value OHNativeImageDetachContextNullptr(napi_env env, napi_callback_info info)
677 {
678 OH_NativeImage *nativeImage = getNativeImage();
679 InitEGLEnv();
680 GLuint textureId;
681 glGenTextures(PARAM_1, &textureId);
682 napi_value result = nullptr;
683 int res = OH_NativeImage_DetachContext(nullptr);
684 if (res != 0) {
685 napi_create_int32(env, FAIL, &result);
686 } else {
687 napi_create_int32(env, SUCCESS, &result);
688 }
689 OH_NativeImage_Destroy(&nativeImage);
690 return result;
691 }
692
OHNativeImageAttachContextNormal(napi_env env,napi_callback_info info)693 static napi_value OHNativeImageAttachContextNormal(napi_env env, napi_callback_info info)
694 {
695 OH_NativeImage *nativeImage = getNativeImage();
696 InitEGLEnv();
697 GLuint textureId;
698 glGenTextures(PARAM_1, &textureId);
699 napi_value result = nullptr;
700 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
701 napi_value result1 = nullptr;
702 napi_value result2 = nullptr;
703 int res1 = OH_NativeImage_AttachContext(nativeImage, textureId);
704 if (res1 == 0) {
705 napi_create_int32(env, SUCCESS, &result1);
706 } else {
707 napi_create_int32(env, FAIL, &result1);
708 }
709 napi_set_element(env, result, ARR_NUMBER_0, result1);
710
711 int res2 = OH_NativeImage_AttachContext(nativeImage, textureId);
712 if (res2 == 0) {
713 napi_create_int32(env, SUCCESS, &result2);
714 } else {
715 napi_create_int32(env, FAIL, &result2);
716 }
717 napi_set_element(env, result, ARR_NUMBER_1, result2);
718
719 OH_NativeImage_Destroy(&nativeImage);
720 return result;
721 }
722
OHNativeImageDetachContextNormal(napi_env env,napi_callback_info info)723 static napi_value OHNativeImageDetachContextNormal(napi_env env, napi_callback_info info)
724 {
725 OH_NativeImage *nativeImage = getNativeImage();
726 InitEGLEnv();
727 GLuint textureId;
728 glGenTextures(PARAM_1, &textureId);
729 napi_value result = nullptr;
730 napi_create_array_with_length(env, ARR_NUMBER_3, &result);
731 napi_value result1 = nullptr;
732 napi_value result2 = nullptr;
733 napi_value result3 = nullptr;
734 int res1 = OH_NativeImage_DetachContext(nativeImage);
735 if (res1 != 0) {
736 napi_create_int32(env, FAIL, &result1);
737 } else {
738 napi_create_int32(env, SUCCESS, &result1);
739 }
740 napi_set_element(env, result, ARR_NUMBER_0, result1);
741
742 int res2 = OH_NativeImage_AttachContext(nativeImage, textureId);
743 if (res2 == 0) {
744 napi_create_int32(env, SUCCESS, &result2);
745 } else {
746 napi_create_int32(env, FAIL, &result2);
747 }
748 napi_set_element(env, result, ARR_NUMBER_1, result2);
749
750 int res3 = OH_NativeImage_DetachContext(nativeImage);
751 if (res3 == 0) {
752 napi_create_int32(env, SUCCESS, &result3);
753 } else {
754 napi_create_int32(env, FAIL, &result3);
755 }
756 napi_set_element(env, result, ARR_NUMBER_2, result3);
757
758 OH_NativeImage_Destroy(&nativeImage);
759 return result;
760 }
761
OHNativeImageGetSurfaceIdNullptr(napi_env env,napi_callback_info info)762 static napi_value OHNativeImageGetSurfaceIdNullptr(napi_env env, napi_callback_info info)
763 {
764 OH_NativeImage *nativeImage = getNativeImage();
765 napi_value result = nullptr;
766 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
767 napi_value result1 = nullptr;
768 napi_value result2 = nullptr;
769 uint64_t surfaceId = NUMBER_99999;
770 int res1 = OH_NativeImage_GetSurfaceId(nullptr, &surfaceId);
771 if (res1 != 0 && surfaceId == NUMBER_99999) {
772 napi_create_int32(env, FAIL, &result1);
773 } else {
774 napi_create_int32(env, SUCCESS, &result1);
775 }
776 napi_set_element(env, result, ARR_NUMBER_0, result1);
777
778 int res2 = OH_NativeImage_GetSurfaceId(nullptr, NULL);
779 if (res2 != 0 && surfaceId == NUMBER_99999) {
780 napi_create_int32(env, FAIL, &result2);
781 } else {
782 napi_create_int32(env, SUCCESS, &result2);
783 }
784 napi_set_element(env, result, ARR_NUMBER_1, result2);
785
786 OH_NativeImage_Destroy(&nativeImage);
787 return result;
788 }
789
OHNativeGetSurfaceIdNormal(napi_env env,napi_callback_info info)790 static napi_value OHNativeGetSurfaceIdNormal(napi_env env, napi_callback_info info)
791 {
792 OH_NativeImage *nativeImage = getNativeImage();
793 napi_value result = nullptr;
794 uint64_t surfaceId1 = NUMBER_99999;
795 int res = OH_NativeImage_GetSurfaceId(nativeImage, &surfaceId1);
796 if (res == 0 && surfaceId1 != NUMBER_99999) {
797 napi_create_int32(env, SUCCESS, &result);
798 } else {
799 napi_create_int32(env, FAIL, &result);
800 return result;
801 }
802 OH_NativeImage *nativeImage2 = OH_ConsumerSurface_Create();
803 uint64_t surfaceId2 = NUMBER_99999;
804 int res2 = OH_NativeImage_GetSurfaceId(nativeImage2, &surfaceId2);
805 if (res2 == 0 && surfaceId2 != NUMBER_99999) {
806 napi_create_int32(env, SUCCESS, &result);
807 } else {
808 napi_create_int32(env, FAIL, &result);
809 return result;
810 }
811 OH_NativeImage_Destroy(&nativeImage);
812 OH_NativeImage_Destroy(&nativeImage2);
813 return result;
814 }
815
OHNativeImageUpdateSurfaceImageNullptr(napi_env env,napi_callback_info info)816 static napi_value OHNativeImageUpdateSurfaceImageNullptr(napi_env env, napi_callback_info info)
817 {
818 OH_NativeImage *nativeImage = getNativeImage();
819 napi_value result = nullptr;
820 int res = OH_NativeImage_UpdateSurfaceImage(nullptr);
821 if (res != 0) {
822 napi_create_int32(env, FAIL, &result);
823 } else {
824 napi_create_int32(env, SUCCESS, &result);
825 }
826 OH_NativeImage_Destroy(&nativeImage);
827 return result;
828 }
829
OHNativeImageGetTimestampNullptr(napi_env env,napi_callback_info info)830 static napi_value OHNativeImageGetTimestampNullptr(napi_env env, napi_callback_info info)
831 {
832 OH_NativeImage *nativeImage = getNativeImage();
833 napi_value result = nullptr;
834 int64_t timestamp = 0;
835 timestamp = OH_NativeImage_GetTimestamp(nullptr);
836 if (timestamp == 0) {
837 napi_create_int32(env, FAIL, &result);
838 } else {
839 napi_create_int32(env, SUCCESS, &result);
840 }
841 OH_NativeImage_Destroy(&nativeImage);
842 return result;
843 }
844
OHNativeImageGetTransformMatrixNullptr(napi_env env,napi_callback_info info)845 static napi_value OHNativeImageGetTransformMatrixNullptr(napi_env env, napi_callback_info info)
846 {
847 OH_NativeImage *nativeImage = getNativeImage();
848 napi_value result = nullptr;
849 float matrix[16];
850 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
851 napi_value result1 = nullptr;
852 napi_value result2 = nullptr;
853 int res1 = OH_NativeImage_GetTransformMatrix(nullptr, matrix);
854 if (res1 == 0) {
855 napi_create_int32(env, SUCCESS, &result1);
856 } else {
857 napi_create_int32(env, FAIL, &result1);
858 }
859 napi_set_element(env, result, ARR_NUMBER_0, result1);
860
861 int res2 = OH_NativeImage_GetTransformMatrix(nativeImage, NULL);
862 if (res2 == 0) {
863 napi_create_int32(env, SUCCESS, &result2);
864 } else {
865 napi_create_int32(env, FAIL, &result2);
866 }
867 napi_set_element(env, result, ARR_NUMBER_1, result2);
868
869 OH_NativeImage_Destroy(&nativeImage);
870 return result;
871 }
872
OHNativeImageGetTransformMatrixV2Nullptr(napi_env env,napi_callback_info info)873 static napi_value OHNativeImageGetTransformMatrixV2Nullptr(napi_env env, napi_callback_info info)
874 {
875 OH_NativeImage *nativeImage = getNativeImage();
876 napi_value result = nullptr;
877 float matrix[16];
878 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
879 napi_value result1 = nullptr;
880 napi_value result2 = nullptr;
881 int res1 = OH_NativeImage_GetTransformMatrixV2(nullptr, matrix);
882 if (res1 == 0) {
883 napi_create_int32(env, SUCCESS, &result1);
884 } else {
885 napi_create_int32(env, FAIL, &result1);
886 }
887 napi_set_element(env, result, ARR_NUMBER_0, result1);
888
889 int res2 = OH_NativeImage_GetTransformMatrixV2(nativeImage, NULL);
890 if (res2 == 0) {
891 napi_create_int32(env, SUCCESS, &result2);
892 } else {
893 napi_create_int32(env, FAIL, &result2);
894 }
895 napi_set_element(env, result, ARR_NUMBER_1, result2);
896
897 OH_NativeImage_Destroy(&nativeImage);
898 return result;
899 }
900
OHNativeImageUpdateSurfaceImageNormal(napi_env env,napi_callback_info info)901 static napi_value OHNativeImageUpdateSurfaceImageNormal(napi_env env, napi_callback_info info)
902 {
903 OH_NativeImage *nativeImage = nullptr;
904 napi_value result = nullptr;
905 GLenum nativeImageTexId_;
906 GLuint GL_TEXTURE_EXTERNAL_OES;
907 glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
908 nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
909 int res = OH_NativeImage_UpdateSurfaceImage(nativeImage);
910 if (res == 0) {
911 napi_create_int32(env, SUCCESS, &result);
912 } else {
913 napi_create_int32(env, FAIL, &result);
914 }
915 OH_NativeImage_Destroy(&nativeImage);
916 return result;
917 }
918
OHNativeImageGetTimestampNormal(napi_env env,napi_callback_info info)919 static napi_value OHNativeImageGetTimestampNormal(napi_env env, napi_callback_info info)
920 {
921 OH_NativeImage *nativeImage = nullptr;
922 napi_value result = nullptr;
923 napi_create_array_with_length(env, ARR_NUMBER_3, &result);
924 napi_value result1 = nullptr;
925 napi_value result2 = nullptr;
926 GLenum nativeImageTexId_;
927 GLuint GL_TEXTURE_EXTERNAL_OES;
928 glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
929 nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
930 int res = OH_NativeImage_UpdateSurfaceImage(nativeImage);
931
932 // 使用chrono获取当前时间点
933 auto now = std::chrono::system_clock::now();
934 // 将当前时间点转换为自1970年1月1日以来的毫秒数
935 auto duration = now.time_since_epoch();
936 auto current_timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
937 // 将chrono库获取的时间戳存储在int64_t类型的变量中
938 int64_t time = static_cast<int64_t>(current_timestamp);
939
940 int64_t timestamp = 0;
941 timestamp = OH_NativeImage_GetTimestamp(nativeImage);
942
943 if (res == 0) {
944 napi_create_int32(env, SUCCESS, &result1);
945 } else {
946 napi_create_int32(env, FAIL, &result1);
947 }
948 napi_set_element(env, result, ARR_NUMBER_0, result1);
949
950 if (timestamp == time) {
951 napi_create_int32(env, SUCCESS, &result2);
952 } else {
953 napi_create_int32(env, FAIL, &result2);
954 }
955 napi_set_element(env, result, ARR_NUMBER_1, result2);
956
957 napi_value result3 = nullptr;
958 int res1 = OH_NativeImage_UpdateSurfaceImage(nativeImage);
959
960 auto now1 = std::chrono::system_clock::now();
961 auto duration1 = now1.time_since_epoch();
962 auto current_timestamp1 = std::chrono::duration_cast<std::chrono::milliseconds>(duration1).count();
963 int64_t time1 = static_cast<int64_t>(current_timestamp);
964
965 int64_t timestamp1 = 0;
966 timestamp1 = OH_NativeImage_GetTimestamp(nativeImage);
967
968 if (res1 == 0 && current_timestamp1 == time1) {
969 napi_create_int32(env, SUCCESS, &result3);
970 } else {
971 napi_create_int32(env, FAIL, &result3);
972 }
973 napi_set_element(env, result, ARR_NUMBER_2, result3);
974
975 OH_NativeImage_Destroy(&nativeImage);
976 return result;
977 }
978
OHNativeImageGetTransformMatrixNormal(napi_env env,napi_callback_info info)979 static napi_value OHNativeImageGetTransformMatrixNormal(napi_env env, napi_callback_info info)
980 {
981 OH_NativeImage *nativeImage = nullptr;
982 GLenum nativeImageTexId_;
983 GLuint GL_TEXTURE_EXTERNAL_OES;
984 glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
985 nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
986 float matrix[16];
987 float matrixNull[16] = {};
988 napi_value result = nullptr;
989 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
990 napi_value result1 = nullptr;
991 napi_value result2 = nullptr;
992
993 int res1 = OH_NativeImage_UpdateSurfaceImage(nativeImage);
994 if (res1 == 0) {
995 napi_create_int32(env, SUCCESS, &result1);
996 } else {
997 napi_create_int32(env, FAIL, &result1);
998 }
999 napi_set_element(env, result, ARR_NUMBER_0, result1);
1000
1001 OH_NativeImage_GetTransformMatrix(nativeImage, matrix);
1002 if (memcmp(matrix, matrixNull, sizeof(matrix)) == 0) {
1003 napi_create_int32(env, FAIL, &result2);
1004 } else {
1005 napi_create_int32(env, SUCCESS, &result2);
1006 }
1007 napi_set_element(env, result, ARR_NUMBER_1, result2);
1008
1009 OH_NativeImage_Destroy(&nativeImage);
1010 return result;
1011 }
1012
OHNativeImageGetTransformMatrixV2Normal(napi_env env,napi_callback_info info)1013 static napi_value OHNativeImageGetTransformMatrixV2Normal(napi_env env, napi_callback_info info)
1014 {
1015 OH_NativeImage *nativeImage = nullptr;
1016 GLenum nativeImageTexId_;
1017 GLuint GL_TEXTURE_EXTERNAL_OES;
1018 glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
1019 nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
1020 float matrixV2[16];
1021 float matrixNull[16] = {};
1022 napi_value result = nullptr;
1023 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1024 napi_value result1 = nullptr;
1025 napi_value result2 = nullptr;
1026
1027 int res1 = OH_NativeImage_UpdateSurfaceImage(nativeImage);
1028 if (res1 == 0) {
1029 napi_create_int32(env, SUCCESS, &result1);
1030 } else {
1031 napi_create_int32(env, FAIL, &result1);
1032 }
1033 napi_set_element(env, result, ARR_NUMBER_0, result1);
1034
1035 OH_NativeImage_GetTransformMatrixV2(nativeImage, matrixV2);
1036 if (memcmp(matrixV2, matrixNull, sizeof(matrixV2)) == 0) {
1037 napi_create_int32(env, FAIL, &result2);
1038 } else {
1039 napi_create_int32(env, SUCCESS, &result2);
1040 }
1041 napi_set_element(env, result, ARR_NUMBER_1, result2);
1042
1043 OH_NativeImage_Destroy(&nativeImage);
1044 return result;
1045 }
1046
OHNativeImageSetOnFrameAvailableListenerNullptr(napi_env env,napi_callback_info info)1047 static napi_value OHNativeImageSetOnFrameAvailableListenerNullptr(napi_env env, napi_callback_info info)
1048 {
1049 OH_NativeImage *nativeImage = nullptr;
1050 napi_value result = nullptr;
1051 OH_OnFrameAvailableListener listener;
1052 int res = OH_NativeImage_SetOnFrameAvailableListener(nullptr, listener);
1053 if (res == 0) {
1054 napi_create_int32(env, SUCCESS, &result);
1055 } else {
1056 napi_create_int32(env, FAIL, &result);
1057 }
1058 return result;
1059 }
1060
OHNativeImageUnSetOnFrameAvailableListenerNullptr(napi_env env,napi_callback_info info)1061 static napi_value OHNativeImageUnSetOnFrameAvailableListenerNullptr(napi_env env, napi_callback_info info)
1062 {
1063 napi_value result = nullptr;
1064 int res = OH_NativeImage_UnsetOnFrameAvailableListener(nullptr);
1065 if (res == 0) {
1066 napi_create_int32(env, SUCCESS, &result);
1067 } else {
1068 napi_create_int32(env, FAIL, &result);
1069 }
1070
1071 return result;
1072 }
1073
OHNativeImageSetOnFrameAvailableListenerNormal(napi_env env,napi_callback_info info)1074 static napi_value OHNativeImageSetOnFrameAvailableListenerNormal(napi_env env, napi_callback_info info)
1075 {
1076 OH_NativeImage *nativeImage1 = nullptr;
1077 nativeImage1 = OH_ConsumerSurface_Create();
1078 OH_OnFrameAvailableListener listener1;
1079 listener1.context = static_cast<void *>(nativeImage1);
1080 listener1.onFrameAvailable = OnFrameAvailable;
1081 napi_value result = nullptr;
1082 int res = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener1);
1083 if (res != 0) {
1084 napi_create_int32(env, FAIL, &result);
1085 return result;
1086 }
1087 napi_value result2 = nullptr;
1088 res = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener1);
1089 if (res != 0) {
1090 napi_create_int32(env, FAIL, &result);
1091 return result;
1092 }
1093 OH_NativeImage *nativeImage2 = nullptr;
1094 nativeImage2 = OH_ConsumerSurface_Create();
1095 OH_OnFrameAvailableListener listener2;
1096 listener2.context = static_cast<void *>(nativeImage1);
1097 listener2.onFrameAvailable = OnFrameAvailable;
1098 res = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener2);
1099 if (res != 0) {
1100 napi_create_int32(env, FAIL, &result);
1101 return result;
1102 }
1103 napi_value result4 = nullptr;
1104 res = OH_NativeImage_SetOnFrameAvailableListener(nativeImage2, listener1);
1105 if (res != 0) {
1106 napi_create_int32(env, FAIL, &result);
1107 return result;
1108 }
1109 napi_create_int32(env, SUCCESS, &result);
1110 OH_NativeImage_Destroy(&nativeImage1);
1111 OH_NativeImage_Destroy(&nativeImage2);
1112 return result;
1113 }
1114
OHNativeImageUnsetOnFrameAvailableListenerNormal(napi_env env,napi_callback_info info)1115 static napi_value OHNativeImageUnsetOnFrameAvailableListenerNormal(napi_env env, napi_callback_info info)
1116 {
1117 OH_NativeImage *nativeImage1 = getNativeImage();
1118 OH_OnFrameAvailableListener listener1;
1119 listener1.context = static_cast<void *>(nativeImage1);
1120 listener1.onFrameAvailable = OnFrameAvailable;
1121 napi_value result = nullptr;
1122 napi_create_array_with_length(env, ARR_NUMBER_3, &result);
1123 napi_value result1 = nullptr;
1124
1125 int res1 = OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage1);
1126 if (res1 == 0) {
1127 napi_create_int32(env, SUCCESS, &result1);
1128 } else {
1129 napi_create_int32(env, FAIL, &result1);
1130 }
1131 napi_set_element(env, result, ARR_NUMBER_0, result1);
1132
1133 napi_value result2 = nullptr;
1134 int res2 = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener1);
1135 if (res2 == 0) {
1136 napi_create_int32(env, SUCCESS, &result2);
1137 } else {
1138 napi_create_int32(env, FAIL, &result2);
1139 }
1140 napi_set_element(env, result, ARR_NUMBER_1, result2);
1141
1142 napi_value result3 = nullptr;
1143 int res3 = OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage1);
1144 if (res3 == 0) {
1145 napi_create_int32(env, SUCCESS, &result3);
1146 } else {
1147 napi_create_int32(env, FAIL, &result3);
1148 }
1149 napi_set_element(env, result, ARR_NUMBER_2, result3);
1150 OH_NativeImage_Destroy(&nativeImage1);
1151 return result;
1152 }
1153
OHNativeImageGetBufferMatrixNormal(napi_env env,napi_callback_info info)1154 static napi_value OHNativeImageGetBufferMatrixNormal(napi_env env, napi_callback_info info)
1155 {
1156 napi_value result = nullptr;
1157 OH_NativeImage *image = getNativeImage();
1158 InitNativeWindow *initNative = new InitNativeWindow(image);
1159 initNative->OH_FlushBuffer();
1160 int32_t ret = OH_NativeImage_UpdateSurfaceImage(image);
1161 float matrix[16] = {-1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1162 ret = OH_NativeImage_GetBufferMatrix(image, matrix);
1163 if (ret == 0) {
1164 napi_create_int32(env, SUCCESS, &result);
1165 } else {
1166 napi_create_int32(env, FAIL, &result);
1167 }
1168 OH_NativeImage_Destroy(&image);
1169 delete initNative;
1170 return result;
1171 }
1172
OHNativeImageGetBufferMatrixNormal01(napi_env env,napi_callback_info info)1173 static napi_value OHNativeImageGetBufferMatrixNormal01(napi_env env, napi_callback_info info)
1174 {
1175 napi_value result = nullptr;
1176 napi_value result1 = nullptr;
1177 napi_value result2 = nullptr;
1178 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1179 OH_NativeImage *image = getNativeImage();
1180 InitNativeWindow *initNative = new InitNativeWindow(image);
1181 OH_OnFrameAvailableListener listener;
1182 listener.context = static_cast<void *>(image);
1183 listener.onFrameAvailable = OnFrameAvailable;
1184 int32_t ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener);
1185 float matrix[16] = {-1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1186 ret = OH_NativeImage_GetBufferMatrix(image, matrix);
1187 if (ret == 0) {
1188 napi_create_int32(env, SUCCESS, &result1);
1189 } else {
1190 napi_create_int32(env, ret, &result1);
1191 }
1192 napi_set_element(env, result, ARR_NUMBER_0, result1);
1193 initNative->OH_FlushBuffer();
1194 ret = OH_NativeImage_UpdateSurfaceImage(image);
1195 int32_t ret1 = OH_NativeImage_GetBufferMatrix(image, matrix);
1196 if (ret1 == 0) {
1197 napi_create_int32(env, SUCCESS, &result2);
1198 } else {
1199 napi_create_int32(env, ret1, &result2);
1200 }
1201 napi_set_element(env, result, ARR_NUMBER_1, result2);
1202 OH_NativeImage_Destroy(&image);
1203 delete initNative;
1204 return result;
1205 }
1206
OHNativeImageGetBufferMatrixAbNormal(napi_env env,napi_callback_info info)1207 static napi_value OHNativeImageGetBufferMatrixAbNormal(napi_env env, napi_callback_info info)
1208 {
1209 napi_value result = nullptr;
1210 napi_create_array_with_length(env, ARR_NUMBER_3, &result);
1211 OH_NativeImage *nativeImage = getNativeImage();
1212 float matrix[16];
1213 napi_value result1 = nullptr;
1214 int32_t ret = OH_NativeImage_GetBufferMatrix(nullptr, matrix);
1215 if (ret == 0) {
1216 napi_create_int32(env, SUCCESS, &result1);
1217 } else {
1218 napi_create_int32(env, ret, &result1);
1219 }
1220 napi_set_element(env, result, ARR_NUMBER_0, result1);
1221 napi_value result2 = nullptr;
1222 int32_t ret1 = OH_NativeImage_GetBufferMatrix(nativeImage, nullptr);
1223 if (ret1 == 0) {
1224 napi_create_int32(env, SUCCESS, &result2);
1225 } else {
1226 napi_create_int32(env, ret1, &result2);
1227 }
1228 napi_set_element(env, result, ARR_NUMBER_1, result2);
1229 napi_value result3 = nullptr;
1230 float matrix1[14];
1231 int32_t ret2 = OH_NativeImage_GetBufferMatrix(nativeImage, matrix1);
1232 if (ret2 == 0) {
1233 napi_create_int32(env, SUCCESS, &result3);
1234 } else {
1235 napi_create_int32(env, ret2, &result3);
1236 }
1237 napi_set_element(env, result, ARR_NUMBER_2, result3);
1238 OH_NativeImage_Destroy(&nativeImage);
1239 return result;
1240 }
1241
OHNativeImageGetBufferMatrixCall(napi_env env,napi_callback_info info)1242 static napi_value OHNativeImageGetBufferMatrixCall(napi_env env, napi_callback_info info)
1243 {
1244 napi_value result = nullptr;
1245 OH_NativeImage *image = getNativeImage();
1246 InitNativeWindow *initNative = new InitNativeWindow(image);
1247 OH_OnFrameAvailableListener listener;
1248 listener.context = static_cast<void *>(image);
1249 listener.onFrameAvailable = OnFrameAvailable;
1250 int32_t ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener);
1251 ret = initNative->OH_FlushBuffer();
1252 if (ret != 0) {
1253 napi_create_int32(env, ret, &result);
1254 return result;
1255 }
1256 ret = OH_NativeImage_UpdateSurfaceImage(image);
1257 if (ret != 0) {
1258 napi_create_int32(env, ret, &result);
1259 return result;
1260 }
1261 float matrix[16] = {-1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1262 for (int i = 0; i < NUMBER_1000; i++) {
1263 int32_t flag = OH_NativeImage_GetBufferMatrix(image, matrix);
1264 if (flag == 0) {
1265 napi_create_int32(env, SUCCESS, &result);
1266 } else {
1267 napi_create_int32(env, (NUMBER_1000 * i) + ARR_NUMBER_1, &result);
1268 return result;
1269 }
1270 }
1271 OH_NativeImage_Destroy(&image);
1272 delete initNative;
1273 return result;
1274 }
1275
OHNativeImageAcquireNativeWindowBufferNormal(napi_env env,napi_callback_info info)1276 static napi_value OHNativeImageAcquireNativeWindowBufferNormal(napi_env env, napi_callback_info info)
1277 {
1278 napi_value result = nullptr;
1279 napi_value result1 = nullptr;
1280 napi_value result2 = nullptr;
1281 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1282 OH_NativeImage *image = OH_ConsumerSurface_Create();
1283 InitNativeWindow *initNative = new InitNativeWindow(image);
1284 int32_t ret = initNative->OH_FlushBuffer();
1285 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
1286 int fenceFd = -1;
1287 ret = OH_NativeImage_AcquireNativeWindowBuffer(image, &nativeWindowBuffer, &fenceFd);
1288 int32_t ret1 = OH_NativeImage_ReleaseNativeWindowBuffer(image, nativeWindowBuffer, fenceFd);
1289 if (ret == 0) {
1290 napi_create_int32(env, SUCCESS, &result1);
1291 } else {
1292 napi_create_int32(env, ret, &result1);
1293 }
1294 napi_set_element(env, result, ARR_NUMBER_0, result1);
1295 if (ret1 == 0) {
1296 napi_create_int32(env, SUCCESS, &result2);
1297 } else {
1298 napi_create_int32(env, ret1, &result2);
1299 }
1300 napi_set_element(env, result, ARR_NUMBER_1, result2);
1301 OH_NativeImage_Destroy(&image);
1302 delete initNative;
1303 return result;
1304 }
1305
OHNativeImageAcquireNativeWindowBufferCall(napi_env env,napi_callback_info info)1306 static napi_value OHNativeImageAcquireNativeWindowBufferCall(napi_env env, napi_callback_info info)
1307 {
1308 napi_value result = nullptr;
1309 napi_value result1 = nullptr;
1310 napi_value result2 = nullptr;
1311 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1312 OH_NativeImage *image = OH_ConsumerSurface_Create();
1313 InitNativeWindow *initNative = new InitNativeWindow(image);
1314 int32_t ret = initNative->OH_FlushBuffer();
1315 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
1316 int fenceFd = -1;
1317 int num = 10;
1318 for (int i = 0; i < num; i++) {
1319 ret = OH_NativeImage_AcquireNativeWindowBuffer(image, &nativeWindowBuffer, &fenceFd);
1320 }
1321 int32_t ret1 = OH_NativeImage_ReleaseNativeWindowBuffer(image, nativeWindowBuffer, fenceFd);
1322 if (ret == 0) {
1323 napi_create_int32(env, SUCCESS, &result1);
1324 } else {
1325 napi_create_int32(env, FAIL, &result1);
1326 }
1327 napi_set_element(env, result, ARR_NUMBER_0, result1);
1328 if (ret1 == 0) {
1329 napi_create_int32(env, SUCCESS, &result2);
1330 } else {
1331 napi_create_int32(env, FAIL, &result2);
1332 }
1333 napi_set_element(env, result, ARR_NUMBER_1, result2);
1334 OH_NativeImage_Destroy(&image);
1335 delete initNative;
1336 return result;
1337 }
1338
OHNativeImageAcquireNativeWindowBufferAbNormal01(napi_env env,napi_callback_info info)1339 static napi_value OHNativeImageAcquireNativeWindowBufferAbNormal01(napi_env env, napi_callback_info info)
1340 {
1341 napi_value result = nullptr;
1342 napi_value result1 = nullptr;
1343 napi_value result2 = nullptr;
1344 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1345 OH_NativeImage *image = OH_ConsumerSurface_Create();
1346 InitNativeWindow *initNative = new InitNativeWindow(image);
1347 int32_t ret = initNative->OH_FlushBuffer();
1348 OHNativeWindowBuffer *nativeWindowBuffer = 0;
1349 int fenceFd = -1;
1350 ret = OH_NativeImage_AcquireNativeWindowBuffer(image, &nativeWindowBuffer, &fenceFd);
1351 int32_t ret1 = OH_NativeImage_ReleaseNativeWindowBuffer(image, 0, fenceFd);
1352 if (ret == 0) {
1353 napi_create_int32(env, SUCCESS, &result1);
1354 } else {
1355 napi_create_int32(env, FAIL, &result1);
1356 }
1357 napi_set_element(env, result, ARR_NUMBER_0, result1);
1358 if (ret1 == 0) {
1359 napi_create_int32(env, SUCCESS, &result2);
1360 } else {
1361 napi_create_int32(env, FAIL, &result2);
1362 }
1363 napi_set_element(env, result, ARR_NUMBER_1, result2);
1364 OH_NativeImage_Destroy(&image);
1365 delete initNative;
1366 return result;
1367 }
1368
OHNativeImageAcquireNativeWindowBufferAbNormal02(napi_env env,napi_callback_info info)1369 static napi_value OHNativeImageAcquireNativeWindowBufferAbNormal02(napi_env env, napi_callback_info info)
1370 {
1371 napi_value result = nullptr;
1372 napi_value result1 = nullptr;
1373 napi_value result2 = nullptr;
1374 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1375 OH_NativeImage *image = OH_ConsumerSurface_Create();
1376 InitNativeWindow *initNative = new InitNativeWindow(image);
1377 int32_t ret = initNative->OH_FlushBuffer();
1378 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
1379 int fenceFd = -1;
1380 ret = OH_NativeImage_AcquireNativeWindowBuffer(nullptr, &nativeWindowBuffer, &fenceFd);
1381 int32_t ret1 = OH_NativeImage_ReleaseNativeWindowBuffer(nullptr, nativeWindowBuffer, fenceFd);
1382 if (ret == 0) {
1383 napi_create_int32(env, SUCCESS, &result1);
1384 } else {
1385 napi_create_int32(env, FAIL, &result1);
1386 }
1387 napi_set_element(env, result, ARR_NUMBER_0, result1);
1388 if (ret1 == 0) {
1389 napi_create_int32(env, SUCCESS, &result2);
1390 } else {
1391 napi_create_int32(env, FAIL, &result2);
1392 }
1393 napi_set_element(env, result, ARR_NUMBER_1, result2);
1394 OH_NativeImage_Destroy(&image);
1395 delete initNative;
1396 return result;
1397 }
1398
OHNativeImageAcquireNativeWindowBufferAbNormal03(napi_env env,napi_callback_info info)1399 static napi_value OHNativeImageAcquireNativeWindowBufferAbNormal03(napi_env env, napi_callback_info info)
1400 {
1401 napi_value result = nullptr;
1402 napi_value result1 = nullptr;
1403 napi_value result2 = nullptr;
1404 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1405 OH_NativeImage *image = OH_ConsumerSurface_Create();
1406 InitNativeWindow *initNative = new InitNativeWindow(image);
1407 int32_t ret = initNative->OH_FlushBuffer();
1408 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
1409 int fenceFd = -1;
1410 image = 0;
1411 ret = OH_NativeImage_AcquireNativeWindowBuffer(image, &nativeWindowBuffer, &fenceFd);
1412 int32_t ret1 = OH_NativeImage_ReleaseNativeWindowBuffer(image, nativeWindowBuffer, fenceFd);
1413 if (ret == 0) {
1414 napi_create_int32(env, SUCCESS, &result1);
1415 } else {
1416 napi_create_int32(env, FAIL, &result1);
1417 }
1418 napi_set_element(env, result, ARR_NUMBER_0, result1);
1419 if (ret1 == 0) {
1420 napi_create_int32(env, SUCCESS, &result2);
1421 } else {
1422 napi_create_int32(env, FAIL, &result2);
1423 }
1424 napi_set_element(env, result, ARR_NUMBER_1, result2);
1425 OH_NativeImage_Destroy(&image);
1426 delete initNative;
1427 return result;
1428 }
1429
OHNativeImageAcquireNativeWindowBufferAbNormal04(napi_env env,napi_callback_info info)1430 static napi_value OHNativeImageAcquireNativeWindowBufferAbNormal04(napi_env env, napi_callback_info info)
1431 {
1432 napi_value result = nullptr;
1433 napi_value result1 = nullptr;
1434 napi_value result2 = nullptr;
1435 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1436 OH_NativeImage *image = OH_ConsumerSurface_Create();
1437 InitNativeWindow *initNative = new InitNativeWindow(image);
1438 int32_t ret = initNative->OH_FlushBuffer();
1439 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
1440 int fenceFd = -1;
1441 int fenceFdTest = 0;
1442 ret = OH_NativeImage_AcquireNativeWindowBuffer(image, &nativeWindowBuffer, &fenceFdTest);
1443 ret = OH_NativeImage_AcquireNativeWindowBuffer(image, &nativeWindowBuffer, &fenceFd);
1444 int32_t ret1 = OH_NativeImage_ReleaseNativeWindowBuffer(image, nativeWindowBuffer, fenceFdTest);
1445 if (ret == 0) {
1446 napi_create_int32(env, SUCCESS, &result1);
1447 } else {
1448 napi_create_int32(env, FAIL, &result1);
1449 }
1450 napi_set_element(env, result, ARR_NUMBER_0, result1);
1451 if (ret1 == 0) {
1452 napi_create_int32(env, SUCCESS, &result2);
1453 } else {
1454 napi_create_int32(env, FAIL, &result2);
1455 }
1456 napi_set_element(env, result, ARR_NUMBER_1, result2);
1457 OH_NativeImage_Destroy(&image);
1458 delete initNative;
1459 return result;
1460 }
1461
OHNativeImageAcquireNativeWindowBufferAbNormal05(napi_env env,napi_callback_info info)1462 static napi_value OHNativeImageAcquireNativeWindowBufferAbNormal05(napi_env env, napi_callback_info info)
1463 {
1464 napi_value result = nullptr;
1465 napi_value result1 = nullptr;
1466 napi_value result2 = nullptr;
1467 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1468 OH_NativeImage *image = OH_ConsumerSurface_Create();
1469 InitNativeWindow *initNative = new InitNativeWindow(image);
1470 int32_t ret = initNative->OH_FlushBuffer();
1471 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
1472 int fenceFd = -1;
1473 ret = OH_NativeImage_ReleaseNativeWindowBuffer(image, nativeWindowBuffer, fenceFd);
1474 int32_t ret1 = OH_NativeImage_AcquireNativeWindowBuffer(image, &nativeWindowBuffer, &fenceFd);
1475 if (ret == 0) {
1476 napi_create_int32(env, SUCCESS, &result1);
1477 } else {
1478 napi_create_int32(env, FAIL, &result1);
1479 }
1480 napi_set_element(env, result, ARR_NUMBER_0, result1);
1481 if (ret1 == 0) {
1482 napi_create_int32(env, SUCCESS, &result2);
1483 } else {
1484 napi_create_int32(env, FAIL, &result2);
1485 }
1486 napi_set_element(env, result, ARR_NUMBER_1, result2);
1487 OH_NativeImage_Destroy(&image);
1488 delete initNative;
1489 return result;
1490 }
1491
OHNativeImageReleaseNativeWindowBufferAbnormal(napi_env env,napi_callback_info info)1492 static napi_value OHNativeImageReleaseNativeWindowBufferAbnormal(napi_env env, napi_callback_info info)
1493 {
1494 napi_value result = nullptr;
1495 OH_NativeImage *image = OH_ConsumerSurface_Create();
1496 InitNativeWindow *initNative = new InitNativeWindow(image);
1497 int32_t ret = initNative->OH_FlushBuffer();
1498 OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
1499 int fenceFd = -1;
1500 ret = OH_NativeImage_ReleaseNativeWindowBuffer(image, nativeWindowBuffer, fenceFd);
1501 if (ret == 0) {
1502 napi_create_int32(env, SUCCESS, &result);
1503 } else {
1504 napi_create_int32(env, FAIL, &result);
1505 }
1506 OH_NativeImage_Destroy(&image);
1507 delete initNative;
1508 return result;
1509 }
1510
OHConsumerSurfaceSetDefaultUsageNormal(napi_env env,napi_callback_info info)1511 static napi_value OHConsumerSurfaceSetDefaultUsageNormal(napi_env env, napi_callback_info info)
1512 {
1513 napi_value result = nullptr;
1514 napi_value result1 = nullptr;
1515 napi_value result2 = nullptr;
1516 napi_value result3 = nullptr;
1517 uint64_t num1 = 0, num2 = 1000, num3 = UINT64_MAX - 1;
1518 napi_create_array_with_length(env, ARR_NUMBER_4, &result);
1519 OH_NativeImage *image = OH_ConsumerSurface_Create();
1520 int32_t ret0 = OH_ConsumerSurface_SetDefaultUsage(image, num1);
1521 napi_create_int32(env, ret0, &result1);
1522 napi_set_element(env, result, ARR_NUMBER_0, result1);
1523 int32_t ret1 = OH_ConsumerSurface_SetDefaultUsage(image, num2);
1524 napi_create_int32(env, ret1, &result2);
1525 napi_set_element(env, result, ARR_NUMBER_1, result2);
1526 int32_t ret2 = OH_ConsumerSurface_SetDefaultUsage(image, num3);
1527 napi_create_int32(env, ret2, &result3);
1528 napi_set_element(env, result, ARR_NUMBER_2, result3);
1529 return result;
1530 }
1531
OHConsumerSurfaceSetDefaultUsageCall(napi_env env,napi_callback_info info)1532 static napi_value OHConsumerSurfaceSetDefaultUsageCall(napi_env env, napi_callback_info info)
1533 {
1534 napi_value result = nullptr;
1535 napi_value result1 = nullptr;
1536 napi_value result2 = nullptr;
1537 uint64_t num = 100;
1538 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1539 OH_NativeImage *image = OH_ConsumerSurface_Create();
1540 uint64_t usage[] = {0, 1000, UINT64_MAX - 1};
1541 for (int i = 0; i < ARR_NUMBER_3; i++) {
1542 usage[i] += 1;
1543 int32_t ret1 = OH_ConsumerSurface_SetDefaultUsage(image, usage[i]);
1544 napi_create_int32(env, ret1, &result1);
1545 }
1546 napi_set_element(env, result, ARR_NUMBER_0, result1);
1547 for (int i = 0; i < NUMBER_100000; i++) {
1548 int32_t ret2 = OH_ConsumerSurface_SetDefaultUsage(image, num);
1549 napi_create_int32(env, ret2, &result2);
1550 }
1551 napi_set_element(env, result, ARR_NUMBER_1, result2);
1552 OH_NativeImage_Destroy(&image);
1553 return result;
1554 }
1555
OHConsumerSurfaceSetDefaultUsageAbNormal(napi_env env,napi_callback_info info)1556 static napi_value OHConsumerSurfaceSetDefaultUsageAbNormal(napi_env env, napi_callback_info info)
1557 {
1558 napi_value result = nullptr;
1559 napi_value result1 = nullptr;
1560 napi_value result2 = nullptr;
1561 napi_value result3 = nullptr;
1562 napi_value result4 = nullptr;
1563 napi_value result5 = nullptr;
1564 uint64_t num = 100;
1565 napi_create_array_with_length(env, ARR_NUMBER_5, &result);
1566 OH_NativeImage *image = OH_ConsumerSurface_Create();
1567 int32_t ret = OH_ConsumerSurface_SetDefaultUsage(image, -ARR_NUMBER_1);
1568 napi_create_int32(env, ret, &result1);
1569 napi_set_element(env, result, ARR_NUMBER_0, result1);
1570 int32_t ret1 = OH_ConsumerSurface_SetDefaultUsage(image, -NUMBER_1000);
1571 napi_create_int32(env, ret1, &result2);
1572 napi_set_element(env, result, ARR_NUMBER_1, result2);
1573 int32_t ret2 = OH_ConsumerSurface_SetDefaultUsage(image, UINT64_MAX);
1574 napi_create_int32(env, ret2, &result3);
1575 napi_set_element(env, result, ARR_NUMBER_2, result3);
1576 int32_t ret3 = OH_ConsumerSurface_SetDefaultUsage(image, -UINT64_MAX);
1577 napi_create_int32(env, ret3, &result4);
1578 napi_set_element(env, result, ARR_NUMBER_3, result4);
1579 int32_t ret4 = OH_ConsumerSurface_SetDefaultUsage(nullptr, num);
1580 napi_create_int32(env, ret4, &result5);
1581 napi_set_element(env, result, ARR_NUMBER_4, result5);
1582 OH_NativeImage_Destroy(&image);
1583 return result;
1584 }
1585
OHConsumerSurfaceSetDefaultSizeNormal(napi_env env,napi_callback_info info)1586 static napi_value OHConsumerSurfaceSetDefaultSizeNormal(napi_env env, napi_callback_info info)
1587 {
1588 napi_value result = nullptr;
1589 napi_value result1 = nullptr;
1590 napi_value result2 = nullptr;
1591 napi_value result3 = nullptr;
1592 napi_value result4 = nullptr;
1593 napi_value result5 = nullptr;
1594 napi_value result6 = nullptr;
1595 napi_create_array_with_length(env, ARR_NUMBER_6, &result);
1596 OH_NativeImage *image = OH_ConsumerSurface_Create();
1597 int32_t res = 1 << 16;
1598 int32_t num1 = 100, num2 = 10000;
1599 int32_t ret = OH_ConsumerSurface_SetDefaultSize(image, ARR_NUMBER_1, ARR_NUMBER_1);
1600 napi_create_int32(env, ret, &result1);
1601 napi_set_element(env, result, ARR_NUMBER_0, result1);
1602 int32_t ret1 = OH_ConsumerSurface_SetDefaultSize(image, ARR_NUMBER_1, res - ARR_NUMBER_1);
1603 napi_create_int32(env, ret1, &result2);
1604 napi_set_element(env, result, ARR_NUMBER_1, result2);
1605 int32_t ret2 = OH_ConsumerSurface_SetDefaultSize(image, res - ARR_NUMBER_1, ARR_NUMBER_1);
1606 napi_create_int32(env, ret2, &result3);
1607 napi_set_element(env, result, ARR_NUMBER_2, result3);
1608 int32_t ret3 = OH_ConsumerSurface_SetDefaultSize(image, num1, num1);
1609 napi_create_int32(env, ret3, &result4);
1610 napi_set_element(env, result, ARR_NUMBER_3, result4);
1611 int32_t ret4 = OH_ConsumerSurface_SetDefaultSize(image, num2, num2);
1612 napi_create_int32(env, ret4, &result5);
1613 napi_set_element(env, result, ARR_NUMBER_4, result5);
1614 int32_t ret5 = OH_ConsumerSurface_SetDefaultSize(image, res - ARR_NUMBER_1, res - ARR_NUMBER_1);
1615 napi_create_int32(env, ret5, &result6);
1616 napi_set_element(env, result, ARR_NUMBER_5, result6);
1617 OH_NativeImage_Destroy(&image);
1618 return result;
1619 }
1620
OHConsumerSurfaceSetDefaultSizeCall(napi_env env,napi_callback_info info)1621 static napi_value OHConsumerSurfaceSetDefaultSizeCall(napi_env env, napi_callback_info info)
1622 {
1623 napi_value result = nullptr;
1624 napi_value result1 = nullptr;
1625 napi_value result2 = nullptr;
1626 napi_create_array_with_length(env, ARR_NUMBER_2, &result);
1627 OH_NativeImage *image = OH_ConsumerSurface_Create();
1628 int32_t w[] = {1, 100, 10000};
1629 int32_t h[] = {1, 100, 10000};
1630 for (int i = 0; i < ARR_NUMBER_3; i++) {
1631 w[i] += 1;
1632 h[i] += 1;
1633 int32_t ret = OH_ConsumerSurface_SetDefaultSize(image, w[i], h[i]);
1634 napi_create_int32(env, ret, &result1);
1635 }
1636 napi_set_element(env, result, ARR_NUMBER_0, result1);
1637 for (int i = 0; i < NUMBER_100000; i++) {
1638 int32_t ret1 = OH_ConsumerSurface_SetDefaultSize(image, ARR_NUMBER_1, ARR_NUMBER_1);
1639 napi_create_int32(env, ret1, &result2);
1640 }
1641 napi_set_element(env, result, ARR_NUMBER_1, result1);
1642 OH_NativeImage_Destroy(&image);
1643 return result;
1644 }
1645
OHConsumerSurfaceSetDefaultSizeAbNormal(napi_env env,napi_callback_info info)1646 static napi_value OHConsumerSurfaceSetDefaultSizeAbNormal(napi_env env, napi_callback_info info)
1647 {
1648 napi_value result = nullptr;
1649 napi_value result1 = nullptr;
1650 napi_value result2 = nullptr;
1651 napi_value result3 = nullptr;
1652 napi_value result4 = nullptr;
1653 napi_value result5 = nullptr;
1654 int32_t num1 = 0, num2 = 1, num3 = 1000;
1655 napi_create_array_with_length(env, ARR_NUMBER_5, &result);
1656 OH_NativeImage *image = OH_ConsumerSurface_Create();
1657 int32_t ret = OH_ConsumerSurface_SetDefaultSize(image, num2, num1);
1658 napi_create_int32(env, ret, &result1);
1659 napi_set_element(env, result, ARR_NUMBER_0, result1);
1660 int32_t ret1 = OH_ConsumerSurface_SetDefaultSize(image, -num2, num1);
1661 napi_create_int32(env, ret1, &result2);
1662 napi_set_element(env, result, ARR_NUMBER_1, result2);
1663 int32_t ret2 = OH_ConsumerSurface_SetDefaultSize(image, num1, -num2);
1664 napi_create_int32(env, ret2, &result3);
1665 napi_set_element(env, result, ARR_NUMBER_2, result3);
1666 int32_t ret3 = OH_ConsumerSurface_SetDefaultSize(image, -num3, -num3);
1667 napi_create_int32(env, ret3, &result4);
1668 napi_set_element(env, result, ARR_NUMBER_3, result4);
1669 int32_t ret4 = OH_ConsumerSurface_SetDefaultSize(image, num3, -num3);
1670 napi_create_int32(env, ret4, &result5);
1671 napi_set_element(env, result, ARR_NUMBER_4, result5);
1672 OH_NativeImage_Destroy(&image);
1673 return result;
1674 }
1675
OHConsumerSurfaceSetDefaultSizeAbNormal01(napi_env env,napi_callback_info info)1676 static napi_value OHConsumerSurfaceSetDefaultSizeAbNormal01(napi_env env, napi_callback_info info)
1677 {
1678 napi_value result = nullptr;
1679 napi_value result1 = nullptr;
1680 napi_value result2 = nullptr;
1681 napi_value result3 = nullptr;
1682 napi_value result4 = nullptr;
1683 napi_value result5 = nullptr;
1684 int32_t num1 = 1, num2 = 100;
1685 napi_create_array_with_length(env, ARR_NUMBER_5, &result);
1686 OH_NativeImage *image = OH_ConsumerSurface_Create();
1687 int32_t res = 1 << 16;
1688 int32_t ret = OH_ConsumerSurface_SetDefaultSize(image, -res, num2);
1689 napi_create_int32(env, ret, &result1);
1690 napi_set_element(env, result, ARR_NUMBER_0, result1);
1691 int32_t ret1 = OH_ConsumerSurface_SetDefaultSize(image, -num2, res - num1);
1692 napi_create_int32(env, ret1, &result2);
1693 napi_set_element(env, result, ARR_NUMBER_1, result2);
1694 int32_t ret2 = OH_ConsumerSurface_SetDefaultSize(image, -res, -res);
1695 napi_create_int32(env, ret2, &result3);
1696 napi_set_element(env, result, ARR_NUMBER_2, result3);
1697 int32_t ret3 = OH_ConsumerSurface_SetDefaultSize(image, res + num1, res + num1);
1698 napi_create_int32(env, ret3, &result4);
1699 napi_set_element(env, result, ARR_NUMBER_3, result4);
1700 int32_t ret4 = OH_ConsumerSurface_SetDefaultSize(nullptr, num2, num2);
1701 napi_create_int32(env, ret4, &result5);
1702 napi_set_element(env, result, ARR_NUMBER_4, result5);
1703 OH_NativeImage_Destroy(&image);
1704 return result;
1705 }
1706
oHConsumerSurfaceCreateAbNormal(napi_env env,napi_callback_info info)1707 static napi_value oHConsumerSurfaceCreateAbNormal(napi_env env, napi_callback_info info)
1708 {
1709 int backInfo = FAIL;
1710 OH_NativeImage *image = OH_ConsumerSurface_Create();
1711 OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
1712 int code = SET_BUFFER_GEOMETRY;
1713 int32_t width = PARAM_800;
1714 int32_t height = PARAM_600;
1715 OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
1716 OHNativeWindowBuffer *buffer = nullptr;
1717 int fenceFd;
1718 OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
1719 BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
1720 void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
1721 static uint32_t value = 0x00;
1722 value++;
1723 uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
1724 for (uint32_t x = PARAM_0; x < width; x++) {
1725 for (uint32_t y = PARAM_0; y < height; y++) {
1726 *pixel++ = value;
1727 }
1728 }
1729 munmap(mappedAddr, handle->size);
1730
1731 Region region{nullptr, PARAM_0};
1732 OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
1733 OH_NativeImage_UpdateSurfaceImage(image);
1734 OH_NativeImage_GetTimestamp(image);
1735 float matrix[16];
1736 OH_NativeImage_GetTransformMatrix(image, matrix);
1737 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
1738 OH_NativeImage_DetachContext(image);
1739 GLuint textureId2;
1740 glGenTextures(PARAM_1, &textureId2);
1741 backInfo = OH_NativeImage_AttachContext(image, textureId2);
1742 napi_value result = nullptr;
1743 napi_create_int32(env, backInfo, &result);
1744 OH_NativeImage_Destroy(&image);
1745 return result;
1746 }
1747
1748 std::map<std::uint64_t, int32_t> usage_num = {
1749 {NATIVEBUFFER_USAGE_CPU_READ | NATIVEBUFFER_USAGE_MEM_DMA, 9},
1750 {NATIVEBUFFER_USAGE_CPU_WRITE, 11},
1751 {NATIVEBUFFER_USAGE_HW_RENDER, 265},
1752 {NATIVEBUFFER_USAGE_HW_TEXTURE, 521},
1753 {NATIVEBUFFER_USAGE_CPU_READ_OFTEN, 65545},
1754 {NATIVEBUFFER_USAGE_ALIGNMENT_512, 262153},
1755 {NATIVEBUFFER_USAGE_CPU_WRITE | NATIVEBUFFER_USAGE_HW_RENDER | NATIVEBUFFER_USAGE_HW_TEXTURE |
1756 NATIVEBUFFER_USAGE_CPU_READ_OFTEN | NATIVEBUFFER_USAGE_ALIGNMENT_512,
1757 328459},
1758 };
1759
1760 std::map<std::uint64_t, int32_t> usage_size = {
1761 {NATIVEBUFFER_USAGE_CPU_READ | NATIVEBUFFER_USAGE_MEM_DMA, 10},
1762 {NATIVEBUFFER_USAGE_CPU_WRITE, 100},
1763 {NATIVEBUFFER_USAGE_HW_RENDER, 1000},
1764 {NATIVEBUFFER_USAGE_HW_TEXTURE, 10000},
1765 {NATIVEBUFFER_USAGE_CPU_READ_OFTEN, 10000},
1766 {NATIVEBUFFER_USAGE_ALIGNMENT_512, 10000},
1767 {NATIVEBUFFER_USAGE_CPU_WRITE | NATIVEBUFFER_USAGE_HW_RENDER | NATIVEBUFFER_USAGE_HW_TEXTURE |
1768 NATIVEBUFFER_USAGE_CPU_READ_OFTEN | NATIVEBUFFER_USAGE_ALIGNMENT_512,
1769 100},
1770 };
1771
OHConsumerSurfaceSetDefaultUsageSizeNormal(napi_env env,napi_callback_info info)1772 static napi_value OHConsumerSurfaceSetDefaultUsageSizeNormal(napi_env env, napi_callback_info info)
1773 {
1774 /**
1775 * usage非互斥关系,可能同时存在多个usage类型
1776 * usage枚举类型通过二进制实现
1777 * NATIVEBUFFER_USAGE_CPU_READ 1
1778 * NATIVEBUFFER_USAGE_CPU_WRITE 10
1779 * NATIVEBUFFER_USAGE_MEM_DMA 1000
1780 * NATIVEBUFFER_USAGE_HW_RENDER 100000000
1781 * NATIVEBUFFER_USAGE_HW_TEXTURE 1000000000
1782 * NATIVEBUFFER_USAGE_CPU_READ_OFTEN 10000000000000000
1783 * NATIVEBUFFER_USAGE_ALIGNMENT_512 1000000000000000000
1784 * buffer 默认有 NATIVEBUFFER_USAGE_CPU_READ和
1785 * NATIVEBUFFER_USAGE_MEM_DMA两个属性,查询到的默认值为1001(与或关系)(即8+1)
1786 */
1787 napi_value result = nullptr;
1788 for (const auto &pair : usage_size) {
1789 OHNativeWindowBuffer *windowBuffer = nullptr;
1790 OH_NativeBuffer *buffer = nullptr;
1791 OH_NativeBuffer_Config config = {
1792 .width = 0,
1793 .height = 0,
1794 .format = 0,
1795 .usage = 0,
1796 .stride = 0,
1797 };
1798 int fenceFd;
1799 OH_NativeImage *image = OH_ConsumerSurface_Create();
1800 int32_t ret0 = OH_ConsumerSurface_SetDefaultUsage(image, pair.first);
1801 int32_t ret1 = OH_ConsumerSurface_SetDefaultSize(image, pair.second, pair.second);
1802 OHNativeWindow *window = OH_NativeImage_AcquireNativeWindow(image);
1803 int32_t ret2 = ConsumerSurfaceBuffer(window, &windowBuffer, fenceFd);
1804 if (ret0 != 0 || ret1 != 0 || ret2 != 0 || windowBuffer == nullptr) {
1805 napi_create_int32(env, FAIL, &result);
1806 return result;
1807 }
1808 int32_t ret3 = OH_NativeBuffer_FromNativeWindowBuffer(windowBuffer, &buffer);
1809 OH_NativeBuffer_GetConfig(buffer, &config);
1810 auto num_usage = usage_num.find(pair.first);
1811 int32_t num = num_usage->second;
1812 if (ret3 != 0 || config.width != pair.second || config.height != pair.second || config.usage != num) {
1813 napi_create_int32(env, FAIL, &result);
1814 return result;
1815 }
1816 OH_NativeImage_Destroy(&image);
1817 OH_NativeWindow_DestroyNativeWindow(window);
1818 OH_NativeWindow_DestroyNativeWindowBuffer(windowBuffer);
1819 OH_NativeBuffer_Unreference(buffer);
1820 }
1821 napi_create_int32(env, SUCCESS, &result);
1822 return result;
1823 }
1824
OHNativeWindowDropBufferModeSetTrueNormal(napi_env env,napi_callback_info info)1825 static napi_value OHNativeWindowDropBufferModeSetTrueNormal(napi_env env, napi_callback_info info)
1826 {
1827 napi_value result = nullptr;
1828 InitEGLEnv();
1829 GLuint textureId;
1830 glGenTextures(PARAM_1, &textureId);
1831 OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
1832 int32_t ret1 = OH_NativeImage_SetDropBufferMode(image, false);
1833 int32_t ret2 = OH_NativeImage_SetDropBufferMode(image, true);
1834 if (ret1 != NATIVE_ERROR_OK || ret2 != NATIVE_ERROR_OK) {
1835 napi_create_int32(env, FAIL, &result);
1836 return result;
1837 }
1838 InitNativeWindow *initNative = new InitNativeWindow(image);
1839 int produceBuffer = 0;
1840 int consumerBuffer = 0;
1841 while (initNative->OH_FlushBuffer() == NATIVE_ERROR_OK) {
1842 produceBuffer++;
1843 }
1844 while (OH_NativeImage_UpdateSurfaceImage(image) == NATIVE_ERROR_OK) {
1845 consumerBuffer++;
1846 }
1847 if (consumerBuffer != PARAM_1 || produceBuffer != PARAM_3) {
1848 napi_create_int32(env, FAIL * PARAM_8, &result);
1849 return result;
1850 }
1851 produceBuffer = 0;
1852 consumerBuffer = 0;
1853 ret1 = OH_NativeImage_SetDropBufferMode(image, false);
1854 // produce_buffer = 2,
1855 // 是因为UpdateSurfaceImage消费完释放的是上一个buffer。由于上一次UpdateSurfaceImage失败导致其中一个buffer未释放
1856 while (initNative->OH_FlushBuffer() == NATIVE_ERROR_OK) {
1857 produceBuffer++;
1858 }
1859 while (OH_NativeImage_UpdateSurfaceImage(image) == NATIVE_ERROR_OK) {
1860 consumerBuffer++;
1861 }
1862 if (produceBuffer != PARAM_2 || consumerBuffer != PARAM_2) {
1863 napi_create_int32(env, FAIL * PARAM_16, &result);
1864 return result;
1865 }
1866 delete initNative;
1867 OH_NativeImage_Destroy(&image);
1868 napi_create_int32(env, SUCCESS, &result);
1869 return result;
1870 }
1871
OHNativeWindowDropBufferModeSetTrueNormal2(napi_env env,napi_callback_info info)1872 static napi_value OHNativeWindowDropBufferModeSetTrueNormal2(napi_env env, napi_callback_info info)
1873 {
1874 napi_value result = nullptr;
1875 InitEGLEnv();
1876 GLuint textureId;
1877 glGenTextures(PARAM_1, &textureId);
1878 OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
1879 int32_t ret1 = OH_NativeImage_SetDropBufferMode(image, false);
1880 if (ret1 != NATIVE_ERROR_OK) {
1881 napi_create_int32(env, FAIL, &result);
1882 return result;
1883 }
1884 InitNativeWindow *initNative = new InitNativeWindow(image);
1885 int produceBuffer = 0;
1886 int consumerBuffer = 0;
1887 while (initNative->OH_FlushBuffer() == NATIVE_ERROR_OK) {
1888 produceBuffer++;
1889 }
1890 while (OH_NativeImage_UpdateSurfaceImage(image) == NATIVE_ERROR_OK) {
1891 consumerBuffer++;
1892 }
1893 if (consumerBuffer != PARAM_3 || produceBuffer != PARAM_3) {
1894 napi_create_int32(env, FAIL * PARAM_8, &result);
1895 return result;
1896 }
1897 produceBuffer = 0;
1898 consumerBuffer = 0;
1899 ret1 = OH_NativeImage_SetDropBufferMode(image, true);
1900 // produce_buffer = 2,
1901 // 是因为UpdateSurfaceImage消费完释放的是上一个buffer。由于上一次UpdateSurfaceImage失败导致其中一个buffer未释放
1902 while (initNative->OH_FlushBuffer() == NATIVE_ERROR_OK) {
1903 produceBuffer++;
1904 }
1905 while (OH_NativeImage_UpdateSurfaceImage(image) == NATIVE_ERROR_OK) {
1906 consumerBuffer++;
1907 }
1908 if (consumerBuffer != PARAM_1 || produceBuffer != PARAM_2) {
1909 napi_create_int32(env, FAIL * PARAM_16, &result);
1910 return result;
1911 }
1912 delete initNative;
1913 OH_NativeImage_Destroy(&image);
1914 napi_create_int32(env, SUCCESS, &result);
1915 return result;
1916 }
1917
OHNativeWindowDropBufferModeSetAbNormal(napi_env env,napi_callback_info info)1918 static napi_value OHNativeWindowDropBufferModeSetAbNormal(napi_env env, napi_callback_info info)
1919 {
1920 napi_value result = nullptr;
1921 OH_NativeImage *image = nullptr;
1922 bool isOpen = true;
1923 int32_t ret = OH_NativeImage_SetDropBufferMode(image, isOpen);
1924 if (ret == NATIVE_ERROR_INVALID_ARGUMENTS) {
1925 napi_create_int32(env, SUCCESS, &result);
1926 return result;
1927 }
1928 napi_create_int32(env, FAIL, &result);
1929 return result;
1930 }
1931
NativeImageInit(napi_env env,napi_value exports)1932 napi_value NativeImageInit(napi_env env, napi_value exports)
1933 {
1934 napi_property_descriptor desc[] = {
1935 {"oHNativeImageCreate", nullptr, OHNativeImageCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
1936 {"oHNativeImageAcquireNativeWindow", nullptr, OHNativeImageAcquireNativeWindow, nullptr, nullptr, nullptr,
1937 napi_default, nullptr},
1938 {"oHNativeImageAcquireNativeWindowAbnormal", nullptr, OHNativeImageAcquireNativeWindowAbnormal, nullptr,
1939 nullptr, nullptr, napi_default, nullptr},
1940 {"oHNativeImageAttachContext", nullptr, OHNativeImageAttachContext, nullptr, nullptr, nullptr, napi_default,
1941 nullptr},
1942 {"oHNativeImageAttachContextAbnormal", nullptr, OHNativeImageAttachContextAbnormal, nullptr, nullptr, nullptr,
1943 napi_default, nullptr},
1944 {"oHNativeImageDetachContext", nullptr, OHNativeImageDetachContext, nullptr, nullptr, nullptr, napi_default,
1945 nullptr},
1946 {"oHNativeImageDetachContextAbnormal", nullptr, OHNativeImageDetachContextAbnormal, nullptr, nullptr, nullptr,
1947 napi_default, nullptr},
1948 {"oHNativeImageUpdateSurfaceImage", nullptr, OHNativeImageUpdateSurfaceImage, nullptr, nullptr, nullptr,
1949 napi_default, nullptr},
1950 {"oHNativeImageUpdateSurfaceImageBoundary", nullptr, OHNativeImageUpdateSurfaceImageBoundary, nullptr,
1951 nullptr, nullptr, napi_default, nullptr},
1952 {"oHNativeImageUpdateSurfaceImageAbnormal", nullptr, OHNativeImageUpdateSurfaceImageAbnormal, nullptr, nullptr,
1953 nullptr, napi_default, nullptr},
1954 {"oHNativeImageGetTimestamp", nullptr, OHNativeImageGetTimestamp, nullptr, nullptr, nullptr, napi_default,
1955 nullptr},
1956 {"oHNativeImageGetTransformMatrix", nullptr, OHNativeImageGetTransformMatrix, nullptr, nullptr, nullptr,
1957 napi_default, nullptr},
1958 {"oHNativeImageGetTransformMatrixAbnormal", nullptr, OHNativeImageGetTransformMatrixAbnormal, nullptr, nullptr,
1959 nullptr, napi_default, nullptr},
1960 {"oHNativeImageDestroy", nullptr, OHNativeImageDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
1961 {"oHNativeImageCreateNormal", nullptr, OHNativeImageCreateNormal, nullptr, nullptr, nullptr, napi_default,
1962 nullptr},
1963 {"oHNativeImageCreateAbnormal", nullptr, OHNativeImageCreateAbnormal, nullptr, nullptr, nullptr, napi_default,
1964 nullptr},
1965 {"oHNativeImageDestroy1", nullptr, OHNativeImageDestroy1, nullptr, nullptr, nullptr, napi_default, nullptr},
1966 {"oHNativeImageCreateMuch", nullptr, OHNativeImageCreateMuch, nullptr, nullptr, nullptr, napi_default, nullptr},
1967 {"oHNativeWindowDropBufferModeSetTrueNormal2", nullptr, OHNativeWindowDropBufferModeSetTrueNormal2,
1968 nullptr, nullptr, nullptr, napi_default, nullptr},
1969 {"oHNativeWindowDropBufferModeSetAbNormal", nullptr, OHNativeWindowDropBufferModeSetAbNormal,
1970 nullptr, nullptr, nullptr, napi_default, nullptr},
1971 };
1972 napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc);
1973 return exports;
1974 }
1975
NativeImageInit2(napi_env env,napi_value exports)1976 static napi_value NativeImageInit2(napi_env env, napi_value exports)
1977 {
1978 napi_property_descriptor desc[] = {
1979 {"oHNativeImageGetBufferMatrixNormal", nullptr, OHNativeImageGetBufferMatrixNormal, nullptr, nullptr, nullptr,
1980 napi_default, nullptr},
1981 {"oHNativeImageGetBufferMatrixNormal01", nullptr, OHNativeImageGetBufferMatrixNormal01, nullptr, nullptr,
1982 nullptr, napi_default, nullptr},
1983 {"oHNativeImageGetBufferMatrixAbNormal", nullptr, OHNativeImageGetBufferMatrixAbNormal, nullptr, nullptr,
1984 nullptr, napi_default, nullptr},
1985 {"oHNativeImageGetBufferMatrixCall", nullptr, OHNativeImageGetBufferMatrixCall, nullptr, nullptr, nullptr,
1986 napi_default, nullptr},
1987 {"oHNativeImageAcquireNativeWindowBufferNormal", nullptr, OHNativeImageAcquireNativeWindowBufferNormal, nullptr,
1988 nullptr, nullptr, napi_default, nullptr},
1989 {"oHNativeImageAcquireNativeWindowBufferCall", nullptr, OHNativeImageAcquireNativeWindowBufferCall, nullptr,
1990 nullptr, nullptr, napi_default, nullptr},
1991 {"oHNativeImageAcquireNativeWindowBufferAbNormal01", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal01,
1992 nullptr, nullptr, nullptr, napi_default, nullptr},
1993 {"oHNativeImageAcquireNativeWindowBufferAbNormal02", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal02,
1994 nullptr, nullptr, nullptr, napi_default, nullptr},
1995 {"oHNativeImageAcquireNativeWindowBufferAbNormal03", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal03,
1996 nullptr, nullptr, nullptr, napi_default, nullptr},
1997 {"oHNativeImageAcquireNativeWindowBufferAbNormal04", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal04,
1998 nullptr, nullptr, nullptr, napi_default, nullptr},
1999 {"oHNativeImageAcquireNativeWindowBufferAbNormal05", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal05,
2000 nullptr, nullptr, nullptr, napi_default, nullptr},
2001 {"oHNativeImageReleaseNativeWindowBufferAbnormal", nullptr, OHNativeImageReleaseNativeWindowBufferAbnormal,
2002 nullptr, nullptr, nullptr, napi_default, nullptr},
2003 {"oHConsumerSurfaceSetDefaultUsageNormal", nullptr, OHConsumerSurfaceSetDefaultUsageNormal, nullptr, nullptr,
2004 nullptr, napi_default, nullptr},
2005 {"oHConsumerSurfaceSetDefaultUsageCall", nullptr, OHConsumerSurfaceSetDefaultUsageCall, nullptr, nullptr,
2006 nullptr, napi_default, nullptr},
2007 {"oHConsumerSurfaceSetDefaultUsageAbNormal", nullptr, OHConsumerSurfaceSetDefaultUsageAbNormal, nullptr,
2008 nullptr, nullptr, napi_default, nullptr},
2009 {"oHConsumerSurfaceSetDefaultSizeNormal", nullptr, OHConsumerSurfaceSetDefaultSizeNormal, nullptr, nullptr,
2010 nullptr, napi_default, nullptr},
2011 {"oHConsumerSurfaceSetDefaultSizeCall", nullptr, OHConsumerSurfaceSetDefaultSizeCall, nullptr, nullptr, nullptr,
2012 napi_default, nullptr},
2013 {"oHConsumerSurfaceSetDefaultSizeAbNormal", nullptr, OHConsumerSurfaceSetDefaultSizeAbNormal, nullptr, nullptr,
2014 nullptr, napi_default, nullptr},
2015 {"oHConsumerSurfaceSetDefaultSizeAbNormal01", nullptr, OHConsumerSurfaceSetDefaultSizeAbNormal01, nullptr,
2016 nullptr, nullptr, napi_default, nullptr},
2017 {"oHConsumerSurfaceCreateAbNormal", nullptr, oHConsumerSurfaceCreateAbNormal, nullptr, nullptr, nullptr,
2018 napi_default, nullptr},
2019 {"OHConsumerSurfaceSetDefaultUsageSizeNormal", nullptr, OHConsumerSurfaceSetDefaultUsageSizeNormal,
2020 nullptr, nullptr, nullptr, napi_default, nullptr},
2021 {"oHNativeWindowDropBufferModeSetTrueNormal", nullptr, OHNativeWindowDropBufferModeSetTrueNormal,
2022 nullptr, nullptr, nullptr, napi_default, nullptr},
2023 };
2024 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
2025 return exports;
2026 }
2027
2028
2029 EXTERN_C_START
Init(napi_env env,napi_value exports)2030 static napi_value Init(napi_env env, napi_value exports)
2031 {
2032 NativeImageInit(env, exports);
2033 NativeImageInit2(env, exports);
2034 napi_property_descriptor desc[] = {
2035 {"oHNativeImageAcquireNativeWindowNullptr", nullptr, OHNativeImageAcquireNativeWindowNullptr, nullptr, nullptr,
2036 nullptr, napi_default, nullptr},
2037 {"oHNativeImageAcquireNativeWindowNormal", nullptr, OHNativeImageAcquireNativeWindowNormal, nullptr, nullptr,
2038 nullptr, napi_default, nullptr},
2039 {"oHNativeImageAttachContextNullptr", nullptr, OHNativeImageAttachContextNullptr, nullptr, nullptr, nullptr,
2040 napi_default, nullptr},
2041 {"oHNativeImageDetachContextNullptr", nullptr, OHNativeImageDetachContextNullptr, nullptr, nullptr, nullptr,
2042 napi_default, nullptr},
2043 {"oHNativeImageAttachContextNormal", nullptr, OHNativeImageAttachContextNormal, nullptr, nullptr, nullptr,
2044 napi_default, nullptr},
2045 {"oHNativeImageDetachContextNormal", nullptr, OHNativeImageDetachContextNormal, nullptr, nullptr, nullptr,
2046 napi_default, nullptr},
2047 {"oHNativeImageGetSurfaceIdNullptr", nullptr, OHNativeImageGetSurfaceIdNullptr, nullptr, nullptr, nullptr,
2048 napi_default, nullptr},
2049 {"oHNativeGetSurfaceIdNormal", nullptr, OHNativeGetSurfaceIdNormal, nullptr, nullptr, nullptr, napi_default,
2050 nullptr},
2051 {"oHNativeImageUpdateSurfaceImageNullptr", nullptr, OHNativeImageUpdateSurfaceImageNullptr, nullptr, nullptr,
2052 nullptr, napi_default, nullptr},
2053 {"oHNativeImageGetTimestampNullptr", nullptr, OHNativeImageGetTimestampNullptr, nullptr, nullptr, nullptr,
2054 napi_default, nullptr},
2055 {"oHNativeImageGetTransformMatrixNullptr", nullptr, OHNativeImageGetTransformMatrixNullptr, nullptr, nullptr,
2056 nullptr, napi_default, nullptr},
2057 {"oHNativeImageGetTransformMatrixV2Nullptr", nullptr, OHNativeImageGetTransformMatrixV2Nullptr, nullptr,
2058 nullptr, nullptr, napi_default, nullptr},
2059 {"oHNativeImageUpdateSurfaceImageNormal", nullptr, OHNativeImageUpdateSurfaceImageNormal, nullptr, nullptr,
2060 nullptr, napi_default, nullptr},
2061 {"oHNativeImageGetTimestampNormal", nullptr, OHNativeImageGetTimestampNormal, nullptr, nullptr, nullptr,
2062 napi_default, nullptr},
2063 {"oHNativeImageGetTransformMatrixNormal", nullptr, OHNativeImageGetTransformMatrixNormal, nullptr, nullptr,
2064 nullptr, napi_default, nullptr},
2065 {"oHNativeImageGetTransformMatrixV2Normal", nullptr, OHNativeImageGetTransformMatrixV2Normal, nullptr, nullptr,
2066 nullptr, napi_default, nullptr},
2067 {"oHNativeImageSetOnFrameAvailableListenerNullptr", nullptr, OHNativeImageSetOnFrameAvailableListenerNullptr,
2068 nullptr, nullptr, nullptr, napi_default, nullptr},
2069 {"oHNativeImageUnSetOnFrameAvailableListenerNullptr", nullptr,
2070 OHNativeImageUnSetOnFrameAvailableListenerNullptr, nullptr, nullptr, nullptr, napi_default, nullptr},
2071 {"oHNativeImageSetOnFrameAvailableListenerNormal", nullptr, OHNativeImageSetOnFrameAvailableListenerNormal,
2072 nullptr, nullptr, nullptr, napi_default, nullptr},
2073 {"oHNativeImageUnsetOnFrameAvailableListenerNormal", nullptr, OHNativeImageUnsetOnFrameAvailableListenerNormal,
2074 nullptr, nullptr, nullptr, napi_default, nullptr},
2075 };
2076 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
2077 return exports;
2078 }
2079 EXTERN_C_END
2080
2081 static napi_module demoModule = {
2082 .nm_version = 1,
2083 .nm_flags = 0,
2084 .nm_filename = nullptr,
2085 .nm_register_func = Init,
2086 .nm_modname = "nativeimage",
2087 .nm_priv = ((void *)0),
2088 .reserved = {0},
2089 };
2090
RegisterModule(void)2091 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); };
2092