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