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 <memory>
17 #include <vector>
18
19 #include "context/webgl_context_attributes.h"
20 #include "context/webgl_rendering_context.h"
21 #include "context/webgl2_rendering_context.h"
22 #include "napi/n_val.h"
23 #include "webgl/webgl_active_info.h"
24 #include "webgl/webgl_buffer.h"
25 #include "webgl/webgl_framebuffer.h"
26 #include "webgl/webgl_program.h"
27 #include "webgl/webgl_query.h"
28 #include "webgl/webgl_renderbuffer.h"
29 #include "webgl/webgl_sampler.h"
30 #include "webgl/webgl_sync.h"
31 #include "webgl/webgl_shader.h"
32 #include "webgl/webgl_texture.h"
33 #include "webgl/webgl_transform_feedback.h"
34 #include "webgl/webgl_uniform_location.h"
35 #include "webgl/webgl_vertex_array_object.h"
36 #include "webgl/webgl_shader_precision_format.h"
37 #include "util/log.h"
38 #include "util/object_manager.h"
39 #include "util/util.h"
40
41 using namespace std;
42
43 namespace OHOS {
44 namespace Rosen {
ExportWebGlObj(napi_env env, napi_value exports)45 static napi_value ExportWebGlObj(napi_env env, napi_value exports)
46 {
47 std::vector<unique_ptr<NExporter>> products;
48 products.emplace_back(make_unique<WebGLActiveInfo>(env, exports));
49 products.emplace_back(make_unique<WebGLBuffer>(env, exports));
50 products.emplace_back(make_unique<WebGLFramebuffer>(env, exports));
51 products.emplace_back(make_unique<WebGLProgram>(env, exports));
52 products.emplace_back(make_unique<WebGLQuery>(env, exports));
53 products.emplace_back(make_unique<WebGLRenderbuffer>(env, exports));
54 products.emplace_back(make_unique<WebGLSampler>(env, exports));
55 products.emplace_back(make_unique<WebGLSync>(env, exports));
56 products.emplace_back(make_unique<WebGLShader>(env, exports));
57 products.emplace_back(make_unique<WebGLTexture>(env, exports));
58 products.emplace_back(make_unique<WebGLTransformFeedback>(env, exports));
59 products.emplace_back(make_unique<WebGLUniformLocation>(env, exports));
60 products.emplace_back(make_unique<WebGLVertexArrayObject>(env, exports));
61 products.emplace_back(make_unique<WebGLShaderPrecisionFormat>(env, exports));
62
63 for (auto &&product : products) {
64 if (!product->Export(env, exports)) {
65 return nullptr;
66 }
67 }
68 return exports;
69 }
70
Export(napi_env env, napi_value exports)71 static napi_value Export(napi_env env, napi_value exports)
72 {
73 string idStr;
74 std::vector<std::string> vec;
75 bool succ = Util::GetContextInfo(env, exports, idStr, vec);
76 if (!succ) {
77 return nullptr;
78 }
79 size_t webglItem = vec[0].find("webgl");
80 if (webglItem == std::string::npos) {
81 LOGE("can not find webgl");
82 return nullptr;
83 }
84 string webgl2Str = vec[0].substr(webglItem, 6); // length of webgl2
85 string webgl1Str = vec[0].substr(webglItem, 5); // length of webgl
86 if (webgl2Str == "webgl2") {
87 WebGL2RenderingContext *webGl2RenderingContext =
88 static_cast<WebGL2RenderingContext *>(ObjectManager::GetInstance().GetWebGLContext(true, idStr));
89 if (webGl2RenderingContext == nullptr) {
90 webGl2RenderingContext = new (std::nothrow) WebGL2RenderingContext(env, exports);
91 if (webGl2RenderingContext == nullptr) {
92 return nullptr;
93 }
94 ObjectManager::GetInstance().AddWebGLObject(true, idStr, webGl2RenderingContext);
95 }
96 if (!webGl2RenderingContext->SetWebGLContextAttributes(vec) || !webGl2RenderingContext->Export(env, exports)) {
97 return nullptr;
98 }
99 } else if (webgl1Str == "webgl") {
100 WebGLRenderingContext *webGlRenderingContext =
101 static_cast<WebGLRenderingContext *>(ObjectManager::GetInstance().GetWebGLContext(false, idStr));
102 if (webGlRenderingContext == nullptr) {
103 webGlRenderingContext = new (std::nothrow) WebGLRenderingContext(env, exports);
104 if (webGlRenderingContext == nullptr) {
105 return nullptr;
106 }
107 ObjectManager::GetInstance().AddWebGLObject(false, idStr, webGlRenderingContext);
108 }
109 if (!webGlRenderingContext->SetWebGLContextAttributes(vec) || !webGlRenderingContext->Export(env, exports)) {
110 return nullptr;
111 }
112 } else {
113 return nullptr;
114 }
115
116 return ExportWebGlObj(env, exports);
117 }
118
119 NAPI_MODULE(libwebglnapi, Export)
120 } // namespace Rosen
121 } // namespace OHOS
122