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