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