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