Merge "Empty Implementation for INPUT_METHOD_SETTINGS property" into tizen
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / atlas-manager / atlas-manager-impl.cpp
index da6ae94..853c0ae 100644 (file)
@@ -17,8 +17,9 @@
 // CLASS HEADER
 #include <dali-toolkit/internal/atlas-manager/atlas-manager-impl.h>
 
-// EXTERNAL INCLUDES
+// EXTERNAL INCLUDE
 #include <iostream>
+#include <string.h>
 #include <dali/integration-api/debug.h>
 
 namespace Dali
@@ -32,15 +33,24 @@ namespace Internal
 
 namespace
 {
-  const Vector2 DEFAULT_ATLAS_SIZE( 512.0f, 512.0f );
-  const Vector2 DEFAULT_BLOCK_SIZE( 32.0f, 32.0f );
+  const uint32_t DEFAULT_ATLAS_WIDTH( 512u );
+  const uint32_t DEFAULT_ATLAS_HEIGHT( 512u );
+  const uint32_t DEFAULT_BLOCK_WIDTH( 16u );
+  const uint32_t DEFAULT_BLOCK_HEIGHT( 16u );
+  const uint32_t SINGLE_PIXEL_PADDING( 1u );
+  const uint32_t DOUBLE_PIXEL_PADDING( SINGLE_PIXEL_PADDING << 1 );
+  const uint32_t FILLED_PIXEL( -1 );
+  Toolkit::AtlasManager::AtlasSize EMPTY_SIZE;
 }
 
 AtlasManager::AtlasManager()
-: mNewAtlasSize( DEFAULT_ATLAS_SIZE ),
-  mNewBlockSize( DEFAULT_BLOCK_SIZE ),
-  mAddFailPolicy( Toolkit::AtlasManager::FAIL_ON_ADD_CREATES )
+: mAddFailPolicy( Toolkit::AtlasManager::FAIL_ON_ADD_CREATES ),
+  mFilledPixel( FILLED_PIXEL )
 {
+  mNewAtlasSize.mWidth = DEFAULT_ATLAS_WIDTH;
+  mNewAtlasSize.mHeight = DEFAULT_ATLAS_HEIGHT;
+  mNewAtlasSize.mBlockWidth = DEFAULT_BLOCK_WIDTH;
+  mNewAtlasSize.mBlockHeight = DEFAULT_BLOCK_HEIGHT;
 }
 
 AtlasManagerPtr AtlasManager::New()
@@ -51,18 +61,23 @@ AtlasManagerPtr AtlasManager::New()
 
 AtlasManager::~AtlasManager()
 {
+  for ( uint32_t i = 0; i < mAtlasList.size(); ++i )
+  {
+    delete[] mAtlasList[ i ].mStripBuffer;
+  }
 }
 
-Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( SizeType width,
-                                                          SizeType height,
-                                                          SizeType blockWidth,
-                                                          SizeType blockHeight,
-                                                          Pixel::Format pixelformat )
+Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( const Toolkit::AtlasManager::AtlasSize& size, Pixel::Format pixelformat )
 {
+  SizeType width = size.mWidth;
+  SizeType height = size.mHeight;
+  SizeType blockWidth = size.mBlockWidth;
+  SizeType blockHeight = size.mBlockHeight;
+
   // 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;
   }
@@ -70,10 +85,7 @@ Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( SizeType width,
   Dali::Atlas atlas = Dali::Atlas::New( width, height, pixelformat );
   AtlasDescriptor atlasDescriptor;
   atlasDescriptor.mAtlas = atlas;
-  atlasDescriptor.mWidth = width;
-  atlasDescriptor.mHeight = height;
-  atlasDescriptor.mBlockWidth = blockWidth;
-  atlasDescriptor.mBlockHeight = blockHeight;
+  atlasDescriptor.mSize = size;
   atlasDescriptor.mPixelFormat = pixelformat;
   std::stringstream materialLabel;
   materialLabel << "Atlas Material - ";
@@ -81,6 +93,23 @@ Toolkit::AtlasManager::AtlasId AtlasManager::CreateAtlas( SizeType width,
   atlasDescriptor.mMaterial = Material::New( materialLabel.str() );
   atlasDescriptor.mMaterial.SetDiffuseTexture( atlas );
   atlasDescriptor.mNextFreeBlock = 1u; // indicate next free block will be the first ( +1 )
+
+  // 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();
 }
@@ -126,7 +155,7 @@ void AtlasManager::Add( const BufferImage& image,
   {
     if ( Toolkit::AtlasManager::FAIL_ON_ADD_CREATES == mAddFailPolicy )
     {
-      SizeType newAtlas = CreateAtlas( mNewAtlasSize.x, mNewAtlasSize.y, mNewBlockSize.x, mNewBlockSize.y, pixelFormat );
+      SizeType newAtlas = CreateAtlas( mNewAtlasSize, pixelFormat );
       if ( !newAtlas-- )
       {
         return;
@@ -210,29 +239,20 @@ AtlasManager::SizeType AtlasManager::CheckAtlas( SizeType atlas,
 {
   if ( pixelFormat == mAtlasList[ atlas ].mPixelFormat )
   {
-    // Work out how many blocks wide and high our bitmap is in the atlas' block size
-    SizeType widthInBlocks = width / mAtlasList[ atlas ].mBlockWidth;
-    if ( width % mAtlasList[ atlas ].mBlockWidth )
-    {
-      widthInBlocks++;
-    }
-    SizeType heightInBlocks = height / mAtlasList[ atlas ].mBlockHeight;
-    if ( height % mAtlasList[ atlas ].mBlockHeight )
-    {
-      heightInBlocks++;
-    }
-    blockArea = widthInBlocks * heightInBlocks;
-
     // Check to see if there are any unused blocks in this atlas to accomodate our image
-    SizeType blocksInX = mAtlasList[ atlas ].mWidth / mAtlasList[ atlas ].mBlockWidth;
-    SizeType blocksInY = mAtlasList[ atlas ].mHeight / mAtlasList[ atlas ].mBlockHeight;
+    SizeType blocksInX = mAtlasList[ atlas ].mSize.mWidth / mAtlasList[ atlas ].mSize.mBlockWidth;
+    SizeType blocksInY = mAtlasList[ atlas ].mSize.mHeight / mAtlasList[ atlas ].mSize.mBlockHeight;
     totalBlocks = blocksInX * blocksInY;
-    SizeType blocksFree = mAtlasList[ atlas ].mNextFreeBlock ? totalBlocks - mAtlasList[ atlas ].mNextFreeBlock + 1u : 0;
-
-    // Check to see if there are enough blocks to accomodate our sliced image ?
-    if ( blockArea <= ( mAtlasList[ atlas ].mFreeBlocksList.Size() + blocksFree ) )
+    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 + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mBlockWidth
+         && height + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mBlockHeight )
     {
-      // Yes, we've found room
+      blockArea = 1u;
       return ( atlas + 1u );
     }
   }
@@ -256,14 +276,14 @@ void AtlasManager::CreateMesh( SizeType atlas,
   meshData.SetHasColor( true );
   meshData.SetHasTextureCoords( true );
 
-  SizeType blockWidth = mAtlasList[ atlas ].mBlockWidth;
-  SizeType blockHeight = mAtlasList[ atlas ].mBlockHeight;
+  SizeType blockWidth = mAtlasList[ atlas ].mSize.mBlockWidth;
+  SizeType blockHeight = mAtlasList[ atlas ].mSize.mBlockHeight;
 
   float vertexBlockWidth = static_cast< float >( blockWidth );
   float vertexBlockHeight = static_cast< float >( blockHeight );
 
-  SizeType width = mAtlasList[ atlas ].mWidth;
-  SizeType height = mAtlasList[ atlas ].mHeight;
+  SizeType width = mAtlasList[ atlas ].mSize.mWidth;
+  SizeType height = mAtlasList[ atlas ].mSize.mHeight;
 
   SizeType atlasWidthInBlocks = width / blockWidth;
 
@@ -318,6 +338,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;
@@ -395,7 +419,6 @@ void AtlasManager::CreateMesh( SizeType atlas,
 
   meshData.SetFaceIndices( faces );
   meshData.SetMaterial( mAtlasList[ atlas ].mMaterial );
-  //PrintMeshData( meshData );
 }
 
 void AtlasManager::PrintMeshData( const MeshData& meshData )
@@ -492,9 +515,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,
@@ -502,7 +522,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();
@@ -548,8 +567,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 );
 }
 
@@ -566,9 +584,9 @@ void AtlasManager::UploadImage( const BufferImage& image,
     return;
   }
 
-  SizeType atlasBlockWidth = mAtlasList[ atlas ].mBlockWidth;
-  SizeType atlasBlockHeight = mAtlasList[ atlas ].mBlockHeight;
-  SizeType atlasWidthInBlocks = mAtlasList[ atlas ].mWidth / mAtlasList[ atlas ].mBlockWidth;
+  SizeType atlasBlockWidth = mAtlasList[ atlas ].mSize.mBlockWidth;
+  SizeType atlasBlockHeight = mAtlasList[ atlas ].mSize.mBlockHeight;
+  SizeType atlasWidthInBlocks = mAtlasList[ atlas ].mSize.mWidth / mAtlasList[ atlas ].mSize.mBlockWidth;
 
   SizeType block = desc.mBlocksList[ 0 ];
   SizeType blockX = block % atlasWidthInBlocks;
@@ -576,9 +594,58 @@ void AtlasManager::UploadImage( const BufferImage& image,
   SizeType blockOffsetX = blockX * atlasBlockWidth;
   SizeType blockOffsetY = blockY * atlasBlockHeight;
 
-  if ( !mAtlasList[ atlas ].mAtlas.Upload( image, blockOffsetX, blockOffsetY ) )
+  SizeType width = image.GetWidth();
+  SizeType height = image.GetHeight();
+
+  // 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 image to Atlas Failed!.\n");
+  }
+
+  // If this is the first block then we need to keep the first pixel free for underline texture
+  if ( block )
+  {
+
+    // Blit top strip
+    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mHorizontalStrip,
+                                             blockOffsetX,
+                                             blockOffsetY ) )
+    {
+      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 ].mSize.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");
+    }
+  }
+
+  // Blit right strip
+  if ( blockOffsetX + width + DOUBLE_PIXEL_PADDING <= mAtlasList[ atlas ].mSize.mWidth )
   {
-    DALI_LOG_ERROR("Uploading block to Atlas Failed!.\n");
+    if ( !mAtlasList[ atlas ].mAtlas.Upload( mAtlasList[ atlas ].mVerticalStrip,
+                                             blockOffsetX + width + SINGLE_PIXEL_PADDING,
+                                             blockOffsetY + SINGLE_PIXEL_PADDING ) )
+    {
+      DALI_LOG_ERROR("Uploading right strip to Atlas Failed!.\n");
+    }
   }
 }
 
@@ -592,13 +659,13 @@ void AtlasManager::GenerateMeshData( ImageId id,
   SizeType width = mImageList[ imageId ].mImageWidth;
   SizeType height = mImageList[ imageId ].mImageHeight;
 
-  SizeType widthInBlocks = width / mAtlasList[ atlas ].mBlockWidth;
-  if ( width % mAtlasList[ atlas ].mBlockWidth )
+  SizeType widthInBlocks = width / mAtlasList[ atlas ].mSize.mBlockWidth;
+  if ( width % mAtlasList[ atlas ].mSize.mBlockWidth )
   {
     widthInBlocks++;
   }
-  SizeType heightInBlocks = height / mAtlasList[ atlas ].mBlockHeight;
-  if ( height % mAtlasList[ atlas ].mBlockHeight )
+  SizeType heightInBlocks = height / mAtlasList[ atlas ].mSize.mBlockHeight;
+  if ( height % mAtlasList[ atlas ].mSize.mBlockHeight )
   {
     heightInBlocks++;
   }
@@ -640,7 +707,7 @@ bool AtlasManager::Remove( ImageId id )
     return false;
   }
 
-  if ( 1u == --mImageList[ imageId ].mCount )
+  if ( 2u > --mImageList[ imageId ].mCount )
   {
     // 'Remove the blocks' from this image and add to the atlas' freelist
     removed = true;
@@ -666,24 +733,20 @@ AtlasManager::AtlasId AtlasManager::GetAtlas( ImageId id ) const
   }
 }
 
-void AtlasManager::SetAtlasSize( const Vector2& size,
-                                 const Vector2& blockSize )
+void AtlasManager::SetNewAtlasSize( const Toolkit::AtlasManager::AtlasSize& size )
 {
   mNewAtlasSize = size;
-  mNewBlockSize = blockSize;
+  mNewAtlasSize.mBlockWidth += DOUBLE_PIXEL_PADDING;
+  mNewAtlasSize.mBlockHeight += DOUBLE_PIXEL_PADDING;
 }
 
-Vector2 AtlasManager::GetBlockSize( AtlasId atlas )
+const Toolkit::AtlasManager::AtlasSize& AtlasManager::GetAtlasSize( AtlasId atlas )
 {
-  if ( atlas && atlas <= mAtlasList.size() )
-  {
-  return Vector2( static_cast< float >( mAtlasList[ atlas - 1u ].mBlockWidth ),
-                  static_cast< float >( mAtlasList[ atlas - 1u ].mBlockHeight) );
-  }
-  else
+  if ( atlas && atlas-- <= mAtlasList.size() )
   {
-    return Vector2( 0.0f, 0.0f );
+    return mAtlasList[ atlas ].mSize;
   }
+  return EMPTY_SIZE;
 }
 
 AtlasManager::SizeType AtlasManager::GetFreeBlocks( AtlasId atlas ) const
@@ -691,27 +754,24 @@ AtlasManager::SizeType AtlasManager::GetFreeBlocks( AtlasId atlas ) const
   if ( atlas && atlas <= mAtlasList.size() )
   {
     uint32_t index = atlas - 1u;
-    uint32_t width = mAtlasList[ index ].mWidth;
-    uint32_t height = mAtlasList[ index ].mHeight;
-    uint32_t blockWidth = mAtlasList[ index ].mBlockWidth;
-    uint32_t blockHeight = mAtlasList[ index ].mBlockHeight;
+    uint32_t width = mAtlasList[ index ].mSize.mWidth;
+    uint32_t height = mAtlasList[ index ].mSize.mHeight;
+    uint32_t blockWidth = mAtlasList[ index ].mSize.mBlockWidth;
+    uint32_t blockHeight = mAtlasList[ index ].mSize.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
-    blockCount -= mAtlasList[ index ].mNextFreeBlock - mAtlasList[ index ].mFreeBlocksList.Size();
+    if ( mAtlasList[ index ].mNextFreeBlock )
+    {
+      blockCount -= mAtlasList[ index ].mNextFreeBlock -1u - mAtlasList[ index ].mFreeBlocksList.Size();
+    }
+    else
+    {
+      blockCount = mAtlasList[ index ].mFreeBlocksList.Size();
+    }
     return blockCount;
   }
   else
@@ -736,6 +796,36 @@ 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 )
+  {
+    entry.mSize = mAtlasList[ i ].mSize;
+    entry.mTotalBlocks = ( entry.mSize.mWidth / entry.mSize.mBlockWidth ) * ( entry.mSize.mHeight / entry.mSize.mBlockHeight );
+    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 = entry.mSize.mWidth * entry.mSize.mHeight;
+    if ( entry.mPixelFormat == Pixel::BGRA8888 )
+    {
+      size <<= 2;
+    }
+
+    textureMemoryUsed += size;
+
+  }
+  metrics.mTextureMemoryUsed = textureMemoryUsed;
+}
+
 
 } // namespace Internal