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