2 * Copyright (c) 2018 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 uint32_t GetPropertyImplementationAlignment( Property::Type& propertyType )
55 uint32_t 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:
113 case Property::EXTENTS:
115 // already handled by higher level code
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, uint32_t size )
136 mSize = size; // size is the number of elements
138 uint32_t bufferSize = mBufferFormatSize * mSize;
140 // create a new DALi vector to store the buffer data
141 // the heap allocated vector will end up being owned by Render::PropertyBuffer
142 OwnerPointer< Vector<uint8_t> > bufferCopy = new Dali::Vector<uint8_t>();
143 bufferCopy->Resize( bufferSize );
146 const uint8_t* source = static_cast<const uint8_t*>( data );
147 uint8_t* destination = &((*bufferCopy)[0]);
148 std::copy( source, source + bufferSize, destination );
150 // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
151 SceneGraph::SetPropertyBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, mSize );
154 uint32_t PropertyBuffer::GetSize() const
159 const Render::PropertyBuffer* PropertyBuffer::GetRenderObject() const
161 return mRenderObject;
164 PropertyBuffer::~PropertyBuffer()
166 if( EventThreadServices::IsCoreRunning() && mRenderObject)
168 SceneGraph::RemovePropertyBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
172 PropertyBuffer::PropertyBuffer()
173 : mEventThreadServices( *Stage::GetCurrent() ),
174 mRenderObject( NULL ),
175 mBufferFormatSize( 0 ),
180 void PropertyBuffer::Initialize( Dali::Property::Map& formatMap )
182 mRenderObject = new Render::PropertyBuffer();
183 OwnerPointer< Render::PropertyBuffer > transferOwnership( mRenderObject );
184 SceneGraph::AddPropertyBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
186 uint32_t numComponents = static_cast<uint32_t>( formatMap.Count() );
189 OwnerPointer< Render::PropertyBuffer::Format> format = new Render::PropertyBuffer::Format();
190 format->components.resize( numComponents );
192 uint32_t currentAlignment = 0u;
193 uint32_t maxAlignmentRequired = 0u;
195 for( uint32_t i = 0u; i < numComponents; ++i )
197 KeyValuePair component = formatMap.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 uint32_t elementSize = GetPropertyImplementationSize( type );
218 uint32_t elementAlignment = GetPropertyImplementationAlignment( type );
220 // check if current alignment is compatible with new member
221 if( uint32_t 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( uint32_t offset = currentAlignment % maxAlignmentRequired )
248 // Not compatible, realign
249 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
253 // Set the format size
254 format->size = currentAlignment;
256 mBufferFormatSize = format->size;
258 SceneGraph::SetPropertyBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
261 uint32_t GetPropertyImplementationSize( Property::Type& propertyType )
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:
321 case Property::EXTENTS:
323 // already handled by higher level code
331 } // namespace Internal