1 /*
2  * Copyright (c) 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 #include <algorithm>
16 
17 #include "context/webgl2_rendering_context_base.h"
18 #include "context/webgl_rendering_context_base.h"
19 #include "context/webgl_rendering_context_base_impl.h"
20 #include "napi/n_class.h"
21 #include "util/log.h"
22 #include "util/util.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Impl {
AddSupportElement(std::vector<GLenum> & supports,GLenum ele)27 static void AddSupportElement(std::vector<GLenum>& supports, GLenum ele)
28 {
29     if (std::find(supports.begin(), supports.end(), ele) == supports.end()) {
30         supports.emplace_back(ele);
31     }
32 }
33 
GetIntegerParaName()34 const std::vector<GLenum>& WebGLRenderingContextBaseImpl::GetIntegerParaName()
35 {
36     static std::vector<GLenum> integerParaNames = { GL_ACTIVE_TEXTURE, GL_ALPHA_BITS, GL_BLEND_DST_ALPHA,
37         GL_BLEND_DST_RGB, GL_BLEND_EQUATION_ALPHA, GL_BLEND_EQUATION_RGB, GL_BLEND_SRC_ALPHA, GL_BLEND_SRC_RGB,
38         GL_BLUE_BITS, GL_CULL_FACE_MODE, GL_DEPTH_BITS, GL_DEPTH_FUNC, GL_FRONT_FACE, GL_GENERATE_MIPMAP_HINT,
39         GL_GREEN_BITS, GL_IMPLEMENTATION_COLOR_READ_FORMAT, GL_IMPLEMENTATION_COLOR_READ_TYPE,
40         GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, GL_MAX_CUBE_MAP_TEXTURE_SIZE, GL_MAX_FRAGMENT_UNIFORM_VECTORS,
41         GL_MAX_RENDERBUFFER_SIZE, GL_MAX_TEXTURE_IMAGE_UNITS, GL_MAX_TEXTURE_SIZE, GL_MAX_VARYING_VECTORS,
42         GL_MAX_VERTEX_ATTRIBS, GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, GL_MAX_VERTEX_UNIFORM_VECTORS, GL_PACK_ALIGNMENT,
43         GL_RED_BITS, GL_SAMPLE_BUFFERS, GL_SAMPLES, GL_STENCIL_BACK_FAIL, GL_STENCIL_BACK_FUNC,
44         GL_STENCIL_BACK_PASS_DEPTH_FAIL, GL_STENCIL_BACK_PASS_DEPTH_PASS, GL_STENCIL_BACK_REF,
45         GL_STENCIL_BACK_VALUE_MASK, GL_STENCIL_BACK_WRITEMASK, GL_STENCIL_BITS, GL_STENCIL_CLEAR_VALUE, GL_STENCIL_FAIL,
46         GL_STENCIL_FUNC, GL_STENCIL_PASS_DEPTH_FAIL, GL_STENCIL_PASS_DEPTH_PASS, GL_STENCIL_REF, GL_STENCIL_VALUE_MASK,
47         GL_STENCIL_WRITEMASK, GL_SUBPIXEL_BITS, GL_UNPACK_ALIGNMENT };
48     return integerParaNames;
49 }
50 
GetBoolParaName()51 const std::vector<GLenum>& WebGLRenderingContextBaseImpl::GetBoolParaName()
52 {
53     static std::vector<GLenum> boolParaName = { GL_BLEND, GL_CULL_FACE, GL_DEPTH_TEST, GL_DEPTH_WRITEMASK, GL_DITHER,
54         GL_POLYGON_OFFSET_FILL, GL_SAMPLE_ALPHA_TO_COVERAGE, GL_SAMPLE_COVERAGE, GL_SAMPLE_COVERAGE_INVERT,
55         GL_SCISSOR_TEST, GL_STENCIL_TEST };
56     return boolParaName;
57 }
58 
GetExtentionAstcTexImageInternal()59 const std::vector<GLenum>& WebGLRenderingContextBaseImpl::GetExtentionAstcTexImageInternal()
60 {
61     static std::vector<GLenum> extentionAstcSupportInternalFormats {};
62     static bool initd = false;
63     if (!initd) {
64         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_4x4_KHR);
65         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_5x4_KHR);
66         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_5x5_KHR);
67         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_6x5_KHR);
68         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_6x6_KHR);
69         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_8x5_KHR);
70         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_8x6_KHR);
71         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_8x8_KHR);
72         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_10x5_KHR);
73         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_10x6_KHR);
74         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_10x10_KHR);
75         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_12x10_KHR);
76         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_RGBA_ASTC_12x12_KHR);
77         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR);
78         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR);
79         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR);
80         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR);
81         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR);
82         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR);
83         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR);
84         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR);
85         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR);
86         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR);
87         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR);
88         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR);
89         AddSupportElement(extentionAstcSupportInternalFormats, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR);
90         initd = true;
91     };
92     return extentionAstcSupportInternalFormats;
93 }
94 
GetTexImageInternalFormat()95 const std::vector<GLenum>& WebGLRenderingContextBaseImpl::GetTexImageInternalFormat()
96 {
97     static std::vector<GLenum> texImageSupportInternalFormats {};
98     static bool initd = false;
99     if (!initd) {
100         GLint count = 0;
101         glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &count);
102         std::unique_ptr<uint32_t[]> params = std::make_unique<uint32_t[]>(count);
103         if (params == nullptr) {
104             return texImageSupportInternalFormats;
105         }
106         glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, reinterpret_cast<GLint*>(params.get()));
107         for (GLint i = 0; i < count; i++) {
108             uint32_t data = *(params.get() + i);
109             AddSupportElement(texImageSupportInternalFormats, data);
110         }
111 #ifdef SUPPORT_COMPRESSED_RGB_S3TC
112         // When using the WEBGL_compressed_texture_s3tc extension:
113         AddSupportElement(texImageSupportInternalFormats, GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
114         AddSupportElement(texImageSupportInternalFormats, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT);
115         AddSupportElement(texImageSupportInternalFormats, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT);
116         AddSupportElement(texImageSupportInternalFormats, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
117 #endif
118         initd = true;
119     }
120     return texImageSupportInternalFormats;
121 }
122 } // namespace Impl
123 } // namespace Rosen
124 } // namespace OHOS
125