[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
index 630f870..e878e8a 100755 (executable)
@@ -62,10 +62,38 @@ Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_REN
 } // unnamed namespace
 #endif
 
-const int partialUpdateMargin = 4u;
+namespace
+{
 const float partialUpdateRatio = 0.8f; // If the partial update area exceeds 80%, change to full update.
 
 /**
+ * @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;
+
+  // 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 );
+
+  // 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 );
+
+  return intersectionBox;
+}
+
+}
+
+/**
  * Structure to contain internal data
  */
 struct RenderManager::Impl
@@ -84,7 +112,6 @@ struct RenderManager::Impl
     renderQueue(),
     instructions(),
     renderAlgorithms(),
-    backgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
     frameCount( 0u ),
     renderBufferIndex( SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX ),
     defaultSurfaceRect(),
@@ -154,8 +181,6 @@ struct RenderManager::Impl
   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.
-
   uint32_t                                  frameCount;              ///< The current frame count
   BufferIndex                               renderBufferIndex;       ///< The index of the buffer to read from; this is opposite of the "update" buffer
 
@@ -260,11 +285,6 @@ RenderInstructionContainer& RenderManager::GetRenderInstructionContainer()
   return mImpl->instructions;
 }
 
-void RenderManager::SetBackgroundColor( const Vector4& color )
-{
-  mImpl->backgroundColor = color;
-}
-
 void RenderManager::SetDefaultSurfaceRect(const Rect<int32_t>& rect)
 {
   mImpl->defaultSurfaceRect = rect;
@@ -475,7 +495,7 @@ ProgramCache* RenderManager::GetProgramCache()
   return &(mImpl->programController);
 }
 
-void RenderManager::Render( Integration::RenderStatus& status, bool forceClear )
+void RenderManager::Render( Integration::RenderStatus& status, bool forceClear, bool uploadOnly )
 {
   DALI_PRINT_RENDER_START( mImpl->renderBufferIndex );
 
@@ -502,8 +522,11 @@ void RenderManager::Render( Integration::RenderStatus& status, bool forceClear )
   {
     DALI_LOG_INFO( gLogFilter, Debug::General, "Render: Processing\n" );
 
-    // Mark that we will require a post-render step to be performed (includes swap-buffers).
-    status.SetNeedsPostRender( true );
+    if ( !uploadOnly )
+    {
+      // Mark that we will require a post-render step to be performed (includes swap-buffers).
+      status.SetNeedsPostRender( true );
+    }
 
     // Switch to the shared context
     if ( mImpl->currentContext != &mImpl->context )
@@ -555,23 +578,26 @@ void RenderManager::Render( Integration::RenderStatus& status, bool forceClear )
       }
     }
 
-    for( uint32_t i = 0; i < count; ++i )
+    if ( !uploadOnly )
     {
-      RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i );
+      for( uint32_t i = 0; i < count; ++i )
+      {
+        RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i );
 
-      DoRender( instruction );
-    }
+        DoRender( instruction );
+      }
 
-    if ( mImpl->currentContext->IsSurfacelessContextSupported() )
-    {
-      mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
-    }
+      if ( mImpl->currentContext->IsSurfacelessContextSupported() )
+      {
+        mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
+      }
 
-    GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
-    mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
-    for ( auto&& context : mImpl->surfaceContextContainer )
-    {
-      context->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
+      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
@@ -646,10 +672,27 @@ bool GetDamagedRect( Rect<int32_t> &viewportRect, RenderInstruction& instruction
 
   if( isPartialUpdate )
   {
-    damagedRect.x = dx1 - partialUpdateMargin;
-    damagedRect.y = dy1 - partialUpdateMargin;
-    damagedRect.width = dx2 - dx1 + ( 2 * partialUpdateMargin );
-    damagedRect.height = dy2 - dy1 + ( 2 * partialUpdateMargin );
+    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;
@@ -660,9 +703,10 @@ void RenderManager::DoRender( RenderInstruction& instruction )
   Rect<int32_t> viewportRect;
   Vector4   clearColor;
   bool isPartialUpdate = false;
-  Rect<int32_t> damagedRect;
-  Rect<int32_t> mergedRect;
+  Dali::DamagedRect damagedRect;
+  Dali::DamagedRect mergedRect;
   Dali::ClippingBox scissorBox;
+  Dali::ClippingBox intersectRect;
 
   if ( instruction.mIsClearColorSet )
   {
@@ -675,7 +719,6 @@ void RenderManager::DoRender( RenderInstruction& instruction )
 
   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;
@@ -708,7 +751,6 @@ void RenderManager::DoRender( RenderInstruction& instruction )
       }
 
       surfaceRect = Rect<int32_t>( 0, 0, static_cast<int32_t>( surfaceFrameBuffer->GetWidth() ), static_cast<int32_t>( surfaceFrameBuffer->GetHeight() ) );
-      backgroundColor = surfaceFrameBuffer->GetBackgroundColor();
     }
     else
     {
@@ -736,7 +778,10 @@ void RenderManager::DoRender( RenderInstruction& instruction )
     {
       // 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() );
+      for (unsigned int i0 = 0, i1 = textureFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
+      {
+        mImpl->textureDependencyList.PushBack( textureFrameBuffer->GetTextureId(i0) );
+      }
     }
   }
   else
@@ -748,7 +793,6 @@ void RenderManager::DoRender( RenderInstruction& instruction )
   if( surfaceFrameBuffer &&
       partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE )
   {
-    const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
     // Iterate through each render list.
     if( surfaceFrameBuffer->IsPartialUpdateEnabled() )
     {
@@ -760,7 +804,7 @@ void RenderManager::DoRender( RenderInstruction& instruction )
       damagedRect = surfaceRect;
     }
 
-    mergedRect = surfaceFrameBuffer->SetDamagedRect( damagedRect );
+    surfaceFrameBuffer->SetDamagedRect( damagedRect, mergedRect );
 
     if( mergedRect.IsEmpty() )
     {
@@ -777,30 +821,16 @@ void RenderManager::DoRender( RenderInstruction& instruction )
 
   if ( surfaceFrameBuffer )
   {
-      mImpl->currentContext->Viewport( surfaceRect.x,
-                                surfaceRect.y,
-                                surfaceRect.width,
-                                surfaceRect.height );
-
-
-      mImpl->currentContext->ClearColor( backgroundColor.r,
-                                  backgroundColor.g,
-                                  backgroundColor.b,
-                                  backgroundColor.a );
+    mImpl->currentContext->Viewport( surfaceRect.x,
+                              surfaceRect.y,
+                              surfaceRect.width,
+                              surfaceRect.height );
   }
 
   // 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.
-  mImpl->currentContext->SetScissorTest( false );
-
-  if( isPartialUpdate )
-  {
-    mImpl->currentContext->SetScissorTest( true );
-    mImpl->currentContext->Scissor( scissorBox.x, scissorBox.y, scissorBox.width, scissorBox.height );
-  }
-
   GLbitfield clearMask = GL_COLOR_BUFFER_BIT;
 
   mImpl->currentContext->ColorMask( true );
@@ -818,14 +848,6 @@ void RenderManager::DoRender( RenderInstruction& instruction )
     clearMask |= GL_STENCIL_BUFFER_BIT;
   }
 
-  mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR );
-
-  if( isPartialUpdate )
-  {
-    mImpl->currentContext->SetScissorTest( false );
-  }
-
-
   if( !instruction.mIgnoreRenderToFbo && ( instruction.mFrameBuffer != 0 ) )
   {
     if ( instruction.mFrameBuffer->IsSurfaceBacked() ) // Surface rendering
@@ -879,6 +901,17 @@ void RenderManager::DoRender( RenderInstruction& instruction )
     }
   }
 
+  bool clearFullFrameRect = true;
+  if( instruction.mFrameBuffer != 0 )
+  {
+    Viewport frameRect( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
+    clearFullFrameRect = ( frameRect == viewportRect );
+  }
+  else
+  {
+    clearFullFrameRect = ( surfaceRect == viewportRect );
+  }
+
   if ( surfaceOrientation == 90 || surfaceOrientation == 270 )
   {
     int temp = viewportRect.width;
@@ -888,26 +921,33 @@ void RenderManager::DoRender( RenderInstruction& instruction )
 
   mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
 
-  if ( instruction.mIsClearColorSet )
+  if( instruction.mIsClearColorSet )
   {
     mImpl->currentContext->ClearColor( clearColor.r,
                                        clearColor.g,
                                        clearColor.b,
                                        clearColor.a );
 
-    // Clear the viewport area only
-    mImpl->currentContext->SetScissorTest( true );
-    if( isPartialUpdate )
+    if( !clearFullFrameRect )
     {
-      mImpl->currentContext->Scissor( scissorBox.x, scissorBox.y, scissorBox.width, scissorBox.height );
+      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->Clear( clearMask, Context::FORCE_CLEAR );
+      mImpl->currentContext->SetScissorTest( false );
     }
     else
     {
-      mImpl->currentContext->Scissor( viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height );
+      mImpl->currentContext->SetScissorTest( false );
+      mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR );
     }
-    mImpl->currentContext->ColorMask( true );
-    mImpl->currentContext->Clear( GL_COLOR_BUFFER_BIT , Context::CHECK_CACHED_VALUES );
-    mImpl->currentContext->SetScissorTest( false );
   }
 
   // Clear the list of bound textures