2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/event/common/property-buffer-impl.h>
22 #include <dali/public-api/rendering/property-buffer.h>
23 #include <dali/internal/event/common/stage-impl.h>
24 #include <dali/internal/update/manager/update-manager.h>
35 * Calculate the alignment requirements of a type
37 * This is used to calculate the memory alignment requirements of a type
38 * It creates a structure with a dummy char and a member of the type we want to check
39 * this will cause the second member to be aligned by it's alignment requirement.
41 template<Property::Type type>
42 struct PropertyImplementationTypeAlignment
44 // Create a structure that forces alignment of the data type
47 char oneChar; ///< Member with sizeof() == 1
48 typename PropertyImplementationType<type>::Type data;
50 enum { VALUE = offsetof( TestStructure, data ) };
53 unsigned int GetPropertyImplementationAlignment( Property::Type& propertyType )
55 unsigned int alignment = 0u;
57 switch( propertyType )
60 case Property::STRING:
64 DALI_ASSERT_ALWAYS( false && "No size for properties with no type, or dynamic sizes" );
67 case Property::BOOLEAN:
69 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
72 case Property::INTEGER:
74 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
79 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
82 case Property::VECTOR2:
84 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
87 case Property::VECTOR3:
89 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
92 case Property::VECTOR4:
94 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
97 case Property::MATRIX3:
99 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
102 case Property::MATRIX:
104 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
107 case Property::RECTANGLE:
109 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
112 case Property::ROTATION:
114 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
122 } // unnamed namespace
124 PropertyBufferPtr PropertyBuffer::New( Dali::Property::Map& format )
126 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
128 PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
129 propertyBuffer->Initialize( format );
131 return propertyBuffer;
134 void PropertyBuffer::SetData( const void* data, std::size_t size )
136 DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
140 // Check if format and size have been set yet
141 if( mBufferFormat != NULL )
143 unsigned int bufferSize = mBufferFormat->size * mSize;
144 mBuffer.Resize( bufferSize );
147 const char* source = static_cast<const char*>( data );
148 std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
150 SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, new Dali::Vector<char>( mBuffer ), mSize );
153 std::size_t PropertyBuffer::GetSize() const
158 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
160 return mRenderObject;
163 PropertyBuffer::~PropertyBuffer()
165 if( EventThreadServices::IsCoreRunning() && mRenderObject)
167 SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
171 PropertyBuffer::PropertyBuffer()
172 : mEventThreadServices( *Stage::GetCurrent() ),
173 mRenderObject( NULL ),
174 mBufferFormat( NULL ),
179 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
181 mRenderObject = new Render::PropertyBuffer();
182 SceneGraph::AddPropertyBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject );
186 size_t numComponents = mFormat.Count();
189 DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
190 Render::PropertyBuffer::Format* format = new Render::PropertyBuffer::Format();
191 format->components.resize( numComponents );
193 unsigned int currentAlignment = 0u;
194 unsigned int maxAlignmentRequired = 0u;
196 for( size_t i = 0u; i < numComponents; ++i )
198 StringValuePair component = mFormat.GetPair( i );
201 format->components[i].name = component.first;
203 // enums are stored in the map as int
204 Property::Type type = Property::Type( component.second.Get<int>() );
206 // Get the size and alignment
207 unsigned int elementSize = GetPropertyImplementationSize( type );
208 unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
210 // check if current alignment is compatible with new member
211 if( unsigned int offset = currentAlignment % elementAlignment )
213 // Not compatible, realign
214 currentAlignment = currentAlignment + elementSize - offset;
217 // write to the format
218 format->components[i].size = elementSize;
219 format->components[i].offset = currentAlignment;
220 format->components[i].type = type;
223 currentAlignment += elementSize;
225 // update max alignment requirement
226 if( elementAlignment > maxAlignmentRequired )
228 maxAlignmentRequired = elementAlignment;
233 // Check the alignment for the maxAlignment required to calculate the size of the format
234 if( maxAlignmentRequired != 0 )
236 if( unsigned int offset = currentAlignment % maxAlignmentRequired )
238 // Not compatible, realign
239 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
243 // Set the format size
244 format->size = currentAlignment;
246 mBufferFormat = format;
248 SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
251 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
253 unsigned int size = 0u;
255 switch( propertyType )
258 case Property::STRING:
259 case Property::ARRAY:
262 DALI_ASSERT_ALWAYS( "No size for properties with no type, or dynamic sizes" );
265 case Property::BOOLEAN:
267 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
270 case Property::INTEGER:
272 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
275 case Property::FLOAT:
277 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
280 case Property::VECTOR2:
282 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
285 case Property::VECTOR3:
287 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
290 case Property::VECTOR4:
292 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
295 case Property::MATRIX3:
297 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
300 case Property::MATRIX:
302 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
305 case Property::RECTANGLE:
307 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
310 case Property::ROTATION:
312 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
321 } // namespace Internal