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