[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 253bb77..986e430
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // CLASS HEADER
 #include <dali/internal/render/common/render-manager.h>
 
+// EXTERNAL INCLUDES
+#include <memory.h>
+
 // INTERNAL INCLUDES
 #include <dali/public-api/actors/sampling.h>
 #include <dali/public-api/common/dali-common.h>
 #include <dali/public-api/common/stage.h>
 #include <dali/public-api/render-tasks/render-task.h>
+#include <dali/devel-api/threading/thread-pool.h>
 #include <dali/integration-api/debug.h>
 #include <dali/integration-api/core.h>
+#include <dali/integration-api/gl-context-helper-abstraction.h>
 #include <dali/internal/common/owner-pointer.h>
 #include <dali/internal/render/common/render-algorithms.h>
 #include <dali/internal/render/common/render-debug.h>
@@ -34,6 +39,8 @@
 #include <dali/internal/render/gl-resources/context.h>
 #include <dali/internal/render/queue/render-queue.h>
 #include <dali/internal/render/renderers/render-frame-buffer.h>
+#include <dali/internal/render/renderers/render-texture-frame-buffer.h>
+#include <dali/internal/render/renderers/render-surface-frame-buffer.h>
 #include <dali/internal/render/renderers/render-geometry.h>
 #include <dali/internal/render/renderers/render-renderer.h>
 #include <dali/internal/render/renderers/render-sampler.h>
@@ -48,27 +55,43 @@ namespace Internal
 namespace SceneGraph
 {
 
-typedef OwnerContainer< Render::Renderer* >    RendererOwnerContainer;
-typedef RendererOwnerContainer::Iterator       RendererOwnerIter;
+#if defined(DEBUG_ENABLED)
+namespace
+{
+Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER" );
+} // unnamed namespace
+#endif
 
-typedef OwnerContainer< Render::Geometry* >    GeometryOwnerContainer;
-typedef GeometryOwnerContainer::Iterator       GeometryOwnerIter;
+namespace
+{
+const float partialUpdateRatio = 0.8f; // If the partial update area exceeds 80%, change to full update.
 
-typedef OwnerContainer< Render::Sampler* >    SamplerOwnerContainer;
-typedef SamplerOwnerContainer::Iterator       SamplerOwnerIter;
+/**
+ * @brief Find the intersection of two AABB rectangles.
+ * This is a logical AND operation. IE. The intersection is the area overlapped by both rectangles.
+ * @param[in]     aabbA                  Rectangle A
+ * @param[in]     aabbB                  Rectangle B
+ * @return                               The intersection of rectangle A & B (result is a rectangle)
+ */
+inline ClippingBox IntersectAABB( const ClippingBox& aabbA, const ClippingBox& aabbB )
+{
+  ClippingBox intersectionBox;
 
-typedef OwnerContainer< Render::Texture* >   TextureOwnerContainer;
-typedef TextureOwnerContainer::Iterator         TextureOwnerIter;
+  // First calculate the largest starting positions in X and Y.
+  intersectionBox.x = std::max( aabbA.x, aabbB.x );
+  intersectionBox.y = std::max( aabbA.y, aabbB.y );
 
-typedef OwnerContainer< Render::FrameBuffer* >  FrameBufferOwnerContainer;
-typedef FrameBufferOwnerContainer::Iterator     FrameBufferOwnerIter;
+  // Now calculate the smallest ending positions, and take the largest starting
+  // positions from the result, to get the width and height respectively.
+  // If the two boxes do not intersect at all, then we need a 0 width and height clipping area.
+  // We use max here to clamp both width and height to >= 0 for this use-case.
+  intersectionBox.width =  std::max( std::min( aabbA.x + aabbA.width,  aabbB.x + aabbB.width  ) - intersectionBox.x, 0 );
+  intersectionBox.height = std::max( std::min( aabbA.y + aabbA.height, aabbB.y + aabbB.height ) - intersectionBox.y, 0 );
 
-typedef OwnerContainer< Render::PropertyBuffer* > PropertyBufferOwnerContainer;
-typedef PropertyBufferOwnerContainer::Iterator    PropertyBufferOwnerIter;
+  return intersectionBox;
+}
 
-typedef OwnerContainer< Render::RenderTracker* > RenderTrackerContainer;
-typedef RenderTrackerContainer::Iterator         RenderTrackerIter;
-typedef RenderTrackerContainer::ConstIterator    RenderTrackerConstIter;
+}
 
 /**
  * Structure to contain internal data
@@ -76,26 +99,42 @@ typedef RenderTrackerContainer::ConstIterator    RenderTrackerConstIter;
 struct RenderManager::Impl
 {
   Impl( Integration::GlAbstraction& glAbstraction,
-        Integration::GlSyncAbstraction& glSyncAbstraction )
-  : context( glAbstraction ),
+        Integration::GlSyncAbstraction& glSyncAbstraction,
+        Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
+        Integration::DepthBufferAvailable depthBufferAvailableParam,
+        Integration::StencilBufferAvailable stencilBufferAvailableParam,
+        Integration::PartialUpdateAvailable partialUpdateAvailableParam )
+  : context( glAbstraction, &surfaceContextContainer ),
+    currentContext( &context ),
+    glAbstraction( glAbstraction ),
     glSyncAbstraction( glSyncAbstraction ),
+    glContextHelperAbstraction( glContextHelperAbstraction ),
     renderQueue(),
     instructions(),
+    renderAlgorithms(),
     backgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
-    frameCount( 0 ),
+    frameCount( 0u ),
     renderBufferIndex( SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX ),
     defaultSurfaceRect(),
     rendererContainer(),
     samplerContainer(),
     textureContainer(),
     frameBufferContainer(),
-    renderersAdded( false ),
-    programController( glAbstraction )
+    lastFrameWasRendered( false ),
+    programController( glAbstraction ),
+    depthBufferAvailable( depthBufferAvailableParam ),
+    stencilBufferAvailable( stencilBufferAvailableParam ),
+    partialUpdateAvailable( partialUpdateAvailableParam ),
+    defaultSurfaceOrientation( 0 )
   {
+     // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
+    threadPool = std::unique_ptr<Dali::ThreadPool>( new Dali::ThreadPool() );
+    threadPool->Initialize( 1u );
   }
 
   ~Impl()
   {
+    threadPool.reset( nullptr ); // reset now to maintain correct destruction order
   }
 
   void AddRenderTracker( Render::RenderTracker* renderTracker )
@@ -109,52 +148,85 @@ struct RenderManager::Impl
     mRenderTrackers.EraseObject( renderTracker );
   }
 
+  Context* CreateSurfaceContext()
+  {
+    surfaceContextContainer.PushBack( new Context( glAbstraction ) );
+    return surfaceContextContainer[ surfaceContextContainer.Count() - 1 ];
+  }
+
+  void DestroySurfaceContext( Context* surfaceContext )
+  {
+    surfaceContextContainer.EraseObject( surfaceContext );
+  }
+
   void UpdateTrackers()
   {
-    for(RenderTrackerIter iter = mRenderTrackers.Begin(), end = mRenderTrackers.End(); iter != end; ++iter)
+    for( auto&& iter : mRenderTrackers )
     {
-      (*iter)->PollSyncObject();
+      iter->PollSyncObject();
     }
   }
 
   // the order is important for destruction,
   // programs are owned by context at the moment.
-  Context                       context;                  ///< holds the GL state
-  Integration::GlSyncAbstraction& glSyncAbstraction;      ///< GL sync abstraction
-  RenderQueue                   renderQueue;              ///< A message queue for receiving messages from the update-thread.
+  Context                                   context;                 ///< Holds the GL state of the share resource context
+  Context*                                  currentContext;          ///< Holds the GL state of the current context for rendering
+  OwnerContainer< Context* >                surfaceContextContainer; ///< List of owned contexts holding the GL state per surface
+  Integration::GlAbstraction&               glAbstraction;           ///< GL abstraction
+  Integration::GlSyncAbstraction&           glSyncAbstraction;       ///< GL sync abstraction
+  Integration::GlContextHelperAbstraction&  glContextHelperAbstraction; ///< GL context helper abstraction
+  RenderQueue                               renderQueue;             ///< A message queue for receiving messages from the update-thread.
 
   // Render instructions describe what should be rendered during RenderManager::Render()
   // Owned by RenderManager. Update manager updates instructions for the next frame while we render the current one
-  RenderInstructionContainer    instructions;
+  RenderInstructionContainer                instructions;
+  Render::RenderAlgorithms                  renderAlgorithms;        ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
+
+  Vector4                                   backgroundColor;         ///< The glClear color used at the beginning of each frame.
 
-  Vector4                       backgroundColor;          ///< The glClear color used at the beginning of each frame.
+  uint32_t                                  frameCount;              ///< The current frame count
+  BufferIndex                               renderBufferIndex;       ///< The index of the buffer to read from; this is opposite of the "update" buffer
 
-  unsigned int                  frameCount;               ///< The current frame count
-  BufferIndex                   renderBufferIndex;        ///< The index of the buffer to read from; this is opposite of the "update" buffer
+  Rect<int32_t>                             defaultSurfaceRect;      ///< Rectangle for the default surface we are rendering to
 
-  Rect<int>                     defaultSurfaceRect;       ///< Rectangle for the default surface we are rendering to
+  OwnerContainer< Render::Renderer* >       rendererContainer;       ///< List of owned renderers
+  OwnerContainer< Render::Sampler* >        samplerContainer;        ///< List of owned samplers
+  OwnerContainer< Render::Texture* >        textureContainer;        ///< List of owned textures
+  OwnerContainer< Render::FrameBuffer* >    frameBufferContainer;    ///< List of owned framebuffers
+  OwnerContainer< Render::PropertyBuffer* > propertyBufferContainer; ///< List of owned property buffers
+  OwnerContainer< Render::Geometry* >       geometryContainer;       ///< List of owned Geometries
 
-  RendererOwnerContainer        rendererContainer;        ///< List of owned renderers
-  SamplerOwnerContainer         samplerContainer;         ///< List of owned samplers
-  TextureOwnerContainer         textureContainer;         ///< List of owned textures
-  FrameBufferOwnerContainer     frameBufferContainer;     ///< List of owned framebuffers
-  PropertyBufferOwnerContainer  propertyBufferContainer;  ///< List of owned property buffers
-  GeometryOwnerContainer        geometryContainer;        ///< List of owned Geometries
+  bool                                      lastFrameWasRendered;    ///< Keeps track of the last frame being rendered due to having render instructions
 
-  bool                          renderersAdded;
+  OwnerContainer< Render::RenderTracker* >  mRenderTrackers;         ///< List of render trackers
 
-  RenderTrackerContainer        mRenderTrackers;          ///< List of render trackers
+  ProgramController                         programController;        ///< Owner of the GL programs
 
-  ProgramController             programController;        ///< Owner of the GL programs
+  Integration::DepthBufferAvailable         depthBufferAvailable;     ///< Whether the depth buffer is available
+  Integration::StencilBufferAvailable       stencilBufferAvailable;   ///< Whether the stencil buffer is available
+  Integration::PartialUpdateAvailable       partialUpdateAvailable;   ///< Whether the partial update is available
+
+  std::unique_ptr<Dali::ThreadPool>         threadPool;               ///< The thread pool
+  Vector<GLuint>                            boundTextures;            ///< The textures bound for rendering
+  Vector<GLuint>                            textureDependencyList;    ///< The dependency list of binded textures
+  int                                       defaultSurfaceOrientation; ///< defaultSurfaceOrientation for the default surface we are rendering to
 
 };
 
 RenderManager* RenderManager::New( Integration::GlAbstraction& glAbstraction,
-                                   Integration::GlSyncAbstraction& glSyncAbstraction )
+                                   Integration::GlSyncAbstraction& glSyncAbstraction,
+                                   Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
+                                   Integration::DepthBufferAvailable depthBufferAvailable,
+                                   Integration::StencilBufferAvailable stencilBufferAvailable,
+                                   Integration::PartialUpdateAvailable partialUpdateAvailable )
 {
   RenderManager* manager = new RenderManager;
   manager->mImpl = new Impl( glAbstraction,
-                             glSyncAbstraction );
+                             glSyncAbstraction,
+                             glContextHelperAbstraction,
+                             depthBufferAvailable,
+                             stencilBufferAvailable,
+                             partialUpdateAvailable );
   return manager;
 }
 
@@ -188,24 +260,21 @@ void RenderManager::ContextDestroyed()
   mImpl->programController.GlContextDestroyed();
 
   //Inform textures
-  for( TextureOwnerIter iter = mImpl->textureContainer.Begin(); iter != mImpl->textureContainer.End(); ++iter )
+  for( auto&& texture : mImpl->textureContainer )
   {
-    (*iter)->GlContextDestroyed();
+    texture->GlContextDestroyed();
   }
 
   //Inform framebuffers
-  for( FrameBufferOwnerIter iter = mImpl->frameBufferContainer.Begin(); iter != mImpl->frameBufferContainer.End(); ++iter )
+  for( auto&& framebuffer : mImpl->frameBufferContainer )
   {
-    (*iter)->GlContextDestroyed();
+    framebuffer->GlContextDestroyed();
   }
 
   // inform renderers
-  RendererOwnerContainer::Iterator end = mImpl->rendererContainer.End();
-  RendererOwnerContainer::Iterator iter = mImpl->rendererContainer.Begin();
-  for( ; iter != end; ++iter )
+  for( auto&& renderer : mImpl->rendererContainer )
   {
-    GlResourceOwner* renderer = *iter;
-    renderer->GlContextDestroyed(); // Clear up vertex buffers
+    renderer->GlContextDestroyed();
   }
 }
 
@@ -224,22 +293,22 @@ void RenderManager::SetBackgroundColor( const Vector4& color )
   mImpl->backgroundColor = color;
 }
 
-void RenderManager::SetDefaultSurfaceRect(const Rect<int>& rect)
+void RenderManager::SetDefaultSurfaceRect(const Rect<int32_t>& rect)
 {
   mImpl->defaultSurfaceRect = rect;
 }
 
+void RenderManager::SetDefaultSurfaceOrientation( int orientation )
+{
+  mImpl->defaultSurfaceOrientation = orientation;
+}
+
 void RenderManager::AddRenderer( OwnerPointer< Render::Renderer >& renderer )
 {
   // Initialize the renderer as we are now in render thread
   renderer->Initialize( mImpl->context );
 
   mImpl->rendererContainer.PushBack( renderer.Release() );
-
-  if( !mImpl->renderersAdded )
-  {
-    mImpl->renderersAdded = true;
-  }
 }
 
 void RenderManager::RemoveRenderer( Render::Renderer* renderer )
@@ -267,16 +336,14 @@ void RenderManager::RemoveTexture( Render::Texture* texture )
 {
   DALI_ASSERT_DEBUG( NULL != texture );
 
-  TextureOwnerContainer& textures = mImpl->textureContainer;
-
-  // Find the texture
-  for ( TextureOwnerIter iter = textures.Begin(); iter != textures.End(); ++iter )
+  // Find the texture, use reference to pointer so we can do the erase safely
+  for ( auto&& iter : mImpl->textureContainer )
   {
-    if ( *iter == texture )
+    if ( iter == texture )
     {
       texture->Destroy( mImpl->context );
-      textures.Erase( iter ); // Texture found; now destroy it
-      break;
+      mImpl->textureContainer.Erase( &iter ); // Texture found; now destroy it
+      return;
     }
   }
 }
@@ -291,46 +358,64 @@ void RenderManager::GenerateMipmaps( Render::Texture* texture )
   texture->GenerateMipmaps( mImpl->context );
 }
 
-void RenderManager::SetFilterMode( Render::Sampler* sampler, unsigned int minFilterMode, unsigned int magFilterMode )
+void RenderManager::SetFilterMode( Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode )
 {
   sampler->mMinificationFilter = static_cast<Dali::FilterMode::Type>(minFilterMode);
   sampler->mMagnificationFilter = static_cast<Dali::FilterMode::Type>(magFilterMode );
 }
 
-void RenderManager::SetWrapMode( Render::Sampler* sampler, unsigned int rWrapMode, unsigned int sWrapMode, unsigned int tWrapMode )
+void RenderManager::SetWrapMode( Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode )
 {
   sampler->mRWrapMode = static_cast<Dali::WrapMode::Type>(rWrapMode);
   sampler->mSWrapMode = static_cast<Dali::WrapMode::Type>(sWrapMode);
   sampler->mTWrapMode = static_cast<Dali::WrapMode::Type>(tWrapMode);
 }
 
-void RenderManager::AddFrameBuffer( Render::FrameBuffer* frameBuffer )
+void RenderManager::AddFrameBuffer( OwnerPointer< Render::FrameBuffer >& frameBuffer )
 {
-  mImpl->frameBufferContainer.PushBack( frameBuffer );
-  frameBuffer->Initialize(mImpl->context);
+  Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
+  mImpl->frameBufferContainer.PushBack( frameBufferPtr );
+  if ( frameBufferPtr->IsSurfaceBacked() )
+  {
+    frameBufferPtr->Initialize( *mImpl->CreateSurfaceContext() );
+  }
+  else
+  {
+    frameBufferPtr->Initialize( mImpl->context );
+  }
 }
 
 void RenderManager::RemoveFrameBuffer( Render::FrameBuffer* frameBuffer )
 {
   DALI_ASSERT_DEBUG( NULL != frameBuffer );
 
-  FrameBufferOwnerContainer& framebuffers = mImpl->frameBufferContainer;
-
-  // Find the sampler
-  for ( FrameBufferOwnerIter iter = framebuffers.Begin(); iter != framebuffers.End(); ++iter )
+  // Find the sampler, use reference so we can safely do the erase
+  for ( auto&& iter : mImpl->frameBufferContainer )
   {
-    if ( *iter == frameBuffer )
+    if ( iter == frameBuffer )
     {
       frameBuffer->Destroy( mImpl->context );
-      framebuffers.Erase( iter ); // frameBuffer found; now destroy it
+
+      if ( frameBuffer->IsSurfaceBacked() )
+      {
+        auto surfaceFrameBuffer = static_cast<Render::SurfaceFrameBuffer*>( frameBuffer );
+        mImpl->DestroySurfaceContext( surfaceFrameBuffer->GetContext() );
+      }
+
+      mImpl->frameBufferContainer.Erase( &iter ); // frameBuffer found; now destroy it
+
       break;
     }
   }
 }
 
-void RenderManager::AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, unsigned int mipmapLevel, unsigned int layer )
+void RenderManager::AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
 {
-  frameBuffer->AttachColorTexture( mImpl->context, texture, mipmapLevel, layer );
+  if ( !frameBuffer->IsSurfaceBacked() )
+  {
+    auto textureFrameBuffer = static_cast<Render::TextureFrameBuffer*>( frameBuffer );
+    textureFrameBuffer->AttachColorTexture( mImpl->context, texture, mipmapLevel, layer );
+  }
 }
 
 void RenderManager::AddPropertyBuffer( OwnerPointer< Render::PropertyBuffer >& propertyBuffer )
@@ -348,12 +433,12 @@ void RenderManager::SetPropertyBufferFormat( Render::PropertyBuffer* propertyBuf
   propertyBuffer->SetFormat( format.Release() );
 }
 
-void RenderManager::SetPropertyBufferData( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Vector<char> >& data, size_t size )
+void RenderManager::SetPropertyBufferData( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Vector<uint8_t> >& data, uint32_t size )
 {
   propertyBuffer->SetData( data.Release(), size );
 }
 
-void RenderManager::SetIndexBuffer( Render::Geometry* geometry, Dali::Vector<unsigned short>& indices )
+void RenderManager::SetIndexBuffer( Render::Geometry* geometry, Dali::Vector<uint16_t>& indices )
 {
   geometry->SetIndexBuffer( indices );
 }
@@ -372,14 +457,12 @@ void RenderManager::AttachVertexBuffer( Render::Geometry* geometry, Render::Prop
 {
   DALI_ASSERT_DEBUG( NULL != geometry );
 
-  GeometryOwnerContainer& geometries = mImpl->geometryContainer;
-
-  // Find the renderer
-  for ( GeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
+  // Find the geometry
+  for ( auto&& iter : mImpl->geometryContainer )
   {
-    if ( *iter == geometry )
+    if ( iter == geometry )
     {
-      (*iter)->AddPropertyBuffer( propertyBuffer );
+      iter->AddPropertyBuffer( propertyBuffer );
       break;
     }
   }
@@ -389,20 +472,18 @@ void RenderManager::RemoveVertexBuffer( Render::Geometry* geometry, Render::Prop
 {
   DALI_ASSERT_DEBUG( NULL != geometry );
 
-  GeometryOwnerContainer& geometries = mImpl->geometryContainer;
-
-  // Find the renderer
-  for ( GeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
+  // Find the geometry
+  for ( auto&& iter : mImpl->geometryContainer )
   {
-    if ( *iter == geometry )
+    if ( iter == geometry )
     {
-      (*iter)->RemovePropertyBuffer( propertyBuffer );
+      iter->RemovePropertyBuffer( propertyBuffer );
       break;
     }
   }
 }
 
-void RenderManager::SetGeometryType( Render::Geometry* geometry, unsigned int geometryType )
+void RenderManager::SetGeometryType( Render::Geometry* geometry, uint32_t geometryType )
 {
   geometry->SetType( Render::Geometry::Type(geometryType) );
 }
@@ -422,7 +503,7 @@ ProgramCache* RenderManager::GetProgramCache()
   return &(mImpl->programController);
 }
 
-void RenderManager::Render( Integration::RenderStatus& status )
+void RenderManager::Render( Integration::RenderStatus& status, bool forceClear )
 {
   DALI_PRINT_RENDER_START( mImpl->renderBufferIndex );
 
@@ -430,58 +511,113 @@ void RenderManager::Render( Integration::RenderStatus& status )
   DALI_ASSERT_DEBUG( mImpl->context.IsGlContextCreated() );
 
   // Increment the frame count at the beginning of each frame
-  ++(mImpl->frameCount);
+  ++mImpl->frameCount;
 
   // Process messages queued during previous update
   mImpl->renderQueue.ProcessMessages( mImpl->renderBufferIndex );
 
-  // switch rendering to adaptor provided (default) buffer
-  mImpl->context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
+  const uint32_t count = mImpl->instructions.Count( mImpl->renderBufferIndex );
+  const bool haveInstructions = count > 0u;
 
-  mImpl->context.Viewport( mImpl->defaultSurfaceRect.x,
-                           mImpl->defaultSurfaceRect.y,
-                           mImpl->defaultSurfaceRect.width,
-                           mImpl->defaultSurfaceRect.height );
+  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" );
 
-  mImpl->context.ClearColor( mImpl->backgroundColor.r,
-                             mImpl->backgroundColor.g,
-                             mImpl->backgroundColor.b,
-                             mImpl->backgroundColor.a );
+  // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
+  if( haveInstructions || mImpl->lastFrameWasRendered || forceClear )
+  {
+    DALI_LOG_INFO( gLogFilter, Debug::General, "Render: Processing\n" );
 
-  mImpl->context.ClearStencil( 0 );
+    // Mark that we will require a post-render step to be performed (includes swap-buffers).
+    status.SetNeedsPostRender( true );
 
-  // Clear the entire color, depth and stencil buffers for the default framebuffer.
-  // It is important to clear all 3 buffers, for performance on deferred renderers like Mali
-  // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit",
-  // and then stall. That problem is only noticeable when rendering a large number of vertices per frame.
-  mImpl->context.SetScissorTest( false );
-  mImpl->context.ColorMask( true );
-  mImpl->context.DepthMask( true );
-  mImpl->context.StencilMask( 0xFF ); // 8 bit stencil mask, all 1's
-  mImpl->context.Clear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT,  Context::FORCE_CLEAR );
+    // Switch to the shared context
+    if ( mImpl->currentContext != &mImpl->context )
+    {
+      mImpl->currentContext = &mImpl->context;
 
-  // reset the program matrices for all programs once per frame
-  // this ensures we will set view and projection matrix once per program per camera
-  mImpl->programController.ResetProgramMatrices();
+      if ( mImpl->currentContext->IsSurfacelessContextSupported() )
+      {
+        mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
+      }
 
-  size_t count = mImpl->instructions.Count( mImpl->renderBufferIndex );
-  for ( size_t i = 0; i < count; ++i )
-  {
-    RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i );
+      // Clear the current cached program when the context is switched
+      mImpl->programController.ClearCurrentProgram();
+    }
 
-    DoRender( instruction );
-  }
-  GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
-  mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
+    // Upload the geometries
+    for( uint32_t i = 0; i < count; ++i )
+    {
+      RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i );
+
+      const Matrix* viewMatrix       = instruction.GetViewMatrix( mImpl->renderBufferIndex );
+      const Matrix* projectionMatrix = instruction.GetProjectionMatrix( mImpl->renderBufferIndex );
+
+      DALI_ASSERT_DEBUG( viewMatrix );
+      DALI_ASSERT_DEBUG( projectionMatrix );
+
+      if( viewMatrix && projectionMatrix )
+      {
+        const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
+
+        // Iterate through each render list.
+        for( RenderListContainer::SizeType index = 0; index < renderListCount; ++index )
+        {
+          const RenderList* renderList = instruction.GetRenderList( index );
+
+          if( renderList && !renderList->IsEmpty() )
+          {
+            const std::size_t itemCount = renderList->Count();
+            for( uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex )
+            {
+              const RenderItem& item = renderList->GetItem( itemIndex );
+              if( DALI_LIKELY( item.mRenderer ) )
+              {
+                item.mRenderer->Upload( *mImpl->currentContext );
+              }
+            }
+          }
+        }
+      }
+    }
 
-  mImpl->UpdateTrackers();
+    for( uint32_t i = 0; i < count; ++i )
+    {
+      RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i );
+
+      DoRender( instruction );
+    }
+
+    if ( mImpl->currentContext->IsSurfacelessContextSupported() )
+    {
+      mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
+    }
 
-  //Notify RenderGeometries that rendering has finished
-  for ( GeometryOwnerIter iter = mImpl->geometryContainer.Begin(); iter != mImpl->geometryContainer.End(); ++iter )
+    GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
+    mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
+    for ( auto&& context : mImpl->surfaceContextContainer )
+    {
+      context->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
+    }
+
+    //Notify RenderGeometries that rendering has finished
+    for ( auto&& iter : mImpl->geometryContainer )
+    {
+      iter->OnRenderFinished();
+    }
+  }
+  else
   {
-    (*iter)->OnRenderFinished();
+    DALI_LOG_RELEASE_INFO( "RenderManager::Render: Skip rendering [%d, %d, %d]\n", haveInstructions, mImpl->lastFrameWasRendered, forceClear );
   }
 
+  mImpl->UpdateTrackers();
+
+  // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
+  mImpl->lastFrameWasRendered = haveInstructions;
+
   /**
    * The rendering has finished; swap to the next buffer.
    * Ideally the update has just finished using this buffer; otherwise the render thread
@@ -492,10 +628,87 @@ void RenderManager::Render( Integration::RenderStatus& status )
   DALI_PRINT_RENDER_END();
 }
 
+bool GetDamagedRect( Rect<int32_t> &viewportRect, RenderInstruction& instruction, Rect<int32_t> &damagedRect )
+{
+  // merge bounding
+  int dx1 = viewportRect.width, dx2 = 0, dy1 = viewportRect.height, dy2 = 0;
+  int checkWidth = static_cast<int>( static_cast<float>( viewportRect.width ) * partialUpdateRatio );
+  int checkHeight = static_cast<int>( static_cast<float>( viewportRect.height ) * partialUpdateRatio );
+  Rect<int32_t> screenRect;
+
+  bool isPartialUpdate = false;
+
+  const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
+  // Iterate through each render list.
+
+  for( RenderListContainer::SizeType index = 0; index < renderListCount; ++index )
+  {
+    const RenderList* renderList = instruction.GetRenderList( index );
+
+    if( renderList && !renderList->IsEmpty() && renderList->IsPartialUpdateEnabled() )
+    {
+      const std::size_t itemCount = renderList->Count();
+      for( uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex )
+      {
+        const RenderItem& item = renderList->GetItem( itemIndex );
+
+        if( item.mPartialUpdateEnabled )
+        {
+          isPartialUpdate = true;
+
+          screenRect = item.CalculateViewportSpaceAABB( viewportRect.width, viewportRect.height, true );
+
+          dx1 = std::min( screenRect.x, dx1 );
+          dx2 = std::max( screenRect.x + screenRect.width, dx2);
+          dy1 = std::min( screenRect.y, dy1 );
+          dy2 = std::max( screenRect.y + screenRect.height, dy2 );
+
+          if( ( dx2 - dx1 )  > checkWidth && ( dy2 - dy1 ) > checkHeight )
+          {
+            return false;
+          }
+        }
+      }
+    }
+  }
+
+  if( isPartialUpdate )
+  {
+    if( dx1 < 0.0f )
+    {
+      dx1 = 0.0f;
+    }
+    if( dy1 < 0.0f )
+    {
+      dy1 = 0.0f;
+    }
+    if( dx2 > viewportRect.width )
+    {
+      dx2 = viewportRect.width;
+    }
+    if( dy2 > viewportRect.height )
+    {
+      dy2 = viewportRect.height;
+    }
+
+    damagedRect.x = dx1;
+    damagedRect.y = dy1;
+    damagedRect.width = dx2 - dx1;
+    damagedRect.height = dy2 - dy1;
+  }
+
+  return isPartialUpdate;
+}
+
 void RenderManager::DoRender( RenderInstruction& instruction )
 {
-  Rect<int> viewportRect;
+  Rect<int32_t> viewportRect;
   Vector4   clearColor;
+  bool isPartialUpdate = false;
+  Rect<int32_t> damagedRect;
+  Rect<int32_t> mergedRect;
+  Dali::ClippingBox scissorBox;
+  Dali::ClippingBox intersectRect;
 
   if ( instruction.mIsClearColorSet )
   {
@@ -506,60 +719,323 @@ void RenderManager::DoRender( RenderInstruction& instruction )
     clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR;
   }
 
-  if( instruction.mFrameBuffer != 0 )
+  Rect<int32_t> surfaceRect = mImpl->defaultSurfaceRect;
+  int surfaceOrientation = mImpl->defaultSurfaceOrientation;
+  Vector4 backgroundColor = mImpl->backgroundColor;
+  Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable;
+  Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
+  Integration::PartialUpdateAvailable partialUpdateAvailable = mImpl->partialUpdateAvailable;
+
+  Render::SurfaceFrameBuffer* surfaceFrameBuffer = nullptr;
+  if ( instruction.mFrameBuffer != 0 )
+  {
+    if ( instruction.mFrameBuffer->IsSurfaceBacked() )
+    {
+      surfaceFrameBuffer = static_cast<Render::SurfaceFrameBuffer*>( instruction.mFrameBuffer );
+
+      if ( !surfaceFrameBuffer->IsSurfaceValid() )
+      {
+        // Skip rendering the frame buffer if the render surface becomes invalid
+        return;
+      }
+
+      if ( mImpl->currentContext->IsSurfacelessContextSupported() )
+      {
+        Context* surfaceContext = surfaceFrameBuffer->GetContext();
+        if ( mImpl->currentContext != surfaceContext )
+        {
+          // Switch the correct context if rendering to a surface
+          mImpl->currentContext = surfaceContext;
+          surfaceFrameBuffer->MakeContextCurrent();
+
+          // Clear the current cached program when the context is switched
+          mImpl->programController.ClearCurrentProgram();
+        }
+      }
+
+      surfaceRect = Rect<int32_t>( 0, 0, static_cast<int32_t>( surfaceFrameBuffer->GetWidth() ), static_cast<int32_t>( surfaceFrameBuffer->GetHeight() ) );
+      backgroundColor = surfaceFrameBuffer->GetBackgroundColor();
+    }
+    else
+    {
+      // Switch to shared context for off-screen buffer
+      mImpl->currentContext = &mImpl->context;
+
+      if ( mImpl->currentContext->IsSurfacelessContextSupported() )
+      {
+        mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
+      }
+    }
+  }
+
+  DALI_ASSERT_DEBUG( mImpl->currentContext->IsGlContextCreated() );
+
+  // reset the program matrices for all programs once per frame
+  // this ensures we will set view and projection matrix once per program per camera
+  mImpl->programController.ResetProgramMatrices();
+
+  if( instruction.mFrameBuffer )
+  {
+    instruction.mFrameBuffer->Bind( *mImpl->currentContext );
+
+    if ( !instruction.mFrameBuffer->IsSurfaceBacked() )
+    {
+      // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
+      Render::TextureFrameBuffer* textureFrameBuffer = static_cast<Render::TextureFrameBuffer*>( instruction.mFrameBuffer );
+      mImpl->textureDependencyList.PushBack( textureFrameBuffer->GetTextureId() );
+    }
+  }
+  else
+  {
+    mImpl->currentContext->BindFramebuffer( GL_FRAMEBUFFER, 0u );
+  }
+
+
+  if( surfaceFrameBuffer &&
+      partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE )
   {
-    instruction.mFrameBuffer->Bind( mImpl->context );
-    if ( instruction.mIsViewportSet )
+    const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
+    // Iterate through each render list.
+    if( surfaceFrameBuffer->IsPartialUpdateEnabled() )
+    {
+      isPartialUpdate = GetDamagedRect( surfaceRect, instruction, damagedRect ) ;
+    }
+
+    if( !isPartialUpdate )
+    {
+      damagedRect = surfaceRect;
+    }
+
+    mergedRect = surfaceFrameBuffer->SetDamagedRect( damagedRect );
+
+    if( mergedRect.IsEmpty() )
     {
-      // For glViewport the lower-left corner is (0,0)
-      const int y = ( instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height ) - instruction.mViewport.y;
-      viewportRect.Set( instruction.mViewport.x,  y, instruction.mViewport.width, instruction.mViewport.height );
+      isPartialUpdate = false;
     }
     else
     {
-      viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
+      scissorBox.x = mergedRect.x;
+      scissorBox.y = mergedRect.y;
+      scissorBox.width = mergedRect.width;
+      scissorBox.height = mergedRect.height;
     }
   }
-  else // !(instruction.mOffscreenTexture)
+
+  if ( surfaceFrameBuffer )
+  {
+      mImpl->currentContext->Viewport( surfaceRect.x,
+                                surfaceRect.y,
+                                surfaceRect.width,
+                                surfaceRect.height );
+
+
+      mImpl->currentContext->ClearColor( backgroundColor.r,
+                                  backgroundColor.g,
+                                  backgroundColor.b,
+                                  backgroundColor.a );
+  }
+
+  // Clear the entire color, depth and stencil buffers for the default framebuffer, if required.
+  // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers
+  // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit",
+  // and then stall. That problem is only noticeable when rendering a large number of vertices per frame.
+  if( isPartialUpdate )
+  {
+    mImpl->currentContext->SetScissorTest( true );
+    mImpl->currentContext->Scissor( scissorBox.x, scissorBox.y, scissorBox.width, scissorBox.height );
+  }
+  else
+  {
+    mImpl->currentContext->SetScissorTest( false );
+  }
+
+  GLbitfield clearMask = GL_COLOR_BUFFER_BIT;
+
+  mImpl->currentContext->ColorMask( true );
+
+  if( depthBufferAvailable == Integration::DepthBufferAvailable::TRUE )
+  {
+    mImpl->currentContext->DepthMask( true );
+    clearMask |= GL_DEPTH_BUFFER_BIT;
+  }
+
+  if( stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
+  {
+    mImpl->currentContext->ClearStencil( 0 );
+    mImpl->currentContext->StencilMask( 0xFF ); // 8 bit stencil mask, all 1's
+    clearMask |= GL_STENCIL_BUFFER_BIT;
+  }
+
+  mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR );
+
+  if( isPartialUpdate )
   {
-    // switch rendering to adaptor provided (default) buffer
-    mImpl->context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
+    mImpl->currentContext->SetScissorTest( false );
+  }
+
 
+  if( !instruction.mIgnoreRenderToFbo && ( instruction.mFrameBuffer != 0 ) )
+  {
+    if ( instruction.mFrameBuffer->IsSurfaceBacked() ) // Surface rendering
+    {
+      if ( instruction.mIsViewportSet )
+      {
+        // For glViewport the lower-left corner is (0,0)
+        // For glViewport the lower-left corner is (0,0)
+        const int32_t y = ( surfaceRect.height - instruction.mViewport.height ) - instruction.mViewport.y;
+        viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height );
+      }
+      else
+      {
+        viewportRect = surfaceRect;
+      }
+    }
+    else // Offscreen buffer rendering
+    {
+      if ( instruction.mIsViewportSet )
+      {
+        // For glViewport the lower-left corner is (0,0)
+        const int32_t y = ( instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height ) - instruction.mViewport.y;
+        viewportRect.Set( instruction.mViewport.x,  y, instruction.mViewport.width, instruction.mViewport.height );
+      }
+      else
+      {
+        viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
+      }
+      surfaceOrientation = 0;
+    }
+  }
+  else // No Offscreen frame buffer rendering
+  {
     // Check whether a viewport is specified, otherwise the full surface size is used
-    if ( instruction.mIsViewportSet )
+    if ( instruction.mFrameBuffer != 0 )
     {
-      // For glViewport the lower-left corner is (0,0)
-      const int y = ( mImpl->defaultSurfaceRect.height - instruction.mViewport.height ) - instruction.mViewport.y;
-      viewportRect.Set( instruction.mViewport.x,  y, instruction.mViewport.width, instruction.mViewport.height );
+      if ( instruction.mIsViewportSet )
+      {
+        // For glViewport the lower-left corner is (0,0)
+        const int32_t y = ( instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height ) - instruction.mViewport.y;
+        viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height );
+      }
+      else
+      {
+        viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
+      }
     }
     else
     {
-      viewportRect = mImpl->defaultSurfaceRect;
+      viewportRect = surfaceRect;
     }
   }
 
-  mImpl->context.Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
+  if ( surfaceOrientation == 90 || surfaceOrientation == 270 )
+  {
+    int temp = viewportRect.width;
+    viewportRect.width = viewportRect.height;
+    viewportRect.height = temp;
+  }
+
+  mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
 
   if ( instruction.mIsClearColorSet )
   {
-    mImpl->context.ClearColor( clearColor.r,
-                               clearColor.g,
-                               clearColor.b,
-                               clearColor.a );
+    mImpl->currentContext->ClearColor( clearColor.r,
+                                       clearColor.g,
+                                       clearColor.b,
+                                       clearColor.a );
 
     // Clear the viewport area only
-    mImpl->context.SetScissorTest( true );
-    mImpl->context.Scissor( viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height );
-    mImpl->context.ColorMask( true );
-    mImpl->context.Clear( GL_COLOR_BUFFER_BIT , Context::CHECK_CACHED_VALUES );
-    mImpl->context.SetScissorTest( false );
+    mImpl->currentContext->SetScissorTest( true );
+    if( isPartialUpdate )
+    {
+      intersectRect = IntersectAABB( scissorBox, viewportRect );
+      mImpl->currentContext->Scissor( intersectRect.x, intersectRect.y, intersectRect.width, intersectRect.height );
+    }
+    else
+    {
+      mImpl->currentContext->Scissor( viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height );
+    }
+    mImpl->currentContext->ColorMask( true );
+    mImpl->currentContext->Clear( GL_COLOR_BUFFER_BIT , Context::CHECK_CACHED_VALUES );
+    mImpl->currentContext->SetScissorTest( false );
   }
 
-  Render::ProcessRenderInstruction( instruction,
-                                    mImpl->context,
-                                    mImpl->renderBufferIndex );
+  // Clear the list of bound textures
+  mImpl->boundTextures.Clear();
+
+  mImpl->renderAlgorithms.ProcessRenderInstruction(
+      instruction,
+      *mImpl->currentContext,
+      mImpl->renderBufferIndex,
+      depthBufferAvailable,
+      stencilBufferAvailable,
+      mImpl->boundTextures,
+      surfaceOrientation,
+      scissorBox );
+
+  // Synchronise the FBO/Texture access when there are multiple contexts
+  if ( mImpl->currentContext->IsSurfacelessContextSupported() )
+  {
+    // Check whether any binded texture is in the dependency list
+    bool textureFound = false;
+
+    if ( mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u )
+    {
+      for ( auto textureId : mImpl->textureDependencyList )
+      {
+
+        textureFound = std::find_if( mImpl->boundTextures.Begin(), mImpl->boundTextures.End(),
+                                     [textureId]( GLuint id )
+                                     {
+                                       return textureId == id;
+                                     } ) != mImpl->boundTextures.End();
+      }
+    }
+
+    if ( textureFound )
+    {
+
+      if ( !instruction.mFrameBuffer || !instruction.mFrameBuffer->IsSurfaceBacked() )
+      {
+        // For off-screen buffer
+
+        // Wait until all rendering calls for the currently context are executed
+        mImpl->glContextHelperAbstraction.WaitClient();
+
+        // Clear the dependency list
+        mImpl->textureDependencyList.Clear();
+      }
+      else
+      {
+        // For surface-backed frame buffer
+
+        // Worker thread lambda function
+        auto& glContextHelperAbstraction = mImpl->glContextHelperAbstraction;
+        auto workerFunction = [&glContextHelperAbstraction]( int workerThread )
+        {
+          // Switch to the shared context in the worker thread
+          glContextHelperAbstraction.MakeSurfacelessContextCurrent();
+
+          // Wait until all rendering calls for the shared context are executed
+          glContextHelperAbstraction.WaitClient();
+
+          // Must clear the context in the worker thread
+          // Otherwise the shared context cannot be switched to from the render thread
+          glContextHelperAbstraction.MakeContextNull();
+        };
+
+        auto future = mImpl->threadPool->SubmitTask( 0u, workerFunction );
+        if ( future )
+        {
+          mImpl->threadPool->Wait();
+
+          // Clear the dependency list
+          mImpl->textureDependencyList.Clear();
+        }
+      }
+    }
+  }
 
-  if( instruction.mRenderTracker && ( instruction.mFrameBuffer != NULL ) )
+  if( instruction.mRenderTracker && ( instruction.mFrameBuffer != 0 ) )
   {
     // This will create a sync object every frame this render tracker
     // is alive (though it should be now be created only for
@@ -567,6 +1043,15 @@ void RenderManager::DoRender( RenderInstruction& instruction )
     instruction.mRenderTracker->CreateSyncObject( mImpl->glSyncAbstraction );
     instruction.mRenderTracker = NULL; // Only create once.
   }
+
+  if ( surfaceFrameBuffer )
+  {
+    surfaceFrameBuffer->PostRender();
+  }
+  else
+  {
+    mImpl->currentContext->Flush();
+  }
 }
 
 } // namespace SceneGraph