2 * Copyright (c) 2019 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/update/manager/update-manager.h>
34 * Calculate the alignment requirements of a type
36 * This is used to calculate the memory alignment requirements of a type
37 * It creates a structure with a dummy char and a member of the type we want to check
38 * this will cause the second member to be aligned by it's alignment requirement.
40 template<Property::Type type>
41 struct PropertyImplementationTypeAlignment
43 // Create a structure that forces alignment of the data type
46 char oneChar; ///< Member with sizeof() == 1
47 typename PropertyImplementationType<type>::Type data;
49 enum { VALUE = offsetof( TestStructure, data ) };
52 uint32_t GetPropertyImplementationAlignment( Property::Type& propertyType )
54 uint32_t alignment = 0u;
56 switch( propertyType )
58 case Property::BOOLEAN:
60 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
63 case Property::INTEGER:
65 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
70 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
73 case Property::VECTOR2:
75 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
78 case Property::VECTOR3:
80 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
83 case Property::VECTOR4:
85 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
88 case Property::MATRIX3:
90 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
93 case Property::MATRIX:
95 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
98 case Property::RECTANGLE:
100 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
103 case Property::ROTATION:
105 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
109 case Property::STRING:
110 case Property::ARRAY:
112 case Property::EXTENTS:
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, uint32_t size )
135 mSize = size; // size is the number of elements
137 uint32_t bufferSize = mBufferFormatSize * mSize;
139 // create a new DALi vector to store the buffer data
140 // the heap allocated vector will end up being owned by Render::PropertyBuffer
141 OwnerPointer< Vector<uint8_t> > bufferCopy = new Dali::Vector<uint8_t>();
142 bufferCopy->Resize( bufferSize );
145 const uint8_t* source = static_cast<const uint8_t*>( data );
146 uint8_t* destination = &((*bufferCopy)[0]);
147 std::copy( source, source + bufferSize, destination );
149 // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
150 SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, mSize );
153 uint32_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( EventThreadServices::Get() ),
173 mRenderObject( NULL ),
174 mBufferFormatSize( 0 ),
179 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
181 mRenderObject = new Render::PropertyBuffer();
182 OwnerPointer< Render::PropertyBuffer > transferOwnership( mRenderObject );
183 SceneGraph::AddPropertyBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
185 uint32_t numComponents = static_cast<uint32_t>( formatMap.Count() );
188 OwnerPointer< Render::PropertyBuffer::Format> format = new Render::PropertyBuffer::Format();
189 format->components.resize( numComponents );
191 uint32_t currentAlignment = 0u;
192 uint32_t maxAlignmentRequired = 0u;
194 for( uint32_t i = 0u; i < numComponents; ++i )
196 KeyValuePair component = formatMap.GetKeyValue( i );
199 if(component.first.type == Property::Key::INDEX)
203 format->components[i].name = component.first.stringKey;
205 // enums are stored in the map as int
206 Property::Type type = Property::Type( component.second.Get<int>() );
208 // Get the size and alignment
209 if( ( type == Property::NONE ) ||
210 ( type == Property::STRING ) ||
211 ( type == Property::ARRAY ) ||
212 ( type == Property::MAP ) )
214 DALI_ABORT( "Property::Type not supported in PropertyBuffer" );
216 uint32_t elementSize = GetPropertyImplementationSize( type );
217 uint32_t elementAlignment = GetPropertyImplementationAlignment( type );
219 // check if current alignment is compatible with new member
220 if( uint32_t offset = currentAlignment % elementAlignment )
222 // Not compatible, realign
223 currentAlignment = currentAlignment + elementSize - offset;
226 // write to the format
227 format->components[i].size = elementSize;
228 format->components[i].offset = currentAlignment;
229 format->components[i].type = type;
232 currentAlignment += elementSize;
234 // update max alignment requirement
235 if( elementAlignment > maxAlignmentRequired )
237 maxAlignmentRequired = elementAlignment;
242 // Check the alignment for the maxAlignment required to calculate the size of the format
243 if( maxAlignmentRequired != 0 )
245 if( uint32_t offset = currentAlignment % maxAlignmentRequired )
247 // Not compatible, realign
248 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
252 // Set the format size
253 format->size = currentAlignment;
255 mBufferFormatSize = format->size;
257 SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
260 uint32_t GetPropertyImplementationSize( Property::Type& propertyType )
264 switch( propertyType )
266 case Property::BOOLEAN:
268 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
271 case Property::INTEGER:
273 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
276 case Property::FLOAT:
278 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
281 case Property::VECTOR2:
283 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
286 case Property::VECTOR3:
288 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
291 case Property::VECTOR4:
293 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
296 case Property::MATRIX3:
298 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
301 case Property::MATRIX:
303 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
306 case Property::RECTANGLE:
308 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
311 case Property::ROTATION:
313 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
317 case Property::STRING:
318 case Property::ARRAY:
320 case Property::EXTENTS:
322 // already handled by higher level code
330 } // namespace Internal