Merge "Adding Depth/Stencil code" into devel/graphics
[platform/core/uifw/dali-core.git] / dali / internal / update / common / scene-graph-scene.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/update/common/scene-graph-scene.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/integration-api/core-enumerations.h>
22 #include <dali/internal/render/gl-resources/context.h>
23 #include <dali/internal/update/render-tasks/scene-graph-render-task-list.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace SceneGraph
30 {
31 Scene::Scene()
32 : mContext(nullptr),
33   mFrameRenderedCallbacks(),
34   mFramePresentedCallbacks(),
35   mSkipRendering(false),
36   mSurfaceRect(),
37   mSurfaceOrientation(0),
38   mSurfaceRectChanged(false)
39 {
40 }
41
42 Scene::~Scene()
43 {
44   mFrameRenderedCallbacks.clear();
45   mFramePresentedCallbacks.clear();
46 }
47
48 void Scene::Initialize(Context& context, Graphics::Controller& graphicsController, Integration::DepthBufferAvailable depthBufferAvailable, Integration::StencilBufferAvailable stencilBufferAvailable)
49 {
50   mContext = &context;
51
52   // Create the render pass for the surface
53   std::vector<Graphics::AttachmentDescription> attachmentDescriptions;
54
55   // Default behaviour for color attachments is to CLEAR and STORE
56   mClearValues.clear();
57   mClearValues.emplace_back();
58
59   // Assume single color attachment
60   Graphics::AttachmentDescription desc{};
61   desc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR);
62   desc.SetStoreOp(Graphics::AttachmentStoreOp::STORE);
63   attachmentDescriptions.push_back(desc);
64
65   if(depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
66      stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
67   {
68     // Depth
69     desc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR);
70     desc.SetStoreOp(Graphics::AttachmentStoreOp::STORE);
71
72     // Stencil
73     desc.SetStencilLoadOp(Graphics::AttachmentLoadOp::CLEAR);
74     desc.SetStencilStoreOp(Graphics::AttachmentStoreOp::STORE);
75     attachmentDescriptions.push_back(desc);
76
77     mClearValues.emplace_back();
78     mClearValues.back().depthStencil.depth   = 0;
79     mClearValues.back().depthStencil.stencil = 0;
80   }
81
82   Graphics::RenderPassCreateInfo rpInfo{};
83   rpInfo.SetAttachments(attachmentDescriptions);
84
85   // Add default render pass (loadOp = clear)
86   mRenderPass = graphicsController.CreateRenderPass(rpInfo, nullptr); // Warning: Shallow ptr
87
88   desc.SetLoadOp(Graphics::AttachmentLoadOp::LOAD);
89   attachmentDescriptions[0] = desc;
90   if(attachmentDescriptions.size() > 1)
91   {
92     desc.SetLoadOp(Graphics::AttachmentLoadOp::LOAD);
93     desc.SetStencilLoadOp(Graphics::AttachmentLoadOp::LOAD);
94     attachmentDescriptions.back() = desc;
95   }
96
97   mRenderPassNoClear = graphicsController.CreateRenderPass(rpInfo, nullptr); // Warning: Shallow ptr
98 }
99
100 Context* Scene::GetContext()
101 {
102   return mContext;
103 }
104
105 RenderInstructionContainer& Scene::GetRenderInstructions()
106 {
107   return mInstructions;
108 }
109
110 void Scene::AddFrameRenderedCallback(CallbackBase* callback, int32_t frameId)
111 {
112   mFrameRenderedCallbacks.push_back(std::make_pair(std::unique_ptr<CallbackBase>(callback), frameId));
113 }
114
115 void Scene::AddFramePresentedCallback(CallbackBase* callback, int32_t frameId)
116 {
117   mFramePresentedCallbacks.push_back(std::make_pair(std::unique_ptr<CallbackBase>(callback), frameId));
118 }
119
120 void Scene::GetFrameRenderedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks)
121 {
122   // Transfer owership of the callbacks
123   for(auto&& iter : mFrameRenderedCallbacks)
124   {
125     callbacks.push_back(std::make_pair(std::move(iter.first), iter.second));
126   }
127
128   mFrameRenderedCallbacks.clear();
129 }
130
131 void Scene::GetFramePresentedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks)
132 {
133   // Transfer owership of the callbacks
134   for(auto&& iter : mFramePresentedCallbacks)
135   {
136     callbacks.push_back(std::make_pair(std::move(iter.first), iter.second));
137   }
138
139   mFramePresentedCallbacks.clear();
140 }
141
142 void Scene::SetSkipRendering(bool skip)
143 {
144   mSkipRendering = skip;
145 }
146
147 bool Scene::IsRenderingSkipped() const
148 {
149   return mSkipRendering;
150 }
151
152 void Scene::SetSurfaceRect(const Rect<int32_t>& rect)
153 {
154   mSurfaceRect        = rect;
155   mSurfaceRectChanged = true;
156 }
157
158 const Rect<int32_t>& Scene::GetSurfaceRect() const
159 {
160   return mSurfaceRect;
161 }
162
163 void Scene::SetSurfaceOrientation(int32_t orientation)
164 {
165   mSurfaceOrientation = orientation;
166 }
167
168 int32_t Scene::GetSurfaceOrientation() const
169 {
170   return mSurfaceOrientation;
171 }
172
173 bool Scene::IsSurfaceRectChanged()
174 {
175   bool surfaceRectChanged = mSurfaceRectChanged;
176   mSurfaceRectChanged     = false;
177
178   return surfaceRectChanged;
179 }
180
181 } // namespace SceneGraph
182
183 } // namespace Internal
184
185 } // namespace Dali