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