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 #include "render_frame_buffer.h"
17 #include "base/render_base.h"
18 #include "graphic/gl_utils.h"
19 #include "effect_log.h"
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Effect {
RenderFrameBuffer(RenderContext * ctx,ResourceCache * cache,int width,int height,GLenum interFmt)24 RenderFrameBuffer::RenderFrameBuffer(RenderContext *ctx, ResourceCache *cache, int width, int height,
25     GLenum interFmt)
26 {
27     CHECK_AND_RETURN_LOG(ctx != nullptr && cache != nullptr && texture_ != nullptr,
28         "RenderFrameBuffer struct fail, ctx or cache or texture_ is null.");
29     context_ = ctx;
30     cache_ = cache;
31     texture_ = cache->RequestTexture(ctx, width, height, interFmt);
32     fboId_ = GLUtils::CreateFramebuffer(texture_->GetName());
33     GLUtils::CheckError(__FILE_NAME__, __LINE__);
34 }
35 
~RenderFrameBuffer()36 RenderFrameBuffer::~RenderFrameBuffer()
37 {
38     if (fboId_) {
39         GLUtils::DeleteFboOnly(fboId_);
40     }
41 }
42 
Resize(int width,int height)43 void RenderFrameBuffer::Resize(int width, int height)
44 {
45     if ((width != (int)texture_->Width()) || (height != (int)texture_->Height())) {
46         GLenum fmt = texture_->Format();
47         texture_.reset();
48         texture_ = cache_->RequestTexture(context_, width, height, fmt);
49         glBindFramebuffer(GL_FRAMEBUFFER, fboId_);
50         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_->GetName(), 0);
51         glBindFramebuffer(GL_FRAMEBUFFER, 0);
52         GLUtils::CheckError(__FILE_NAME__, __LINE__);
53     }
54 }
55 
Texture() const56 RenderTexturePtr RenderFrameBuffer::Texture() const
57 {
58     return texture_;
59 }
60 
Bind()61 void RenderFrameBuffer::Bind()
62 {
63     glBindFramebuffer(GL_FRAMEBUFFER, fboId_);
64 }
65 
UnBind()66 void RenderFrameBuffer::UnBind()
67 {
68     glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);
69 }
70 
Width()71 int RenderFrameBuffer::Width()
72 {
73     return texture_->Width();
74 }
75 
Height()76 int RenderFrameBuffer::Height()
77 {
78     return texture_->Height();
79 }
80 } // namespace Effect
81 } // namespace Media
82 } // namespace OHOS