2 * Copyright (c) 2015 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.
18 #include <dali-toolkit/internal/atlas-manager/atlas-manager-impl.h>
23 #include <dali/public-api/shader-effects/sampler.h>
24 #include <dali/public-api/shader-effects/shader.h>
25 #include <dali/integration-api/debug.h>
38 const uint32_t DEFAULT_ATLAS_WIDTH( 512u );
39 const uint32_t DEFAULT_ATLAS_HEIGHT( 512u );
40 const uint32_t DEFAULT_BLOCK_WIDTH( 16u );
41 const uint32_t DEFAULT_BLOCK_HEIGHT( 16u );
42 const uint32_t SINGLE_PIXEL_PADDING( 1u );
43 const uint32_t DOUBLE_PIXEL_PADDING( SINGLE_PIXEL_PADDING << 1 );
44 const uint32_t FILLED_PIXEL( -1 );
45 Toolkit::AtlasManager::AtlasSize EMPTY_SIZE;
47 #define MAKE_SHADER(A)#A
49 const char* VERTEX_SHADER = MAKE_SHADER(
50 attribute mediump vec2 aPosition;
51 attribute mediump vec2 aTexCoord;
52 uniform mediump mat4 uMvpMatrix;
53 uniform mediump vec3 uSize;
54 varying mediump vec2 vTexCoord;
58 mediump vec4 position = vec4( aPosition, 0.0, 1.0 );
59 position.xyz *= uSize;
60 gl_Position = uMvpMatrix * position;
61 vTexCoord = aTexCoord;
65 const char* FRAGMENT_SHADER_L8 = MAKE_SHADER(
66 uniform lowp vec4 uColor;
67 uniform sampler2D sTexture;
68 varying mediump vec2 vTexCoord;
72 mediump vec4 color = texture2D( sTexture, vTexCoord );
73 gl_FragColor = vec4( uColor.rgb, uColor.a * color.r );
77 const char* FRAGMENT_SHADER_BGRA = MAKE_SHADER(
78 uniform sampler2D sTexture;
79 varying mediump vec2 vTexCoord;
83 gl_FragColor = texture2D( sTexture, vTexCoord );
89 AtlasManager::AtlasManager()
90 : mAddFailPolicy( Toolkit::AtlasManager::FAIL_ON_ADD_CREATES ),
91 mFilledPixel( FILLED_PIXEL )
93 mNewAtlasSize.mWidth = DEFAULT_ATLAS_WIDTH;
94 mNewAtlasSize.mHeight = DEFAULT_ATLAS_HEIGHT;
95 mNewAtlasSize.mBlockWidth = DEFAULT_BLOCK_WIDTH;
96 mNewAtlasSize.mBlockHeight = DEFAULT_BLOCK_HEIGHT;
99 AtlasManagerPtr AtlasManager::New()
101 AtlasManagerPtr internal = new AtlasManager();
105 AtlasManager::~AtlasManager()
107 for ( uint32_t i = 0; i < mAtlasList.size(); ++i )
109 delete[] mAtlasList[ i ].mStripBuffer;
113 Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( const Toolkit::AtlasManager::AtlasSize& size, Pixel::Format pixelformat )
115 SizeType width = size.mWidth;
116 SizeType height = size.mHeight;
117 SizeType blockWidth = size.mBlockWidth;
118 SizeType blockHeight = size.mBlockHeight;
120 // Check to see if the atlas is large enough to hold a single block even ?
121 if ( blockWidth > width || blockHeight > height )
123 DALI_LOG_ERROR("Atlas %i x %i too small. Dimensions need to be at least %ix%i\n",
124 width, height, blockWidth, blockHeight );
128 Dali::Atlas atlas = Dali::Atlas::New( width, height, pixelformat );
129 AtlasDescriptor atlasDescriptor;
130 atlasDescriptor.mAtlas = atlas;
131 atlasDescriptor.mSize = size;
132 atlasDescriptor.mPixelFormat = pixelformat;
133 atlasDescriptor.mNextFreeBlock = 1u; // indicate next free block will be the first ( +1 )
135 // What size do we need for this atlas' strip buffer ( assume 32bit pixel format )?
136 uint32_t neededStripSize =( blockWidth > blockHeight - DOUBLE_PIXEL_PADDING ? blockWidth : blockHeight - DOUBLE_PIXEL_PADDING ) << 2;
137 atlasDescriptor.mStripBuffer = new PixelBuffer[ neededStripSize ];
138 memset( atlasDescriptor.mStripBuffer, 0, neededStripSize );
140 atlasDescriptor.mHorizontalStrip = BufferImage::New( atlasDescriptor.mStripBuffer,
142 SINGLE_PIXEL_PADDING,
145 atlasDescriptor.mVerticalStrip = BufferImage::New( atlasDescriptor.mStripBuffer,
146 SINGLE_PIXEL_PADDING,
147 blockHeight - DOUBLE_PIXEL_PADDING,
149 atlasDescriptor.mFilledPixelImage = BufferImage::New( reinterpret_cast< PixelBuffer* >( &mFilledPixel ), 1, 1, pixelformat );
150 atlas.Upload( atlasDescriptor.mFilledPixelImage, 0, 0 );
152 Sampler sampler = Sampler::New( atlas, "sTexture" );
153 sampler.SetFilterMode( Sampler::NEAREST, Sampler::NEAREST );
154 sampler.SetProperty( Sampler::Property::AFFECTS_TRANSPARENCY, true );
156 if ( pixelformat == Pixel::BGRA8888 )
158 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_BGRA );
162 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
164 atlasDescriptor.mMaterial = Material::New( shader );
165 atlasDescriptor.mMaterial.AddSampler( sampler );
166 atlasDescriptor.mMaterial.SetBlendMode( BlendingMode::ON );
167 mAtlasList.push_back( atlasDescriptor );
168 return mAtlasList.size();
171 void AtlasManager::SetAddPolicy( Toolkit::AtlasManager::AddFailPolicy policy )
173 mAddFailPolicy = policy;
176 void AtlasManager::Add( const BufferImage& image,
177 Toolkit::AtlasManager::AtlasSlot& slot,
178 Toolkit::AtlasManager::AtlasId atlas )
180 // See if there's a slot in an atlas that matches the requirements of this image
181 // A bitmap must be sliceable into a single atlas
182 Pixel::Format pixelFormat = image.GetPixelFormat();
183 SizeType width = image.GetWidth();
184 SizeType height = image.GetHeight();
185 SizeType blockArea = 0;
186 SizeType totalBlocks = 0;
187 SizeType foundAtlas = 0;
191 AtlasSlotDescriptor desc;
193 // If there is a preferred atlas then check for room in that first
196 foundAtlas = CheckAtlas( atlas, width, height, pixelFormat, blockArea, totalBlocks );
199 // Search current atlases to see if there is a good match
201 while( !foundAtlas && index < mAtlasList.size() )
203 foundAtlas = CheckAtlas( index, width, height, pixelFormat, blockArea, totalBlocks );
207 // If we can't find a suitable atlas then check the policy to determine action
210 if ( Toolkit::AtlasManager::FAIL_ON_ADD_CREATES == mAddFailPolicy )
212 SizeType newAtlas = CreateAtlas( mNewAtlasSize, pixelFormat );
219 foundAtlas = CheckAtlas( newAtlas, width, height, pixelFormat, blockArea, totalBlocks );
223 if ( Toolkit::AtlasManager::FAIL_ON_ADD_FAILS == mAddFailPolicy || !foundAtlas-- )
225 // Haven't found an atlas for this image!!!!!!
230 // Work out where the blocks are we're going to use
231 for ( SizeType i = 0; i < blockArea; ++i )
233 // Is there currently a next free block available ?
234 if ( mAtlasList[ foundAtlas ].mNextFreeBlock )
236 // Yes, so use this for our next block
237 SizeType selectedBlock = mAtlasList[ foundAtlas ].mNextFreeBlock - 1u;
238 desc.mBlocksList.PushBack( selectedBlock );
240 // Any blocks going to be available after this one (adjust to store +1 )?
243 if ( selectedBlock > totalBlocks )
245 // No so start trying to use free blocks list
248 mAtlasList[ foundAtlas ].mNextFreeBlock = selectedBlock;
252 // Our next block must be from the free list, fetch from the start of the list
253 desc.mBlocksList.PushBack( mAtlasList[ foundAtlas ].mFreeBlocksList[ 0 ] );
254 mAtlasList[ foundAtlas ].mFreeBlocksList.Remove( mAtlasList[ foundAtlas ].mFreeBlocksList.Begin() );
258 desc.mImageWidth = width;
259 desc.mImageHeight = height;
260 desc.mAtlasId = foundAtlas + 1u;
263 // See if there's a previously freed image ID that we can assign to this new image
264 uint32_t imageId = 0;
265 for ( uint32_t i = 0; i < mImageList.size(); ++i )
267 if ( !mImageList[ i ].mCount )
275 mImageList.push_back( desc );
276 slot.mImageId = mImageList.size();
280 mImageList[ imageId - 1u ] = desc;
281 slot.mImageId = imageId;
283 slot.mAtlasId = foundAtlas + 1u;
284 UploadImage( image, desc );
287 AtlasManager::SizeType AtlasManager::CheckAtlas( SizeType atlas,
290 Pixel::Format pixelFormat,
292 SizeType& totalBlocks )
294 if ( pixelFormat == mAtlasList[ atlas ].mPixelFormat )
296 // Check to see if there are any unused blocks in this atlas to accomodate our image
297 SizeType blocksInX = mAtlasList[ atlas ].mSize.mWidth / mAtlasList[ atlas ].mSize.mBlockWidth;
298 SizeType blocksInY = mAtlasList[ atlas ].mSize.mHeight / mAtlasList[ atlas ].mSize.mBlockHeight;
299 totalBlocks = blocksInX * blocksInY;
300 SizeType blocksFree = mAtlasList[ atlas ].mNextFreeBlock ?
301 totalBlocks - mAtlasList[ atlas ].mNextFreeBlock + 1u :
302 mAtlasList[ atlas ].mFreeBlocksList.Size();
304 // Check to see if the image will fit in these blocks, if not we'll need to create a new atlas
306 && width + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mBlockWidth
307 && height + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mBlockHeight )
310 return ( atlas + 1u );
316 void AtlasManager::CreateMesh( SizeType atlas,
318 SizeType imageHeight,
319 const Vector2& position,
320 SizeType widthInBlocks,
321 SizeType heightInBlocks,
322 Toolkit::AtlasManager::Mesh2D& mesh,
323 AtlasSlotDescriptor& desc )
326 Toolkit::AtlasManager::Vertex2D vertex;
327 uint32_t faceIndex = 0; // TODO change to unsigned short when property type is available
329 SizeType blockWidth = mAtlasList[ atlas ].mSize.mBlockWidth;
330 SizeType blockHeight = mAtlasList[ atlas ].mSize.mBlockHeight;
332 float vertexBlockWidth = static_cast< float >( blockWidth );
333 float vertexBlockHeight = static_cast< float >( blockHeight );
335 SizeType width = mAtlasList[ atlas ].mSize.mWidth;
336 SizeType height = mAtlasList[ atlas ].mSize.mHeight;
338 SizeType atlasWidthInBlocks = width / blockWidth;
340 // Get the normalized size of a texel in both directions
341 // TODO when texture resizing and passing texture size via uniforms is available,
342 // we will encode pixel positions into the vertex data rather than normalized
343 // meaning that geometry needn't be changed on an atlas resize
344 float texelX = 1.0f / static_cast< float >( width );
345 float texelY = 1.0f / static_cast< float >( height );
347 // Get the normalized size of a block in texels
348 float texelBlockWidth = texelX * vertexBlockWidth;
349 float texelBlockHeight = texelY * vertexBlockHeight;
351 // Get partial block space
352 float vertexEdgeWidth = static_cast< float >( imageWidth % blockWidth );
353 float vertexEdgeHeight = static_cast< float >( imageHeight % blockHeight );
356 float texelEdgeWidth = vertexEdgeWidth * texelX;
357 float texelEdgeHeight = vertexEdgeHeight * texelY;
359 // Block by block create the two triangles for the quad
360 SizeType blockIndex = 0;
366 Vector2 topLeft = position;
368 for ( SizeType y = 0; y < heightInBlocks; ++y )
371 float currentX = position.x;
373 if ( ( heightInBlocks - 1u ) == y && vertexEdgeHeight > 0.0f )
375 ndcHeight = texelEdgeHeight;
376 ndcVHeight = vertexEdgeHeight;
380 ndcHeight = texelBlockHeight;
381 ndcVHeight = vertexBlockHeight;
384 for ( SizeType x = 0; x < widthInBlocks; ++x )
386 SizeType block = desc.mBlocksList[ blockIndex++ ];
388 float fBlockX = texelBlockWidth * static_cast< float >( block % atlasWidthInBlocks );
389 float fBlockY = texelBlockHeight * static_cast< float >( block / atlasWidthInBlocks );
391 // Add on texture filtering compensation
395 if ( ( widthInBlocks - 1u ) == x && vertexEdgeWidth > 0.0f )
397 ndcWidth = texelEdgeWidth;
398 ndcVWidth = vertexEdgeWidth;
402 ndcWidth = texelBlockWidth;
403 ndcVWidth = vertexBlockWidth;
407 vertex.mPosition.x = topLeft.x;
408 vertex.mPosition.y = topLeft.y;
409 vertex.mTexCoords.x = fBlockX;
410 vertex.mTexCoords.y = fBlockY;
412 mesh.mVertices.PushBack( vertex );
415 vertex.mPosition.x = topLeft.x + ndcVWidth;
416 vertex.mPosition.y = topLeft.y;
417 vertex.mTexCoords.x = fBlockX + ndcWidth;
418 vertex.mTexCoords.y = fBlockY;
420 mesh.mVertices.PushBack( vertex );
423 vertex.mPosition.x = topLeft.x;
424 vertex.mPosition.y = topLeft.y + ndcVHeight;
425 vertex.mTexCoords.x = fBlockX;
426 vertex.mTexCoords.y = fBlockY + ndcHeight;
428 mesh.mVertices.PushBack( vertex );
431 topLeft.x += ndcVWidth;
432 vertex.mPosition.x = topLeft.x;
433 vertex.mPosition.y = topLeft.y + ndcVHeight;
434 vertex.mTexCoords.x = fBlockX + ndcWidth;
435 vertex.mTexCoords.y = fBlockY + ndcHeight;
437 mesh.mVertices.PushBack( vertex );
439 // Six indices in counter clockwise winding
440 mesh.mIndices.PushBack( faceIndex + 1u );
441 mesh.mIndices.PushBack( faceIndex );
442 mesh.mIndices.PushBack( faceIndex + 2u );
443 mesh.mIndices.PushBack( faceIndex + 2u );
444 mesh.mIndices.PushBack( faceIndex + 3u );
445 mesh.mIndices.PushBack( faceIndex + 1u );
450 topLeft.x = currentX;
451 topLeft.y += vertexBlockHeight;
454 // If there's only one block then skip this next vertex optimisation
455 if ( widthInBlocks * heightInBlocks > 1 )
457 Toolkit::AtlasManager::Mesh2D optimizedMesh;
458 OptimizeMesh( mesh, optimizedMesh );
460 //PrintMeshData( mesh );
463 void AtlasManager::PrintMeshData( const Toolkit::AtlasManager::Mesh2D& mesh )
465 uint32_t vertexCount = mesh.mVertices.Size();
466 uint32_t indexCount = mesh.mIndices.Size();
467 std::cout << "\nMesh Data for Image: VertexCount = " << vertexCount;
468 std::cout << ", Triangles = " << indexCount / 3 << std::endl;
470 for ( SizeType v = 0; v < vertexCount; ++v )
472 std::cout << " Vertex(" << v << ") x = " << mesh.mVertices[v].mPosition.x << ", ";
473 std::cout << "y = " << mesh.mVertices[v].mPosition.y << ", ";
474 std::cout << "u = " << mesh.mVertices[v].mTexCoords.x << ", ";
475 std::cout << "v = " << mesh.mVertices[v].mTexCoords.y << std::endl;
478 std::cout << "\n Indices: ";
479 for ( SizeType i = 0; i < indexCount; ++i )
481 std::cout << " " << mesh.mIndices[ i ];
483 std::cout << std::endl;
486 void AtlasManager::OptimizeMesh( const Toolkit::AtlasManager::Mesh2D& in,
487 Toolkit::AtlasManager::Mesh2D& out )
489 unsigned short vertexIndex = 0;
491 // We could check to see if blocks are next to each other, but it's probably just as quick to compare verts
492 for ( SizeType i = 0; i < in.mIndices.Size(); ++i )
494 // Fetch a vertex, has it already been assigned?
495 bool foundVertex = false;
496 Toolkit::AtlasManager::Vertex2D v = in.mVertices[ in.mIndices[ i ] ];
497 for ( SizeType j = 0; j < out.mVertices.Size(); ++j )
499 if ( v.mPosition.x == out.mVertices[ j ].mPosition.x && v.mPosition.y == out.mVertices[ j ].mPosition.y &&
500 v.mTexCoords.x == out.mVertices[ j ].mTexCoords.x && v.mTexCoords.y == out.mVertices[ j ].mTexCoords.y )
502 // Yes, so store this down as the vertex to use
503 out.mIndices.PushBack( j );
509 // Did we find a vertex ?
512 // No so add a new one
513 out.mVertices.PushBack( v );
519 void AtlasManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
520 const Toolkit::AtlasManager::Mesh2D& second,
523 uint32_t vc = first.mVertices.Size();
525 for ( uint32_t v = 0; v < second.mVertices.Size(); ++v )
527 first.mVertices.PushBack( second.mVertices[ v ] );
530 for ( uint32_t i = 0; i < second.mIndices.Size(); ++i )
532 first.mIndices.PushBack( second.mIndices[ i ] + vc );
537 Toolkit::AtlasManager::Mesh2D optimizedMesh;
538 OptimizeMesh( first, optimizedMesh );
539 first = optimizedMesh;
543 void AtlasManager::StitchMesh( const Toolkit::AtlasManager::Mesh2D& first,
544 const Toolkit::AtlasManager::Mesh2D& second,
545 Toolkit::AtlasManager::Mesh2D& out,
548 uint32_t vc = first.mVertices.Size();
550 for ( uint32_t v = 0; v < vc; ++v )
552 out.mVertices.PushBack( first.mVertices[ v ] );
555 for ( uint32_t v = 0; v < second.mVertices.Size(); ++v )
557 out.mVertices.PushBack( second.mVertices[ v ] );
560 for ( uint32_t i = 0; i < first.mIndices.Size(); ++i )
562 out.mIndices.PushBack( first.mIndices[ i ] );
565 for ( uint32_t i = 0; i < second.mIndices.Size(); ++i )
567 out.mIndices.PushBack( second.mIndices[ i ] + vc );
572 Toolkit::AtlasManager::Mesh2D optimizedMesh;
573 OptimizeMesh( out, optimizedMesh );
578 void AtlasManager::UploadImage( const BufferImage& image,
579 const AtlasSlotDescriptor& desc )
581 // Get the atlas to upload the image to
582 SizeType atlas = desc.mAtlasId - 1u;
584 // Check to see that the pixel formats are compatible
585 if ( image.GetPixelFormat() != mAtlasList[ atlas ].mPixelFormat )
587 DALI_LOG_ERROR("Cannot upload an image with a different PixelFormat to the Atlas.\n");
591 SizeType atlasBlockWidth = mAtlasList[ atlas ].mSize.mBlockWidth;
592 SizeType atlasBlockHeight = mAtlasList[ atlas ].mSize.mBlockHeight;
593 SizeType atlasWidthInBlocks = mAtlasList[ atlas ].mSize.mWidth / mAtlasList[ atlas ].mSize.mBlockWidth;
595 SizeType block = desc.mBlocksList[ 0 ];
596 SizeType blockX = block % atlasWidthInBlocks;
597 SizeType blockY = block / atlasWidthInBlocks;
598 SizeType blockOffsetX = blockX * atlasBlockWidth;
599 SizeType blockOffsetY = blockY * atlasBlockHeight;
601 SizeType width = image.GetWidth();
602 SizeType height = image.GetHeight();
604 // Blit image 1 pixel to the right and down into the block to compensate for texture filtering
605 if ( !mAtlasList[ atlas ].mAtlas.Upload( image,
606 blockOffsetX + SINGLE_PIXEL_PADDING,
607 blockOffsetY + SINGLE_PIXEL_PADDING ) )
609 DALI_LOG_ERROR("Uploading image to Atlas Failed!.\n");
612 // If this is the first block then we need to keep the first pixel free for underline texture
617 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mHorizontalStrip,
621 DALI_LOG_ERROR("Uploading top strip to Atlas Failed!\n");
625 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mVerticalStrip,
627 blockOffsetY + SINGLE_PIXEL_PADDING ) )
629 DALI_LOG_ERROR("Uploading left strip to Atlas Failed!\n");
634 if ( blockOffsetY + height + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mHeight )
636 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mHorizontalStrip,
638 blockOffsetY + height + SINGLE_PIXEL_PADDING ) )
640 DALI_LOG_ERROR("Uploading bottom strip to Atlas Failed!.\n");
645 if ( blockOffsetX + width + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mWidth )
647 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mVerticalStrip,
648 blockOffsetX + width + SINGLE_PIXEL_PADDING,
649 blockOffsetY + SINGLE_PIXEL_PADDING ) )
651 DALI_LOG_ERROR("Uploading right strip to Atlas Failed!.\n");
656 void AtlasManager::GenerateMeshData( ImageId id,
657 const Vector2& position,
658 Toolkit::AtlasManager::Mesh2D& meshData )
660 // Read the atlas Id to use for this image
661 SizeType imageId = id - 1u;
662 SizeType atlas = mImageList[ imageId ].mAtlasId - 1u;
663 SizeType width = mImageList[ imageId ].mImageWidth;
664 SizeType height = mImageList[ imageId ].mImageHeight;
666 SizeType widthInBlocks = width / mAtlasList[ atlas ].mSize.mBlockWidth;
667 if ( width % mAtlasList[ atlas ].mSize.mBlockWidth )
671 SizeType heightInBlocks = height / mAtlasList[ atlas ].mSize.mBlockHeight;
672 if ( height % mAtlasList[ atlas ].mSize.mBlockHeight )
677 CreateMesh( atlas, width, height, position, widthInBlocks, heightInBlocks, meshData, mImageList[ imageId ] );
679 // Mesh created so increase the reference count
680 mImageList[ imageId ].mCount++;
683 Dali::Atlas AtlasManager::GetAtlasContainer( AtlasId atlas ) const
686 if ( !atlas || atlas > mAtlasList.size( ) )
689 DALI_LOG_ERROR("Cannot get Atlas from AtlasID ( doesn't exist ).\n");
692 return mAtlasList[ atlas -1u ].mAtlas;
695 bool AtlasManager::Remove( ImageId id )
697 // Decrements the reference count of this image, and removes the blocks if zero.
698 SizeType imageId = id - 1u;
699 bool removed = false;
701 if ( id > mImageList.size() )
703 DALI_LOG_ERROR("Atlas was asked to free an invalid imageID: %i\n", id );
707 // If we attempt to free an image that is already freed then do nothing, other than log
708 if ( !mImageList[ imageId ].mCount )
710 DALI_LOG_ERROR("Atlas was asked to free an imageID: %i, that has already been freed!\n", id );
714 if ( 2u > --mImageList[ imageId ].mCount )
716 // 'Remove the blocks' from this image and add to the atlas' freelist
718 mImageList[ imageId ].mCount = 0;
719 SizeType atlas = mImageList[ imageId ].mAtlasId - 1u;
720 for ( uint32_t i = 0; i < mImageList[ imageId ].mBlocksList.Size(); ++i )
722 mAtlasList[ atlas ].mFreeBlocksList.PushBack( mImageList[ imageId ].mBlocksList[ i ] );
728 AtlasManager::AtlasId AtlasManager::GetAtlas( ImageId id ) const
730 if ( id && id <= mImageList.size() )
732 return mImageList[ id - 1u ].mAtlasId;
740 void AtlasManager::SetNewAtlasSize( const Toolkit::AtlasManager::AtlasSize& size )
742 mNewAtlasSize = size;
743 mNewAtlasSize.mBlockWidth += DOUBLE_PIXEL_PADDING;
744 mNewAtlasSize.mBlockHeight += DOUBLE_PIXEL_PADDING;
747 const Toolkit::AtlasManager::AtlasSize& AtlasManager::GetAtlasSize( AtlasId atlas )
749 if ( atlas && atlas-- <= mAtlasList.size() )
751 return mAtlasList[ atlas ].mSize;
756 AtlasManager::SizeType AtlasManager::GetFreeBlocks( AtlasId atlas ) const
758 if ( atlas && atlas <= mAtlasList.size() )
760 uint32_t index = atlas - 1u;
761 uint32_t width = mAtlasList[ index ].mSize.mWidth;
762 uint32_t height = mAtlasList[ index ].mSize.mHeight;
763 uint32_t blockWidth = mAtlasList[ index ].mSize.mBlockWidth;
764 uint32_t blockHeight = mAtlasList[ index ].mSize.mBlockHeight;
766 SizeType widthInBlocks = width / blockWidth;
767 SizeType heightInBlocks = height / blockHeight;
768 uint32_t blockCount = widthInBlocks * heightInBlocks;
770 // Check free previously unallocated blocks and any free blocks
771 if ( mAtlasList[ index ].mNextFreeBlock )
773 blockCount -= mAtlasList[ index ].mNextFreeBlock -1u - mAtlasList[ index ].mFreeBlocksList.Size();
777 blockCount = mAtlasList[ index ].mFreeBlocksList.Size();
787 AtlasManager::SizeType AtlasManager::GetAtlasCount() const
789 return mAtlasList.size();
792 Pixel::Format AtlasManager::GetPixelFormat( AtlasId atlas )
794 if ( !atlas || atlas > mAtlasList.size( ) )
797 DALI_LOG_ERROR("Cannot get Atlas from AtlasID ( doesn't exist ).\n");
800 return mAtlasList[ atlas -1u ].mPixelFormat;
803 void AtlasManager::GetMetrics( Toolkit::AtlasManager::Metrics& metrics )
805 Toolkit::AtlasManager::AtlasMetricsEntry entry;
806 uint32_t textureMemoryUsed = 0;
807 uint32_t atlasCount = mAtlasList.size();
808 metrics.mAtlasCount = atlasCount;
809 metrics.mAtlasMetrics.Resize(0);
811 for ( uint32_t i = 0; i < atlasCount; ++i )
813 entry.mSize = mAtlasList[ i ].mSize;
814 entry.mTotalBlocks = ( entry.mSize.mWidth / entry.mSize.mBlockWidth ) * ( entry.mSize.mHeight / entry.mSize.mBlockHeight );
815 uint32_t reuseBlocks = mAtlasList[ i ].mFreeBlocksList.Size();
816 entry.mBlocksUsed = mAtlasList[ i ].mNextFreeBlock ? mAtlasList[ i ].mNextFreeBlock - reuseBlocks - 1u: entry.mTotalBlocks - reuseBlocks;
817 entry.mPixelFormat = GetPixelFormat( i + 1 );
819 metrics.mAtlasMetrics.PushBack( entry );
821 uint32_t size = entry.mSize.mWidth * entry.mSize.mHeight;
822 if ( entry.mPixelFormat == Pixel::BGRA8888 )
827 textureMemoryUsed += size;
830 metrics.mTextureMemoryUsed = textureMemoryUsed;
833 Material AtlasManager::GetMaterial( AtlasId atlas ) const
835 if ( atlas && atlas <= mAtlasList.size() )
837 return mAtlasList[ atlas -1u ].mMaterial;
843 } // namespace Internal
845 } // namespace Toolkit