[dali_2.3.23] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / frame-buffer-impl.cpp
1 /*
2  * Copyright (c) 2023 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
18 // CLASS HEADER
19 #include <dali/internal/event/rendering/frame-buffer-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/render/renderers/render-frame-buffer.h>
23 #include <dali/internal/update/manager/update-manager.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 FrameBufferPtr FrameBuffer::New(uint32_t width, uint32_t height, Mask attachments)
30 {
31   FrameBufferPtr frameBuffer(new FrameBuffer(width, height, attachments));
32   frameBuffer->Initialize();
33   return frameBuffer;
34 }
35
36 Render::FrameBuffer* FrameBuffer::GetRenderObject() const
37 {
38   return mRenderObject;
39 }
40
41 FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
42 : mEventThreadServices(EventThreadServices::Get()),
43   mRenderObject(nullptr),
44   mColor{nullptr},
45   mDepth(nullptr),
46   mStencil(nullptr),
47   mWidth(width),
48   mHeight(height),
49   mAttachments(attachments),
50   mColorAttachmentCount(0)
51 {
52 }
53
54 void FrameBuffer::Initialize()
55 {
56   mRenderObject = new Render::FrameBuffer(mWidth, mHeight, mAttachments);
57
58   OwnerPointer<Render::FrameBuffer> transferOwnership(mRenderObject);
59   AddFrameBuffer(mEventThreadServices.GetUpdateManager(), transferOwnership);
60 }
61
62 void FrameBuffer::AttachColorTexture(TexturePtr texture, uint32_t mipmapLevel, uint32_t layer)
63 {
64   if((texture->GetWidth() / (1u << mipmapLevel) != mWidth) ||
65      (texture->GetHeight() / (1u << mipmapLevel) != mHeight))
66   {
67     DALI_LOG_ERROR("Failed to attach color texture to FrameBuffer: Size mismatch \n");
68   }
69   else if(mColorAttachmentCount >= Dali::DevelFrameBuffer::MAX_COLOR_ATTACHMENTS)
70   {
71     DALI_LOG_ERROR("Failed to attach color texture to FrameBuffer: Exceeded maximum supported color attachments.\n");
72   }
73   else
74   {
75     mColor[mColorAttachmentCount] = texture;
76     ++mColorAttachmentCount;
77
78     AttachColorTextureToFrameBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderTextureKey().Get(), mipmapLevel, layer);
79   }
80 }
81
82 void FrameBuffer::AttachDepthTexture(TexturePtr texture, uint32_t mipmapLevel)
83 {
84   if((texture->GetWidth() / (1u << mipmapLevel) != mWidth) ||
85      (texture->GetHeight() / (1u << mipmapLevel) != mHeight))
86   {
87     DALI_LOG_ERROR("Failed to attach depth texture to FrameBuffer: Size mismatch \n");
88   }
89   else
90   {
91     mDepth = texture;
92     AttachDepthTextureToFrameBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderTextureKey().Get(), mipmapLevel);
93   }
94 }
95
96 void FrameBuffer::AttachDepthStencilTexture(TexturePtr texture, unsigned int mipmapLevel)
97 {
98   if((texture->GetWidth() / (1u << mipmapLevel) != mWidth) ||
99      (texture->GetHeight() / (1u << mipmapLevel) != mHeight))
100   {
101     DALI_LOG_ERROR("Failed to attach depth/stencil texture to FrameBuffer: Size mismatch \n");
102   }
103   else
104   {
105     mStencil = texture;
106     AttachDepthStencilTextureToFrameBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderTextureKey().Get(), mipmapLevel);
107   }
108 }
109
110 void FrameBuffer::SetMultiSamplingLevel(uint8_t multiSamplingLevel)
111 {
112   SetMultiSamplingLevelToFrameBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject, multiSamplingLevel);
113 }
114
115 Texture* FrameBuffer::GetColorTexture(uint8_t index) const
116 {
117   return (index >= mColorAttachmentCount) ? nullptr : mColor[index].Get();
118 }
119
120 Texture* FrameBuffer::GetDepthTexture() const
121 {
122   return (mDepth) ? mDepth.Get() : nullptr;
123 }
124
125 Texture* FrameBuffer::GetDepthStencilTexture() const
126 {
127   return (mStencil) ? mStencil.Get() : nullptr;
128 }
129
130 void FrameBuffer::SetSize(uint32_t width, uint32_t height)
131 {
132   mWidth  = width;
133   mHeight = height;
134 }
135
136 FrameBuffer::~FrameBuffer()
137 {
138   if(EventThreadServices::IsCoreRunning() && mRenderObject)
139   {
140     RemoveFrameBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject);
141   }
142 }
143
144 } // namespace Internal
145 } // namespace Dali