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/devel-api/rendering/sampler.h>
24 #include <dali/devel-api/rendering/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 ( SizeType i = 0; i < mAtlasList.size(); ++i )
109 mAtlasList[ i ].mAtlas.UploadedSignal().Disconnect( this, &AtlasManager::OnUpload );
110 delete[] mAtlasList[ i ].mStripBuffer;
113 // Are there any upload signals pending? Free up those buffer images now.
114 for ( SizeType i = 0; i < mUploadedImages.Size(); ++i )
116 delete[] mUploadedImages[ i ];
120 void AtlasManager::OnUpload( Image image )
122 if ( mUploadedImages.Size() )
124 delete[] mUploadedImages[ 0 ];
125 mUploadedImages.Erase( mUploadedImages.Begin() );
129 DALI_LOG_ERROR("Atlas Image Upload List should not be empty\n");
133 Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( const Toolkit::AtlasManager::AtlasSize& size, Pixel::Format pixelformat )
135 SizeType width = size.mWidth;
136 SizeType height = size.mHeight;
137 SizeType blockWidth = size.mBlockWidth;
138 SizeType blockHeight = size.mBlockHeight;
140 // Check to see if the atlas is large enough to hold a single block even ?
141 if ( blockWidth > width || blockHeight > height )
143 DALI_LOG_ERROR("Atlas %i x %i too small. Dimensions need to be at least %ix%i\n",
144 width, height, blockWidth, blockHeight );
148 Dali::Atlas atlas = Dali::Atlas::New( width, height, pixelformat );
149 AtlasDescriptor atlasDescriptor;
150 atlasDescriptor.mAtlas = atlas;
151 atlasDescriptor.mSize = size;
152 atlasDescriptor.mPixelFormat = pixelformat;
153 atlasDescriptor.mNextFreeBlock = 1u; // indicate next free block will be the first ( +1 )
154 atlas.UploadedSignal().Connect( this, &AtlasManager::OnUpload );
156 // What size do we need for this atlas' strip buffer ( assume 32bit pixel format )?
157 SizeType neededStripSize =( blockWidth > blockHeight - DOUBLE_PIXEL_PADDING ? blockWidth : blockHeight - DOUBLE_PIXEL_PADDING ) << 2;
158 atlasDescriptor.mStripBuffer = new PixelBuffer[ neededStripSize ];
159 memset( atlasDescriptor.mStripBuffer, 0, neededStripSize );
161 atlasDescriptor.mHorizontalStrip = BufferImage::New( atlasDescriptor.mStripBuffer,
163 SINGLE_PIXEL_PADDING,
166 atlasDescriptor.mVerticalStrip = BufferImage::New( atlasDescriptor.mStripBuffer,
167 SINGLE_PIXEL_PADDING,
168 blockHeight - DOUBLE_PIXEL_PADDING,
170 mUploadedImages.PushBack( NULL );
171 atlasDescriptor.mFilledPixelImage = BufferImage::New( reinterpret_cast< PixelBuffer* >( &mFilledPixel ), 1, 1, pixelformat );
172 atlas.Upload( atlasDescriptor.mFilledPixelImage, 0, 0 );
174 Sampler sampler = Sampler::New( atlas, "sTexture" );
175 sampler.SetProperty( Sampler::Property::AFFECTS_TRANSPARENCY, true );
177 if ( pixelformat == Pixel::BGRA8888 )
179 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_BGRA );
183 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_L8 );
185 atlasDescriptor.mMaterial = Material::New( shader );
186 atlasDescriptor.mMaterial.AddSampler( sampler );
187 atlasDescriptor.mSampler = sampler;
188 atlasDescriptor.mMaterial.SetBlendMode( BlendingMode::ON );
189 mAtlasList.push_back( atlasDescriptor );
190 return mAtlasList.size();
193 void AtlasManager::SetAddPolicy( Toolkit::AtlasManager::AddFailPolicy policy )
195 mAddFailPolicy = policy;
198 void AtlasManager::Add( const BufferImage& image,
199 Toolkit::AtlasManager::AtlasSlot& slot,
200 Toolkit::AtlasManager::AtlasId atlas )
202 // See if there's a slot in an atlas that matches the requirements of this image
203 // A bitmap must be sliceable into a single atlas
204 Pixel::Format pixelFormat = image.GetPixelFormat();
205 SizeType width = image.GetWidth();
206 SizeType height = image.GetHeight();
207 SizeType blockArea = 0;
208 SizeType totalBlocks = 0;
209 SizeType foundAtlas = 0;
213 AtlasSlotDescriptor desc;
215 // If there is a preferred atlas then check for room in that first
218 foundAtlas = CheckAtlas( atlas, width, height, pixelFormat, blockArea, totalBlocks );
221 // Search current atlases to see if there is a good match
223 while( !foundAtlas && index < mAtlasList.size() )
225 foundAtlas = CheckAtlas( index, width, height, pixelFormat, blockArea, totalBlocks );
229 // If we can't find a suitable atlas then check the policy to determine action
232 if ( Toolkit::AtlasManager::FAIL_ON_ADD_CREATES == mAddFailPolicy )
234 SizeType newAtlas = CreateAtlas( mNewAtlasSize, pixelFormat );
241 foundAtlas = CheckAtlas( newAtlas, width, height, pixelFormat, blockArea, totalBlocks );
245 if ( Toolkit::AtlasManager::FAIL_ON_ADD_FAILS == mAddFailPolicy || !foundAtlas-- )
247 // Haven't found an atlas for this image!!!!!!
252 // Work out where the blocks are we're going to use
253 for ( SizeType i = 0; i < blockArea; ++i )
255 // Is there currently a next free block available ?
256 if ( mAtlasList[ foundAtlas ].mNextFreeBlock )
258 // Yes, so use this for our next block
259 SizeType selectedBlock = mAtlasList[ foundAtlas ].mNextFreeBlock - 1u;
260 desc.mBlocksList.PushBack( selectedBlock );
262 // Any blocks going to be available after this one (adjust to store +1 )?
265 if ( selectedBlock > totalBlocks )
267 // No so start trying to use free blocks list
270 mAtlasList[ foundAtlas ].mNextFreeBlock = selectedBlock;
274 // Our next block must be from the free list, fetch from the start of the list
275 desc.mBlocksList.PushBack( mAtlasList[ foundAtlas ].mFreeBlocksList[ 0 ] );
276 mAtlasList[ foundAtlas ].mFreeBlocksList.Remove( mAtlasList[ foundAtlas ].mFreeBlocksList.Begin() );
280 desc.mImageWidth = width;
281 desc.mImageHeight = height;
282 desc.mAtlasId = foundAtlas + 1u;
285 // See if there's a previously freed image ID that we can assign to this new image
286 uint32_t imageId = 0;
287 for ( uint32_t i = 0; i < mImageList.size(); ++i )
289 if ( !mImageList[ i ].mCount )
297 mImageList.push_back( desc );
298 slot.mImageId = mImageList.size();
302 mImageList[ imageId - 1u ] = desc;
303 slot.mImageId = imageId;
305 slot.mAtlasId = foundAtlas + 1u;
306 UploadImage( image, desc );
309 AtlasManager::SizeType AtlasManager::CheckAtlas( SizeType atlas,
312 Pixel::Format pixelFormat,
314 SizeType& totalBlocks )
316 if ( pixelFormat == mAtlasList[ atlas ].mPixelFormat )
318 // Check to see if there are any unused blocks in this atlas to accomodate our image
319 SizeType blocksInX = mAtlasList[ atlas ].mSize.mWidth / mAtlasList[ atlas ].mSize.mBlockWidth;
320 SizeType blocksInY = mAtlasList[ atlas ].mSize.mHeight / mAtlasList[ atlas ].mSize.mBlockHeight;
321 totalBlocks = blocksInX * blocksInY;
322 SizeType blocksFree = mAtlasList[ atlas ].mNextFreeBlock ?
323 totalBlocks - mAtlasList[ atlas ].mNextFreeBlock + 1u :
324 mAtlasList[ atlas ].mFreeBlocksList.Size();
326 // Check to see if the image will fit in these blocks, if not we'll need to create a new atlas
328 && width + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mBlockWidth
329 && height + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mBlockHeight )
332 return ( atlas + 1u );
338 void AtlasManager::CreateMesh( SizeType atlas,
340 SizeType imageHeight,
341 const Vector2& position,
342 SizeType widthInBlocks,
343 SizeType heightInBlocks,
344 Toolkit::AtlasManager::Mesh2D& mesh,
345 AtlasSlotDescriptor& desc )
348 Toolkit::AtlasManager::Vertex2D vertex;
349 uint32_t faceIndex = 0; // TODO change to unsigned short when property type is available
351 SizeType blockWidth = mAtlasList[ atlas ].mSize.mBlockWidth;
352 SizeType blockHeight = mAtlasList[ atlas ].mSize.mBlockHeight;
354 float vertexBlockWidth = static_cast< float >( blockWidth );
355 float vertexBlockHeight = static_cast< float >( blockHeight );
357 SizeType width = mAtlasList[ atlas ].mSize.mWidth;
358 SizeType height = mAtlasList[ atlas ].mSize.mHeight;
360 SizeType atlasWidthInBlocks = width / blockWidth;
362 // Get the normalized size of a texel in both directions
363 // TODO when texture resizing and passing texture size via uniforms is available,
364 // we will encode pixel positions into the vertex data rather than normalized
365 // meaning that geometry needn't be changed on an atlas resize
366 float texelX = 1.0f / static_cast< float >( width );
367 float texelY = 1.0f / static_cast< float >( height );
369 // Get the normalized size of a block in texels
370 float texelBlockWidth = texelX * vertexBlockWidth;
371 float texelBlockHeight = texelY * vertexBlockHeight;
373 // Get partial block space
374 float vertexEdgeWidth = static_cast< float >( imageWidth % blockWidth );
375 float vertexEdgeHeight = static_cast< float >( imageHeight % blockHeight );
378 float texelEdgeWidth = vertexEdgeWidth * texelX;
379 float texelEdgeHeight = vertexEdgeHeight * texelY;
381 // Block by block create the two triangles for the quad
382 SizeType blockIndex = 0;
388 Vector2 topLeft = position;
390 for ( SizeType y = 0; y < heightInBlocks; ++y )
393 float currentX = position.x;
395 if ( ( heightInBlocks - 1u ) == y && vertexEdgeHeight > 0.0f )
397 ndcHeight = texelEdgeHeight;
398 ndcVHeight = vertexEdgeHeight;
402 ndcHeight = texelBlockHeight;
403 ndcVHeight = vertexBlockHeight;
406 for ( SizeType x = 0; x < widthInBlocks; ++x )
408 SizeType block = desc.mBlocksList[ blockIndex++ ];
410 float fBlockX = texelBlockWidth * static_cast< float >( block % atlasWidthInBlocks );
411 float fBlockY = texelBlockHeight * static_cast< float >( block / atlasWidthInBlocks );
413 // Add on texture filtering compensation
417 if ( ( widthInBlocks - 1u ) == x && vertexEdgeWidth > 0.0f )
419 ndcWidth = texelEdgeWidth;
420 ndcVWidth = vertexEdgeWidth;
424 ndcWidth = texelBlockWidth;
425 ndcVWidth = vertexBlockWidth;
429 vertex.mPosition.x = topLeft.x;
430 vertex.mPosition.y = topLeft.y;
431 vertex.mTexCoords.x = fBlockX;
432 vertex.mTexCoords.y = fBlockY;
434 mesh.mVertices.PushBack( vertex );
437 vertex.mPosition.x = topLeft.x + ndcVWidth;
438 vertex.mPosition.y = topLeft.y;
439 vertex.mTexCoords.x = fBlockX + ndcWidth;
440 vertex.mTexCoords.y = fBlockY;
442 mesh.mVertices.PushBack( vertex );
445 vertex.mPosition.x = topLeft.x;
446 vertex.mPosition.y = topLeft.y + ndcVHeight;
447 vertex.mTexCoords.x = fBlockX;
448 vertex.mTexCoords.y = fBlockY + ndcHeight;
450 mesh.mVertices.PushBack( vertex );
453 topLeft.x += ndcVWidth;
454 vertex.mPosition.x = topLeft.x;
455 vertex.mPosition.y = topLeft.y + ndcVHeight;
456 vertex.mTexCoords.x = fBlockX + ndcWidth;
457 vertex.mTexCoords.y = fBlockY + ndcHeight;
459 mesh.mVertices.PushBack( vertex );
461 // Six indices in counter clockwise winding
462 mesh.mIndices.PushBack( faceIndex + 1u );
463 mesh.mIndices.PushBack( faceIndex );
464 mesh.mIndices.PushBack( faceIndex + 2u );
465 mesh.mIndices.PushBack( faceIndex + 2u );
466 mesh.mIndices.PushBack( faceIndex + 3u );
467 mesh.mIndices.PushBack( faceIndex + 1u );
472 topLeft.x = currentX;
473 topLeft.y += vertexBlockHeight;
476 // If there's only one block then skip this next vertex optimisation
477 if ( widthInBlocks * heightInBlocks > 1 )
479 Toolkit::AtlasManager::Mesh2D optimizedMesh;
480 OptimizeMesh( mesh, optimizedMesh );
482 //PrintMeshData( mesh );
485 void AtlasManager::PrintMeshData( const Toolkit::AtlasManager::Mesh2D& mesh )
487 uint32_t vertexCount = mesh.mVertices.Size();
488 uint32_t indexCount = mesh.mIndices.Size();
489 std::cout << "\nMesh Data for Image: VertexCount = " << vertexCount;
490 std::cout << ", Triangles = " << indexCount / 3 << std::endl;
492 for ( SizeType v = 0; v < vertexCount; ++v )
494 std::cout << " Vertex(" << v << ") x = " << mesh.mVertices[v].mPosition.x << ", ";
495 std::cout << "y = " << mesh.mVertices[v].mPosition.y << ", ";
496 std::cout << "u = " << mesh.mVertices[v].mTexCoords.x << ", ";
497 std::cout << "v = " << mesh.mVertices[v].mTexCoords.y << std::endl;
500 std::cout << "\n Indices: ";
501 for ( SizeType i = 0; i < indexCount; ++i )
503 std::cout << " " << mesh.mIndices[ i ];
505 std::cout << std::endl;
508 void AtlasManager::OptimizeMesh( const Toolkit::AtlasManager::Mesh2D& in,
509 Toolkit::AtlasManager::Mesh2D& out )
511 unsigned short vertexIndex = 0;
513 // We could check to see if blocks are next to each other, but it's probably just as quick to compare verts
514 for ( SizeType i = 0; i < in.mIndices.Size(); ++i )
516 // Fetch a vertex, has it already been assigned?
517 bool foundVertex = false;
518 Toolkit::AtlasManager::Vertex2D v = in.mVertices[ in.mIndices[ i ] ];
519 for ( SizeType j = 0; j < out.mVertices.Size(); ++j )
521 if ( v.mPosition.x == out.mVertices[ j ].mPosition.x && v.mPosition.y == out.mVertices[ j ].mPosition.y &&
522 v.mTexCoords.x == out.mVertices[ j ].mTexCoords.x && v.mTexCoords.y == out.mVertices[ j ].mTexCoords.y )
524 // Yes, so store this down as the vertex to use
525 out.mIndices.PushBack( j );
531 // Did we find a vertex ?
534 // No so add a new one
535 out.mVertices.PushBack( v );
541 void AtlasManager::StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
542 const Toolkit::AtlasManager::Mesh2D& second,
545 uint32_t vc = first.mVertices.Size();
547 for ( uint32_t v = 0; v < second.mVertices.Size(); ++v )
549 first.mVertices.PushBack( second.mVertices[ v ] );
552 for ( uint32_t i = 0; i < second.mIndices.Size(); ++i )
554 first.mIndices.PushBack( second.mIndices[ i ] + vc );
559 Toolkit::AtlasManager::Mesh2D optimizedMesh;
560 OptimizeMesh( first, optimizedMesh );
561 first = optimizedMesh;
565 void AtlasManager::StitchMesh( const Toolkit::AtlasManager::Mesh2D& first,
566 const Toolkit::AtlasManager::Mesh2D& second,
567 Toolkit::AtlasManager::Mesh2D& out,
570 uint32_t vc = first.mVertices.Size();
572 for ( uint32_t v = 0; v < vc; ++v )
574 out.mVertices.PushBack( first.mVertices[ v ] );
577 for ( uint32_t v = 0; v < second.mVertices.Size(); ++v )
579 out.mVertices.PushBack( second.mVertices[ v ] );
582 for ( uint32_t i = 0; i < first.mIndices.Size(); ++i )
584 out.mIndices.PushBack( first.mIndices[ i ] );
587 for ( uint32_t i = 0; i < second.mIndices.Size(); ++i )
589 out.mIndices.PushBack( second.mIndices[ i ] + vc );
594 Toolkit::AtlasManager::Mesh2D optimizedMesh;
595 OptimizeMesh( out, optimizedMesh );
600 void AtlasManager::UploadImage( const BufferImage& image,
601 const AtlasSlotDescriptor& desc )
603 // Get the atlas to upload the image to
604 SizeType atlas = desc.mAtlasId - 1u;
606 // Check to see that the pixel formats are compatible
607 if ( image.GetPixelFormat() != mAtlasList[ atlas ].mPixelFormat )
609 DALI_LOG_ERROR("Cannot upload an image with a different PixelFormat to the Atlas.\n");
613 SizeType atlasBlockWidth = mAtlasList[ atlas ].mSize.mBlockWidth;
614 SizeType atlasBlockHeight = mAtlasList[ atlas ].mSize.mBlockHeight;
615 SizeType atlasWidthInBlocks = mAtlasList[ atlas ].mSize.mWidth / mAtlasList[ atlas ].mSize.mBlockWidth;
617 SizeType block = desc.mBlocksList[ 0 ];
618 SizeType blockX = block % atlasWidthInBlocks;
619 SizeType blockY = block / atlasWidthInBlocks;
620 SizeType blockOffsetX = blockX * atlasBlockWidth;
621 SizeType blockOffsetY = blockY * atlasBlockHeight;
623 SizeType width = image.GetWidth();
624 SizeType height = image.GetHeight();
626 // Blit image 1 pixel to the right and down into the block to compensate for texture filtering
627 if ( !mAtlasList[ atlas ].mAtlas.Upload( image,
628 blockOffsetX + SINGLE_PIXEL_PADDING,
629 blockOffsetY + SINGLE_PIXEL_PADDING ) )
631 DALI_LOG_ERROR("Uploading image to Atlas Failed!.\n");
635 mUploadedImages.PushBack( const_cast< BufferImage& >( image ).GetBuffer() );
638 // If this is the first block then we need to keep the first pixel free for underline texture
643 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mHorizontalStrip,
647 DALI_LOG_ERROR("Uploading top strip to Atlas Failed!\n");
651 mUploadedImages.PushBack( NULL );
655 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mVerticalStrip,
657 blockOffsetY + SINGLE_PIXEL_PADDING ) )
659 DALI_LOG_ERROR("Uploading left strip to Atlas Failed!\n");
663 mUploadedImages.PushBack( NULL );
668 if ( blockOffsetY + height + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mHeight )
670 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mHorizontalStrip,
672 blockOffsetY + height + SINGLE_PIXEL_PADDING ) )
674 DALI_LOG_ERROR("Uploading bottom strip to Atlas Failed!.\n");
678 mUploadedImages.PushBack( NULL );
683 if ( blockOffsetX + width + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mWidth )
685 if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mVerticalStrip,
686 blockOffsetX + width + SINGLE_PIXEL_PADDING,
687 blockOffsetY + SINGLE_PIXEL_PADDING ) )
689 DALI_LOG_ERROR("Uploading right strip to Atlas Failed!.\n");
693 mUploadedImages.PushBack( NULL );
698 void AtlasManager::GenerateMeshData( ImageId id,
699 const Vector2& position,
700 Toolkit::AtlasManager::Mesh2D& meshData )
702 // Read the atlas Id to use for this image
703 SizeType imageId = id - 1u;
704 SizeType atlas = mImageList[ imageId ].mAtlasId - 1u;
705 SizeType width = mImageList[ imageId ].mImageWidth;
706 SizeType height = mImageList[ imageId ].mImageHeight;
708 SizeType widthInBlocks = width / mAtlasList[ atlas ].mSize.mBlockWidth;
709 if ( width % mAtlasList[ atlas ].mSize.mBlockWidth )
713 SizeType heightInBlocks = height / mAtlasList[ atlas ].mSize.mBlockHeight;
714 if ( height % mAtlasList[ atlas ].mSize.mBlockHeight )
719 CreateMesh( atlas, width, height, position, widthInBlocks, heightInBlocks, meshData, mImageList[ imageId ] );
721 // Mesh created so increase the reference count
722 mImageList[ imageId ].mCount++;
725 Dali::Atlas AtlasManager::GetAtlasContainer( AtlasId atlas ) const
728 if ( !atlas || atlas > mAtlasList.size( ) )
731 DALI_LOG_ERROR("Cannot get Atlas from AtlasID ( doesn't exist ).\n");
734 return mAtlasList[ atlas -1u ].mAtlas;
737 bool AtlasManager::Remove( ImageId id )
739 // Decrements the reference count of this image, and removes the blocks if zero.
740 SizeType imageId = id - 1u;
741 bool removed = false;
743 if ( id > mImageList.size() )
745 DALI_LOG_ERROR("Atlas was asked to free an invalid imageID: %i\n", id );
749 // If we attempt to free an image that is already freed then do nothing, other than log
750 if ( !mImageList[ imageId ].mCount )
752 DALI_LOG_ERROR("Atlas was asked to free an imageID: %i, that has already been freed!\n", id );
756 if ( 2u > --mImageList[ imageId ].mCount )
758 // 'Remove the blocks' from this image and add to the atlas' freelist
760 mImageList[ imageId ].mCount = 0;
761 SizeType atlas = mImageList[ imageId ].mAtlasId - 1u;
762 for ( uint32_t i = 0; i < mImageList[ imageId ].mBlocksList.Size(); ++i )
764 mAtlasList[ atlas ].mFreeBlocksList.PushBack( mImageList[ imageId ].mBlocksList[ i ] );
770 AtlasManager::AtlasId AtlasManager::GetAtlas( ImageId id ) const
772 if ( id && id <= mImageList.size() )
774 return mImageList[ id - 1u ].mAtlasId;
782 void AtlasManager::SetNewAtlasSize( const Toolkit::AtlasManager::AtlasSize& size )
784 mNewAtlasSize = size;
785 mNewAtlasSize.mBlockWidth += DOUBLE_PIXEL_PADDING;
786 mNewAtlasSize.mBlockHeight += DOUBLE_PIXEL_PADDING;
789 const Toolkit::AtlasManager::AtlasSize& AtlasManager::GetAtlasSize( AtlasId atlas )
791 if ( atlas && atlas-- <= mAtlasList.size() )
793 return mAtlasList[ atlas ].mSize;
798 AtlasManager::SizeType AtlasManager::GetFreeBlocks( AtlasId atlas ) const
800 if ( atlas && atlas <= mAtlasList.size() )
802 uint32_t index = atlas - 1u;
803 uint32_t width = mAtlasList[ index ].mSize.mWidth;
804 uint32_t height = mAtlasList[ index ].mSize.mHeight;
805 uint32_t blockWidth = mAtlasList[ index ].mSize.mBlockWidth;
806 uint32_t blockHeight = mAtlasList[ index ].mSize.mBlockHeight;
808 SizeType widthInBlocks = width / blockWidth;
809 SizeType heightInBlocks = height / blockHeight;
810 uint32_t blockCount = widthInBlocks * heightInBlocks;
812 // Check free previously unallocated blocks and any free blocks
813 if ( mAtlasList[ index ].mNextFreeBlock )
815 blockCount -= mAtlasList[ index ].mNextFreeBlock -1u - mAtlasList[ index ].mFreeBlocksList.Size();
819 blockCount = mAtlasList[ index ].mFreeBlocksList.Size();
829 AtlasManager::SizeType AtlasManager::GetAtlasCount() const
831 return mAtlasList.size();
834 Pixel::Format AtlasManager::GetPixelFormat( AtlasId atlas )
836 if ( !atlas || atlas > mAtlasList.size( ) )
839 DALI_LOG_ERROR("Cannot get Atlas from AtlasID ( doesn't exist ).\n");
842 return mAtlasList[ atlas -1u ].mPixelFormat;
845 void AtlasManager::GetMetrics( Toolkit::AtlasManager::Metrics& metrics )
847 Toolkit::AtlasManager::AtlasMetricsEntry entry;
848 uint32_t textureMemoryUsed = 0;
849 uint32_t atlasCount = mAtlasList.size();
850 metrics.mAtlasCount = atlasCount;
851 metrics.mAtlasMetrics.Resize(0);
853 for ( uint32_t i = 0; i < atlasCount; ++i )
855 entry.mSize = mAtlasList[ i ].mSize;
856 entry.mTotalBlocks = ( entry.mSize.mWidth / entry.mSize.mBlockWidth ) * ( entry.mSize.mHeight / entry.mSize.mBlockHeight );
857 uint32_t reuseBlocks = mAtlasList[ i ].mFreeBlocksList.Size();
858 entry.mBlocksUsed = mAtlasList[ i ].mNextFreeBlock ? mAtlasList[ i ].mNextFreeBlock - reuseBlocks - 1u: entry.mTotalBlocks - reuseBlocks;
859 entry.mPixelFormat = GetPixelFormat( i + 1 );
861 metrics.mAtlasMetrics.PushBack( entry );
863 uint32_t size = entry.mSize.mWidth * entry.mSize.mHeight;
864 if ( entry.mPixelFormat == Pixel::BGRA8888 )
869 textureMemoryUsed += size;
872 metrics.mTextureMemoryUsed = textureMemoryUsed;
875 Material AtlasManager::GetMaterial( AtlasId atlas ) const
877 if ( atlas && atlas <= mAtlasList.size() )
879 return mAtlasList[ atlas -1u ].mMaterial;
885 Sampler AtlasManager::GetSampler( AtlasId atlas ) const
887 if ( atlas && atlas <= mAtlasList.size() )
889 return mAtlasList[ atlas -1u ].mSampler;
895 } // namespace Internal
897 } // namespace Toolkit