Revert "[Tizen] Revert "Support multiple window rendering""
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-geometry.cpp
index 5675db0..17ba522 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
  * limitations under the License.
  */
 
+// CLASS HEADER
 #include <dali/internal/render/renderers/render-geometry.h>
 
+// INTERNAL INCLUDES
 #include <dali/internal/common/buffer-index.h>
-#include <dali/internal/update/common/scene-graph-property-buffer.h>
-#include <dali/internal/update/rendering/scene-graph-geometry.h>
-#include <dali/internal/render/data-providers/render-data-provider.h>
 #include <dali/internal/render/gl-resources/context.h>
 #include <dali/internal/render/gl-resources/gpu-buffer.h>
+#include <dali/internal/render/renderers/render-property-buffer.h>
 #include <dali/internal/render/shaders/program.h>
 
 namespace Dali
 {
 namespace Internal
 {
-namespace SceneGraph
+namespace Render
 {
 
-RenderGeometry::RenderGeometry( const GeometryDataProvider& geometryDataProvider )
-: mGeometryDataProvider( geometryDataProvider ),
+Geometry::Geometry()
+: mIndices(),
+  mIndexBuffer(NULL),
+  mGeometryType( Dali::Geometry::TRIANGLES ),
+  mIndicesChanged(false),
   mHasBeenUpdated(false),
   mAttributesChanged(true)
 {
 }
 
-RenderGeometry::~RenderGeometry()
+Geometry::~Geometry()
 {
 }
 
-void RenderGeometry::GlContextCreated( Context& context )
+void Geometry::GlContextCreated( Context& context )
 {
 }
 
-void RenderGeometry::GlContextDestroyed()
+void Geometry::GlContextDestroyed()
 {
 }
 
-void RenderGeometry::AddPropertyBuffer( const PropertyBufferDataProvider* dataProvider, GpuBuffer::Target gpuBufferTarget, GpuBuffer::Usage gpuBufferUsage )
+void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer )
 {
-  if( gpuBufferTarget == GpuBuffer::ELEMENT_ARRAY_BUFFER )
-  {
-    RenderPropertyBuffer* renderPropertyBuffer = new RenderPropertyBuffer( *dataProvider, gpuBufferTarget, gpuBufferUsage );
-    mIndexBuffer = renderPropertyBuffer;
-  }
-  else if( gpuBufferTarget == GpuBuffer::ARRAY_BUFFER )
-  {
-    RenderPropertyBuffer* renderPropertyBuffer = new RenderPropertyBuffer( *dataProvider, gpuBufferTarget, gpuBufferUsage );
-    mVertexBuffers.PushBack( renderPropertyBuffer );
-    mAttributesChanged = true;
-  }
+  mVertexBuffers.PushBack( propertyBuffer );
+  mAttributesChanged = true;
 }
 
-void RenderGeometry::RemovePropertyBuffer( const PropertyBufferDataProvider* dataProvider )
+void Geometry::SetIndexBuffer( Dali::Vector<uint16_t>& indices )
 {
-  if( dataProvider == &mIndexBuffer->GetDataProvider() )
-  {
-    mIndexBuffer.Reset();
-  }
-  else
+  mIndices.Swap( indices );
+  mIndicesChanged = true;
+}
+
+void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
+{
+  const auto&& end = mVertexBuffers.End();
+  for( auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter )
   {
-    for( RenderPropertyBufferIter iter( mVertexBuffers.Begin() ); iter != mVertexBuffers.End(); ++iter )
+    if( *iter == propertyBuffer )
     {
-      if( dataProvider == &(*iter)->GetDataProvider() )
-      {
-        //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
-        mVertexBuffers.Remove( iter );
-        mAttributesChanged = true;
-        break;
-      }
+      //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
+      mVertexBuffers.Remove( iter );
+      mAttributesChanged = true;
+      break;
     }
   }
 }
 
-void RenderGeometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
+void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
 {
   attributeLocation.Clear();
 
-  for( size_t i(0); i< mVertexBuffers.Size(); ++i )
+  for( auto&& vertexBuffer : mVertexBuffers )
   {
-    unsigned int attributeCount = mVertexBuffers[i]->GetDataProvider().GetAttributeCount( bufferIndex );
-    for( unsigned int j = 0; j < attributeCount; ++j )
+    const uint32_t attributeCount = vertexBuffer->GetAttributeCount();
+    for( uint32_t j = 0; j < attributeCount; ++j )
     {
-      const std::string& attributeName = mVertexBuffers[i]->GetDataProvider().GetAttributeName( bufferIndex, j );
-      unsigned int index = program.RegisterCustomAttribute( attributeName );
+      const std::string& attributeName = vertexBuffer->GetAttributeName( j );
+      uint32_t index = program.RegisterCustomAttribute( attributeName );
       GLint location = program.GetCustomAttributeLocation( index );
 
       if( -1 == location )
@@ -109,118 +103,152 @@ void RenderGeometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLo
   }
 }
 
-void RenderGeometry::OnRenderFinished()
+void Geometry::OnRenderFinished()
 {
   mHasBeenUpdated = false;
   mAttributesChanged = false;
 }
 
-void RenderGeometry::UploadAndDraw(
-    Context& context,
-    BufferIndex bufferIndex,
-    Vector<GLint>& attributeLocation )
+void Geometry::Upload( Context& context )
 {
   if( !mHasBeenUpdated )
   {
-    //Update buffers
-    if( mIndexBuffer )
+    // Update buffers
+    if( mIndicesChanged )
     {
-      mIndexBuffer->Update( context, bufferIndex );
+      if( mIndices.Empty() )
+      {
+        mIndexBuffer = NULL;
+      }
+      else
+      {
+        if ( mIndexBuffer == NULL )
+        {
+          mIndexBuffer = new GpuBuffer( context );
+        }
+
+        uint32_t bufferSize = static_cast<uint32_t>( sizeof( uint16_t ) * mIndices.Size() );
+        mIndexBuffer->UpdateDataBuffer( context, bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
+      }
+
+      mIndicesChanged = false;
     }
-    for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+
+    for( auto&& buffer : mVertexBuffers )
     {
-      mVertexBuffers[i]->Update( context, bufferIndex );
+      if( !buffer->Update( context ) )
+      {
+        //Vertex buffer is not ready ( Size, data or format has not been specified yet )
+        return;
+      }
     }
+
     mHasBeenUpdated = true;
   }
+}
 
+void Geometry::Draw(
+    Context& context,
+    BufferIndex bufferIndex,
+    Vector<GLint>& attributeLocation,
+    uint32_t elementBufferOffset,
+    uint32_t elementBufferCount )
+{
   //Bind buffers to attribute locations
-  unsigned int base = 0;
-  for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+  uint32_t base = 0u;
+  const uint32_t vertexBufferCount = static_cast<uint32_t>( mVertexBuffers.Count() );
+  for( uint32_t i = 0; i < vertexBufferCount; ++i )
   {
-    mVertexBuffers[i]->BindBuffer( context );
-    base += mVertexBuffers[i]->EnableVertexAttributes( context, bufferIndex, attributeLocation, base );
+    mVertexBuffers[i]->BindBuffer( context, GpuBuffer::ARRAY_BUFFER );
+    base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
   }
 
+  uint32_t numIndices(0u);
+  intptr_t firstIndexOffset(0u);
   if( mIndexBuffer )
   {
-    mIndexBuffer->BindBuffer( context );
-  }
+    numIndices = static_cast<uint32_t>( mIndices.Size() );
 
-  //Bind index buffer
-  unsigned int numIndices(0u);
-  if( mIndexBuffer )
-  {
-    const PropertyBufferDataProvider& indexBuffer = mIndexBuffer->GetDataProvider();
-    numIndices = mIndexBuffer->GetDataProvider().GetDataSize(bufferIndex) / indexBuffer.GetElementSize(bufferIndex);
+    if( elementBufferOffset != 0u )
+    {
+      elementBufferOffset = (elementBufferOffset >= numIndices ) ? numIndices - 1 : elementBufferOffset;
+      firstIndexOffset = elementBufferOffset * sizeof(GLushort);
+      numIndices -= elementBufferOffset;
+    }
+
+    if( elementBufferCount != 0u )
+    {
+      numIndices = std::min( elementBufferCount, numIndices );
+    }
   }
 
-  //Draw call
-  GeometryDataProvider::GeometryType type = mGeometryDataProvider.GetGeometryType( bufferIndex );
-  switch(type)
+  GLenum geometryGLType(GL_NONE);
+  switch(mGeometryType)
   {
     case Dali::Geometry::TRIANGLES:
     {
-      if( numIndices )
-      {
-        context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
-      }
-      else
-      {
-        const PropertyBufferDataProvider& firstVertexBuffer = mVertexBuffers[0]->GetDataProvider();
-        unsigned int numVertices = firstVertexBuffer.GetElementCount( bufferIndex );
-        context.DrawArrays( GL_TRIANGLES, 0, numVertices );
-      }
+      geometryGLType = GL_TRIANGLES;
       break;
     }
     case Dali::Geometry::LINES:
     {
-      if( numIndices )
-      {
-        context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
-      }
-      else
-      {
-        const PropertyBufferDataProvider& firstVertexBuffer = mVertexBuffers[0]->GetDataProvider();
-        unsigned int numVertices = firstVertexBuffer.GetElementCount( bufferIndex );
-        context.DrawArrays( GL_LINES, 0, numVertices );
-      }
+      geometryGLType = GL_LINES;
       break;
     }
     case Dali::Geometry::POINTS:
     {
-      const PropertyBufferDataProvider& firstVertexBuffer = mVertexBuffers[0]->GetDataProvider();
-      unsigned int numVertices = firstVertexBuffer.GetElementCount( bufferIndex );
-      context.DrawArrays(GL_POINTS, 0, numVertices );
+      geometryGLType = GL_POINTS;
       break;
     }
     case Dali::Geometry::TRIANGLE_STRIP:
     {
-      const PropertyBufferDataProvider& firstVertexBuffer = mVertexBuffers[0]->GetDataProvider();
-      unsigned int numVertices = firstVertexBuffer.GetElementCount( bufferIndex );
-      context.DrawArrays(GL_TRIANGLE_STRIP, 0, numVertices );
+      geometryGLType = GL_TRIANGLE_STRIP;
       break;
     }
     case Dali::Geometry::TRIANGLE_FAN:
     {
-      const PropertyBufferDataProvider& firstVertexBuffer = mVertexBuffers[0]->GetDataProvider();
-      unsigned int numVertices = firstVertexBuffer.GetElementCount( bufferIndex );
-      context.DrawArrays(GL_TRIANGLE_FAN, 0, numVertices );
+      geometryGLType = GL_TRIANGLE_FAN;
+      break;
+    }
+    case Dali::Geometry::LINE_LOOP:
+    {
+      geometryGLType = GL_LINE_LOOP;
       break;
     }
-    default:
+    case Dali::Geometry::LINE_STRIP:
     {
-      DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
+      geometryGLType = GL_LINE_STRIP;
       break;
     }
   }
 
-  //Disable atrributes
-  for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
+  //Draw call
+  if( mIndexBuffer && geometryGLType != GL_POINTS )
+  {
+    //Indexed draw call
+    mIndexBuffer->Bind( context, GpuBuffer::ELEMENT_ARRAY_BUFFER );
+    // numIndices truncated, no value loss happening in practice
+    context.DrawElements( geometryGLType, static_cast<GLsizei>( numIndices ), GL_UNSIGNED_SHORT, reinterpret_cast<void*>( firstIndexOffset ) );
+  }
+  else
+  {
+    //Unindex draw call
+    GLsizei numVertices(0u);
+    if( vertexBufferCount > 0 )
+    {
+      // truncated, no value loss happening in practice
+      numVertices = static_cast<GLsizei>( mVertexBuffers[0]->GetElementCount() );
+    }
+
+    context.DrawArrays( geometryGLType, 0, numVertices );
+  }
+
+  //Disable attributes
+  for( auto&& attribute : attributeLocation )
   {
-    if( attributeLocation[i] != -1 )
+    if( attribute != -1 )
     {
-      context.DisableVertexAttributeArray( attributeLocation[i] );
+      context.DisableVertexAttributeArray( static_cast<GLuint>( attribute ) );
     }
   }
 }