1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi/native_api.h"
17 #include <EGL/egl.h>
18 #include <EGL/eglplatform.h>
19 #include <GLES2/gl2.h>
20
21 #define INT_INIT_VAL 0
22 #define CREAT_NUM_ONE 1
23 #define RGB_SIZE 8
24 #define INIT_WIDTH 480
25 #define INIT_HEIGHT 800
26 #define INIT_EGL_VERSION 3
27
28 static EGLDisplay eglDisplay = nullptr;
29 static EGLContext eglContext = nullptr;
30 static EGLSurface eglSurface = nullptr;
InitGLES()31 void InitGLES()
32 {
33 eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
34 eglInitialize(eglDisplay, nullptr, nullptr);
35 EGLint numConfigs = INT_INIT_VAL;
36 const EGLint configAttribs[] = {EGL_RED_SIZE, RGB_SIZE, EGL_GREEN_SIZE, RGB_SIZE,
37 EGL_BLUE_SIZE, RGB_SIZE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
38 EGL_NONE};
39 EGLConfig config = nullptr;
40 eglChooseConfig(eglDisplay, configAttribs, &config, CREAT_NUM_ONE, &numConfigs);
41 const EGLint surfaceAttribs[] = {EGL_WIDTH, INIT_WIDTH, EGL_HEIGHT, INIT_HEIGHT, EGL_NONE};
42 eglSurface = eglCreatePbufferSurface(eglDisplay, config, surfaceAttribs);
43 const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, INIT_EGL_VERSION, EGL_NONE};
44 eglContext = eglCreateContext(eglDisplay, config, EGL_NO_CONTEXT, contextAttribs);
45 eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
46 }
DestroyGLES()47 void DestroyGLES()
48 {
49 eglDestroySurface(eglDisplay, eglSurface);
50 eglDestroyContext(eglDisplay, eglContext);
51 eglTerminate(eglDisplay);
52 eglSwapBuffers(eglDisplay, eglSurface);
53 }
54
OpenglExist(napi_env env, napi_callback_info info)55 static napi_value OpenglExist(napi_env env, napi_callback_info info)
56 {
57 InitGLES();
58 const GLubyte *renderer = glGetString(GL_RENDERER);
59 napi_value result = nullptr;
60 if (renderer != nullptr) {
61 napi_create_int32(env, 0, &result);
62 } else {
63 napi_create_int32(env, 1, &result);
64 }
65 DestroyGLES();
66 return result;
67 }
68
OpenglEnabled(napi_env env, napi_callback_info info)69 static napi_value OpenglEnabled(napi_env env, napi_callback_info info)
70 {
71 InitGLES();
72 napi_value result = nullptr;
73 GLboolean isEnabled = glIsEnabled(GL_DITHER);
74 if (isEnabled) {
75 napi_create_int32(env, 0, &result);
76 } else {
77 napi_create_int32(env, 1, &result);
78 }
79 DestroyGLES();
80 return result;
81 }
82
83 EXTERN_C_START
Init(napi_env env, napi_value exports)84 static napi_value Init(napi_env env, napi_value exports)
85 {
86 napi_property_descriptor desc[] = {
87 {"OpenglExist", nullptr, OpenglExist, nullptr, nullptr, nullptr, napi_default, nullptr},
88 {"OpenglEnabled", nullptr, OpenglEnabled, nullptr, nullptr, nullptr, napi_default, nullptr}
89 };
90 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
91 return exports;
92 }
93 EXTERN_C_END
94
95 static napi_module demoModule = {
96 .nm_version = 1,
97 .nm_flags = 0,
98 .nm_filename = nullptr,
99 .nm_register_func = Init,
100 .nm_modname = "openglNdk",
101 .nm_priv = ((void*)0),
102 .reserved = { 0 },
103 };
104
RegisterEntryModule(void)105 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
106 {
107 napi_module_register(&demoModule);
108 }
109