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