[Tizen] Revert "Support multiple window rendering"
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-buffer-impl.cpp
index 4279169..ecd9a69 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 <dali/internal/event/common/property-buffer-impl.h>  // Dali::Internal::PropertyBuffer
+#include <dali/internal/event/common/property-buffer-impl.h>
 
 // INTERNAL INCLUDES
-#include <dali/public-api/object/property-buffer.h>     // Dali::Internal::PropertyBuffer
-#include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
-#include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
-#include <dali/internal/update/common/scene-graph-property-buffer.h>
+#include <dali/public-api/rendering/property-buffer.h>
+#include <dali/internal/event/common/stage-impl.h>
 #include <dali/internal/update/manager/update-manager.h>
-// @todo MESH_REWORK remove this comment - Don't include "stage.h" - use GetEventThreadServices() instead.
 
 namespace Dali
 {
@@ -35,222 +32,301 @@ namespace
 {
 
 /**
- *            |name    |type             |writable|animatable|constraint-input|enum for index-checking|
+ * Calculate the alignment requirements of a type
+ *
+ * This is used to calculate the memory alignment requirements of a type
+ * It creates a structure with a dummy char and a member of the type we want to check
+ * this will cause the second member to be aligned by it's alignment requirement.
  */
-DALI_PROPERTY_TABLE_BEGIN
-DALI_PROPERTY( "size",          UNSIGNED_INTEGER, true, false,  true,   Dali::PropertyBuffer::Property::SIZE )
-DALI_PROPERTY( "buffer-format", MAP,              false, false, false,  Dali::PropertyBuffer::Property::BUFFER_FORMAT )
-DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
-
-const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> PROPERTY_BUFFER_IMPL = { DEFAULT_PROPERTY_DETAILS };
-
-} // unnamed namespace
-
-PropertyBufferPtr PropertyBuffer::New()
+template<Property::Type type>
+struct PropertyImplementationTypeAlignment
 {
-  return PropertyBufferPtr( new PropertyBuffer() );
-}
+  // Create a structure that forces alignment of the data type
+  struct TestStructure
+  {
+    char oneChar;  ///< Member with sizeof() == 1
+    typename PropertyImplementationType<type>::Type data;
+  };
+  enum { VALUE = offsetof( TestStructure, data ) };
+};
 
-void PropertyBuffer::SetSize( std::size_t size )
+uint32_t GetPropertyImplementationAlignment( Property::Type& propertyType )
 {
-  //TODO: MESH_REWORK
-  DALI_ASSERT_ALWAYS( false && "MESH_REWORK" );
-}
+  uint32_t alignment = 0u;
 
-std::size_t PropertyBuffer::GetSize() const
-{
-  //TODO: MESH_REWORK
-  DALI_ASSERT_ALWAYS( false && "MESH_REWORK" );
+  switch( propertyType )
+  {
+    case Property::BOOLEAN:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
+      break;
+    }
+    case Property::INTEGER:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
+      break;
+    }
+    case Property::FLOAT:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
+      break;
+    }
+    case Property::VECTOR2:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
+      break;
+    }
+    case Property::VECTOR3:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
+      break;
+    }
+    case Property::VECTOR4:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
+      break;
+    }
+    case Property::MATRIX3:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
+      break;
+    }
+    case Property::MATRIX:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
+      break;
+    }
+    case Property::RECTANGLE:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
+      break;
+    }
+    case Property::ROTATION:
+    {
+      alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
+      break;
+    }
+    case Property::NONE:
+    case Property::STRING:
+    case Property::ARRAY:
+    case Property::MAP:
+    case Property::EXTENTS:
+    {
+      // already handled by higher level code
+    }
+  }
 
-  return 0;
+  return alignment;
 }
 
-void PropertyBuffer::SetData( void* data )
-{
-  //TODO: MESH_REWORK
-  DALI_ASSERT_ALWAYS( false && "MESH_REWORK" );
-}
+} // unnamed namespace
 
-Dali::Property::Index PropertyBuffer::GetPropertyIndex( const std::string name, std::size_t index )
+PropertyBufferPtr PropertyBuffer::New( Dali::Property::Map& format )
 {
-  //TODO: MESH_REWORK
-  DALI_ASSERT_ALWAYS( false && "MESH_REWORK" );
-  return 0;
-}
+  DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
 
-const SceneGraph::PropertyBuffer* PropertyBuffer::GetPropertyBufferSceneObject() const
-{
-  return mSceneObject;
-}
+  PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
+  propertyBuffer->Initialize( format );
 
-unsigned int PropertyBuffer::GetDefaultPropertyCount() const
-{
-  return PROPERTY_BUFFER_IMPL.GetDefaultPropertyCount();
+  return propertyBuffer;
 }
 
-void PropertyBuffer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
+void PropertyBuffer::SetData( const void* data, uint32_t size )
 {
-  PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndices( indices );
-}
+  mSize = size; // size is the number of elements
 
-const char* PropertyBuffer::GetDefaultPropertyName(Property::Index index) const
-{
-  return PROPERTY_BUFFER_IMPL.GetDefaultPropertyName( index );
-}
+  uint32_t bufferSize = mBufferFormatSize * mSize;
 
-Property::Index PropertyBuffer::GetDefaultPropertyIndex( const std::string& name ) const
-{
-  return PROPERTY_BUFFER_IMPL.GetDefaultPropertyIndex( name );
-}
+  // create a new DALi vector to store the buffer data
+  // the heap allocated vector will end up being owned by Render::PropertyBuffer
+  OwnerPointer< Vector<uint8_t> > bufferCopy = new Dali::Vector<uint8_t>();
+  bufferCopy->Resize( bufferSize );
 
-bool PropertyBuffer::IsDefaultPropertyWritable( Property::Index index ) const
-{
-  return PROPERTY_BUFFER_IMPL.IsDefaultPropertyWritable( index );
-}
+  // copy the data
+  const uint8_t* source = static_cast<const uint8_t*>( data );
+  uint8_t* destination = &((*bufferCopy)[0]);
+  std::copy( source, source + bufferSize, destination );
 
-bool PropertyBuffer::IsDefaultPropertyAnimatable( Property::Index index ) const
-{
-  return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAnimatable( index );
+  // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
+  SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, mSize );
 }
 
-bool PropertyBuffer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
+uint32_t PropertyBuffer::GetSize() const
 {
-  return PROPERTY_BUFFER_IMPL.IsDefaultPropertyAConstraintInput( index );
+  return mSize;
 }
 
-Property::Type PropertyBuffer::GetDefaultPropertyType( Property::Index index ) const
+const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
 {
-  return PROPERTY_BUFFER_IMPL.GetDefaultPropertyType( index );
+  return mRenderObject;
 }
 
-void PropertyBuffer::SetDefaultProperty( Property::Index index,
-                                         const Property::Value& propertyValue )
+PropertyBuffer::~PropertyBuffer()
 {
-  switch( index )
+  if( EventThreadServices::IsCoreRunning() && mRenderObject)
   {
-    case Dali::PropertyBuffer::Property::SIZE:
-    {
-      SetSize( propertyValue.Get<int>() );
-      break;
-    }
-    case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
-    {
-      DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
-      break;
-    }
+    SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
   }
 }
 
-void PropertyBuffer::SetSceneGraphProperty( Property::Index index,
-                                            const PropertyMetadata& entry,
-                                            const Property::Value& value )
+PropertyBuffer::PropertyBuffer()
+: mEventThreadServices( *Stage::GetCurrent() ),
+  mRenderObject( NULL ),
+  mBufferFormatSize( 0 ),
+  mSize( 0 )
 {
-  PROPERTY_BUFFER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
 }
 
-Property::Value PropertyBuffer::GetDefaultProperty( Property::Index index ) const
+void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
 {
-  Property::Value value;
+  mRenderObject = new Render::PropertyBuffer();
+  OwnerPointer< Render::PropertyBuffer > transferOwnership( mRenderObject );
+  SceneGraph::AddPropertyBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
+
+  uint32_t numComponents = static_cast<uint32_t>( formatMap.Count() );
+
+  // Create the format
+  OwnerPointer< Render::PropertyBuffer::Format> format = new Render::PropertyBuffer::Format();
+  format->components.resize( numComponents );
 
-  switch( index )
+  uint32_t currentAlignment = 0u;
+  uint32_t maxAlignmentRequired = 0u;
+
+  for( uint32_t i = 0u; i < numComponents; ++i )
   {
-    case Dali::PropertyBuffer::Property::SIZE:
+    KeyValuePair component = formatMap.GetKeyValue( i );
+
+    // Get the name
+    if(component.first.type == Property::Key::INDEX)
     {
-      value = static_cast<int>( GetSize() ); // @todo MESH_REWORK Add a size_t type to PropertyValue
-      break;
+      continue;
     }
-    case Dali::PropertyBuffer::Property::BUFFER_FORMAT:
+    format->components[i].name = component.first.stringKey;
+
+    // enums are stored in the map as int
+    Property::Type type = Property::Type( component.second.Get<int>() );
+
+    // Get the size and alignment
+    if( ( type == Property::NONE   ) ||
+        ( type == Property::STRING ) ||
+        ( type == Property::ARRAY  ) ||
+        ( type == Property::MAP    ) )
     {
-      DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
-      break;
+      DALI_ABORT( "Property::Type not supported in PropertyBuffer" );
     }
-  }
-  return value;
-}
+    uint32_t elementSize = GetPropertyImplementationSize( type );
+    uint32_t elementAlignment = GetPropertyImplementationAlignment( type );
 
-const SceneGraph::PropertyOwner* PropertyBuffer::GetPropertyOwner() const
-{
-  return mSceneObject;
-}
-
-const SceneGraph::PropertyOwner* PropertyBuffer::GetSceneObject() const
-{
-  return mSceneObject;
-}
+    // check if current alignment is compatible with new member
+    if( uint32_t offset = currentAlignment % elementAlignment )
+    {
+      // Not compatible, realign
+      currentAlignment = currentAlignment + elementSize - offset;
+    }
 
-const SceneGraph::PropertyBase* PropertyBuffer::GetSceneObjectAnimatableProperty( Property::Index index ) const
-{
-  DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
-  const SceneGraph::PropertyBase* property = NULL;
+    // write to the format
+    format->components[i].size = elementSize;
+    format->components[i].offset = currentAlignment;
+    format->components[i].type = type;
 
-  if( OnStage() )
-  {
-    property = PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty(
-      this,
-      &PropertyBuffer::FindAnimatableProperty,
-      &PropertyBuffer::FindCustomProperty,
-      index );
+    // update offset
+    currentAlignment += elementSize;
 
-    if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
+    // update max alignment requirement
+    if( elementAlignment > maxAlignmentRequired )
     {
-      DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
+      maxAlignmentRequired = elementAlignment;
     }
-  }
-
-  return property;
-}
 
-const PropertyInputImpl* PropertyBuffer::GetSceneObjectInputProperty( Property::Index index ) const
-{
-  const PropertyInputImpl* property = NULL;
+  }
 
-  if( OnStage() )
+  // Check the alignment for the maxAlignment required to calculate the size of the format
+  if( maxAlignmentRequired != 0 )
   {
-    const SceneGraph::PropertyBase* baseProperty =
-      PROPERTY_BUFFER_IMPL.GetRegisteredSceneGraphProperty( this,
-                                                            &PropertyBuffer::FindAnimatableProperty,
-                                                            &PropertyBuffer::FindCustomProperty,
-                                                            index );
-    property = static_cast<const PropertyInputImpl*>( baseProperty );
-
-    if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
+    if( uint32_t offset = currentAlignment % maxAlignmentRequired )
     {
-      if( index == Dali::PropertyBuffer::Property::SIZE )
-      {
-        // @todo MESH_REWORK
-        DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
-      }
+      // Not compatible, realign
+      currentAlignment = currentAlignment + maxAlignmentRequired - offset;
     }
   }
 
-  return property;
-}
+  // Set the format size
+  format->size = currentAlignment;
 
-int PropertyBuffer::GetPropertyComponentIndex( Property::Index index ) const
-{
-  return PROPERTY_BUFFER_IMPL.GetPropertyComponentIndex( index );
-}
+  mBufferFormatSize = format->size;
 
-bool PropertyBuffer::OnStage() const
-{
-  return mOnStage;
+  SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
 }
 
-void PropertyBuffer::Connect()
+uint32_t GetPropertyImplementationSize( Property::Type& propertyType )
 {
-  mOnStage = true;
-}
+  uint32_t size = 0u;
 
-void PropertyBuffer::Disconnect()
-{
-  mOnStage = false;
-}
+  switch( propertyType )
+  {
+    case Property::BOOLEAN:
+    {
+      size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
+      break;
+    }
+    case Property::INTEGER:
+    {
+      size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
+      break;
+    }
+    case Property::FLOAT:
+    {
+      size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
+      break;
+    }
+    case Property::VECTOR2:
+    {
+      size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
+      break;
+    }
+    case Property::VECTOR3:
+    {
+      size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
+      break;
+    }
+    case Property::VECTOR4:
+    {
+      size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
+      break;
+    }
+    case Property::MATRIX3:
+    {
+      size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
+      break;
+    }
+    case Property::MATRIX:
+    {
+      size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
+      break;
+    }
+    case Property::RECTANGLE:
+    {
+      size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
+      break;
+    }
+    case Property::ROTATION:
+    {
+      size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
+      break;
+    }
+    case Property::NONE:
+    case Property::STRING:
+    case Property::ARRAY:
+    case Property::MAP:
+    case Property::EXTENTS:
+    {
+      // already handled by higher level code
+    }
+  }
 
-PropertyBuffer::PropertyBuffer()
-: mSceneObject(NULL),
-  mOnStage( false )
-{
+  return size;
 }
 
+
 } // namespace Internal
 } // namespace Dali