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