• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "../include/context/webgl_rendering_context_basic_base.h"
17 #include "../include/context/webgl_rendering_context_base.h"
18 #include "../include/context/webgl2_rendering_context_base.h"
19 #include "../include/context/webgl_context_attributes.h"
20 #include "../../common/napi/n_func_arg.h"
21 #include "../../common/napi/n_class.h"
22 #include "../include/webgl/webgl_shader.h"
23 #include "../include/webgl/webgl_buffer.h"
24 #include "../include/webgl/webgl_framebuffer.h"
25 #include "../include/webgl/webgl_program.h"
26 #include "../include/webgl/webgl_renderbuffer.h"
27 #include "../include/webgl/webgl_texture.h"
28 #include "../include/webgl/webgl_uniform_location.h"
29 #include "../include/webgl/webgl_active_info.h"
30 #include "../include/webgl/webgl_shader_precision_format.h"
31 #include "../include/util/log.h"
32 #include "../include/util/egl_manager.h"
33 #include "../include/util/object_source.h"
34 #include "../include/util/util.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 namespace OHOS {
41 namespace Rosen {
42 using namespace std;
43 
GetContextAttributes(napi_env env,napi_callback_info info)44 napi_value WebGLRenderingContextBase::GetContextAttributes(napi_env env, napi_callback_info info)
45 {
46     NFuncArg funcArg(env, info);
47 
48     if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
49         return NVal::CreateNull(env).val_;
50     }
51     WebGLRenderingContextBasicBase *obj = Util::GetContextObject(env, funcArg.GetThisVar(), "webgl");
52     if (obj == nullptr) {
53         return NVal::CreateNull(env).val_;
54     }
55     WebGLContextAttributes *webGlContextAttributes = obj->webGlContextAttributes;
56     if (webGlContextAttributes == nullptr) {
57         webGlContextAttributes = new WebGLContextAttributes();
58     }
59     NVal res = NVal::CreateObject(env);
60     napi_value alpha = NVal::CreateBool(env, webGlContextAttributes->alpha).val_;
61     napi_value antialias = NVal::CreateBool(env, webGlContextAttributes->antialias).val_;
62     napi_value depth = NVal::CreateBool(env, webGlContextAttributes->depth).val_;
63     napi_value failIfMajorPerformanceCaveat =
64         NVal::CreateBool(env, webGlContextAttributes->failIfMajorPerformanceCaveat).val_;
65     napi_value desynchronized = NVal::CreateBool(env, webGlContextAttributes->desynchronized).val_;
66     napi_value premultipliedAlpha = NVal::CreateBool(env, webGlContextAttributes->premultipliedAlpha).val_;
67     napi_value preserveDrawingBuffer = NVal::CreateBool(env, webGlContextAttributes->preserveDrawingBuffer).val_;
68     napi_value stencil = NVal::CreateBool(env, webGlContextAttributes->stencil).val_;
69     napi_value powerPreference = NVal::CreateUTF8String(env, webGlContextAttributes->powerPreference).val_;
70     res.AddProp("alpha", alpha);
71     res.AddProp("antialias", antialias);
72     res.AddProp("depth", depth);
73     res.AddProp("failIfMajorPerformanceCaveat", failIfMajorPerformanceCaveat);
74     res.AddProp("desynchronized", desynchronized);
75     res.AddProp("premultipliedAlpha", premultipliedAlpha);
76     res.AddProp("preserveDrawingBuffer", preserveDrawingBuffer);
77     res.AddProp("stencil", stencil);
78     res.AddProp("powerPreference", powerPreference);
79     delete webGlContextAttributes;
80     return res.val_;
81 }
82 
IsContextLost(napi_env env,napi_callback_info info)83 napi_value WebGLRenderingContextBase::IsContextLost(napi_env env, napi_callback_info info)
84 {
85     bool res = false;
86     if (EglManager::GetInstance().GetEGLContext() == nullptr) {
87         res = true;
88     };
89     return NVal::CreateBool(env, res).val_;
90 }
91 
GetSupportedExtensions(napi_env env,napi_callback_info info)92 napi_value WebGLRenderingContextBase::GetSupportedExtensions(napi_env env, napi_callback_info info)
93 {
94     LOGI("WebGL getSupportedExtensions start");
95     const char* extensions = eglQueryString(EglManager::GetInstance().GetEGLDisplay(), EGL_EXTENSIONS);
96     string str = extensions;
97     vector<string> vec;
98     Util::SplitString(str, vec, " ");
99     napi_value result = nullptr;
100     napi_create_array_with_length(env, vec.size(), &result);
101     for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
102         napi_set_element(env, result, i, NVal::CreateUTF8String(env, vec[i]).val_);
103     }
104     LOGI("WebGL getSupportedExtensions end");
105     return result;
106 }
107 
GetExtension(napi_env env,napi_callback_info info)108 napi_value WebGLRenderingContextBase::GetExtension(napi_env env, napi_callback_info info)
109 {
110     return nullptr;
111 }
112 
ActiveTexture(napi_env env,napi_callback_info info)113 napi_value WebGLRenderingContextBase::ActiveTexture(napi_env env, napi_callback_info info)
114 {
115     NFuncArg funcArg(env, info);
116 
117     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
118         return nullptr;
119     }
120     bool succ = false;
121     LOGI("WebGL activeTexture start");
122     int64_t texture;
123     tie(succ, texture) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
124     if (!succ) {
125         return nullptr;
126     }
127     LOGI("WebGL WebGLRenderingContextBase::activeTexture texture = %{public}u", texture);
128     glActiveTexture(static_cast<GLenum>(texture));
129     LOGI("WebGL activeTexture end");
130     return nullptr;
131 }
132 
AttachShader(napi_env env,napi_callback_info info)133 napi_value WebGLRenderingContextBase::AttachShader(napi_env env, napi_callback_info info)
134 {
135     NFuncArg funcArg(env, info);
136 
137     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
138         return nullptr;
139     }
140     if (funcArg[NARG_POS::FIRST] == nullptr || funcArg[NARG_POS::SECOND] == nullptr) {
141         return nullptr;
142     }
143     LOGI("WebGL attachShader start");
144     WebGLProgram *webGlProgram = nullptr;
145     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
146     if (programStatus != napi_ok) {
147         return nullptr;
148     }
149     int programId = webGlProgram->GetProgramId();
150 
151     WebGLShader *webGlShader = nullptr;
152     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGlShader);
153     if (shaderStatus != napi_ok) {
154         return nullptr;
155     }
156     int shaderId = webGlShader->GetShaderId();
157     glAttachShader(static_cast<GLuint>(programId), static_cast<GLuint>(shaderId));
158     LOGI("WebGL attachShader end");
159     return nullptr;
160 }
161 
BindAttribLocation(napi_env env,napi_callback_info info)162 napi_value WebGLRenderingContextBase::BindAttribLocation(napi_env env, napi_callback_info info)
163 {
164     NFuncArg funcArg(env, info);
165 
166     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
167         return nullptr;
168     }
169     bool succ = false;
170     LOGI("WebGL bindAttribLocation start");
171     if (funcArg[NARG_POS::FIRST] == nullptr) {
172         return nullptr;
173     }
174     WebGLProgram *webGlProgram = nullptr;
175     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
176     if (programStatus != napi_ok) {
177         return nullptr;
178     }
179     int programId = webGlProgram->GetProgramId();
180 
181     int64_t index;
182     tie(succ, index) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
183     if (!succ) {
184         return nullptr;
185     }
186     LOGI("WebGL WebGLRenderingContextBase::bindAttribLocation index = %{public}u", index);
187 
188     unique_ptr<char[]> name;
189     tie(succ, name, ignore) = NVal(env, funcArg[NARG_POS::THIRD]).ToUTF8String();
190     if (!succ) {
191         return nullptr;
192     }
193     glBindAttribLocation(static_cast<GLuint>(programId), static_cast<GLuint>(index),
194         const_cast<char *>(name.get()));
195     LOGI("WebGL bindAttribLocation end");
196     return nullptr;
197 }
198 
BindBuffer(napi_env env,napi_callback_info info)199 napi_value WebGLRenderingContextBase::BindBuffer(napi_env env, napi_callback_info info)
200 {
201     NFuncArg funcArg(env, info);
202 
203     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
204         return nullptr;
205     }
206     bool succ = false;
207     LOGI("WebGL bindBuffer start");
208     int64_t target;
209     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
210     if (!succ) {
211         return nullptr;
212     }
213     LOGI("WebGL WebGLRenderingContextBase::bindBuffer target = %{public}u", target);
214 
215     if (funcArg[NARG_POS::SECOND] == nullptr) {
216         return nullptr;
217     }
218     WebGLBuffer *webGlBuffer = nullptr;
219     napi_status bufferStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGlBuffer);
220     if (bufferStatus != napi_ok) {
221         return nullptr;
222     }
223     unsigned int buffer = webGlBuffer->GetBuffer();
224     glBindBuffer(static_cast<GLenum>(target), static_cast<GLuint>(buffer));
225     LOGI("WebGL bindBuffer end");
226     return nullptr;
227 }
228 
BindFramebuffer(napi_env env,napi_callback_info info)229 napi_value WebGLRenderingContextBase::BindFramebuffer(napi_env env, napi_callback_info info)
230 {
231     NFuncArg funcArg(env, info);
232 
233     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
234         return nullptr;
235     }
236     bool succ = false;
237     LOGI("WebGL bindFramebuffer start");
238     int64_t target;
239     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
240     if (!succ) {
241         return nullptr;
242     }
243     LOGI("WebGL WebGLRenderingContextBase::bindFramebuffer target = %{public}u", target);
244 
245     if (funcArg[NARG_POS::SECOND] == nullptr) {
246         return nullptr;
247     }
248     WebGLFramebuffer *webGlFramebuffer = nullptr;
249     napi_status framebufferStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGlFramebuffer);
250     if (framebufferStatus != napi_ok) {
251         return nullptr;
252     }
253     unsigned int framebuffer = webGlFramebuffer->GetFramebuffer();
254     glBindFramebuffer(static_cast<GLenum>(target), static_cast<GLuint>(framebuffer));
255     LOGI("WebGL bindFramebuffer end");
256     return nullptr;
257 }
258 
BindRenderbuffer(napi_env env,napi_callback_info info)259 napi_value WebGLRenderingContextBase::BindRenderbuffer(napi_env env, napi_callback_info info)
260 {
261     NFuncArg funcArg(env, info);
262 
263     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
264         return nullptr;
265     }
266     bool succ = false;
267     LOGI("WebGL bindRenderbuffer start");
268     int64_t target;
269     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
270     if (!succ) {
271         return nullptr;
272     }
273     LOGI("WebGL WebGLRenderingContextBase::bindFramebuffer target = %{public}u", target);
274 
275     if (funcArg[NARG_POS::SECOND] == nullptr) {
276         return nullptr;
277     }
278     WebGLRenderbuffer *webGlRenderbuffer = nullptr;
279     napi_status renderbufferStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGlRenderbuffer);
280     if (renderbufferStatus != napi_ok) {
281         return nullptr;
282     }
283     unsigned int renderbuffer = webGlRenderbuffer->GetRenderbuffer();
284     glBindRenderbuffer(static_cast<GLenum>(target), static_cast<GLuint>(renderbuffer));
285     LOGI("WebGL bindRenderbuffer end");
286     return nullptr;
287 }
288 
BindTexture(napi_env env,napi_callback_info info)289 napi_value WebGLRenderingContextBase::BindTexture(napi_env env, napi_callback_info info)
290 {
291     NFuncArg funcArg(env, info);
292 
293     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
294         return nullptr;
295     }
296     bool succ = false;
297     LOGI("WebGL bindTexture start");
298     int64_t target;
299     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
300     if (!succ) {
301         return nullptr;
302     }
303     LOGI("WebGL WebGLRenderingContextBase::bindTexture target = %{public}u", target);
304 
305     if (funcArg[NARG_POS::SECOND] == nullptr) {
306         return nullptr;
307     }
308     WebGLTexture *webGlTexture = nullptr;
309     napi_status textureStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGlTexture);
310     if (textureStatus != napi_ok) {
311         return nullptr;
312     }
313     unsigned int texture = webGlTexture->GetTexture();
314     LOGI("WebGL WebGLRenderingContextBase::bindTexture textureId = %{public}u", texture);
315     glBindTexture(static_cast<GLenum>(target), static_cast<GLuint>(texture));
316     LOGI("WebGL bindTexture end");
317     return nullptr;
318 }
319 
BlendColor(napi_env env,napi_callback_info info)320 napi_value WebGLRenderingContextBase::BlendColor(napi_env env, napi_callback_info info)
321 {
322     NFuncArg funcArg(env, info);
323 
324     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
325         return nullptr;
326     }
327     bool succ = false;
328     LOGI("WebGL blendColor start");
329     double red;
330     tie(succ, red) = NVal(env, funcArg[NARG_POS::FIRST]).ToDouble();
331     if (!succ) {
332         return nullptr;
333     }
334     double green;
335     tie(succ, green) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
336     if (!succ) {
337         return nullptr;
338     }
339     double blue;
340     tie(succ, blue) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
341     if (!succ) {
342         return nullptr;
343     }
344     double alpha;
345     tie(succ, alpha) = NVal(env, funcArg[NARG_POS::FOURTH]).ToDouble();
346     if (!succ) {
347         return nullptr;
348     }
349     glBlendColor(static_cast<GLclampf>((float) red), static_cast<GLclampf>((float) green),
350         static_cast<GLclampf>((float) blue), static_cast<GLclampf>((float) alpha));
351     LOGI("WebGL blendColor end");
352     return nullptr;
353 }
354 
BlendEquation(napi_env env,napi_callback_info info)355 napi_value WebGLRenderingContextBase::BlendEquation(napi_env env, napi_callback_info info)
356 {
357     NFuncArg funcArg(env, info);
358 
359     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
360         return nullptr;
361     }
362     bool succ = false;
363     LOGI("WebGL blendEquation start");
364     int64_t mode;
365     tie(succ, mode) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
366     if (!succ) {
367         return nullptr;
368     }
369     LOGI("WebGL WebGLRenderingContextBase::blendEquation mode = %{public}u", mode);
370     glBlendEquation(static_cast<GLenum>(mode));
371     LOGI("WebGL blendEquation end");
372     return nullptr;
373 }
374 
BlendEquationSeparate(napi_env env,napi_callback_info info)375 napi_value WebGLRenderingContextBase::BlendEquationSeparate(napi_env env, napi_callback_info info)
376 {
377     NFuncArg funcArg(env, info);
378 
379     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
380         return nullptr;
381     }
382     bool succ = false;
383     LOGI("WebGL blendEquationSeparate start");
384     int64_t modeRGB;
385     tie(succ, modeRGB) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
386     if (!succ) {
387         return nullptr;
388     }
389     LOGI("WebGL WebGLRenderingContextBase::blendEquationSeparate modeRGB = %{public}u", modeRGB);
390     int64_t modeAlpha;
391     tie(succ, modeAlpha) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
392     if (!succ) {
393         return nullptr;
394     }
395     LOGI("WebGL WebGLRenderingContextBase::blendEquationSeparate modeAlpha = %{public}u", modeAlpha);
396     glBlendEquationSeparate(static_cast<GLenum>(modeRGB), static_cast<GLenum>(modeAlpha));
397     LOGI("WebGL blendEquationSeparate end");
398     return nullptr;
399 }
400 
BlendFunc(napi_env env,napi_callback_info info)401 napi_value WebGLRenderingContextBase::BlendFunc(napi_env env, napi_callback_info info)
402 {
403     NFuncArg funcArg(env, info);
404 
405     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
406         return nullptr;
407     }
408     bool succ = false;
409     LOGI("WebGL blendFunc start");
410     int64_t sFactor;
411     tie(succ, sFactor) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
412     if (!succ) {
413         return nullptr;
414     }
415     LOGI("WebGL WebGLRenderingContextBase::blendFunc sFactor = %{public}u", sFactor);
416     int64_t dFactor;
417     tie(succ, dFactor) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
418     if (!succ) {
419         return nullptr;
420     }
421     LOGI("WebGL WebGLRenderingContextBase::blendFunc dFactor = %{public}u", dFactor);
422     glBlendFunc(static_cast<GLenum>(sFactor), static_cast<GLenum>(dFactor));
423     LOGI("WebGL blendFunc end");
424     return nullptr;
425 }
426 
BlendFuncSeparate(napi_env env,napi_callback_info info)427 napi_value WebGLRenderingContextBase::BlendFuncSeparate(napi_env env, napi_callback_info info)
428 {
429     NFuncArg funcArg(env, info);
430 
431     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
432         return nullptr;
433     }
434     bool succ = false;
435     LOGI("WebGL blendFuncSeparate start");
436     int64_t srcRGB;
437     tie(succ, srcRGB) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
438     if (!succ) {
439         return nullptr;
440     }
441     LOGI("WebGL WebGLRenderingContextBase::blendFuncSeparate srcRGB = %{public}u", srcRGB);
442     int64_t dstRGB;
443     tie(succ, dstRGB) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
444     if (!succ) {
445         return nullptr;
446     }
447     LOGI("WebGL WebGLRenderingContextBase::blendFuncSeparate dstRGB = %{public}u", dstRGB);
448     int64_t srcAlpha;
449     tie(succ, srcAlpha) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
450     if (!succ) {
451         return nullptr;
452     }
453     LOGI("WebGL WebGLRenderingContextBase::blendFuncSeparate srcAlpha = %{public}u", srcAlpha);
454     int64_t dstAlpha;
455     tie(succ, dstAlpha) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt64();
456     if (!succ) {
457         return nullptr;
458     }
459     LOGI("WebGL WebGLRenderingContextBase::blendFuncSeparate dstAlpha = %{public}u", dstAlpha);
460     glBlendFuncSeparate(static_cast<GLenum>(srcRGB), static_cast<GLenum>(dstRGB), static_cast<GLenum>(srcAlpha),
461         static_cast<GLenum>(dstAlpha));
462     LOGI("WebGL blendFuncSeparate end");
463     return nullptr;
464 }
465 
CheckFramebufferStatus(napi_env env,napi_callback_info info)466 napi_value WebGLRenderingContextBase::CheckFramebufferStatus(napi_env env, napi_callback_info info)
467 {
468     NFuncArg funcArg(env, info);
469 
470     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
471         return nullptr;
472     }
473     bool succ = false;
474     LOGI("WebGL checkFramebufferStatus start");
475     int64_t target;
476     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
477     if (!succ) {
478         return nullptr;
479     }
480     LOGI("WebGL WebGLRenderingContextBase::checkFramebufferStatus target = %{public}u", target);
481     int64_t res = static_cast<int64_t>(glCheckFramebufferStatus(static_cast<GLenum>(target)));
482     LOGI("WebGL checkFramebufferStatus end");
483     return NVal::CreateInt64(env, res).val_;
484 }
485 
Clear(napi_env env,napi_callback_info info)486 napi_value WebGLRenderingContextBase::Clear(napi_env env, napi_callback_info info)
487 {
488     NFuncArg funcArg(env, info);
489 
490     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
491         return nullptr;
492     }
493     bool succ = false;
494     LOGI("WebGL clear start");
495     int64_t mask;
496     tie(succ, mask) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
497     if (!succ) {
498         return nullptr;
499     }
500     LOGI("WebGL WebGLRenderingContextBase::clear mask = %{public}u", mask);
501     glClear(static_cast<GLbitfield>(mask));
502     LOGI("WebGL clear end");
503     return nullptr;
504 }
505 
ClearColor(napi_env env,napi_callback_info info)506 napi_value WebGLRenderingContextBase::ClearColor(napi_env env, napi_callback_info info)
507 {
508     NFuncArg funcArg(env, info);
509 
510     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
511         return nullptr;
512     }
513     bool succ = false;
514     LOGI("WebGL clearColor start");
515     double red;
516     tie(succ, red) = NVal(env, funcArg[NARG_POS::FIRST]).ToDouble();
517     if (!succ) {
518         return nullptr;
519     }
520     double green;
521     tie(succ, green) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
522     if (!succ) {
523         return nullptr;
524     }
525     double blue;
526     tie(succ, blue) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
527     if (!succ) {
528         return nullptr;
529     }
530     double alpha;
531     tie(succ, alpha) = NVal(env, funcArg[NARG_POS::FOURTH]).ToDouble();
532     if (!succ) {
533         return nullptr;
534     }
535     glClearColor(static_cast<GLclampf>((float) red), static_cast<GLclampf>((float) green),
536         static_cast<GLclampf>((float) blue), static_cast<GLclampf>((float) alpha));
537     LOGI("WebGL clearColor end");
538     return nullptr;
539 }
540 
ClearDepth(napi_env env,napi_callback_info info)541 napi_value WebGLRenderingContextBase::ClearDepth(napi_env env, napi_callback_info info)
542 {
543     NFuncArg funcArg(env, info);
544 
545     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
546         return nullptr;
547     }
548     bool succ = false;
549     LOGI("WebGL clearDepth start");
550     double depth;
551     tie(succ, depth) = NVal(env, funcArg[NARG_POS::FIRST]).ToDouble();
552     if (!succ) {
553         return nullptr;
554     }
555     glClearDepthf(static_cast<GLclampf>((float) depth));
556     LOGI("WebGL clearDepth end");
557     return nullptr;
558 }
559 
ClearStencil(napi_env env,napi_callback_info info)560 napi_value WebGLRenderingContextBase::ClearStencil(napi_env env, napi_callback_info info)
561 {
562     NFuncArg funcArg(env, info);
563 
564     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
565         return nullptr;
566     }
567     bool succ = false;
568     LOGI("WebGL clearStencil start");
569     int64_t s;
570     tie(succ, s) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
571     if (!succ) {
572         return nullptr;
573     }
574     LOGI("WebGL WebGLRenderingContextBase::clearStencil s = %{public}u", s);
575     glClearStencil(static_cast<GLint>(s));
576     LOGI("WebGL clearStencil end");
577     return nullptr;
578 }
579 
ColorMask(napi_env env,napi_callback_info info)580 napi_value WebGLRenderingContextBase::ColorMask(napi_env env, napi_callback_info info)
581 {
582     NFuncArg funcArg(env, info);
583 
584     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
585         return nullptr;
586     }
587     bool succ = false;
588     LOGI("WebGL colorMask start");
589     bool red = false;
590     tie(succ, red) = NVal(env, funcArg[NARG_POS::FIRST]).ToBool();
591     if (!succ) {
592         return nullptr;
593     }
594     LOGI("WebGL WebGLRenderingContextBase::colorMask red = %{public}u", red);
595     bool green = false;
596     tie(succ, green) = NVal(env, funcArg[NARG_POS::SECOND]).ToBool();
597     if (!succ) {
598         return nullptr;
599     }
600     LOGI("WebGL WebGLRenderingContextBase::colorMask green = %{public}u", green);
601     bool blue = false;
602     tie(succ, blue) = NVal(env, funcArg[NARG_POS::THIRD]).ToBool();
603     if (!succ) {
604         return nullptr;
605     }
606     LOGI("WebGL WebGLRenderingContextBase::colorMask blue = %{public}u", blue);
607     bool alpha = false;
608     tie(succ, alpha) = NVal(env, funcArg[NARG_POS::FOURTH]).ToBool();
609     if (!succ) {
610         return nullptr;
611     }
612     LOGI("WebGL WebGLRenderingContextBase::colorMask alpha = %{public}u", alpha);
613     glColorMask(static_cast<GLboolean>(red), static_cast<GLboolean>(green), static_cast<GLboolean>(blue),
614         static_cast<GLboolean>(alpha));
615     LOGI("WebGL colorMask end");
616     return nullptr;
617 }
618 
CompileShader(napi_env env,napi_callback_info info)619 napi_value WebGLRenderingContextBase::CompileShader(napi_env env, napi_callback_info info)
620 {
621     NFuncArg funcArg(env, info);
622 
623     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
624         return nullptr;
625     }
626     LOGI("WebGL compileShader start");
627     if (funcArg[NARG_POS::FIRST] == nullptr) {
628         return nullptr;
629     }
630 
631     WebGLShader *webGlShader = nullptr;
632     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlShader);
633     if (shaderStatus != napi_ok) {
634         return nullptr;
635     }
636     int shaderId = webGlShader->GetShaderId();
637     glCompileShader(static_cast<GLuint>(shaderId));
638     LOGI("WebGL compileShader end");
639     return nullptr;
640 }
641 
CopyTexImage2D(napi_env env,napi_callback_info info)642 napi_value WebGLRenderingContextBase::CopyTexImage2D(napi_env env, napi_callback_info info)
643 {
644     NFuncArg funcArg(env, info);
645 
646     if (!funcArg.InitArgs(NARG_CNT::EIGHT)) {
647         return nullptr;
648     }
649     bool succ = false;
650     LOGI("WebGL copyTexImage2D start");
651     int64_t target;
652     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
653     if (!succ) {
654         return nullptr;
655     }
656     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D target = %{public}u", target);
657     int64_t level;
658     tie(succ, level) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
659     if (!succ) {
660         return nullptr;
661     }
662     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D level = %{public}u", level);
663     int64_t internalformat;
664     tie(succ, internalformat) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
665     if (!succ) {
666         return nullptr;
667     }
668     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D internalformat = %{public}u", internalformat);
669     int64_t x;
670     tie(succ, x) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt64();
671     if (!succ) {
672         return nullptr;
673     }
674     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D x = %{public}u", x);
675     int64_t y;
676     tie(succ, y) = NVal(env, funcArg[NARG_POS::FIFTH]).ToInt64();
677     if (!succ) {
678         return nullptr;
679     }
680     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D y = %{public}u", y);
681     int64_t width;
682     tie(succ, width) = NVal(env, funcArg[NARG_POS::SIXTH]).ToInt64();
683     if (!succ) {
684         return nullptr;
685     }
686     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D width = %{public}u", width);
687     int64_t height;
688     tie(succ, height) = NVal(env, funcArg[NARG_POS::SEVENTH]).ToInt64();
689     if (!succ) {
690         return nullptr;
691     }
692     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D height = %{public}u", height);
693     int64_t border;
694     tie(succ, border) = NVal(env, funcArg[NARG_POS::EIGHTH]).ToInt64();
695     if (!succ) {
696         return nullptr;
697     }
698     LOGI("WebGL WebGLRenderingContextBase::copyTexImage2D border = %{public}u", border);
699     glCopyTexImage2D(static_cast<GLenum>(target), static_cast<GLint>(level),
700         static_cast<GLenum>(internalformat), static_cast<GLint>(x), static_cast<GLint>(y),
701         static_cast<GLsizei>(width), static_cast<GLsizei>(height), static_cast<GLint>(border));
702     LOGI("WebGL copyTexImage2D end");
703     return nullptr;
704 }
705 
CopyTexSubImage2D(napi_env env,napi_callback_info info)706 napi_value WebGLRenderingContextBase::CopyTexSubImage2D(napi_env env, napi_callback_info info)
707 {
708     NFuncArg funcArg(env, info);
709 
710     if (!funcArg.InitArgs(NARG_CNT::EIGHT)) {
711         return nullptr;
712     }
713     bool succ = false;
714     LOGI("WebGL copyTexSubImage2D start");
715     int64_t target;
716     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
717     if (!succ) {
718         return nullptr;
719     }
720     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D target = %{public}u", target);
721     int64_t level;
722     tie(succ, level) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
723     if (!succ) {
724         return nullptr;
725     }
726     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D level = %{public}u", level);
727     int64_t xoffset;
728     tie(succ, xoffset) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
729     if (!succ) {
730         return nullptr;
731     }
732     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D xoffset = %{public}u", xoffset);
733     int64_t yoffset;
734     tie(succ, yoffset) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt64();
735     if (!succ) {
736         return nullptr;
737     }
738     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D yoffset = %{public}u", yoffset);
739     int64_t x;
740     tie(succ, x) = NVal(env, funcArg[NARG_POS::FIFTH]).ToInt64();
741     if (!succ) {
742         return nullptr;
743     }
744     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D x = %{public}u", x);
745     int64_t y;
746     tie(succ, y) = NVal(env, funcArg[NARG_POS::SIXTH]).ToInt64();
747     if (!succ) {
748         return nullptr;
749     }
750     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D y = %{public}u", y);
751     int64_t width;
752     tie(succ, width) = NVal(env, funcArg[NARG_POS::SEVENTH]).ToInt64();
753     if (!succ) {
754         return nullptr;
755     }
756     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D width = %{public}u", width);
757     int64_t height;
758     tie(succ, height) = NVal(env, funcArg[NARG_POS::EIGHTH]).ToInt64();
759     if (!succ) {
760         return nullptr;
761     }
762     LOGI("WebGL WebGLRenderingContextBase::copyTexSubImage2D height = %{public}u", height);
763     glCopyTexSubImage2D(static_cast<GLenum>(target), static_cast<GLint>(level), static_cast<GLint>(xoffset),
764         static_cast<GLint>(yoffset), static_cast<GLint>(x), static_cast<GLint>(y), static_cast<GLsizei>(width),
765         static_cast<GLsizei>(height));
766     LOGI("WebGL copyTexSubImage2D end");
767     return nullptr;
768 }
769 
CreateBuffer(napi_env env,napi_callback_info info)770 napi_value WebGLRenderingContextBase::CreateBuffer(napi_env env, napi_callback_info info)
771 {
772     LOGI("WebGL createBuffer start");
773     napi_value objBuffer = NClass::InstantiateClass(env, WebGLBuffer::className, {});
774     if (!objBuffer) {
775         return nullptr;
776     }
777     auto webGlBuffer = NClass::GetEntityOf<WebGLBuffer>(env, objBuffer);
778     if (!webGlBuffer) {
779         return nullptr;
780     }
781     unsigned int bufferId;
782     glGenBuffers(1, &bufferId);
783     webGlBuffer->SetBuffer(bufferId);
784     LOGI("WebGL WebGLRenderingContextBase::createBuffer bufferId = %{public}u", bufferId);
785     LOGI("WebGL createBuffer end");
786     return objBuffer;
787 }
788 
CreateFramebuffer(napi_env env,napi_callback_info info)789 napi_value WebGLRenderingContextBase::CreateFramebuffer(napi_env env, napi_callback_info info)
790 {
791     LOGI("WebGL createFramebuffer start");
792     napi_value objFramebuffer = NClass::InstantiateClass(env, WebGLFramebuffer::className, {});
793     if (!objFramebuffer) {
794         return nullptr;
795     }
796     auto webGlFramebuffer = NClass::GetEntityOf<WebGLFramebuffer>(env, objFramebuffer);
797     if (!webGlFramebuffer) {
798         return nullptr;
799     }
800     unsigned int framebufferId;
801     glGenFramebuffers(1, &framebufferId);
802     webGlFramebuffer->SetFramebuffer(framebufferId);
803     LOGI("WebGL WebGLRenderingContextBase::createFramebuffer framebufferId = %{public}u", framebufferId);
804     LOGI("WebGL createFramebuffer end");
805     return objFramebuffer;
806 }
807 
CreateProgram(napi_env env,napi_callback_info info)808 napi_value WebGLRenderingContextBase::CreateProgram(napi_env env, napi_callback_info info)
809 {
810     LOGI("WebGL createProgram start");
811     napi_value objProgram = NClass::InstantiateClass(env, WebGLProgram::className, {});
812     if (!objProgram) {
813         return nullptr;
814     }
815     auto webGlProgram = NClass::GetEntityOf<WebGLProgram>(env, objProgram);
816     if (!webGlProgram) {
817         return nullptr;
818     }
819     int programId = static_cast<int>(glCreateProgram());
820     LOGI("WebGL WebGLRenderingContextBase::createProgram programId = %{public}u", programId);
821     webGlProgram->SetProgramId(programId);
822     LOGI("WebGL createProgram end");
823     return objProgram;
824 }
825 
CreateRenderbuffer(napi_env env,napi_callback_info info)826 napi_value WebGLRenderingContextBase::CreateRenderbuffer(napi_env env, napi_callback_info info)
827 {
828     LOGI("WebGL createRenderbuffer start");
829     napi_value objRenderbuffer = NClass::InstantiateClass(env, WebGLRenderbuffer::className, {});
830     if (!objRenderbuffer) {
831         return nullptr;
832     }
833     auto webGlRenderbuffer = NClass::GetEntityOf<WebGLRenderbuffer>(env, objRenderbuffer);
834     if (!webGlRenderbuffer) {
835         return nullptr;
836     }
837     unsigned int renderbufferId;
838     glGenRenderbuffers(1, &renderbufferId);
839     webGlRenderbuffer->SetRenderbuffer(renderbufferId);
840     LOGI("WebGL WebGLRenderingContextBase::createRenderbuffer renderbufferId = %{public}u", renderbufferId);
841     LOGI("WebGL createRenderbuffer end");
842     return objRenderbuffer;
843 }
844 
CreateShader(napi_env env,napi_callback_info info)845 napi_value WebGLRenderingContextBase::CreateShader(napi_env env, napi_callback_info info)
846 {
847     NFuncArg funcArg(env, info);
848 
849     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
850         return nullptr;
851     }
852     bool succ = false;
853     LOGI("WebGL createShader start");
854     int64_t type;
855     tie(succ, type) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
856     if (!succ) {
857         return NVal::CreateNull(env).val_;
858     }
859     LOGI("WebGL WebGLRenderingContextBase::createShader type = %{public}u", type);
860 
861     napi_value objShader = NClass::InstantiateClass(env, WebGLShader::className, {});
862     if (!objShader) {
863         return nullptr;
864     }
865     auto webGlShader = NClass::GetEntityOf<WebGLShader>(env, objShader);
866     if (!webGlShader) {
867         return nullptr;
868     }
869     int shaderId = static_cast<int>(glCreateShader(static_cast<GLenum>(type)));
870     webGlShader->SetShaderId(shaderId);
871     LOGI("WebGL WebGLRenderingContextBase::createShader shaderId = %{public}u", shaderId);
872     LOGI("WebGL createShader end");
873     return objShader;
874 }
875 
CreateTexture(napi_env env,napi_callback_info info)876 napi_value WebGLRenderingContextBase::CreateTexture(napi_env env, napi_callback_info info)
877 {
878     LOGI("WebGL createTexture start");
879     napi_value objTexture = NClass::InstantiateClass(env, WebGLTexture::className, {});
880     if (!objTexture) {
881         return nullptr;
882     }
883     auto webGlTexture = NClass::GetEntityOf<WebGLTexture>(env, objTexture);
884     if (!webGlTexture) {
885         return nullptr;
886     }
887     unsigned int textureId;
888     glGenTextures(1, &textureId);
889     webGlTexture->SetTexture(textureId);
890     LOGI("WebGL WebGLRenderingContextBase::createTexture textureId = %{public}u", textureId);
891     LOGI("WebGL createTexture end");
892     return objTexture;
893 }
894 
CullFace(napi_env env,napi_callback_info info)895 napi_value WebGLRenderingContextBase::CullFace(napi_env env, napi_callback_info info)
896 {
897     NFuncArg funcArg(env, info);
898 
899     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
900         return nullptr;
901     }
902     bool succ = false;
903     LOGI("WebGL cullFace start");
904     int64_t mode;
905     tie(succ, mode) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
906     if (!succ) {
907         return nullptr;
908     }
909     LOGI("WebGL WebGLRenderingContextBase::cullFace mode = %{public}u", mode);
910     glCullFace(static_cast<GLenum>(mode));
911     LOGI("WebGL cullFace end");
912     return nullptr;
913 }
914 
DeleteBuffer(napi_env env,napi_callback_info info)915 napi_value WebGLRenderingContextBase::DeleteBuffer(napi_env env, napi_callback_info info)
916 {
917     NFuncArg funcArg(env, info);
918 
919     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
920         return nullptr;
921     }
922     LOGI("WebGL deleteBuffer start");
923     if (funcArg[NARG_POS::FIRST] == nullptr) {
924         return nullptr;
925     }
926 
927     WebGLBuffer *webGlBuffer = nullptr;
928     napi_status bufferStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlBuffer);
929     if (bufferStatus != napi_ok) {
930         return nullptr;
931     }
932     unsigned int buffer = webGlBuffer->GetBuffer();
933 
934     glDeleteBuffers(1, &buffer);
935     LOGI("WebGL deleteBuffer end");
936     return nullptr;
937 }
938 
DeleteFramebuffer(napi_env env,napi_callback_info info)939 napi_value WebGLRenderingContextBase::DeleteFramebuffer(napi_env env, napi_callback_info info)
940 {
941     NFuncArg funcArg(env, info);
942 
943     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
944         return nullptr;
945     }
946     LOGI("WebGL deleteFramebuffer start");
947     if (funcArg[NARG_POS::FIRST] == nullptr) {
948         return nullptr;
949     }
950 
951     WebGLFramebuffer *webGlFramebuffer = nullptr;
952     napi_status framebufferStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlFramebuffer);
953     if (framebufferStatus != napi_ok) {
954         return nullptr;
955     }
956     unsigned int framebuffer = webGlFramebuffer->GetFramebuffer();
957     glDeleteFramebuffers(1, &framebuffer);
958     LOGI("WebGL deleteFramebuffer end");
959     return nullptr;
960 }
961 
DeleteProgram(napi_env env,napi_callback_info info)962 napi_value WebGLRenderingContextBase::DeleteProgram(napi_env env, napi_callback_info info)
963 {
964     NFuncArg funcArg(env, info);
965 
966     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
967         return nullptr;
968     }
969     LOGI("WebGL deleteProgram start");
970     if (funcArg[NARG_POS::FIRST] == nullptr) {
971         return nullptr;
972     }
973 
974     WebGLProgram *webGlProgram = nullptr;
975     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
976     if (programStatus != napi_ok) {
977         return nullptr;
978     }
979     int programId = webGlProgram->GetProgramId();
980 
981     glDeleteProgram(static_cast<GLuint>(programId));
982     LOGI("WebGL deleteProgram end");
983     return nullptr;
984 }
985 
DeleteRenderbuffer(napi_env env,napi_callback_info info)986 napi_value WebGLRenderingContextBase::DeleteRenderbuffer(napi_env env, napi_callback_info info)
987 {
988     NFuncArg funcArg(env, info);
989 
990     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
991         return nullptr;
992     }
993     LOGI("WebGL deleteRenderbuffer start");
994     if (funcArg[NARG_POS::FIRST] == nullptr) {
995         return nullptr;
996     }
997 
998     WebGLRenderbuffer *webGlRenderbuffer = nullptr;
999     napi_status renderbufferStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlRenderbuffer);
1000     if (renderbufferStatus != napi_ok) {
1001         return nullptr;
1002     }
1003     unsigned int renderbuffer = webGlRenderbuffer->GetRenderbuffer();
1004 
1005     glDeleteRenderbuffers(1, &renderbuffer);
1006     LOGI("WebGL deleteRenderbuffer end");
1007     return nullptr;
1008 }
1009 
DeleteShader(napi_env env,napi_callback_info info)1010 napi_value WebGLRenderingContextBase::DeleteShader(napi_env env, napi_callback_info info)
1011 {
1012     NFuncArg funcArg(env, info);
1013 
1014     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1015         return nullptr;
1016     }
1017     LOGI("WebGL deleteShader start");
1018     if (funcArg[NARG_POS::FIRST] == nullptr) {
1019         return nullptr;
1020     }
1021 
1022     WebGLShader *webGlShader = nullptr;
1023     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlShader);
1024     if (shaderStatus != napi_ok) {
1025         return nullptr;
1026     }
1027     int shaderId = webGlShader->GetShaderId();
1028 
1029     glDeleteShader(static_cast<GLuint>(shaderId));
1030     LOGI("WebGL deleteShader end");
1031     return nullptr;
1032 }
1033 
DeleteTexture(napi_env env,napi_callback_info info)1034 napi_value WebGLRenderingContextBase::DeleteTexture(napi_env env, napi_callback_info info)
1035 {
1036     NFuncArg funcArg(env, info);
1037 
1038     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1039         return nullptr;
1040     }
1041     LOGI("WebGL deleteTexture start");
1042     if (funcArg[NARG_POS::FIRST] == nullptr) {
1043         return nullptr;
1044     }
1045 
1046     WebGLTexture *webGlTexture = nullptr;
1047     napi_status textureStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlTexture);
1048     if (textureStatus != napi_ok) {
1049         return nullptr;
1050     }
1051     unsigned int texture = webGlTexture->GetTexture();
1052 
1053     glDeleteTextures(1, &texture);
1054     LOGI("WebGL deleteTexture end");
1055     return nullptr;
1056 }
1057 
DepthFunc(napi_env env,napi_callback_info info)1058 napi_value WebGLRenderingContextBase::DepthFunc(napi_env env, napi_callback_info info)
1059 {
1060     NFuncArg funcArg(env, info);
1061 
1062     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1063         return nullptr;
1064     }
1065     bool succ = false;
1066     LOGI("WebGL depthFunc start");
1067     int64_t func;
1068     tie(succ, func) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1069     if (!succ) {
1070         return nullptr;
1071     }
1072     LOGI("WebGL WebGLRenderingContextBase::depthFunc func = %{public}u", func);
1073     glDepthFunc(static_cast<GLenum>(func));
1074     LOGI("WebGL depthFunc end");
1075     return nullptr;
1076 }
1077 
DepthMask(napi_env env,napi_callback_info info)1078 napi_value WebGLRenderingContextBase::DepthMask(napi_env env, napi_callback_info info)
1079 {
1080     NFuncArg funcArg(env, info);
1081 
1082     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1083         return nullptr;
1084     }
1085     bool succ = false;
1086     LOGI("WebGL depthMask start");
1087     bool flag = false;
1088     tie(succ, flag) = NVal(env, funcArg[NARG_POS::FIRST]).ToBool();
1089     if (!succ) {
1090         return nullptr;
1091     }
1092     LOGI("WebGL WebGLRenderingContextBase::depthMask flag = %{public}u", flag);
1093     glDepthMask(static_cast<GLboolean>(flag));
1094     LOGI("WebGL depthMask end");
1095     return nullptr;
1096 }
1097 
DepthRange(napi_env env,napi_callback_info info)1098 napi_value WebGLRenderingContextBase::DepthRange(napi_env env, napi_callback_info info)
1099 {
1100     NFuncArg funcArg(env, info);
1101 
1102     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1103         return nullptr;
1104     }
1105     bool succ = false;
1106     LOGI("WebGL depthRange start");
1107     double zNear;
1108     tie(succ, zNear) = NVal(env, funcArg[NARG_POS::FIRST]).ToDouble();
1109     if (!succ) {
1110         return nullptr;
1111     }
1112     double zFar;
1113     tie(succ, zFar) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
1114     if (!succ) {
1115         return nullptr;
1116     }
1117     glDepthRangef(static_cast<GLclampf>((float) zNear), static_cast<GLclampf>((float) zFar));
1118     LOGI("WebGL depthRange end");
1119     return nullptr;
1120 }
1121 
DetachShader(napi_env env,napi_callback_info info)1122 napi_value WebGLRenderingContextBase::DetachShader(napi_env env, napi_callback_info info)
1123 {
1124     NFuncArg funcArg(env, info);
1125 
1126     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1127         return nullptr;
1128     }
1129     if (funcArg[NARG_POS::FIRST] == nullptr || funcArg[NARG_POS::SECOND] == nullptr) {
1130         return nullptr;
1131     }
1132     LOGI("WebGL detachShader start");
1133     WebGLProgram *webGlProgram = nullptr;
1134     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
1135     if (programStatus != napi_ok) {
1136         return nullptr;
1137     }
1138     int programId = webGlProgram->GetProgramId();
1139 
1140     WebGLShader *webGlShader = nullptr;
1141     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGlShader);
1142     if (shaderStatus != napi_ok) {
1143         return nullptr;
1144     }
1145     int shaderId = webGlShader->GetShaderId();
1146 
1147     glDetachShader(static_cast<GLuint>(programId), static_cast<GLuint>(shaderId));
1148     LOGI("WebGL detachShader end");
1149     return nullptr;
1150 }
1151 
Disable(napi_env env,napi_callback_info info)1152 napi_value WebGLRenderingContextBase::Disable(napi_env env, napi_callback_info info)
1153 {
1154     NFuncArg funcArg(env, info);
1155 
1156     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1157         return nullptr;
1158     }
1159     bool succ = false;
1160     LOGI("WebGL disable start");
1161     int64_t cap;
1162     tie(succ, cap) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1163     if (!succ) {
1164         return nullptr;
1165     }
1166     LOGI("WebGL WebGLRenderingContextBase::disable cap = %{public}u", cap);
1167     glDisable(static_cast<GLenum>(cap));
1168     LOGI("WebGL disable end");
1169     return nullptr;
1170 }
1171 
DisableVertexAttribArray(napi_env env,napi_callback_info info)1172 napi_value WebGLRenderingContextBase::DisableVertexAttribArray(napi_env env, napi_callback_info info)
1173 {
1174     NFuncArg funcArg(env, info);
1175 
1176     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1177         return nullptr;
1178     }
1179     bool succ = false;
1180     LOGI("WebGL disableVertexAttribArray start");
1181     int64_t index;
1182     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1183     if (!succ) {
1184         return nullptr;
1185     }
1186     LOGI("WebGL WebGLRenderingContextBase::disableVertexAttribArray index = %{public}u", index);
1187     glDisableVertexAttribArray(static_cast<GLuint>(index));
1188     LOGI("WebGL disableVertexAttribArray end");
1189     return nullptr;
1190 }
1191 
DrawArrays(napi_env env,napi_callback_info info)1192 napi_value WebGLRenderingContextBase::DrawArrays(napi_env env, napi_callback_info info)
1193 {
1194     NFuncArg funcArg(env, info);
1195 
1196     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
1197         return nullptr;
1198     }
1199     bool succ = false;
1200     LOGI("WebGL drawArrays start");
1201     int64_t mode;
1202     tie(succ, mode) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1203     if (!succ) {
1204         return nullptr;
1205     }
1206     LOGI("WebGL WebGLRenderingContextBase::drawArrays mode = %{public}u", mode);
1207     int64_t first;
1208     tie(succ, first) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
1209     if (!succ) {
1210         return nullptr;
1211     }
1212     LOGI("WebGL WebGLRenderingContextBase::drawArrays first = %{public}u", first);
1213     int64_t count;
1214     tie(succ, count) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
1215     if (!succ) {
1216         return nullptr;
1217     }
1218     LOGI("WebGL WebGLRenderingContextBase::drawArrays count = %{public}u", count);
1219     glDrawArrays(static_cast<GLenum>(mode), static_cast<GLint>(first), static_cast<GLsizei>(count));
1220     LOGI("WebGL drawArrays end");
1221     return nullptr;
1222 }
1223 
DrawElements(napi_env env,napi_callback_info info)1224 napi_value WebGLRenderingContextBase::DrawElements(napi_env env, napi_callback_info info)
1225 {
1226     NFuncArg funcArg(env, info);
1227 
1228     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
1229         return nullptr;
1230     }
1231     bool succ = false;
1232     LOGI("WebGL drawElements start");
1233     int64_t mode;
1234     tie(succ, mode) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1235     if (!succ) {
1236         return nullptr;
1237     }
1238     LOGI("WebGL WebGLRenderingContextBase::drawElements mode = %{public}u", mode);
1239     int64_t count;
1240     tie(succ, count) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
1241     if (!succ) {
1242         return nullptr;
1243     }
1244     LOGI("WebGL WebGLRenderingContextBase::drawElements count = %{public}u", count);
1245     int64_t type;
1246     tie(succ, type) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
1247     if (!succ) {
1248         return nullptr;
1249     }
1250     LOGI("WebGL WebGLRenderingContextBase::drawElements type = %{public}u", type);
1251     int64_t offset;
1252     tie(succ, offset) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt64();
1253     if (!succ) {
1254         return nullptr;
1255     }
1256     LOGI("WebGL WebGLRenderingContextBase::drawElements offset = %{public}u", offset);
1257     glDrawElements(static_cast<GLenum>(mode), static_cast<GLsizei>(count), static_cast<GLenum>(type),
1258         reinterpret_cast<GLvoid *>(static_cast<intptr_t>(offset)));
1259     LOGI("WebGL drawElements end");
1260     return nullptr;
1261 }
1262 
Enable(napi_env env,napi_callback_info info)1263 napi_value WebGLRenderingContextBase::Enable(napi_env env, napi_callback_info info)
1264 {
1265     NFuncArg funcArg(env, info);
1266 
1267     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1268         return nullptr;
1269     }
1270     bool succ = false;
1271     LOGI("WebGL enable start");
1272     int64_t cap;
1273     tie(succ, cap) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1274     if (!succ) {
1275         return nullptr;
1276     }
1277     LOGI("WebGL WebGLRenderingContextBase::enable cap = %{public}u", cap);
1278     glEnable(static_cast<GLenum>(cap));
1279     LOGI("WebGL enable end");
1280     return nullptr;
1281 }
1282 
EnableVertexAttribArray(napi_env env,napi_callback_info info)1283 napi_value WebGLRenderingContextBase::EnableVertexAttribArray(napi_env env, napi_callback_info info)
1284 {
1285     NFuncArg funcArg(env, info);
1286 
1287     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1288         return nullptr;
1289     }
1290     bool succ = false;
1291     LOGI("WebGL enableVertexAttribArray start");
1292     int64_t index;
1293     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1294     if (!succ) {
1295         return nullptr;
1296     }
1297     LOGI("WebGL WebGLRenderingContextBase::enableVertexAttribArray index = %{public}u", index);
1298     glEnableVertexAttribArray(static_cast<GLuint>(index));
1299     LOGI("WebGL enableVertexAttribArray end");
1300     return nullptr;
1301 }
1302 
Finish(napi_env env,napi_callback_info info)1303 napi_value WebGLRenderingContextBase::Finish(napi_env env, napi_callback_info info)
1304 {
1305     LOGI("WebGL finish start");
1306     glFinish();
1307     LOGI("WebGL finish end");
1308     return nullptr;
1309 }
1310 
Flush(napi_env env,napi_callback_info info)1311 napi_value WebGLRenderingContextBase::Flush(napi_env env, napi_callback_info info)
1312 {
1313     NFuncArg funcArg(env, info);
1314 
1315     if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
1316         return nullptr;
1317     }
1318     LOGI("WebGL flush start");
1319     WebGLRenderingContextBasicBase *obj = Util::GetContextObject(env, funcArg.GetThisVar(), "webgl");
1320     if (obj == nullptr) {
1321         LOGE("WebGL flush obj == nullptr");
1322         return nullptr;
1323     }
1324     glFlush();
1325     obj->Update();
1326     LOGI("WebGL flush end");
1327     return nullptr;
1328 }
1329 
FramebufferRenderbuffer(napi_env env,napi_callback_info info)1330 napi_value WebGLRenderingContextBase::FramebufferRenderbuffer(napi_env env, napi_callback_info info)
1331 {
1332     NFuncArg funcArg(env, info);
1333 
1334     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
1335         return nullptr;
1336     }
1337     bool succ = false;
1338     LOGI("WebGL framebufferRenderbuffer start");
1339     int64_t target;
1340     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1341     if (!succ) {
1342         return nullptr;
1343     }
1344     LOGI("WebGL WebGLRenderingContextBase::framebufferRenderbuffer target = %{public}u", target);
1345 
1346     int64_t attachment;
1347     tie(succ, attachment) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
1348     if (!succ) {
1349         return nullptr;
1350     }
1351     LOGI("WebGL WebGLRenderingContextBase::framebufferRenderbuffer attachment = %{public}u", attachment);
1352 
1353     int64_t renderBufferTarget;
1354     tie(succ, renderBufferTarget) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
1355     if (!succ) {
1356         return nullptr;
1357     }
1358 
1359     if (funcArg[NARG_POS::FOURTH] == nullptr) {
1360         return nullptr;
1361     }
1362     WebGLRenderbuffer *webGlRenderbuffer = nullptr;
1363     napi_status renderbufferStatus = napi_unwrap(env, funcArg[NARG_POS::FOURTH], (void **) &webGlRenderbuffer);
1364     if (renderbufferStatus != napi_ok) {
1365         return nullptr;
1366     }
1367     unsigned int renderbuffer = webGlRenderbuffer->GetRenderbuffer();
1368     glFramebufferRenderbuffer(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
1369         static_cast<GLenum>(renderBufferTarget), static_cast<GLuint>(renderbuffer));
1370     LOGI("WebGL framebufferRenderbuffer end");
1371     return nullptr;
1372 }
1373 
1374 
FramebufferTexture2D(napi_env env,napi_callback_info info)1375 napi_value WebGLRenderingContextBase::FramebufferTexture2D(napi_env env, napi_callback_info info)
1376 {
1377     NFuncArg funcArg(env, info);
1378 
1379     if (!funcArg.InitArgs(NARG_CNT::FIVE)) {
1380         return nullptr;
1381     }
1382     bool succ = false;
1383     LOGI("WebGL framebufferTexture2D start");
1384     int64_t target;
1385     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1386     if (!succ) {
1387         return nullptr;
1388     }
1389     LOGI("WebGL WebGLRenderingContextBase::framebufferTexture2D target = %{public}u", target);
1390 
1391     int64_t attachment;
1392     tie(succ, attachment) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
1393     if (!succ) {
1394         return nullptr;
1395     }
1396     LOGI("WebGL WebGLRenderingContextBase::framebufferTexture2D attachment = %{public}u", attachment);
1397 
1398     int64_t textarget;
1399     tie(succ, textarget) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
1400     if (!succ) {
1401         return nullptr;
1402     }
1403     LOGI("WebGL WebGLRenderingContextBase::framebufferTexture2D textarget = %{public}u", textarget);
1404 
1405     if (funcArg[NARG_POS::FOURTH] == nullptr) {
1406         return nullptr;
1407     }
1408     WebGLTexture *webGlTexture = nullptr;
1409     napi_status textureStatus = napi_unwrap(env, funcArg[NARG_POS::FOURTH], (void **) &webGlTexture);
1410     if (textureStatus != napi_ok) {
1411         return nullptr;
1412     }
1413     unsigned int texture = webGlTexture->GetTexture();
1414 
1415     int64_t level;
1416     tie(succ, level) = NVal(env, funcArg[NARG_POS::FIFTH]).ToInt64();
1417     if (!succ) {
1418         return nullptr;
1419     }
1420     LOGI("WebGL WebGLRenderingContextBase::framebufferTexture2D level = %{public}u", level);
1421     glFramebufferTexture2D(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
1422         static_cast<GLenum>(textarget), static_cast<GLuint>(texture), static_cast<GLint>(level));
1423     LOGI("WebGL framebufferTexture2D end");
1424     return nullptr;
1425 }
1426 
GetUniformLocation(napi_env env,napi_callback_info info)1427 napi_value WebGLRenderingContextBase::GetUniformLocation(napi_env env, napi_callback_info info)
1428 {
1429     NFuncArg funcArg(env, info);
1430     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1431         return nullptr;
1432     }
1433     bool succ = false;
1434     LOGI("WebGL getUniformLocation start");
1435     WebGLProgram *webGlProgram = nullptr;
1436     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
1437     if (programStatus != napi_ok) {
1438         return nullptr;
1439     }
1440     int programId = webGlProgram->GetProgramId();
1441     LOGI("WebGL WebGLRenderContext::getUniformLocation programId = %{public}u", programId);
1442     unique_ptr<char[]> name;
1443     tie(succ, name, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
1444     if (!succ) {
1445         return nullptr;
1446     }
1447     LOGI("WebGL WebGLRenderContext::getUniformLocation name = %{public}s", name.get());
1448 
1449     napi_value objUniformLocation = NClass::InstantiateClass(env, WebGLUniformLocation::className, {});
1450     if (!objUniformLocation) {
1451         return nullptr;
1452     }
1453     auto webGLUniformLocation = NClass::GetEntityOf<WebGLUniformLocation>(env, objUniformLocation);
1454     if (!webGLUniformLocation) {
1455         return nullptr;
1456     }
1457     int64_t locationId = static_cast<int64_t>(glGetUniformLocation(static_cast<GLuint>(programId),
1458         const_cast<char *>(name.get())));
1459     LOGI("WebGL WebGLRenderingContextBase::getUniformLocation locationId = %{public}u", locationId);
1460     webGLUniformLocation->SetUniformLocationId(locationId);
1461     LOGI("WebGL getUniformLocation end");
1462     return objUniformLocation;
1463 }
1464 
GetVertexAttribOffset(napi_env env,napi_callback_info info)1465 napi_value WebGLRenderingContextBase::GetVertexAttribOffset(napi_env env, napi_callback_info info)
1466 {
1467     NFuncArg funcArg(env, info);
1468     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1469         return nullptr;
1470     }
1471     bool succ = false;
1472     LOGI("WebGL getVertexAttribOffset start");
1473     int32_t pnameindex;
1474     tie(succ, pnameindex) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1475     if (!succ) {
1476         return nullptr;
1477     }
1478     LOGI("WebGL WebGLRenderContext::getVertexAttribOffset index = %{public}u", pnameindex);
1479     int32_t pname;
1480     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1481     if (!succ) {
1482         return nullptr;
1483     }
1484     LOGI("WebGL WebGLRenderContext::getVertexAttribOffset pname = %{public}u", pname);
1485     GLvoid *point = nullptr;
1486     glGetVertexAttribPointerv(static_cast<GLint>(pnameindex), static_cast<GLint>(pname), &point);
1487     LOGI("WebGL getVertexAttribOffset end");
1488     int po = (double) (intptr_t) point;
1489     return NVal::CreateBool(env, po).val_;
1490 }
1491 
ShaderSource(napi_env env,napi_callback_info info)1492 napi_value WebGLRenderingContextBase::ShaderSource(napi_env env, napi_callback_info info)
1493 {
1494     NFuncArg funcArg(env, info);
1495 
1496     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1497         return nullptr;
1498     }
1499     bool succ = false;
1500     LOGI("WebGL shaderSource start");
1501     WebGLShader *webGlShader = nullptr;
1502     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlShader);
1503     if (shaderStatus != napi_ok) {
1504         return nullptr;
1505     }
1506     int shader = webGlShader->GetShaderId();
1507     LOGI("WebGL WebGLRenderContext::shaderSource shader = %{public}u", shader);
1508     unique_ptr<char[]> source;
1509     tie(succ, source, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
1510     if (!succ) {
1511         return nullptr;
1512     }
1513     LOGI("WebGL WebGLRenderContext::shaderSource source = %{public}s", source.get());
1514     const char *shaderString = source.get();
1515     glShaderSource(static_cast<GLuint>(shader), 1, &shaderString, 0);
1516     string res = shaderString;
1517     auto& objects = ObjectSource::GetInstance().GetObjectMap();
1518     objects.insert({shader, res});
1519     LOGI("WebGL shaderSource end");
1520     return nullptr;
1521 }
1522 
Hint(napi_env env,napi_callback_info info)1523 napi_value WebGLRenderingContextBase::Hint(napi_env env, napi_callback_info info)
1524 {
1525     NFuncArg funcArg(env, info);
1526     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1527         return nullptr;
1528     }
1529     bool succ = false;
1530     LOGI("WebGL hint start");
1531     int32_t target;
1532     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1533     if (!succ) {
1534         return nullptr;
1535     }
1536     LOGI("WebGL WebGLRenderContext::hint target = %{public}u", target);
1537     int32_t mode;
1538     tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1539     if (!succ) {
1540         return nullptr;
1541     }
1542     LOGI("WebGL WebGLRenderContext::hint mode = %{public}u", mode);
1543     glHint(static_cast<GLenum>(target), static_cast<GLenum>(mode));
1544     LOGI("WebGL hint end");
1545     return nullptr;
1546 }
1547 
IsBuffer(napi_env env,napi_callback_info info)1548 napi_value WebGLRenderingContextBase::IsBuffer(napi_env env, napi_callback_info info)
1549 {
1550     NFuncArg funcArg(env, info);
1551     bool res = false;
1552     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1553         return NVal::CreateBool(env, res).val_;
1554     }
1555     LOGI("WebGL isBuffer start");
1556     WebGLBuffer *webGlBuffer = nullptr;
1557     napi_status bufferStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlBuffer);
1558     if (bufferStatus != napi_ok) {
1559         return NVal::CreateBool(env, res).val_;
1560     }
1561     unsigned int buffer = webGlBuffer->GetBuffer();
1562     res = static_cast<bool>(glIsBuffer(static_cast<GLuint>(buffer)));
1563     LOGI("WebGL WebGLRenderContext::isBuffer res = %{public}u", res);
1564     LOGI("WebGL isBuffer end");
1565     return NVal::CreateBool(env, res).val_;
1566 }
1567 
IsEnabled(napi_env env,napi_callback_info info)1568 napi_value WebGLRenderingContextBase::IsEnabled(napi_env env, napi_callback_info info)
1569 {
1570     NFuncArg funcArg(env, info);
1571     bool res = false;
1572     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1573         return NVal::CreateBool(env, res).val_;
1574     }
1575     bool succ = false;
1576     LOGI("WebGL isEnabled start");
1577     int64_t cap;
1578     tie(succ, cap) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
1579     if (!succ) {
1580         return NVal::CreateBool(env, res).val_;
1581     }
1582     LOGI("WebGL WebGLRenderContext::isEnabled cap = %{public}u", cap);
1583     res = static_cast<bool>(glIsEnabled(static_cast<GLenum>(cap)));
1584     LOGI("WebGL isEnabled end");
1585     return NVal::CreateBool(env, res).val_;
1586 }
1587 
RenderbufferStorage(napi_env env,napi_callback_info info)1588 napi_value WebGLRenderingContextBase::RenderbufferStorage(napi_env env, napi_callback_info info)
1589 {
1590     NFuncArg funcArg(env, info);
1591 
1592     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
1593         return nullptr;
1594     }
1595     bool succ = false;
1596     LOGI("WebGL renderbufferStorage start");
1597     int32_t target;
1598     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1599     if (!succ) {
1600         return nullptr;
1601     }
1602     LOGI("WebGL WebGLRenderContext::renderbufferStorage target = %{public}u", target);
1603     int32_t internalformat;
1604     tie(succ, internalformat) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1605     if (!succ) {
1606         return nullptr;
1607     }
1608     LOGI("WebGL WebGLRenderContext::renderbufferStorage internalformat = %{public}u", internalformat);
1609     int32_t width;
1610     tie(succ, width) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
1611     if (!succ) {
1612         return nullptr;
1613     }
1614     LOGI("WebGL WebGLRenderContext::renderbufferStorage width = %{public}u", width);
1615     int32_t height;
1616     tie(succ, height) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt32();
1617     if (!succ) {
1618         return nullptr;
1619     }
1620     LOGI("WebGL WebGLRenderContext::renderbufferStorage height = %{public}u", height);
1621     glRenderbufferStorage(static_cast<GLenum>(target), static_cast<GLenum>(internalformat),
1622                           static_cast<GLsizei>(width), static_cast<GLsizei>(height));
1623     LOGI("WebGL renderbufferStorage end");
1624     return nullptr;
1625 }
1626 
SampleCoverage(napi_env env,napi_callback_info info)1627 napi_value WebGLRenderingContextBase::SampleCoverage(napi_env env, napi_callback_info info)
1628 {
1629     NFuncArg funcArg(env, info);
1630 
1631     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1632         return nullptr;
1633     }
1634     bool succ = false;
1635     LOGI("WebGL sampleCoverage start");
1636     double value;
1637     tie(succ, value) = NVal(env, funcArg[NARG_POS::FIRST]).ToDouble();
1638     if (!succ) {
1639         return nullptr;
1640     }
1641     LOGI("WebGL WebGLRenderContext::sampleCoverage value = %{public}f", value);
1642     bool invert = false;
1643     tie(succ, invert) = NVal(env, funcArg[NARG_POS::SECOND]).ToBool();
1644     if (!succ) {
1645         return nullptr;
1646     }
1647     LOGI("WebGL WebGLRenderContext::sampleCoverage invert = %{public}u", invert);
1648     glSampleCoverage(static_cast<GLclampf>((float) value), static_cast<GLboolean>(invert));
1649     LOGI("WebGL sampleCoverage end");
1650     return nullptr;
1651 }
1652 
Scissor(napi_env env,napi_callback_info info)1653 napi_value WebGLRenderingContextBase::Scissor(napi_env env, napi_callback_info info)
1654 {
1655     NFuncArg funcArg(env, info);
1656 
1657     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
1658         return nullptr;
1659     }
1660     bool succ = false;
1661     LOGI("WebGL scissor start");
1662     int32_t x;
1663     tie(succ, x) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1664     if (!succ) {
1665         return nullptr;
1666     }
1667     LOGI("WebGL WebGLRenderContext::scissor x = %{public}u", x);
1668     int32_t y;
1669     tie(succ, y) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1670     if (!succ) {
1671         return nullptr;
1672     }
1673     LOGI("WebGL WebGLRenderContext::scissor y = %{public}u", y);
1674     int32_t width;
1675     tie(succ, width) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
1676     if (!succ) {
1677         return nullptr;
1678     }
1679     LOGI("WebGL WebGLRenderContext::scissor width = %{public}u", width);
1680     int32_t height;
1681     tie(succ, height) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt32();
1682     if (!succ) {
1683         return nullptr;
1684     }
1685     LOGI("WebGL WebGLRenderContext::scissor height = %{public}u", height);
1686     glScissor(static_cast<GLint>(x), static_cast<GLint>(y), static_cast<GLsizei>(width),
1687               static_cast<GLsizei>(height));
1688     LOGI("WebGL scissor end");
1689     return nullptr;
1690 }
1691 
StencilFunc(napi_env env,napi_callback_info info)1692 napi_value WebGLRenderingContextBase::StencilFunc(napi_env env, napi_callback_info info)
1693 {
1694     NFuncArg funcArg(env, info);
1695 
1696     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
1697         return nullptr;
1698     }
1699     bool succ = false;
1700     LOGI("WebGL stencilFunc start");
1701     int32_t func;
1702     tie(succ, func) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1703     if (!succ) {
1704         return nullptr;
1705     }
1706     LOGI("WebGL WebGLRenderContext::stencilFunc func = %{public}u", func);
1707     int32_t ref;
1708     tie(succ, ref) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1709     if (!succ) {
1710         return nullptr;
1711     }
1712     LOGI("WebGL WebGLRenderContext::stencilFunc ref = %{public}u", ref);
1713     int32_t mask;
1714     tie(succ, mask) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
1715     if (!succ) {
1716         return nullptr;
1717     }
1718     LOGI("WebGL WebGLRenderContext::stencilFunc mask = %{public}u", mask);
1719     glStencilFunc(static_cast<GLenum>(func), static_cast<GLint>(ref), static_cast<GLuint>(mask));
1720     LOGI("WebGL stencilFunc end");
1721     return nullptr;
1722 }
1723 
StencilFuncSeparate(napi_env env,napi_callback_info info)1724 napi_value WebGLRenderingContextBase::StencilFuncSeparate(napi_env env, napi_callback_info info)
1725 {
1726     NFuncArg funcArg(env, info);
1727 
1728     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
1729         return nullptr;
1730     }
1731     bool succ = false;
1732     LOGI("WebGL stencilFuncSeparate start");
1733     int32_t face;
1734     tie(succ, face) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1735     if (!succ) {
1736         return nullptr;
1737     }
1738     LOGI("WebGL WebGLRenderContext::stencilFuncSeparate face = %{public}u", face);
1739     int32_t func;
1740     tie(succ, func) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1741     if (!succ) {
1742         return nullptr;
1743     }
1744     LOGI("WebGL WebGLRenderContext::stencilFuncSeparate func = %{public}u", func);
1745     int32_t ref;
1746     tie(succ, ref) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
1747     if (!succ) {
1748         return nullptr;
1749     }
1750     LOGI("WebGL WebGLRenderContext::stencilFuncSeparate ref = %{public}u", ref);
1751     int32_t mask;
1752     tie(succ, mask) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt32();
1753     if (!succ) {
1754         return nullptr;
1755     }
1756     LOGI("WebGL WebGLRenderContext::stencilFuncSeparate mask = %{public}u", mask);
1757     glStencilFuncSeparate(static_cast<GLenum>(face), static_cast<GLenum>(func), static_cast<GLint>(ref),
1758                           static_cast<GLuint>(mask));
1759     LOGI("WebGL stencilFuncSeparate end");
1760     return nullptr;
1761 }
1762 
StencilMask(napi_env env,napi_callback_info info)1763 napi_value WebGLRenderingContextBase::StencilMask(napi_env env, napi_callback_info info)
1764 {
1765     NFuncArg funcArg(env, info);
1766 
1767     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
1768         return nullptr;
1769     }
1770     bool succ = false;
1771     LOGI("WebGL stencilMask start");
1772     int32_t mask;
1773     tie(succ, mask) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1774     if (!succ) {
1775         return nullptr;
1776     }
1777     LOGI("WebGL WebGLRenderContext::stencilMask mask = %{public}u", mask);
1778     glStencilMask(static_cast<GLuint>(mask));
1779     LOGI("WebGL stencilMask end");
1780     return nullptr;
1781 }
1782 
StencilMaskSeparate(napi_env env,napi_callback_info info)1783 napi_value WebGLRenderingContextBase::StencilMaskSeparate(napi_env env, napi_callback_info info)
1784 {
1785     NFuncArg funcArg(env, info);
1786 
1787     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1788         return nullptr;
1789     }
1790     bool succ = false;
1791     LOGI("WebGL stencilMaskSeparate start");
1792     int32_t face;
1793     tie(succ, face) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1794     if (!succ) {
1795         return nullptr;
1796     }
1797     LOGI("WebGL WebGLRenderContext::stencilMaskSeparate face = %{public}u", face);
1798     int32_t mask;
1799     tie(succ, mask) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1800     if (!succ) {
1801         return nullptr;
1802     }
1803     LOGI("WebGL WebGLRenderContext::stencilMaskSeparate mask = %{public}u", mask);
1804     glStencilMaskSeparate(static_cast<GLenum>(face), static_cast<GLuint>(mask));
1805     LOGI("WebGL stencilMaskSeparate end");
1806     return nullptr;
1807 }
1808 
StencilOp(napi_env env,napi_callback_info info)1809 napi_value WebGLRenderingContextBase::StencilOp(napi_env env, napi_callback_info info)
1810 {
1811     NFuncArg funcArg(env, info);
1812 
1813     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
1814         return nullptr;
1815     }
1816     bool succ = false;
1817     LOGI("WebGL stencilOp start");
1818     int32_t fail;
1819     tie(succ, fail) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1820     if (!succ) {
1821         return nullptr;
1822     }
1823     LOGI("WebGL WebGLRenderContext::stencilOp fail = %{public}u", fail);
1824     int32_t zfail;
1825     tie(succ, zfail) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1826     if (!succ) {
1827         return nullptr;
1828     }
1829     LOGI("WebGL WebGLRenderContext::stencilOp zfail = %{public}u", zfail);
1830     int32_t zpass;
1831     tie(succ, zpass) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
1832     if (!succ) {
1833         return nullptr;
1834     }
1835     LOGI("WebGL WebGLRenderContext::stencilOp zpass = %{public}u", zpass);
1836     glStencilOp(static_cast<GLenum>(fail), static_cast<GLenum>(zfail), static_cast<GLenum>(zpass));
1837     LOGI("WebGL stencilOp end");
1838     return nullptr;
1839 }
1840 
StencilOpSeparate(napi_env env,napi_callback_info info)1841 napi_value WebGLRenderingContextBase::StencilOpSeparate(napi_env env, napi_callback_info info)
1842 {
1843     NFuncArg funcArg(env, info);
1844 
1845     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
1846         return nullptr;
1847     }
1848     bool succ = false;
1849     LOGI("WebGL stencilOpSeparate start");
1850     int32_t face;
1851     tie(succ, face) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1852     if (!succ) {
1853         return nullptr;
1854     }
1855     LOGI("WebGL WebGLRenderContext::stencilOpSeparate face = %{public}u", face);
1856     int32_t sfail;
1857     tie(succ, sfail) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1858     if (!succ) {
1859         return nullptr;
1860     }
1861     LOGI("WebGL WebGLRenderContext::stencilOpSeparate sfail = %{public}u", sfail);
1862     int32_t dpfail;
1863     tie(succ, dpfail) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
1864     if (!succ) {
1865         return nullptr;
1866     }
1867     LOGI("WebGL WebGLRenderContext::stencilOpSeparate dpfail = %{public}u", dpfail);
1868     int32_t dppass;
1869     tie(succ, dppass) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt32();
1870     if (!succ) {
1871         return nullptr;
1872     }
1873     LOGI("WebGL WebGLRenderContext::stencilOpSeparate dppass = %{public}u", dppass);
1874     glStencilOpSeparate(static_cast<GLenum>(face), static_cast<GLenum>(sfail), static_cast<GLenum>(dpfail),
1875                         static_cast<GLenum>(dppass));
1876     LOGI("WebGL stencilOpSeparate end");
1877     return nullptr;
1878 }
1879 
TexParameterf(napi_env env,napi_callback_info info)1880 napi_value WebGLRenderingContextBase::TexParameterf(napi_env env, napi_callback_info info)
1881 {
1882     NFuncArg funcArg(env, info);
1883 
1884     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
1885         return nullptr;
1886     }
1887     bool succ = false;
1888     LOGI("WebGL texParameterf start");
1889     int32_t target;
1890     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1891     if (!succ) {
1892         return nullptr;
1893     }
1894     LOGI("WebGL WebGLRenderContext::texParameterf target = %{public}u", target);
1895     int32_t pname;
1896     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1897     if (!succ) {
1898         return nullptr;
1899     }
1900     LOGI("WebGL WebGLRenderContext::texParameterf pname = %{public}u", pname);
1901     double param;
1902     tie(succ, param) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
1903     if (!succ) {
1904         return nullptr;
1905     }
1906     LOGI("WebGL WebGLRenderContext::texParameterf param = %{public}f", param);
1907     glTexParameterf(static_cast<GLenum>(target), static_cast<GLenum>(pname), static_cast<GLfloat>((float) param));
1908     LOGI("WebGL texParameterf end");
1909     return nullptr;
1910 }
1911 
TexParameteri(napi_env env,napi_callback_info info)1912 napi_value WebGLRenderingContextBase::TexParameteri(napi_env env, napi_callback_info info)
1913 {
1914     NFuncArg funcArg(env, info);
1915 
1916     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
1917         return nullptr;
1918     }
1919     bool succ = false;
1920     LOGI("WebGL texParameteri start");
1921     int32_t target;
1922     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
1923     if (!succ) {
1924         return nullptr;
1925     }
1926     LOGI("WebGL WebGLRenderContext::texParameteri target = %{public}u", target);
1927     int32_t pname;
1928     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
1929     if (!succ) {
1930         return nullptr;
1931     }
1932     LOGI("WebGL WebGLRenderContext::texParameteri pname = %{public}u", pname);
1933     int32_t param;
1934     tie(succ, param) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
1935     if (!succ) {
1936         return nullptr;
1937     }
1938     LOGI("WebGL WebGLRenderContext::texParameteri param = %{public}u", param);
1939     glTexParameteri(static_cast<GLenum>(target), static_cast<GLenum>(pname), static_cast<GLint>(param));
1940     LOGI("WebGL texParameteri end");
1941     return nullptr;
1942 }
1943 
Uniform1f(napi_env env,napi_callback_info info)1944 napi_value WebGLRenderingContextBase::Uniform1f(napi_env env, napi_callback_info info)
1945 {
1946     NFuncArg funcArg(env, info);
1947 
1948     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
1949         return nullptr;
1950     }
1951     bool succ = false;
1952     LOGI("WebGL uniform1f start");
1953     WebGLUniformLocation *webGLUniformLocation = nullptr;
1954     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
1955     if (locationStatus != napi_ok) {
1956         return nullptr;
1957     }
1958     int location = webGLUniformLocation->GetUniformLocationId();
1959     double v0;
1960     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
1961     if (!succ) {
1962         return nullptr;
1963     }
1964     LOGI("WebGL WebGLRenderContext::uniform1f v0 = %{public}f", v0);
1965     glUniform1f(static_cast<GLint>(location), static_cast<GLfloat>((float) v0));
1966     LOGI("WebGL uniform1f end");
1967     return nullptr;
1968 }
1969 
Uniform2f(napi_env env,napi_callback_info info)1970 napi_value WebGLRenderingContextBase::Uniform2f(napi_env env, napi_callback_info info)
1971 {
1972     NFuncArg funcArg(env, info);
1973 
1974     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
1975         return nullptr;
1976     }
1977     bool succ = false;
1978     LOGI("WebGL uniform2f start");
1979     WebGLUniformLocation *webGLUniformLocation = nullptr;
1980     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
1981     if (locationStatus != napi_ok) {
1982         return nullptr;
1983     }
1984     int location = webGLUniformLocation->GetUniformLocationId();
1985     double v0;
1986     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
1987     if (!succ) {
1988         return nullptr;
1989     }
1990     LOGI("WebGL WebGLRenderContext::uniform2f v0 = %{public}f", v0);
1991     double v1;
1992     tie(succ, v1) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
1993     if (!succ) {
1994         return nullptr;
1995     }
1996     LOGI("WebGL WebGLRenderContext::uniform2f v1 = %{public}f", v1);
1997     glUniform2f(static_cast<GLint>(location), static_cast<GLfloat>((float) v0), static_cast<GLfloat>((float) v1));
1998     LOGI("WebGL uniform2f end");
1999     return nullptr;
2000 }
2001 
Uniform3f(napi_env env,napi_callback_info info)2002 napi_value WebGLRenderingContextBase::Uniform3f(napi_env env, napi_callback_info info)
2003 {
2004     NFuncArg funcArg(env, info);
2005 
2006     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
2007         return nullptr;
2008     }
2009     bool succ = false;
2010     LOGI("WebGL uniform3f start");
2011     WebGLUniformLocation *webGLUniformLocation = nullptr;
2012     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
2013     if (locationStatus != napi_ok) {
2014         return nullptr;
2015     }
2016     int location = webGLUniformLocation->GetUniformLocationId();
2017     double v0;
2018     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
2019     if (!succ) {
2020         return nullptr;
2021     }
2022     LOGI("WebGL WebGLRenderContext::uniform3f v0 = %{public}f", v0);
2023     double v1;
2024     tie(succ, v1) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
2025     if (!succ) {
2026         return nullptr;
2027     }
2028     LOGI("WebGL WebGLRenderContext::uniform3f v1 = %{public}f", v1);
2029     double v2;
2030     tie(succ, v2) = NVal(env, funcArg[NARG_POS::FOURTH]).ToDouble();
2031     if (!succ) {
2032         return nullptr;
2033     }
2034     LOGI("WebGL WebGLRenderContext::uniform3f v2 = %{public}f", v2);
2035     glUniform3f(static_cast<GLint>(location), static_cast<GLfloat>((float) v0), static_cast<GLfloat>((float) v1),
2036                 static_cast<GLfloat>((float) v2));
2037     LOGI("WebGL uniform3f end");
2038     return nullptr;
2039 }
2040 
Uniform4f(napi_env env,napi_callback_info info)2041 napi_value WebGLRenderingContextBase::Uniform4f(napi_env env, napi_callback_info info)
2042 {
2043     NFuncArg funcArg(env, info);
2044 
2045     if (!funcArg.InitArgs(NARG_CNT::FIVE)) {
2046         return nullptr;
2047     }
2048     bool succ = false;
2049     LOGI("WebGL uniform4f start");
2050     WebGLUniformLocation *webGLUniformLocation = nullptr;
2051     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
2052     if (locationStatus != napi_ok) {
2053         return nullptr;
2054     }
2055     int location = webGLUniformLocation->GetUniformLocationId();
2056     double v0;
2057     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
2058     if (!succ) {
2059         return nullptr;
2060     }
2061     LOGI("WebGL WebGLRenderContext::uniform4f v0 = %{public}f", v0);
2062     double v1;
2063     tie(succ, v1) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
2064     if (!succ) {
2065         return nullptr;
2066     }
2067     LOGI("WebGL WebGLRenderContext::uniform4f v1 = %{public}f", v1);
2068     double v2;
2069     tie(succ, v2) = NVal(env, funcArg[NARG_POS::FOURTH]).ToDouble();
2070     if (!succ) {
2071         return nullptr;
2072     }
2073     LOGI("WebGL WebGLRenderContext::uniform4f v2 = %{public}f", v2);
2074     double v3;
2075     tie(succ, v3) = NVal(env, funcArg[NARG_POS::FIFTH]).ToDouble();
2076     if (!succ) {
2077         return nullptr;
2078     }
2079     LOGI("WebGL WebGLRenderContext::uniform4f v3 = %{public}f", v3);
2080     glUniform4f(static_cast<GLint>(location), static_cast<GLfloat>((float) v0), static_cast<GLfloat>((float) v1),
2081                 static_cast<GLfloat>((float) v2), static_cast<GLfloat>((float) v3));
2082     LOGI("WebGL uniform4f end");
2083     return nullptr;
2084 }
2085 
Uniform1i(napi_env env,napi_callback_info info)2086 napi_value WebGLRenderingContextBase::Uniform1i(napi_env env, napi_callback_info info)
2087 {
2088     NFuncArg funcArg(env, info);
2089 
2090     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2091         return nullptr;
2092     }
2093     bool succ = false;
2094     LOGI("WebGL uniform1i start");
2095     WebGLUniformLocation *webGLUniformLocation = nullptr;
2096     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
2097     if (locationStatus != napi_ok) {
2098         return nullptr;
2099     }
2100     int location = webGLUniformLocation->GetUniformLocationId();
2101     int32_t v0;
2102     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
2103     if (!succ) {
2104         return nullptr;
2105     }
2106     LOGI("WebGL WebGLRenderContext::uniform1i v0 = %{public}u", v0);
2107     glUniform1i(static_cast<GLint>(location), static_cast<GLint>(v0));
2108     LOGI("WebGL uniform1i end");
2109     return nullptr;
2110 }
2111 
Uniform2i(napi_env env,napi_callback_info info)2112 napi_value WebGLRenderingContextBase::Uniform2i(napi_env env, napi_callback_info info)
2113 {
2114     NFuncArg funcArg(env, info);
2115 
2116     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
2117         return nullptr;
2118     }
2119     bool succ = false;
2120     LOGI("WebGL uniform2i start");
2121     WebGLUniformLocation *webGLUniformLocation = nullptr;
2122     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
2123     if (locationStatus != napi_ok) {
2124         return nullptr;
2125     }
2126     int location = webGLUniformLocation->GetUniformLocationId();
2127     int32_t v0;
2128     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
2129     if (!succ) {
2130         return nullptr;
2131     }
2132     LOGI("WebGL WebGLRenderContext::uniform2i v0 = %{public}u", v0);
2133     int32_t v1;
2134     tie(succ, v1) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
2135     if (!succ) {
2136         return nullptr;
2137     }
2138     LOGI("WebGL WebGLRenderContext::uniform2i v1 = %{public}u", v1);
2139     glUniform2i(static_cast<GLint>(location), static_cast<GLint>(v0), static_cast<GLint>(v1));
2140     LOGI("WebGL uniform2i end");
2141     return nullptr;
2142 }
2143 
Uniform3i(napi_env env,napi_callback_info info)2144 napi_value WebGLRenderingContextBase::Uniform3i(napi_env env, napi_callback_info info)
2145 {
2146     NFuncArg funcArg(env, info);
2147 
2148     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
2149         return nullptr;
2150     }
2151     bool succ = false;
2152     LOGI("WebGL uniform3i start");
2153     WebGLUniformLocation *webGLUniformLocation = nullptr;
2154     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
2155     if (locationStatus != napi_ok) {
2156         return nullptr;
2157     }
2158     int location = webGLUniformLocation->GetUniformLocationId();
2159     int32_t v0;
2160     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
2161     if (!succ) {
2162         return nullptr;
2163     }
2164     LOGI("WebGL WebGLRenderContext::uniform3i v0 = %{public}u", v0);
2165     int32_t v1;
2166     tie(succ, v1) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
2167     if (!succ) {
2168         return nullptr;
2169     }
2170     LOGI("WebGL WebGLRenderContext::uniform3i v1 = %{public}u", v1);
2171     int32_t v2;
2172     tie(succ, v2) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt32();
2173     if (!succ) {
2174         return nullptr;
2175     }
2176     LOGI("WebGL WebGLRenderContext::uniform3i v2 = %{public}u", v2);
2177     glUniform3i(static_cast<GLint>(location), static_cast<GLint>(v0), static_cast<GLint>(v1),
2178                 static_cast<GLint>(v2));
2179     LOGI("WebGL uniform3i end");
2180     return nullptr;
2181 }
2182 
Uniform4i(napi_env env,napi_callback_info info)2183 napi_value WebGLRenderingContextBase::Uniform4i(napi_env env, napi_callback_info info)
2184 {
2185     NFuncArg funcArg(env, info);
2186 
2187     if (!funcArg.InitArgs(NARG_CNT::FIVE)) {
2188         return nullptr;
2189     }
2190     bool succ = false;
2191     LOGI("WebGL uniform4i start");
2192     WebGLUniformLocation *webGLUniformLocation = nullptr;
2193     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGLUniformLocation);
2194     if (locationStatus != napi_ok) {
2195         return nullptr;
2196     }
2197     int location = webGLUniformLocation->GetUniformLocationId();
2198     int32_t v0;
2199     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
2200     if (!succ) {
2201         return nullptr;
2202     }
2203     LOGI("WebGL WebGLRenderContext::uniform4i v0 = %{public}u", v0);
2204     int32_t v1;
2205     tie(succ, v1) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
2206     if (!succ) {
2207         return nullptr;
2208     }
2209     LOGI("WebGL WebGLRenderContext::uniform4i v1 = %{public}u", v1);
2210     int32_t v2;
2211     tie(succ, v2) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt32();
2212     if (!succ) {
2213         return nullptr;
2214     }
2215     LOGI("WebGL WebGLRenderContext::uniform4i v2 = %{public}u", v2);
2216     int32_t v3;
2217     tie(succ, v3) = NVal(env, funcArg[NARG_POS::FIFTH]).ToInt32();
2218     if (!succ) {
2219         return nullptr;
2220     }
2221     LOGI("WebGL WebGLRenderContext::uniform4i v3 = %{public}u", v3);
2222     glUniform4i(static_cast<GLint>(location), static_cast<GLint>(v0), static_cast<GLint>(v1),
2223                 static_cast<GLint>(v2), static_cast<GLint>(v3));
2224     LOGI("WebGL uniform4i end");
2225     return nullptr;
2226 }
2227 
UseProgram(napi_env env,napi_callback_info info)2228 napi_value WebGLRenderingContextBase::UseProgram(napi_env env, napi_callback_info info)
2229 {
2230     NFuncArg funcArg(env, info);
2231 
2232     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2233         return nullptr;
2234     }
2235     LOGI("WebGL useProgram start");
2236     WebGLProgram *webGlProgram = nullptr;
2237     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
2238     if (programStatus != napi_ok) {
2239         return nullptr;
2240     }
2241     int program = webGlProgram->GetProgramId();
2242     glUseProgram(static_cast<GLuint>(program));
2243     LOGI("WebGL useProgram end");
2244     return nullptr;
2245 }
2246 
ValidateProgram(napi_env env,napi_callback_info info)2247 napi_value WebGLRenderingContextBase::ValidateProgram(napi_env env, napi_callback_info info)
2248 {
2249     NFuncArg funcArg(env, info);
2250 
2251     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2252         return nullptr;
2253     }
2254     LOGI("WebGL validateProgram start");
2255     WebGLProgram *webGlProgram = nullptr;
2256     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
2257     if (programStatus != napi_ok) {
2258         return nullptr;
2259     }
2260     int program = webGlProgram->GetProgramId();
2261     LOGI("WebGL WebGLRenderContext::validateProgram program = %{public}u", program);
2262     glValidateProgram(static_cast<GLuint>(program));
2263     LOGI("WebGL validateProgram end");
2264     return nullptr;
2265 }
2266 
VertexAttrib1f(napi_env env,napi_callback_info info)2267 napi_value WebGLRenderingContextBase::VertexAttrib1f(napi_env env, napi_callback_info info)
2268 {
2269     NFuncArg funcArg(env, info);
2270 
2271     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2272         return nullptr;
2273     }
2274     bool succ = false;
2275     LOGI("WebGL vertexAttrib1f start");
2276     int32_t index;
2277     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
2278     if (!succ) {
2279         return nullptr;
2280     }
2281     LOGI("WebGL WebGLRenderContext::vertexAttrib1f index = %{public}u", index);
2282     double v0;
2283     tie(succ, v0) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
2284     if (!succ) {
2285         return nullptr;
2286     }
2287     LOGI("WebGL WebGLRenderContext::vertexAttrib1f v0 = %{public}f", v0);
2288     glVertexAttrib1f(static_cast<GLuint>(index), static_cast<GLfloat>((float) v0));
2289     LOGI("WebGL vertexAttrib1f end");
2290     return nullptr;
2291 }
2292 
VertexAttrib2f(napi_env env,napi_callback_info info)2293 napi_value WebGLRenderingContextBase::VertexAttrib2f(napi_env env, napi_callback_info info)
2294 {
2295     NFuncArg funcArg(env, info);
2296 
2297     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
2298         return nullptr;
2299     }
2300     bool succ = false;
2301     LOGI("WebGL vertexAttrib2f start");
2302     int32_t index;
2303     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
2304     if (!succ) {
2305         return nullptr;
2306     }
2307     LOGI("WebGL WebGLRenderContext::vertexAttrib2f index = %{public}u", index);
2308     double x;
2309     tie(succ, x) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
2310     if (!succ) {
2311         return nullptr;
2312     }
2313     LOGI("WebGL WebGLRenderContext::vertexAttrib2f x = %{public}f", x);
2314     double y;
2315     tie(succ, y) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
2316     if (!succ) {
2317         return nullptr;
2318     }
2319     LOGI("WebGL WebGLRenderContext::vertexAttrib2f y = %{public}f", y);
2320     glVertexAttrib2f(static_cast<GLint>(index), static_cast<GLfloat>((float) x), static_cast<GLfloat>((float) y));
2321     LOGI("WebGL vertexAttrib2f end");
2322     return nullptr;
2323 }
2324 
VertexAttrib3f(napi_env env,napi_callback_info info)2325 napi_value WebGLRenderingContextBase::VertexAttrib3f(napi_env env, napi_callback_info info)
2326 {
2327     NFuncArg funcArg(env, info);
2328 
2329     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
2330         return nullptr;
2331     }
2332     bool succ = false;
2333     LOGI("WebGL vertexAttrib3f start");
2334     int32_t index;
2335     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
2336     if (!succ) {
2337         return nullptr;
2338     }
2339     LOGI("WebGL WebGLRenderContext::vertexAttrib3f index = %{public}u", index);
2340     double x;
2341     tie(succ, x) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
2342     if (!succ) {
2343         return nullptr;
2344     }
2345     LOGI("WebGL WebGLRenderContext::vertexAttrib3f x = %{public}f", x);
2346     double y;
2347     tie(succ, y) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
2348     if (!succ) {
2349         return nullptr;
2350     }
2351     LOGI("WebGL WebGLRenderContext::vertexAttrib3f y = %{public}f", y);
2352     double z;
2353     tie(succ, z) = NVal(env, funcArg[NARG_POS::FOURTH]).ToDouble();
2354     if (!succ) {
2355         return nullptr;
2356     }
2357     LOGI("WebGL WebGLRenderContext::vertexAttrib3f z = %{public}f", z);
2358     glVertexAttrib3f(static_cast<GLuint>(index), static_cast<GLfloat>((float) x), static_cast<GLfloat>((float) y),
2359                      static_cast<GLfloat>(z));
2360     LOGI("WebGL vertexAttrib3f end");
2361     return nullptr;
2362 }
2363 
VertexAttrib4f(napi_env env,napi_callback_info info)2364 napi_value WebGLRenderingContextBase::VertexAttrib4f(napi_env env, napi_callback_info info)
2365 {
2366     NFuncArg funcArg(env, info);
2367 
2368     if (!funcArg.InitArgs(NARG_CNT::FIVE)) {
2369         return nullptr;
2370     }
2371     bool succ = false;
2372     LOGI("WebGL vertexAttrib4f start");
2373     int32_t index;
2374     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
2375     if (!succ) {
2376         return nullptr;
2377     }
2378     LOGI("WebGL WebGLRenderContext::vertexAttrib4f index = %{public}u", index);
2379     double x;
2380     tie(succ, x) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
2381     if (!succ) {
2382         return nullptr;
2383     }
2384     LOGI("WebGL WebGLRenderContext::vertexAttrib4f x = %{public}f", x);
2385     double y;
2386     tie(succ, y) = NVal(env, funcArg[NARG_POS::THIRD]).ToDouble();
2387     if (!succ) {
2388         return nullptr;
2389     }
2390     LOGI("WebGL WebGLRenderContext::vertexAttrib4f y = %{public}f", y);
2391     double z;
2392     tie(succ, z) = NVal(env, funcArg[NARG_POS::FOURTH]).ToDouble();
2393     if (!succ) {
2394         return nullptr;
2395     }
2396     LOGI("WebGL WebGLRenderContext::vertexAttrib4f z = %{public}f", z);
2397     double w;
2398     tie(succ, w) = NVal(env, funcArg[NARG_POS::FIFTH]).ToDouble();
2399     if (!succ) {
2400         return nullptr;
2401     }
2402     LOGI("WebGL WebGLRenderContext::vertexAttrib4f w = %{public}f", w);
2403     glVertexAttrib4f(static_cast<GLuint>(index), static_cast<GLfloat>((float) x), static_cast<GLfloat>((float) y),
2404                      static_cast<GLfloat>((float) z), static_cast<GLfloat>((float) w));
2405     LOGI("WebGL vertexAttrib4f end");
2406     return nullptr;
2407 }
2408 
VertexAttribPointer(napi_env env,napi_callback_info info)2409 napi_value WebGLRenderingContextBase::VertexAttribPointer(napi_env env, napi_callback_info info)
2410 {
2411     NFuncArg funcArg(env, info);
2412 
2413     if (!funcArg.InitArgs(NARG_CNT::SIX)) {
2414         return nullptr;
2415     }
2416     bool succ = false;
2417     LOGI("WebGL vertexAttribPointer start");
2418     int32_t index;
2419     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
2420     if (!succ) {
2421         return nullptr;
2422     }
2423     LOGI("WebGL WebGLRenderContext::vertexAttribPointer index = %{public}u", index);
2424     int32_t size;
2425     tie(succ, size) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
2426     if (!succ) {
2427         return nullptr;
2428     }
2429     LOGI("WebGL WebGLRenderContext::vertexAttribPointer size = %{public}u", size);
2430     int32_t type;
2431     tie(succ, type) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
2432     if (!succ) {
2433         return nullptr;
2434     }
2435     LOGI("WebGL WebGLRenderContext::vertexAttribPointer type = %{public}u", type);
2436     bool normalized = false;
2437     tie(succ, normalized) = NVal(env, funcArg[NARG_POS::FOURTH]).ToBool();
2438     if (!succ) {
2439         return nullptr;
2440     }
2441     LOGI("WebGL WebGLRenderContext::vertexAttribPointer normalized = %{public}u", normalized);
2442     int32_t stride;
2443     tie(succ, stride) = NVal(env, funcArg[NARG_POS::FIFTH]).ToInt32();
2444     if (!succ) {
2445         return nullptr;
2446     }
2447     LOGI("WebGL WebGLRenderContext::vertexAttribPointer stride = %{public}u", stride);
2448     int32_t offset;
2449     tie(succ, offset) = NVal(env, funcArg[NARG_POS::SIXTH]).ToInt32();
2450     if (!succ) {
2451         return nullptr;
2452     }
2453     LOGI("WebGL WebGLRenderContext::vertexAttribPointer offset = %{public}u", offset);
2454     glVertexAttribPointer(static_cast<GLuint>(index), static_cast<GLint>(size), static_cast<GLenum>(type),
2455                           static_cast<GLboolean>(normalized), static_cast<GLsizei>(stride),
2456                           reinterpret_cast<GLvoid *>(static_cast<intptr_t>(offset)));
2457     LOGI("WebGL vertexAttribPointer end");
2458     return nullptr;
2459 }
2460 
Viewport(napi_env env,napi_callback_info info)2461 napi_value WebGLRenderingContextBase::Viewport(napi_env env, napi_callback_info info)
2462 {
2463     NFuncArg funcArg(env, info);
2464 
2465     if (!funcArg.InitArgs(NARG_CNT::FOUR)) {
2466         return nullptr;
2467     }
2468     bool succ = false;
2469     LOGI("webgl viewport start");
2470     int32_t x;
2471     tie(succ, x) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
2472     if (!succ) {
2473         return nullptr;
2474     }
2475     LOGI("webgl WebGLRenderContext::viewport x = %{public}u", x);
2476     int32_t y;
2477     tie(succ, y) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
2478     if (!succ) {
2479         return nullptr;
2480     }
2481     LOGI("webgl WebGLRenderContext::viewport y = %{public}u", y);
2482     int32_t width;
2483     tie(succ, width) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
2484     if (!succ) {
2485         return nullptr;
2486     }
2487     LOGI("webgl WebGLRenderContext::viewport width = %{public}u", width);
2488     int32_t height;
2489     tie(succ, height) = NVal(env, funcArg[NARG_POS::FOURTH]).ToInt32();
2490     if (!succ) {
2491         return nullptr;
2492     }
2493     LOGI("webgl WebGLRenderContext::viewport height = %{public}u", height);
2494     glViewport(static_cast<GLint>(x), static_cast<GLint>(y), static_cast<GLsizei>(width),
2495                static_cast<GLsizei>(height));
2496     LOGI("webgl viewport end");
2497     return nullptr;
2498 }
2499 
IsFramebuffer(napi_env env,napi_callback_info info)2500 napi_value WebGLRenderingContextBase::IsFramebuffer(napi_env env, napi_callback_info info)
2501 {
2502     LOGI("WebGL isFramebuffer into one");
2503     NFuncArg funcArg(env, info);
2504     bool res = false;
2505     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2506         return NVal::CreateBool(env, res).val_;
2507     }
2508 
2509     if (funcArg[NARG_POS::FIRST] == nullptr) {
2510         return NVal::CreateBool(env, res).val_;
2511     }
2512     LOGI("WebGL isFramebuffer start");
2513     WebGLFramebuffer *webGLFramebuffer = nullptr;
2514     napi_status framebufferStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGLFramebuffer);
2515     if (framebufferStatus != napi_ok) {
2516         return NVal::CreateBool(env, res).val_;
2517     }
2518     unsigned int framebuffer = webGLFramebuffer->GetFramebuffer();
2519     LOGI("WebGL WebGLRenderContext::isFramebuffer framebuffer = %{public}u", framebuffer);
2520     res = static_cast<bool>(glIsFramebuffer(static_cast<GLuint>(framebuffer)));
2521     LOGI("WebGL isFramebuffer end");
2522     return NVal::CreateBool(env, res).val_;
2523 }
2524 
IsProgram(napi_env env,napi_callback_info info)2525 napi_value WebGLRenderingContextBase::IsProgram(napi_env env, napi_callback_info info)
2526 {
2527     NFuncArg funcArg(env, info);
2528     bool res = false;
2529     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2530         return NVal::CreateBool(env, res).val_;
2531     }
2532     if (funcArg[NARG_POS::FIRST] == nullptr) {
2533         return NVal::CreateBool(env, res).val_;
2534     }
2535     LOGI("WebGL isProgram start");
2536     WebGLProgram *webGLProgram = nullptr;
2537     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGLProgram);
2538     if (programStatus != napi_ok) {
2539         return NVal::CreateBool(env, res).val_;
2540     }
2541     unsigned int program = static_cast<unsigned int>(webGLProgram->GetProgramId());
2542     LOGI("WebGL WebGLRenderContext::isProgram program = %{public}u", program);
2543     res = static_cast<bool>(glIsProgram(static_cast<GLuint>(program)));
2544     LOGI("WebGL isProgram end");
2545     return NVal::CreateBool(env, res).val_;
2546 }
2547 
IsRenderbuffer(napi_env env,napi_callback_info info)2548 napi_value WebGLRenderingContextBase::IsRenderbuffer(napi_env env, napi_callback_info info)
2549 {
2550     NFuncArg funcArg(env, info);
2551     bool res = false;
2552     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2553         return NVal::CreateBool(env, res).val_;
2554     }
2555     if (funcArg[NARG_POS::FIRST] == nullptr) {
2556         return NVal::CreateBool(env, res).val_;
2557     }
2558     LOGI("WebGL isRenderbuffer start");
2559     WebGLRenderbuffer *webGLRenderbuffer = nullptr;
2560     napi_status renderbufferStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGLRenderbuffer);
2561     if (renderbufferStatus != napi_ok) {
2562         return NVal::CreateBool(env, res).val_;
2563     }
2564     unsigned int renderbuffer = static_cast<unsigned int>(webGLRenderbuffer->GetRenderbuffer());
2565     LOGI("WebGL WebGLRenderContext::isRenderbuffer renderbuffer = %{public}u", renderbuffer);
2566     res = static_cast<bool>(glIsRenderbuffer(static_cast<GLuint>(renderbuffer)));
2567     LOGI("WebGL isRenderbuffer end");
2568     return NVal::CreateBool(env, res).val_;
2569 }
2570 
IsShader(napi_env env,napi_callback_info info)2571 napi_value WebGLRenderingContextBase::IsShader(napi_env env, napi_callback_info info)
2572 {
2573     NFuncArg funcArg(env, info);
2574     bool res = false;
2575     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2576         return NVal::CreateBool(env, res).val_;
2577     }
2578     if (funcArg[NARG_POS::FIRST] == nullptr) {
2579         return NVal::CreateBool(env, res).val_;
2580     }
2581     LOGI("WebGL isShader start");
2582     WebGLShader *webGLShader = nullptr;
2583     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGLShader);
2584     if (shaderStatus != napi_ok) {
2585         return NVal::CreateBool(env, res).val_;
2586     }
2587     unsigned int shader = static_cast<unsigned int>(webGLShader->GetShaderId());
2588     LOGI("WebGL WebGLRenderContext::isShader shader = %{public}u", shader);
2589     res = static_cast<bool>(glIsShader(static_cast<GLuint>(shader)));
2590     LOGI("WebGL isShader end");
2591     return NVal::CreateBool(env, res).val_;
2592 }
2593 
IsTexture(napi_env env,napi_callback_info info)2594 napi_value WebGLRenderingContextBase::IsTexture(napi_env env, napi_callback_info info)
2595 {
2596     NFuncArg funcArg(env, info);
2597     bool res = false;
2598     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2599         return NVal::CreateBool(env, res).val_;
2600     }
2601     if (funcArg[NARG_POS::FIRST] == nullptr) {
2602         return NVal::CreateBool(env, res).val_;
2603     }
2604     LOGI("WebGL isTexture start");
2605     WebGLTexture *webGLTexture = nullptr;
2606     napi_status textureStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGLTexture);
2607     if (textureStatus != napi_ok) {
2608         return NVal::CreateBool(env, res).val_;
2609     }
2610     unsigned int texture = webGLTexture->GetTexture();
2611     LOGI("WebGL WebGLRenderContext::isTexture texture = %{public}u", texture);
2612     res = static_cast<bool>(glIsTexture(static_cast<GLuint>(texture)));
2613     LOGI("WebGL isTexture end");
2614     return NVal::CreateBool(env, res).val_;
2615 }
2616 
LineWidth(napi_env env,napi_callback_info info)2617 napi_value WebGLRenderingContextBase::LineWidth(napi_env env, napi_callback_info info)
2618 {
2619     NFuncArg funcArg(env, info);
2620 
2621     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2622         return nullptr;
2623     }
2624     bool succ = false;
2625     LOGI("WebGL lineWidth start");
2626     double linewidth;
2627     tie(succ, linewidth) = NVal(env, funcArg[NARG_POS::FIRST]).ToDouble();
2628     if (!succ) {
2629         return nullptr;
2630     }
2631     LOGI("WebGL WebGLRenderContext::lineWidth linewidth = %{public}f", linewidth);
2632     glLineWidth(static_cast<GLfloat>((float) linewidth));
2633     LOGI("WebGL lineWidth end");
2634     return nullptr;
2635 }
2636 
LinkProgram(napi_env env,napi_callback_info info)2637 napi_value WebGLRenderingContextBase::LinkProgram(napi_env env, napi_callback_info info)
2638 {
2639     NFuncArg funcArg(env, info);
2640 
2641     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2642         return nullptr;
2643     }
2644     if (funcArg[NARG_POS::FIRST] == nullptr) {
2645         return nullptr;
2646     }
2647     LOGI("WebGL linkProgram start");
2648     WebGLProgram *webGLProgram = nullptr;
2649     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGLProgram);
2650     if (programStatus != napi_ok) {
2651         return nullptr;
2652     }
2653     unsigned int linkprogram = static_cast<unsigned int>(webGLProgram->GetProgramId());
2654     LOGI("WebGL WebGLRenderContext::linkProgram linkprogram = %{public}u", linkprogram);
2655     glLinkProgram(static_cast<GLuint>(linkprogram));
2656     LOGI("WebGL linkProgram end");
2657     return nullptr;
2658 }
2659 
PixelStorei(napi_env env,napi_callback_info info)2660 napi_value WebGLRenderingContextBase::PixelStorei(napi_env env, napi_callback_info info)
2661 {
2662     NFuncArg funcArg(env, info);
2663 
2664     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2665         return nullptr;
2666     }
2667     bool succ = false;
2668     LOGI("WebGL pixelStorei start");
2669     int32_t pname;
2670     tie(succ, pname) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
2671     if (!succ) {
2672         return nullptr;
2673     }
2674     LOGI("WebGL WebGLRenderContext::pixelStorei pname = %{public}u", pname);
2675     bool parambool = false;
2676     tie(succ, parambool) = NVal(env, funcArg[NARG_POS::SECOND]).ToBool();
2677     if (succ) {
2678         return nullptr;
2679     }
2680     int32_t param;
2681     tie(succ, param) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
2682     if (!succ) {
2683         return nullptr;
2684     }
2685     LOGI("WebGL WebGLRenderContext::pixelStorei pname = %{public}u", param);
2686     glPixelStorei(static_cast<GLenum>(pname), static_cast<GLint>(param));
2687     LOGI("WebGL pixelStorei end");
2688     return nullptr;
2689 }
2690 
PolygonOffset(napi_env env,napi_callback_info info)2691 napi_value WebGLRenderingContextBase::PolygonOffset(napi_env env, napi_callback_info info)
2692 {
2693     NFuncArg funcArg(env, info);
2694 
2695     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2696         return nullptr;
2697     }
2698     bool succ = false;
2699     LOGI("WebGL polygonOffset start");
2700     double factor;
2701     tie(succ, factor) = NVal(env, funcArg[NARG_POS::FIRST]).ToDouble();
2702     if (!succ) {
2703         return nullptr;
2704     }
2705     LOGI("WebGL WebGLRenderContext::polygonOffset factor = %{public}f", factor);
2706     double units;
2707     tie(succ, units) = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();
2708     if (!succ) {
2709         return nullptr;
2710     }
2711     LOGI("WebGL WebGLRenderContext::polygonOffset units = %{public}f", units);
2712     glPolygonOffset(static_cast<GLfloat>((float) factor), static_cast<GLfloat>((float) units));
2713     LOGI("WebGL polygonOffset end");
2714     return nullptr;
2715 }
2716 
FrontFace(napi_env env,napi_callback_info info)2717 napi_value WebGLRenderingContextBase::FrontFace(napi_env env, napi_callback_info info)
2718 {
2719     NFuncArg funcArg(env, info);
2720     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2721         return nullptr;
2722     }
2723     bool succ = false;
2724     LOGI("WebGL frontFace start");
2725     int64_t mode;
2726     tie(succ, mode) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
2727     if (!succ) {
2728         return nullptr;
2729     }
2730     LOGI("WebGL WebGLRenderContext::frontFace mode = %{public}u", mode);
2731     glFrontFace(static_cast<GLenum>(mode));
2732     LOGI("WebGL frontFace end");
2733     return nullptr;
2734 }
2735 
GenerateMipmap(napi_env env,napi_callback_info info)2736 napi_value WebGLRenderingContextBase::GenerateMipmap(napi_env env, napi_callback_info info)
2737 {
2738     NFuncArg funcArg(env, info);
2739     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
2740         return nullptr;
2741     }
2742     bool succ = false;
2743     LOGI("WebGL generateMipmap start");
2744     int64_t target;
2745     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
2746     if (!succ) {
2747         return nullptr;
2748     }
2749     LOGI("WebGL WebGLRenderContext::generateMipmap target = %{public}u", target);
2750     glGenerateMipmap(static_cast<GLenum>(target));
2751     LOGI("WebGL generateMipmap end");
2752     return nullptr;
2753 }
2754 
GetActiveAttrib(napi_env env,napi_callback_info info)2755 napi_value WebGLRenderingContextBase::GetActiveAttrib(napi_env env, napi_callback_info info)
2756 {
2757     NFuncArg funcArg(env, info);
2758     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2759         return nullptr;
2760     }
2761     bool succ = false;
2762     LOGI("WebGL getActiveAttrib start");
2763     WebGLProgram *webGlProgram = nullptr;
2764     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
2765     if (programStatus != napi_ok) {
2766         return nullptr;
2767     }
2768     int programId = webGlProgram->GetProgramId();
2769     LOGI("WebGL WebGLRenderContext::getActiveAttrib programId = %{public}u", programId);
2770     int64_t index;
2771     tie(succ, index) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
2772     if (!succ) {
2773         return nullptr;
2774     }
2775     LOGI("WebGL WebGLRenderContext::getActiveAttrib index = %{public}u", index);
2776 
2777     napi_value objActiveInfo = NClass::InstantiateClass(env, WebGLActiveInfo::className, {});
2778     if (!objActiveInfo) {
2779         return nullptr;
2780     }
2781     auto webGLActiveInfo = NClass::GetEntityOf<WebGLActiveInfo>(env, objActiveInfo);
2782     if (!webGLActiveInfo) {
2783         return nullptr;
2784     }
2785     int64_t bufSize = 0;
2786     GLsizei length;
2787     GLint size;
2788     GLenum type;
2789     char name;
2790     glGetActiveAttrib(static_cast<GLuint>(programId), static_cast<GLuint>(index),
2791         static_cast<GLsizei>(bufSize), &length, &size, &type, &name);
2792     webGLActiveInfo->SetActiveName(name);
2793     webGLActiveInfo->SetActiveType(type);
2794     webGLActiveInfo->SetActiveSize(size);
2795     LOGI("WebGL getActiveAttrib end");
2796     return objActiveInfo;
2797 }
2798 
GetActiveUniform(napi_env env,napi_callback_info info)2799 napi_value WebGLRenderingContextBase::GetActiveUniform(napi_env env, napi_callback_info info)
2800 {
2801     NFuncArg funcArg(env, info);
2802     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2803         return nullptr;
2804     }
2805     bool succ = false;
2806     LOGI("WebGL getActiveUniform start");
2807     WebGLProgram *webGlProgram = nullptr;
2808     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
2809     if (programStatus != napi_ok) {
2810         return nullptr;
2811     }
2812     int programId = webGlProgram->GetProgramId();
2813     LOGI("WebGL WebGLRenderContext::getActiveUniform programId = %{public}u", programId);
2814     int64_t index;
2815     tie(succ, index) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
2816     if (!succ) {
2817         return nullptr;
2818     }
2819     LOGI("WebGL WebGLRenderContext::getActiveUniform index = %{public}u", index);
2820 
2821     napi_value objActiveInfo = NClass::InstantiateClass(env, WebGLActiveInfo::className, {});
2822     if (!objActiveInfo) {
2823         return nullptr;
2824     }
2825     auto webGLActiveInfo = NClass::GetEntityOf<WebGLActiveInfo>(env, objActiveInfo);
2826     if (!webGLActiveInfo) {
2827         return nullptr;
2828     }
2829     int64_t bufSize = 0;
2830     GLsizei length;
2831     GLint size;
2832     GLenum type;
2833     char name;
2834     glGetActiveUniform(static_cast<GLuint>(programId), static_cast<GLuint>(index),
2835         static_cast<GLsizei>(bufSize), &length, &size, &type, &name);
2836     webGLActiveInfo->SetActiveName(name);
2837     webGLActiveInfo->SetActiveType(type);
2838     webGLActiveInfo->SetActiveSize(size);
2839     LOGI("WebGL getActiveUniform end");
2840     return objActiveInfo;
2841 }
2842 
GetBufferParameter(napi_env env,napi_callback_info info)2843 napi_value WebGLRenderingContextBase::GetBufferParameter(napi_env env, napi_callback_info info)
2844 {
2845     NFuncArg funcArg(env, info);
2846     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2847         return nullptr;
2848     }
2849     bool succ = false;
2850     LOGI("WebGL getBufferParameter start");
2851     int64_t target;
2852     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
2853     if (!succ) {
2854         return nullptr;
2855     }
2856     LOGI("WebGL WebGLRenderContext::getBufferParameter target = %{public}u", target);
2857     int64_t pname;
2858     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
2859     if (!succ) {
2860         return nullptr;
2861     }
2862     LOGI("WebGL WebGLRenderContext::getBufferParameter pname = %{public}u", pname);
2863     if (pname == GL_BUFFER_SIZE || pname == GL_BUFFER_USAGE) {
2864         GLint params;
2865         glGetBufferParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
2866         int64_t res = static_cast<int64_t>(params);
2867         LOGI("WebGL getBufferParameter end");
2868         return NVal::CreateInt64(env, res).val_;
2869     } else {
2870         LOGI("WebGL getBufferParameter : pname is wrong");
2871         return nullptr;
2872     }
2873 }
2874 
GetError(napi_env env,napi_callback_info info)2875 napi_value WebGLRenderingContextBase::GetError(napi_env env, napi_callback_info info)
2876 {
2877     NFuncArg funcArg(env, info);
2878     LOGI("WebGL getError start");
2879     int64_t res = static_cast<int64_t>(glGetError());
2880     LOGI("WebGL WebGLRenderContext::getError res = %{public}u", res);
2881     LOGI("WebGL getError end");
2882     return NVal::CreateInt64(env, res).val_;
2883 }
2884 
GetFramebufferAttachmentParameter(napi_env env,napi_callback_info info)2885 napi_value WebGLRenderingContextBase::GetFramebufferAttachmentParameter(napi_env env, napi_callback_info info)
2886 {
2887     NFuncArg funcArg(env, info);
2888     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
2889         return nullptr;
2890     }
2891     bool succ = false;
2892     LOGI("WebGL getFramebufferAttachmentParameter start");
2893     int64_t target;
2894     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
2895     if (!succ) {
2896         return nullptr;
2897     }
2898     LOGI("WebGL WebGLRenderingContextBase::getFramebufferAttachmentParameter target = %{public}u", target);
2899     int64_t attachment;
2900     tie(succ, attachment) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
2901     if (!succ) {
2902         return nullptr;
2903     }
2904     LOGI("WebGL WebGLRenderingContextBase::getFramebufferAttachmentParameter attachment = %{public}u", attachment);
2905     int64_t pname;
2906     tie(succ, pname) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
2907     if (!succ) {
2908         return nullptr;
2909     }
2910     LOGI("WebGL WebGLRenderingContextBase::getFramebufferAttachmentParameter pname = %{public}u", pname);
2911     if (pname == FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE ||
2912         pname == FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL ||
2913         pname == FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE ||
2914         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE ||
2915         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_BLUE_SIZE ||
2916         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING ||
2917         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE ||
2918         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE ||
2919         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_GREEN_SIZE ||
2920         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_RED_SIZE ||
2921         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE ||
2922         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER) {
2923         GLint params;
2924         glGetFramebufferAttachmentParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2925                                               static_cast<GLenum>(pname), &params);
2926         int64_t res = static_cast<int64_t>(params);
2927         LOGI("WebGL getFramebufferAttachmentParameter end");
2928         return NVal::CreateInt64(env, res).val_;
2929     } else if (pname == FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
2930         GLint params;
2931         napi_value res = nullptr;
2932         napi_get_cb_info(env, info, nullptr, nullptr, &res, nullptr);
2933         glGetFramebufferAttachmentParameteriv (static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2934                                                FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &params);
2935         int64_t type = static_cast<int64_t>(params);
2936         if (type == RENDERBUFFER) {
2937             LOGI("WebGL getFramebufferAttachmentParameter WebGLRenderbuffer start");
2938             napi_value objFramebuffer = NClass::InstantiateClass(env, WebGLFramebuffer::className, {});
2939             if (!objFramebuffer) {
2940                 return nullptr;
2941             }
2942             auto webGlFramebuffer = NClass::GetEntityOf<WebGLFramebuffer>(env, objFramebuffer);
2943             if (!webGlFramebuffer) {
2944                 return nullptr;
2945             }
2946             GLint framebufferId;
2947             glGetFramebufferAttachmentParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2948                                                   static_cast<GLenum>(pname), &framebufferId);
2949             webGlFramebuffer->SetFramebuffer(static_cast<unsigned int>(framebufferId));
2950             LOGI("WebGL getFramebufferAttachmentParameter framebufferId = %{public}u", framebufferId);
2951             LOGI("WebGL getFramebufferAttachmentParameter WebGLRenderbuffer end");
2952             return objFramebuffer;
2953         } else if (type == TEXTURE) {
2954             LOGI("WebGL getFramebufferAttachmentParameter WebGLTexture start");
2955             napi_value objTexture = NClass::InstantiateClass(env, WebGLTexture::className, {});
2956             if (!objTexture) {
2957                 return nullptr;
2958             }
2959             auto webGlTexture = NClass::GetEntityOf<WebGLTexture>(env, objTexture);
2960             if (!webGlTexture) {
2961                 return nullptr;
2962             }
2963             GLint textureId;
2964             glGetFramebufferAttachmentParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2965                                                   static_cast<GLenum>(pname), &textureId);
2966             webGlTexture->SetTexture(static_cast<unsigned int>(textureId));
2967             LOGI("WebGL getFramebufferAttachmentParameter textureId = %{public}u", textureId);
2968             LOGI("WebGL getFramebufferAttachmentParameter WebGLTexture end");
2969             return objTexture;
2970         } else {
2971             LOGI("WebGL getFramebufferAttachmentParameter : no image is attached");
2972             return 0;
2973         }
2974     } else {
2975         LOGI("WebGL getFramebufferAttachmentParameter : pname is wrong");
2976         return nullptr;
2977     }
2978 }
2979 
GetProgramParameter(napi_env env,napi_callback_info info)2980 napi_value WebGLRenderingContextBase::GetProgramParameter(napi_env env, napi_callback_info info)
2981 {
2982     NFuncArg funcArg(env, info);
2983     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2984         return nullptr;
2985     }
2986     bool succ = false;
2987     LOGI("WebGL getProgramParameter start");
2988     if (funcArg[NARG_POS::FIRST] == nullptr) {
2989         return nullptr;
2990     }
2991     WebGLProgram *webGlProgram = nullptr;
2992     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
2993     if (programStatus != napi_ok) {
2994         return nullptr;
2995     }
2996     int programId = webGlProgram->GetProgramId();
2997     LOGI("WebGL WebGLRenderContext::getProgramParameter programId = %{public}u", programId);
2998     int64_t pname;
2999     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3000     if (!succ) {
3001         return nullptr;
3002     }
3003     LOGI("WebGL WebGLRenderContext::getProgramParameter pname = %{public}u", pname);
3004     GLint params;
3005     glGetProgramiv(static_cast<GLuint>(programId), static_cast<GLenum>(pname), &params);
3006     if (pname == GL_DELETE_STATUS || pname == GL_LINK_STATUS || pname == GL_VALIDATE_STATUS) {
3007         bool res = (params == GL_FALSE) ? false : true;
3008         LOGI("WebGL WebGLRenderContext::getProgramParameter params = %{public}u", params);
3009         LOGI("WebGL getProgramParameter end");
3010         return NVal::CreateBool(env, res).val_;
3011     } else if (pname == GL_ATTACHED_SHADERS || pname == GL_ACTIVE_ATTRIBUTES || pname == GL_ACTIVE_UNIFORMS) {
3012         int64_t res = static_cast<int64_t>(params);
3013         LOGI("WebGL getProgramParameter end");
3014         return NVal::CreateInt64(env, res).val_;
3015     } else {
3016         LOGI("WebGL getProgramParameter : pname is wrong");
3017         return nullptr;
3018     }
3019 }
3020 
GetRenderbufferParameter(napi_env env,napi_callback_info info)3021 napi_value WebGLRenderingContextBase::GetRenderbufferParameter(napi_env env, napi_callback_info info)
3022 {
3023     NFuncArg funcArg(env, info);
3024     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3025         return nullptr;
3026     }
3027     bool succ = false;
3028     LOGI("WebGL getRenderbufferParameter start");
3029     int64_t target;
3030     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3031     if (!succ) {
3032         return nullptr;
3033     }
3034     LOGI("WebGL WebGLRenderContext::getRenderbufferParameter target = %{public}u", target);
3035     int64_t pname;
3036     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3037     if (!succ) {
3038         return nullptr;
3039     }
3040     LOGI("WebGL WebGLRenderContext::getRenderbufferParameter pname = %{public}u", pname);
3041     if (pname == GL_RENDERBUFFER_WIDTH ||
3042         pname == GL_RENDERBUFFER_HEIGHT ||
3043         pname == GL_RENDERBUFFER_INTERNAL_FORMAT ||
3044         pname == GL_RENDERBUFFER_RED_SIZE ||
3045         pname == GL_RENDERBUFFER_GREEN_SIZE ||
3046         pname == GL_RENDERBUFFER_BLUE_SIZE ||
3047         pname == GL_RENDERBUFFER_ALPHA_SIZE ||
3048         pname == GL_RENDERBUFFER_DEPTH_SIZE ||
3049         pname == GL_RENDERBUFFER_STENCIL_SIZE) {
3050         GLint params;
3051         glGetRenderbufferParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
3052         int64_t res = static_cast<int64_t>(params);
3053         LOGI("WebGL getRenderbufferParameter end");
3054         return NVal::CreateInt64(env, res).val_;
3055     } else {
3056         LOGI("WebGL getRenderbufferParameter : pname is wrong");
3057         return nullptr;
3058     }
3059 }
3060 
GetTexParameter(napi_env env,napi_callback_info info)3061 napi_value WebGLRenderingContextBase::GetTexParameter(napi_env env, napi_callback_info info)
3062 {
3063     NFuncArg funcArg(env, info);
3064     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3065         return nullptr;
3066     }
3067     bool succ = false;
3068     LOGI("WebGL getTexParameter start");
3069     int64_t target;
3070     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3071     if (!succ) {
3072         return nullptr;
3073     }
3074     LOGI("WebGL WebGLRenderContext::getTexParameter target = %{public}u", target);
3075     int64_t pname;
3076     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3077     if (!succ) {
3078         return nullptr;
3079     }
3080     LOGI("WebGL WebGLRenderContext::getTexParameter pname = %{public}u", pname);
3081     if (pname == GL_TEXTURE_MAX_ANISOTROPY_EXT) {
3082         GLfloat params;
3083         glGetTexParameterfv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
3084         float res = static_cast<float>(params);
3085         LOGI("WebGL getTexParameter end");
3086         return NVal::CreateDouble(env, (double)res).val_;
3087     } else if (pname == GL_TEXTURE_MAG_FILTER || pname == GL_TEXTURE_MIN_FILTER ||
3088                pname == GL_TEXTURE_WRAP_S || pname == GL_TEXTURE_WRAP_T) {
3089         GLint params;
3090         glGetTexParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
3091         int64_t res = static_cast<int64_t>(params);
3092         LOGI("WebGL getTexParameter end");
3093         return NVal::CreateInt64(env, res).val_;
3094     } else {
3095         LOGI("WebGL getTexParameter : pname is wrong");
3096         return nullptr;
3097     }
3098 }
3099 
GetShaderParameter(napi_env env,napi_callback_info info)3100 napi_value WebGLRenderingContextBase::GetShaderParameter(napi_env env, napi_callback_info info)
3101 {
3102     NFuncArg funcArg(env, info);
3103     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3104         return nullptr;
3105     }
3106     bool succ = false;
3107     LOGI("WebGL getShaderParameter start");
3108     if (funcArg[NARG_POS::FIRST] == nullptr) {
3109         return nullptr;
3110     }
3111     WebGLShader *webGlShader = nullptr;
3112     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlShader);
3113     if (shaderStatus != napi_ok) {
3114         return nullptr;
3115     }
3116     int shaderId = webGlShader->GetShaderId();
3117     LOGI("WebGL WebGLRenderContext::getShaderParameter shaderId = %{public}u", shaderId);
3118     int64_t pname;
3119     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3120     if (!succ) {
3121         return nullptr;
3122     }
3123     LOGI("WebGL WebGLRenderContext::getShaderParameter pname = %{public}u", pname);
3124     GLint params;
3125     glGetShaderiv(static_cast<GLuint>(shaderId), static_cast<GLenum>(pname), &params);
3126     if (pname == GL_SHADER_TYPE) {
3127         int64_t res = static_cast<int64_t>(params);
3128         LOGI("WebGL getShaderParameter end");
3129         return NVal::CreateInt64(env, res).val_;
3130     } else if (pname == GL_DELETE_STATUS || pname == GL_COMPILE_STATUS) {
3131         bool res = (params == GL_FALSE) ? false : true;
3132         LOGI("WebGL WebGLRenderContext::getShaderParameter params = %{public}u", params);
3133         LOGI("WebGL getShaderParameter end");
3134         return NVal::CreateBool(env, res).val_;
3135     } else {
3136         LOGI("WebGL getShaderParameter : pname is wrong");
3137         return nullptr;
3138     }
3139 }
3140 
GetAttribLocation(napi_env env,napi_callback_info info)3141 napi_value WebGLRenderingContextBase::GetAttribLocation(napi_env env, napi_callback_info info)
3142 {
3143     NFuncArg funcArg(env, info);
3144     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3145         return nullptr;
3146     }
3147     bool succ = false;
3148     LOGI("WebGL getAttribLocation start");
3149     if (funcArg[NARG_POS::FIRST] == nullptr) {
3150         return nullptr;
3151     }
3152     WebGLProgram *webGlProgram = nullptr;
3153     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
3154     if (programStatus != napi_ok) {
3155         return nullptr;
3156     }
3157     int programId = webGlProgram->GetProgramId();
3158     LOGI("WebGL WebGLRenderContext::getAttribLocation programId = %{public}u", programId);
3159     unique_ptr<char[]> name;
3160     tie(succ, name, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
3161     if (!succ) {
3162         return nullptr;
3163     }
3164     LOGI("WebGL WebGLRenderContext::getAttribLocation name = %{public}s", name.get());
3165     GLint returnValue = glGetAttribLocation(static_cast<GLuint>(programId), const_cast<char *>(name.get()));
3166     int64_t res = static_cast<int64_t>(returnValue);
3167     LOGI("WebGL getAttribLocation end");
3168     return NVal::CreateInt64(env, res).val_;
3169 }
3170 
VertexAttrib1fv(napi_env env,napi_callback_info info)3171 napi_value WebGLRenderingContextBase::VertexAttrib1fv(napi_env env, napi_callback_info info)
3172 {
3173     NFuncArg funcArg(env, info);
3174 
3175     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3176         return nullptr;
3177     }
3178     bool succ = false;
3179     LOGI("WebGL vertexAttrib1fv start");
3180     int32_t index;
3181     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3182     if (!succ) {
3183         return nullptr;
3184     }
3185     LOGI("WebGL WebGLRenderContext::vertexAttrib1fv index = %{public}u", index);
3186     napi_value array = funcArg[NARG_POS::SECOND];
3187     bool isArray = false;
3188     tie(succ, isArray) = NVal(env, array).IsArray();
3189     if (isArray) {     // []
3190         LOGI("WebGL vertexAttrib1fv is Array");
3191         uint32_t length;
3192         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3193         if (lengthStatus != napi_ok) {
3194             return nullptr;
3195         }
3196         float vertexAttrib1fv[length];
3197         uint32_t i;
3198         for (i = 0; i < length; i++) {
3199             napi_value element;
3200             napi_status eleStatus = napi_get_element(env, array, i, &element);
3201             if (eleStatus != napi_ok) {
3202                 return nullptr;
3203             }
3204             double ele;
3205             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3206             if (doubleStatus != napi_ok) {
3207                 return nullptr;
3208             }
3209             vertexAttrib1fv[i] = (float) ele;
3210         }
3211         glVertexAttrib1fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib1fv));
3212         LOGI("WebGL2 vertexAttrib1fv array end");
3213         return nullptr;
3214     }
3215     bool isTypedarray = false;
3216     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3217     if (!isTypedarray || !succ) {
3218         return nullptr;
3219     }
3220     void *data = nullptr;
3221     size_t length;
3222     napi_typedarray_type type;
3223     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3224     if (!succ) {
3225         return nullptr;
3226     }
3227     if (type == napi_float32_array) {
3228         float *inputFloat = (float *) ((uint8_t*)(data));
3229         glVertexAttrib1fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3230     }
3231     LOGI("WebGL2 vertexAttrib1fv typeArray end");
3232     return nullptr;
3233 }
3234 
VertexAttrib2fv(napi_env env,napi_callback_info info)3235 napi_value WebGLRenderingContextBase::VertexAttrib2fv(napi_env env, napi_callback_info info)
3236 {
3237     NFuncArg funcArg(env, info);
3238 
3239     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3240         return nullptr;
3241     }
3242     bool succ = false;
3243     LOGI("WebGL vertexAttrib2fv start");
3244     int32_t index;
3245     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3246     if (!succ) {
3247         return nullptr;
3248     }
3249     LOGI("WebGL WebGLRenderContext::vertexAttrib2fv index = %{public}u", index);
3250     napi_value array = funcArg[NARG_POS::SECOND];
3251     bool isArray = false;
3252     tie(succ, isArray) = NVal(env, array).IsArray();
3253     if (isArray) {     // []
3254         LOGI("WebGL vertexAttrib2fv is Array");
3255         uint32_t length;
3256         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3257         if (lengthStatus != napi_ok) {
3258             return nullptr;
3259         }
3260         float vertexAttrib2fv[length];
3261         uint32_t i;
3262         for (i = 0; i < length; i++) {
3263             napi_value element;
3264             napi_status eleStatus = napi_get_element(env, array, i, &element);
3265             if (eleStatus != napi_ok) {
3266                 return nullptr;
3267             }
3268             double ele;
3269             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3270             if (doubleStatus != napi_ok) {
3271                 return nullptr;
3272             }
3273             vertexAttrib2fv[i] = (float) ele;
3274         }
3275         glVertexAttrib2fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib2fv));
3276         LOGI("WebGL2 vertexAttrib2fv array end");
3277         return nullptr;
3278     }
3279     bool isTypedarray = false;
3280     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3281     if (!isTypedarray || !succ) {
3282         return nullptr;
3283     }
3284     void *data = nullptr;
3285     size_t length;
3286     napi_typedarray_type type;
3287     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3288     if (!succ) {
3289         return nullptr;
3290     }
3291     if (type == napi_float32_array) {
3292         float *inputFloat = (float *) ((uint8_t*)(data));
3293         glVertexAttrib2fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3294     }
3295     LOGI("WebGL2 vertexAttrib2fv typeArray end");
3296     return nullptr;
3297 }
3298 
VertexAttrib3fv(napi_env env,napi_callback_info info)3299 napi_value WebGLRenderingContextBase::VertexAttrib3fv(napi_env env, napi_callback_info info)
3300 {
3301     NFuncArg funcArg(env, info);
3302 
3303     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3304         return nullptr;
3305     }
3306     bool succ = false;
3307     LOGI("WebGL vertexAttrib3fv start");
3308     int32_t index;
3309     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3310     if (!succ) {
3311         return nullptr;
3312     }
3313     LOGI("WebGL WebGLRenderContext::vertexAttrib3fv index = %{public}u", index);
3314     napi_value array = funcArg[NARG_POS::SECOND];
3315     bool isArray = false;
3316     tie(succ, isArray) = NVal(env, array).IsArray();
3317     if (isArray) {     // []
3318         LOGI("WebGL vertexAttrib3fv is Array");
3319         uint32_t length;
3320         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3321         if (lengthStatus != napi_ok) {
3322             return nullptr;
3323         }
3324         float vertexAttrib3fv[length];
3325         uint32_t i;
3326         for (i = 0; i < length; i++) {
3327             napi_value element;
3328             napi_status eleStatus = napi_get_element(env, array, i, &element);
3329             if (eleStatus != napi_ok) {
3330                 return nullptr;
3331             }
3332             double ele;
3333             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3334             if (doubleStatus != napi_ok) {
3335                 return nullptr;
3336             }
3337             vertexAttrib3fv[i] = (float) ele;
3338         }
3339         glVertexAttrib3fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib3fv));
3340         LOGI("WebGL2 vertexAttrib3fv array end");
3341         return nullptr;
3342     }
3343     bool isTypedarray = false;
3344     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3345     if (!isTypedarray || !succ) {
3346         return nullptr;
3347     }
3348     void *data = nullptr;
3349     size_t length;
3350     napi_typedarray_type type;
3351     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3352     if (!succ) {
3353         return nullptr;
3354     }
3355     if (type == napi_float32_array) {
3356         float *inputFloat = (float *) ((uint8_t*)(data));
3357         glVertexAttrib3fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3358     }
3359     LOGI("WebGL2 vertexAttrib3fv typeArray end");
3360     return nullptr;
3361 }
3362 
VertexAttrib4fv(napi_env env,napi_callback_info info)3363 napi_value WebGLRenderingContextBase::VertexAttrib4fv(napi_env env, napi_callback_info info)
3364 {
3365     NFuncArg funcArg(env, info);
3366 
3367     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3368         return nullptr;
3369     }
3370     bool succ = false;
3371     LOGI("WebGL vertexAttrib4fv start");
3372     int32_t index;
3373     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3374     if (!succ) {
3375         return nullptr;
3376     }
3377     LOGI("WebGL WebGLRenderContext::vertexAttrib4fv index = %{public}u", index);
3378     napi_value array = funcArg[NARG_POS::SECOND];
3379     bool isArray = false;
3380     tie(succ, isArray) = NVal(env, array).IsArray();
3381     if (isArray) {     // []
3382         LOGI("WebGL vertexAttrib4fv is Array");
3383         uint32_t length;
3384         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3385         if (lengthStatus != napi_ok) {
3386             return nullptr;
3387         }
3388         float vertexAttrib4fv[length];
3389         uint32_t i;
3390         for (i = 0; i < length; i++) {
3391             napi_value element;
3392             napi_status eleStatus = napi_get_element(env, array, i, &element);
3393             if (eleStatus != napi_ok) {
3394                 return nullptr;
3395             }
3396             double ele;
3397             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3398             if (doubleStatus != napi_ok) {
3399                 return nullptr;
3400             }
3401             vertexAttrib4fv[i] = (float) ele;
3402         }
3403         glVertexAttrib4fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib4fv));
3404         LOGI("WebGL2 vertexAttrib4fv array end");
3405         return nullptr;
3406     }
3407     bool isTypedarray = false;
3408     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3409     if (!isTypedarray || !succ) {
3410         return nullptr;
3411     }
3412     void *data = nullptr;
3413     size_t length;
3414     napi_typedarray_type type;
3415     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3416     if (!succ) {
3417         return nullptr;
3418     }
3419     if (type == napi_float32_array) {
3420         float *inputFloat = (float *) ((uint8_t*)(data));
3421         glVertexAttrib4fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3422     }
3423     LOGI("WebGL2 vertexAttrib4fv typeArray end");
3424     return nullptr;
3425 }
3426 
GetVertexAttrib(napi_env env,napi_callback_info info)3427 napi_value WebGLRenderingContextBase::GetVertexAttrib(napi_env env, napi_callback_info info)
3428 {
3429     NFuncArg funcArg(env, info);
3430     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3431         return nullptr;
3432     }
3433     bool succ = false;
3434     LOGI("WebGL getVertexAttrib start");
3435     int32_t index;
3436     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3437     if (!succ) {
3438         return nullptr;
3439     }
3440     LOGI("WebGL WebGLRenderContext::getVertexAttrib index = %{public}u", index);
3441     int32_t pname;
3442     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
3443     if (!succ) {
3444         return nullptr;
3445     }
3446     LOGI("WebGL WebGLRenderContext::getVertexAttrib pname = %{public}u", pname);
3447     if (pname == GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING) {  // webglBuffer
3448         int32_t params;
3449         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3450         GLfloat par = static_cast<float>(params);
3451         napi_value objBuffer = NClass::InstantiateClass(env, WebGLBuffer::className, {});
3452         if (!objBuffer) {
3453             return nullptr;
3454         }
3455         auto webGlBuffer = NClass::GetEntityOf<WebGLBuffer>(env, objBuffer);
3456         if (!webGlBuffer) {
3457             return nullptr;
3458         }
3459         webGlBuffer->SetParams(par);
3460         LOGI("WebGL WebGLRenderingContextBase::createBuffer par = %{public}u", par);
3461         LOGI("WebGL getVertexAttrib panem is array end");
3462         return objBuffer;
3463     } else if (pname == GL_VERTEX_ATTRIB_ARRAY_ENABLED || pname == GL_VERTEX_ATTRIB_ARRAY_NORMALIZED ||
3464         pname == GL_VERTEX_ATTRIB_ARRAY_INTEGER) {
3465         int32_t params;
3466         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3467         bool res = static_cast<bool>(params);
3468         LOGI("WebGL getVertexAttrib pname is GLBoolean end");
3469         return NVal::CreateBool(env, res).val_;
3470     } else if (pname == GL_VERTEX_ATTRIB_ARRAY_SIZE || pname == GL_VERTEX_ATTRIB_ARRAY_STRIDE ||
3471         pname == GL_VERTEX_ATTRIB_ARRAY_DIVISOR || pname == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE) {
3472         int32_t params;
3473         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3474         int res = static_cast<int>(params);
3475         LOGI("WebGL getVertexAttrib pname is GLint end");
3476         return NVal::CreateInt64(env, res).val_;
3477     } else if (pname == GL_VERTEX_ATTRIB_ARRAY_TYPE) {  // GLenum
3478         int32_t params;
3479         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3480         int64_t res = static_cast<int>(params);
3481         LOGI("WebGL getVertexAttrib pname is Glenum end");
3482         return NVal::CreateInt64(env, res).val_;
3483     } else if (pname == GL_CURRENT_VERTEX_ATTRIB) {   // float32Array
3484         static float res[4] = {};
3485         glGetVertexAttribfv(static_cast<GLuint>(index), static_cast<GLenum>(pname), reinterpret_cast<GLfloat*>(res));
3486         napi_value outputBuffer = nullptr;
3487         napi_create_external_arraybuffer(env, res, sizeof(res),
3488                                          [](napi_env env, void *finalize_data, void *finalize_hint) {},
3489                                          NULL, &outputBuffer);
3490         napi_value output_array = nullptr;
3491         napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float), outputBuffer, 0, &output_array);
3492         LOGI("WebGL getVertexAttrib pname is float32Array end");
3493         return output_array;
3494     } else {
3495         return nullptr;
3496     }
3497 }
3498 
GetParameter(napi_env env,napi_callback_info info)3499 napi_value WebGLRenderingContextBase::GetParameter(napi_env env, napi_callback_info info)
3500 {
3501     NFuncArg funcArg(env, info);
3502     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3503         return nullptr;
3504     }
3505     bool succ = false;
3506     LOGI("WebGL getParameter start");
3507     int64_t pname;
3508     tie(succ, pname) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3509     if (!succ) {
3510         return nullptr;
3511     }
3512     LOGI("WebGL WebGLRenderContext::getParameter pname = %{public}u", pname);
3513     if (pname == GL_DEPTH_CLEAR_VALUE || pname == GL_LINE_WIDTH ||
3514         pname == GL_POLYGON_OFFSET_FACTOR || pname == GL_POLYGON_OFFSET_UNITS ||
3515         pname == GL_SAMPLE_COVERAGE_VALUE) {
3516         GLfloat params;
3517         glGetFloatv(static_cast<GLenum>(pname), &params);
3518         float res = static_cast<float>(params);
3519         LOGI("WebGL getParameter end");
3520         return NVal::CreateDouble(env, (double)res).val_;
3521     } else if (pname == GL_TEXTURE_BINDING_2D || pname == GL_TEXTURE_BINDING_CUBE_MAP) {
3522         GLint params;
3523         glGetIntegerv(static_cast<GLenum>(pname), &params);
3524         napi_value objTexture = NClass::InstantiateClass(env, WebGLTexture::className, {});
3525         if (!objTexture) {
3526             return nullptr;
3527         }
3528         auto webGlTexture = NClass::GetEntityOf<WebGLTexture>(env, objTexture);
3529         if (!webGlTexture) {
3530             return nullptr;
3531         }
3532         webGlTexture->SetTexture(params);
3533         LOGI("WebGL getParameter end");
3534         return objTexture;
3535     } else if (pname == GL_RENDERER || pname == GL_SHADING_LANGUAGE_VERSION ||
3536                pname == GL_VENDOR || pname == GL_VERSION) {
3537         LOGI("WebGL pname : string");
3538         const unsigned char* extensions = glGetString(static_cast<GLenum>(pname));
3539         string str = (char*)extensions;
3540         vector<string> vec;
3541         Util::SplitString(str, vec, " ");
3542         napi_value result = nullptr;
3543         napi_create_array_with_length(env, vec.size(), &result);
3544         for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
3545             napi_set_element(env, result, i, NVal::CreateUTF8String(env, vec[i]).val_);
3546         }
3547         return result;
3548     } else if (pname == GL_ARRAY_BUFFER_BINDING || pname == GL_ELEMENT_ARRAY_BUFFER_BINDING) {
3549         GLint params;
3550         glGetIntegerv(static_cast<GLenum>(pname), &params);
3551         napi_value objBuffer = NClass::InstantiateClass(env, WebGLBuffer::className, {});
3552         if (!objBuffer) {
3553             return nullptr;
3554         }
3555         auto webGlBuffer = NClass::GetEntityOf<WebGLBuffer>(env, objBuffer);
3556         if (!webGlBuffer) {
3557             return nullptr;
3558         }
3559         webGlBuffer->SetBuffer(params);
3560         LOGI("WebGL getParameter end");
3561         return objBuffer;
3562     } else if (pname == GL_FRAMEBUFFER_BINDING) {
3563         GLint params;
3564         glGetIntegerv(static_cast<GLenum>(pname), &params);
3565         napi_value objFramebuffer = NClass::InstantiateClass(env, WebGLFramebuffer::className, {});
3566         if (!objFramebuffer) {
3567             return nullptr;
3568         }
3569         auto webGLFramebuffer = NClass::GetEntityOf<WebGLFramebuffer>(env, objFramebuffer);
3570         if (!webGLFramebuffer) {
3571             return nullptr;
3572         }
3573         webGLFramebuffer->SetFramebuffer(params);
3574         LOGI("WebGL getParameter end");
3575         return objFramebuffer;
3576     } else if (pname == GL_CURRENT_PROGRAM) {
3577         GLint params;
3578         glGetIntegerv(static_cast<GLenum>(pname), &params);
3579         napi_value objProgram = NClass::InstantiateClass(env, WebGLProgram::className, {});
3580         if (!objProgram) {
3581             return nullptr;
3582         }
3583         auto webGlProgram = NClass::GetEntityOf<WebGLProgram>(env, objProgram);
3584         if (!webGlProgram) {
3585             return nullptr;
3586         }
3587         webGlProgram->SetProgramId(params);
3588         LOGI("WebGL getParameter end");
3589         return objProgram;
3590     } else if (pname == GL_RENDERBUFFER_BINDING) {
3591         GLint params;
3592         glGetIntegerv(static_cast<GLenum>(pname), &params);
3593         napi_value objRenderbuffer = NClass::InstantiateClass(env, WebGLRenderbuffer::className, {});
3594         if (!objRenderbuffer) {
3595             return nullptr;
3596         }
3597         auto webGlRenderbuffer = NClass::GetEntityOf<WebGLRenderbuffer>(env, objRenderbuffer);
3598         if (!webGlRenderbuffer) {
3599             return nullptr;
3600         }
3601         webGlRenderbuffer->SetRenderbuffer(params);
3602         LOGI("WebGL getParameter end");
3603         return objRenderbuffer;
3604     } else if (pname == GL_ALIASED_LINE_WIDTH_RANGE || pname == GL_ALIASED_POINT_SIZE_RANGE ||
3605                pname == GL_DEPTH_RANGE) {
3606         LOGI("WebGL pname : Float32Array with 2 elements");
3607         static float res[2] = {};
3608         glGetFloatv(static_cast<GLenum>(pname), reinterpret_cast<GLfloat*>(res));
3609         napi_value outputBuffer = nullptr;
3610         napi_create_external_arraybuffer(env, res, sizeof(res),
3611                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3612                                          NULL, &outputBuffer);
3613         napi_value outputArray = nullptr;
3614         napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
3615                                outputBuffer, 0, &outputArray);
3616         LOGI("WebGL getParameter end");
3617         return outputArray;
3618     } else if (pname == GL_COLOR_CLEAR_VALUE || pname == GL_BLEND_COLOR) {
3619         LOGI("WebGL pname : Float32Array with 4 valus");
3620         static float res[4] = {};
3621         glGetFloatv(static_cast<GLenum>(pname), reinterpret_cast<GLfloat*>(res));
3622         napi_value outputBuffer = nullptr;
3623         napi_create_external_arraybuffer(env, res, sizeof(res),
3624                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3625                                          NULL, &outputBuffer);
3626         napi_value outputArray = nullptr;
3627         napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
3628                                outputBuffer, 0, &outputArray);
3629         LOGI("WebGL getParameter end");
3630         return outputArray;
3631     } else if (pname == GL_COMPRESSED_TEXTURE_FORMATS) {
3632         LOGI("WebGL pname : Uint32Array");
3633         GLint count = 0;
3634         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &count);
3635         GLint params[count];
3636         glGetIntegerv(static_cast<GLenum>(pname), static_cast<GLint*>(params));
3637         static uint32_t *res = new uint32_t[count];
3638         int i;
3639         for (i = 0; i < count; i++) {
3640             res[i] = static_cast<uint32_t>(params[i]);
3641         }
3642         napi_value outputBuffer = nullptr;
3643         napi_create_external_arraybuffer(env, res, sizeof(params),
3644                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3645                                          NULL, &outputBuffer);
3646         napi_value outputArray = nullptr;
3647         napi_create_typedarray(env, napi_uint32_array, sizeof(params) / sizeof(uint32_t),
3648                                outputBuffer, 0, &outputArray);
3649         LOGI("WebGL getParameter end");
3650         return outputArray;
3651     } else if (pname == GL_MAX_VIEWPORT_DIMS) {
3652         LOGI("WebGL pname : int32Array with 2 elements");
3653         static int64_t res[2] = {};
3654         glGetIntegerv(static_cast<GLenum>(pname), reinterpret_cast<GLint*>(res));
3655         napi_value outputBuffer = nullptr;
3656         napi_create_external_arraybuffer(env, res, sizeof(res),
3657                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3658                                          NULL, &outputBuffer);
3659         napi_value outputArray = nullptr;
3660         napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
3661                                outputBuffer, 0, &outputArray);
3662         LOGI("WebGL getParameter end");
3663         return outputArray;
3664     } else if (pname == GL_SCISSOR_BOX) {
3665         LOGI("WebGL pname : int32Array with 2 4 valus");
3666         static int64_t res[4] = {};
3667         glGetIntegerv(static_cast<GLenum>(pname), reinterpret_cast<GLint*>(res));
3668         napi_value outputBuffer = nullptr;
3669         napi_create_external_arraybuffer(env, res, sizeof(res),
3670                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3671                                          NULL, &outputBuffer);
3672         napi_value outputArray = nullptr;
3673         napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
3674                                outputBuffer, 0, &outputArray);
3675         LOGI("WebGL getParameter end");
3676         return outputArray;
3677     } else if (pname == GL_BLEND || pname == GL_CULL_FACE || pname == GL_DEPTH_TEST ||
3678                pname == GL_DEPTH_WRITEMASK || pname == GL_DITHER || pname == GL_POLYGON_OFFSET_FILL ||
3679                pname == GL_SAMPLE_ALPHA_TO_COVERAGE || pname == GL_SAMPLE_COVERAGE ||
3680                pname == GL_SAMPLE_COVERAGE_INVERT || pname == GL_SCISSOR_TEST || pname == GL_STENCIL_TEST ||
3681                pname == WebGLRenderingContextBase::UNPACK_FLIP_Y_WEBGL ||
3682                pname == WebGLRenderingContextBase::UNPACK_PREMULTIPLY_ALPHA_WEBGL) {
3683         GLboolean params;
3684         glGetBooleanv(static_cast<GLenum>(pname), &params);
3685         bool res = static_cast<bool>(params);
3686         LOGI("WebGL getParameter end");
3687         return NVal::CreateBool(env, res).val_;
3688     } else if (pname == GL_COLOR_WRITEMASK) {
3689         LOGI("WebGL bool end");
3690         GLboolean params[4];
3691         glGetBooleanv(static_cast<GLenum>(pname), static_cast<GLboolean*>(params));
3692         napi_value res = nullptr;
3693         napi_create_array(env, &res);
3694         uint32_t i;
3695         uint32_t length = 4;
3696         for (i = 0; i < length; i++) {
3697             bool a = static_cast<bool>(params[i]);
3698             napi_value result = nullptr;
3699             napi_status status = napi_get_boolean(env, a, &result);
3700             if (status != napi_ok) {
3701                 return nullptr;
3702             }
3703             napi_set_element(env, res, i, result);
3704         }
3705         LOGI("WebGL getParameter end");
3706         return res;
3707     } else if (pname == GL_ACTIVE_TEXTURE ||
3708                pname == GL_ALPHA_BITS ||
3709                pname == GL_BLEND_DST_ALPHA ||
3710                pname == GL_BLEND_DST_RGB ||
3711                pname == GL_BLEND_EQUATION_ALPHA ||
3712                pname == GL_BLEND_EQUATION_RGB ||
3713                pname == GL_BLEND_SRC_ALPHA ||
3714                pname == GL_BLEND_SRC_RGB ||
3715                pname == GL_BLUE_BITS ||
3716                pname == GL_CULL_FACE_MODE ||
3717                pname == GL_DEPTH_BITS ||
3718                pname == GL_DEPTH_FUNC ||
3719                pname == GL_FRONT_FACE ||
3720                pname == GL_GENERATE_MIPMAP_HINT ||
3721                pname == GL_GREEN_BITS ||
3722                pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT ||
3723                pname == GL_IMPLEMENTATION_COLOR_READ_TYPE ||
3724                pname == GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ||
3725                pname == GL_MAX_CUBE_MAP_TEXTURE_SIZE ||
3726                pname == GL_MAX_FRAGMENT_UNIFORM_VECTORS ||
3727                pname == GL_MAX_RENDERBUFFER_SIZE ||
3728                pname == GL_MAX_TEXTURE_IMAGE_UNITS ||
3729                pname == GL_MAX_TEXTURE_SIZE ||
3730                pname == GL_MAX_VARYING_VECTORS ||
3731                pname == GL_MAX_VERTEX_ATTRIBS ||
3732                pname == GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS ||
3733                pname == GL_MAX_VERTEX_UNIFORM_VECTORS ||
3734                pname == GL_PACK_ALIGNMENT ||
3735                pname == GL_RED_BITS ||
3736                pname == GL_SAMPLE_BUFFERS ||
3737                pname == GL_SAMPLES ||
3738                pname == GL_STENCIL_BACK_FAIL ||
3739                pname == GL_STENCIL_BACK_FUNC ||
3740                pname == GL_STENCIL_BACK_PASS_DEPTH_FAIL ||
3741                pname == GL_STENCIL_BACK_PASS_DEPTH_PASS ||
3742                pname == GL_STENCIL_BACK_REF ||
3743                pname == GL_STENCIL_BACK_VALUE_MASK ||
3744                pname == GL_STENCIL_BACK_WRITEMASK ||
3745                pname == GL_STENCIL_BITS ||
3746                pname == GL_STENCIL_CLEAR_VALUE ||
3747                pname == GL_STENCIL_FAIL ||
3748                pname == GL_STENCIL_FUNC ||
3749                pname == GL_STENCIL_PASS_DEPTH_FAIL ||
3750                pname == GL_STENCIL_PASS_DEPTH_PASS ||
3751                pname == GL_STENCIL_REF ||
3752                pname == GL_STENCIL_VALUE_MASK ||
3753                pname == GL_STENCIL_WRITEMASK ||
3754                pname == GL_SUBPIXEL_BITS ||
3755                pname == GL_UNPACK_ALIGNMENT ||
3756                pname == UNPACK_COLORSPACE_CONVERSION_WEBGL) {
3757         GLint params;
3758         glGetIntegerv(static_cast<GLenum>(pname), &params);
3759         int64_t res = static_cast<int64_t>(params);
3760         LOGI("WebGL getParameter end");
3761         return NVal::CreateInt64(env, res).val_;
3762     } else {
3763         LOGI("WebGL getParameter : pname is wrong");
3764         return nullptr;
3765     }
3766 }
3767 
GetAttachedShaders(napi_env env,napi_callback_info info)3768 napi_value WebGLRenderingContextBase::GetAttachedShaders(napi_env env, napi_callback_info info)
3769 {
3770     NFuncArg funcArg(env, info);
3771     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3772         return nullptr;
3773     }
3774     LOGI("WebGL getAttachedShaders start");
3775     WebGLProgram *webGlProgram = nullptr;
3776     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
3777     if (programStatus != napi_ok) {
3778         return nullptr;
3779     }
3780     int programId = webGlProgram->GetProgramId();
3781     LOGI("WebGL WebGLRenderContext::getAttachedShaders programId = %{public}u", programId);
3782     int64_t maxCount = 0;
3783     GLsizei count = 1;
3784     GLuint shader[count];
3785     glGetAttachedShaders(static_cast<GLuint>(programId), static_cast<GLsizei>(maxCount), &count,
3786                          static_cast<GLuint*>(shader));
3787     LOGI("WebGL WebGLRenderContext::getAttachedShaders count = %{public}u", count);
3788     GLsizei i;
3789     napi_value objShader = NClass::InstantiateClass(env, WebGLShader::className, {});
3790     if (!objShader) {
3791         return nullptr;
3792     }
3793     auto webGlShader = NClass::GetEntityOf<WebGLShader>(env, objShader);
3794     if (!webGlShader) {
3795         return nullptr;
3796     }
3797     napi_value shaderArr = nullptr;
3798     napi_create_array(env, &shaderArr);
3799     for (i = 0; i < count; i++) {
3800         webGlShader->SetShaderId(shader[i]);
3801         napi_set_element(env, shaderArr, i, objShader);
3802     }
3803     LOGI("WebGL getAttachedShaders end");
3804     return shaderArr;
3805 }
3806 
GetShaderPrecisionFormat(napi_env env,napi_callback_info info)3807 napi_value WebGLRenderingContextBase::GetShaderPrecisionFormat(napi_env env, napi_callback_info info)
3808 {
3809     NFuncArg funcArg(env, info);
3810     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3811         return nullptr;
3812     }
3813     bool succ = false;
3814     LOGI("WebGL getShaderPrecisionFormat start");
3815     int64_t shaderType;
3816     tie(succ, shaderType) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3817     if (!succ) {
3818         return nullptr;
3819     }
3820     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat shaderType = %{public}u", shaderType);
3821     int64_t precisionType;
3822     tie(succ, precisionType) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3823     if (!succ) {
3824         return nullptr;
3825     }
3826     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat precisionType = %{public}u", precisionType);
3827     GLint range[2] { };
3828     GLint precision = 0;
3829     napi_value objShaderPrecisionFormat = NClass::InstantiateClass(env, WebGLShaderPrecisionFormat::className, {});
3830     if (!objShaderPrecisionFormat) {
3831         LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat objShaderPrecisionFormat = %{public}u",
3832              objShaderPrecisionFormat);
3833         return nullptr;
3834     }
3835     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat objShaderPrecisionFormat = %{public}u",
3836          objShaderPrecisionFormat);
3837     auto webGLShaderPrecisionFormat = NClass::GetEntityOf<WebGLShaderPrecisionFormat>(env, objShaderPrecisionFormat);
3838     if (!webGLShaderPrecisionFormat) {
3839         return nullptr;
3840     }
3841     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat webGLShaderPrecisionFormat = %{public}u",
3842          webGLShaderPrecisionFormat);
3843     glGetShaderPrecisionFormat(static_cast<GLenum>(shaderType), static_cast<GLenum>(precisionType), range, &precision);
3844     webGLShaderPrecisionFormat->SetShaderPrecisionFormatRangeMin(range[0]);
3845     webGLShaderPrecisionFormat->SetShaderPrecisionFormatRangeMax(range[1]);
3846     webGLShaderPrecisionFormat->SetShaderPrecisionFormatPrecision(precision);
3847     LOGI("WebGL getShaderPrecisionFormat end");
3848     return objShaderPrecisionFormat;
3849 }
3850 
GetShaderInfoLog(napi_env env,napi_callback_info info)3851 napi_value WebGLRenderingContextBase::GetShaderInfoLog(napi_env env, napi_callback_info info)
3852 {
3853     NFuncArg funcArg(env, info);
3854     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3855         return NVal::CreateNull(env).val_;
3856     }
3857     LOGI("WebGL getShaderInfoLog start");
3858     if (funcArg[NARG_POS::FIRST] == nullptr) {
3859         return NVal::CreateNull(env).val_;
3860     }
3861     WebGLShader *webGlShader = nullptr;
3862     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlShader);
3863     if (shaderStatus != napi_ok) {
3864         return NVal::CreateNull(env).val_;
3865     }
3866     int shaderId = webGlShader->GetShaderId();
3867     LOGI("WebGL WebGLRenderContext::getShaderInfoLog shaderId = %{public}u", shaderId);
3868     GLint length = 0;
3869     glGetShaderiv(static_cast<GLuint>(shaderId), GL_INFO_LOG_LENGTH, &length);
3870     GLsizei size = 0;
3871     std::unique_ptr<char[]> buf = std::make_unique<char[]>(length);
3872     glGetShaderInfoLog(shaderId, length, &size, buf.get());
3873     string str = buf.get();
3874     LOGI("WebGL getShaderInfoLog end");
3875     return NVal::CreateUTF8String(env, str).val_;
3876 }
3877 
GetProgramInfoLog(napi_env env,napi_callback_info info)3878 napi_value WebGLRenderingContextBase::GetProgramInfoLog(napi_env env, napi_callback_info info)
3879 {
3880     NFuncArg funcArg(env, info);
3881     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3882         return nullptr;
3883     }
3884     LOGI("WebGL getProgramInfoLog start");
3885     if (funcArg[NARG_POS::FIRST] == nullptr) {
3886         return nullptr;
3887     }
3888     WebGLProgram *webGlProgram = nullptr;
3889     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
3890     if (programStatus != napi_ok) {
3891         return nullptr;
3892     }
3893     int programId = webGlProgram->GetProgramId();
3894     LOGI("WebGL WebGLRenderContext::getProgramInfoLog programId = %{public}u", programId);
3895     GLint length = 0;
3896     GLsizei size = 0;
3897     glGetProgramiv(static_cast<GLuint>(programId), GL_INFO_LOG_LENGTH, &length);
3898     std::unique_ptr<char[]> buf = std::make_unique<char[]>(length);
3899     LOGI("WebGL WebGLRenderContext::getProgramInfoLog bufSize = %{public}u", length);
3900     if (buf == nullptr) {
3901         return nullptr;
3902     }
3903     glGetProgramInfoLog(programId, length, &size, buf.get());
3904     string str = buf.get();
3905     LOGI("WebGL getProgramInfoLog end");
3906     return NVal::CreateUTF8String(env, str).val_;
3907 }
3908 
GetShaderSource(napi_env env,napi_callback_info info)3909 napi_value WebGLRenderingContextBase::GetShaderSource(napi_env env, napi_callback_info info)
3910 {
3911     NFuncArg funcArg(env, info);
3912     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3913         return nullptr;
3914     }
3915     LOGI("WebGL getShaderSource start");
3916     if (funcArg[NARG_POS::FIRST] == nullptr) {
3917         return nullptr;
3918     }
3919     WebGLShader *webGlShader = nullptr;
3920     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlShader);
3921     if (shaderStatus != napi_ok) {
3922         return nullptr;
3923     }
3924     GLuint shaderId = static_cast<GLuint>(webGlShader->GetShaderId());
3925     LOGI("WebGL WebGLRenderContext::getShaderSource shaderId = %{public}u", shaderId);
3926     auto objects = ObjectSource::GetInstance().GetObjectMap();
3927     auto it = objects.find(shaderId);
3928     if (it == objects.end()) {
3929         return NVal::CreateNull(env).val_;
3930     }
3931     LOGI("WebGL getShaderSource end");
3932     return NVal::CreateUTF8String(env, it->second).val_;
3933 }
3934 
GetUniform(napi_env env,napi_callback_info info)3935 napi_value WebGLRenderingContextBase::GetUniform(napi_env env, napi_callback_info info)
3936 {
3937     NFuncArg funcArg(env, info);
3938 
3939     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3940         return nullptr;
3941     }
3942     LOGI("WebGL getUniform start");
3943     if (funcArg[NARG_POS::FIRST] == nullptr || funcArg[NARG_POS::SECOND] == nullptr) {
3944         return nullptr;
3945     }
3946     WebGLProgram *webGlProgram = nullptr;
3947     napi_value a = funcArg[NARG_POS::FIRST];
3948     LOGI("WebGL WebGLRenderContext::getUniform a = %{public}d", a);
3949     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
3950     if (programStatus != napi_ok) {
3951         return nullptr;
3952     }
3953     int programId = webGlProgram->GetProgramId();
3954     LOGI("WebGL WebGLRenderContext::getUniform programId = %{public}d", programId);
3955 
3956     WebGLUniformLocation *webGLUniformLocation = nullptr;
3957     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGLUniformLocation);
3958     if (locationStatus != napi_ok) {
3959         return nullptr;
3960     }
3961     int locationId = webGLUniformLocation->GetUniformLocationId();
3962     LOGI("WebGL WebGLRenderContext::getUniform locationId = %{public}d", locationId);
3963     GLint maxNameLength = -1;
3964     glGetProgramiv(programId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength);
3965     LOGI("WebGL WebGLRenderContext::getUniform maxNameLength = %{public}d", maxNameLength);
3966     if (maxNameLength <= 0) {
3967         return nullptr;
3968     }
3969     GLint activeUniforms = 0;
3970     glGetProgramiv(programId, GL_ACTIVE_UNIFORMS, &activeUniforms);
3971     LOGI("WebGL WebGLRenderContext::getUniform activeUniforms = %{public}d", activeUniforms);
3972     for (GLint i = 0; i < activeUniforms; i++) {
3973         char name_ptr[maxNameLength];
3974         GLsizei name_length = 0;
3975         GLint size = -1;
3976         GLenum type = 0;
3977         glGetActiveUniform(programId, i, maxNameLength, &name_length, &size, &type,
3978                            reinterpret_cast<GLchar *>(name_ptr));
3979         LOGI("WebGL WebGLRenderContext::getUniform type = %{public}u", type);
3980         if (type == GL_INT || type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
3981             LOGI("WebGL getUniform1 end");
3982             GLint params;
3983             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId), &params);
3984             int64_t res = static_cast<int64_t>(params);
3985             LOGI("WebGL getUniform end");
3986             return NVal::CreateInt64(env, res).val_;
3987         } else if (type == GL_FLOAT) {
3988             LOGI("WebGL getUniform2 end");
3989             GLfloat params;
3990             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId), &params);
3991             float res = static_cast<float>(params);
3992             LOGI("WebGL getUniform end");
3993             return NVal::CreateDouble(env, (double) res).val_;
3994         } else if (type == GL_FLOAT_VEC2) {
3995             LOGI("WebGL getUniform3 end");
3996             static float res[2] = {};
3997             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
3998                            reinterpret_cast<GLfloat *>(res));
3999             napi_value outputBuffer = nullptr;
4000             napi_create_external_arraybuffer(env, res, sizeof(res),
4001                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4002                                              NULL, &outputBuffer);
4003             napi_value outputArray = nullptr;
4004             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4005                                    outputBuffer, 0, &outputArray);
4006             LOGI("WebGL getUniform end");
4007             return outputArray;
4008         } else if (type == GL_FLOAT_VEC3) {
4009             LOGI("WebGL getUniform4 end");
4010             static float res[3] = {};
4011             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4012                            reinterpret_cast<GLfloat *>(res));
4013             napi_value outputBuffer = nullptr;
4014             napi_create_external_arraybuffer(env, res, sizeof(res),
4015                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4016                                              NULL, &outputBuffer);
4017             napi_value outputArray = nullptr;
4018             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4019                                    outputBuffer, 0, &outputArray);
4020             LOGI("WebGL getUniform end");
4021             return outputArray;
4022         } else if (type == GL_FLOAT_VEC4 || locationId == GL_FLOAT_MAT2) {
4023             LOGI("WebGL getUniform5 end");
4024             static float res[4] = {};
4025             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4026                            reinterpret_cast<GLfloat *>(res));
4027             napi_value outputBuffer = nullptr;
4028             napi_create_external_arraybuffer(env, res, sizeof(res),
4029                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4030                                              NULL, &outputBuffer);
4031             napi_value outputArray = nullptr;
4032             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4033                                    outputBuffer, 0, &outputArray);
4034             LOGI("WebGL getUniform end");
4035             return outputArray;
4036         } else if (type == GL_FLOAT_MAT3) {
4037             LOGI("WebGL getUniform6 end");
4038             static float res[9] = {};
4039             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4040                            reinterpret_cast<GLfloat *>(res));
4041             napi_value outputBuffer = nullptr;
4042             napi_create_external_arraybuffer(env, res, sizeof(res),
4043                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4044                                              NULL, &outputBuffer);
4045             napi_value outputArray = nullptr;
4046             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4047                                    outputBuffer, 0, &outputArray);
4048             LOGI("WebGL getUniform end");
4049             return outputArray;
4050         } else if (type == GL_FLOAT_MAT4) {
4051             LOGI("WebGL getUniform7 end");
4052             static float res[16] = {};
4053             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4054                            reinterpret_cast<GLfloat *>(res));
4055             napi_value outputBuffer = nullptr;
4056             napi_create_external_arraybuffer(env, res, sizeof(res),
4057                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4058                                              NULL, &outputBuffer);
4059             napi_value outputArray = nullptr;
4060             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4061                                    outputBuffer, 0, &outputArray);
4062             LOGI("WebGL getUniform end");
4063             return outputArray;
4064         } else if (type == GL_INT_VEC2) {
4065             LOGI("WebGL getUniform8 end");
4066             static int64_t res[2] = {};
4067             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4068                            reinterpret_cast<GLint *>(res));
4069             napi_value outputBuffer = nullptr;
4070             napi_create_external_arraybuffer(env, res, sizeof(res),
4071                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4072                                              NULL, &outputBuffer);
4073             napi_value outputArray = nullptr;
4074             napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
4075                                    outputBuffer, 0, &outputArray);
4076             LOGI("WebGL getUniform end");
4077             return outputArray;
4078         } else if (type == GL_INT_VEC3) {
4079             LOGI("WebGL getUniform9 end");
4080             static int64_t res[3] = {};
4081             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4082                            reinterpret_cast<GLint *>(res));
4083             napi_value outputBuffer = nullptr;
4084             napi_create_external_arraybuffer(env, res, sizeof(res),
4085                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4086                                              NULL, &outputBuffer);
4087             napi_value outputArray = nullptr;
4088             napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
4089                                    outputBuffer, 0, &outputArray);
4090             LOGI("WebGL getUniform end");
4091             return outputArray;
4092         } else if (type == GL_INT_VEC4) {
4093             LOGI("WebGL getUniform10 end");
4094             static int64_t res[4] = {};
4095             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4096                            reinterpret_cast<GLint *>(res));
4097             napi_value outputBuffer = nullptr;
4098             napi_create_external_arraybuffer(env, res, sizeof(res),
4099                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4100                                              NULL, &outputBuffer);
4101             napi_value outputArray = nullptr;
4102             napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
4103                                    outputBuffer, 0, &outputArray);
4104             LOGI("WebGL getUniform end");
4105             return outputArray;
4106         } else if (type == GL_BOOL) {
4107             LOGI("WebGL getUniform11 end");
4108             GLint params;
4109             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId), &params);
4110             bool res = (params == GL_FALSE) ? false : true;
4111             LOGI("WebGL getUniform end");
4112             return NVal::CreateBool(env, res).val_;
4113         } else if (type == GL_BOOL_VEC2) {
4114             LOGI("WebGL getUniform12 end");
4115             GLint params[2] = {0};
4116             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4117                            static_cast<GLint *>(params));
4118             uint32_t i;
4119             uint32_t length = 2;
4120             napi_value res = nullptr;
4121             napi_create_array(env, &res);
4122             for (i = 0; i < length; i++) {
4123                 bool a = static_cast<bool>(params[i]);
4124                 napi_value result = nullptr;
4125                 napi_status status = napi_get_boolean(env, a, &result);
4126                 if (status != napi_ok) {
4127                     return nullptr;
4128                 }
4129                 napi_set_element(env, res, i, result);
4130             }
4131             LOGI("WebGL getUniform end");
4132             return res;
4133         } else if (type == GL_BOOL_VEC4) {
4134             LOGI("WebGL getUniform13 end");
4135             LOGI("WebGL getUniform end");
4136             GLint params[4] = {0};
4137             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4138                            static_cast<GLint *>(params));
4139             uint32_t i;
4140             uint32_t length = 4;
4141             napi_value res = nullptr;
4142             napi_create_array(env, &res);
4143             for (i = 0; i < length; i++) {
4144                 bool a = static_cast<bool>(params[i]);
4145                 napi_value result = nullptr;
4146                 napi_status status = napi_get_boolean(env, a, &result);
4147                 if (status != napi_ok) {
4148                     return nullptr;
4149                 }
4150                 napi_set_element(env, res, i, result);
4151             }
4152             LOGI("WebGL getUniform end");
4153             return res;
4154         } else {
4155             LOGI("WebGL getUniform end");
4156             return nullptr;
4157         }
4158     }
4159     return nullptr;
4160 }
4161 } // namespace Rosen
4162 } // namespace OHOS
4163 
4164 #ifdef __cplusplus
4165 }
4166 #endif
4167