1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "webgl/webgl_texture.h"
17 
18 #include "context/webgl2_rendering_context_base.h"
19 #include "context/webgl_rendering_context_base.h"
20 #include "napi/n_class.h"
21 #include "napi/n_func_arg.h"
22 #include "util/util.h"
23 #include "webgl/webgl_arg.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 using namespace std;
28 
Constructor(napi_env env,napi_callback_info info)29 napi_value WebGLTexture::Constructor(napi_env env, napi_callback_info info)
30 {
31     NFuncArg funcArg(env, info);
32     if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
33         return NVal::CreateNull(env).val_;
34     }
35 
36     unique_ptr<WebGLTexture> webGlTexture = make_unique<WebGLTexture>();
37     if (!NClass::SetEntityFor<WebGLTexture>(env, funcArg.GetThisVar(), move(webGlTexture))) {
38         return NVal::CreateNull(env).val_;
39     }
40     return funcArg.GetThisVar();
41 }
42 
Export(napi_env env,napi_value exports)43 bool WebGLTexture::Export(napi_env env, napi_value exports)
44 {
45     vector<napi_property_descriptor> props = {};
46 
47     string className = GetClassName();
48     bool succ = false;
49     napi_value clas = nullptr;
50     tie(succ, clas) = NClass::DefineClass(exports_.env_, className, WebGLTexture::Constructor, std::move(props));
51     if (!succ) {
52         return false;
53     }
54     succ = NClass::SaveClass(exports_.env_, className, clas);
55     if (!succ) {
56         return false;
57     }
58 
59     return exports_.AddProp(className, clas);
60 }
61 
GetClassName()62 string WebGLTexture::GetClassName()
63 {
64     return WebGLTexture::className;
65 }
66 
ComputeTextureLevel(int64_t width,int64_t height,int64_t depth)67 int64_t WebGLTexture::ComputeTextureLevel(int64_t width, int64_t height, int64_t depth)
68 {
69     int64_t max = std::max(std::max(width, height), depth);
70     if (max <= 0) {
71         return 0;
72     }
73     double result = log2(max);
74     return static_cast<int64_t>(ceil(result));
75 }
76 
GetMaxTextureLevelForTarget(GLenum target,bool highWebGL)77 GLint WebGLTexture::GetMaxTextureLevelForTarget(GLenum target, bool highWebGL)
78 {
79     static GLint maxTextureLevel = 0;
80     static GLint maxCubeMapTextureLevel = 0;
81     static GLint max3DTextureLevel = 0;
82     GLint max = 0;
83     switch (target) {
84         case GL_TEXTURE_2D_ARRAY: // webgl 2
85             if (!highWebGL) {
86                 break;
87             }
88             [[fallthrough]];
89         case GL_TEXTURE_2D: {
90             if (maxTextureLevel != 0) {
91                 return maxTextureLevel;
92             }
93             glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
94             maxTextureLevel = ComputeTextureLevel(max, max, 1);
95             return maxTextureLevel;
96         }
97         case GL_TEXTURE_CUBE_MAP:
98         case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
99         case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
100         case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
101         case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
102         case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
103         case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: {
104             if (maxCubeMapTextureLevel != 0) {
105                 return maxCubeMapTextureLevel;
106             }
107             glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &max);
108             maxCubeMapTextureLevel = ComputeTextureLevel(max, max, 1);
109             return maxCubeMapTextureLevel;
110         }
111         case GL_TEXTURE_3D: {
112             if (!highWebGL) {
113                 break;
114             }
115             if (max3DTextureLevel != 0) {
116                 return max3DTextureLevel;
117             }
118             glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max);
119             max3DTextureLevel = ComputeTextureLevel(max, max, 1);
120             return max3DTextureLevel;
121         }
122         default:
123             break;
124     }
125     return 0;
126 }
127 
~WebGLTexture()128 WebGLTexture::~WebGLTexture()
129 {
130     textureLevelInfos_.clear();
131 }
132 
SetTarget(GLenum target)133 bool WebGLTexture::SetTarget(GLenum target)
134 {
135     if (target_) {
136         return true;
137     }
138     target_ = target;
139     int32_t maxTextureLevelCtrl = 1;
140     switch (target) {
141         case GL_TEXTURE_2D:
142         case GL_TEXTURE_2D_ARRAY:
143         case GL_TEXTURE_3D: {
144             target_ = target;
145             maxTextureLevelCtrl = 1;
146             break;
147         }
148         case GL_TEXTURE_CUBE_MAP: {
149             target_ = target;
150             maxTextureLevelCtrl = 6; // 6 for GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
151             break;
152         }
153         default:
154             return false;
155     }
156     textureLevelInfos_.resize(maxTextureLevelCtrl);
157     for (int32_t i = 0; i < maxTextureLevelCtrl; ++i) {
158         textureLevelInfos_[i].Init(target);
159     }
160     return true;
161 }
162 
GetTextureLevel(GLenum target,GLint level) const163 const TextureLevelInfo* WebGLTexture::GetTextureLevel(GLenum target, GLint level) const
164 {
165     int32_t index = -1;
166     switch (target_) {
167         case GL_TEXTURE_CUBE_MAP:
168             index = static_cast<int32_t>(target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
169             break;
170         case GL_TEXTURE_2D_ARRAY:
171         case GL_TEXTURE_2D:
172         case GL_TEXTURE_3D:
173             index = static_cast<int32_t>(target - target_);
174             break;
175         default:
176             break;
177     }
178     if (index < 0 || index >= static_cast<int32_t>(textureLevelInfos_.size())) {
179         return nullptr;
180     }
181     return textureLevelInfos_[index].GetTextureLevel(level);
182 }
183 
SetTextureLevel(const TexImageArg & arg)184 bool WebGLTexture::SetTextureLevel(const TexImageArg& arg)
185 {
186     TextureLevelInfo* levelInfo = const_cast<TextureLevelInfo*>(GetTextureLevel(arg.target, arg.level));
187     if (levelInfo) {
188         levelInfo->valid = true;
189         levelInfo->internalFormat = arg.internalFormat;
190         levelInfo->width = arg.width;
191         levelInfo->height = arg.height;
192         levelInfo->depth = arg.depth;
193         levelInfo->type = arg.type;
194         levelInfo->Dump("SetTextureLevel", arg.target, arg.level);
195     }
196     return true;
197 }
198 
SetTexStorageInfo(const TexStorageArg * arg)199 bool WebGLTexture::SetTexStorageInfo(const TexStorageArg* arg)
200 {
201     GLenum type = GetTypeFromInternalFormat(arg->internalFormat);
202     if (type == GL_NONE) {
203         return false;
204     }
205 
206     for (size_t i = 0; i < textureLevelInfos_.size(); ++i) {
207         GLsizei levelWidth = arg->width;
208         GLsizei levelHeight = arg->height;
209         GLsizei levelDepth = arg->depth;
210         for (GLint level = 0; level < arg->levels; ++level) {
211             TextureLevelInfo* levelInfo = const_cast<TextureLevelInfo*>(GetTextureLevel(arg->target, level));
212             if (levelInfo != nullptr) {
213                 levelInfo->internalFormat = arg->internalFormat;
214                 levelInfo->width = levelWidth;
215                 levelInfo->height = levelHeight;
216                 levelInfo->depth = levelDepth;
217                 levelInfo->type = type;
218                 levelInfo->valid = true;
219                 levelInfo->Dump("SetTexStorageInfo", arg->target, level);
220             }
221             levelWidth = std::max(1, levelWidth / 2); // 2 is half the width
222             levelHeight = std::max(1, levelHeight / 2); // 2 is half the height
223             levelDepth = std::max(1, levelDepth / 2); // 2 is half the depth
224         }
225     }
226     immutable_ = true;
227     return true;
228 }
229 
GetTypeFromInternalFormat(GLenum internalFormat)230 GLenum WebGLTexture::GetTypeFromInternalFormat(GLenum internalFormat)
231 {
232     switch (internalFormat) {
233         case GL_RGBA4:
234             return GL_UNSIGNED_SHORT_4_4_4_4;
235         case GL_RGB565:
236             return GL_UNSIGNED_SHORT_5_6_5;
237         case GL_RGB5_A1:
238             return GL_UNSIGNED_SHORT_5_5_5_1;
239         case GL_DEPTH_COMPONENT16:
240             return GL_UNSIGNED_SHORT;
241         case GL_R8:
242             return GL_UNSIGNED_BYTE;
243         case GL_R8_SNORM:
244             return GL_BYTE;
245         case GL_R16F:
246             return GL_HALF_FLOAT;
247         case GL_R32F:
248             return GL_FLOAT;
249         case GL_R8UI:
250             return GL_UNSIGNED_BYTE;
251         case GL_R8I:
252             return GL_BYTE;
253         case GL_R16UI:
254             return GL_UNSIGNED_SHORT;
255         case GL_R16I:
256             return GL_SHORT;
257         case GL_R32UI:
258             return GL_UNSIGNED_INT;
259         case GL_R32I:
260             return GL_INT;
261         case GL_RG8:
262             return GL_UNSIGNED_BYTE;
263         case GL_RG8_SNORM:
264             return GL_BYTE;
265         case GL_RG16F:
266             return GL_HALF_FLOAT;
267         case GL_RG32F:
268             return GL_FLOAT;
269         case GL_RG8UI:
270             return GL_UNSIGNED_BYTE;
271         case GL_RG8I:
272             return GL_BYTE;
273         case GL_RG16UI:
274             return GL_UNSIGNED_SHORT;
275         case GL_RG16I:
276             return GL_SHORT;
277         case GL_RG32UI:
278             return GL_UNSIGNED_INT;
279         case GL_RG32I:
280             return GL_INT;
281         case GL_RGB8:
282             return GL_UNSIGNED_BYTE;
283         case GL_SRGB8:
284             return GL_UNSIGNED_BYTE;
285         case GL_RGB8_SNORM:
286             return GL_BYTE;
287         case GL_R11F_G11F_B10F:
288             return GL_UNSIGNED_INT_10F_11F_11F_REV;
289         case GL_RGB9_E5:
290             return GL_UNSIGNED_INT_5_9_9_9_REV;
291         case GL_RGB16F:
292             return GL_HALF_FLOAT;
293         case GL_RGB32F:
294             return GL_FLOAT;
295         case GL_RGB8UI:
296             return GL_UNSIGNED_BYTE;
297         case GL_RGB8I:
298             return GL_BYTE;
299         case GL_RGB16UI:
300             return GL_UNSIGNED_SHORT;
301         case GL_RGB16I:
302             return GL_SHORT;
303         case GL_RGB32UI:
304             return GL_UNSIGNED_INT;
305         case GL_RGB32I:
306             return GL_INT;
307         case GL_RGBA8:
308             return GL_UNSIGNED_BYTE;
309         case GL_SRGB8_ALPHA8:
310             return GL_UNSIGNED_BYTE;
311         case GL_RGBA8_SNORM:
312             return GL_BYTE;
313         case GL_RGB10_A2:
314             return GL_UNSIGNED_INT_2_10_10_10_REV;
315         case GL_RGBA16F:
316             return GL_HALF_FLOAT;
317         case GL_RGBA32F:
318             return GL_FLOAT;
319         case GL_RGBA8UI:
320             return GL_UNSIGNED_BYTE;
321         case GL_RGBA8I:
322             return GL_BYTE;
323         case GL_RGB10_A2UI:
324             return GL_UNSIGNED_INT_2_10_10_10_REV;
325         case GL_RGBA16UI:
326             return GL_UNSIGNED_SHORT;
327         case GL_RGBA16I:
328             return GL_SHORT;
329         case GL_RGBA32I:
330             return GL_INT;
331         case GL_RGBA32UI:
332             return GL_UNSIGNED_INT;
333         case GL_DEPTH_COMPONENT24:
334             return GL_UNSIGNED_INT;
335         case GL_DEPTH_COMPONENT32F:
336             return GL_FLOAT;
337         case GL_DEPTH24_STENCIL8:
338             return GL_UNSIGNED_INT_24_8;
339         case GL_DEPTH32F_STENCIL8:
340             return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
341         // Compressed types.
342         case GL_ATC_RGB_AMD:
343         case GL_ATC_RGBA_EXPLICIT_ALPHA_AMD:
344         case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD:
345         case GL_ETC1_RGB8_OES:
346         case GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
347         case GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
348         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
349         case GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
350         case GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
351         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
352         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
353         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
354             return GL_UNSIGNED_BYTE;
355         default:
356             return GL_NONE;
357     }
358 }
359 
GetInternalFormat(GLenum target,GLint level) const360 GLenum WebGLTexture::GetInternalFormat(GLenum target, GLint level) const
361 {
362     const TextureLevelInfo* levelInfo = GetTextureLevel(target, level);
363     if (levelInfo) {
364         return levelInfo->internalFormat;
365     }
366     return 0;
367 }
368 
GetType(GLenum target,GLint level) const369 GLenum WebGLTexture::GetType(GLenum target, GLint level) const
370 {
371     const TextureLevelInfo* levelInfo = GetTextureLevel(target, level);
372     if (levelInfo) {
373         return levelInfo->type;
374     }
375     return 0;
376 }
377 
GetWidth(GLenum target,GLint level) const378 GLsizei WebGLTexture::GetWidth(GLenum target, GLint level) const
379 {
380     const TextureLevelInfo* levelInfo = GetTextureLevel(target, level);
381     if (levelInfo) {
382         return levelInfo->width;
383     }
384     return 0;
385 }
386 
GetHeight(GLenum target,GLint level) const387 GLsizei WebGLTexture::GetHeight(GLenum target, GLint level) const
388 {
389     const TextureLevelInfo* levelInfo = GetTextureLevel(target, level);
390     if (levelInfo) {
391         return levelInfo->height;
392     }
393     return 0;
394 }
395 
GetDepth(GLenum target,GLint level) const396 GLsizei WebGLTexture::GetDepth(GLenum target, GLint level) const
397 {
398     const TextureLevelInfo* levelInfo = GetTextureLevel(target, level);
399     if (levelInfo) {
400         return levelInfo->depth;
401     }
402     return 0;
403 }
404 
CheckValid(GLenum target,GLint level) const405 bool WebGLTexture::CheckValid(GLenum target, GLint level) const
406 {
407     const TextureLevelInfo* levelInfo = GetTextureLevel(target, level);
408     if (levelInfo) {
409         levelInfo->Dump("CheckValid", target, level);
410         return levelInfo->valid;
411     }
412     return 0;
413 }
414 
Init(GLenum target)415 void TextureLevelCtrl::Init(GLenum target)
416 {
417     GLint maxLevel = WebGLTexture::GetMaxTextureLevelForTarget(target, true);
418     if (maxLevel > 0) {
419         textureInfos_.resize(maxLevel);
420     }
421 }
422 
CheckTextureSize(GLsizei offset,GLsizei w,GLsizei real)423 bool WebGLTexture::CheckTextureSize(GLsizei offset, GLsizei w, GLsizei real)
424 {
425     if (offset < 0 || w < 0) {
426         return false;
427     }
428 
429     if (WebGLArg::CheckOverflow<GLsizei, GLsizei>(offset, w)) {
430         return false;
431     }
432     if ((offset + w) > real) {
433         return false;
434     }
435     return true;
436 }
437 
GetSupportInternalFormatGroup1()438 const std::vector<GLenum>& WebGLTexture::GetSupportInternalFormatGroup1()
439 {
440     const static std::vector<GLenum> support1 = { WebGL2RenderingContextBase::R8UI, WebGL2RenderingContextBase::R8I,
441         WebGL2RenderingContextBase::R16UI, WebGL2RenderingContextBase::R16I, WebGL2RenderingContextBase::R32UI,
442         WebGL2RenderingContextBase::R32I, WebGL2RenderingContextBase::RG8UI, WebGL2RenderingContextBase::RG8I,
443         WebGL2RenderingContextBase::RG16UI, WebGL2RenderingContextBase::RG16I, WebGL2RenderingContextBase::RG32UI,
444         WebGL2RenderingContextBase::RG32I, WebGL2RenderingContextBase::RGBA8UI, WebGL2RenderingContextBase::RGBA8I,
445         WebGL2RenderingContextBase::RGB10_A2UI, WebGL2RenderingContextBase::RGBA16UI,
446         WebGL2RenderingContextBase::RGBA16I, WebGL2RenderingContextBase::RGBA32I,
447         WebGL2RenderingContextBase::RGBA32UI };
448     return support1;
449 }
450 
GetSupportInternalFormatGroup2()451 const std::vector<GLenum>& WebGLTexture::GetSupportInternalFormatGroup2()
452 {
453     const static std::vector<GLenum> support2 = { WebGL2RenderingContextBase::R8, WebGL2RenderingContextBase::RG8,
454         WebGL2RenderingContextBase::RGB8, WebGLRenderingContextBase::RGB565, WebGL2RenderingContextBase::RGBA8,
455         WebGL2RenderingContextBase::SRGB8_ALPHA8, WebGLRenderingContextBase::RGB5_A1, WebGLRenderingContextBase::RGBA4,
456         WebGL2RenderingContextBase::RGB10_A2, WebGLRenderingContextBase::DEPTH_COMPONENT16,
457         WebGL2RenderingContextBase::DEPTH_COMPONENT24, WebGL2RenderingContextBase::DEPTH_COMPONENT32F,
458         WebGL2RenderingContextBase::DEPTH24_STENCIL8, WebGL2RenderingContextBase::DEPTH32F_STENCIL8,
459         WebGLRenderingContextBase::STENCIL_INDEX8 };
460     return support2;
461 }
462 
Dump(const std::string & info,GLenum target,GLint level) const463 void TextureLevelInfo::Dump(const std::string& info, GLenum target, GLint level) const
464 {
465     LOGD("%{public}s [%{public}u %{public}d] %{public}u %{public}u [%{public}d %{public}d %{public}d]", info.c_str(),
466         target, level, internalFormat, type, width, height, depth);
467 }
468 
GetSupportedInternalFormats()469 const std::vector<GLenum>& WebGLTexture::GetSupportedInternalFormats()
470 {
471     static std::vector<GLenum> supportedInternalFormats = {
472         GL_R8,
473         GL_R8_SNORM,
474         GL_R16F,
475         GL_R32F,
476         GL_R8UI,
477         GL_R8I,
478         GL_R16UI,
479         GL_R16I,
480         GL_R32UI,
481         GL_R32I,
482         GL_RG8,
483         GL_RG8_SNORM,
484         GL_RG16F,
485         GL_RG32F,
486         GL_RG8UI,
487         GL_RG8I,
488         GL_RG16UI,
489         GL_RG16I,
490         GL_RG32UI,
491         GL_RG32I,
492         GL_RGB8,
493         GL_SRGB8,
494         GL_RGB565,
495         GL_RGB8_SNORM,
496         GL_R11F_G11F_B10F,
497         GL_RGB9_E5,
498         GL_RGB16F,
499         GL_RGB32F,
500         GL_RGB8UI,
501         GL_RGB8I,
502         GL_RGB16UI,
503         GL_RGB16I,
504         GL_RGB32UI,
505         GL_RGB32I,
506         GL_RGBA8,
507         GL_SRGB8_ALPHA8,
508         GL_RGBA8_SNORM,
509         GL_RGB5_A1,
510         GL_RGBA4,
511         GL_RGB10_A2,
512         GL_RGBA16F,
513         GL_RGBA32F,
514         GL_RGBA8UI,
515         GL_RGBA8I,
516         GL_RGB10_A2UI,
517         GL_RGBA16UI,
518         GL_RGBA16I,
519         GL_RGBA32I,
520         GL_RGBA32UI,
521         GL_DEPTH_COMPONENT16,
522         GL_DEPTH_COMPONENT24,
523         GL_DEPTH_COMPONENT32F,
524         GL_DEPTH24_STENCIL8,
525     };
526     // for es2
527     supportedInternalFormats.emplace_back(GL_RGB);
528     supportedInternalFormats.emplace_back(GL_RGBA);
529     supportedInternalFormats.emplace_back(GL_LUMINANCE_ALPHA);
530     supportedInternalFormats.emplace_back(GL_LUMINANCE);
531     supportedInternalFormats.emplace_back(GL_ALPHA);
532     return supportedInternalFormats;
533 }
534 
GetSupportedFormats()535 const std::vector<GLenum>& WebGLTexture::GetSupportedFormats()
536 {
537     static std::vector<GLenum> supportedFormats = { GL_RED, GL_RED_INTEGER, GL_RG, GL_RG_INTEGER, GL_RGB,
538         GL_RGB_INTEGER, GL_RGBA, GL_RGBA_INTEGER, GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL };
539     // for es2
540     supportedFormats.emplace_back(GL_LUMINANCE_ALPHA);
541     supportedFormats.emplace_back(GL_LUMINANCE);
542     supportedFormats.emplace_back(GL_ALPHA);
543     return supportedFormats;
544 }
545 
GetSupportedTypes()546 const std::vector<GLenum>& WebGLTexture::GetSupportedTypes()
547 {
548     static std::vector<GLenum> supportedTypes = {
549         GL_BYTE,
550         GL_UNSIGNED_SHORT,
551         GL_SHORT,
552         GL_UNSIGNED_INT,
553         GL_INT,
554         GL_HALF_FLOAT,
555         GL_HALF_FLOAT_OES,
556         GL_FLOAT,
557         GL_UNSIGNED_INT_2_10_10_10_REV,
558         GL_UNSIGNED_INT_10F_11F_11F_REV,
559         GL_UNSIGNED_INT_5_9_9_9_REV,
560         GL_UNSIGNED_INT_24_8,
561     };
562 
563     // for es2
564     supportedTypes.emplace_back(GL_UNSIGNED_BYTE);
565     supportedTypes.emplace_back(GL_UNSIGNED_SHORT_5_6_5);
566     supportedTypes.emplace_back(GL_UNSIGNED_SHORT_4_4_4_4);
567     supportedTypes.emplace_back(GL_UNSIGNED_SHORT_5_5_5_1);
568     return supportedTypes;
569 }
570 
ChangeToBufferDataType(GLenum type)571 BufferDataType WebGLTexture::ChangeToBufferDataType(GLenum type)
572 {
573     switch (type) {
574         case GL_BYTE:
575             return BUFFER_DATA_INT_8;
576         case GL_UNSIGNED_BYTE:
577             return BUFFER_DATA_UINT_8;
578         case GL_SHORT:
579             return BUFFER_DATA_INT_16;
580         case GL_UNSIGNED_SHORT:
581         case GL_UNSIGNED_SHORT_5_6_5:
582         case GL_UNSIGNED_SHORT_4_4_4_4:
583         case GL_UNSIGNED_SHORT_5_5_5_1:
584             return BUFFER_DATA_UINT_16;
585         case GL_INT:
586             return BUFFER_DATA_INT_32;
587         case GL_UNSIGNED_INT:
588         case GL_UNSIGNED_INT_2_10_10_10_REV:
589         case GL_UNSIGNED_INT_10F_11F_11F_REV:
590         case GL_UNSIGNED_INT_5_9_9_9_REV:
591         case GL_UNSIGNED_INT_24_8:
592             return BUFFER_DATA_UINT_32;
593         case GL_FLOAT: // OES_texture_float
594             return BUFFER_DATA_FLOAT_32;
595         case GL_HALF_FLOAT:
596         case GL_HALF_FLOAT_OES:
597             return BUFFER_DATA_UINT_16;
598         default:
599             break;
600     }
601     return BUFFER_DATA_INVALID;
602 }
603 
GetSupportedFormatTypeMaps()604 const std::set<TextureFormatTypeMap, TextureFormatTypeMapCompare>& WebGLTexture::GetSupportedFormatTypeMaps()
605 {
606     static std::set<TextureFormatTypeMap, TextureFormatTypeMapCompare> supportedFormatTypeMaps = {
607         { GL_R8, GL_RED, GL_UNSIGNED_BYTE },
608         { GL_R8_SNORM, GL_RED, GL_BYTE },
609         { GL_R16F, GL_RED, GL_HALF_FLOAT },
610         { GL_R16F, GL_RED, GL_FLOAT },
611         { GL_R32F, GL_RED, GL_FLOAT },
612         { GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE },
613         { GL_R8I, GL_RED_INTEGER, GL_BYTE },
614         { GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT },
615         { GL_R16I, GL_RED_INTEGER, GL_SHORT },
616         { GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT },
617         { GL_R32I, GL_RED_INTEGER, GL_INT },
618         { GL_RG8, GL_RG, GL_UNSIGNED_BYTE },
619         { GL_RG8_SNORM, GL_RG, GL_BYTE },
620         { GL_RG16F, GL_RG, GL_HALF_FLOAT },
621         { GL_RG16F, GL_RG, GL_FLOAT },
622         { GL_RG32F, GL_RG, GL_FLOAT },
623         { GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE },
624         { GL_RG8I, GL_RG_INTEGER, GL_BYTE },
625         { GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT },
626         { GL_RG16I, GL_RG_INTEGER, GL_SHORT },
627         { GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT },
628         { GL_RG32I, GL_RG_INTEGER, GL_INT },
629         { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE },
630         { GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE },
631         { GL_RGB565, GL_RGB, GL_UNSIGNED_BYTE },
632         { GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 },
633         { GL_RGB8_SNORM, GL_RGB, GL_BYTE },
634         { GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV },
635         { GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT },
636         { GL_R11F_G11F_B10F, GL_RGB, GL_FLOAT },
637         { GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV },
638         { GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT },
639         { GL_RGB9_E5, GL_RGB, GL_FLOAT },
640         { GL_RGB16F, GL_RGB, GL_HALF_FLOAT },
641         { GL_RGB16F, GL_RGB, GL_FLOAT },
642         { GL_RGB32F, GL_RGB, GL_FLOAT },
643         { GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE },
644         { GL_RGB8I, GL_RGB_INTEGER, GL_BYTE },
645         { GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT },
646         { GL_RGB16I, GL_RGB_INTEGER, GL_SHORT },
647         { GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT },
648         { GL_RGB32I, GL_RGB_INTEGER, GL_INT },
649         { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE },
650         { GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE },
651         { GL_RGBA8_SNORM, GL_RGBA, GL_BYTE },
652         { GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_BYTE },
653         { GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 },
654         { GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV },
655         { GL_RGBA4, GL_RGBA, GL_UNSIGNED_BYTE },
656         { GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 },
657         { GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV },
658         { GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT },
659         { GL_RGBA16F, GL_RGBA, GL_FLOAT },
660         { GL_RGBA32F, GL_RGBA, GL_FLOAT },
661         { GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE },
662         { GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE },
663         { GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV },
664         { GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT },
665         { GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT },
666         { GL_RGBA32I, GL_RGBA_INTEGER, GL_INT },
667         { GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT },
668         { GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT },
669         { GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT },
670         { GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT },
671         { GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT },
672         { GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 },
673 
674         // es2
675         { GL_RGB, GL_RGB, GL_UNSIGNED_BYTE },
676         { GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 },
677         { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE },
678         { GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 },
679         { GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 },
680         { GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE },
681         { GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE },
682         { GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE },
683     };
684     return supportedFormatTypeMaps;
685 }
686 
687 } // namespace Rosen
688 } // namespace OHOS
689