1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include "napi/native_api.h"
17f6603c60Sopenharmony_ci#include <EGL/egl.h>
18f6603c60Sopenharmony_ci#include <EGL/eglext.h>
19f6603c60Sopenharmony_ci#include <GLES3/gl32.h>
20f6603c60Sopenharmony_ci#include <bits/alltypes.h>
21f6603c60Sopenharmony_ci#include <cstring>
22f6603c60Sopenharmony_ci#include <native_buffer/native_buffer.h>
23f6603c60Sopenharmony_ci#include <native_image/native_image.h>
24f6603c60Sopenharmony_ci#include <native_window/external_window.h>
25f6603c60Sopenharmony_ci#include <sys/mman.h>
26f6603c60Sopenharmony_ci#include <chrono>
27f6603c60Sopenharmony_ci
28f6603c60Sopenharmony_ci#define SUCCESS 0
29f6603c60Sopenharmony_ci#define PARAM_0 0
30f6603c60Sopenharmony_ci#define PARAM_8 8
31f6603c60Sopenharmony_ci#define PARAM_1 1
32f6603c60Sopenharmony_ci#define PARAM_16 16
33f6603c60Sopenharmony_ci#define PARAM_800 800
34f6603c60Sopenharmony_ci#define PARAM_600 600
35f6603c60Sopenharmony_ci#define FAIL (-1)
36f6603c60Sopenharmony_ci#define ONEVAL 1
37f6603c60Sopenharmony_ci#define NUMMAX 16
38f6603c60Sopenharmony_ci#define TWOVAL 2
39f6603c60Sopenharmony_ci#define ARR_NUMBER_0 0
40f6603c60Sopenharmony_ci#define ARR_NUMBER_1 1
41f6603c60Sopenharmony_ci#define ARR_NUMBER_2 2
42f6603c60Sopenharmony_ci#define ARR_NUMBER_3 3
43f6603c60Sopenharmony_ci#define ARR_NUMBER_4 4
44f6603c60Sopenharmony_ci#define NUMBER_99999 99999
45f6603c60Sopenharmony_ci#define NUMBER_500 500
46f6603c60Sopenharmony_ci
47f6603c60Sopenharmony_ciusing GetPlatformDisplayExt = PFNEGLGETPLATFORMDISPLAYEXTPROC;
48f6603c60Sopenharmony_ciconstexpr const char *EGL_EXT_PLATFORM_WAYLAND = "EGL_EXT_platform_wayland";
49f6603c60Sopenharmony_ciconstexpr const char *EGL_KHR_PLATFORM_WAYLAND = "EGL_KHR_platform_wayland";
50f6603c60Sopenharmony_ciconstexpr int32_t EGL_CONTEXT_CLIENT_VERSION_NUM = 2;
51f6603c60Sopenharmony_ciconstexpr char CHARACTER_WHITESPACE = ' ';
52f6603c60Sopenharmony_ciconstexpr const char *CHARACTER_STRING_WHITESPACE = " ";
53f6603c60Sopenharmony_ciconstexpr const char *EGL_GET_PLATFORM_DISPLAY_EXT = "eglGetPlatformDisplayEXT";
54f6603c60Sopenharmony_ciEGLContext eglContext_ = EGL_NO_CONTEXT;
55f6603c60Sopenharmony_ciEGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
56f6603c60Sopenharmony_cistatic inline EGLConfig config_;
57f6603c60Sopenharmony_ci
58f6603c60Sopenharmony_cistatic bool CheckEglExtension(const char *extensions, const char *extension)
59f6603c60Sopenharmony_ci{
60f6603c60Sopenharmony_ci    size_t extlen = strlen(extension);
61f6603c60Sopenharmony_ci    const char *end = extensions + strlen(extensions);
62f6603c60Sopenharmony_ci
63f6603c60Sopenharmony_ci    while (extensions < end) {
64f6603c60Sopenharmony_ci        size_t n = PARAM_0;
65f6603c60Sopenharmony_ci        if (*extensions == CHARACTER_WHITESPACE) {
66f6603c60Sopenharmony_ci            extensions++;
67f6603c60Sopenharmony_ci            continue;
68f6603c60Sopenharmony_ci        }
69f6603c60Sopenharmony_ci        n = strcspn(extensions, CHARACTER_STRING_WHITESPACE);
70f6603c60Sopenharmony_ci        if (n == extlen && strncmp(extension, extensions, n) == PARAM_0) {
71f6603c60Sopenharmony_ci            return true;
72f6603c60Sopenharmony_ci        }
73f6603c60Sopenharmony_ci        extensions += n;
74f6603c60Sopenharmony_ci    }
75f6603c60Sopenharmony_ci    return false;
76f6603c60Sopenharmony_ci}
77f6603c60Sopenharmony_ci
78f6603c60Sopenharmony_cistatic EGLDisplay GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list)
79f6603c60Sopenharmony_ci{
80f6603c60Sopenharmony_ci    static GetPlatformDisplayExt eglGetPlatformDisplayExt = nullptr;
81f6603c60Sopenharmony_ci
82f6603c60Sopenharmony_ci    if (!eglGetPlatformDisplayExt) {
83f6603c60Sopenharmony_ci        const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
84f6603c60Sopenharmony_ci        if (extensions && (CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) ||
85f6603c60Sopenharmony_ci                           CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) {
86f6603c60Sopenharmony_ci            eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT);
87f6603c60Sopenharmony_ci        }
88f6603c60Sopenharmony_ci    }
89f6603c60Sopenharmony_ci
90f6603c60Sopenharmony_ci    if (eglGetPlatformDisplayExt) {
91f6603c60Sopenharmony_ci        return eglGetPlatformDisplayExt(platform, native_display, attrib_list);
92f6603c60Sopenharmony_ci    }
93f6603c60Sopenharmony_ci
94f6603c60Sopenharmony_ci    return eglGetDisplay((EGLNativeDisplayType)native_display);
95f6603c60Sopenharmony_ci}
96f6603c60Sopenharmony_ci
97f6603c60Sopenharmony_cistatic void InitEGLEnv()
98f6603c60Sopenharmony_ci{
99f6603c60Sopenharmony_ci    eglDisplay_ = GetPlatformEglDisplay(EGL_PLATFORM_OHOS_KHR, EGL_DEFAULT_DISPLAY, nullptr);
100f6603c60Sopenharmony_ci    EGLint major, minor;
101f6603c60Sopenharmony_ci    eglInitialize(eglDisplay_, &major, &minor);
102f6603c60Sopenharmony_ci    eglBindAPI(EGL_OPENGL_ES_API);
103f6603c60Sopenharmony_ci    unsigned int ret;
104f6603c60Sopenharmony_ci    EGLint count;
105f6603c60Sopenharmony_ci    EGLint config_attribs[] = {EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE,        PARAM_8,
106f6603c60Sopenharmony_ci                               EGL_GREEN_SIZE,   PARAM_8,        EGL_BLUE_SIZE,       PARAM_8,
107f6603c60Sopenharmony_ci                               EGL_ALPHA_SIZE,   PARAM_8,        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
108f6603c60Sopenharmony_ci                               EGL_NONE};
109f6603c60Sopenharmony_ci
110f6603c60Sopenharmony_ci    ret = eglChooseConfig(eglDisplay_, config_attribs, &config_, PARAM_1, &count);
111f6603c60Sopenharmony_ci    static const EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_NUM, EGL_NONE};
112f6603c60Sopenharmony_ci    eglContext_ = eglCreateContext(eglDisplay_, config_, EGL_NO_CONTEXT, context_attribs);
113f6603c60Sopenharmony_ci    eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglContext_);
114f6603c60Sopenharmony_ci}
115f6603c60Sopenharmony_ci
116f6603c60Sopenharmony_cistatic OH_NativeImage *getNativeImage()
117f6603c60Sopenharmony_ci{
118f6603c60Sopenharmony_ci    GLuint textureId = SUCCESS;
119f6603c60Sopenharmony_ci    glGenTextures(ONEVAL, &textureId);
120f6603c60Sopenharmony_ci    OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
121f6603c60Sopenharmony_ci    return image;
122f6603c60Sopenharmony_ci}
123f6603c60Sopenharmony_ci
124f6603c60Sopenharmony_cistatic napi_value OHNativeImageCreate(napi_env env, napi_callback_info info)
125f6603c60Sopenharmony_ci{
126f6603c60Sopenharmony_ci    napi_value result = nullptr;
127f6603c60Sopenharmony_ci    OH_NativeImage *image = getNativeImage();
128f6603c60Sopenharmony_ci    int backInfo = FAIL;
129f6603c60Sopenharmony_ci    if (image != nullptr) {
130f6603c60Sopenharmony_ci        backInfo = SUCCESS;
131f6603c60Sopenharmony_ci        OH_NativeImage_Destroy(&image);
132f6603c60Sopenharmony_ci    }
133f6603c60Sopenharmony_ci
134f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
135f6603c60Sopenharmony_ci    return result;
136f6603c60Sopenharmony_ci}
137f6603c60Sopenharmony_ci
138f6603c60Sopenharmony_cistatic napi_value OHNativeImageAcquireNativeWindow(napi_env env, napi_callback_info info)
139f6603c60Sopenharmony_ci{
140f6603c60Sopenharmony_ci    napi_value result = nullptr;
141f6603c60Sopenharmony_ci    int backInfo = FAIL;
142f6603c60Sopenharmony_ci    OH_NativeImage *image = getNativeImage();
143f6603c60Sopenharmony_ci
144f6603c60Sopenharmony_ci    if (image != nullptr) {
145f6603c60Sopenharmony_ci        OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
146f6603c60Sopenharmony_ci        if (nativeWindow != nullptr) {
147f6603c60Sopenharmony_ci            backInfo = SUCCESS;
148f6603c60Sopenharmony_ci        }
149f6603c60Sopenharmony_ci        OH_NativeImage_Destroy(&image);
150f6603c60Sopenharmony_ci    }
151f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
152f6603c60Sopenharmony_ci    return result;
153f6603c60Sopenharmony_ci}
154f6603c60Sopenharmony_ci
155f6603c60Sopenharmony_cistatic napi_value OHNativeImageAcquireNativeWindowAbnormal(napi_env env, napi_callback_info info)
156f6603c60Sopenharmony_ci{
157f6603c60Sopenharmony_ci    napi_value result = nullptr;
158f6603c60Sopenharmony_ci    int backInfo = FAIL;
159f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(nullptr);
160f6603c60Sopenharmony_ci    if (nativeWindow != nullptr) {
161f6603c60Sopenharmony_ci        backInfo = SUCCESS;
162f6603c60Sopenharmony_ci    }
163f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
164f6603c60Sopenharmony_ci    return result;
165f6603c60Sopenharmony_ci}
166f6603c60Sopenharmony_ci
167f6603c60Sopenharmony_cistatic napi_value OHNativeImageAttachContext(napi_env env, napi_callback_info info)
168f6603c60Sopenharmony_ci{
169f6603c60Sopenharmony_ci    int backInfo = FAIL;
170f6603c60Sopenharmony_ci    InitEGLEnv();
171f6603c60Sopenharmony_ci    GLuint textureId;
172f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
173f6603c60Sopenharmony_ci    OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
174f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
175f6603c60Sopenharmony_ci    int code = SET_BUFFER_GEOMETRY;
176f6603c60Sopenharmony_ci    int32_t width = PARAM_800;
177f6603c60Sopenharmony_ci    int32_t height = PARAM_600;
178f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
179f6603c60Sopenharmony_ci    OHNativeWindowBuffer *buffer = nullptr;
180f6603c60Sopenharmony_ci    int fenceFd;
181f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
182f6603c60Sopenharmony_ci    BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
183f6603c60Sopenharmony_ci    void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
184f6603c60Sopenharmony_ci    static uint32_t value = 0x00;
185f6603c60Sopenharmony_ci    value++;
186f6603c60Sopenharmony_ci    uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
187f6603c60Sopenharmony_ci    for (uint32_t x = PARAM_0; x < width; x++) {
188f6603c60Sopenharmony_ci        for (uint32_t y = PARAM_0; y < height; y++) {
189f6603c60Sopenharmony_ci            *pixel++ = value;
190f6603c60Sopenharmony_ci        }
191f6603c60Sopenharmony_ci    }
192f6603c60Sopenharmony_ci    munmap(mappedAddr, handle->size);
193f6603c60Sopenharmony_ci
194f6603c60Sopenharmony_ci    Region region{nullptr, PARAM_0};
195f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
196f6603c60Sopenharmony_ci    OH_NativeImage_UpdateSurfaceImage(image);
197f6603c60Sopenharmony_ci    OH_NativeImage_GetTimestamp(image);
198f6603c60Sopenharmony_ci    float matrix[16];
199f6603c60Sopenharmony_ci    OH_NativeImage_GetTransformMatrix(image, matrix);
200f6603c60Sopenharmony_ci    OH_NativeWindow_DestroyNativeWindow(nativeWindow);
201f6603c60Sopenharmony_ci    OH_NativeImage_DetachContext(image);
202f6603c60Sopenharmony_ci    GLuint textureId2;
203f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId2);
204f6603c60Sopenharmony_ci    backInfo = OH_NativeImage_AttachContext(image, textureId2);
205f6603c60Sopenharmony_ci    napi_value result = nullptr;
206f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
207f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&image);
208f6603c60Sopenharmony_ci    return result;
209f6603c60Sopenharmony_ci}
210f6603c60Sopenharmony_ci
211f6603c60Sopenharmony_cistatic napi_value OHNativeImageAttachContextAbnormal(napi_env env, napi_callback_info info)
212f6603c60Sopenharmony_ci{
213f6603c60Sopenharmony_ci    GLuint textureId2;
214f6603c60Sopenharmony_ci    glGenTextures(ONEVAL, &textureId2);
215f6603c60Sopenharmony_ci    int backInfo = OH_NativeImage_AttachContext(nullptr, textureId2);
216f6603c60Sopenharmony_ci    if (backInfo != SUCCESS) {
217f6603c60Sopenharmony_ci        backInfo = FAIL;
218f6603c60Sopenharmony_ci    }
219f6603c60Sopenharmony_ci    napi_value result = nullptr;
220f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
221f6603c60Sopenharmony_ci    return result;
222f6603c60Sopenharmony_ci}
223f6603c60Sopenharmony_ci
224f6603c60Sopenharmony_cistatic napi_value OHNativeImageDetachContext(napi_env env, napi_callback_info info)
225f6603c60Sopenharmony_ci{
226f6603c60Sopenharmony_ci    int backInfo = FAIL;
227f6603c60Sopenharmony_ci    InitEGLEnv();
228f6603c60Sopenharmony_ci    GLuint textureId;
229f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
230f6603c60Sopenharmony_ci    OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
231f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
232f6603c60Sopenharmony_ci    int code = SET_BUFFER_GEOMETRY;
233f6603c60Sopenharmony_ci    int32_t width = PARAM_800;
234f6603c60Sopenharmony_ci    int32_t height = PARAM_600;
235f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
236f6603c60Sopenharmony_ci    OHNativeWindowBuffer *buffer = nullptr;
237f6603c60Sopenharmony_ci    int fenceFd;
238f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
239f6603c60Sopenharmony_ci    BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
240f6603c60Sopenharmony_ci    void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
241f6603c60Sopenharmony_ci    static uint32_t value = 0x00;
242f6603c60Sopenharmony_ci    value++;
243f6603c60Sopenharmony_ci    uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
244f6603c60Sopenharmony_ci    for (uint32_t x = PARAM_0; x < width; x++) {
245f6603c60Sopenharmony_ci        for (uint32_t y = PARAM_0; y < height; y++) {
246f6603c60Sopenharmony_ci            *pixel++ = value;
247f6603c60Sopenharmony_ci        }
248f6603c60Sopenharmony_ci    }
249f6603c60Sopenharmony_ci    munmap(mappedAddr, handle->size);
250f6603c60Sopenharmony_ci
251f6603c60Sopenharmony_ci    Region region{nullptr, PARAM_0};
252f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
253f6603c60Sopenharmony_ci    OH_NativeImage_UpdateSurfaceImage(image);
254f6603c60Sopenharmony_ci    OH_NativeImage_GetTimestamp(image);
255f6603c60Sopenharmony_ci    float matrix[16];
256f6603c60Sopenharmony_ci    OH_NativeImage_GetTransformMatrix(image, matrix);
257f6603c60Sopenharmony_ci    OH_NativeWindow_DestroyNativeWindow(nativeWindow);
258f6603c60Sopenharmony_ci    backInfo = OH_NativeImage_DetachContext(image);
259f6603c60Sopenharmony_ci    GLuint textureId2;
260f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId2);
261f6603c60Sopenharmony_ci    OH_NativeImage_AttachContext(image, textureId2);
262f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&image);
263f6603c60Sopenharmony_ci    napi_value result = nullptr;
264f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
265f6603c60Sopenharmony_ci    return result;
266f6603c60Sopenharmony_ci}
267f6603c60Sopenharmony_ci
268f6603c60Sopenharmony_cistatic napi_value OHNativeImageDetachContextAbnormal(napi_env env, napi_callback_info info)
269f6603c60Sopenharmony_ci{
270f6603c60Sopenharmony_ci    int backInfo = OH_NativeImage_DetachContext(nullptr);
271f6603c60Sopenharmony_ci    if (backInfo != SUCCESS) {
272f6603c60Sopenharmony_ci        backInfo = FAIL;
273f6603c60Sopenharmony_ci    }
274f6603c60Sopenharmony_ci    napi_value result = nullptr;
275f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
276f6603c60Sopenharmony_ci    return result;
277f6603c60Sopenharmony_ci}
278f6603c60Sopenharmony_ci
279f6603c60Sopenharmony_cistatic napi_value OHNativeImageUpdateSurfaceImage(napi_env env, napi_callback_info info)
280f6603c60Sopenharmony_ci{
281f6603c60Sopenharmony_ci    InitEGLEnv();
282f6603c60Sopenharmony_ci    GLuint textureId;
283f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
284f6603c60Sopenharmony_ci    OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
285f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
286f6603c60Sopenharmony_ci    int code = SET_BUFFER_GEOMETRY;
287f6603c60Sopenharmony_ci    int32_t width = PARAM_800;
288f6603c60Sopenharmony_ci    int32_t height = PARAM_600;
289f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
290f6603c60Sopenharmony_ci    OHNativeWindowBuffer *buffer = nullptr;
291f6603c60Sopenharmony_ci    int fenceFd;
292f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
293f6603c60Sopenharmony_ci    BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
294f6603c60Sopenharmony_ci    void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
295f6603c60Sopenharmony_ci    static uint32_t value = 0x00;
296f6603c60Sopenharmony_ci    value++;
297f6603c60Sopenharmony_ci    uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
298f6603c60Sopenharmony_ci    for (uint32_t x = PARAM_0; x < width; x++) {
299f6603c60Sopenharmony_ci        for (uint32_t y = PARAM_0; y < height; y++) {
300f6603c60Sopenharmony_ci            *pixel++ = value;
301f6603c60Sopenharmony_ci        }
302f6603c60Sopenharmony_ci    }
303f6603c60Sopenharmony_ci    munmap(mappedAddr, handle->size);
304f6603c60Sopenharmony_ci    Region region{nullptr, PARAM_0};
305f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
306f6603c60Sopenharmony_ci    int backInfo = OH_NativeImage_UpdateSurfaceImage(image);
307f6603c60Sopenharmony_ci    OH_NativeImage_GetTimestamp(image);
308f6603c60Sopenharmony_ci    float matrix[16];
309f6603c60Sopenharmony_ci    OH_NativeImage_GetTransformMatrix(image, matrix);
310f6603c60Sopenharmony_ci    OH_NativeWindow_DestroyNativeWindow(nativeWindow);
311f6603c60Sopenharmony_ci    OH_NativeImage_DetachContext(image);
312f6603c60Sopenharmony_ci    GLuint textureId2;
313f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId2);
314f6603c60Sopenharmony_ci    OH_NativeImage_AttachContext(image, textureId2);
315f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&image);
316f6603c60Sopenharmony_ci    napi_value result = nullptr;
317f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
318f6603c60Sopenharmony_ci    return result;
319f6603c60Sopenharmony_ci}
320f6603c60Sopenharmony_ci
321f6603c60Sopenharmony_cistatic napi_value OHNativeImageUpdateSurfaceImageBoundary(napi_env env, napi_callback_info info)
322f6603c60Sopenharmony_ci{
323f6603c60Sopenharmony_ci    InitEGLEnv();
324f6603c60Sopenharmony_ci    GLuint textureId;
325f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
326f6603c60Sopenharmony_ci    OH_NativeImage *image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
327f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
328f6603c60Sopenharmony_ci    int code = SET_BUFFER_GEOMETRY;
329f6603c60Sopenharmony_ci    int32_t width = 4096;
330f6603c60Sopenharmony_ci    int32_t height = 2160;
331f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
332f6603c60Sopenharmony_ci    OHNativeWindowBuffer *buffer = nullptr;
333f6603c60Sopenharmony_ci    int fenceFd;
334f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
335f6603c60Sopenharmony_ci    BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
336f6603c60Sopenharmony_ci    void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, PARAM_0);
337f6603c60Sopenharmony_ci    static uint32_t value = 0x00;
338f6603c60Sopenharmony_ci    value++;
339f6603c60Sopenharmony_ci    uint32_t *pixel = static_cast<uint32_t *>(mappedAddr);
340f6603c60Sopenharmony_ci    for (uint32_t x = PARAM_0; x < width; x++) {
341f6603c60Sopenharmony_ci        for (uint32_t y = PARAM_0; y < height; y++) {
342f6603c60Sopenharmony_ci            *pixel++ = value;
343f6603c60Sopenharmony_ci        }
344f6603c60Sopenharmony_ci    }
345f6603c60Sopenharmony_ci    munmap(mappedAddr, handle->size);
346f6603c60Sopenharmony_ci    Region region{nullptr, PARAM_0};
347f6603c60Sopenharmony_ci    OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region);
348f6603c60Sopenharmony_ci    int backInfo = OH_NativeImage_UpdateSurfaceImage(image);
349f6603c60Sopenharmony_ci    OH_NativeImage_GetTimestamp(image);
350f6603c60Sopenharmony_ci    float matrix[16];
351f6603c60Sopenharmony_ci    OH_NativeImage_GetTransformMatrix(image, matrix);
352f6603c60Sopenharmony_ci    OH_NativeWindow_DestroyNativeWindow(nativeWindow);
353f6603c60Sopenharmony_ci    OH_NativeImage_DetachContext(image);
354f6603c60Sopenharmony_ci    GLuint textureId2;
355f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId2);
356f6603c60Sopenharmony_ci    OH_NativeImage_AttachContext(image, textureId2);
357f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&image);
358f6603c60Sopenharmony_ci    napi_value result = nullptr;
359f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
360f6603c60Sopenharmony_ci    return result;
361f6603c60Sopenharmony_ci}
362f6603c60Sopenharmony_ci
363f6603c60Sopenharmony_cistatic napi_value OHNativeImageUpdateSurfaceImageAbnormal(napi_env env, napi_callback_info info)
364f6603c60Sopenharmony_ci{
365f6603c60Sopenharmony_ci    int backInfo = OH_NativeImage_UpdateSurfaceImage(nullptr);
366f6603c60Sopenharmony_ci    if (backInfo != SUCCESS) {
367f6603c60Sopenharmony_ci        backInfo = FAIL;
368f6603c60Sopenharmony_ci    }
369f6603c60Sopenharmony_ci    napi_value result = nullptr;
370f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
371f6603c60Sopenharmony_ci    return result;
372f6603c60Sopenharmony_ci}
373f6603c60Sopenharmony_ci
374f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTimestamp(napi_env env, napi_callback_info info)
375f6603c60Sopenharmony_ci{
376f6603c60Sopenharmony_ci    int backInfo = FAIL;
377f6603c60Sopenharmony_ci    OH_NativeImage *image = getNativeImage();
378f6603c60Sopenharmony_ci    if (image != nullptr) {
379f6603c60Sopenharmony_ci        backInfo = OH_NativeImage_GetTimestamp(image);
380f6603c60Sopenharmony_ci        OH_NativeImage_Destroy(&image);
381f6603c60Sopenharmony_ci    }
382f6603c60Sopenharmony_ci    napi_value result = nullptr;
383f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
384f6603c60Sopenharmony_ci    return result;
385f6603c60Sopenharmony_ci}
386f6603c60Sopenharmony_ci
387f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTransformMatrix(napi_env env, napi_callback_info info)
388f6603c60Sopenharmony_ci{
389f6603c60Sopenharmony_ci    int backInfo = FAIL;
390f6603c60Sopenharmony_ci    OH_NativeImage *image = getNativeImage();
391f6603c60Sopenharmony_ci    if (image != nullptr) {
392f6603c60Sopenharmony_ci        float matrix[NUMMAX];
393f6603c60Sopenharmony_ci        backInfo = OH_NativeImage_GetTransformMatrix(image, matrix);
394f6603c60Sopenharmony_ci        OH_NativeImage_Destroy(&image);
395f6603c60Sopenharmony_ci    }
396f6603c60Sopenharmony_ci    napi_value result = nullptr;
397f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
398f6603c60Sopenharmony_ci    return result;
399f6603c60Sopenharmony_ci}
400f6603c60Sopenharmony_ci
401f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTransformMatrixAbnormal(napi_env env, napi_callback_info info)
402f6603c60Sopenharmony_ci{
403f6603c60Sopenharmony_ci    int backInfo = FAIL;
404f6603c60Sopenharmony_ci    float matrix[NUMMAX];
405f6603c60Sopenharmony_ci    int ret = OH_NativeImage_GetTransformMatrix(nullptr, matrix);
406f6603c60Sopenharmony_ci    if (ret != SUCCESS) {
407f6603c60Sopenharmony_ci        backInfo = FAIL;
408f6603c60Sopenharmony_ci    }
409f6603c60Sopenharmony_ci
410f6603c60Sopenharmony_ci    napi_value result = nullptr;
411f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
412f6603c60Sopenharmony_ci    return result;
413f6603c60Sopenharmony_ci}
414f6603c60Sopenharmony_ci
415f6603c60Sopenharmony_cistatic napi_value OHNativeImageDestroy(napi_env env, napi_callback_info info)
416f6603c60Sopenharmony_ci{
417f6603c60Sopenharmony_ci    int backInfo = FAIL;
418f6603c60Sopenharmony_ci    OH_NativeImage *image = getNativeImage();
419f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&image);
420f6603c60Sopenharmony_ci    if (image == nullptr) {
421f6603c60Sopenharmony_ci        backInfo = SUCCESS;
422f6603c60Sopenharmony_ci    }
423f6603c60Sopenharmony_ci
424f6603c60Sopenharmony_ci    napi_value result = nullptr;
425f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
426f6603c60Sopenharmony_ci    return result;
427f6603c60Sopenharmony_ci}
428f6603c60Sopenharmony_ci
429f6603c60Sopenharmony_cistatic napi_value OHNativeImageCreateNormal(napi_env env, napi_callback_info info)
430f6603c60Sopenharmony_ci{
431f6603c60Sopenharmony_ci    int backInfo = FAIL;
432f6603c60Sopenharmony_ci    OH_NativeImage *image = nullptr;
433f6603c60Sopenharmony_ci    InitEGLEnv();
434f6603c60Sopenharmony_ci    GLuint textureId;
435f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
436f6603c60Sopenharmony_ci    GLuint textureTarget = GL_TEXTURE_2D;
437f6603c60Sopenharmony_ci    image = OH_NativeImage_Create(textureId, textureTarget);
438f6603c60Sopenharmony_ci    if (image != nullptr) {
439f6603c60Sopenharmony_ci        backInfo = SUCCESS;
440f6603c60Sopenharmony_ci    }
441f6603c60Sopenharmony_ci    napi_value result = nullptr;
442f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
443f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&image);
444f6603c60Sopenharmony_ci    return result;
445f6603c60Sopenharmony_ci}
446f6603c60Sopenharmony_ci
447f6603c60Sopenharmony_cistatic napi_value OHNativeImageCreateAbnormal(napi_env env, napi_callback_info info)
448f6603c60Sopenharmony_ci{
449f6603c60Sopenharmony_ci    int backInfo = FAIL;
450f6603c60Sopenharmony_ci
451f6603c60Sopenharmony_ci    OH_NativeImage *image = OH_NativeImage_Create(0, 1);
452f6603c60Sopenharmony_ci    if (image != nullptr) {
453f6603c60Sopenharmony_ci        backInfo = SUCCESS;
454f6603c60Sopenharmony_ci    }
455f6603c60Sopenharmony_ci    napi_value result = nullptr;
456f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
457f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&image);
458f6603c60Sopenharmony_ci    return result;
459f6603c60Sopenharmony_ci}
460f6603c60Sopenharmony_ci
461f6603c60Sopenharmony_cistatic napi_value OHNativeImageDestroy1(napi_env env, napi_callback_info info)
462f6603c60Sopenharmony_ci{
463f6603c60Sopenharmony_ci    int backInfo = FAIL;
464f6603c60Sopenharmony_ci    OH_NativeImage *image = nullptr;
465f6603c60Sopenharmony_ci    GLenum nativeImageTexId_;
466f6603c60Sopenharmony_ci    GLuint GL_TEXTURE_EXTERNAL_OES;
467f6603c60Sopenharmony_ci    glGenTextures(1, &nativeImageTexId_);
468f6603c60Sopenharmony_ci    glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
469f6603c60Sopenharmony_ci    image = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
470f6603c60Sopenharmony_ci    if (image != nullptr) {
471f6603c60Sopenharmony_ci        OH_NativeImage_Destroy(&image);
472f6603c60Sopenharmony_ci        if (image == nullptr) {
473f6603c60Sopenharmony_ci            backInfo = SUCCESS;
474f6603c60Sopenharmony_ci        }
475f6603c60Sopenharmony_ci    }
476f6603c60Sopenharmony_ci    napi_value result = nullptr;
477f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
478f6603c60Sopenharmony_ci    return result;
479f6603c60Sopenharmony_ci}
480f6603c60Sopenharmony_ci
481f6603c60Sopenharmony_cistatic napi_value OHNativeImageCreateMuch(napi_env env, napi_callback_info info)
482f6603c60Sopenharmony_ci{
483f6603c60Sopenharmony_ci    int backInfo = SUCCESS;
484f6603c60Sopenharmony_ci    const int count = NUMBER_500;
485f6603c60Sopenharmony_ci    OH_NativeImage *imageArray[500] = {nullptr};
486f6603c60Sopenharmony_ci    for (int i = 0; i < count; ++i) {
487f6603c60Sopenharmony_ci        GLuint GL_TEXTURE_EXTERNAL_OES;
488f6603c60Sopenharmony_ci        glBindTexture(GL_TEXTURE_EXTERNAL_OES, 1);
489f6603c60Sopenharmony_ci        imageArray[i] = OH_NativeImage_Create(1, GL_TEXTURE_EXTERNAL_OES);
490f6603c60Sopenharmony_ci        if (imageArray[i] == nullptr) {
491f6603c60Sopenharmony_ci            backInfo = FAIL;
492f6603c60Sopenharmony_ci            break;
493f6603c60Sopenharmony_ci        }
494f6603c60Sopenharmony_ci    }
495f6603c60Sopenharmony_ci
496f6603c60Sopenharmony_ci    for (int i = 0; i < count; ++i) {
497f6603c60Sopenharmony_ci        if (imageArray[i] != nullptr) {
498f6603c60Sopenharmony_ci            OH_NativeImage_Destroy(&imageArray[i]); // 销毁单个图像
499f6603c60Sopenharmony_ci            if (imageArray[i] != nullptr) {
500f6603c60Sopenharmony_ci                backInfo = FAIL;
501f6603c60Sopenharmony_ci                break;
502f6603c60Sopenharmony_ci            }
503f6603c60Sopenharmony_ci        }
504f6603c60Sopenharmony_ci    }
505f6603c60Sopenharmony_ci
506f6603c60Sopenharmony_ci    napi_value result = nullptr;
507f6603c60Sopenharmony_ci    if (backInfo == FAIL) {
508f6603c60Sopenharmony_ci        napi_create_int32(env, backInfo, &result);
509f6603c60Sopenharmony_ci    } else {
510f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
511f6603c60Sopenharmony_ci    }
512f6603c60Sopenharmony_ci
513f6603c60Sopenharmony_ci    return result;
514f6603c60Sopenharmony_ci}
515f6603c60Sopenharmony_ci
516f6603c60Sopenharmony_cistatic napi_value OHNativeImageAcquireNativeWindowNullptr(napi_env env, napi_callback_info info)
517f6603c60Sopenharmony_ci{
518f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow = nullptr;
519f6603c60Sopenharmony_ci    nativeWindow = OH_NativeImage_AcquireNativeWindow(nullptr);
520f6603c60Sopenharmony_ci    napi_value result = nullptr;
521f6603c60Sopenharmony_ci    if (nativeWindow != nullptr) {
522f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
523f6603c60Sopenharmony_ci    } else {
524f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
525f6603c60Sopenharmony_ci    }
526f6603c60Sopenharmony_ci    return result;
527f6603c60Sopenharmony_ci}
528f6603c60Sopenharmony_ci
529f6603c60Sopenharmony_cistatic napi_value OHNativeImageAcquireNativeWindowNormal(napi_env env, napi_callback_info info)
530f6603c60Sopenharmony_ci{
531f6603c60Sopenharmony_ci    int backInfo = FAIL;
532f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow1 = nullptr;
533f6603c60Sopenharmony_ci    OHNativeWindow *nativeWindow2 = nullptr;
534f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
535f6603c60Sopenharmony_ci    nativeWindow1 = OH_NativeImage_AcquireNativeWindow(nativeImage);
536f6603c60Sopenharmony_ci    if (nativeWindow1 != nullptr) {
537f6603c60Sopenharmony_ci        nativeWindow2 = OH_NativeImage_AcquireNativeWindow(nativeImage);
538f6603c60Sopenharmony_ci        if (nativeWindow1 == nativeWindow2) {
539f6603c60Sopenharmony_ci            backInfo = SUCCESS;
540f6603c60Sopenharmony_ci        }
541f6603c60Sopenharmony_ci    }
542f6603c60Sopenharmony_ci
543f6603c60Sopenharmony_ci    napi_value result = nullptr;
544f6603c60Sopenharmony_ci    napi_create_int32(env, backInfo, &result);
545f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
546f6603c60Sopenharmony_ci    return result;
547f6603c60Sopenharmony_ci}
548f6603c60Sopenharmony_ci
549f6603c60Sopenharmony_cistatic napi_value OHNativeImageAttachContextNullptr(napi_env env, napi_callback_info info)
550f6603c60Sopenharmony_ci{
551f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
552f6603c60Sopenharmony_ci    InitEGLEnv();
553f6603c60Sopenharmony_ci    GLuint textureId;
554f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
555f6603c60Sopenharmony_ci    napi_value result = nullptr;
556f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_2, &result);
557f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
558f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
559f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_AttachContext(nullptr, textureId);
560f6603c60Sopenharmony_ci    if (res1 != 0) {
561f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
562f6603c60Sopenharmony_ci    } else {
563f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
564f6603c60Sopenharmony_ci    }
565f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
566f6603c60Sopenharmony_ci
567f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_AttachContext(nativeImage, NULL);
568f6603c60Sopenharmony_ci    if (res2 != 0) {
569f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
570f6603c60Sopenharmony_ci    } else {
571f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
572f6603c60Sopenharmony_ci    }
573f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
574f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
575f6603c60Sopenharmony_ci    return result;
576f6603c60Sopenharmony_ci}
577f6603c60Sopenharmony_ci
578f6603c60Sopenharmony_cistatic napi_value OHNativeImageDetachContextNullptr(napi_env env, napi_callback_info info)
579f6603c60Sopenharmony_ci{
580f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
581f6603c60Sopenharmony_ci    InitEGLEnv();
582f6603c60Sopenharmony_ci    GLuint textureId;
583f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
584f6603c60Sopenharmony_ci    napi_value result = nullptr;
585f6603c60Sopenharmony_ci    int res = OH_NativeImage_DetachContext(nullptr);
586f6603c60Sopenharmony_ci    if (res != 0) {
587f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
588f6603c60Sopenharmony_ci    } else {
589f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
590f6603c60Sopenharmony_ci    }
591f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
592f6603c60Sopenharmony_ci    return result;
593f6603c60Sopenharmony_ci}
594f6603c60Sopenharmony_ci
595f6603c60Sopenharmony_cistatic napi_value OHNativeImageAttachContextNormal(napi_env env, napi_callback_info info)
596f6603c60Sopenharmony_ci{
597f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
598f6603c60Sopenharmony_ci    InitEGLEnv();
599f6603c60Sopenharmony_ci    GLuint textureId;
600f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
601f6603c60Sopenharmony_ci    napi_value result = nullptr;
602f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_2, &result);
603f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
604f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
605f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_AttachContext(nativeImage, textureId);
606f6603c60Sopenharmony_ci    if (res1 == 0) {
607f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
608f6603c60Sopenharmony_ci    } else {
609f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
610f6603c60Sopenharmony_ci    }
611f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
612f6603c60Sopenharmony_ci
613f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_AttachContext(nativeImage, textureId);
614f6603c60Sopenharmony_ci    if (res2 == 0) {
615f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
616f6603c60Sopenharmony_ci    } else {
617f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
618f6603c60Sopenharmony_ci    }
619f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
620f6603c60Sopenharmony_ci
621f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
622f6603c60Sopenharmony_ci    return result;
623f6603c60Sopenharmony_ci}
624f6603c60Sopenharmony_ci
625f6603c60Sopenharmony_cistatic napi_value OHNativeImageDetachContextNormal(napi_env env, napi_callback_info info)
626f6603c60Sopenharmony_ci{
627f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
628f6603c60Sopenharmony_ci    InitEGLEnv();
629f6603c60Sopenharmony_ci    GLuint textureId;
630f6603c60Sopenharmony_ci    glGenTextures(PARAM_1, &textureId);
631f6603c60Sopenharmony_ci    napi_value result = nullptr;
632f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_3, &result);
633f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
634f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
635f6603c60Sopenharmony_ci    napi_value result3 = nullptr;
636f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_DetachContext(nativeImage);
637f6603c60Sopenharmony_ci    if (res1 != 0) {
638f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
639f6603c60Sopenharmony_ci    } else {
640f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
641f6603c60Sopenharmony_ci    }
642f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
643f6603c60Sopenharmony_ci
644f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_AttachContext(nativeImage, textureId);
645f6603c60Sopenharmony_ci    if (res2 == 0) {
646f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
647f6603c60Sopenharmony_ci    } else {
648f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
649f6603c60Sopenharmony_ci    }
650f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
651f6603c60Sopenharmony_ci
652f6603c60Sopenharmony_ci    int res3 = OH_NativeImage_DetachContext(nativeImage);
653f6603c60Sopenharmony_ci    if (res3 == 0) {
654f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result3);
655f6603c60Sopenharmony_ci    } else {
656f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result3);
657f6603c60Sopenharmony_ci    }
658f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_2, result3);
659f6603c60Sopenharmony_ci
660f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
661f6603c60Sopenharmony_ci    return result;
662f6603c60Sopenharmony_ci}
663f6603c60Sopenharmony_ci
664f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetSurfaceIdNullptr(napi_env env, napi_callback_info info)
665f6603c60Sopenharmony_ci{
666f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
667f6603c60Sopenharmony_ci    napi_value result = nullptr;
668f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_2, &result);
669f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
670f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
671f6603c60Sopenharmony_ci    uint64_t surfaceId = NUMBER_99999;
672f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_GetSurfaceId(nullptr, &surfaceId);
673f6603c60Sopenharmony_ci    if (res1 != 0 && surfaceId == NUMBER_99999) {
674f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
675f6603c60Sopenharmony_ci    } else {
676f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
677f6603c60Sopenharmony_ci    }
678f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
679f6603c60Sopenharmony_ci
680f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_GetSurfaceId(nullptr, NULL);
681f6603c60Sopenharmony_ci    if (res2 != 0 && surfaceId == NUMBER_99999) {
682f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
683f6603c60Sopenharmony_ci    } else {
684f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
685f6603c60Sopenharmony_ci    }
686f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
687f6603c60Sopenharmony_ci
688f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
689f6603c60Sopenharmony_ci    return result;
690f6603c60Sopenharmony_ci}
691f6603c60Sopenharmony_ci
692f6603c60Sopenharmony_cistatic napi_value OHNativeGetSurfaceIdNormal(napi_env env, napi_callback_info info)
693f6603c60Sopenharmony_ci{
694f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
695f6603c60Sopenharmony_ci    napi_value result = nullptr;
696f6603c60Sopenharmony_ci    uint64_t surfaceId = NUMBER_99999;
697f6603c60Sopenharmony_ci    int res = OH_NativeImage_GetSurfaceId(nativeImage, &surfaceId);
698f6603c60Sopenharmony_ci    if (res == 0 && surfaceId != NUMBER_99999) {
699f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
700f6603c60Sopenharmony_ci    } else {
701f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
702f6603c60Sopenharmony_ci    }
703f6603c60Sopenharmony_ci
704f6603c60Sopenharmony_ci    return result;
705f6603c60Sopenharmony_ci}
706f6603c60Sopenharmony_ci
707f6603c60Sopenharmony_cistatic napi_value OHNativeImageUpdateSurfaceImageNullptr(napi_env env, napi_callback_info info)
708f6603c60Sopenharmony_ci{
709f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
710f6603c60Sopenharmony_ci    napi_value result = nullptr;
711f6603c60Sopenharmony_ci    int res = OH_NativeImage_UpdateSurfaceImage(nullptr);
712f6603c60Sopenharmony_ci    if (res != 0) {
713f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
714f6603c60Sopenharmony_ci    } else {
715f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
716f6603c60Sopenharmony_ci    }
717f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
718f6603c60Sopenharmony_ci    return result;
719f6603c60Sopenharmony_ci}
720f6603c60Sopenharmony_ci
721f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTimestampNullptr(napi_env env, napi_callback_info info)
722f6603c60Sopenharmony_ci{
723f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
724f6603c60Sopenharmony_ci    napi_value result = nullptr;
725f6603c60Sopenharmony_ci    int64_t timestamp = 0;
726f6603c60Sopenharmony_ci    timestamp = OH_NativeImage_GetTimestamp(nullptr);
727f6603c60Sopenharmony_ci    if (timestamp == 0) {
728f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
729f6603c60Sopenharmony_ci    } else {
730f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
731f6603c60Sopenharmony_ci    }
732f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
733f6603c60Sopenharmony_ci    return result;
734f6603c60Sopenharmony_ci}
735f6603c60Sopenharmony_ci
736f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTransformMatrixNullptr(napi_env env, napi_callback_info info)
737f6603c60Sopenharmony_ci{
738f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
739f6603c60Sopenharmony_ci    napi_value result = nullptr;
740f6603c60Sopenharmony_ci    float matrix[16];
741f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_2, &result);
742f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
743f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
744f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_GetTransformMatrix(nullptr, matrix);
745f6603c60Sopenharmony_ci    if (res1 == 0) {
746f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
747f6603c60Sopenharmony_ci    } else {
748f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
749f6603c60Sopenharmony_ci    }
750f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
751f6603c60Sopenharmony_ci
752f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_GetTransformMatrix(nativeImage, NULL);
753f6603c60Sopenharmony_ci    if (res2 == 0) {
754f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
755f6603c60Sopenharmony_ci    } else {
756f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
757f6603c60Sopenharmony_ci    }
758f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
759f6603c60Sopenharmony_ci
760f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
761f6603c60Sopenharmony_ci    return result;
762f6603c60Sopenharmony_ci}
763f6603c60Sopenharmony_ci
764f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTransformMatrixV2Nullptr(napi_env env, napi_callback_info info)
765f6603c60Sopenharmony_ci{
766f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = getNativeImage();
767f6603c60Sopenharmony_ci    napi_value result = nullptr;
768f6603c60Sopenharmony_ci    float matrix[16];
769f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_2, &result);
770f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
771f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
772f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_GetTransformMatrixV2(nullptr, matrix);
773f6603c60Sopenharmony_ci    if (res1 == 0) {
774f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
775f6603c60Sopenharmony_ci    } else {
776f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
777f6603c60Sopenharmony_ci    }
778f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
779f6603c60Sopenharmony_ci
780f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_GetTransformMatrixV2(nativeImage, NULL);
781f6603c60Sopenharmony_ci    if (res2 == 0) {
782f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
783f6603c60Sopenharmony_ci    } else {
784f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
785f6603c60Sopenharmony_ci    }
786f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
787f6603c60Sopenharmony_ci
788f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
789f6603c60Sopenharmony_ci    return result;
790f6603c60Sopenharmony_ci}
791f6603c60Sopenharmony_ci
792f6603c60Sopenharmony_cistatic napi_value OHNativeImageUpdateSurfaceImageNormal(napi_env env, napi_callback_info info)
793f6603c60Sopenharmony_ci{
794f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = nullptr;
795f6603c60Sopenharmony_ci    napi_value result = nullptr;
796f6603c60Sopenharmony_ci    GLenum nativeImageTexId_;
797f6603c60Sopenharmony_ci    GLuint GL_TEXTURE_EXTERNAL_OES;
798f6603c60Sopenharmony_ci    glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
799f6603c60Sopenharmony_ci    nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
800f6603c60Sopenharmony_ci    int res = OH_NativeImage_UpdateSurfaceImage(nativeImage);
801f6603c60Sopenharmony_ci    if (res == 0) {
802f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
803f6603c60Sopenharmony_ci    } else {
804f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
805f6603c60Sopenharmony_ci    }
806f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
807f6603c60Sopenharmony_ci    return result;
808f6603c60Sopenharmony_ci}
809f6603c60Sopenharmony_ci
810f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTimestampNormal(napi_env env, napi_callback_info info)
811f6603c60Sopenharmony_ci{
812f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = nullptr;
813f6603c60Sopenharmony_ci    napi_value result = nullptr;
814f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_3, &result);
815f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
816f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
817f6603c60Sopenharmony_ci    GLenum nativeImageTexId_;
818f6603c60Sopenharmony_ci    GLuint GL_TEXTURE_EXTERNAL_OES;
819f6603c60Sopenharmony_ci    glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
820f6603c60Sopenharmony_ci    nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
821f6603c60Sopenharmony_ci    int res = OH_NativeImage_UpdateSurfaceImage(nativeImage);
822f6603c60Sopenharmony_ci
823f6603c60Sopenharmony_ci    // 使用chrono获取当前时间点
824f6603c60Sopenharmony_ci    auto now = std::chrono::system_clock::now();
825f6603c60Sopenharmony_ci    // 将当前时间点转换为自1970年1月1日以来的毫秒数
826f6603c60Sopenharmony_ci    auto duration = now.time_since_epoch();
827f6603c60Sopenharmony_ci    auto current_timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
828f6603c60Sopenharmony_ci    // 将chrono库获取的时间戳存储在int64_t类型的变量中
829f6603c60Sopenharmony_ci    int64_t time = static_cast<int64_t>(current_timestamp);
830f6603c60Sopenharmony_ci
831f6603c60Sopenharmony_ci    int64_t timestamp = 0;
832f6603c60Sopenharmony_ci    timestamp = OH_NativeImage_GetTimestamp(nativeImage);
833f6603c60Sopenharmony_ci
834f6603c60Sopenharmony_ci    if (res == 0) {
835f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
836f6603c60Sopenharmony_ci    } else {
837f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
838f6603c60Sopenharmony_ci    }
839f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
840f6603c60Sopenharmony_ci
841f6603c60Sopenharmony_ci    if (timestamp == time) {
842f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
843f6603c60Sopenharmony_ci    } else {
844f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
845f6603c60Sopenharmony_ci    }
846f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
847f6603c60Sopenharmony_ci
848f6603c60Sopenharmony_ci    napi_value result3 = nullptr;
849f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_UpdateSurfaceImage(nativeImage);
850f6603c60Sopenharmony_ci
851f6603c60Sopenharmony_ci    auto now1 = std::chrono::system_clock::now();
852f6603c60Sopenharmony_ci    auto duration1 = now1.time_since_epoch();
853f6603c60Sopenharmony_ci    auto current_timestamp1 = std::chrono::duration_cast<std::chrono::milliseconds>(duration1).count();
854f6603c60Sopenharmony_ci    int64_t time1 = static_cast<int64_t>(current_timestamp);
855f6603c60Sopenharmony_ci
856f6603c60Sopenharmony_ci    int64_t timestamp1 = 0;
857f6603c60Sopenharmony_ci    timestamp1 = OH_NativeImage_GetTimestamp(nativeImage);
858f6603c60Sopenharmony_ci
859f6603c60Sopenharmony_ci    if (res1 == 0 && current_timestamp1 == time1) {
860f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result3);
861f6603c60Sopenharmony_ci    } else {
862f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result3);
863f6603c60Sopenharmony_ci    }
864f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_2, result3);
865f6603c60Sopenharmony_ci
866f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
867f6603c60Sopenharmony_ci    return result;
868f6603c60Sopenharmony_ci}
869f6603c60Sopenharmony_ci
870f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTransformMatrixNormal(napi_env env, napi_callback_info info)
871f6603c60Sopenharmony_ci{
872f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = nullptr;
873f6603c60Sopenharmony_ci    GLenum nativeImageTexId_;
874f6603c60Sopenharmony_ci    GLuint GL_TEXTURE_EXTERNAL_OES;
875f6603c60Sopenharmony_ci    glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
876f6603c60Sopenharmony_ci    nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
877f6603c60Sopenharmony_ci    float matrix[16];
878f6603c60Sopenharmony_ci    float matrixNull[16] = {};
879f6603c60Sopenharmony_ci    napi_value result = nullptr;
880f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_2, &result);
881f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
882f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
883f6603c60Sopenharmony_ci
884f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_UpdateSurfaceImage(nativeImage);
885f6603c60Sopenharmony_ci    if (res1 == 0) {
886f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
887f6603c60Sopenharmony_ci    } else {
888f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
889f6603c60Sopenharmony_ci    }
890f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
891f6603c60Sopenharmony_ci
892f6603c60Sopenharmony_ci    OH_NativeImage_GetTransformMatrix(nativeImage, matrix);
893f6603c60Sopenharmony_ci    if (memcmp(matrix, matrixNull, sizeof(matrix)) == 0) {
894f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
895f6603c60Sopenharmony_ci    } else {
896f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
897f6603c60Sopenharmony_ci    }
898f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
899f6603c60Sopenharmony_ci
900f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
901f6603c60Sopenharmony_ci    return result;
902f6603c60Sopenharmony_ci}
903f6603c60Sopenharmony_ci
904f6603c60Sopenharmony_cistatic napi_value OHNativeImageGetTransformMatrixV2Normal(napi_env env, napi_callback_info info)
905f6603c60Sopenharmony_ci{
906f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = nullptr;
907f6603c60Sopenharmony_ci    GLenum nativeImageTexId_;
908f6603c60Sopenharmony_ci    GLuint GL_TEXTURE_EXTERNAL_OES;
909f6603c60Sopenharmony_ci    glBindTexture(GL_TEXTURE_EXTERNAL_OES, nativeImageTexId_);
910f6603c60Sopenharmony_ci    nativeImage = OH_NativeImage_Create(nativeImageTexId_, GL_TEXTURE_EXTERNAL_OES);
911f6603c60Sopenharmony_ci    float matrixV2[16];
912f6603c60Sopenharmony_ci    float matrixNull[16] = {};
913f6603c60Sopenharmony_ci    napi_value result = nullptr;
914f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_2, &result);
915f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
916f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
917f6603c60Sopenharmony_ci
918f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_UpdateSurfaceImage(nativeImage);
919f6603c60Sopenharmony_ci    if (res1 == 0) {
920f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
921f6603c60Sopenharmony_ci    } else {
922f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
923f6603c60Sopenharmony_ci    }
924f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
925f6603c60Sopenharmony_ci
926f6603c60Sopenharmony_ci    OH_NativeImage_GetTransformMatrixV2(nativeImage, matrixV2);
927f6603c60Sopenharmony_ci    if (memcmp(matrixV2, matrixNull, sizeof(matrixV2)) == 0) {
928f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
929f6603c60Sopenharmony_ci    } else {
930f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
931f6603c60Sopenharmony_ci    }
932f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
933f6603c60Sopenharmony_ci
934f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage);
935f6603c60Sopenharmony_ci    return result;
936f6603c60Sopenharmony_ci}
937f6603c60Sopenharmony_ci
938f6603c60Sopenharmony_cistatic napi_value OHNativeImageSetOnFrameAvailableListenerNullptr(napi_env env, napi_callback_info info)
939f6603c60Sopenharmony_ci{
940f6603c60Sopenharmony_ci    class NativeImageAdaptor {
941f6603c60Sopenharmony_ci    public:
942f6603c60Sopenharmony_ci        ~NativeImageAdaptor();
943f6603c60Sopenharmony_ci        static NativeImageAdaptor *GetInstance();
944f6603c60Sopenharmony_ci        static void OnFrameAvailable(void *context);
945f6603c60Sopenharmony_ci    };
946f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage = nullptr;
947f6603c60Sopenharmony_ci    napi_value result = nullptr;
948f6603c60Sopenharmony_ci    OH_OnFrameAvailableListener listener;
949f6603c60Sopenharmony_ci    int res = OH_NativeImage_SetOnFrameAvailableListener(nullptr, listener);
950f6603c60Sopenharmony_ci    if (res == 0) {
951f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
952f6603c60Sopenharmony_ci    } else {
953f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
954f6603c60Sopenharmony_ci    }
955f6603c60Sopenharmony_ci    return result;
956f6603c60Sopenharmony_ci}
957f6603c60Sopenharmony_ci
958f6603c60Sopenharmony_cistatic napi_value OHNativeImageUnSetOnFrameAvailableListenerNullptr(napi_env env, napi_callback_info info)
959f6603c60Sopenharmony_ci{
960f6603c60Sopenharmony_ci    napi_value result = nullptr;
961f6603c60Sopenharmony_ci    int res = OH_NativeImage_UnsetOnFrameAvailableListener(nullptr);
962f6603c60Sopenharmony_ci    if (res == 0) {
963f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
964f6603c60Sopenharmony_ci    } else {
965f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
966f6603c60Sopenharmony_ci    }
967f6603c60Sopenharmony_ci
968f6603c60Sopenharmony_ci    return result;
969f6603c60Sopenharmony_ci}
970f6603c60Sopenharmony_ci
971f6603c60Sopenharmony_cistatic napi_value OHNativeImageSetOnFrameAvailableListenerNormal(napi_env env, napi_callback_info info)
972f6603c60Sopenharmony_ci{
973f6603c60Sopenharmony_ci    class NativeImageAdaptor {
974f6603c60Sopenharmony_ci    public:
975f6603c60Sopenharmony_ci        ~NativeImageAdaptor();
976f6603c60Sopenharmony_ci        static NativeImageAdaptor *GetInstance();
977f6603c60Sopenharmony_ci        static void OnFrameAvailable(void *context);
978f6603c60Sopenharmony_ci    };
979f6603c60Sopenharmony_ci
980f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage1 = getNativeImage();
981f6603c60Sopenharmony_ci    OH_OnFrameAvailableListener listener1;
982f6603c60Sopenharmony_ci    napi_value result = nullptr;
983f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_4, &result);
984f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
985f6603c60Sopenharmony_ci
986f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener1);
987f6603c60Sopenharmony_ci    if (res1 == 0) {
988f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
989f6603c60Sopenharmony_ci    } else {
990f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
991f6603c60Sopenharmony_ci    }
992f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
993f6603c60Sopenharmony_ci
994f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
995f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener1);
996f6603c60Sopenharmony_ci    if (res2 == 0) {
997f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
998f6603c60Sopenharmony_ci    } else {
999f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
1000f6603c60Sopenharmony_ci    }
1001f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
1002f6603c60Sopenharmony_ci
1003f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage2 = getNativeImage();
1004f6603c60Sopenharmony_ci    OH_OnFrameAvailableListener listener2;
1005f6603c60Sopenharmony_ci    napi_value result3 = nullptr;
1006f6603c60Sopenharmony_ci    int res3 = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener2);
1007f6603c60Sopenharmony_ci    if (res3 == 0) {
1008f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result3);
1009f6603c60Sopenharmony_ci    } else {
1010f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result3);
1011f6603c60Sopenharmony_ci    }
1012f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_2, result3);
1013f6603c60Sopenharmony_ci
1014f6603c60Sopenharmony_ci    napi_value result4 = nullptr;
1015f6603c60Sopenharmony_ci    int res4 = OH_NativeImage_SetOnFrameAvailableListener(nativeImage2, listener1);
1016f6603c60Sopenharmony_ci    if (res4 == 0) {
1017f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result4);
1018f6603c60Sopenharmony_ci    } else {
1019f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result4);
1020f6603c60Sopenharmony_ci    }
1021f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_3, result4);
1022f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage1);
1023f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage2);
1024f6603c60Sopenharmony_ci    return result;
1025f6603c60Sopenharmony_ci}
1026f6603c60Sopenharmony_ci
1027f6603c60Sopenharmony_cistatic napi_value OHNativeImageUnsetOnFrameAvailableListenerNormal(napi_env env, napi_callback_info info)
1028f6603c60Sopenharmony_ci{
1029f6603c60Sopenharmony_ci    class NativeImageAdaptor {
1030f6603c60Sopenharmony_ci    public:
1031f6603c60Sopenharmony_ci        ~NativeImageAdaptor();
1032f6603c60Sopenharmony_ci        static NativeImageAdaptor *GetInstance();
1033f6603c60Sopenharmony_ci        static void OnFrameAvailable(void *context);
1034f6603c60Sopenharmony_ci    };
1035f6603c60Sopenharmony_ci
1036f6603c60Sopenharmony_ci    OH_NativeImage *nativeImage1 = getNativeImage();
1037f6603c60Sopenharmony_ci    OH_OnFrameAvailableListener listener1;
1038f6603c60Sopenharmony_ci    napi_value result = nullptr;
1039f6603c60Sopenharmony_ci    napi_create_array_with_length(env, ARR_NUMBER_3, &result);
1040f6603c60Sopenharmony_ci    napi_value result1 = nullptr;
1041f6603c60Sopenharmony_ci
1042f6603c60Sopenharmony_ci    int res1 = OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage1);
1043f6603c60Sopenharmony_ci    if (res1 == 0) {
1044f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result1);
1045f6603c60Sopenharmony_ci    } else {
1046f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result1);
1047f6603c60Sopenharmony_ci    }
1048f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_0, result1);
1049f6603c60Sopenharmony_ci
1050f6603c60Sopenharmony_ci    napi_value result2 = nullptr;
1051f6603c60Sopenharmony_ci    int res2 = OH_NativeImage_SetOnFrameAvailableListener(nativeImage1, listener1);
1052f6603c60Sopenharmony_ci    if (res2 == 0) {
1053f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result2);
1054f6603c60Sopenharmony_ci    } else {
1055f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result2);
1056f6603c60Sopenharmony_ci    }
1057f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_1, result2);
1058f6603c60Sopenharmony_ci
1059f6603c60Sopenharmony_ci    napi_value result3 = nullptr;
1060f6603c60Sopenharmony_ci    int res3 = OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage1);
1061f6603c60Sopenharmony_ci    if (res3 == 0) {
1062f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result3);
1063f6603c60Sopenharmony_ci    } else {
1064f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result3);
1065f6603c60Sopenharmony_ci    }
1066f6603c60Sopenharmony_ci    napi_set_element(env, result, ARR_NUMBER_2, result3);
1067f6603c60Sopenharmony_ci    OH_NativeImage_Destroy(&nativeImage1);
1068f6603c60Sopenharmony_ci    return result;
1069f6603c60Sopenharmony_ci}
1070f6603c60Sopenharmony_ci
1071f6603c60Sopenharmony_cinapi_value NativeImageInit(napi_env env, napi_value exports)
1072f6603c60Sopenharmony_ci{
1073f6603c60Sopenharmony_ci    napi_property_descriptor desc[] = {
1074f6603c60Sopenharmony_ci        {"oHNativeImageCreate", nullptr, OHNativeImageCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
1075f6603c60Sopenharmony_ci        {"oHNativeImageAcquireNativeWindow", nullptr, OHNativeImageAcquireNativeWindow, nullptr, nullptr, nullptr,
1076f6603c60Sopenharmony_ci         napi_default, nullptr},
1077f6603c60Sopenharmony_ci        {"oHNativeImageAcquireNativeWindowAbnormal", nullptr, OHNativeImageAcquireNativeWindowAbnormal, nullptr,
1078f6603c60Sopenharmony_ci         nullptr, nullptr, napi_default, nullptr},
1079f6603c60Sopenharmony_ci        {"oHNativeImageAttachContext", nullptr, OHNativeImageAttachContext, nullptr, nullptr, nullptr, napi_default,
1080f6603c60Sopenharmony_ci         nullptr},
1081f6603c60Sopenharmony_ci        {"oHNativeImageAttachContextAbnormal", nullptr, OHNativeImageAttachContextAbnormal, nullptr, nullptr, nullptr,
1082f6603c60Sopenharmony_ci         napi_default, nullptr},
1083f6603c60Sopenharmony_ci        {"oHNativeImageDetachContext", nullptr, OHNativeImageDetachContext, nullptr, nullptr, nullptr, napi_default,
1084f6603c60Sopenharmony_ci         nullptr},
1085f6603c60Sopenharmony_ci        {"oHNativeImageDetachContextAbnormal", nullptr, OHNativeImageDetachContextAbnormal, nullptr, nullptr, nullptr,
1086f6603c60Sopenharmony_ci         napi_default, nullptr},
1087f6603c60Sopenharmony_ci        {"oHNativeImageUpdateSurfaceImage", nullptr, OHNativeImageUpdateSurfaceImage, nullptr, nullptr, nullptr,
1088f6603c60Sopenharmony_ci         napi_default, nullptr},
1089f6603c60Sopenharmony_ci        {"oHNativeImageUpdateSurfaceImageBoundary", nullptr, OHNativeImageUpdateSurfaceImageBoundary, nullptr,
1090f6603c60Sopenharmony_ci         nullptr, nullptr, napi_default, nullptr},
1091f6603c60Sopenharmony_ci        {"oHNativeImageUpdateSurfaceImageAbnormal", nullptr, OHNativeImageUpdateSurfaceImageAbnormal, nullptr, nullptr,
1092f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1093f6603c60Sopenharmony_ci        {"oHNativeImageGetTimestamp", nullptr, OHNativeImageGetTimestamp, nullptr, nullptr, nullptr, napi_default,
1094f6603c60Sopenharmony_ci         nullptr},
1095f6603c60Sopenharmony_ci        {"oHNativeImageGetTransformMatrix", nullptr, OHNativeImageGetTransformMatrix, nullptr, nullptr, nullptr,
1096f6603c60Sopenharmony_ci         napi_default, nullptr},
1097f6603c60Sopenharmony_ci        {"oHNativeImageGetTransformMatrixAbnormal", nullptr, OHNativeImageGetTransformMatrixAbnormal, nullptr, nullptr,
1098f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1099f6603c60Sopenharmony_ci        {"oHNativeImageDestroy", nullptr, OHNativeImageDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
1100f6603c60Sopenharmony_ci        {"oHNativeImageCreateNormal", nullptr, OHNativeImageCreateNormal, nullptr, nullptr, nullptr, napi_default,
1101f6603c60Sopenharmony_ci         nullptr},
1102f6603c60Sopenharmony_ci        {"oHNativeImageCreateAbnormal", nullptr, OHNativeImageCreateAbnormal, nullptr, nullptr, nullptr, napi_default,
1103f6603c60Sopenharmony_ci         nullptr},
1104f6603c60Sopenharmony_ci        {"oHNativeImageDestroy1", nullptr, OHNativeImageDestroy1, nullptr, nullptr, nullptr, napi_default, nullptr},
1105f6603c60Sopenharmony_ci        {"oHNativeImageCreateMuch", nullptr, OHNativeImageCreateMuch, nullptr, nullptr, nullptr, napi_default, nullptr},
1106f6603c60Sopenharmony_ci    };
1107f6603c60Sopenharmony_ci    napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc);
1108f6603c60Sopenharmony_ci    return exports;
1109f6603c60Sopenharmony_ci}
1110f6603c60Sopenharmony_ci
1111f6603c60Sopenharmony_ciEXTERN_C_START
1112f6603c60Sopenharmony_cistatic napi_value Init(napi_env env, napi_value exports)
1113f6603c60Sopenharmony_ci{
1114f6603c60Sopenharmony_ci    NativeImageInit(env, exports);
1115f6603c60Sopenharmony_ci    napi_property_descriptor desc[] = {
1116f6603c60Sopenharmony_ci        {"oHNativeImageAcquireNativeWindowNullptr", nullptr, OHNativeImageAcquireNativeWindowNullptr, nullptr, nullptr,
1117f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1118f6603c60Sopenharmony_ci        {"oHNativeImageAcquireNativeWindowNormal", nullptr, OHNativeImageAcquireNativeWindowNormal, nullptr, nullptr,
1119f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1120f6603c60Sopenharmony_ci        {"oHNativeImageAttachContextNullptr", nullptr, OHNativeImageAttachContextNullptr, nullptr, nullptr, nullptr,
1121f6603c60Sopenharmony_ci         napi_default, nullptr},
1122f6603c60Sopenharmony_ci        {"oHNativeImageDetachContextNullptr", nullptr, OHNativeImageDetachContextNullptr, nullptr, nullptr, nullptr,
1123f6603c60Sopenharmony_ci         napi_default, nullptr},
1124f6603c60Sopenharmony_ci        {"oHNativeImageAttachContextNormal", nullptr, OHNativeImageAttachContextNormal, nullptr, nullptr, nullptr,
1125f6603c60Sopenharmony_ci         napi_default, nullptr},
1126f6603c60Sopenharmony_ci        {"oHNativeImageDetachContextNormal", nullptr, OHNativeImageDetachContextNormal, nullptr, nullptr, nullptr,
1127f6603c60Sopenharmony_ci         napi_default, nullptr},
1128f6603c60Sopenharmony_ci        {"oHNativeImageGetSurfaceIdNullptr", nullptr, OHNativeImageGetSurfaceIdNullptr, nullptr, nullptr, nullptr,
1129f6603c60Sopenharmony_ci         napi_default, nullptr},
1130f6603c60Sopenharmony_ci        {"oHNativeGetSurfaceIdNormal", nullptr, OHNativeGetSurfaceIdNormal, nullptr, nullptr, nullptr, napi_default,
1131f6603c60Sopenharmony_ci         nullptr},
1132f6603c60Sopenharmony_ci        {"oHNativeImageUpdateSurfaceImageNullptr", nullptr, OHNativeImageUpdateSurfaceImageNullptr, nullptr, nullptr,
1133f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1134f6603c60Sopenharmony_ci        {"oHNativeImageGetTimestampNullptr", nullptr, OHNativeImageGetTimestampNullptr, nullptr, nullptr, nullptr,
1135f6603c60Sopenharmony_ci         napi_default, nullptr},
1136f6603c60Sopenharmony_ci        {"oHNativeImageGetTransformMatrixNullptr", nullptr, OHNativeImageGetTransformMatrixNullptr, nullptr, nullptr,
1137f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1138f6603c60Sopenharmony_ci        {"oHNativeImageGetTransformMatrixV2Nullptr", nullptr, OHNativeImageGetTransformMatrixV2Nullptr, nullptr,
1139f6603c60Sopenharmony_ci         nullptr, nullptr, napi_default, nullptr},
1140f6603c60Sopenharmony_ci        {"oHNativeImageUpdateSurfaceImageNormal", nullptr, OHNativeImageUpdateSurfaceImageNormal, nullptr, nullptr,
1141f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1142f6603c60Sopenharmony_ci        {"oHNativeImageGetTimestampNormal", nullptr, OHNativeImageGetTimestampNormal, nullptr, nullptr, nullptr,
1143f6603c60Sopenharmony_ci         napi_default, nullptr},
1144f6603c60Sopenharmony_ci        {"oHNativeImageGetTransformMatrixNormal", nullptr, OHNativeImageGetTransformMatrixNormal, nullptr, nullptr,
1145f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1146f6603c60Sopenharmony_ci        {"oHNativeImageGetTransformMatrixV2Normal", nullptr, OHNativeImageGetTransformMatrixV2Normal, nullptr, nullptr,
1147f6603c60Sopenharmony_ci         nullptr, napi_default, nullptr},
1148f6603c60Sopenharmony_ci        {"oHNativeImageSetOnFrameAvailableListenerNullptr", nullptr, OHNativeImageSetOnFrameAvailableListenerNullptr,
1149f6603c60Sopenharmony_ci         nullptr, nullptr, nullptr, napi_default, nullptr},
1150f6603c60Sopenharmony_ci        {"oHNativeImageUnSetOnFrameAvailableListenerNullptr", nullptr,
1151f6603c60Sopenharmony_ci         OHNativeImageUnSetOnFrameAvailableListenerNullptr, nullptr, nullptr, nullptr, napi_default, nullptr},
1152f6603c60Sopenharmony_ci        {"oHNativeImageSetOnFrameAvailableListenerNormal", nullptr, OHNativeImageSetOnFrameAvailableListenerNormal,
1153f6603c60Sopenharmony_ci         nullptr, nullptr, nullptr, napi_default, nullptr},
1154f6603c60Sopenharmony_ci        {"oHNativeImageUnsetOnFrameAvailableListenerNormal", nullptr, OHNativeImageUnsetOnFrameAvailableListenerNormal,
1155f6603c60Sopenharmony_ci         nullptr, nullptr, nullptr, napi_default, nullptr},
1156f6603c60Sopenharmony_ci    };
1157f6603c60Sopenharmony_ci    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1158f6603c60Sopenharmony_ci    return exports;
1159f6603c60Sopenharmony_ci}
1160f6603c60Sopenharmony_ciEXTERN_C_END
1161f6603c60Sopenharmony_ci
1162f6603c60Sopenharmony_cistatic napi_module demoModule = {
1163f6603c60Sopenharmony_ci    .nm_version = 1,
1164f6603c60Sopenharmony_ci    .nm_flags = 0,
1165f6603c60Sopenharmony_ci    .nm_filename = nullptr,
1166f6603c60Sopenharmony_ci    .nm_register_func = Init,
1167f6603c60Sopenharmony_ci    .nm_modname = "nativeimage",
1168f6603c60Sopenharmony_ci    .nm_priv = ((void *)0),
1169f6603c60Sopenharmony_ci    .reserved = {0},
1170f6603c60Sopenharmony_ci};
1171f6603c60Sopenharmony_ci
1172f6603c60Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); };
1173