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