2 * Copyright (c) 2020 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/rendering/vertex-buffer-impl.h>
22 #include <dali/public-api/rendering/vertex-buffer.h>
23 #include <dali/internal/update/manager/update-manager.h>
25 #if defined (ANDROID) || defined(WIN32)
28 uint64_t _Hash_bytes(const void* bytes, uint64_t size, uint64_t seed)
30 for (uint64_t i = 0; i < size; i++)
32 seed = seed * 31 + reinterpret_cast<const unsigned char*>(bytes)[i];
49 * Calculate the alignment requirements of a type
51 * This is used to calculate the memory alignment requirements of a type
52 * It creates a structure with a dummy char and a member of the type we want to check
53 * this will cause the second member to be aligned by it's alignment requirement.
55 template<Property::Type type>
56 struct PropertyImplementationTypeAlignment
58 // Create a structure that forces alignment of the data type
61 char oneChar; ///< Member with sizeof() == 1
62 typename PropertyImplementationType<type>::Type data;
64 enum { VALUE = offsetof( TestStructure, data ) };
67 uint32_t GetPropertyImplementationAlignment( Property::Type& propertyType )
69 uint32_t alignment = 0u;
71 switch( propertyType )
73 case Property::BOOLEAN:
75 alignment = PropertyImplementationTypeAlignment< Property::BOOLEAN >::VALUE;
78 case Property::INTEGER:
80 alignment = PropertyImplementationTypeAlignment< Property::INTEGER >::VALUE;
85 alignment = PropertyImplementationTypeAlignment< Property::FLOAT >::VALUE;
88 case Property::VECTOR2:
90 alignment = PropertyImplementationTypeAlignment< Property::VECTOR2 >::VALUE;
93 case Property::VECTOR3:
95 alignment = PropertyImplementationTypeAlignment< Property::VECTOR3 >::VALUE;
98 case Property::VECTOR4:
100 alignment = PropertyImplementationTypeAlignment< Property::VECTOR4 >::VALUE;
103 case Property::MATRIX3:
105 alignment = PropertyImplementationTypeAlignment< Property::MATRIX3 >::VALUE;
108 case Property::MATRIX:
110 alignment = PropertyImplementationTypeAlignment< Property::MATRIX >::VALUE;
113 case Property::RECTANGLE:
115 alignment = PropertyImplementationTypeAlignment< Property::RECTANGLE >::VALUE;
118 case Property::ROTATION:
120 alignment = PropertyImplementationTypeAlignment< Property::ROTATION >::VALUE;
124 case Property::STRING:
125 case Property::ARRAY:
127 case Property::EXTENTS:
129 // already handled by higher level code
136 } // unnamed namespace
138 VertexBufferPtr VertexBuffer::New( Dali::Property::Map& format )
140 DALI_ASSERT_ALWAYS( format.Count() && "Format cannot be empty." );
142 VertexBufferPtr vertexBuffer( new VertexBuffer() );
143 vertexBuffer->Initialize( format );
148 void VertexBuffer::SetData( const void* data, uint32_t size )
150 mSize = size; // size is the number of elements
152 uint32_t bufferSize = mBufferFormatSize * mSize;
154 // create a new DALi vector to store the buffer data
155 // the heap allocated vector will end up being owned by Render::VertexBuffer
156 OwnerPointer< Vector<uint8_t> > bufferCopy = new Dali::Vector<uint8_t>();
157 bufferCopy->Resize( bufferSize );
160 const uint8_t* source = static_cast<const uint8_t*>( data );
161 uint8_t* destination = &((*bufferCopy)[0]);
162 std::copy( source, source + bufferSize, destination );
164 // Ownership of the bufferCopy is passed to the message ( uses an owner pointer )
165 SceneGraph::SetVertexBufferData( mEventThreadServices.GetUpdateManager(), *mRenderObject, bufferCopy, mSize );
168 uint32_t VertexBuffer::GetSize() const
173 const Render::VertexBuffer* VertexBuffer::GetRenderObject() const
175 return mRenderObject;
178 VertexBuffer::~VertexBuffer()
180 if( EventThreadServices::IsCoreRunning() && mRenderObject)
182 SceneGraph::RemoveVertexBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
186 VertexBuffer::VertexBuffer()
187 : mEventThreadServices( EventThreadServices::Get() ),
188 mRenderObject( nullptr ),
189 mBufferFormatSize( 0 ),
194 void VertexBuffer::Initialize( Dali::Property::Map& formatMap )
196 mRenderObject = new Render::VertexBuffer();
197 OwnerPointer< Render::VertexBuffer > transferOwnership( mRenderObject );
198 SceneGraph::AddVertexBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
200 uint32_t numComponents = static_cast<uint32_t>( formatMap.Count() );
203 OwnerPointer< Render::VertexBuffer::Format> format = new Render::VertexBuffer::Format();
204 format->components.resize( numComponents );
206 uint32_t currentAlignment = 0u;
207 uint32_t maxAlignmentRequired = 0u;
209 for( uint32_t i = 0u; i < numComponents; ++i )
211 KeyValuePair component = formatMap.GetKeyValue( i );
214 if(component.first.type == Property::Key::INDEX)
218 format->components[i].name = component.first.stringKey;
220 // enums are stored in the map as int
221 Property::Type type = Property::Type( component.second.Get<int>() );
223 // Get the size and alignment
224 if( ( type == Property::NONE ) ||
225 ( type == Property::STRING ) ||
226 ( type == Property::ARRAY ) ||
227 ( type == Property::MAP ) )
229 DALI_ABORT( "Property::Type not supported in VertexBuffer" );
231 uint32_t elementSize = GetPropertyImplementationSize( type );
232 uint32_t elementAlignment = GetPropertyImplementationAlignment( type );
234 // check if current alignment is compatible with new member
235 if( uint32_t offset = currentAlignment % elementAlignment )
237 // Not compatible, realign
238 currentAlignment = currentAlignment + elementSize - offset;
241 // write to the format
242 format->components[i].size = elementSize;
243 format->components[i].offset = currentAlignment;
244 format->components[i].type = type;
247 currentAlignment += elementSize;
249 // update max alignment requirement
250 if( elementAlignment > maxAlignmentRequired )
252 maxAlignmentRequired = elementAlignment;
257 // Check the alignment for the maxAlignment required to calculate the size of the format
258 if( maxAlignmentRequired != 0 )
260 if( uint32_t offset = currentAlignment % maxAlignmentRequired )
262 // Not compatible, realign
263 currentAlignment = currentAlignment + maxAlignmentRequired - offset;
267 // Set the format size
268 format->size = currentAlignment;
270 mBufferFormatSize = format->size;
272 SceneGraph::SetVertexBufferFormat(mEventThreadServices.GetUpdateManager(), *mRenderObject, format );
275 uint32_t GetPropertyImplementationSize( Property::Type& propertyType )
279 switch( propertyType )
281 case Property::BOOLEAN:
283 size = sizeof( PropertyImplementationType< Property::BOOLEAN >::Type );
286 case Property::INTEGER:
288 size = sizeof( PropertyImplementationType< Property::INTEGER >::Type );
291 case Property::FLOAT:
293 size = sizeof( PropertyImplementationType< Property::FLOAT >::Type );
296 case Property::VECTOR2:
298 size = sizeof( PropertyImplementationType< Property::VECTOR2 >::Type );
301 case Property::VECTOR3:
303 size = sizeof( PropertyImplementationType< Property::VECTOR3 >::Type );
306 case Property::VECTOR4:
308 size = sizeof( PropertyImplementationType< Property::VECTOR4 >::Type );
311 case Property::MATRIX3:
313 size = sizeof( PropertyImplementationType< Property::MATRIX3 >::Type );
316 case Property::MATRIX:
318 size = sizeof( PropertyImplementationType< Property::MATRIX >::Type );
321 case Property::RECTANGLE:
323 size = sizeof( PropertyImplementationType< Property::RECTANGLE >::Type );
326 case Property::ROTATION:
328 size = sizeof( PropertyImplementationType< Property::ROTATION >::Type );
332 case Property::STRING:
333 case Property::ARRAY:
335 case Property::EXTENTS:
337 // already handled by higher level code
345 } // namespace Internal