Implementing Framebuffer object
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali/internal/render/renderers/render-frame-buffer.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/render/renderers/render-texture.h>
22
23 namespace Dali
24 {
25 namespace Internal
26 {
27 namespace Render
28 {
29 FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
30 : mWidth(width),
31   mHeight(height),
32   mDepthBuffer(attachments & Dali::FrameBuffer::Attachment::DEPTH),
33   mStencilBuffer(attachments & Dali::FrameBuffer::Attachment::STENCIL)
34 {
35 }
36
37 FrameBuffer::~FrameBuffer() = default;
38
39 void FrameBuffer::Destroy()
40 {
41   mGraphicsObject.reset();
42 }
43
44 void FrameBuffer::Initialize(Graphics::Controller& graphicsController)
45 {
46   mGraphicsController = &graphicsController;
47 }
48
49 void FrameBuffer::AttachColorTexture(Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
50 {
51   if(texture)
52   {
53     if(!texture->GetGraphicsObject())
54     {
55       texture->Create(0 | Graphics::TextureUsageFlagBits::COLOR_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
56     }
57
58     uint32_t                  attachmentId = mCreateInfo.colorAttachments.size();
59     Graphics::ColorAttachment colorAttachment{attachmentId, texture->GetGraphicsObject(), layer, mipmapLevel};
60     mCreateInfo.colorAttachments.push_back(colorAttachment);
61   }
62 }
63
64 void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel)
65 {
66   if(texture && mDepthBuffer)
67   {
68     if(!texture->GetGraphicsObject())
69     {
70       texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
71     }
72
73     mCreateInfo.depthStencilAttachment.depthTexture = texture->GetGraphicsObject();
74     mCreateInfo.depthStencilAttachment.depthLevel   = mipmapLevel;
75   }
76 }
77
78 void FrameBuffer::AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel)
79 {
80   if(texture && mStencilBuffer)
81   {
82     if(!texture->GetGraphicsObject())
83     {
84       texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
85     }
86     mCreateInfo.depthStencilAttachment.stencilTexture = texture->GetGraphicsObject();
87     mCreateInfo.depthStencilAttachment.stencilLevel   = mipmapLevel;
88   }
89 }
90
91 void FrameBuffer::Bind()
92 {
93   if(!mGraphicsObject)
94   {
95     mGraphicsObject = mGraphicsController->CreateFramebuffer(mCreateInfo, nullptr);
96   }
97 }
98
99 uint32_t FrameBuffer::GetWidth() const
100 {
101   return mWidth;
102 }
103
104 uint32_t FrameBuffer::GetHeight() const
105 {
106   return mHeight;
107 }
108
109 } // namespace Render
110
111 } // namespace Internal
112
113 } // namespace Dali