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_FRAMEBUFFER_H
17 #define WEBGL_FRAMEBUFFER_H
18 
19 #include <map>
20 
21 #include "napi/n_exporter.h"
22 #include "webgl_object.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Impl {
27 class WebGLRenderingContextBaseImpl;
28 }
29 
30 enum AttachmentType : uint32_t {
31     RENDER_BUFFER,
32     TEXTURE,
33     INVALID_TYPE,
34 };
35 
36 struct WebGLAttachmentInfo {
37     GLsizei width = 0;
38     GLsizei height = 0;
39     GLenum format;
40     GLenum type;
41 };
42 
43 struct WebGLAttachment {
WebGLAttachmentWebGLAttachment44     WebGLAttachment(AttachmentType type, GLenum attachment, GLuint id)
45     {
46         this->type = type;
47         this->attachment = attachment;
48         this->id = id;
49     }
IsTextureWebGLAttachment50     bool IsTexture() const
51     {
52         return type == AttachmentType::TEXTURE;
53     }
IsRenderBufferWebGLAttachment54     bool IsRenderBuffer() const
55     {
56         return type == AttachmentType::RENDER_BUFFER;
57     }
58     AttachmentType type;
59     GLenum attachment;
60     GLuint id;
61 };
62 
63 struct AttachmentTexture : public WebGLAttachment {
AttachmentTextureAttachmentTexture64     AttachmentTexture(AttachmentType type, GLenum attachment, GLuint id)
65         : WebGLAttachment(type, attachment, id) {}
66     GLenum target {};
67     GLint level {};
68 };
69 
70 class WebGLFramebuffer : public NExporter, public WebGLObject {
71 public:
72     inline static const std::string className = "WebGLFramebuffer";
73     inline static const int objectType = WEBGL_OBJECT_FRAME_BUFFER;
74     inline static const int DEFAULT_FRAME_BUFFER = 0;
75 
76     bool Export(napi_env env, napi_value exports) override;
77     std::string GetClassName() override;
78     static napi_value Constructor(napi_env env, napi_callback_info info);
79     static NVal CreateObjectInstance(napi_env env, WebGLFramebuffer** instance);
WebGLFramebuffer()80     explicit WebGLFramebuffer() : framebuffer_(0) {};
WebGLFramebuffer(napi_env env,napi_value exports)81     WebGLFramebuffer(napi_env env, napi_value exports) : NExporter(env, exports), framebuffer_(0) {};
82     ~WebGLFramebuffer();
83 
SetFramebuffer(unsigned int framebuffer)84     void SetFramebuffer(unsigned int framebuffer)
85     {
86         framebuffer_ = framebuffer;
87     }
88 
GetFramebuffer()89     unsigned int GetFramebuffer() const
90     {
91         return framebuffer_;
92     }
93 
SetTarget(GLenum target)94     void SetTarget(GLenum target)
95     {
96         target_ = target;
97     }
98 
GetTarget()99     GLenum GetTarget() const
100     {
101         return target_;
102     }
103 
SetReadBufferMode(GLenum mode)104     void SetReadBufferMode(GLenum mode)
105     {
106         readBufferMode_ = mode;
107     }
108 
GetReadBufferMode()109     GLenum GetReadBufferMode() const
110     {
111         return readBufferMode_;
112     }
113 
114     bool AddAttachment(GLenum target, GLenum attachment, GLuint id);
115     bool AddAttachment(GLenum target, GLenum attachment, GLuint id, GLenum textureTarget, GLint level);
116     WebGLAttachment* GetAttachment(GLenum attachment) const;
117     void RemoveAttachment(GLenum target, GLuint id, AttachmentType type);
118     GLenum CheckStatus(napi_env env, Impl::WebGLRenderingContextBaseImpl* context) const;
119     GLenum CheckStatus(napi_env env, Impl::WebGLRenderingContextBaseImpl* context, WebGLAttachmentInfo info,
120         std::vector<WebGLAttachment*>& attachments, WebGLAttachment* attachedObject) const;
121     GLenum CheckAttachStatus(Impl::WebGLRenderingContextBaseImpl* context,
122         std::vector<WebGLAttachment*>& attachments) const;
123     bool GetWebGLAttachmentInfo(napi_env env, Impl::WebGLRenderingContextBaseImpl* context,
124         const WebGLAttachment* attachedObject, WebGLAttachmentInfo& info) const;
125 private:
126     void DoDetachment(GLenum target, WebGLAttachment* attachment);
127     bool CheckAttachmentComplete(bool isHighWebGL, WebGLAttachment* attachedObject,
128         const WebGLAttachmentInfo& info, std::vector<WebGLAttachment*>& attachments) const;
129     bool IsStencilRenderAble(GLenum internalFormat, bool includesDepthStencil) const;
130     bool IsDepthRenderAble(GLenum internalFormat, bool includesDepthStencil) const;
131     bool IsColorRenderAble(GLenum internalFormat) const;
132     void Clear();
133     std::map<GLenum, WebGLAttachment*> attachments_ {};
134     GLenum target_ { GL_NONE };
135     unsigned int framebuffer_  { 0 };
136     GLenum readBufferMode_ { GL_COLOR_ATTACHMENT0 };
137 };
138 } // namespace Rosen
139 } // namespace OHOS
140 #endif // WEBGL_FRAMEBUFFER_H
141