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 #ifndef WEBGL_RENDERING_CONTEXT_BASIC_BASE_H
17 #define WEBGL_RENDERING_CONTEXT_BASIC_BASE_H
18 
19 #include <map>
20 
21 #include <EGL/egl.h>
22 #include <EGL/eglext.h>
23 #include <GLES2/gl2.h>
24 
25 #include "canvas_render_context_base.h"
26 #include "context/webgl_context_attributes.h"
27 #include "napi/n_class.h"
28 #include "napi/n_exporter.h"
29 #include "webgl/webgl_object.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 enum : int { WEBGL_1_0 = 0, WEBGL_2_0 };
34 
35 class WebGLRenderingContextBasicBase : public OHOS::Ace::CanvasRenderContextBase {
36 public:
WebGLRenderingContextBasicBase()37     WebGLRenderingContextBasicBase() {};
38     virtual ~WebGLRenderingContextBasicBase();
39 
40     void SetEglWindow(void* window);
41     void Create(void* context) override;
42     void Init() override;
43     void Attach(uint64_t textureId) override;
44     void Update() override;
45     void Detach() override;
46     void SetBitMapPtr(char* bitMapPtr, int bitMapWidth, int bitMapHeight) override;
47     uint64_t CreateTexture() override;
48     void SetTexture(uint64_t id);
49     void SetUpdateCallback(std::function<void()>) override;
50     bool CreateSurface();
51     bool SetWebGLContextAttributes(const std::vector<std::string>& vec);
52     napi_value GetContextInstance(napi_env env, std::string className,
53         napi_callback constructor, napi_finalize finalize_cb);
54 
GetBufferWidth()55     int GetBufferWidth() const
56     {
57         return bitMapWidth_;
58     }
59 
GetBufferHeight()60     int GetBufferHeight() const
61     {
62         return bitMapHeight_;
63     }
64 
CreateWebGlContextAttributes()65     std::shared_ptr<WebGLContextAttributes> CreateWebGlContextAttributes()
66     {
67         if (webGlContextAttributes_ == nullptr) {
68             webGlContextAttributes_ = std::make_shared<WebGLContextAttributes>();
69         }
70         return webGlContextAttributes_;
71     }
72 
SetPackAlignment(GLint packAlignment)73     void SetPackAlignment(GLint packAlignment)
74     {
75         packAlignment_ = packAlignment;
76     }
77     std::function<void()> updateCallback_;
78     napi_ref contextRef_ = nullptr;
79 private:
80     std::string GetContextAttr(const std::string& str, const std::string& key, int keyLength, int value);
81     char* bitMapPtr_ = nullptr;
82     int bitMapWidth_ = 0;
83     int bitMapHeight_ = 0;
84     GLint packAlignment_ = 4;
85     EGLSurface eglSurface_ = nullptr;
86     NativeWindow* eglWindow_ = nullptr;
87     std::shared_ptr<WebGLContextAttributes> webGlContextAttributes_ = nullptr;
88 };
89 } // namespace Rosen
90 } // namespace OHOS
91 #endif // WEBGL_RENDERING_CONTEXT_BASIC_BASE_H
92