Add KeepRendering method to Scene
[platform/core/uifw/dali-core.git] / dali / internal / update / common / scene-graph-scene.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 // 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/update/render-tasks/scene-graph-render-task-list.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace SceneGraph
29 {
30 Scene::Scene()
31 : mFrameRenderedCallbacks(),
32   mFramePresentedCallbacks(),
33   mSurfaceRect(),
34   mSurfaceOrientation(0),
35   mScreenOrientation(0),
36   mSurfaceRectChanged(false),
37   mRotationCompletedAcknowledgement(false),
38   mSkipRendering(false),
39   mNeedFullUpdate(false)
40 {
41 }
42
43 Scene::~Scene()
44 {
45   mFrameRenderedCallbacks.clear();
46   mFramePresentedCallbacks.clear();
47 }
48
49 void Scene::Initialize(Graphics::Controller& graphicsController, Integration::DepthBufferAvailable depthBufferAvailable, Integration::StencilBufferAvailable stencilBufferAvailable)
50 {
51   mGraphicsController = &graphicsController;
52
53   // Create the render target for the surface. It should already have been sent via message.
54   mRenderTarget = graphicsController.CreateRenderTarget(mRenderTargetCreateInfo, std::move(mRenderTarget));
55
56   // Create the render pass for the surface
57   std::vector<Graphics::AttachmentDescription> attachmentDescriptions;
58
59   // Default behaviour for color attachments is to CLEAR and STORE
60   mClearValues.clear();
61   mClearValues.emplace_back();
62
63   // Assume single color attachment
64   Graphics::AttachmentDescription desc{};
65   desc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR);
66   desc.SetStoreOp(Graphics::AttachmentStoreOp::STORE);
67   attachmentDescriptions.push_back(desc);
68
69   if(depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
70      stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
71   {
72     // Depth
73     desc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR);
74     desc.SetStoreOp(Graphics::AttachmentStoreOp::STORE);
75
76     // Stencil
77     desc.SetStencilLoadOp(Graphics::AttachmentLoadOp::CLEAR);
78     desc.SetStencilStoreOp(Graphics::AttachmentStoreOp::STORE);
79     attachmentDescriptions.push_back(desc);
80
81     mClearValues.emplace_back();
82     mClearValues.back().depthStencil.depth   = 0;
83     mClearValues.back().depthStencil.stencil = 0;
84   }
85
86   Graphics::RenderPassCreateInfo rpInfo{};
87   rpInfo.SetAttachments(attachmentDescriptions);
88
89   // Add default render pass (loadOp = clear)
90   mRenderPass = graphicsController.CreateRenderPass(rpInfo, nullptr); // Warning: Shallow ptr
91
92   desc.SetLoadOp(Graphics::AttachmentLoadOp::LOAD);
93   attachmentDescriptions[0] = desc;
94   if(attachmentDescriptions.size() > 1)
95   {
96     desc.SetLoadOp(Graphics::AttachmentLoadOp::LOAD);
97     desc.SetStencilLoadOp(Graphics::AttachmentLoadOp::LOAD);
98     attachmentDescriptions.back() = desc;
99   }
100
101   mRenderPassNoClear = graphicsController.CreateRenderPass(rpInfo, nullptr); // Warning: Shallow ptr
102 }
103
104 RenderInstructionContainer& Scene::GetRenderInstructions()
105 {
106   return mInstructions;
107 }
108
109 void Scene::AddFrameRenderedCallback(CallbackBase* callback, int32_t frameId)
110 {
111   mFrameRenderedCallbacks.push_back(std::make_pair(std::unique_ptr<CallbackBase>(callback), frameId));
112 }
113
114 void Scene::AddFramePresentedCallback(CallbackBase* callback, int32_t frameId)
115 {
116   mFramePresentedCallbacks.push_back(std::make_pair(std::unique_ptr<CallbackBase>(callback), frameId));
117 }
118
119 void Scene::GetFrameRenderedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks)
120 {
121   // Transfer owership of the callbacks
122   for(auto&& iter : mFrameRenderedCallbacks)
123   {
124     callbacks.push_back(std::make_pair(std::move(iter.first), iter.second));
125   }
126
127   mFrameRenderedCallbacks.clear();
128 }
129
130 void Scene::GetFramePresentedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks)
131 {
132   // Transfer owership of the callbacks
133   for(auto&& iter : mFramePresentedCallbacks)
134   {
135     callbacks.push_back(std::make_pair(std::move(iter.first), iter.second));
136   }
137
138   mFramePresentedCallbacks.clear();
139 }
140
141 void Scene::SetSkipRendering(bool skip)
142 {
143   mSkipRendering = skip;
144 }
145
146 bool Scene::IsRenderingSkipped() const
147 {
148   return mSkipRendering;
149 }
150
151 void Scene::SetSurfaceRect(const Rect<int32_t>& rect)
152 {
153   mSurfaceRect        = rect;
154   mSurfaceRectChanged = true;
155
156   DALI_LOG_RELEASE_INFO("update surfce rect in scene-graph, width[%d], height[%d]\n", mSurfaceRect.width, mSurfaceRect.height);
157
158   if(mRoot)
159   {
160     mRoot->SetUpdated(true);
161   }
162 }
163
164 const Rect<int32_t>& Scene::GetSurfaceRect() const
165 {
166   return mSurfaceRect;
167 }
168
169 bool Scene::IsSurfaceRectChanged()
170 {
171   bool surfaceRectChanged = mSurfaceRectChanged;
172   mSurfaceRectChanged     = false;
173
174   return surfaceRectChanged;
175 }
176
177 void Scene::SetSurfaceOrientations(int32_t windowOrientation, int32_t screenOrienation)
178 {
179   mSurfaceOrientation = windowOrientation;
180   mScreenOrientation  = screenOrienation;
181
182   if(mRoot)
183   {
184     mRoot->SetUpdated(true);
185   }
186
187   DALI_LOG_RELEASE_INFO("update orientation in scene-graph, surface [%d], screen[%d]\n", mSurfaceOrientation, mScreenOrientation);
188 }
189
190 int32_t Scene::GetSurfaceOrientation() const
191 {
192   return mSurfaceOrientation;
193 }
194
195 int32_t Scene::GetScreenOrientation() const
196 {
197   return mScreenOrientation;
198 }
199
200 void Scene::SetRotationCompletedAcknowledgement()
201 {
202   mRotationCompletedAcknowledgement = true;
203 }
204
205 bool Scene::IsRotationCompletedAcknowledgementSet()
206 {
207   bool setRotationCompletedAcknowledgement = mRotationCompletedAcknowledgement;
208   mRotationCompletedAcknowledgement        = false;
209   return setRotationCompletedAcknowledgement;
210 }
211
212 Scene::ItemsDirtyRectsContainer& Scene::GetItemsDirtyRects()
213 {
214   return mItemsDirtyRects;
215 }
216
217 void Scene::SetSurfaceRenderTargetCreateInfo(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo)
218 {
219   if(mRenderTarget != nullptr &&
220      mRenderTargetCreateInfo.surface != renderTargetCreateInfo.surface)
221   {
222     // Only recreate if the surface has changed.
223     mRenderTargetCreateInfo = renderTargetCreateInfo;
224     if(mGraphicsController) // shouldn't be null, as we can't have already set mRenderTarget unless graphics controller exists.
225     {
226       mRenderTarget = mGraphicsController->CreateRenderTarget(renderTargetCreateInfo, std::move(mRenderTarget));
227     }
228   }
229   else
230   {
231     // 2nd Stage initialization happens in RenderManager, not UpdateManager, so is delayed.
232     mRenderTargetCreateInfo = renderTargetCreateInfo;
233   }
234 }
235
236 void Scene::KeepRendering(float durationSeconds)
237 {
238   mKeepRenderingSeconds = std::max(mKeepRenderingSeconds, durationSeconds);
239 }
240
241 bool Scene::KeepRenderingCheck(float elapsedSeconds)
242 {
243   if(mKeepRenderingSeconds > 0.0f)
244   {
245     mNeedFullUpdate       = true; // Full update if KeepRendering is required
246     mKeepRenderingSeconds = std::max(0.0f, mKeepRenderingSeconds - elapsedSeconds);
247     return true;
248   }
249
250   mNeedFullUpdate       = false;
251   mKeepRenderingSeconds = 0.0f;
252   return false;
253 }
254
255 } // namespace SceneGraph
256
257 } // namespace Internal
258
259 } // namespace Dali