Remove RenderSurface from Core
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / render-instruction-processor.cpp
index d8e0c0e..1e41c12 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
 #include <dali/internal/update/manager/render-instruction-processor.h>
 
 // INTERNAL INCLUDES
-#include <dali/devel-api/shader-effects/shader-effect.h>
 #include <dali/public-api/actors/layer.h>
 #include <dali/integration-api/debug.h>
 #include <dali/internal/event/actors/layer-impl.h> // for the default sorting function
 #include <dali/internal/update/manager/sorted-layers.h>
 #include <dali/internal/update/render-tasks/scene-graph-render-task.h>
 #include <dali/internal/update/rendering/scene-graph-texture-set.h>
-#include <dali/internal/update/resources/resource-manager-declarations.h>
 #include <dali/internal/render/common/render-item.h>
 #include <dali/internal/render/common/render-tracker.h>
 #include <dali/internal/render/common/render-instruction.h>
@@ -34,7 +32,6 @@
 #include <dali/internal/render/shaders/scene-graph-shader.h>
 #include <dali/internal/render/renderers/render-renderer.h>
 #include <dali/internal/render/renderers/render-property-buffer.h>
-#include <dali/internal/update/manager/geometry-batcher.h>
 #include <dali/internal/update/nodes/scene-graph-layer.h>
 
 namespace
@@ -57,47 +54,41 @@ namespace
 {
 
 /**
- * Function which sorts render items by depth index then by instance
- * ptrs of shader/geometry/material.
+ * Function which compares render items by shader/textureSet/geometry
  * @param[in] lhs Left hand side item
  * @param[in] rhs Right hand side item
  * @return True if left item is greater than right
  */
-bool CompareItems( const RenderInstructionProcessor::SortAttributes& lhs, const RenderInstructionProcessor::SortAttributes& rhs )
+inline bool PartialCompareItems( const RenderInstructionProcessor::SortAttributes& lhs,
+                                 const RenderInstructionProcessor::SortAttributes& rhs )
 {
-  // @todo Consider replacing all these sortAttributes with a single long int that
-  // encapsulates the same data (e.g. the middle-order bits of the ptrs).
-  if( lhs.renderItem->mDepthIndex == rhs.renderItem->mDepthIndex )
+  if( lhs.shader == rhs.shader )
   {
-    if( lhs.shader == rhs.shader )
+    if( lhs.textureSet == rhs.textureSet )
     {
-      if( lhs.textureResourceId == rhs.textureResourceId )
-      {
-        return lhs.geometry < rhs.geometry;
-      }
-      return lhs.textureResourceId < rhs.textureResourceId;
+      return lhs.geometry < rhs.geometry;
     }
-    return lhs.shader < rhs.shader;
+    return lhs.textureSet < rhs.textureSet;
   }
-  return lhs.renderItem->mDepthIndex < rhs.renderItem->mDepthIndex;
+  return lhs.shader < rhs.shader;
 }
 
 /**
- * Function which sorts render items by clipping hierarchy, then depth index and instance
- * ptrs of shader/geometry/material.
+ * Function which sorts render items by depth index then by instance
+ * ptrs of shader/textureSet/geometry.
  * @param[in] lhs Left hand side item
  * @param[in] rhs Right hand side item
  * @return True if left item is greater than right
  */
-bool CompareItemsWithClipping( const RenderInstructionProcessor::SortAttributes& lhs, const RenderInstructionProcessor::SortAttributes& rhs )
+bool CompareItems( const RenderInstructionProcessor::SortAttributes& lhs, const RenderInstructionProcessor::SortAttributes& rhs )
 {
-  // Items must be sorted in order of clipping first, otherwise incorrect clipping regions could be used.
-  if( lhs.renderItem->mNode->mClippingSortModifier == rhs.renderItem->mNode->mClippingSortModifier )
+  // @todo Consider replacing all these sortAttributes with a single long int that
+  // encapsulates the same data (e.g. the middle-order bits of the ptrs).
+  if( lhs.renderItem->mDepthIndex == rhs.renderItem->mDepthIndex )
   {
-    return CompareItems( lhs, rhs );
+    return PartialCompareItems( lhs, rhs );
   }
-
-  return lhs.renderItem->mNode->mClippingSortModifier < rhs.renderItem->mNode->mClippingSortModifier;
+  return lhs.renderItem->mDepthIndex < rhs.renderItem->mDepthIndex;
 }
 
 /**
@@ -109,36 +100,20 @@ bool CompareItemsWithClipping( const RenderInstructionProcessor::SortAttributes&
  */
 bool CompareItems3D( const RenderInstructionProcessor::SortAttributes& lhs, const RenderInstructionProcessor::SortAttributes& rhs )
 {
-  bool lhsIsOpaque = lhs.renderItem->mIsOpaque;
+  const bool lhsIsOpaque = lhs.renderItem->mIsOpaque;
   if( lhsIsOpaque == rhs.renderItem->mIsOpaque )
   {
     if( lhsIsOpaque )
     {
       // If both RenderItems are opaque, sort using shader, then material then geometry.
-      if( lhs.shader == rhs.shader )
-      {
-        if( lhs.textureResourceId == rhs.textureResourceId )
-        {
-          return lhs.geometry < rhs.geometry;
-        }
-        return lhs.textureResourceId < rhs.textureResourceId;
-      }
-      return lhs.shader < rhs.shader;
+      return PartialCompareItems( lhs, rhs );
     }
     else
     {
       // If both RenderItems are transparent, sort using Z, then shader, then material, then geometry.
       if( Equals( lhs.zValue, rhs.zValue ) )
       {
-        if( lhs.shader == rhs.shader )
-        {
-          if( lhs.textureResourceId == rhs.textureResourceId )
-          {
-            return lhs.geometry < rhs.geometry;
-          }
-          return lhs.textureResourceId < rhs.textureResourceId;
-        }
-        return lhs.shader < rhs.shader;
+        return PartialCompareItems( lhs, rhs );
       }
       return lhs.zValue > rhs.zValue;
     }
@@ -173,7 +148,6 @@ bool CompareItems3DWithClipping( const RenderInstructionProcessor::SortAttribute
  * @param renderable Node-Renderer pair
  * @param viewMatrix used to calculate modelview matrix for the item
  * @param camera The camera used to render
- * @param geometryBatcher The instance of the geometry batcher
  * @param isLayer3d Whether we are processing a 3D layer or not
  * @param cull Whether frustum culling is enabled or not
  */
@@ -182,24 +156,13 @@ inline void AddRendererToRenderList( BufferIndex updateBufferIndex,
                                      Renderable& renderable,
                                      const Matrix& viewMatrix,
                                      SceneGraph::Camera& camera,
-                                     GeometryBatcher& geometryBatcher,
                                      bool isLayer3d,
                                      bool cull )
 {
-  // Discard renderable early if it belongs to the batch which has been consumed in during frame.
-  Node* renderableNode = renderable.mNode;
-  const bool batchingValid( renderable.mRenderer->IsBatchingEnabled() && renderableNode->mBatchIndex != BATCH_NULL_HANDLE );
-  if( batchingValid && geometryBatcher.HasRendered( renderableNode->mBatchIndex ) )
-  {
-    return;
-  }
-
   bool inside( true );
-  const Node* batchParentNode = renderable.mNode->GetBatchParent();
-  const Node* node = ( renderable.mRenderer->IsBatchingEnabled() && batchParentNode ) ?
-        batchParentNode : renderableNode;
+  Node* node = renderable.mNode;
 
-  if( cull && !renderable.mRenderer->GetShader().HintEnabled( Dali::Shader::Hint::MODIFIES_GEOMETRY ) )
+  if( cull && renderable.mRenderer && !renderable.mRenderer->GetShader().HintEnabled( Dali::Shader::Hint::MODIFIES_GEOMETRY ) )
   {
     const Vector4& boundingSphere = node->GetBoundingSphere();
     inside = ( boundingSphere.w > Math::MACHINE_EPSILON_1000 ) &&
@@ -208,24 +171,30 @@ inline void AddRendererToRenderList( BufferIndex updateBufferIndex,
 
   if( inside )
   {
-    if( batchingValid )
-    {
-      geometryBatcher.SetRendered( renderableNode->mBatchIndex );
-    }
-
-    Renderer::Opacity opacity = renderable.mRenderer->GetOpacity( updateBufferIndex, *renderable.mNode );
-    if( opacity != Renderer::TRANSPARENT )
+    Renderer::OpacityType opacityType = renderable.mRenderer ? renderable.mRenderer->GetOpacityType( updateBufferIndex, *renderable.mNode ) : Renderer::OPAQUE;
+    if( opacityType != Renderer::TRANSPARENT || node->GetClippingMode() == ClippingMode::CLIP_CHILDREN )
     {
       // Get the next free RenderItem.
       RenderItem& item = renderList.GetNextFreeItem();
-      item.mRenderer = &renderable.mRenderer->GetRenderer();
+
       item.mNode = renderable.mNode;
-      item.mIsOpaque = ( opacity == Renderer::OPAQUE );
-      item.mDepthIndex = renderable.mRenderer->GetDepthIndex();
+      item.mIsOpaque = ( opacityType == Renderer::OPAQUE );
+      item.mDepthIndex = 0;
 
-      if( !isLayer3d )
+      if(!isLayer3d)
       {
-        item.mDepthIndex += static_cast<int>( renderable.mNode->GetDepth() ) * Dali::Layer::TREE_DEPTH_MULTIPLIER;
+        item.mDepthIndex = renderable.mNode->GetDepthIndex();
+      }
+
+      if( DALI_LIKELY( renderable.mRenderer ) )
+      {
+        item.mRenderer =   &renderable.mRenderer->GetRenderer();
+        item.mTextureSet =  renderable.mRenderer->GetTextures();
+        item.mDepthIndex += renderable.mRenderer->GetDepthIndex();
+      }
+      else
+      {
+        item.mRenderer = nullptr;
       }
 
       // Save ModelView matrix onto the item.
@@ -233,6 +202,12 @@ inline void AddRendererToRenderList( BufferIndex updateBufferIndex,
 
       Matrix::Multiply( item.mModelViewMatrix, item.mModelMatrix, viewMatrix );
     }
+
+     node->SetCulled( updateBufferIndex, false );
+  }
+  else
+  {
+     node->SetCulled( updateBufferIndex, true );
   }
 }
 
@@ -244,7 +219,6 @@ inline void AddRendererToRenderList( BufferIndex updateBufferIndex,
  * NodeRendererContainer Node-Renderer pairs
  * @param viewMatrix used to calculate modelview matrix for the items
  * @param camera The camera used to render
- * @param geometryBatcher The instance of the geometry batcher
  * @param isLayer3d Whether we are processing a 3D layer or not
  * @param cull Whether frustum culling is enabled or not
  */
@@ -253,16 +227,20 @@ inline void AddRenderersToRenderList( BufferIndex updateBufferIndex,
                                       RenderableContainer& renderers,
                                       const Matrix& viewMatrix,
                                       SceneGraph::Camera& camera,
-                                      GeometryBatcher* geometryBatcher,
                                       bool isLayer3d,
                                       bool cull )
 {
   DALI_LOG_INFO( gRenderListLogFilter, Debug::Verbose, "AddRenderersToRenderList()\n");
 
-  unsigned int rendererCount( renderers.Size() );
-  for( unsigned int i(0); i < rendererCount; ++i )
+  for( auto&& renderer : renderers )
   {
-    AddRendererToRenderList( updateBufferIndex, renderList, renderers[i], viewMatrix, camera, *geometryBatcher, isLayer3d, cull );
+    AddRendererToRenderList( updateBufferIndex,
+                             renderList,
+                             renderer,
+                             viewMatrix,
+                             camera,
+                             isLayer3d,
+                             cull );
   }
 }
 
@@ -279,7 +257,7 @@ inline bool TryReuseCachedRenderers( Layer& layer,
                                      RenderableContainer& renderables )
 {
   bool retValue = false;
-  size_t renderableCount = renderables.Size();
+  uint32_t renderableCount = static_cast<uint32_t>( renderables.Size() );
   // Check that the cached list originates from this layer and that the counts match
   if( ( renderList.GetSourceLayer() == &layer )&&
       ( renderList.GetCachedItemCount() == renderableCount ) )
@@ -289,11 +267,11 @@ inline bool TryReuseCachedRenderers( Layer& layer,
     // Therefore we check a combined sum of all renderer addresses.
     size_t checkSumNew = 0;
     size_t checkSumOld = 0;
-    for( size_t index = 0; index < renderableCount; ++index )
+    for( uint32_t index = 0; index < renderableCount; ++index )
     {
       const Render::Renderer& renderer = renderables[index].mRenderer->GetRenderer();
-      checkSumNew += size_t( &renderer );
-      checkSumOld += size_t( &renderList.GetRenderer( index ) );
+      checkSumNew += reinterpret_cast<std::size_t>( &renderer );
+      checkSumOld += reinterpret_cast<std::size_t>( &renderList.GetRenderer( index ) );
     }
     if( checkSumNew == checkSumOld )
     {
@@ -326,10 +304,9 @@ RenderInstructionProcessor::RenderInstructionProcessor()
 : mSortingHelper()
 {
   // Set up a container of comparators for fast run-time selection.
-  mSortComparitors.Reserve( 4u );
+  mSortComparitors.Reserve( 3u );
 
   mSortComparitors.PushBack( CompareItems );
-  mSortComparitors.PushBack( CompareItemsWithClipping );
   mSortComparitors.PushBack( CompareItems3D );
   mSortComparitors.PushBack( CompareItems3DWithClipping );
 }
@@ -340,9 +317,9 @@ RenderInstructionProcessor::~RenderInstructionProcessor()
 
 inline void RenderInstructionProcessor::SortRenderItems( BufferIndex bufferIndex, RenderList& renderList, Layer& layer, bool respectClippingOrder )
 {
-  const size_t renderableCount = renderList.Count();
+  const uint32_t renderableCount = static_cast<uint32_t>( renderList.Count() );
   // Reserve space if needed.
-  const unsigned int oldcapacity = mSortingHelper.size();
+  const uint32_t oldcapacity = static_cast<uint32_t>( mSortingHelper.size() );
   if( oldcapacity < renderableCount )
   {
     mSortingHelper.reserve( renderableCount );
@@ -361,14 +338,20 @@ inline void RenderInstructionProcessor::SortRenderItems( BufferIndex bufferIndex
   // Using an if and two for-loops rather than if inside for as its better for branch prediction.
   if( layer.UsesDefaultSortFunction() )
   {
-    for( size_t index = 0; index < renderableCount; ++index )
+    for( uint32_t index = 0; index < renderableCount; ++index )
     {
       RenderItem& item = renderList.GetItem( index );
 
-      item.mRenderer->SetSortAttributes( bufferIndex, mSortingHelper[ index ] );
+      if( item.mRenderer )
+      {
+        item.mRenderer->SetSortAttributes( bufferIndex, mSortingHelper[ index ] );
+      }
+
+      // texture set
+      mSortingHelper[ index ].textureSet = item.mTextureSet;
 
       // The default sorting function should get inlined here.
-      mSortingHelper[ index ].zValue = Internal::Layer::ZValue( item.mModelViewMatrix.GetTranslation3() ) - item.mDepthIndex;
+      mSortingHelper[ index ].zValue = Internal::Layer::ZValue( item.mModelViewMatrix.GetTranslation3() ) - static_cast<float>( item.mDepthIndex );
 
       // Keep the renderitem pointer in the helper so we can quickly reorder items after sort.
       mSortingHelper[ index ].renderItem = &item;
@@ -377,31 +360,35 @@ inline void RenderInstructionProcessor::SortRenderItems( BufferIndex bufferIndex
   else
   {
     const Dali::Layer::SortFunctionType sortFunction = layer.GetSortFunction();
-    for( size_t index = 0; index < renderableCount; ++index )
+    for( uint32_t index = 0; index < renderableCount; ++index )
     {
       RenderItem& item = renderList.GetItem( index );
 
       item.mRenderer->SetSortAttributes( bufferIndex, mSortingHelper[ index ] );
-      mSortingHelper[ index ].zValue = (*sortFunction)( item.mModelViewMatrix.GetTranslation3() ) - item.mDepthIndex;
+
+      // texture set
+      mSortingHelper[ index ].textureSet = item.mTextureSet;
+
+
+      mSortingHelper[ index ].zValue = (*sortFunction)( item.mModelViewMatrix.GetTranslation3() ) - static_cast<float>( item.mDepthIndex );
 
       // Keep the RenderItem pointer in the helper so we can quickly reorder items after sort.
       mSortingHelper[ index ].renderItem = &item;
     }
   }
 
-  // Here we detemine which comparitor (of the 4) to use.
-  // The comparitors work like a bitmask.
-  //   1 << 0  is added to select a clipping comparitor.
-  //   1 << 1  is added for 3D comparitors.
-  const unsigned int comparitorIndex = ( respectClippingOrder                         ? ( 1u << 0u ) : 0u ) |
-                                       ( layer.GetBehavior() == Dali::Layer::LAYER_3D ? ( 1u << 1u ) : 0u );
+  // Here we determine which comparitor (of the 3) to use.
+  //   0 is LAYER_UI
+  //   1 is LAYER_3D
+  //   2 is LAYER_3D + Clipping
+  const unsigned int comparitorIndex = layer.GetBehavior() == Dali::Layer::LAYER_3D ? respectClippingOrder ? 2u : 1u : 0u;
 
   std::stable_sort( mSortingHelper.begin(), mSortingHelper.end(), mSortComparitors[ comparitorIndex ] );
 
   // Reorder / re-populate the RenderItems in the RenderList to correct order based on the sortinghelper.
   DALI_LOG_INFO( gRenderListLogFilter, Debug::Verbose, "Sorted Transparent List:\n");
   RenderItemContainer::Iterator renderListIter = renderList.GetContainer().Begin();
-  for( unsigned int index = 0; index < renderableCount; ++index, ++renderListIter )
+  for( uint32_t index = 0; index < renderableCount; ++index, ++renderListIter )
   {
     *renderListIter = mSortingHelper[ index ].renderItem;
     DALI_LOG_INFO( gRenderListLogFilter, Debug::Verbose, "  sortedList[%d] = %p\n", index, mSortingHelper[ index ].renderItem->mRenderer);
@@ -410,17 +397,18 @@ inline void RenderInstructionProcessor::SortRenderItems( BufferIndex bufferIndex
 
 void RenderInstructionProcessor::Prepare( BufferIndex updateBufferIndex,
                                           SortedLayerPointers& sortedLayers,
+                                          Context& context,
                                           RenderTask& renderTask,
                                           bool cull,
-                                          GeometryBatcher& geometryBatcher,
                                           bool hasClippingNodes,
                                           RenderInstructionContainer& instructions )
 {
   // Retrieve the RenderInstruction buffer from the RenderInstructionContainer
   // then populate with instructions.
-  RenderInstruction& instruction = instructions.GetNextInstruction( updateBufferIndex );
-  renderTask.PrepareRenderInstruction( instruction, updateBufferIndex );
+  RenderInstruction& instruction = renderTask.PrepareRenderInstruction( updateBufferIndex );
   bool viewMatrixHasNotChanged = !renderTask.ViewMatrixUpdated();
+  bool isRenderListAdded = false;
+  bool isRootLayerDirty = false;
 
   const Matrix& viewMatrix = renderTask.GetViewMatrix( updateBufferIndex );
   SceneGraph::Camera& camera = renderTask.GetCamera();
@@ -433,6 +421,12 @@ void RenderInstructionProcessor::Prepare( BufferIndex updateBufferIndex,
     const bool isLayer3D = layer.GetBehavior() == Dali::Layer::LAYER_3D;
     RenderList* renderList = NULL;
 
+    if( layer.IsRoot() && ( layer.GetDirtyFlags() != NodePropertyFlags::NOTHING ) )
+    {
+      // If root-layer & dirty, i.e. a property has changed or a child has been deleted, then we need to ensure we render once more
+      isRootLayerDirty = true;
+    }
+
     if( !layer.colorRenderables.Empty() )
     {
       RenderableContainer& renderables = layer.colorRenderables;
@@ -440,11 +434,19 @@ void RenderInstructionProcessor::Prepare( BufferIndex updateBufferIndex,
       if( !SetupRenderList( renderables, layer, instruction, tryReuseRenderList, &renderList ) )
       {
         renderList->SetHasColorRenderItems( true );
-        AddRenderersToRenderList( updateBufferIndex, *renderList, renderables, viewMatrix, camera, &geometryBatcher, isLayer3D, cull );
+        AddRenderersToRenderList( updateBufferIndex,
+                                  *renderList,
+                                  renderables,
+                                  viewMatrix,
+                                  camera,
+                                  isLayer3D,
+                                  cull );
 
         // We only use the clipping version of the sort comparitor if any clipping nodes exist within the RenderList.
         SortRenderItems( updateBufferIndex, *renderList, layer, hasClippingNodes );
       }
+
+      isRenderListAdded = true;
     }
 
     if( !layer.overlayRenderables.Empty() )
@@ -454,18 +456,31 @@ void RenderInstructionProcessor::Prepare( BufferIndex updateBufferIndex,
       if( !SetupRenderList( renderables, layer, instruction, tryReuseRenderList, &renderList ) )
       {
         renderList->SetHasColorRenderItems( false );
-        AddRenderersToRenderList( updateBufferIndex, *renderList, renderables, viewMatrix, camera, NULL, isLayer3D, cull );
+        AddRenderersToRenderList( updateBufferIndex,
+                                  *renderList,
+                                  renderables,
+                                  viewMatrix,
+                                  camera,
+                                  isLayer3D,
+                                  cull );
 
         // Clipping hierarchy is irrelevant when sorting overlay items, so we specify using the non-clipping version of the sort comparitor.
         SortRenderItems( updateBufferIndex, *renderList, layer, false );
       }
+
+      isRenderListAdded = true;
     }
   }
 
   // Inform the render instruction that all renderers have been added and this frame is complete.
   instruction.UpdateCompleted();
-}
 
+  if( isRenderListAdded || instruction.mIsClearColorSet || isRootLayerDirty )
+  {
+    instruction.mContext = &context;
+    instructions.PushBack( updateBufferIndex, &instruction );
+  }
+}
 
 } // SceneGraph