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 
16 #ifndef WEBGL_OBJECT_H
17 #define WEBGL_OBJECT_H
18 
19 #include <map>
20 #include "napi/n_exporter.h"
21 #include "napi/n_class.h"
22 #include "napi/n_func_arg.h"
23 #include "napi/n_val.h"
24 #include "util/log.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class WebGLObject {
29 public:
WebGLObject()30     WebGLObject() {};
~WebGLObject()31     ~WebGLObject() {}
32 
33     enum {
34         WEBGL_OBJECT_PROGRAM = 0,
35         WEBGL_OBJECT_SHADER,
36         WEBGL_OBJECT_BUFFER,
37         WEBGL_OBJECT_FRAME_BUFFER,
38         WEBGL_OBJECT_RENDER_BUFFER,
39         WEBGL_OBJECT_TEXTURE,
40         WEBGL_OBJECT_ACTIVE_INFO,
41         WEBGL_OBJECT_QUERY,
42         WEBGL_OBJECT_UNIFORM_LOCATION,
43         WEBGL_OBJECT_VERTEX_ARRAY,
44         WEBGL_OBJECT_SAMPLE,
45         WEBGL_OBJECT_TRANSFORM_FEEDBACK,
46         WEBGL_OBJECT_MAX,
47         WEBGL_OBJECT_SYNC,
48     };
49 
50     template<class T>
CreateObjectInstance(napi_env env,T ** instance)51     static NVal CreateObjectInstance(napi_env env, T** instance)
52     {
53         napi_value obj = NClass::InstantiateClass(env, T::className, {});
54         if (obj == nullptr) {
55             return {env, nullptr};
56         }
57         *instance = NClass::GetEntityOf<T>(env, obj);
58         if (*instance == nullptr) {
59             return {env, nullptr};
60         }
61         return NVal(env, obj);
62     }
63 
64     template<class T>
GetObjectInstance(napi_env env,napi_value obj)65     static T* GetObjectInstance(napi_env env, napi_value obj)
66     {
67         if (NVal(env, obj).IsNull()) {
68             return nullptr;
69         }
70         NExporter* webGLObj = nullptr;
71         napi_status status = napi_unwrap(env, obj, (void **)&webGLObj);
72         if (status != napi_ok) {
73             return nullptr;
74         }
75         if (webGLObj->GetClassName() == T::className) {
76             return static_cast<T *>(webGLObj);
77         }
78         LOGE("Invalid object input object %{public}s real %{public}s",
79             webGLObj->GetClassName().c_str(), T::className.c_str());
80         return nullptr;
81     }
82 };
83 } // namespace Rosen
84 } // namespace OHOS
85 #endif // WEBGL_OBJECT_H
86