1 /*
2  * Copyright (C) 2024 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 RENDER_TEXTURE_H
17 #define RENDER_TEXTURE_H
18 
19 #include "base/render_base.h"
20 #include "graphic/gl_utils.h"
21 #include "graphic/render_object.h"
22 #include "effect_buffer.h"
23 namespace OHOS {
24 namespace Media {
25 namespace Effect {
26 class RenderTexture : public RenderObject {
27 public:
RenderTexture(RenderContext *ctx, GLsizei w, GLsizei h, GLenum interFmt = GL_RGBA8)28     RenderTexture(RenderContext *ctx, GLsizei w, GLsizei h, GLenum interFmt = GL_RGBA8)
29     {
30         context_ = ctx;
31         internalFormat_ = interFmt;
32         width_ = w;
33         height_ = h;
34     }
35 
36     ~RenderTexture() = default;
37 
Width()38     unsigned int Width()
39     {
40         return width_;
41     }
42 
Height()43     unsigned int Height()
44     {
45         return height_;
46     }
47 
Format()48     GLenum Format()
49     {
50         return internalFormat_;
51     }
52 
SetName(unsigned int name)53     void SetName(unsigned int name)
54     {
55         name_ = name;
56     }
57 
GetName()58     unsigned int GetName()
59     {
60         return name_;
61     }
62 
63     bool Init() override
64     {
65         if (!IsReady()) {
66             name_ = GLUtils::CreateTexture2D(width_, height_, 1, internalFormat_, GL_LINEAR, GL_LINEAR,
67                 GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
68             SetReady(true);
69         }
70         return true;
71     }
72 
73     bool Release() override
74     {
75         GLUtils::DeleteTexture(name_);
76         name_ = GL_ZERO;
77         width_ = 0;
78         height_ = 0;
79         SetReady(false);
80         return true;
81     }
82 
83 private:
84     GLuint name_{ GL_ZERO };
85     GLsizei width_{ 0 };
86     GLsizei height_{ 0 };
87     GLenum internalFormat_;
88     RenderContext *context_{ nullptr };
89 };
90 
91 class TextureSizeMeasurer {
92 public:
operator ()(const RenderTexturePtr &tex)93     size_t operator () (const RenderTexturePtr &tex)
94     {
95         size_t width = static_cast<size_t>(tex->Width());
96         size_t height = static_cast<size_t>(tex->Height());
97         return (width * height * GLUtils::GetInternalFormatPixelByteSize(tex->Format()));
98     }
99 };
100 } // namespace Effect
101 } // namespace Media
102 } // namespace OHOS
103 #endif