Fix npatch visual issue 12/123612/1
authorHeeyong Song <heeyong.song@samsung.com>
Thu, 6 Apr 2017 06:55:03 +0000 (15:55 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Thu, 6 Apr 2017 08:25:15 +0000 (17:25 +0900)
When the same image with different borders is used, the cached data should not be used.

Change-Id: Ib986594bd74f82cf18afad3b14b28dca5dcd9254

automated-tests/src/dali-toolkit/utc-Dali-VisualFactory.cpp
dali-toolkit/internal/visuals/npatch-loader.cpp
dali-toolkit/internal/visuals/npatch-loader.h

index acf3192..b8e40b8 100644 (file)
@@ -695,6 +695,25 @@ int UtcDaliVisualFactoryGetNPatchVisual2(void)
     DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
   }
 
+  propertyMap.Clear();
+  propertyMap.Insert( Visual::Property::TYPE, DevelVisual::N_PATCH );
+  propertyMap.Insert( ImageVisual::Property::URL, gImage_34_RGBA );
+  propertyMap.Insert( DevelImageVisual::Property::BORDER, Rect< int >( 1, 1, 1, 1 ) );
+  {
+    tet_infoline( "whole grid" );
+    Visual::Base visual = factory.CreateVisual( propertyMap );
+    DALI_TEST_CHECK( visual );
+
+    TestGlAbstraction& gl = application.GetGlAbstraction();
+    TraceCallStack& textureTrace = gl.GetTextureTrace();
+    textureTrace.Enable(true);
+
+    DummyControl actor = DummyControl::New();
+    TestVisualRender( application, actor, visual, 1u );
+
+    DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
+  }
+
   END_TEST;
 }
 
index 5da7bd1..f23f1ca 100644 (file)
@@ -45,6 +45,8 @@ std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& borde
   std::size_t hash = CalculateHash( url );
   OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
   const OwnerContainer< Data* >::SizeType count = mCache.Count();
+  int cachedIndex = -1;
+
   for( ; index < count; ++index )
   {
     if( mCache[ index ]->hash == hash )
@@ -52,10 +54,45 @@ std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& borde
       // hash match, check url as well in case of hash collision
       if( mCache[ index ]->url == url )
       {
-        return index+1u; // valid indices are from 1 onwards
+        // Use cached data
+        if( mCache[ index ]->border == border )
+        {
+          return index+1u; // valid indices are from 1 onwards
+        }
+        else
+        {
+          cachedIndex = index;
+        }
       }
     }
   }
+
+  if( cachedIndex != -1 )
+  {
+    // Same url but border is different - use the existing texture
+    Data* data = new Data();
+    data->hash = hash;
+    data->url = url;
+    data->croppedWidth = mCache[ cachedIndex ]->croppedWidth;
+    data->croppedHeight = mCache[ cachedIndex ]->croppedHeight;
+
+    data->textureSet = mCache[ cachedIndex ]->textureSet;
+
+    NinePatchImage::StretchRanges stretchRangesX;
+    stretchRangesX.PushBack( Uint16Pair( border.left, data->croppedWidth - border.right ) );
+
+    NinePatchImage::StretchRanges stretchRangesY;
+    stretchRangesY.PushBack( Uint16Pair( border.top, data->croppedHeight - border.bottom ) );
+
+    data->stretchPixelsX = stretchRangesX;
+    data->stretchPixelsY = stretchRangesY;
+    data->border = border;
+
+    mCache.PushBack( data );
+
+    return mCache.Count(); // valid ids start from 1u
+  }
+
   // got to the end so no match, decode N patch and append new item to cache
   if( border == Rect< int >( 0, 0, 0, 0 ) )
   {
@@ -74,6 +111,7 @@ std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& borde
         data->croppedHeight = croppedImage.GetHeight();
         data->stretchPixelsX = ninePatch.GetStretchPixelsX();
         data->stretchPixelsY = ninePatch.GetStretchPixelsY();
+        data->border = Rect< int >( 0, 0, 0, 0 );
         mCache.PushBack( data );
 
         return mCache.Count(); // valid ids start from 1u
@@ -106,6 +144,7 @@ std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& borde
 
       data->stretchPixelsX = stretchRangesX;
       data->stretchPixelsY = stretchRangesY;
+      data->border = border;
 
       mCache.PushBack( data );
 
index 61ec60d..d1df302 100644 (file)
@@ -62,6 +62,7 @@ public:
     std::size_t hash;                             ///< Hash code for the Url
     uint32_t croppedWidth;                        ///< Width of the cropped middle part of N-patch
     uint32_t croppedHeight;                       ///< Height of the cropped middle part of N-patch
+    Rect< int > border;                           ///< The size of the border
   };
 
 public: