[Tizen] Print logs if dali skip rendering
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
1 /*
2  * Copyright (c) 2024 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/render/common/render-manager.h>
20
21 // EXTERNAL INCLUDES
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/core.h>
26 #include <dali/internal/common/ordered-set.h>
27
28 #include <dali/internal/event/common/scene-impl.h>
29
30 #include <dali/internal/update/common/scene-graph-scene.h>
31 #include <dali/internal/update/nodes/scene-graph-layer.h>
32 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
33
34 #include <dali/internal/common/owner-key-container.h>
35
36 #include <dali/internal/render/common/render-algorithms.h>
37 #include <dali/internal/render/common/render-debug.h>
38 #include <dali/internal/render/common/render-instruction.h>
39 #include <dali/internal/render/common/render-tracker.h>
40 #include <dali/internal/render/queue/render-queue.h>
41 #include <dali/internal/render/renderers/pipeline-cache.h>
42 #include <dali/internal/render/renderers/render-frame-buffer.h>
43 #include <dali/internal/render/renderers/render-texture.h>
44 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
45 #include <dali/internal/render/renderers/uniform-buffer.h>
46 #include <dali/internal/render/shaders/program-controller.h>
47
48 #include <memory>
49
50 namespace Dali
51 {
52 namespace Internal
53 {
54 namespace SceneGraph
55 {
56 #if defined(DEBUG_ENABLED)
57 namespace
58 {
59 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
60 } // unnamed namespace
61 #endif
62
63 namespace
64 {
65 // TODO : Cache clean logic have some problem now. Just block it until bug resolved
66 //constexpr uint32_t CACHE_CLEAN_FRAME_COUNT = 600u; // 60fps * 10sec
67
68 inline Graphics::Rect2D RecalculateScissorArea(const Graphics::Rect2D& scissorArea, int orientation, const Rect<int32_t>& viewportRect)
69 {
70   Graphics::Rect2D newScissorArea;
71
72   if(orientation == 90)
73   {
74     newScissorArea.x      = viewportRect.height - (scissorArea.y + scissorArea.height);
75     newScissorArea.y      = scissorArea.x;
76     newScissorArea.width  = scissorArea.height;
77     newScissorArea.height = scissorArea.width;
78   }
79   else if(orientation == 180)
80   {
81     newScissorArea.x      = viewportRect.width - (scissorArea.x + scissorArea.width);
82     newScissorArea.y      = viewportRect.height - (scissorArea.y + scissorArea.height);
83     newScissorArea.width  = scissorArea.width;
84     newScissorArea.height = scissorArea.height;
85   }
86   else if(orientation == 270)
87   {
88     newScissorArea.x      = scissorArea.y;
89     newScissorArea.y      = viewportRect.width - (scissorArea.x + scissorArea.width);
90     newScissorArea.width  = scissorArea.height;
91     newScissorArea.height = scissorArea.width;
92   }
93   else
94   {
95     newScissorArea.x      = scissorArea.x;
96     newScissorArea.y      = scissorArea.y;
97     newScissorArea.width  = scissorArea.width;
98     newScissorArea.height = scissorArea.height;
99   }
100   return newScissorArea;
101 }
102
103 inline Rect<int32_t> CalculateUpdateArea(RenderItem& item, BufferIndex renderBufferIndex, const Rect<int32_t>& viewportRect)
104 {
105   Vector4 updateArea;
106   if(item.mNode && item.mNode->IsTextureUpdateAreaUsed() && item.mRenderer)
107   {
108     updateArea = item.mRenderer->GetTextureUpdateArea();
109   }
110   else
111   {
112     updateArea = item.mRenderer ? item.mRenderer->GetVisualTransformedUpdateArea(renderBufferIndex, item.mUpdateArea) : item.mUpdateArea;
113   }
114
115   return RenderItem::CalculateViewportSpaceAABB(item.mModelViewMatrix, Vector3(updateArea.x, updateArea.y, 0.0f), Vector3(updateArea.z, updateArea.w, 0.0f), viewportRect.width, viewportRect.height);
116 }
117
118 inline void AlignDamagedRect(Rect<int32_t>& rect)
119 {
120   const int left   = rect.x;
121   const int top    = rect.y;
122   const int right  = rect.x + rect.width;
123   const int bottom = rect.y + rect.height;
124   rect.x           = (left / 16) * 16;
125   rect.y           = (top / 16) * 16;
126   rect.width       = ((right + 16) / 16) * 16 - rect.x;
127   rect.height      = ((bottom + 16) / 16) * 16 - rect.y;
128 }
129 } // namespace
130
131 /**
132  * Structure to contain internal data
133  */
134 struct RenderManager::Impl
135 {
136   Impl(Graphics::Controller&               graphicsController,
137        Integration::DepthBufferAvailable   depthBufferAvailableParam,
138        Integration::StencilBufferAvailable stencilBufferAvailableParam,
139        Integration::PartialUpdateAvailable partialUpdateAvailableParam)
140   : graphicsController(graphicsController),
141     renderAlgorithms(graphicsController),
142     programController(graphicsController),
143     depthBufferAvailable(depthBufferAvailableParam),
144     stencilBufferAvailable(stencilBufferAvailableParam),
145     partialUpdateAvailable(partialUpdateAvailableParam)
146   {
147     uniformBufferManager = std::make_unique<Render::UniformBufferManager>(&graphicsController);
148     pipelineCache        = std::make_unique<Render::PipelineCache>(graphicsController);
149   }
150
151   ~Impl()
152   {
153     rendererContainer.Clear(); // clear now before the pipeline cache is deleted
154   }
155
156   void AddRenderTracker(Render::RenderTracker* renderTracker)
157   {
158     DALI_ASSERT_DEBUG(renderTracker != nullptr);
159     mRenderTrackers.PushBack(renderTracker);
160   }
161
162   void RemoveRenderTracker(Render::RenderTracker* renderTracker)
163   {
164     mRenderTrackers.EraseObject(renderTracker);
165   }
166
167   void UpdateTrackers()
168   {
169     for(auto&& iter : mRenderTrackers)
170     {
171       iter->PollSyncObject();
172     }
173   }
174
175   // the order is important for destruction,
176   Graphics::Controller&           graphicsController;
177   RenderQueue                     renderQueue;      ///< A message queue for receiving messages from the update-thread.
178   std::vector<SceneGraph::Scene*> sceneContainer;   ///< List of pointers to the scene graph objects of the scenes
179   Render::RenderAlgorithms        renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
180
181   OrderedSet<Render::Sampler>         samplerContainer;      ///< List of owned samplers
182   OrderedSet<Render::FrameBuffer>     frameBufferContainer;  ///< List of owned framebuffers
183   OrderedSet<Render::VertexBuffer>    vertexBufferContainer; ///< List of owned vertex buffers
184   OrderedSet<Render::Geometry>        geometryContainer;     ///< List of owned Geometries
185   OwnerKeyContainer<Render::Renderer> rendererContainer;     ///< List of owned renderers
186   OwnerKeyContainer<Render::Texture>  textureContainer;      ///< List of owned textures
187
188   OrderedSet<Render::RenderTracker> mRenderTrackers; ///< List of owned render trackers
189
190   OwnerKeyContainer<Render::Texture> textureDiscardQueue; ///< Discarded textures
191
192   ProgramController programController; ///< Owner of the programs
193
194   std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
195   std::unique_ptr<Render::PipelineCache>        pipelineCache;
196
197   Integration::DepthBufferAvailable   depthBufferAvailable;   ///< Whether the depth buffer is available
198   Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
199   Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
200
201   Vector<Render::TextureKey> updatedTextures{}; ///< The updated texture list
202
203   uint32_t    frameCount{0u};                                                    ///< The current frame count
204   BufferIndex renderBufferIndex{SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX}; ///< The index of the buffer to read from;
205
206   bool lastFrameWasRendered{false}; ///< Keeps track of the last frame being rendered due to having render instructions
207   bool commandBufferSubmitted{false};
208 };
209
210 RenderManager* RenderManager::New(Graphics::Controller&               graphicsController,
211                                   Integration::DepthBufferAvailable   depthBufferAvailable,
212                                   Integration::StencilBufferAvailable stencilBufferAvailable,
213                                   Integration::PartialUpdateAvailable partialUpdateAvailable)
214 {
215   auto* manager  = new RenderManager;
216   manager->mImpl = new Impl(graphicsController,
217                             depthBufferAvailable,
218                             stencilBufferAvailable,
219                             partialUpdateAvailable);
220   return manager;
221 }
222
223 RenderManager::RenderManager()
224 : mImpl(nullptr)
225 {
226 }
227
228 RenderManager::~RenderManager()
229 {
230   delete mImpl;
231 }
232
233 void RenderManager::ContextDestroyed()
234 {
235   mImpl->uniformBufferManager->ContextDestroyed();
236 }
237
238 RenderQueue& RenderManager::GetRenderQueue()
239 {
240   return mImpl->renderQueue;
241 }
242
243 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
244 {
245 }
246
247 void RenderManager::AddRenderer(const Render::RendererKey& renderer)
248 {
249   // Initialize the renderer as we are now in render thread
250   renderer->Initialize(mImpl->graphicsController, mImpl->programController, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get()));
251
252   mImpl->rendererContainer.PushBack(renderer);
253 }
254
255 void RenderManager::RemoveRenderer(const Render::RendererKey& renderer)
256 {
257   mImpl->rendererContainer.EraseKey(renderer);
258 }
259
260 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
261 {
262   sampler->Initialize(mImpl->graphicsController);
263   mImpl->samplerContainer.PushBack(sampler.Release());
264 }
265
266 void RenderManager::RemoveSampler(Render::Sampler* sampler)
267 {
268   mImpl->samplerContainer.EraseObject(sampler);
269 }
270
271 void RenderManager::AddTexture(const Render::TextureKey& textureKey)
272 {
273   DALI_ASSERT_DEBUG(textureKey && "Trying to add empty texture key");
274
275   textureKey->Initialize(mImpl->graphicsController, *this);
276   mImpl->textureContainer.PushBack(textureKey);
277   mImpl->updatedTextures.PushBack(textureKey);
278 }
279
280 void RenderManager::RemoveTexture(const Render::TextureKey& textureKey)
281 {
282   DALI_ASSERT_DEBUG(textureKey && "Trying to remove empty texture key");
283
284   // Find the texture, use std::find so we can do the erase by iterator safely
285   auto iter = std::find(mImpl->textureContainer.Begin(), mImpl->textureContainer.End(), textureKey);
286
287   if(iter != mImpl->textureContainer.End())
288   {
289     // Destroy texture.
290     textureKey->Destroy();
291
292     // Transfer ownership to the discard queue, this keeps the object alive, until the render-thread has finished with it
293     mImpl->textureDiscardQueue.PushBack(mImpl->textureContainer.Release(iter));
294   }
295 }
296
297 void RenderManager::UploadTexture(const Render::TextureKey& textureKey, PixelDataPtr pixelData, const Graphics::UploadParams& params)
298 {
299   DALI_ASSERT_DEBUG(textureKey && "Trying to upload to empty texture key");
300   textureKey->Upload(pixelData, params);
301
302   mImpl->updatedTextures.PushBack(textureKey);
303 }
304
305 void RenderManager::GenerateMipmaps(const Render::TextureKey& textureKey)
306 {
307   DALI_ASSERT_DEBUG(textureKey && "Trying to generate mipmaps on empty texture key");
308   textureKey->GenerateMipmaps();
309
310   mImpl->updatedTextures.PushBack(textureKey);
311 }
312
313 void RenderManager::SetTextureSize(const Render::TextureKey& textureKey, const Dali::ImageDimensions& size)
314 {
315   DALI_ASSERT_DEBUG(textureKey && "Trying to set size on empty texture key");
316   textureKey->SetWidth(size.GetWidth());
317   textureKey->SetHeight(size.GetHeight());
318 }
319
320 void RenderManager::SetTextureFormat(const Render::TextureKey& textureKey, Dali::Pixel::Format pixelFormat)
321 {
322   DALI_ASSERT_DEBUG(textureKey && "Trying to set pixel format on empty texture key");
323   textureKey->SetPixelFormat(pixelFormat);
324 }
325
326 void RenderManager::SetTextureUpdated(const Render::TextureKey& textureKey)
327 {
328   DALI_ASSERT_DEBUG(textureKey && "Trying to set updated on empty texture key");
329   textureKey->SetUpdated(true);
330
331   mImpl->updatedTextures.PushBack(textureKey);
332 }
333
334 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
335 {
336   sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
337                          static_cast<Dali::FilterMode::Type>(magFilterMode));
338 }
339
340 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
341 {
342   sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
343                        static_cast<Dali::WrapMode::Type>(sWrapMode),
344                        static_cast<Dali::WrapMode::Type>(tWrapMode));
345 }
346
347 void RenderManager::AddFrameBuffer(OwnerPointer<Render::FrameBuffer>& frameBuffer)
348 {
349   Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
350   mImpl->frameBufferContainer.PushBack(frameBufferPtr);
351   frameBufferPtr->Initialize(mImpl->graphicsController);
352 }
353
354 void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
355 {
356   DALI_ASSERT_DEBUG(nullptr != frameBuffer);
357
358   // Find the framebuffer, use OrderedSet.Find so we can safely do the erase
359   auto iter = mImpl->frameBufferContainer.Find(frameBuffer);
360
361   if(iter != mImpl->frameBufferContainer.End())
362   {
363     frameBuffer->Destroy();
364     mImpl->frameBufferContainer.Erase(iter); // frameBuffer found; now destroy it
365   }
366 }
367
368 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
369 {
370   scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
371   mImpl->sceneContainer.push_back(scene);
372   mImpl->uniformBufferManager->RegisterScene(scene);
373 }
374
375 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
376 {
377   mImpl->uniformBufferManager->UnregisterScene(scene);
378   auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
379   if(iter != mImpl->sceneContainer.end())
380   {
381     mImpl->sceneContainer.erase(iter);
382   }
383 }
384
385 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
386 {
387   scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
388 }
389
390 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
391 {
392   frameBuffer->AttachColorTexture(texture, mipmapLevel, layer);
393 }
394
395 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
396 {
397   frameBuffer->AttachDepthTexture(texture, mipmapLevel);
398 }
399
400 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
401 {
402   frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel);
403 }
404
405 void RenderManager::SetMultiSamplingLevelToFrameBuffer(Render::FrameBuffer* frameBuffer, uint8_t multiSamplingLevel)
406 {
407   frameBuffer->SetMultiSamplingLevel(multiSamplingLevel);
408 }
409
410 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
411 {
412   mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
413 }
414
415 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
416 {
417   mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
418 }
419
420 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
421 {
422   vertexBuffer->SetFormat(format.Release());
423 }
424
425 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
426 {
427   vertexBuffer->SetData(data.Release(), size);
428 }
429
430 void RenderManager::SetVertexBufferUpdateCallback(Render::VertexBuffer* vertexBuffer, Dali::VertexBufferUpdateCallback* callback)
431 {
432   vertexBuffer->SetVertexBufferUpdateCallback(callback);
433 }
434
435 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Render::Geometry::Uint16ContainerType& indices)
436 {
437   geometry->SetIndexBuffer(indices);
438 }
439
440 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Render::Geometry::Uint32ContainerType& indices)
441 {
442   geometry->SetIndexBuffer(indices);
443 }
444
445 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
446 {
447   mImpl->geometryContainer.PushBack(geometry.Release());
448 }
449
450 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
451 {
452   mImpl->geometryContainer.EraseObject(geometry);
453 }
454
455 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
456 {
457   geometry->AddVertexBuffer(vertexBuffer);
458 }
459
460 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
461 {
462   geometry->RemoveVertexBuffer(vertexBuffer);
463 }
464
465 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
466 {
467   geometry->SetType(Render::Geometry::Type(geometryType));
468 }
469
470 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
471 {
472   mImpl->AddRenderTracker(renderTracker);
473 }
474
475 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
476 {
477   mImpl->RemoveRenderTracker(renderTracker);
478 }
479
480 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear)
481 {
482   DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
483   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "\n\nNewFrame %d\n", mImpl->frameCount);
484
485   // Increment the frame count at the beginning of each frame
486   ++mImpl->frameCount;
487
488   // Process messages queued during previous update
489   mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
490
491   uint32_t totalInstructionCount = 0u;
492   for(auto& i : mImpl->sceneContainer)
493   {
494     totalInstructionCount += i->GetRenderInstructions().Count(mImpl->renderBufferIndex);
495   }
496
497   const bool haveInstructions = totalInstructionCount > 0u;
498
499   DALI_LOG_INFO(gLogFilter, Debug::General, "Render: haveInstructions(%s) || mImpl->lastFrameWasRendered(%s) || forceClear(%s)\n", haveInstructions ? "true" : "false", mImpl->lastFrameWasRendered ? "true" : "false", forceClear ? "true" : "false");
500
501   // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
502   if(haveInstructions || mImpl->lastFrameWasRendered || forceClear)
503   {
504     DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n");
505
506     // Upload the geometries
507     for(auto&& geom : mImpl->geometryContainer)
508     {
509       geom->Upload(mImpl->graphicsController);
510     }
511   }
512
513   // Reset pipeline cache before rendering
514   mImpl->pipelineCache->PreRender();
515
516   // Let we collect reference counts during CACHE_CLEAN_FRAME_COUNT frames.
517   // TODO : Cache clean logic have some problem now. Just block it until bug resolved
518   /*
519   if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 1)
520   {
521     mImpl->programController.ResetReferenceCount();
522   }
523   */
524
525   mImpl->commandBufferSubmitted = false;
526 }
527
528 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
529 {
530   if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
531   {
532     return;
533   }
534
535   Internal::Scene&   sceneInternal = GetImplementation(scene);
536   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
537
538   if(!sceneObject || sceneObject->IsRenderingSkipped())
539   {
540     // We don't need to calculate dirty rects
541     if(!sceneObject)
542     {
543       DALI_LOG_ERROR("Scene was empty handle. Skip pre-rendering\n");
544     }
545     else
546     {
547       DALI_LOG_RELEASE_INFO("RenderingSkipped set true. Skip pre-rendering\n");
548     }
549     return;
550   }
551
552   class DamagedRectsCleaner
553   {
554   public:
555     explicit DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects, Rect<int>& surfaceRect)
556     : mDamagedRects(damagedRects),
557       mSurfaceRect(surfaceRect),
558       mCleanOnReturn(true)
559     {
560     }
561
562     void SetCleanOnReturn(bool cleanOnReturn)
563     {
564       mCleanOnReturn = cleanOnReturn;
565     }
566
567     ~DamagedRectsCleaner()
568     {
569       if(mCleanOnReturn)
570       {
571         mDamagedRects.clear();
572         mDamagedRects.push_back(mSurfaceRect);
573       }
574     }
575
576   private:
577     std::vector<Rect<int>>& mDamagedRects;
578     Rect<int>               mSurfaceRect;
579     bool                    mCleanOnReturn;
580   };
581
582   Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
583
584   // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
585   DamagedRectsCleaner damagedRectCleaner(damagedRects, surfaceRect);
586   bool                cleanDamagedRect = false;
587
588   Scene::ItemsDirtyRectsContainer& itemsDirtyRects = sceneObject->GetItemsDirtyRects();
589
590   if(!sceneObject->IsPartialUpdateEnabled())
591   {
592     // Clear all dirty rects
593     // The rects will be added when partial updated is enabled again
594     itemsDirtyRects.clear();
595     return;
596   }
597
598   // Mark previous dirty rects in the std::unordered_map.
599   for(auto& dirtyRectPair : itemsDirtyRects)
600   {
601     dirtyRectPair.second.visited = false;
602   }
603
604   uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
605   for(uint32_t i = 0; i < instructionCount; ++i)
606   {
607     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
608
609     if(instruction.mFrameBuffer)
610     {
611       cleanDamagedRect = true;
612       continue; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
613     }
614
615     const Camera* camera = instruction.GetCamera();
616     if(camera && camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
617     {
618       Vector3    position;
619       Vector3    scale;
620       Quaternion orientation;
621       camera->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
622
623       Vector3 orientationAxis;
624       Radian  orientationAngle;
625       orientation.ToAxisAngle(orientationAxis, orientationAngle);
626
627       if(position.x > Math::MACHINE_EPSILON_10000 ||
628          position.y > Math::MACHINE_EPSILON_10000 ||
629          orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
630          orientationAngle != ANGLE_180 ||
631          scale != Vector3(1.0f, 1.0f, 1.0f))
632       {
633         cleanDamagedRect = true;
634         continue;
635       }
636     }
637     else
638     {
639       cleanDamagedRect = true;
640       continue;
641     }
642
643     Rect<int32_t> viewportRect;
644     if(instruction.mIsViewportSet)
645     {
646       const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
647       viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
648       if(viewportRect.IsEmpty() || !viewportRect.IsValid())
649       {
650         cleanDamagedRect = true;
651         continue; // just skip funny use cases for now, empty viewport means it is set somewhere else
652       }
653     }
654     else
655     {
656       viewportRect = surfaceRect;
657     }
658
659     const Matrix* viewMatrix       = instruction.GetViewMatrix(mImpl->renderBufferIndex);
660     const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
661     if(viewMatrix && projectionMatrix)
662     {
663       const RenderListContainer::SizeType count = instruction.RenderListCount();
664       for(RenderListContainer::SizeType index = 0u; index < count; ++index)
665       {
666         const RenderList* renderList = instruction.GetRenderList(index);
667         if(renderList)
668         {
669           if(!renderList->IsEmpty())
670           {
671             const std::size_t listCount = renderList->Count();
672             for(uint32_t listIndex = 0u; listIndex < listCount; ++listIndex)
673             {
674               RenderItem& item = renderList->GetItem(listIndex);
675               // If the item does 3D transformation, make full update
676               if(item.mUpdateArea == Vector4::ZERO)
677               {
678                 cleanDamagedRect = true;
679
680                 // Save the full rect in the damaged list. We need it when this item is removed
681                 DirtyRectKey dirtyRectKey(item.mNode, item.mRenderer);
682                 auto         dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
683                 if(dirtyRectPos != itemsDirtyRects.end())
684                 {
685                   // Replace the rect
686                   dirtyRectPos->second.visited = true;
687                   dirtyRectPos->second.rect    = surfaceRect;
688                 }
689                 else
690                 {
691                   // Else, just insert the new dirtyrect
692                   itemsDirtyRects.insert({dirtyRectKey, surfaceRect});
693                 }
694                 continue;
695               }
696
697               Rect<int>    rect;
698               DirtyRectKey dirtyRectKey(item.mNode, item.mRenderer);
699               // If the item refers to updated node or renderer.
700               if(item.mIsUpdated ||
701                  (item.mNode &&
702                   (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated()))))
703               {
704                 item.mIsUpdated = false;
705
706                 rect = CalculateUpdateArea(item, mImpl->renderBufferIndex, viewportRect);
707                 if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
708                 {
709                   AlignDamagedRect(rect);
710
711                   // Found valid dirty rect.
712                   auto dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
713                   if(dirtyRectPos != itemsDirtyRects.end())
714                   {
715                     Rect<int> currentRect = rect;
716
717                     // Same item, merge it with the previous rect
718                     rect.Merge(dirtyRectPos->second.rect);
719
720                     // Replace the rect as current
721                     dirtyRectPos->second.visited = true;
722                     dirtyRectPos->second.rect    = currentRect;
723                   }
724                   else
725                   {
726                     // Else, just insert the new dirtyrect
727                     itemsDirtyRects.insert({dirtyRectKey, rect});
728                   }
729
730                   damagedRects.push_back(rect);
731                 }
732               }
733               else
734               {
735                 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
736                 // 2. Mark the related dirty rects as visited so they will not be removed below.
737                 auto dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
738                 if(dirtyRectPos != itemsDirtyRects.end())
739                 {
740                   dirtyRectPos->second.visited = true;
741                 }
742                 else
743                 {
744                   // The item is not in the list for some reason. Add the current rect!
745                   rect = CalculateUpdateArea(item, mImpl->renderBufferIndex, viewportRect);
746                   if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
747                   {
748                     AlignDamagedRect(rect);
749
750                     itemsDirtyRects.insert({dirtyRectKey, rect});
751                   }
752                   cleanDamagedRect = true; // And make full update at this frame
753                 }
754               }
755             }
756           }
757         }
758       }
759     }
760   }
761
762   // Check removed nodes or removed renderers dirty rects
763   // Note, std::unordered_map end iterator is validate if we call erase.
764   for(auto iter = itemsDirtyRects.cbegin(), iterEnd = itemsDirtyRects.cend(); iter != iterEnd;)
765   {
766     if(!iter->second.visited)
767     {
768       damagedRects.push_back(iter->second.rect);
769       iter = itemsDirtyRects.erase(iter);
770     }
771     else
772     {
773       ++iter;
774     }
775   }
776
777   if(sceneObject->IsNeededFullUpdate())
778   {
779     cleanDamagedRect = true; // And make full update at this frame
780   }
781
782   if(!cleanDamagedRect)
783   {
784     damagedRectCleaner.SetCleanOnReturn(false);
785   }
786 }
787
788 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
789 {
790   SceneGraph::Scene* sceneObject = GetImplementation(scene).GetSceneObject();
791   if(!sceneObject)
792   {
793     return;
794   }
795
796   Rect<int> clippingRect = sceneObject->GetSurfaceRect();
797   RenderScene(status, scene, renderToFbo, clippingRect);
798 }
799
800 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
801 {
802   if(mImpl->partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE && !renderToFbo && clippingRect.IsEmpty())
803   {
804     // ClippingRect is empty. Skip rendering
805     DALI_LOG_DEBUG_INFO("ClippingRect is empty. Skip rendering\n");
806     return;
807   }
808
809   // Reset main algorithms command buffer
810   mImpl->renderAlgorithms.ResetCommandBuffer();
811
812   auto mainCommandBuffer = mImpl->renderAlgorithms.GetMainCommandBuffer();
813
814   Internal::Scene&   sceneInternal = GetImplementation(scene);
815   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
816   if(!sceneObject)
817   {
818     DALI_LOG_ERROR("Scene was empty handle. Skip rendering\n");
819     return;
820   }
821
822   uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
823
824   std::vector<Graphics::RenderTarget*> targetstoPresent;
825
826   Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
827   if(clippingRect == surfaceRect)
828   {
829     // Full rendering case
830     // Make clippingRect empty because we're doing full rendering now if the clippingRect is empty.
831     // To reduce side effects, keep this logic now.
832     clippingRect = Rect<int>();
833   }
834
835   // Prefetch programs before we start rendering so reflection is
836   // ready, and we can pull exact size of UBO needed (no need to resize during drawing)
837   auto totalSizeCPU = 0u;
838   auto totalSizeGPU = 0u;
839
840   for(uint32_t i = 0; i < instructionCount; ++i)
841   {
842     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
843
844     if((instruction.mFrameBuffer != nullptr && renderToFbo) ||
845        (instruction.mFrameBuffer == nullptr && !renderToFbo))
846     {
847       for(auto j = 0u; j < instruction.RenderListCount(); ++j)
848       {
849         const auto& renderList = instruction.GetRenderList(j);
850         for(auto k = 0u; k < renderList->Count(); ++k)
851         {
852           auto& item = renderList->GetItem(k);
853           if(item.mRenderer && item.mRenderer->NeedsProgram())
854           {
855             // Prepare and store used programs for further processing
856             auto program = item.mRenderer->PrepareProgram(instruction);
857             if(program)
858             {
859               const auto& memoryRequirements = program->GetUniformBlocksMemoryRequirements();
860
861               totalSizeCPU += memoryRequirements.totalCpuSizeRequired;
862               totalSizeGPU += memoryRequirements.totalGpuSizeRequired;
863             }
864           }
865         }
866       }
867     }
868   }
869
870   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Render scene (%s), CPU:%d GPU:%d\n", renderToFbo ? "Offscreen" : "Onscreen", totalSizeCPU, totalSizeGPU);
871
872   auto& uboManager = mImpl->uniformBufferManager;
873
874   uboManager->SetCurrentSceneRenderInfo(sceneObject, renderToFbo);
875   uboManager->Rollback(sceneObject, renderToFbo);
876
877   // Respec UBOs for this frame (orphan buffers or double buffer in the GPU)
878   if(instructionCount)
879   {
880     uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, true)->ReSpecify(totalSizeCPU);
881     uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, false)->ReSpecify(totalSizeGPU);
882   }
883
884 #if defined(DEBUG_ENABLED)
885   auto uniformBuffer1 = uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, true);
886   auto uniformBuffer2 = uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, false);
887   if(uniformBuffer1)
888   {
889     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "CPU buffer: Offset(%d), Cap(%d)\n", uniformBuffer1->GetCurrentOffset(), uniformBuffer1->GetCurrentCapacity());
890   }
891   else
892   {
893     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "CPU buffer: nil\n");
894   }
895   if(uniformBuffer2)
896   {
897     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GPU buffer: Offset(%d), Cap(%d)\n", uniformBuffer2->GetCurrentOffset(), uniformBuffer2->GetCurrentCapacity());
898   }
899   else
900   {
901     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GPU buffer: nil\n");
902   }
903 #endif
904
905   for(uint32_t i = 0; i < instructionCount; ++i)
906   {
907     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
908
909     if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
910     {
911       continue; // skip
912     }
913
914     // Mark that we will require a post-render step to be performed (includes swap-buffers).
915     status.SetNeedsPostRender(true);
916
917     Rect<int32_t> viewportRect;
918
919     int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation() + sceneObject->GetScreenOrientation();
920     if(surfaceOrientation >= 360)
921     {
922       surfaceOrientation -= 360;
923     }
924
925     // @todo Should these be part of scene?
926     Integration::DepthBufferAvailable   depthBufferAvailable   = mImpl->depthBufferAvailable;
927     Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
928
929     Graphics::RenderTarget*           currentRenderTarget = nullptr;
930     Graphics::RenderPass*             currentRenderPass   = nullptr;
931     std::vector<Graphics::ClearValue> currentClearValues{};
932
933     if(instruction.mFrameBuffer)
934     {
935       // Ensure graphics framebuffer is created, bind attachments and create render passes
936       // Only happens once per framebuffer. If the create fails, e.g. no attachments yet,
937       // then don't render to this framebuffer.
938       if(!instruction.mFrameBuffer->GetGraphicsObject())
939       {
940         const bool created = instruction.mFrameBuffer->CreateGraphicsObjects();
941         if(!created)
942         {
943           continue;
944         }
945       }
946
947       auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues();
948
949       // Set the clear color for first color attachment
950       if(instruction.mIsClearColorSet && !clearValues.empty())
951       {
952         clearValues[0].color = {
953           instruction.mClearColor.r,
954           instruction.mClearColor.g,
955           instruction.mClearColor.b,
956           instruction.mClearColor.a};
957       }
958
959       currentClearValues = clearValues;
960
961       auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
962
963       // offscreen buffer
964       currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget();
965       currentRenderPass   = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
966     }
967     else // no framebuffer
968     {
969       // surface
970       auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues();
971
972       if(instruction.mIsClearColorSet)
973       {
974         clearValues[0].color = {
975           instruction.mClearColor.r,
976           instruction.mClearColor.g,
977           instruction.mClearColor.b,
978           instruction.mClearColor.a};
979       }
980
981       currentClearValues = clearValues;
982
983       // @todo SceneObject should already have the depth clear / stencil clear in the clearValues array.
984       // if the window has a depth/stencil buffer.
985       if((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
986           stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) &&
987          (currentClearValues.size() <= 1))
988       {
989         currentClearValues.emplace_back();
990         currentClearValues.back().depthStencil.depth   = 0;
991         currentClearValues.back().depthStencil.stencil = 0;
992       }
993
994       auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
995
996       currentRenderTarget = sceneObject->GetSurfaceRenderTarget();
997       currentRenderPass   = sceneObject->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
998     }
999
1000     targetstoPresent.emplace_back(currentRenderTarget);
1001
1002     if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
1003     {
1004       // Offscreen buffer rendering
1005       if(instruction.mIsViewportSet)
1006       {
1007         // For Viewport the lower-left corner is (0,0)
1008         const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
1009         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
1010       }
1011       else
1012       {
1013         viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
1014       }
1015       surfaceOrientation = 0;
1016     }
1017     else // No Offscreen frame buffer rendering
1018     {
1019       // Check whether a viewport is specified, otherwise the full surface size is used
1020       if(instruction.mIsViewportSet)
1021       {
1022         // For Viewport the lower-left corner is (0,0)
1023         const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
1024         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
1025       }
1026       else
1027       {
1028         viewportRect = surfaceRect;
1029       }
1030     }
1031
1032     // Set surface orientation
1033     // @todo Inform graphics impl by another route.
1034     // was: mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
1035
1036     /*** Clear region of framebuffer or surface before drawing ***/
1037     bool clearFullFrameRect = (surfaceRect == viewportRect);
1038     if(instruction.mFrameBuffer != nullptr)
1039     {
1040       Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
1041       clearFullFrameRect = (frameRect == viewportRect);
1042     }
1043
1044     if(!clippingRect.IsEmpty())
1045     {
1046       if(!clippingRect.Intersect(viewportRect))
1047       {
1048         DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
1049         clippingRect = Rect<int>();
1050       }
1051       clearFullFrameRect = false;
1052     }
1053
1054     Graphics::Rect2D scissorArea{viewportRect.x, viewportRect.y, uint32_t(viewportRect.width), uint32_t(viewportRect.height)};
1055     if(instruction.mIsClearColorSet)
1056     {
1057       if(!clearFullFrameRect)
1058       {
1059         if(!clippingRect.IsEmpty())
1060         {
1061           scissorArea = {clippingRect.x, clippingRect.y, uint32_t(clippingRect.width), uint32_t(clippingRect.height)};
1062         }
1063       }
1064     }
1065
1066     // Scissor's value should be set based on the default system coordinates.
1067     // When the surface is rotated, the input values already were set with the rotated angle.
1068     // So, re-calculation is needed.
1069     scissorArea = RecalculateScissorArea(scissorArea, surfaceOrientation, surfaceRect);
1070
1071     // Begin render pass
1072     mainCommandBuffer->BeginRenderPass(
1073       currentRenderPass,
1074       currentRenderTarget,
1075       scissorArea,
1076       currentClearValues);
1077
1078     mainCommandBuffer->SetViewport({float(viewportRect.x),
1079                                     float(viewportRect.y),
1080                                     float(viewportRect.width),
1081                                     float(viewportRect.height)});
1082
1083     mImpl->renderAlgorithms.ProcessRenderInstruction(
1084       instruction,
1085       mImpl->renderBufferIndex,
1086       depthBufferAvailable,
1087       stencilBufferAvailable,
1088       viewportRect,
1089       clippingRect,
1090       surfaceOrientation,
1091       Uint16Pair(surfaceRect.width, surfaceRect.height));
1092
1093     Graphics::SyncObject* syncObject{nullptr};
1094     // If the render instruction has an associated render tracker (owned separately)
1095     // and framebuffer, create a one shot sync object, and use it to determine when
1096     // the render pass has finished executing on GPU.
1097     if(instruction.mRenderTracker && instruction.mFrameBuffer)
1098     {
1099       syncObject                 = instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController);
1100       instruction.mRenderTracker = nullptr;
1101     }
1102     mainCommandBuffer->EndRenderPass(syncObject);
1103   }
1104
1105   // Flush UBOs
1106   mImpl->uniformBufferManager->Flush(sceneObject, renderToFbo);
1107   mImpl->renderAlgorithms.SubmitCommandBuffer();
1108   mImpl->commandBufferSubmitted = true;
1109
1110   std::sort(targetstoPresent.begin(), targetstoPresent.end());
1111
1112   Graphics::RenderTarget* rt = nullptr;
1113   for(auto& target : targetstoPresent)
1114   {
1115     if(target != rt)
1116     {
1117       mImpl->graphicsController.PresentRenderTarget(target);
1118       rt = target;
1119     }
1120   }
1121 }
1122
1123 void RenderManager::PostRender()
1124 {
1125   if(!mImpl->commandBufferSubmitted)
1126   {
1127     // Rendering is skipped but there may be pending tasks. Flush them.
1128     Graphics::SubmitInfo submitInfo;
1129     submitInfo.cmdBuffer.clear(); // Only flush
1130     submitInfo.flags = 0 | Graphics::SubmitFlagBits::FLUSH;
1131     mImpl->graphicsController.SubmitCommandBuffers(submitInfo);
1132
1133     mImpl->commandBufferSubmitted = true;
1134   }
1135
1136   // Notify RenderGeometries that rendering has finished
1137   for(auto&& iter : mImpl->geometryContainer)
1138   {
1139     iter->OnRenderFinished();
1140   }
1141
1142   // Notify updated RenderTexture that rendering has finished
1143   for(auto&& iter : mImpl->updatedTextures)
1144   {
1145     iter->OnRenderFinished();
1146   }
1147   mImpl->updatedTextures.Clear();
1148
1149   // Remove discarded textures after OnRenderFinished called
1150   mImpl->textureDiscardQueue.Clear();
1151
1152   mImpl->UpdateTrackers();
1153
1154   uint32_t count = 0u;
1155   for(auto& scene : mImpl->sceneContainer)
1156   {
1157     count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
1158   }
1159
1160   // Remove unused shader and programs during CACHE_CLEAN_FRAME_COUNT frames.
1161   // TODO : Cache clean logic have some problem now. Just block it until bug resolved
1162   /*
1163   if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 0)
1164   {
1165     mImpl->programController.ClearUnusedCache();
1166   }
1167   */
1168
1169   const bool haveInstructions = count > 0u;
1170
1171   // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
1172   mImpl->lastFrameWasRendered = haveInstructions;
1173
1174   /**
1175    * The rendering has finished; swap to the next buffer.
1176    * Ideally the update has just finished using this buffer; otherwise the render thread
1177    * should block until the update has finished.
1178    */
1179   mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1180
1181   DALI_PRINT_RENDER_END();
1182 }
1183
1184 } // namespace SceneGraph
1185
1186 } // namespace Internal
1187
1188 } // namespace Dali