[dali_1.2.62] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-buffer-impl.cpp
index 30f1ed5..0718c4b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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
-
-// EXTERNAL INCLUDE
-#include <algorithm> // std::sort
+#include <dali/internal/event/common/property-buffer-impl.h>
 
 // INTERNAL INCLUDES
-#include <dali/devel-api/object/property-buffer.h> // Dali::Internal::PropertyBuffer
+#include <dali/public-api/rendering/property-buffer.h>
 #include <dali/internal/event/common/stage-impl.h>
 #include <dali/internal/update/manager/update-manager.h>
 
@@ -59,14 +56,6 @@ unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
 
   switch( propertyType )
   {
-    case Property::NONE:
-    case Property::STRING:
-    case Property::ARRAY:
-    case Property::MAP:
-    {
-      DALI_ASSERT_ALWAYS( false && "No size for properties with no type, or dynamic sizes" );
-      break;
-    }
     case Property::BOOLEAN:
     {
       alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
@@ -117,6 +106,14 @@ unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
       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 alignment;
@@ -124,49 +121,39 @@ unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
 
 } // unnamed namespace
 
-PropertyBufferPtr PropertyBuffer::New()
+PropertyBufferPtr PropertyBuffer::New( Dali::Property::Map& format )
 {
+  DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
+
   PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
-  propertyBuffer->Initialize();
+  propertyBuffer->Initialize( format );
 
   return propertyBuffer;
 }
 
-void PropertyBuffer::SetSize( std::size_t size )
-{
-  mSize = size;
-
-  SizeChanged();
-
-  SceneGraph::SetPropertyBufferSize(mEventThreadServices.GetUpdateManager(),*mRenderObject, mSize );
-}
-
-std::size_t PropertyBuffer::GetSize() const
+void PropertyBuffer::SetData( const void* data, std::size_t size )
 {
-  return mSize;
-}
+  mSize = size; // size is the number of elements
 
-void PropertyBuffer::SetData( const void* data )
-{
-  DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
+  unsigned int bufferSize = mBufferFormatSize * mSize;
 
-  DALI_ASSERT_ALWAYS( mSize && "Size of the buffer must be set before setting the data." );
+  // create a new DALi vector to store the buffer data
+  // the heap allocated vector will end up being owned by Render::PropertyBuffer
+  OwnerPointer< Vector<char> > bufferCopy = new Dali::Vector<char>();
+  bufferCopy->Resize( bufferSize );
 
+  // copy the data
   const char* source = static_cast<const char*>( data );
-  std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
+  char *destination = &((*bufferCopy)[0]);
+  std::copy( source, source + bufferSize, destination );
 
-  SceneGraph::SetPropertyBufferData(mEventThreadServices.GetUpdateManager(),*mRenderObject,new Dali::Vector<char>( mBuffer ));
+  // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
+  SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, mSize );
 }
 
-void PropertyBuffer::SetFormat( Dali::Property::Map& format )
+std::size_t PropertyBuffer::GetSize() const
 {
-  DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
-
-  DALI_ASSERT_DEBUG( 0 == mFormat.Count() && "Format of property buffer can only be set once." );
-
-  mFormat = format;
-
-  FormatChanged();
+  return mSize;
 }
 
 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
@@ -183,26 +170,23 @@ PropertyBuffer::~PropertyBuffer()
 }
 
 PropertyBuffer::PropertyBuffer()
-:mEventThreadServices( *Stage::GetCurrent() )
-,mRenderObject(NULL)
-,mBufferFormat( NULL )
-,mSize( 0 )
+: mEventThreadServices( *Stage::GetCurrent() ),
+  mRenderObject( NULL ),
+  mBufferFormatSize( 0 ),
+  mSize( 0 )
 {
 }
 
-void PropertyBuffer::Initialize()
+void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
 {
   mRenderObject = new Render::PropertyBuffer();
-  SceneGraph::AddPropertyBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject );
-}
+  OwnerPointer< Render::PropertyBuffer > transferOwnership( mRenderObject );
+  SceneGraph::AddPropertyBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
 
-void PropertyBuffer::FormatChanged()
-{
-  size_t numComponents = mFormat.Count();
+  size_t numComponents = formatMap.Count();
 
   // Create the format
-  DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
-  Render::PropertyBuffer::Format* format = new Render::PropertyBuffer::Format();
+  OwnerPointer< Render::PropertyBuffer::Format> format = new Render::PropertyBuffer::Format();
   format->components.resize( numComponents );
 
   unsigned int currentAlignment = 0u;
@@ -210,15 +194,26 @@ void PropertyBuffer::FormatChanged()
 
   for( size_t i = 0u; i < numComponents; ++i )
   {
-    StringValuePair component = mFormat.GetPair( i );
+    KeyValuePair component = formatMap.GetKeyValue( i );
 
     // Get the name
-    format->components[i].name = component.first;
+    if(component.first.type == Property::Key::INDEX)
+    {
+      continue;
+    }
+    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_ABORT( "Property::Type not supported in PropertyBuffer" );
+    }
     unsigned int elementSize = GetPropertyImplementationSize( type );
     unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
 
@@ -258,23 +253,9 @@ void PropertyBuffer::FormatChanged()
   // Set the format size
   format->size = currentAlignment;
 
-  mBufferFormat = format;
+  mBufferFormatSize = format->size;
 
   SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
-  if( mSize )
-  {
-    SizeChanged();
-  }
-}
-
-void PropertyBuffer::SizeChanged()
-{
-  // Check if format and size have been set yet
-  if( mBufferFormat != NULL )
-  {
-    unsigned int bufferSize = mBufferFormat->size * mSize;
-    mBuffer.Resize( bufferSize );
-  }
 }
 
 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
@@ -283,14 +264,6 @@ unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
 
   switch( propertyType )
   {
-    case Property::NONE:
-    case Property::STRING:
-    case Property::ARRAY:
-    case Property::MAP:
-    {
-      DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
-      break;
-    }
     case Property::BOOLEAN:
     {
       size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
@@ -341,6 +314,14 @@ unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
       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
+    }
   }
 
   return size;