[dali_1.9.3] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch-loader.cpp
index 209840b..6745adf 100644 (file)
@@ -1,5 +1,5 @@
  /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
 
 // EXTERNAL HEADER
 #include <dali/devel-api/adaptor-framework/image-loading.h>
-#include <dali/devel-api/adaptor-framework/pixel-buffer.h>
 #include <dali/devel-api/common/hash.h>
 #include <dali/integration-api/debug.h>
 
@@ -157,7 +156,7 @@ void GetRedOffsetAndMask( Dali::Pixel::Format pixelFormat, int& byteOffset, int&
   }
 }
 
-Uint16Pair ParseRange( unsigned int& index, unsigned int width, unsigned char* pixel, unsigned int pixelStride, int testByte, int testBits, int testValue )
+Uint16Pair ParseRange( unsigned int& index, unsigned int width, unsigned char*& pixel, unsigned int pixelStride, int testByte, int testBits, int testValue )
 {
   unsigned int start = 0xFFFF;
   for( ; index < width; ++index, pixel += pixelStride )
@@ -248,6 +247,35 @@ void ParseBorders( Devel::PixelBuffer& pixelBuffer, NPatchLoader::Data* data  )
   }
 }
 
+void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuffer )
+{
+  if( data->border == Rect< int >( 0, 0, 0, 0 ) )
+  {
+    NPatchBuffer::ParseBorders( pixelBuffer, data );
+
+    // Crop the image
+    pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
+  }
+  else
+  {
+    data->stretchPixelsX.PushBack( Uint16Pair( data->border.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( data->border.right )) ? pixelBuffer.GetWidth() - data->border.right : 0 ) ) );
+    data->stretchPixelsY.PushBack( Uint16Pair( data->border.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( data->border.bottom )) ? pixelBuffer.GetHeight() - data->border.bottom : 0 ) ) );
+  }
+
+  data->croppedWidth = pixelBuffer.GetWidth();
+  data->croppedHeight = pixelBuffer.GetHeight();
+
+  PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
+
+  Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
+  texture.Upload( pixels );
+
+  data->textureSet = TextureSet::New();
+  data->textureSet.SetTexture( 0u, texture );
+
+  data->loadCompleted = true;
+}
+
 } // namespace NPatchBuffer
 
 NPatchLoader::NPatchLoader()
@@ -258,12 +286,13 @@ NPatchLoader::~NPatchLoader()
 {
 }
 
-std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad )
+std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading )
 {
   std::size_t hash = CalculateHash( url );
   OwnerContainer< Data* >::SizeType index = UNINITIALIZED_ID;
   const OwnerContainer< Data* >::SizeType count = mCache.Count();
   int cachedIndex = -1;
+  Data* data;
 
   for( ; index < count; ++index )
   {
@@ -275,92 +304,85 @@ std::size_t NPatchLoader::Load( const std::string& url, const Rect< int >& borde
         // Use cached data
         if( mCache[ index ]->border == border )
         {
-          return index+1u; // valid indices are from 1 onwards
+          if( mCache[ index ]->loadCompleted )
+          {
+            return index + 1u; // valid indices are from 1 onwards
+          }
+          data = mCache[ index ];
+          cachedIndex = index + 1u; // valid indices are from 1 onwards
+          break;
         }
         else
         {
-          cachedIndex = index;
-        }
-      }
-    }
-  }
+          if( mCache[ index ]->loadCompleted )
+          {
+            // Same url but border is different - use the existing texture
+            Data* data = new Data();
+            data->hash = hash;
+            data->url = url;
+            data->croppedWidth = mCache[ index ]->croppedWidth;
+            data->croppedHeight = mCache[ index ]->croppedHeight;
 
-  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[ index ]->textureSet;
 
-    data->textureSet = mCache[ cachedIndex ]->textureSet;
+            StretchRanges stretchRangesX;
+            stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
 
-    StretchRanges stretchRangesX;
-    stretchRangesX.PushBack( Uint16Pair( border.left, ( (data->croppedWidth >= static_cast< unsigned int >( border.right )) ? data->croppedWidth - border.right : 0 ) ) );
+            StretchRanges stretchRangesY;
+            stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) );
 
-    StretchRanges stretchRangesY;
-    stretchRangesY.PushBack( Uint16Pair( border.top, ( (data->croppedHeight >= static_cast< unsigned int >( border.bottom )) ? data->croppedHeight - border.bottom : 0 ) ) );
+            data->stretchPixelsX = stretchRangesX;
+            data->stretchPixelsY = stretchRangesY;
+            data->border = border;
 
-    data->stretchPixelsX = stretchRangesX;
-    data->stretchPixelsY = stretchRangesY;
-    data->border = border;
+            data->loadCompleted = mCache[ index ]->loadCompleted;
 
-    mCache.PushBack( data );
+            mCache.PushBack( data );
 
-    return mCache.Count(); // valid ids start from 1u
+            return mCache.Count(); // valid ids start from 1u
+          }
+        }
+      }
+    }
   }
 
-  // got to the end so no match, decode N patch and append new item to cache
-  Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromFile( url, ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, true );
-  if( pixelBuffer )
+  if( cachedIndex == -1 )
   {
-    Data* data = new Data();
+    data = new Data();
+    data->loadCompleted = false;
     data->hash = hash;
     data->url = url;
+    data->border = border;
 
-    if( border == Rect< int >( 0, 0, 0, 0 ) )
-    {
-      NPatchBuffer::ParseBorders( pixelBuffer, data );
-
-      data->border = Rect< int >( 0, 0, 0, 0 );
-
-      // Crop the image
-      pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
-    }
-    else
-    {
-      data->stretchPixelsX.PushBack( Uint16Pair( border.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( border.right )) ? pixelBuffer.GetWidth() - border.right : 0 ) ) );
-      data->stretchPixelsY.PushBack( Uint16Pair( border.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( border.bottom )) ? pixelBuffer.GetHeight() - border.bottom : 0 ) ) );
-      data->border = border;
-    }
-
-    data->croppedWidth = pixelBuffer.GetWidth();
-    data->croppedHeight = pixelBuffer.GetHeight();
+    mCache.PushBack( data );
 
-    if( preMultiplyOnLoad && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
-    {
-      pixelBuffer.MultiplyColorByAlpha();
-    }
-    else
-    {
-      preMultiplyOnLoad = false;
-    }
+    cachedIndex = mCache.Count();
+  }
 
-    PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
+  auto preMultiplyOnLoading = preMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
+                                                : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
+  Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer( url, Dali::ImageDimensions(), FittingMode::DEFAULT,
+                                                                   SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
+                                                                   textureObserver, true, preMultiplyOnLoading );
 
-    Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
-    texture.Upload( pixels );
+  if( pixelBuffer )
+  {
+    NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
+    preMultiplyOnLoad = ( preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ) ? true : false;
+  }
 
-    data->textureSet = TextureSet::New();
-    data->textureSet.SetTexture( 0u, texture );
+  return cachedIndex;
+}
 
-    mCache.PushBack( data );
+void NPatchLoader::SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer )
+{
+  Data* data;
+  data = mCache[ id - 1u ];
 
-    return mCache.Count(); // valid ids start from 1u
+  if( !data->loadCompleted )
+  {
+    NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
   }
-
-  return 0u;
 }
 
 bool NPatchLoader::GetNPatchData( std::size_t id, const Data*& data )