Moved framebuffer messages from UpdateManager to FrameBuffer
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-geometry.cpp
index 5d6cb1a..2be9653 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.
  */
 
 // CLASS HEADER
-#include "scene-graph-geometry.h"
+#include <dali/internal/update/rendering/scene-graph-geometry.h>
 
 // INTERNAL HEADERS
-#include <dali/internal/update/controllers/scene-controller.h>
-#include <dali/internal/render/renderers/render-geometry.h>
-#include <dali/internal/update/controllers/render-message-dispatcher.h>
+#include <dali/integration-api/debug.h>
+#include <dali/internal/common/buffer-index.h>
+#include <dali/internal/update/rendering/scene-graph-property-buffer.h>
 
 namespace Dali
 {
@@ -30,310 +30,51 @@ namespace SceneGraph
 {
 
 Geometry::Geometry()
-: mIndexBuffer( NULL ),
-  mRenderGeometry(0),
-  mSceneController(0),
-  mRendererRefCount(0u),
-  mCenter(),
-  mGeometryType(Dali::Geometry::TRIANGLES),
-  mRequiresDepthTest(false)
+: mGraphics( nullptr ),
+  mIndices(),
+  mGeometryType( Dali::Geometry::TRIANGLES ),
+  mIndicesChanged(false),
+  mHasBeenUpdated(false),
+  mAttributesChanged(true)
 {
-
-  // Observe our own PropertyOwner's uniform map
-  AddUniformMapObserver( *this );
 }
 
 Geometry::~Geometry()
 {
-  mConnectionObservers.Destroy( *this );
 }
 
-void Geometry::AddVertexBuffer( PropertyBuffer* vertexBuffer )
+void Geometry::Initialize( Integration::Graphics::Graphics& graphics )
 {
-  mVertexBuffers.PushBack( vertexBuffer );
-  CalculateExtents( vertexBuffer );
-  vertexBuffer->AddUniformMapObserver(*this);
-  mConnectionObservers.ConnectionsChanged(*this);
-
-  if( mRenderGeometry )
-  {
-    mSceneController->GetRenderMessageDispatcher().AddPropertyBuffer( *mRenderGeometry, vertexBuffer, GpuBuffer::ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
-  }
+  mGraphics = &graphics;
 }
 
-void Geometry::RemoveVertexBuffer( PropertyBuffer* vertexBuffer )
+void Geometry::AddPropertyBuffer( SceneGraph::PropertyBuffer* propertyBuffer )
 {
-  DALI_ASSERT_DEBUG( NULL != vertexBuffer );
-
-  // Find the object and destroy it
-  Vector<PropertyBuffer*>::Iterator match = std::find(
-    mVertexBuffers.Begin(),
-    mVertexBuffers.End(),
-    vertexBuffer );
-
-  DALI_ASSERT_DEBUG( mVertexBuffers.End() != match );
-  if( mVertexBuffers.End() != match )
-  {
-    vertexBuffer->RemoveUniformMapObserver(*this);
-    mVertexBuffers.Erase( match );
-    mConnectionObservers.ConnectionsChanged(*this);
-
-    if( mRenderGeometry )
-    {
-      mSceneController->GetRenderMessageDispatcher().RemovePropertyBuffer( *mRenderGeometry, vertexBuffer );
-    }
-  }
+  mVertexBuffers.PushBack( propertyBuffer );
+  mAttributesChanged = true;
 }
 
-void Geometry::SetIndexBuffer( PropertyBuffer* indexBuffer )
+void Geometry::SetIndexBuffer( Dali::Vector<unsigned short>& indices )
 {
-  if( mIndexBuffer != indexBuffer )
-  {
-    mIndexBuffer = indexBuffer;
-    indexBuffer->AddUniformMapObserver(*this);
-    mConnectionObservers.ConnectionsChanged(*this);
-
-    if( mRenderGeometry )
-    {
-      mSceneController->GetRenderMessageDispatcher().AddPropertyBuffer( *mRenderGeometry, indexBuffer, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
-    }
-  }
+  mIndices.Swap( indices );
+  mIndicesChanged = true;
 }
 
-void Geometry::ClearIndexBuffer()
+void Geometry::RemovePropertyBuffer( const SceneGraph::PropertyBuffer* propertyBuffer )
 {
-  if( mIndexBuffer )
+  size_t bufferCount = mVertexBuffers.Size();
+  for( size_t i(0); i<bufferCount; ++i )
   {
-    mIndexBuffer->RemoveUniformMapObserver(*this);
-
-    if( mRenderGeometry )
+    if( propertyBuffer == mVertexBuffers[i] )
     {
-      mSceneController->GetRenderMessageDispatcher().RemovePropertyBuffer( *mRenderGeometry, mIndexBuffer );
+      //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
+      mVertexBuffers.Remove( mVertexBuffers.Begin()+i );
+      mAttributesChanged = true;
+      break;
     }
   }
-  mIndexBuffer = 0;
-  mConnectionObservers.ConnectionsChanged(*this);
-}
-
-void Geometry::SetGeometryType( BufferIndex bufferIndex, Geometry::GeometryType geometryType )
-{
-  mGeometryType.Set( bufferIndex, geometryType);
-}
-
-Vector<PropertyBuffer*>& Geometry::GetVertexBuffers()
-{
-  return mVertexBuffers;
 }
 
-PropertyBuffer* Geometry::GetIndexBuffer()
-{
-  return mIndexBuffer;
-}
-
-Geometry::GeometryType Geometry::GetGeometryType( BufferIndex bufferIndex) const
-{
-  int geometryType = mGeometryType[ bufferIndex ];
-  return static_cast< GeometryDataProvider::GeometryType > ( geometryType );
-}
-
-bool Geometry::GetRequiresDepthTesting( BufferIndex bufferIndex ) const
-{
-  return mRequiresDepthTest.GetBoolean( bufferIndex );
-}
-
-void Geometry::ResetDefaultProperties( BufferIndex updateBufferIndex )
-{
-  // Reset the animated properties
-  mCenter.ResetToBaseValue( updateBufferIndex );
-
-  // Age the double buffered properties
-  mGeometryType.CopyPrevious(updateBufferIndex);
-  mRequiresDepthTest.CopyPrevious(updateBufferIndex);
-}
-
-void Geometry::CalculateExtents( PropertyBuffer* vertexBuffer )
-{
-  unsigned int elementIndex = 0;
-  unsigned int elementCount = vertexBuffer->GetElementCount( 0 );
-  unsigned int elementCount1 = vertexBuffer->GetElementCount( 1 );
-
-  // Select the double buffered element list that is the largest...
-  if ( elementCount < elementCount1 )
-  {
-    elementCount = elementCount1;
-    elementIndex = 1;
-  }
-
-  unsigned int attributeCount = vertexBuffer->GetAttributeCount( elementIndex );
-  unsigned int elementSize = vertexBuffer->GetElementSize( elementIndex );
-
-  std::string posName( "aPos" );
-  std::size_t found;
-
-  float left = 0.0f;
-  float right = 0.0f;
-  float top = 0.0f;
-  float bottom = 0.0f;
-
-  // Find the position attribute index
-  for ( unsigned int i = 0; i < attributeCount; ++i )
-  {
-    found = vertexBuffer->GetAttributeName( 0, i ).find( posName );
-    if ( found != std::string::npos )
-    {
-      unsigned int offset = vertexBuffer->GetAttributeOffset( elementIndex, i );
-      const PropertyBufferDataProvider::BufferType& data = vertexBuffer->GetData( elementIndex );
-
-      // Check attribute type to determine correct position type
-      Property::Type positionType = vertexBuffer->GetAttributeType( elementIndex, i );
-      Vector3 halfExtents;
-      Vector3 center;
-      switch ( positionType )
-      {
-        case Property::VECTOR2:
-        {
-          for ( unsigned int j = 0; j < elementCount; ++j )
-          {
-            const Vector2* position = reinterpret_cast< const Vector2* >( &data[ offset ] );
-            offset += elementSize;
-
-            if ( position->x < left )
-            {
-              left = position->x;
-            }
-            if ( position->x > right )
-            {
-              right = position->x;
-            }
-            if ( position->y < top )
-            {
-              top = position->y;
-            }
-            if ( position->y > bottom )
-            {
-              bottom = position->y;
-            }
-          }
-
-          halfExtents = Vector3( ( right - left ) * 0.5f, ( bottom - top ) * 0.5f, 0.0f );
-          center = Vector3( halfExtents.x + left , halfExtents.y + top, 0.0f );
-          break;
-        }
-        case Property::VECTOR3:
-        {
-          float near = 0.0f;
-          float far = 0.0f;
-          for ( unsigned int j = 0; j < elementCount; ++j )
-          {
-            const Vector3* position = reinterpret_cast< const Vector3* >( &data[ offset ] );
-            offset += elementSize;
-
-            if ( position->x < left )
-            {
-              left = position->x;
-            }
-            if ( position->x > right )
-            {
-              right = position->x;
-            }
-            if ( position->y < top )
-            {
-              top = position->y;
-            }
-            if ( position->y > bottom )
-            {
-              bottom = position->y;
-            }
-            if ( position->z > far )
-            {
-              far = position->z;
-            }
-            if ( position->z < near )
-            {
-              near = position->z;
-            }
-          }
-          halfExtents = Vector3( ( right - left ) * 0.5f, ( bottom - top ) * 0.5f, ( far - near ) * 0.5f );
-          center = Vector3( halfExtents.x + left , halfExtents.y + top, halfExtents.z + near );
-          break;
-        }
-        default:
-        {
-          break;
-        }
-      }
-      mCenter.Bake( 0, center );
-      mCenter.Bake( 1, center );
-    }
-  }
-}
-
-void Geometry::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
-{
-}
-
-void Geometry::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
-{
-}
-
-void Geometry::AddConnectionObserver( ConnectionChangePropagator::Observer& observer )
-{
-  mConnectionObservers.Add(observer);
-}
-
-void Geometry::RemoveConnectionObserver( ConnectionChangePropagator::Observer& observer )
-{
-  mConnectionObservers.Remove(observer);
-}
-
-void Geometry::UniformMappingsChanged( const UniformMap& mappings )
-{
-  // Our uniform map, or that of one of the watched children has changed.
-  // Inform connected observers.
-  mConnectionObservers.ConnectedUniformMapChanged();
-}
-
-RenderGeometry* Geometry::GetRenderGeometry(SceneController* sceneController)
-{
-  if(!mRenderGeometry)
-  {
-    //Create RenderGeometry
-    mSceneController = sceneController;
-    mRenderGeometry = new RenderGeometry( *this );
-
-    size_t vertexBufferCount( mVertexBuffers.Size() );
-    for( size_t i(0); i<vertexBufferCount; ++i )
-    {
-      mRenderGeometry->AddPropertyBuffer( mVertexBuffers[i], GpuBuffer::ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
-    }
-
-    if( mIndexBuffer )
-    {
-      mRenderGeometry->AddPropertyBuffer( mIndexBuffer, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
-    }
-
-    //Transfer ownership to RenderManager
-    sceneController->GetRenderMessageDispatcher().AddGeometry( *mRenderGeometry );
-  }
-
-  ++mRendererRefCount;
-  return mRenderGeometry;
-}
-
-void Geometry::OnRendererDisconnect()
-{
-  --mRendererRefCount;
-  if( mRendererRefCount == 0 )
-  {
-    //Delete the corresponding RenderGeometry via message to RenderManager
-    mSceneController->GetRenderMessageDispatcher().RemoveGeometry( *mRenderGeometry );
-
-    mRenderGeometry = 0;
-    mSceneController = 0;
-  }
-}
-
-
 } // namespace SceneGraph
 } // namespace Internal
 } // namespace Dali