Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / atlas-manager / atlas-manager-impl.cpp
index 15e9684..83bb423 100644 (file)
@@ -35,15 +35,16 @@ namespace
 {
   const Vector2 DEFAULT_ATLAS_SIZE( 512.0f, 512.0f );
   const Vector2 DEFAULT_BLOCK_SIZE( 32.0f, 32.0f );
-  const uint32_t PIXEL_PADDING( 2u );
+  const uint32_t SINGLE_PIXEL_PADDING( 1u );
+  const uint32_t DOUBLE_PIXEL_PADDING( SINGLE_PIXEL_PADDING << 1 );
+  const uint32_t FILLED_PIXEL( -1 );
 }
 
 AtlasManager::AtlasManager()
 : mNewAtlasSize( DEFAULT_ATLAS_SIZE ),
   mNewBlockSize( DEFAULT_BLOCK_SIZE ),
   mAddFailPolicy( Toolkit::AtlasManager::FAIL_ON_ADD_CREATES ),
-  mEdgeBuffer( NULL ),
-  mEdgeBufferSize( 0 )
+  mFilledPixel( FILLED_PIXEL )
 {
 }
 
@@ -55,7 +56,10 @@ AtlasManagerPtr AtlasManager::New()
 
 AtlasManager::~AtlasManager()
 {
-  delete[] mEdgeBuffer;
+  for ( uint32_t i = 0; i < mAtlasList.size(); ++i )
+  {
+    delete[] mAtlasList[ i ].mStripBuffer;
+  }
 }
 
 Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( SizeType width,
@@ -67,7 +71,7 @@ Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( SizeType width,
   // Check to see if the atlas is large enough to hold a single block even ?
   if ( blockWidth > width || blockHeight > height )
   {
-    DALI_LOG_ERROR("Atlas %i x %i too small. Dimensions need to be at least %i x %i\n",
+    DALI_LOG_ERROR("Atlas %i x %i too small. Dimensions need to be at least %ix%i\n",
                     width, height, blockWidth, blockHeight );
     return 0;
   }
@@ -87,21 +91,22 @@ Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( SizeType width,
   atlasDescriptor.mMaterial.SetDiffuseTexture( atlas );
   atlasDescriptor.mNextFreeBlock = 1u; // indicate next free block will be the first ( +1 )
 
-  // What size do we need for this atlas' edge buffer ( assume RGBA pixel format )?
-  uint32_t neededEdgeSize = ( blockWidth > blockHeight ? blockWidth : blockHeight ) << 2;
-
-  // Is the current edge buffer large enough?
-  if ( neededEdgeSize > mEdgeBufferSize )
-  {
-    delete[] mEdgeBuffer;
-    mEdgeBuffer = new PixelBuffer[ neededEdgeSize ];
-    memset( mEdgeBuffer, 0, neededEdgeSize );
-    mEdgeBufferSize = neededEdgeSize;
-  }
-
-  atlasDescriptor.mEdgeX = BufferImage::New( mEdgeBuffer, blockWidth, PIXEL_PADDING, pixelformat );
-  atlasDescriptor.mEdgeY = BufferImage::New( mEdgeBuffer, PIXEL_PADDING, blockHeight, pixelformat );
-
+  // What size do we need for this atlas' strip buffer ( assume 32bit pixel format )?
+  uint32_t neededStripSize =( blockWidth > blockHeight - DOUBLE_PIXEL_PADDING ? blockWidth : blockHeight - DOUBLE_PIXEL_PADDING ) << 2;
+  atlasDescriptor.mStripBuffer = new PixelBuffer[ neededStripSize ];
+  memset( atlasDescriptor.mStripBuffer, 0, neededStripSize );
+
+  atlasDescriptor.mHorizontalStrip = BufferImage::New( atlasDescriptor.mStripBuffer,
+                                                       blockWidth,
+                                                       SINGLE_PIXEL_PADDING,
+                                                       pixelformat );
+
+  atlasDescriptor.mVerticalStrip = BufferImage::New( atlasDescriptor.mStripBuffer,
+                                                     SINGLE_PIXEL_PADDING,
+                                                     blockHeight - DOUBLE_PIXEL_PADDING,
+                                                     pixelformat );
+  atlasDescriptor.mFilledPixelImage = BufferImage::New( reinterpret_cast< PixelBuffer* >( &mFilledPixel ), 1, 1, pixelformat );
+  atlas.Upload( atlasDescriptor.mFilledPixelImage, 0, 0 );
   mAtlasList.push_back( atlasDescriptor );
   return mAtlasList.size();
 }
@@ -235,12 +240,14 @@ AtlasManager::SizeType AtlasManager::CheckAtlas( SizeType atlas,
     SizeType blocksInX = mAtlasList[ atlas ].mWidth / mAtlasList[ atlas ].mBlockWidth;
     SizeType blocksInY = mAtlasList[ atlas ].mHeight / mAtlasList[ atlas ].mBlockHeight;
     totalBlocks = blocksInX * blocksInY;
-    SizeType blocksFree = mAtlasList[ atlas ].mNextFreeBlock ? totalBlocks - mAtlasList[ atlas ].mNextFreeBlock + 1u : 0;
+    SizeType blocksFree = mAtlasList[ atlas ].mNextFreeBlock ?
+                          totalBlocks - mAtlasList[ atlas ].mNextFreeBlock + 1u :
+                          mAtlasList[ atlas ].mFreeBlocksList.Size();
 
     // Check to see if the image will fit in these blocks, if not we'll need to create a new atlas
     if ( blocksFree
-         && width + PIXEL_PADDING <= mAtlasList[ atlas ].mBlockWidth
-         && height + PIXEL_PADDING <= mAtlasList[ atlas ].mBlockHeight )
+         && width + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mBlockWidth
+         && height + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mBlockHeight )
     {
       blockArea = 1u;
       return ( atlas + 1u );
@@ -328,6 +335,10 @@ void AtlasManager::CreateMesh( SizeType atlas,
       float fBlockX = texelBlockWidth * static_cast< float >( block % atlasWidthInBlocks );
       float fBlockY = texelBlockHeight * static_cast< float >( block / atlasWidthInBlocks );
 
+      // Add on texture filtering compensation
+      fBlockX += texelX;
+      fBlockY += texelY;
+
       if (  ( widthInBlocks - 1u ) == x && vertexEdgeWidth > 0.0f )
       {
         ndcWidth = texelEdgeWidth;
@@ -405,7 +416,6 @@ void AtlasManager::CreateMesh( SizeType atlas,
 
   meshData.SetFaceIndices( faces );
   meshData.SetMaterial( mAtlasList[ atlas ].mMaterial );
-  //PrintMeshData( meshData );
 }
 
 void AtlasManager::PrintMeshData( const MeshData& meshData )
@@ -502,9 +512,6 @@ void AtlasManager::StitchMesh( MeshData& first,
   }
 
   first.SetFaceIndices( f1 );
-
-  // TODO rather than set the material to the second, check to see if there's a match and return if not
-  first.SetMaterial( second.GetMaterial() );
 }
 
 void AtlasManager::StitchMesh( const MeshData& first,
@@ -512,7 +519,6 @@ void AtlasManager::StitchMesh( const MeshData& first,
                                MeshData& out,
                                bool optimize )
 {
-  // TODO Would be much quicker to be able to get a non-const reference to these containers and update in situ
   MeshData::VertexContainer v1 = first.GetVertices();
   MeshData::VertexContainer v2 = second.GetVertices();
   MeshData::FaceIndices f1 = first.GetFaces();
@@ -558,8 +564,7 @@ void AtlasManager::StitchMesh( const MeshData& first,
     out.SetVertices( vertices );
   }
 
-  // TODO rather than set the material to the second, check to see if there's a match and return if not
-  out.SetMaterial( second.GetMaterial() );
+  out.SetMaterial( first.GetMaterial() );
   out.SetFaceIndices( faces );
 }
 
@@ -589,22 +594,54 @@ void AtlasManager::UploadImage( const BufferImage& image,
   SizeType width = image.GetWidth();
   SizeType height = image.GetHeight();
 
-  if ( !mAtlasList[ atlas ].mAtlas.Upload( image, blockOffsetX, blockOffsetY ) )
+  // Blit image 1 pixel to the right and down into the block to compensate for texture filtering
+  if ( !mAtlasList[ atlas ].mAtlas.Upload( image,
+                                           blockOffsetX + SINGLE_PIXEL_PADDING,
+                                           blockOffsetY + SINGLE_PIXEL_PADDING ) )
   {
-    DALI_LOG_ERROR("Uploading block to Atlas Failed!.\n");
+    DALI_LOG_ERROR("Uploading image to Atlas Failed!.\n");
   }
-  if ( blockOffsetY + height + PIXEL_PADDING <= mAtlasList[ atlas ].mHeight )
+
+  // If this is the first block then we need to keep the first pixel free for underline texture
+  if ( block )
   {
-    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mEdgeX, blockOffsetX, blockOffsetY + height ) )
+
+    // Blit top strip
+    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mHorizontalStrip,
+                                             blockOffsetX,
+                                             blockOffsetY ) )
     {
-      DALI_LOG_ERROR("Uploading edgeX to Atlas Failed!.\n");
+      DALI_LOG_ERROR("Uploading top strip to Atlas Failed!\n");
+    }
+
+    // Blit left strip
+    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mVerticalStrip,
+                                             blockOffsetX,
+                                             blockOffsetY + SINGLE_PIXEL_PADDING ) )
+    {
+      DALI_LOG_ERROR("Uploading left strip to Atlas Failed!\n");
+    }
+  }
+
+  // Blit bottom strip
+  if ( blockOffsetY + height + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mHeight )
+  {
+    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mHorizontalStrip,
+                                             blockOffsetX,
+                                             blockOffsetY + height + SINGLE_PIXEL_PADDING ) )
+    {
+      DALI_LOG_ERROR("Uploading bottom strip to Atlas Failed!.\n");
     }
   }
-  if ( blockOffsetX + width + PIXEL_PADDING <= mAtlasList[ atlas ].mWidth )
+
+  // Blit right strip
+  if ( blockOffsetX + width + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mWidth )
   {
-    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mEdgeY, blockOffsetX + width, blockOffsetY ) )
+    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mVerticalStrip,
+                                             blockOffsetX + width + SINGLE_PIXEL_PADDING,
+                                             blockOffsetY + SINGLE_PIXEL_PADDING ) )
     {
-      DALI_LOG_ERROR("Uploading edgeY to Atlas Failed!.\n");
+      DALI_LOG_ERROR("Uploading right strip to Atlas Failed!.\n");
     }
   }
 }
@@ -693,8 +730,8 @@ AtlasManager::AtlasId AtlasManager::GetAtlas( ImageId id ) const
   }
 }
 
-void AtlasManager::SetAtlasSize( const Vector2& size,
-                                 const Vector2& blockSize )
+void AtlasManager::SetNewAtlasSize( const Vector2& size,
+                                    const Vector2& blockSize )
 {
   mNewAtlasSize = size;
   mNewBlockSize = blockSize;
@@ -709,7 +746,20 @@ Vector2 AtlasManager::GetBlockSize( AtlasId atlas )
   }
   else
   {
-    return Vector2( 0.0f, 0.0f );
+    return Vector2::ZERO;
+  }
+}
+
+Vector2 AtlasManager::GetAtlasSize( AtlasId atlas )
+{
+  if ( atlas && atlas <= mAtlasList.size() )
+  {
+    return Vector2( static_cast< float >( mAtlasList[ atlas - 1u ].mWidth ),
+                    static_cast< float >( mAtlasList[ atlas - 1u ].mHeight ) );
+  }
+  else
+  {
+    return Vector2::ZERO;
   }
 }
 
@@ -723,18 +773,8 @@ AtlasManager::SizeType AtlasManager::GetFreeBlocks( AtlasId atlas ) const
     uint32_t blockWidth = mAtlasList[ index ].mBlockWidth;
     uint32_t blockHeight = mAtlasList[ index ].mBlockHeight;
 
-    //Work out how many blocks wide and high our bitmap is in the atlas' block size
     SizeType widthInBlocks = width / blockWidth;
-    if ( width % blockWidth )
-    {
-      widthInBlocks++;
-    }
     SizeType heightInBlocks = height / blockHeight;
-    if ( height % blockHeight )
-    {
-      heightInBlocks++;
-    }
-
     uint32_t blockCount = widthInBlocks * heightInBlocks;
 
     // Check free previously unallocated blocks and any free blocks
@@ -763,6 +803,44 @@ Pixel::Format AtlasManager::GetPixelFormat( AtlasId atlas )
   return mAtlasList[ atlas -1u ].mPixelFormat;
 }
 
+void AtlasManager::GetMetrics( Toolkit::AtlasManager::Metrics& metrics )
+{
+  Toolkit::AtlasManager::AtlasMetricsEntry entry;
+  uint32_t textureMemoryUsed = 0;
+  uint32_t atlasCount = mAtlasList.size();
+  metrics.mAtlasCount = atlasCount;
+  metrics.mAtlasMetrics.Resize(0);
+
+  for ( uint32_t i = 0; i < atlasCount; ++i )
+  {
+    SizeType width = mAtlasList[ i ].mWidth;
+    SizeType height = mAtlasList[ i ].mHeight;
+    SizeType blockWidth = mAtlasList[ i ].mBlockWidth;
+    SizeType blockHeight = mAtlasList[ i ].mBlockHeight;
+
+    entry.mWidth = width;
+    entry.mHeight = height;
+    entry.mBlockWidth = blockWidth;
+    entry.mBlockHeight = blockHeight;
+    entry.mTotalBlocks = ( width / blockWidth ) * ( height / blockHeight );
+    uint32_t reuseBlocks = mAtlasList[ i ].mFreeBlocksList.Size();
+    entry.mBlocksUsed = mAtlasList[ i ].mNextFreeBlock ? mAtlasList[ i ].mNextFreeBlock - reuseBlocks - 1u: entry.mTotalBlocks - reuseBlocks;
+    entry.mPixelFormat = GetPixelFormat( i + 1 );
+
+      metrics.mAtlasMetrics.PushBack( entry );
+
+    uint32_t size = width * height;
+    if ( entry.mPixelFormat == Pixel::BGRA8888 )
+    {
+      size <<= 2;
+    }
+
+    textureMemoryUsed += size;
+
+  }
+  metrics.mTextureMemoryUsed = textureMemoryUsed;
+}
+
 
 } // namespace Internal