• 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 = WEBGL_ACTIVE_INFO_NAME_MAX_LENGTH;
2786     GLsizei length;
2787     GLint size;
2788     GLenum type;
2789     GLchar name[WEBGL_ACTIVE_INFO_NAME_MAX_LENGTH] = {0};
2790     glGetActiveAttrib(static_cast<GLuint>(programId), static_cast<GLuint>(index),
2791         static_cast<GLsizei>(bufSize), &length, &size, &type, name);
2792     if (bufSize > WEBGL_ACTIVE_INFO_NAME_MAX_LENGTH) {
2793         LOGE("WebGL: GetActiveAttrib: [error] bufSize exceed!");
2794     }
2795     webGLActiveInfo->SetActiveName(name);
2796     webGLActiveInfo->SetActiveType(type);
2797     webGLActiveInfo->SetActiveSize(size);
2798     LOGI("WebGL getActiveAttrib end");
2799     return objActiveInfo;
2800 }
2801 
GetActiveUniform(napi_env env,napi_callback_info info)2802 napi_value WebGLRenderingContextBase::GetActiveUniform(napi_env env, napi_callback_info info)
2803 {
2804     NFuncArg funcArg(env, info);
2805     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2806         return nullptr;
2807     }
2808     bool succ = false;
2809     LOGI("WebGL getActiveUniform start");
2810     WebGLProgram *webGlProgram = nullptr;
2811     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
2812     if (programStatus != napi_ok) {
2813         return nullptr;
2814     }
2815     int programId = webGlProgram->GetProgramId();
2816     LOGI("WebGL WebGLRenderContext::getActiveUniform programId = %{public}u", programId);
2817     int64_t index;
2818     tie(succ, index) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
2819     if (!succ) {
2820         return nullptr;
2821     }
2822     LOGI("WebGL WebGLRenderContext::getActiveUniform index = %{public}u", index);
2823 
2824     napi_value objActiveInfo = NClass::InstantiateClass(env, WebGLActiveInfo::className, {});
2825     if (!objActiveInfo) {
2826         return nullptr;
2827     }
2828     auto webGLActiveInfo = NClass::GetEntityOf<WebGLActiveInfo>(env, objActiveInfo);
2829     if (!webGLActiveInfo) {
2830         return nullptr;
2831     }
2832     int64_t bufSize = WEBGL_ACTIVE_INFO_NAME_MAX_LENGTH;
2833     GLsizei length;
2834     GLint size;
2835     GLenum type;
2836     GLchar name[WEBGL_ACTIVE_INFO_NAME_MAX_LENGTH] = {0};
2837     glGetActiveUniform(static_cast<GLuint>(programId), static_cast<GLuint>(index),
2838         static_cast<GLsizei>(bufSize), &length, &size, &type, name);
2839     if (bufSize > WEBGL_ACTIVE_INFO_NAME_MAX_LENGTH) {
2840         LOGE("WebGL: GetActiveUniform: [error] bufSize exceed!");
2841     }
2842     webGLActiveInfo->SetActiveName(name);
2843     webGLActiveInfo->SetActiveType(type);
2844     webGLActiveInfo->SetActiveSize(size);
2845     LOGI("WebGL getActiveUniform end");
2846     return objActiveInfo;
2847 }
2848 
GetBufferParameter(napi_env env,napi_callback_info info)2849 napi_value WebGLRenderingContextBase::GetBufferParameter(napi_env env, napi_callback_info info)
2850 {
2851     NFuncArg funcArg(env, info);
2852     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2853         return nullptr;
2854     }
2855     bool succ = false;
2856     LOGI("WebGL getBufferParameter start");
2857     int64_t target;
2858     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
2859     if (!succ) {
2860         return nullptr;
2861     }
2862     LOGI("WebGL WebGLRenderContext::getBufferParameter target = %{public}u", target);
2863     int64_t pname;
2864     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
2865     if (!succ) {
2866         return nullptr;
2867     }
2868     LOGI("WebGL WebGLRenderContext::getBufferParameter pname = %{public}u", pname);
2869     if (pname == GL_BUFFER_SIZE || pname == GL_BUFFER_USAGE) {
2870         GLint params;
2871         glGetBufferParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
2872         int64_t res = static_cast<int64_t>(params);
2873         LOGI("WebGL getBufferParameter end");
2874         return NVal::CreateInt64(env, res).val_;
2875     } else {
2876         LOGI("WebGL getBufferParameter : pname is wrong");
2877         return nullptr;
2878     }
2879 }
2880 
GetError(napi_env env,napi_callback_info info)2881 napi_value WebGLRenderingContextBase::GetError(napi_env env, napi_callback_info info)
2882 {
2883     NFuncArg funcArg(env, info);
2884     LOGI("WebGL getError start");
2885     int64_t res = static_cast<int64_t>(glGetError());
2886     LOGI("WebGL WebGLRenderContext::getError res = %{public}u", res);
2887     LOGI("WebGL getError end");
2888     return NVal::CreateInt64(env, res).val_;
2889 }
2890 
GetFramebufferAttachmentParameter(napi_env env,napi_callback_info info)2891 napi_value WebGLRenderingContextBase::GetFramebufferAttachmentParameter(napi_env env, napi_callback_info info)
2892 {
2893     NFuncArg funcArg(env, info);
2894     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
2895         return nullptr;
2896     }
2897     bool succ = false;
2898     LOGI("WebGL getFramebufferAttachmentParameter start");
2899     int64_t target;
2900     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
2901     if (!succ) {
2902         return nullptr;
2903     }
2904     LOGI("WebGL WebGLRenderingContextBase::getFramebufferAttachmentParameter target = %{public}u", target);
2905     int64_t attachment;
2906     tie(succ, attachment) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
2907     if (!succ) {
2908         return nullptr;
2909     }
2910     LOGI("WebGL WebGLRenderingContextBase::getFramebufferAttachmentParameter attachment = %{public}u", attachment);
2911     int64_t pname;
2912     tie(succ, pname) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt64();
2913     if (!succ) {
2914         return nullptr;
2915     }
2916     LOGI("WebGL WebGLRenderingContextBase::getFramebufferAttachmentParameter pname = %{public}u", pname);
2917     if (pname == FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE ||
2918         pname == FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL ||
2919         pname == FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE ||
2920         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE ||
2921         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_BLUE_SIZE ||
2922         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING ||
2923         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE ||
2924         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE ||
2925         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_GREEN_SIZE ||
2926         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_RED_SIZE ||
2927         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE ||
2928         pname == WebGL2RenderingContextBase::FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER) {
2929         GLint params;
2930         glGetFramebufferAttachmentParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2931                                               static_cast<GLenum>(pname), &params);
2932         int64_t res = static_cast<int64_t>(params);
2933         LOGI("WebGL getFramebufferAttachmentParameter end");
2934         return NVal::CreateInt64(env, res).val_;
2935     } else if (pname == FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
2936         GLint params;
2937         napi_value res = nullptr;
2938         napi_get_cb_info(env, info, nullptr, nullptr, &res, nullptr);
2939         glGetFramebufferAttachmentParameteriv (static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2940                                                FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &params);
2941         int64_t type = static_cast<int64_t>(params);
2942         if (type == RENDERBUFFER) {
2943             LOGI("WebGL getFramebufferAttachmentParameter WebGLRenderbuffer start");
2944             napi_value objFramebuffer = NClass::InstantiateClass(env, WebGLFramebuffer::className, {});
2945             if (!objFramebuffer) {
2946                 return nullptr;
2947             }
2948             auto webGlFramebuffer = NClass::GetEntityOf<WebGLFramebuffer>(env, objFramebuffer);
2949             if (!webGlFramebuffer) {
2950                 return nullptr;
2951             }
2952             GLint framebufferId;
2953             glGetFramebufferAttachmentParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2954                                                   static_cast<GLenum>(pname), &framebufferId);
2955             webGlFramebuffer->SetFramebuffer(static_cast<unsigned int>(framebufferId));
2956             LOGI("WebGL getFramebufferAttachmentParameter framebufferId = %{public}u", framebufferId);
2957             LOGI("WebGL getFramebufferAttachmentParameter WebGLRenderbuffer end");
2958             return objFramebuffer;
2959         } else if (type == TEXTURE) {
2960             LOGI("WebGL getFramebufferAttachmentParameter WebGLTexture start");
2961             napi_value objTexture = NClass::InstantiateClass(env, WebGLTexture::className, {});
2962             if (!objTexture) {
2963                 return nullptr;
2964             }
2965             auto webGlTexture = NClass::GetEntityOf<WebGLTexture>(env, objTexture);
2966             if (!webGlTexture) {
2967                 return nullptr;
2968             }
2969             GLint textureId;
2970             glGetFramebufferAttachmentParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(attachment),
2971                                                   static_cast<GLenum>(pname), &textureId);
2972             webGlTexture->SetTexture(static_cast<unsigned int>(textureId));
2973             LOGI("WebGL getFramebufferAttachmentParameter textureId = %{public}u", textureId);
2974             LOGI("WebGL getFramebufferAttachmentParameter WebGLTexture end");
2975             return objTexture;
2976         } else {
2977             LOGI("WebGL getFramebufferAttachmentParameter : no image is attached");
2978             return 0;
2979         }
2980     } else {
2981         LOGI("WebGL getFramebufferAttachmentParameter : pname is wrong");
2982         return nullptr;
2983     }
2984 }
2985 
GetProgramParameter(napi_env env,napi_callback_info info)2986 napi_value WebGLRenderingContextBase::GetProgramParameter(napi_env env, napi_callback_info info)
2987 {
2988     NFuncArg funcArg(env, info);
2989     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
2990         return nullptr;
2991     }
2992     bool succ = false;
2993     LOGI("WebGL getProgramParameter start");
2994     if (funcArg[NARG_POS::FIRST] == nullptr) {
2995         return nullptr;
2996     }
2997     WebGLProgram *webGlProgram = nullptr;
2998     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
2999     if (programStatus != napi_ok) {
3000         return nullptr;
3001     }
3002     int programId = webGlProgram->GetProgramId();
3003     LOGI("WebGL WebGLRenderContext::getProgramParameter programId = %{public}u", programId);
3004     int64_t pname;
3005     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3006     if (!succ) {
3007         return nullptr;
3008     }
3009     LOGI("WebGL WebGLRenderContext::getProgramParameter pname = %{public}u", pname);
3010     GLint params;
3011     glGetProgramiv(static_cast<GLuint>(programId), static_cast<GLenum>(pname), &params);
3012     if (pname == GL_DELETE_STATUS || pname == GL_LINK_STATUS || pname == GL_VALIDATE_STATUS) {
3013         bool res = (params == GL_FALSE) ? false : true;
3014         LOGI("WebGL WebGLRenderContext::getProgramParameter params = %{public}u", params);
3015         LOGI("WebGL getProgramParameter end");
3016         return NVal::CreateBool(env, res).val_;
3017     } else if (pname == GL_ATTACHED_SHADERS || pname == GL_ACTIVE_ATTRIBUTES || pname == GL_ACTIVE_UNIFORMS) {
3018         int64_t res = static_cast<int64_t>(params);
3019         LOGI("WebGL getProgramParameter end");
3020         return NVal::CreateInt64(env, res).val_;
3021     } else {
3022         LOGI("WebGL getProgramParameter : pname is wrong");
3023         return nullptr;
3024     }
3025 }
3026 
GetRenderbufferParameter(napi_env env,napi_callback_info info)3027 napi_value WebGLRenderingContextBase::GetRenderbufferParameter(napi_env env, napi_callback_info info)
3028 {
3029     NFuncArg funcArg(env, info);
3030     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3031         return nullptr;
3032     }
3033     bool succ = false;
3034     LOGI("WebGL getRenderbufferParameter start");
3035     int64_t target;
3036     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3037     if (!succ) {
3038         return nullptr;
3039     }
3040     LOGI("WebGL WebGLRenderContext::getRenderbufferParameter target = %{public}u", target);
3041     int64_t pname;
3042     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3043     if (!succ) {
3044         return nullptr;
3045     }
3046     LOGI("WebGL WebGLRenderContext::getRenderbufferParameter pname = %{public}u", pname);
3047     if (pname == GL_RENDERBUFFER_WIDTH ||
3048         pname == GL_RENDERBUFFER_HEIGHT ||
3049         pname == GL_RENDERBUFFER_INTERNAL_FORMAT ||
3050         pname == GL_RENDERBUFFER_RED_SIZE ||
3051         pname == GL_RENDERBUFFER_GREEN_SIZE ||
3052         pname == GL_RENDERBUFFER_BLUE_SIZE ||
3053         pname == GL_RENDERBUFFER_ALPHA_SIZE ||
3054         pname == GL_RENDERBUFFER_DEPTH_SIZE ||
3055         pname == GL_RENDERBUFFER_STENCIL_SIZE) {
3056         GLint params;
3057         glGetRenderbufferParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
3058         int64_t res = static_cast<int64_t>(params);
3059         LOGI("WebGL getRenderbufferParameter end");
3060         return NVal::CreateInt64(env, res).val_;
3061     } else {
3062         LOGI("WebGL getRenderbufferParameter : pname is wrong");
3063         return nullptr;
3064     }
3065 }
3066 
GetTexParameter(napi_env env,napi_callback_info info)3067 napi_value WebGLRenderingContextBase::GetTexParameter(napi_env env, napi_callback_info info)
3068 {
3069     NFuncArg funcArg(env, info);
3070     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3071         return nullptr;
3072     }
3073     bool succ = false;
3074     LOGI("WebGL getTexParameter start");
3075     int64_t target;
3076     tie(succ, target) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3077     if (!succ) {
3078         return nullptr;
3079     }
3080     LOGI("WebGL WebGLRenderContext::getTexParameter target = %{public}u", target);
3081     int64_t pname;
3082     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3083     if (!succ) {
3084         return nullptr;
3085     }
3086     LOGI("WebGL WebGLRenderContext::getTexParameter pname = %{public}u", pname);
3087     if (pname == GL_TEXTURE_MAX_ANISOTROPY_EXT) {
3088         GLfloat params;
3089         glGetTexParameterfv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
3090         float res = static_cast<float>(params);
3091         LOGI("WebGL getTexParameter end");
3092         return NVal::CreateDouble(env, (double)res).val_;
3093     } else if (pname == GL_TEXTURE_MAG_FILTER || pname == GL_TEXTURE_MIN_FILTER ||
3094                pname == GL_TEXTURE_WRAP_S || pname == GL_TEXTURE_WRAP_T) {
3095         GLint params;
3096         glGetTexParameteriv(static_cast<GLenum>(target), static_cast<GLenum>(pname), &params);
3097         int64_t res = static_cast<int64_t>(params);
3098         LOGI("WebGL getTexParameter end");
3099         return NVal::CreateInt64(env, res).val_;
3100     } else {
3101         LOGI("WebGL getTexParameter : pname is wrong");
3102         return nullptr;
3103     }
3104 }
3105 
GetShaderParameter(napi_env env,napi_callback_info info)3106 napi_value WebGLRenderingContextBase::GetShaderParameter(napi_env env, napi_callback_info info)
3107 {
3108     NFuncArg funcArg(env, info);
3109     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3110         return nullptr;
3111     }
3112     bool succ = false;
3113     LOGI("WebGL getShaderParameter start");
3114     if (funcArg[NARG_POS::FIRST] == nullptr) {
3115         return nullptr;
3116     }
3117     WebGLShader *webGlShader = nullptr;
3118     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlShader);
3119     if (shaderStatus != napi_ok) {
3120         return nullptr;
3121     }
3122     int shaderId = webGlShader->GetShaderId();
3123     LOGI("WebGL WebGLRenderContext::getShaderParameter shaderId = %{public}u", shaderId);
3124     int64_t pname;
3125     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3126     if (!succ) {
3127         return nullptr;
3128     }
3129     LOGI("WebGL WebGLRenderContext::getShaderParameter pname = %{public}u", pname);
3130     GLint params;
3131     glGetShaderiv(static_cast<GLuint>(shaderId), static_cast<GLenum>(pname), &params);
3132     if (pname == GL_SHADER_TYPE) {
3133         int64_t res = static_cast<int64_t>(params);
3134         LOGI("WebGL getShaderParameter end");
3135         return NVal::CreateInt64(env, res).val_;
3136     } else if (pname == GL_DELETE_STATUS || pname == GL_COMPILE_STATUS) {
3137         bool res = (params == GL_FALSE) ? false : true;
3138         LOGI("WebGL WebGLRenderContext::getShaderParameter params = %{public}u", params);
3139         LOGI("WebGL getShaderParameter end");
3140         return NVal::CreateBool(env, res).val_;
3141     } else {
3142         LOGI("WebGL getShaderParameter : pname is wrong");
3143         return nullptr;
3144     }
3145 }
3146 
GetAttribLocation(napi_env env,napi_callback_info info)3147 napi_value WebGLRenderingContextBase::GetAttribLocation(napi_env env, napi_callback_info info)
3148 {
3149     NFuncArg funcArg(env, info);
3150     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3151         return nullptr;
3152     }
3153     bool succ = false;
3154     LOGI("WebGL getAttribLocation start");
3155     if (funcArg[NARG_POS::FIRST] == nullptr) {
3156         return nullptr;
3157     }
3158     WebGLProgram *webGlProgram = nullptr;
3159     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
3160     if (programStatus != napi_ok) {
3161         return nullptr;
3162     }
3163     int programId = webGlProgram->GetProgramId();
3164     LOGI("WebGL WebGLRenderContext::getAttribLocation programId = %{public}u", programId);
3165     unique_ptr<char[]> name;
3166     tie(succ, name, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
3167     if (!succ) {
3168         return nullptr;
3169     }
3170     LOGI("WebGL WebGLRenderContext::getAttribLocation name = %{public}s", name.get());
3171     GLint returnValue = glGetAttribLocation(static_cast<GLuint>(programId), const_cast<char *>(name.get()));
3172     int64_t res = static_cast<int64_t>(returnValue);
3173     LOGI("WebGL getAttribLocation end");
3174     return NVal::CreateInt64(env, res).val_;
3175 }
3176 
VertexAttrib1fv(napi_env env,napi_callback_info info)3177 napi_value WebGLRenderingContextBase::VertexAttrib1fv(napi_env env, napi_callback_info info)
3178 {
3179     NFuncArg funcArg(env, info);
3180 
3181     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3182         return nullptr;
3183     }
3184     bool succ = false;
3185     LOGI("WebGL vertexAttrib1fv start");
3186     int32_t index;
3187     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3188     if (!succ) {
3189         return nullptr;
3190     }
3191     LOGI("WebGL WebGLRenderContext::vertexAttrib1fv index = %{public}u", index);
3192     napi_value array = funcArg[NARG_POS::SECOND];
3193     bool isArray = false;
3194     tie(succ, isArray) = NVal(env, array).IsArray();
3195     if (isArray) {     // []
3196         LOGI("WebGL vertexAttrib1fv is Array");
3197         uint32_t length;
3198         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3199         if (lengthStatus != napi_ok) {
3200             return nullptr;
3201         }
3202         float vertexAttrib1fv[length];
3203         uint32_t i;
3204         for (i = 0; i < length; i++) {
3205             napi_value element;
3206             napi_status eleStatus = napi_get_element(env, array, i, &element);
3207             if (eleStatus != napi_ok) {
3208                 return nullptr;
3209             }
3210             double ele;
3211             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3212             if (doubleStatus != napi_ok) {
3213                 return nullptr;
3214             }
3215             vertexAttrib1fv[i] = (float) ele;
3216         }
3217         glVertexAttrib1fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib1fv));
3218         LOGI("WebGL2 vertexAttrib1fv array end");
3219         return nullptr;
3220     }
3221     bool isTypedarray = false;
3222     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3223     if (!isTypedarray || !succ) {
3224         return nullptr;
3225     }
3226     void *data = nullptr;
3227     size_t length;
3228     napi_typedarray_type type;
3229     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3230     if (!succ) {
3231         return nullptr;
3232     }
3233     if (type == napi_float32_array) {
3234         float inputFloat[INPUTFLOAT_LENGTH] = {0};
3235         errno_t ret = memcpy_s(inputFloat, sizeof(inputFloat), data, length);
3236         if (ret != EOK) {
3237             LOGE("WebGL2 vertexAttrib1fv memcpy_s failed");
3238             return nullptr;
3239         }
3240         glVertexAttrib1fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3241     }
3242     LOGI("WebGL2 vertexAttrib1fv typeArray end");
3243     return nullptr;
3244 }
3245 
VertexAttrib2fv(napi_env env,napi_callback_info info)3246 napi_value WebGLRenderingContextBase::VertexAttrib2fv(napi_env env, napi_callback_info info)
3247 {
3248     NFuncArg funcArg(env, info);
3249 
3250     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3251         return nullptr;
3252     }
3253     bool succ = false;
3254     LOGI("WebGL vertexAttrib2fv start");
3255     int32_t index;
3256     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3257     if (!succ) {
3258         return nullptr;
3259     }
3260     LOGI("WebGL WebGLRenderContext::vertexAttrib2fv index = %{public}u", index);
3261     napi_value array = funcArg[NARG_POS::SECOND];
3262     bool isArray = false;
3263     tie(succ, isArray) = NVal(env, array).IsArray();
3264     if (isArray) {     // []
3265         LOGI("WebGL vertexAttrib2fv is Array");
3266         uint32_t length;
3267         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3268         if (lengthStatus != napi_ok) {
3269             return nullptr;
3270         }
3271         float vertexAttrib2fv[length];
3272         uint32_t i;
3273         for (i = 0; i < length; i++) {
3274             napi_value element;
3275             napi_status eleStatus = napi_get_element(env, array, i, &element);
3276             if (eleStatus != napi_ok) {
3277                 return nullptr;
3278             }
3279             double ele;
3280             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3281             if (doubleStatus != napi_ok) {
3282                 return nullptr;
3283             }
3284             vertexAttrib2fv[i] = (float) ele;
3285         }
3286         glVertexAttrib2fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib2fv));
3287         LOGI("WebGL2 vertexAttrib2fv array end");
3288         return nullptr;
3289     }
3290     bool isTypedarray = false;
3291     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3292     if (!isTypedarray || !succ) {
3293         return nullptr;
3294     }
3295     void *data = nullptr;
3296     size_t length;
3297     napi_typedarray_type type;
3298     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3299     if (!succ) {
3300         return nullptr;
3301     }
3302     if (type == napi_float32_array) {
3303         float inputFloat[INPUTFLOAT_LENGTH] = {0};
3304         errno_t ret = memcpy_s(inputFloat, sizeof(inputFloat), data, length);
3305         if (ret != EOK) {
3306             LOGE("WebGL2 vertexAttrib2fv memcpy_s failed");
3307             return nullptr;
3308         }
3309         glVertexAttrib2fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3310     }
3311     LOGI("WebGL2 vertexAttrib2fv typeArray end");
3312     return nullptr;
3313 }
3314 
VertexAttrib3fv(napi_env env,napi_callback_info info)3315 napi_value WebGLRenderingContextBase::VertexAttrib3fv(napi_env env, napi_callback_info info)
3316 {
3317     NFuncArg funcArg(env, info);
3318 
3319     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3320         return nullptr;
3321     }
3322     bool succ = false;
3323     LOGI("WebGL vertexAttrib3fv start");
3324     int32_t index;
3325     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3326     if (!succ) {
3327         return nullptr;
3328     }
3329     LOGI("WebGL WebGLRenderContext::vertexAttrib3fv index = %{public}u", index);
3330     napi_value array = funcArg[NARG_POS::SECOND];
3331     bool isArray = false;
3332     tie(succ, isArray) = NVal(env, array).IsArray();
3333     if (isArray) {     // []
3334         LOGI("WebGL vertexAttrib3fv is Array");
3335         uint32_t length;
3336         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3337         if (lengthStatus != napi_ok) {
3338             return nullptr;
3339         }
3340         float vertexAttrib3fv[length];
3341         uint32_t i;
3342         for (i = 0; i < length; i++) {
3343             napi_value element;
3344             napi_status eleStatus = napi_get_element(env, array, i, &element);
3345             if (eleStatus != napi_ok) {
3346                 return nullptr;
3347             }
3348             double ele;
3349             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3350             if (doubleStatus != napi_ok) {
3351                 return nullptr;
3352             }
3353             vertexAttrib3fv[i] = (float) ele;
3354         }
3355         glVertexAttrib3fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib3fv));
3356         LOGI("WebGL2 vertexAttrib3fv array end");
3357         return nullptr;
3358     }
3359     bool isTypedarray = false;
3360     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3361     if (!isTypedarray || !succ) {
3362         return nullptr;
3363     }
3364     void *data = nullptr;
3365     size_t length;
3366     napi_typedarray_type type;
3367     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3368     if (!succ) {
3369         return nullptr;
3370     }
3371     if (type == napi_float32_array) {
3372         float inputFloat[INPUTFLOAT_LENGTH] = {0};
3373         errno_t ret = memcpy_s(inputFloat, sizeof(inputFloat), data, length);
3374         if (ret != EOK) {
3375             LOGE("WebGL2 vertexAttrib3fv memcpy_s failed");
3376             return nullptr;
3377         }
3378         glVertexAttrib3fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3379     }
3380     LOGI("WebGL2 vertexAttrib3fv typeArray end");
3381     return nullptr;
3382 }
3383 
VertexAttrib4fv(napi_env env,napi_callback_info info)3384 napi_value WebGLRenderingContextBase::VertexAttrib4fv(napi_env env, napi_callback_info info)
3385 {
3386     NFuncArg funcArg(env, info);
3387 
3388     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3389         return nullptr;
3390     }
3391     bool succ = false;
3392     LOGI("WebGL vertexAttrib4fv start");
3393     int32_t index;
3394     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3395     if (!succ) {
3396         return nullptr;
3397     }
3398     LOGI("WebGL WebGLRenderContext::vertexAttrib4fv index = %{public}u", index);
3399     napi_value array = funcArg[NARG_POS::SECOND];
3400     bool isArray = false;
3401     tie(succ, isArray) = NVal(env, array).IsArray();
3402     if (isArray) {     // []
3403         LOGI("WebGL vertexAttrib4fv is Array");
3404         uint32_t length;
3405         napi_status lengthStatus = napi_get_array_length(env, array, &length);
3406         if (lengthStatus != napi_ok) {
3407             return nullptr;
3408         }
3409         float vertexAttrib4fv[length];
3410         uint32_t i;
3411         for (i = 0; i < length; i++) {
3412             napi_value element;
3413             napi_status eleStatus = napi_get_element(env, array, i, &element);
3414             if (eleStatus != napi_ok) {
3415                 return nullptr;
3416             }
3417             double ele;
3418             napi_status doubleStatus = napi_get_value_double(env, element, &ele);
3419             if (doubleStatus != napi_ok) {
3420                 return nullptr;
3421             }
3422             vertexAttrib4fv[i] = (float) ele;
3423         }
3424         glVertexAttrib4fv(static_cast<GLuint>(index), static_cast<GLfloat *>(vertexAttrib4fv));
3425         LOGI("WebGL2 vertexAttrib4fv array end");
3426         return nullptr;
3427     }
3428     bool isTypedarray = false;
3429     tie(succ, isTypedarray) = NVal(env, array).IsTypeArray();
3430     if (!isTypedarray || !succ) {
3431         return nullptr;
3432     }
3433     void *data = nullptr;
3434     size_t length;
3435     napi_typedarray_type type;
3436     tie(succ, type, data, length) = NVal(env, array).ToTypedArray();
3437     if (!succ) {
3438         return nullptr;
3439     }
3440     if (type == napi_float32_array) {
3441         float inputFloat[INPUTFLOAT_LENGTH] = {0};
3442         errno_t ret = memcpy_s(inputFloat, sizeof(inputFloat), data, length);
3443         if (ret != EOK) {
3444             LOGE("WebGL2 vertexAttrib4fv memcpy_s failed");
3445             return nullptr;
3446         }
3447         glVertexAttrib4fv(static_cast<GLuint>(index), static_cast<GLfloat *>(inputFloat));
3448     }
3449     LOGI("WebGL2 vertexAttrib4fv typeArray end");
3450     return nullptr;
3451 }
3452 
GetVertexAttrib(napi_env env,napi_callback_info info)3453 napi_value WebGLRenderingContextBase::GetVertexAttrib(napi_env env, napi_callback_info info)
3454 {
3455     NFuncArg funcArg(env, info);
3456     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3457         return nullptr;
3458     }
3459     bool succ = false;
3460     LOGI("WebGL getVertexAttrib start");
3461     int32_t index;
3462     tie(succ, index) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
3463     if (!succ) {
3464         return nullptr;
3465     }
3466     LOGI("WebGL WebGLRenderContext::getVertexAttrib index = %{public}u", index);
3467     int32_t pname;
3468     tie(succ, pname) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
3469     if (!succ) {
3470         return nullptr;
3471     }
3472     LOGI("WebGL WebGLRenderContext::getVertexAttrib pname = %{public}u", pname);
3473     if (pname == GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING) {  // webglBuffer
3474         int32_t params;
3475         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3476         GLfloat par = static_cast<float>(params);
3477         napi_value objBuffer = NClass::InstantiateClass(env, WebGLBuffer::className, {});
3478         if (!objBuffer) {
3479             return nullptr;
3480         }
3481         auto webGlBuffer = NClass::GetEntityOf<WebGLBuffer>(env, objBuffer);
3482         if (!webGlBuffer) {
3483             return nullptr;
3484         }
3485         webGlBuffer->SetParams(par);
3486         LOGI("WebGL WebGLRenderingContextBase::createBuffer par = %{public}u", par);
3487         LOGI("WebGL getVertexAttrib panem is array end");
3488         return objBuffer;
3489     } else if (pname == GL_VERTEX_ATTRIB_ARRAY_ENABLED || pname == GL_VERTEX_ATTRIB_ARRAY_NORMALIZED ||
3490         pname == GL_VERTEX_ATTRIB_ARRAY_INTEGER) {
3491         int32_t params;
3492         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3493         bool res = static_cast<bool>(params);
3494         LOGI("WebGL getVertexAttrib pname is GLBoolean end");
3495         return NVal::CreateBool(env, res).val_;
3496     } else if (pname == GL_VERTEX_ATTRIB_ARRAY_SIZE || pname == GL_VERTEX_ATTRIB_ARRAY_STRIDE ||
3497         pname == GL_VERTEX_ATTRIB_ARRAY_DIVISOR || pname == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE) {
3498         int32_t params;
3499         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3500         int res = static_cast<int>(params);
3501         LOGI("WebGL getVertexAttrib pname is GLint end");
3502         return NVal::CreateInt64(env, res).val_;
3503     } else if (pname == GL_VERTEX_ATTRIB_ARRAY_TYPE) {  // GLenum
3504         int32_t params;
3505         glGetVertexAttribiv(static_cast<GLuint>(index), static_cast<GLenum>(pname), &params);
3506         int64_t res = static_cast<int>(params);
3507         LOGI("WebGL getVertexAttrib pname is Glenum end");
3508         return NVal::CreateInt64(env, res).val_;
3509     } else if (pname == GL_CURRENT_VERTEX_ATTRIB) {   // float32Array
3510         static float res[4] = {};
3511         glGetVertexAttribfv(static_cast<GLuint>(index), static_cast<GLenum>(pname), reinterpret_cast<GLfloat*>(res));
3512         napi_value outputBuffer = nullptr;
3513         napi_create_external_arraybuffer(env, res, sizeof(res),
3514                                          [](napi_env env, void *finalize_data, void *finalize_hint) {},
3515                                          NULL, &outputBuffer);
3516         napi_value output_array = nullptr;
3517         napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float), outputBuffer, 0, &output_array);
3518         LOGI("WebGL getVertexAttrib pname is float32Array end");
3519         return output_array;
3520     } else {
3521         return nullptr;
3522     }
3523 }
3524 
GetParameter(napi_env env,napi_callback_info info)3525 napi_value WebGLRenderingContextBase::GetParameter(napi_env env, napi_callback_info info)
3526 {
3527     NFuncArg funcArg(env, info);
3528     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3529         return nullptr;
3530     }
3531     bool succ = false;
3532     LOGI("WebGL getParameter start");
3533     int64_t pname;
3534     tie(succ, pname) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3535     if (!succ) {
3536         return nullptr;
3537     }
3538     LOGI("WebGL WebGLRenderContext::getParameter pname = %{public}u", pname);
3539     if (pname == GL_DEPTH_CLEAR_VALUE || pname == GL_LINE_WIDTH ||
3540         pname == GL_POLYGON_OFFSET_FACTOR || pname == GL_POLYGON_OFFSET_UNITS ||
3541         pname == GL_SAMPLE_COVERAGE_VALUE) {
3542         GLfloat params;
3543         glGetFloatv(static_cast<GLenum>(pname), &params);
3544         float res = static_cast<float>(params);
3545         LOGI("WebGL getParameter end");
3546         return NVal::CreateDouble(env, (double)res).val_;
3547     } else if (pname == GL_TEXTURE_BINDING_2D || pname == GL_TEXTURE_BINDING_CUBE_MAP) {
3548         GLint params;
3549         glGetIntegerv(static_cast<GLenum>(pname), &params);
3550         napi_value objTexture = NClass::InstantiateClass(env, WebGLTexture::className, {});
3551         if (!objTexture) {
3552             return nullptr;
3553         }
3554         auto webGlTexture = NClass::GetEntityOf<WebGLTexture>(env, objTexture);
3555         if (!webGlTexture) {
3556             return nullptr;
3557         }
3558         webGlTexture->SetTexture(params);
3559         LOGI("WebGL getParameter end");
3560         return objTexture;
3561     } else if (pname == GL_RENDERER || pname == GL_SHADING_LANGUAGE_VERSION ||
3562                pname == GL_VENDOR || pname == GL_VERSION) {
3563         LOGI("WebGL pname : string");
3564         const unsigned char* extensions = glGetString(static_cast<GLenum>(pname));
3565         string str = const_cast<char*>(reinterpret_cast<const char*>(extensions));
3566         vector<string> vec;
3567         Util::SplitString(str, vec, " ");
3568         napi_value result = nullptr;
3569         napi_create_array_with_length(env, vec.size(), &result);
3570         for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
3571             napi_set_element(env, result, i, NVal::CreateUTF8String(env, vec[i]).val_);
3572         }
3573         return result;
3574     } else if (pname == GL_ARRAY_BUFFER_BINDING || pname == GL_ELEMENT_ARRAY_BUFFER_BINDING) {
3575         GLint params;
3576         glGetIntegerv(static_cast<GLenum>(pname), &params);
3577         napi_value objBuffer = NClass::InstantiateClass(env, WebGLBuffer::className, {});
3578         if (!objBuffer) {
3579             return nullptr;
3580         }
3581         auto webGlBuffer = NClass::GetEntityOf<WebGLBuffer>(env, objBuffer);
3582         if (!webGlBuffer) {
3583             return nullptr;
3584         }
3585         webGlBuffer->SetBuffer(params);
3586         LOGI("WebGL getParameter end");
3587         return objBuffer;
3588     } else if (pname == GL_FRAMEBUFFER_BINDING) {
3589         GLint params;
3590         glGetIntegerv(static_cast<GLenum>(pname), &params);
3591         napi_value objFramebuffer = NClass::InstantiateClass(env, WebGLFramebuffer::className, {});
3592         if (!objFramebuffer) {
3593             return nullptr;
3594         }
3595         auto webGLFramebuffer = NClass::GetEntityOf<WebGLFramebuffer>(env, objFramebuffer);
3596         if (!webGLFramebuffer) {
3597             return nullptr;
3598         }
3599         webGLFramebuffer->SetFramebuffer(params);
3600         LOGI("WebGL getParameter end");
3601         return objFramebuffer;
3602     } else if (pname == GL_CURRENT_PROGRAM) {
3603         GLint params;
3604         glGetIntegerv(static_cast<GLenum>(pname), &params);
3605         napi_value objProgram = NClass::InstantiateClass(env, WebGLProgram::className, {});
3606         if (!objProgram) {
3607             return nullptr;
3608         }
3609         auto webGlProgram = NClass::GetEntityOf<WebGLProgram>(env, objProgram);
3610         if (!webGlProgram) {
3611             return nullptr;
3612         }
3613         webGlProgram->SetProgramId(params);
3614         LOGI("WebGL getParameter end");
3615         return objProgram;
3616     } else if (pname == GL_RENDERBUFFER_BINDING) {
3617         GLint params;
3618         glGetIntegerv(static_cast<GLenum>(pname), &params);
3619         napi_value objRenderbuffer = NClass::InstantiateClass(env, WebGLRenderbuffer::className, {});
3620         if (!objRenderbuffer) {
3621             return nullptr;
3622         }
3623         auto webGlRenderbuffer = NClass::GetEntityOf<WebGLRenderbuffer>(env, objRenderbuffer);
3624         if (!webGlRenderbuffer) {
3625             return nullptr;
3626         }
3627         webGlRenderbuffer->SetRenderbuffer(params);
3628         LOGI("WebGL getParameter end");
3629         return objRenderbuffer;
3630     } else if (pname == GL_ALIASED_LINE_WIDTH_RANGE || pname == GL_ALIASED_POINT_SIZE_RANGE ||
3631                pname == GL_DEPTH_RANGE) {
3632         LOGI("WebGL pname : Float32Array with 2 elements");
3633         static float res[2] = {};
3634         glGetFloatv(static_cast<GLenum>(pname), reinterpret_cast<GLfloat*>(res));
3635         napi_value outputBuffer = nullptr;
3636         napi_create_external_arraybuffer(env, res, sizeof(res),
3637                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3638                                          NULL, &outputBuffer);
3639         napi_value outputArray = nullptr;
3640         napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
3641                                outputBuffer, 0, &outputArray);
3642         LOGI("WebGL getParameter end");
3643         return outputArray;
3644     } else if (pname == GL_COLOR_CLEAR_VALUE || pname == GL_BLEND_COLOR) {
3645         LOGI("WebGL pname : Float32Array with 4 valus");
3646         static float res[4] = {};
3647         glGetFloatv(static_cast<GLenum>(pname), reinterpret_cast<GLfloat*>(res));
3648         napi_value outputBuffer = nullptr;
3649         napi_create_external_arraybuffer(env, res, sizeof(res),
3650                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3651                                          NULL, &outputBuffer);
3652         napi_value outputArray = nullptr;
3653         napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
3654                                outputBuffer, 0, &outputArray);
3655         LOGI("WebGL getParameter end");
3656         return outputArray;
3657     } else if (pname == GL_COMPRESSED_TEXTURE_FORMATS) {
3658         LOGI("WebGL pname : Uint32Array");
3659         GLint count = 0;
3660         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &count);
3661         GLint params[count];
3662         glGetIntegerv(static_cast<GLenum>(pname), static_cast<GLint*>(params));
3663         static uint32_t *res = new uint32_t[count];
3664         int i;
3665         for (i = 0; i < count; i++) {
3666             res[i] = static_cast<uint32_t>(params[i]);
3667         }
3668         napi_value outputBuffer = nullptr;
3669         napi_create_external_arraybuffer(env, res, sizeof(params),
3670                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3671                                          NULL, &outputBuffer);
3672         napi_value outputArray = nullptr;
3673         napi_create_typedarray(env, napi_uint32_array, sizeof(params) / sizeof(uint32_t),
3674                                outputBuffer, 0, &outputArray);
3675         LOGI("WebGL getParameter end");
3676         return outputArray;
3677     } else if (pname == GL_MAX_VIEWPORT_DIMS) {
3678         LOGI("WebGL pname : int32Array with 2 elements");
3679         static int64_t res[2] = {};
3680         glGetIntegerv(static_cast<GLenum>(pname), reinterpret_cast<GLint*>(res));
3681         napi_value outputBuffer = nullptr;
3682         napi_create_external_arraybuffer(env, res, sizeof(res),
3683                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3684                                          NULL, &outputBuffer);
3685         napi_value outputArray = nullptr;
3686         napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
3687                                outputBuffer, 0, &outputArray);
3688         LOGI("WebGL getParameter end");
3689         return outputArray;
3690     } else if (pname == GL_SCISSOR_BOX) {
3691         LOGI("WebGL pname : int32Array with 2 4 valus");
3692         static int64_t res[4] = {};
3693         glGetIntegerv(static_cast<GLenum>(pname), reinterpret_cast<GLint*>(res));
3694         napi_value outputBuffer = nullptr;
3695         napi_create_external_arraybuffer(env, res, sizeof(res),
3696                                          [](napi_env env, void *finalize_data, void *finalize_hint) {  },
3697                                          NULL, &outputBuffer);
3698         napi_value outputArray = nullptr;
3699         napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
3700                                outputBuffer, 0, &outputArray);
3701         LOGI("WebGL getParameter end");
3702         return outputArray;
3703     } else if (pname == GL_BLEND || pname == GL_CULL_FACE || pname == GL_DEPTH_TEST ||
3704                pname == GL_DEPTH_WRITEMASK || pname == GL_DITHER || pname == GL_POLYGON_OFFSET_FILL ||
3705                pname == GL_SAMPLE_ALPHA_TO_COVERAGE || pname == GL_SAMPLE_COVERAGE ||
3706                pname == GL_SAMPLE_COVERAGE_INVERT || pname == GL_SCISSOR_TEST || pname == GL_STENCIL_TEST ||
3707                pname == WebGLRenderingContextBase::UNPACK_FLIP_Y_WEBGL ||
3708                pname == WebGLRenderingContextBase::UNPACK_PREMULTIPLY_ALPHA_WEBGL) {
3709         GLboolean params;
3710         glGetBooleanv(static_cast<GLenum>(pname), &params);
3711         bool res = static_cast<bool>(params);
3712         LOGI("WebGL getParameter end");
3713         return NVal::CreateBool(env, res).val_;
3714     } else if (pname == GL_COLOR_WRITEMASK) {
3715         LOGI("WebGL bool end");
3716         GLboolean params[4];
3717         glGetBooleanv(static_cast<GLenum>(pname), static_cast<GLboolean*>(params));
3718         napi_value res = nullptr;
3719         napi_create_array(env, &res);
3720         uint32_t i;
3721         uint32_t length = 4;
3722         for (i = 0; i < length; i++) {
3723             bool a = static_cast<bool>(params[i]);
3724             napi_value result = nullptr;
3725             napi_status status = napi_get_boolean(env, a, &result);
3726             if (status != napi_ok) {
3727                 return nullptr;
3728             }
3729             napi_set_element(env, res, i, result);
3730         }
3731         LOGI("WebGL getParameter end");
3732         return res;
3733     } else if (pname == GL_ACTIVE_TEXTURE ||
3734                pname == GL_ALPHA_BITS ||
3735                pname == GL_BLEND_DST_ALPHA ||
3736                pname == GL_BLEND_DST_RGB ||
3737                pname == GL_BLEND_EQUATION_ALPHA ||
3738                pname == GL_BLEND_EQUATION_RGB ||
3739                pname == GL_BLEND_SRC_ALPHA ||
3740                pname == GL_BLEND_SRC_RGB ||
3741                pname == GL_BLUE_BITS ||
3742                pname == GL_CULL_FACE_MODE ||
3743                pname == GL_DEPTH_BITS ||
3744                pname == GL_DEPTH_FUNC ||
3745                pname == GL_FRONT_FACE ||
3746                pname == GL_GENERATE_MIPMAP_HINT ||
3747                pname == GL_GREEN_BITS ||
3748                pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT ||
3749                pname == GL_IMPLEMENTATION_COLOR_READ_TYPE ||
3750                pname == GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ||
3751                pname == GL_MAX_CUBE_MAP_TEXTURE_SIZE ||
3752                pname == GL_MAX_FRAGMENT_UNIFORM_VECTORS ||
3753                pname == GL_MAX_RENDERBUFFER_SIZE ||
3754                pname == GL_MAX_TEXTURE_IMAGE_UNITS ||
3755                pname == GL_MAX_TEXTURE_SIZE ||
3756                pname == GL_MAX_VARYING_VECTORS ||
3757                pname == GL_MAX_VERTEX_ATTRIBS ||
3758                pname == GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS ||
3759                pname == GL_MAX_VERTEX_UNIFORM_VECTORS ||
3760                pname == GL_PACK_ALIGNMENT ||
3761                pname == GL_RED_BITS ||
3762                pname == GL_SAMPLE_BUFFERS ||
3763                pname == GL_SAMPLES ||
3764                pname == GL_STENCIL_BACK_FAIL ||
3765                pname == GL_STENCIL_BACK_FUNC ||
3766                pname == GL_STENCIL_BACK_PASS_DEPTH_FAIL ||
3767                pname == GL_STENCIL_BACK_PASS_DEPTH_PASS ||
3768                pname == GL_STENCIL_BACK_REF ||
3769                pname == GL_STENCIL_BACK_VALUE_MASK ||
3770                pname == GL_STENCIL_BACK_WRITEMASK ||
3771                pname == GL_STENCIL_BITS ||
3772                pname == GL_STENCIL_CLEAR_VALUE ||
3773                pname == GL_STENCIL_FAIL ||
3774                pname == GL_STENCIL_FUNC ||
3775                pname == GL_STENCIL_PASS_DEPTH_FAIL ||
3776                pname == GL_STENCIL_PASS_DEPTH_PASS ||
3777                pname == GL_STENCIL_REF ||
3778                pname == GL_STENCIL_VALUE_MASK ||
3779                pname == GL_STENCIL_WRITEMASK ||
3780                pname == GL_SUBPIXEL_BITS ||
3781                pname == GL_UNPACK_ALIGNMENT ||
3782                pname == UNPACK_COLORSPACE_CONVERSION_WEBGL) {
3783         GLint params;
3784         glGetIntegerv(static_cast<GLenum>(pname), &params);
3785         int64_t res = static_cast<int64_t>(params);
3786         LOGI("WebGL getParameter end");
3787         return NVal::CreateInt64(env, res).val_;
3788     } else {
3789         LOGI("WebGL getParameter : pname is wrong");
3790         return nullptr;
3791     }
3792 }
3793 
GetAttachedShaders(napi_env env,napi_callback_info info)3794 napi_value WebGLRenderingContextBase::GetAttachedShaders(napi_env env, napi_callback_info info)
3795 {
3796     NFuncArg funcArg(env, info);
3797     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3798         return nullptr;
3799     }
3800     LOGI("WebGL getAttachedShaders start");
3801     WebGLProgram *webGlProgram = nullptr;
3802     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
3803     if (programStatus != napi_ok) {
3804         return nullptr;
3805     }
3806     int programId = webGlProgram->GetProgramId();
3807     LOGI("WebGL WebGLRenderContext::getAttachedShaders programId = %{public}u", programId);
3808     int64_t maxCount = 0;
3809     GLsizei count = 1;
3810     GLuint shader[count];
3811     glGetAttachedShaders(static_cast<GLuint>(programId), static_cast<GLsizei>(maxCount), &count,
3812                          static_cast<GLuint*>(shader));
3813     LOGI("WebGL WebGLRenderContext::getAttachedShaders count = %{public}u", count);
3814     GLsizei i;
3815     napi_value objShader = NClass::InstantiateClass(env, WebGLShader::className, {});
3816     if (!objShader) {
3817         return nullptr;
3818     }
3819     auto webGlShader = NClass::GetEntityOf<WebGLShader>(env, objShader);
3820     if (!webGlShader) {
3821         return nullptr;
3822     }
3823     napi_value shaderArr = nullptr;
3824     napi_create_array(env, &shaderArr);
3825     for (i = 0; i < count; i++) {
3826         webGlShader->SetShaderId(shader[i]);
3827         napi_set_element(env, shaderArr, i, objShader);
3828     }
3829     LOGI("WebGL getAttachedShaders end");
3830     return shaderArr;
3831 }
3832 
GetShaderPrecisionFormat(napi_env env,napi_callback_info info)3833 napi_value WebGLRenderingContextBase::GetShaderPrecisionFormat(napi_env env, napi_callback_info info)
3834 {
3835     NFuncArg funcArg(env, info);
3836     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3837         return nullptr;
3838     }
3839     bool succ = false;
3840     LOGI("WebGL getShaderPrecisionFormat start");
3841     int64_t shaderType;
3842     tie(succ, shaderType) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64();
3843     if (!succ) {
3844         return nullptr;
3845     }
3846     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat shaderType = %{public}u", shaderType);
3847     int64_t precisionType;
3848     tie(succ, precisionType) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64();
3849     if (!succ) {
3850         return nullptr;
3851     }
3852     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat precisionType = %{public}u", precisionType);
3853     GLint range[2] { };
3854     GLint precision = 0;
3855     napi_value objShaderPrecisionFormat = NClass::InstantiateClass(env, WebGLShaderPrecisionFormat::className, {});
3856     if (!objShaderPrecisionFormat) {
3857         LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat objShaderPrecisionFormat = %{public}u",
3858              objShaderPrecisionFormat);
3859         return nullptr;
3860     }
3861     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat objShaderPrecisionFormat = %{public}u",
3862          objShaderPrecisionFormat);
3863     auto webGLShaderPrecisionFormat = NClass::GetEntityOf<WebGLShaderPrecisionFormat>(env, objShaderPrecisionFormat);
3864     if (!webGLShaderPrecisionFormat) {
3865         return nullptr;
3866     }
3867     LOGI("WebGL WebGLRenderContext::getShaderPrecisionFormat webGLShaderPrecisionFormat = %{public}u",
3868          webGLShaderPrecisionFormat);
3869     glGetShaderPrecisionFormat(static_cast<GLenum>(shaderType), static_cast<GLenum>(precisionType), range, &precision);
3870     webGLShaderPrecisionFormat->SetShaderPrecisionFormatRangeMin(range[0]);
3871     webGLShaderPrecisionFormat->SetShaderPrecisionFormatRangeMax(range[1]);
3872     webGLShaderPrecisionFormat->SetShaderPrecisionFormatPrecision(precision);
3873     LOGI("WebGL getShaderPrecisionFormat end");
3874     return objShaderPrecisionFormat;
3875 }
3876 
GetShaderInfoLog(napi_env env,napi_callback_info info)3877 napi_value WebGLRenderingContextBase::GetShaderInfoLog(napi_env env, napi_callback_info info)
3878 {
3879     NFuncArg funcArg(env, info);
3880     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3881         return NVal::CreateNull(env).val_;
3882     }
3883     LOGI("WebGL getShaderInfoLog start");
3884     if (funcArg[NARG_POS::FIRST] == nullptr) {
3885         return NVal::CreateNull(env).val_;
3886     }
3887     WebGLShader *webGlShader = nullptr;
3888     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlShader);
3889     if (shaderStatus != napi_ok) {
3890         return NVal::CreateNull(env).val_;
3891     }
3892     int shaderId = webGlShader->GetShaderId();
3893     LOGI("WebGL WebGLRenderContext::getShaderInfoLog shaderId = %{public}u", shaderId);
3894     GLint length = 0;
3895     glGetShaderiv(static_cast<GLuint>(shaderId), GL_INFO_LOG_LENGTH, &length);
3896     GLsizei size = 0;
3897     std::unique_ptr<char[]> buf = std::make_unique<char[]>(length);
3898     glGetShaderInfoLog(shaderId, length, &size, buf.get());
3899     string str = buf.get();
3900     LOGI("WebGL getShaderInfoLog end");
3901     return NVal::CreateUTF8String(env, str).val_;
3902 }
3903 
GetProgramInfoLog(napi_env env,napi_callback_info info)3904 napi_value WebGLRenderingContextBase::GetProgramInfoLog(napi_env env, napi_callback_info info)
3905 {
3906     NFuncArg funcArg(env, info);
3907     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3908         return nullptr;
3909     }
3910     LOGI("WebGL getProgramInfoLog start");
3911     if (funcArg[NARG_POS::FIRST] == nullptr) {
3912         return nullptr;
3913     }
3914     WebGLProgram *webGlProgram = nullptr;
3915     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlProgram);
3916     if (programStatus != napi_ok) {
3917         return nullptr;
3918     }
3919     int programId = webGlProgram->GetProgramId();
3920     LOGI("WebGL WebGLRenderContext::getProgramInfoLog programId = %{public}u", programId);
3921     GLint length = 0;
3922     GLsizei size = 0;
3923     glGetProgramiv(static_cast<GLuint>(programId), GL_INFO_LOG_LENGTH, &length);
3924     std::unique_ptr<char[]> buf = std::make_unique<char[]>(length);
3925     LOGI("WebGL WebGLRenderContext::getProgramInfoLog bufSize = %{public}u", length);
3926     if (buf == nullptr) {
3927         return nullptr;
3928     }
3929     glGetProgramInfoLog(programId, length, &size, buf.get());
3930     string str = buf.get();
3931     LOGI("WebGL getProgramInfoLog end");
3932     return NVal::CreateUTF8String(env, str).val_;
3933 }
3934 
GetShaderSource(napi_env env,napi_callback_info info)3935 napi_value WebGLRenderingContextBase::GetShaderSource(napi_env env, napi_callback_info info)
3936 {
3937     NFuncArg funcArg(env, info);
3938     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
3939         return nullptr;
3940     }
3941     LOGI("WebGL getShaderSource start");
3942     if (funcArg[NARG_POS::FIRST] == nullptr) {
3943         return nullptr;
3944     }
3945     WebGLShader *webGlShader = nullptr;
3946     napi_status shaderStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **)&webGlShader);
3947     if (shaderStatus != napi_ok) {
3948         return nullptr;
3949     }
3950     GLuint shaderId = static_cast<GLuint>(webGlShader->GetShaderId());
3951     LOGI("WebGL WebGLRenderContext::getShaderSource shaderId = %{public}u", shaderId);
3952     auto objects = ObjectSource::GetInstance().GetObjectMap();
3953     auto it = objects.find(shaderId);
3954     if (it == objects.end()) {
3955         return NVal::CreateNull(env).val_;
3956     }
3957     LOGI("WebGL getShaderSource end");
3958     return NVal::CreateUTF8String(env, it->second).val_;
3959 }
3960 
GetUniform(napi_env env,napi_callback_info info)3961 napi_value WebGLRenderingContextBase::GetUniform(napi_env env, napi_callback_info info)
3962 {
3963     NFuncArg funcArg(env, info);
3964 
3965     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
3966         return nullptr;
3967     }
3968     LOGI("WebGL getUniform start");
3969     if (funcArg[NARG_POS::FIRST] == nullptr || funcArg[NARG_POS::SECOND] == nullptr) {
3970         return nullptr;
3971     }
3972     WebGLProgram *webGlProgram = nullptr;
3973     napi_value a = funcArg[NARG_POS::FIRST];
3974     LOGI("WebGL WebGLRenderContext::getUniform a = %{public}d", a);
3975     napi_status programStatus = napi_unwrap(env, funcArg[NARG_POS::FIRST], (void **) &webGlProgram);
3976     if (programStatus != napi_ok) {
3977         return nullptr;
3978     }
3979     int programId = webGlProgram->GetProgramId();
3980     LOGI("WebGL WebGLRenderContext::getUniform programId = %{public}d", programId);
3981 
3982     WebGLUniformLocation *webGLUniformLocation = nullptr;
3983     napi_status locationStatus = napi_unwrap(env, funcArg[NARG_POS::SECOND], (void **) &webGLUniformLocation);
3984     if (locationStatus != napi_ok) {
3985         return nullptr;
3986     }
3987     int locationId = webGLUniformLocation->GetUniformLocationId();
3988     LOGI("WebGL WebGLRenderContext::getUniform locationId = %{public}d", locationId);
3989     GLint maxNameLength = -1;
3990     glGetProgramiv(programId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength);
3991     LOGI("WebGL WebGLRenderContext::getUniform maxNameLength = %{public}d", maxNameLength);
3992     if (maxNameLength <= 0) {
3993         return nullptr;
3994     }
3995     GLint activeUniforms = 0;
3996     glGetProgramiv(programId, GL_ACTIVE_UNIFORMS, &activeUniforms);
3997     LOGI("WebGL WebGLRenderContext::getUniform activeUniforms = %{public}d", activeUniforms);
3998     for (GLint i = 0; i < activeUniforms; i++) {
3999         char name_ptr[maxNameLength];
4000         GLsizei name_length = 0;
4001         GLint size = -1;
4002         GLenum type = 0;
4003         glGetActiveUniform(programId, i, maxNameLength, &name_length, &size, &type,
4004                            reinterpret_cast<GLchar *>(name_ptr));
4005         LOGI("WebGL WebGLRenderContext::getUniform type = %{public}u", type);
4006         if (type == GL_INT || type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
4007             LOGI("WebGL getUniform1 end");
4008             GLint params;
4009             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId), &params);
4010             int64_t res = static_cast<int64_t>(params);
4011             LOGI("WebGL getUniform end");
4012             return NVal::CreateInt64(env, res).val_;
4013         } else if (type == GL_FLOAT) {
4014             LOGI("WebGL getUniform2 end");
4015             GLfloat params;
4016             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId), &params);
4017             float res = static_cast<float>(params);
4018             LOGI("WebGL getUniform end");
4019             return NVal::CreateDouble(env, (double) res).val_;
4020         } else if (type == GL_FLOAT_VEC2) {
4021             LOGI("WebGL getUniform3 end");
4022             static float res[2] = {};
4023             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4024                            reinterpret_cast<GLfloat *>(res));
4025             napi_value outputBuffer = nullptr;
4026             napi_create_external_arraybuffer(env, res, sizeof(res),
4027                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4028                                              NULL, &outputBuffer);
4029             napi_value outputArray = nullptr;
4030             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4031                                    outputBuffer, 0, &outputArray);
4032             LOGI("WebGL getUniform end");
4033             return outputArray;
4034         } else if (type == GL_FLOAT_VEC3) {
4035             LOGI("WebGL getUniform4 end");
4036             static float res[3] = {};
4037             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4038                            reinterpret_cast<GLfloat *>(res));
4039             napi_value outputBuffer = nullptr;
4040             napi_create_external_arraybuffer(env, res, sizeof(res),
4041                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4042                                              NULL, &outputBuffer);
4043             napi_value outputArray = nullptr;
4044             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4045                                    outputBuffer, 0, &outputArray);
4046             LOGI("WebGL getUniform end");
4047             return outputArray;
4048         } else if (type == GL_FLOAT_VEC4 || locationId == GL_FLOAT_MAT2) {
4049             LOGI("WebGL getUniform5 end");
4050             static float res[4] = {};
4051             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4052                            reinterpret_cast<GLfloat *>(res));
4053             napi_value outputBuffer = nullptr;
4054             napi_create_external_arraybuffer(env, res, sizeof(res),
4055                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4056                                              NULL, &outputBuffer);
4057             napi_value outputArray = nullptr;
4058             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4059                                    outputBuffer, 0, &outputArray);
4060             LOGI("WebGL getUniform end");
4061             return outputArray;
4062         } else if (type == GL_FLOAT_MAT3) {
4063             LOGI("WebGL getUniform6 end");
4064             static float res[9] = {};
4065             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4066                            reinterpret_cast<GLfloat *>(res));
4067             napi_value outputBuffer = nullptr;
4068             napi_create_external_arraybuffer(env, res, sizeof(res),
4069                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4070                                              NULL, &outputBuffer);
4071             napi_value outputArray = nullptr;
4072             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4073                                    outputBuffer, 0, &outputArray);
4074             LOGI("WebGL getUniform end");
4075             return outputArray;
4076         } else if (type == GL_FLOAT_MAT4) {
4077             LOGI("WebGL getUniform7 end");
4078             static float res[16] = {};
4079             glGetUniformfv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4080                            reinterpret_cast<GLfloat *>(res));
4081             napi_value outputBuffer = nullptr;
4082             napi_create_external_arraybuffer(env, res, sizeof(res),
4083                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4084                                              NULL, &outputBuffer);
4085             napi_value outputArray = nullptr;
4086             napi_create_typedarray(env, napi_float32_array, sizeof(res) / sizeof(float),
4087                                    outputBuffer, 0, &outputArray);
4088             LOGI("WebGL getUniform end");
4089             return outputArray;
4090         } else if (type == GL_INT_VEC2) {
4091             LOGI("WebGL getUniform8 end");
4092             static int64_t res[2] = {};
4093             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4094                            reinterpret_cast<GLint *>(res));
4095             napi_value outputBuffer = nullptr;
4096             napi_create_external_arraybuffer(env, res, sizeof(res),
4097                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4098                                              NULL, &outputBuffer);
4099             napi_value outputArray = nullptr;
4100             napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
4101                                    outputBuffer, 0, &outputArray);
4102             LOGI("WebGL getUniform end");
4103             return outputArray;
4104         } else if (type == GL_INT_VEC3) {
4105             LOGI("WebGL getUniform9 end");
4106             static int64_t res[3] = {};
4107             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4108                            reinterpret_cast<GLint *>(res));
4109             napi_value outputBuffer = nullptr;
4110             napi_create_external_arraybuffer(env, res, sizeof(res),
4111                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4112                                              NULL, &outputBuffer);
4113             napi_value outputArray = nullptr;
4114             napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
4115                                    outputBuffer, 0, &outputArray);
4116             LOGI("WebGL getUniform end");
4117             return outputArray;
4118         } else if (type == GL_INT_VEC4) {
4119             LOGI("WebGL getUniform10 end");
4120             static int64_t res[4] = {};
4121             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4122                            reinterpret_cast<GLint *>(res));
4123             napi_value outputBuffer = nullptr;
4124             napi_create_external_arraybuffer(env, res, sizeof(res),
4125                                              [](napi_env env, void *finalize_data, void *finalize_hint) {},
4126                                              NULL, &outputBuffer);
4127             napi_value outputArray = nullptr;
4128             napi_create_typedarray(env, napi_int32_array, sizeof(res) / sizeof(int64_t),
4129                                    outputBuffer, 0, &outputArray);
4130             LOGI("WebGL getUniform end");
4131             return outputArray;
4132         } else if (type == GL_BOOL) {
4133             LOGI("WebGL getUniform11 end");
4134             GLint params;
4135             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId), &params);
4136             bool res = (params == GL_FALSE) ? false : true;
4137             LOGI("WebGL getUniform end");
4138             return NVal::CreateBool(env, res).val_;
4139         } else if (type == GL_BOOL_VEC2) {
4140             LOGI("WebGL getUniform12 end");
4141             GLint params[2] = {0};
4142             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4143                            static_cast<GLint *>(params));
4144             uint32_t i;
4145             uint32_t length = 2;
4146             napi_value res = nullptr;
4147             napi_create_array(env, &res);
4148             for (i = 0; i < length; i++) {
4149                 bool a = static_cast<bool>(params[i]);
4150                 napi_value result = nullptr;
4151                 napi_status status = napi_get_boolean(env, a, &result);
4152                 if (status != napi_ok) {
4153                     return nullptr;
4154                 }
4155                 napi_set_element(env, res, i, result);
4156             }
4157             LOGI("WebGL getUniform end");
4158             return res;
4159         } else if (type == GL_BOOL_VEC4) {
4160             LOGI("WebGL getUniform13 end");
4161             LOGI("WebGL getUniform end");
4162             GLint params[4] = {0};
4163             glGetUniformiv(static_cast<GLuint>(programId), static_cast<GLint>(locationId),
4164                            static_cast<GLint *>(params));
4165             uint32_t i;
4166             uint32_t length = 4;
4167             napi_value res = nullptr;
4168             napi_create_array(env, &res);
4169             for (i = 0; i < length; i++) {
4170                 bool a = static_cast<bool>(params[i]);
4171                 napi_value result = nullptr;
4172                 napi_status status = napi_get_boolean(env, a, &result);
4173                 if (status != napi_ok) {
4174                     return nullptr;
4175                 }
4176                 napi_set_element(env, res, i, result);
4177             }
4178             LOGI("WebGL getUniform end");
4179             return res;
4180         } else {
4181             LOGI("WebGL getUniform end");
4182             return nullptr;
4183         }
4184     }
4185     return nullptr;
4186 }
4187 } // namespace Rosen
4188 } // namespace OHOS
4189 
4190 #ifdef __cplusplus
4191 }
4192 #endif
4193