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 )
59 case Property::BOOLEAN:
61 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
64 case Property::INTEGER:
66 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
71 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
74 case Property::VECTOR2:
76 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
79 case Property::VECTOR3:
81 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
84 case Property::VECTOR4:
86 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
89 case Property::MATRIX3:
91 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
94 case Property::MATRIX:
96 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
99 case Property::RECTANGLE:
101 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
104 case Property::ROTATION:
106 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
110 case Property::STRING:
111 case Property::ARRAY:
114 // already handled by higher level code
121 } // unnamed namespace
123 PropertyBufferPtr PropertyBuffer::New( Dali::Property::Map& format )
125 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
127 PropertyBufferPtr propertyBuffer( new PropertyBuffer() );
128 propertyBuffer->Initialize( format );
130 return propertyBuffer;
133 void PropertyBuffer::SetData( const void* data, std::size_t size )
135 DALI_ASSERT_DEBUG( mFormat.Count() && "Format must be set before setting the data." );
139 // Check if format and size have been set yet
140 if( mBufferFormat != NULL )
142 unsigned int bufferSize = mBufferFormat->size * mSize;
143 mBuffer.Resize( bufferSize );
146 const char* source = static_cast<const char*>( data );
147 std::copy( source, source + mBuffer.Size(), &mBuffer[0] );
149 SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, new Dali::Vector<char>( mBuffer ), mSize );
152 std::size_t PropertyBuffer::GetSize() const
157 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
159 return mRenderObject;
162 PropertyBuffer::~PropertyBuffer()
164 if( EventThreadServices::IsCoreRunning() && mRenderObject)
166 SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
170 PropertyBuffer::PropertyBuffer()
171 : mEventThreadServices( *Stage::GetCurrent() ),
172 mRenderObject( NULL ),
173 mBufferFormat( NULL ),
178 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
180 mRenderObject = new Render::PropertyBuffer();
181 SceneGraph::AddPropertyBuffer(mEventThreadServices.GetUpdateManager(), *mRenderObject );
185 size_t numComponents = mFormat.Count();
188 DALI_ASSERT_DEBUG( mBufferFormat == NULL && "PropertyFormat should not be set yet" );
189 Render::PropertyBuffer::Format* format = new Render::PropertyBuffer::Format();
190 format->components.resize( numComponents );
192 unsigned int currentAlignment = 0u;
193 unsigned int maxAlignmentRequired = 0u;
195 for( size_t i = 0u; i < numComponents; ++i )
197 KeyValuePair component = mFormat.GetKeyValue( i );
200 if(component.first.type == Property::Key::INDEX)
204 format->components[i].name = component.first.stringKey;
206 // enums are stored in the map as int
207 Property::Type type = Property::Type( component.second.Get<int>() );
209 // Get the size and alignment
210 if( ( type == Property::NONE ) ||
211 ( type == Property::STRING ) ||
212 ( type == Property::ARRAY ) ||
213 ( type == Property::MAP ) )
215 DALI_ABORT( "Property::Type not supported in PropertyBuffer" );
217 unsigned int elementSize = GetPropertyImplementationSize( type );
218 unsigned int elementAlignment = GetPropertyImplementationAlignment( type );
220 // check if current alignment is compatible with new member
221 if( unsigned int offset = currentAlignment % elementAlignment )
223 // Not compatible, realign
224 currentAlignment = currentAlignment + elementSize - offset;
227 // write to the format
228 format->components[i].size = elementSize;
229 format->components[i].offset = currentAlignment;
230 format->components[i].type = type;
233 currentAlignment += elementSize;
235 // update max alignment requirement
236 if( elementAlignment > maxAlignmentRequired )
238 maxAlignmentRequired = elementAlignment;
243 // Check the alignment for the maxAlignment required to calculate the size of the format
244 if( maxAlignmentRequired != 0 )
246 if( unsigned int offset = currentAlignment % maxAlignmentRequired )
248 // Not compatible, realign
249 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
253 // Set the format size
254 format->size = currentAlignment;
256 mBufferFormat = format;
258 SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
261 unsigned int GetPropertyImplementationSize( Property::Type& propertyType )
263 unsigned int size = 0u;
265 switch( propertyType )
267 case Property::BOOLEAN:
269 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
272 case Property::INTEGER:
274 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
277 case Property::FLOAT:
279 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
282 case Property::VECTOR2:
284 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
287 case Property::VECTOR3:
289 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
292 case Property::VECTOR4:
294 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
297 case Property::MATRIX3:
299 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
302 case Property::MATRIX:
304 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
307 case Property::RECTANGLE:
309 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
312 case Property::ROTATION:
314 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
318 case Property::STRING:
319 case Property::ARRAY:
322 // already handled by higher level code
330 } // namespace Internal