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_shader_precision_format.h"
17
18 #include "napi/n_class.h"
19 #include "napi/n_func_arg.h"
20 #include "napi/n_val.h"
21 #include "util/log.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 using namespace std;
26
GetObjectFromArg(napi_env env,napi_callback_info info)27 WebGLShaderPrecisionFormat* WebGLShaderPrecisionFormat::GetObjectFromArg(napi_env env, napi_callback_info info)
28 {
29 NFuncArg funcArg(env, info);
30 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
31 return nullptr;
32 }
33 if (funcArg.GetThisVar() == nullptr) {
34 LOGE("WebGLShaderPrecisionFormat::GetObjectFromArg invalid arg.");
35 return nullptr;
36 }
37 WebGLShaderPrecisionFormat* obj = nullptr;
38 napi_status status = napi_unwrap(env, funcArg.GetThisVar(), (void**)&obj);
39 if (status != napi_ok) {
40 LOGE("WebGLShade webGLShaderPrecisionFormat failed.");
41 return nullptr;
42 }
43 return obj;
44 }
45
GetShaderPrecisionFormatRangeMin(napi_env env,napi_callback_info info)46 napi_value WebGLShaderPrecisionFormat::GetShaderPrecisionFormatRangeMin(napi_env env, napi_callback_info info)
47 {
48 WebGLShaderPrecisionFormat* obj = GetObjectFromArg(env, info);
49 if (obj == nullptr) {
50 LOGE("WebGLShader getShaderPrecisionFormatRangeMin failed.");
51 return nullptr;
52 }
53 int size = obj->GetShaderPrecisionFormatRangeMin();
54 LOGD("WebGLShaderPrecisionFormat::GetShaderPrecisionFormatRangeMin %d", size);
55 return NVal::CreateInt64(env, size).val_;
56 }
57
GetShaderPrecisionFormatRangeMax(napi_env env,napi_callback_info info)58 napi_value WebGLShaderPrecisionFormat::GetShaderPrecisionFormatRangeMax(napi_env env, napi_callback_info info)
59 {
60 WebGLShaderPrecisionFormat* obj = GetObjectFromArg(env, info);
61 if (obj == nullptr) {
62 LOGE("WebGLShaderPrecisionFormat getShaderPrecisionFormatRangeMax failed.");
63 return nullptr;
64 }
65 int size = obj->GetShaderPrecisionFormatRangeMax();
66 LOGD("WebGLShaderPrecisionFormat::GetShaderPrecisionFormatRangeMax %d", size);
67 return NVal::CreateInt64(env, size).val_;
68 }
69
GetShaderPrecisionFormatPrecision(napi_env env,napi_callback_info info)70 napi_value WebGLShaderPrecisionFormat::GetShaderPrecisionFormatPrecision(napi_env env, napi_callback_info info)
71 {
72 WebGLShaderPrecisionFormat* obj = GetObjectFromArg(env, info);
73 if (obj == nullptr) {
74 LOGE("WebGLShaderPrecisionFormat getShaderPrecisionFormatPrecision failed.");
75 return nullptr;
76 }
77 int precision = obj->GetShaderPrecisionFormatPrecision();
78 LOGD("WebGLShaderPrecisionFormat::GetShaderPrecisionFormatPrecision %d", precision);
79 return NVal::CreateInt64(env, precision).val_;
80 }
81
Constructor(napi_env env,napi_callback_info info)82 napi_value WebGLShaderPrecisionFormat::Constructor(napi_env env, napi_callback_info info)
83 {
84 NFuncArg funcArg(env, info);
85 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
86 return nullptr;
87 }
88
89 unique_ptr<WebGLShaderPrecisionFormat> webGLShaderPrecisionFormat = make_unique<WebGLShaderPrecisionFormat>();
90 if (!NClass::SetEntityFor<WebGLShaderPrecisionFormat>(
91 env, funcArg.GetThisVar(), move(webGLShaderPrecisionFormat))) {
92 LOGE("WebGLShaderPrecisionFormat SetEntityFor failed.");
93 return nullptr;
94 }
95 return funcArg.GetThisVar();
96 }
97
Export(napi_env env,napi_value exports)98 bool WebGLShaderPrecisionFormat::Export(napi_env env, napi_value exports)
99 {
100 vector<napi_property_descriptor> props = {
101 NVal::DeclareNapiGetter("precision", WebGLShaderPrecisionFormat::GetShaderPrecisionFormatPrecision),
102 NVal::DeclareNapiGetter("rangeMax", WebGLShaderPrecisionFormat::GetShaderPrecisionFormatRangeMax),
103 NVal::DeclareNapiGetter("rangeMin", WebGLShaderPrecisionFormat::GetShaderPrecisionFormatRangeMin)
104 };
105
106 string className = GetClassName();
107 bool succ = false;
108 napi_value clas = nullptr;
109 tie(succ, clas) =
110 NClass::DefineClass(exports_.env_, className, WebGLShaderPrecisionFormat::Constructor, std::move(props));
111 if (!succ) {
112 LOGE("WebGLShaderPrecisionFormat defineClass failed");
113 return false;
114 }
115 succ = NClass::SaveClass(exports_.env_, className, clas);
116 if (!succ) {
117 LOGE("WebGLShaderPrecisionFormat saveClass failed");
118 return false;
119 }
120
121 return exports_.AddProp(className, clas);
122 }
123
GetClassName()124 string WebGLShaderPrecisionFormat::GetClassName()
125 {
126 return WebGLShaderPrecisionFormat::className;
127 }
128 } // namespace Rosen
129 } // namespace OHOS
130