Merge "Make do not caching pixelBuffer in texture-manager." into devel/master
authorDavid Steele <david.steele@samsung.com>
Wed, 25 Nov 2020 10:10:12 +0000 (10:10 +0000)
committerGerrit Code Review <gerrit@review>
Wed, 25 Nov 2020 10:10:12 +0000 (10:10 +0000)
automated-tests/src/dali-toolkit/utc-Dali-VisualFactory.cpp
dali-toolkit/internal/visuals/npatch-loader.cpp
dali-toolkit/internal/visuals/npatch-loader.h
dali-toolkit/internal/visuals/npatch/npatch-visual.cpp
dali-toolkit/internal/visuals/texture-manager-impl.cpp
dali-toolkit/internal/visuals/texture-manager-impl.h

index faae3fe..db708c1 100644 (file)
@@ -984,7 +984,7 @@ int UtcDaliVisualFactoryGetNPatchVisual7(void)
   END_TEST;
 }
 
-int UtcDaliNPatchVisualAuxiliaryImage(void)
+int UtcDaliNPatchVisualAuxiliaryImage01(void)
 {
   ToolkitTestApplication application;
   tet_infoline( "NPatchVisual with aux image" );
@@ -1034,6 +1034,38 @@ int UtcDaliNPatchVisualAuxiliaryImage(void)
   END_TEST;
 }
 
+int UtcDaliNPatchVisualAuxiliaryImage02(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline( "Multiple NPatchVisual with aux image coincidentally" );
+
+  const Property::Value NPATCH_TEST{
+    {ImageVisual::Property::URL, TEST_9_PATCH_FILE_NAME},
+    {DevelImageVisual::Property::AUXILIARY_IMAGE, TEST_AUX_IMAGE}};
+
+  ImageView imageView1 = ImageView::New();
+  imageView1[ImageView::Property::IMAGE] = NPATCH_TEST;
+  application.GetScene().Add( imageView1 );
+
+  ImageView imageView2 = ImageView::New();
+  imageView2[ImageView::Property::IMAGE] = NPATCH_TEST;
+  application.GetScene().Add( imageView2 );
+
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
+
+  application.SendNotification();
+  application.Render();
+
+  Renderer renderer1 = imageView1.GetRendererAt( 0 );
+  auto textureSet1 = renderer1.GetTextures();
+
+  Renderer renderer2 = imageView2.GetRendererAt( 0 );
+  auto textureSet2 = renderer2.GetTextures();
+  DALI_TEST_EQUALS( textureSet1 == textureSet2, true, TEST_LOCATION );
+
+  END_TEST;
+}
+
 
 int UtcDaliVisualFactoryGetNPatchVisualN1(void)
 {
index 2e216f3..2550001 100644 (file)
@@ -110,9 +110,9 @@ std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObs
           {
             return index + 1u; // valid indices are from 1 onwards
           }
+          mCache[ index ]->observerList.PushBack( textureObserver );
           data = mCache[ index ];
-          cachedIndex = index + 1u; // valid indices are from 1 onwards
-          break;
+          return index + 1u; // valid indices are from 1 onwards
         }
         else
         {
@@ -176,14 +176,25 @@ std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObs
   return cachedIndex;
 }
 
-void NPatchLoader::SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer )
+void NPatchLoader::SetNPatchData( bool loadSuccess, std::size_t id, Devel::PixelBuffer& pixelBuffer, const Internal::VisualUrl& url, bool preMultiplied )
 {
   Data* data;
   data = mCache[ id - 1u ];
 
-  if( !data->loadCompleted )
+  // To prevent recursion.
+  // data->loadCompleted will be set true in the NPatchBuffer::SetLoadedNPatchData when the first observer called this method.
+  if( data->loadCompleted )
   {
-    NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
+    return;
+  }
+
+  NPatchBuffer::SetLoadedNPatchData( data, pixelBuffer );
+
+  while( data->observerList.Count() )
+  {
+    TextureUploadObserver* observer = data->observerList[0];
+    observer->LoadComplete( loadSuccess, Devel::PixelBuffer(), url, preMultiplied );
+    data->observerList.Erase( data->observerList.begin() );
   }
 }
 
index 2c0e852..a3aa464 100644 (file)
@@ -69,6 +69,9 @@ public:
 
     ~Data();
 
+    using ObserverListType = Dali::Vector< TextureUploadObserver* >;
+
+    ObserverListType observerList;                 ///< Container used to store all observer clients of this Texture
     std::string url;                               ///< Url of the N-Patch
     TextureSet textureSet;                         ///< Texture containing the cropped image
     NPatchUtility::StretchRanges stretchPixelsX;   ///< X stretch pixels
@@ -110,10 +113,13 @@ public:
   /**
    * @brief Set loaded PixelBuffer and its information
    *
+   * @param [in] loadSuccess True if the texture load was successful (i.e. the resource is available). If false, then the resource failed to load.
    * @param [in] id cache data id
    * @param [in] pixelBuffer of loaded image
+   * @param [in] url           The url address of the loaded image.
+   * @param [in] preMultiplied True if the image had pre-multiplied alpha applied
    */
-  void SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer );
+  void SetNPatchData( bool loadSuccess, std::size_t id, Devel::PixelBuffer& pixelBuffer, const Internal::VisualUrl& url, bool preMultiplied );
 
   /**
    * @brief Retrieve N patch data matching to an id
index bd374aa..5c03ac2 100644 (file)
@@ -873,11 +873,17 @@ void NPatchVisual::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffe
   }
   else
   {
+    Devel::PixelBuffer loadedPixelBuffer;
     if( loadSuccess )
     {
-      mLoader.SetNPatchData( mId, pixelBuffer );
+      loadedPixelBuffer = pixelBuffer;
       EnablePreMultipliedAlpha( preMultiplied );
     }
+    else
+    {
+      loadedPixelBuffer = LoadImageFromFile( mFactoryCache.GetTextureManager().GetBrokenImageUrl() );
+    }
+    mLoader.SetNPatchData( loadSuccess, mId, loadedPixelBuffer, url, preMultiplied );
 
     if( mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid() )
     {
index 00b8eca..2d82b41 100644 (file)
@@ -38,6 +38,7 @@
 namespace
 {
 
+constexpr auto INITIAL_CACHE_NUMBER = size_t{0u};
 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
 
@@ -454,13 +455,18 @@ TextureManager::TextureId TextureManager::RequestLoadInternal(
 {
   // First check if the requested Texture is cached.
   bool isAnimatedImage = ( animatedImageLoading ) ? true : false;
-  const TextureHash textureHash = GenerateHash( url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
-                                                maskTextureId, storageType, isAnimatedImage, frameIndex );
+
+  TextureHash textureHash = INITIAL_CACHE_NUMBER;
+  int cacheIndex = INVALID_CACHE_INDEX;
+  if(storageType != StorageType::RETURN_PIXEL_BUFFER)
+  {
+    textureHash = GenerateHash(url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, isAnimatedImage, frameIndex);
+
+    // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
+    cacheIndex = FindCachedTexture(textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, preMultiplyOnLoad, isAnimatedImage, frameIndex);
+  }
 
   TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
-  // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
-  int cacheIndex = FindCachedTexture( textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
-                                      maskTextureId, preMultiplyOnLoad, storageType, isAnimatedImage, frameIndex );
 
   // Check if the requested Texture exists in the cache.
   if( cacheIndex != INVALID_CACHE_INDEX )
@@ -1170,7 +1176,6 @@ TextureManager::TextureHash TextureManager::GenerateHash(
   const Dali::SamplingMode::Type samplingMode,
   const UseAtlas                 useAtlas,
   TextureId                      maskTextureId,
-  StorageType                    storageType,
   bool                           isAnimationImage,
   uint32_t                       frameIndex )
 {
@@ -1193,8 +1198,8 @@ TextureManager::TextureHash TextureManager::GenerateHash(
     *hashTargetPtr++ = ( size.GetHeight() >> 8u ) & 0xff;
 
     // Bit-pack the FittingMode, SamplingMode and atlasing.
-    // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit, storageType=2bits
-    *hashTargetPtr   = ( fittingMode << 6u ) | ( samplingMode << 3 ) | ( useAtlas << 2 ) | static_cast<unsigned int>(storageType);
+    // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit
+    *hashTargetPtr   = ( fittingMode << 4u ) | ( samplingMode << 1 ) | useAtlas;
   }
   else
   {
@@ -1257,7 +1262,6 @@ int TextureManager::FindCachedTexture(
   const bool                        useAtlas,
   TextureId                         maskTextureId,
   TextureManager::MultiplyOnLoad    preMultiplyOnLoad,
-  StorageType                       storageType,
   bool                              isAnimatedImage,
   uint32_t                          frameIndex )
 {
@@ -1280,7 +1284,6 @@ int TextureManager::FindCachedTexture(
           ( ( size.GetWidth() == 0 && size.GetHeight() == 0 ) ||
             ( fittingMode == textureInfo.fittingMode &&
               samplingMode == textureInfo.samplingMode ) ) &&
-          ( storageType == textureInfo.storageType ) &&
           ( isAnimatedImage == ( ( textureInfo.animatedImageLoading ) ? true : false ) ) &&
           ( frameIndex == textureInfo.frameIndex ) )
       {
@@ -1399,6 +1402,11 @@ void TextureManager::SetBrokenImageUrl(const std::string& brokenImageUrl)
   mBrokenImageUrl = brokenImageUrl;
 }
 
+const std::string TextureManager::GetBrokenImageUrl()
+{
+  return mBrokenImageUrl;
+}
+
 Geometry TextureManager::GetRenderGeometry(TextureId textureId, uint32_t& frontElements, uint32_t& backElements )
 {
   return RenderingAddOn::Get().IsValid() ?
index 89afd47..a3da3fc 100644 (file)
@@ -428,6 +428,12 @@ public:
   void SetBrokenImageUrl(const std::string& brokenImageUrl);
 
   /**
+   * @brief Get an image to be used when a visual has failed to correctly render
+   * @return Returns The broken image url.
+   */
+  const std::string GetBrokenImageUrl();
+
+  /**
    * @brief Returns the geometry associated with texture.
    * @param[in] textureId Id of the texture
    * @param[out] frontElements number of front elements
@@ -753,7 +759,7 @@ private:
   TextureHash GenerateHash( const std::string& url, const ImageDimensions size,
                             const FittingMode::Type fittingMode,
                             const Dali::SamplingMode::Type samplingMode, const UseAtlas useAtlas,
-                            TextureId maskTextureId, StorageType storageType, bool isAnimatedImage, uint32_t frameIndex );
+                            TextureId maskTextureId, bool isAnimatedImage, uint32_t frameIndex );
 
   /**
    * @brief Looks up a cached texture by its hash.
@@ -766,7 +772,6 @@ private:
    * @param[in] useAtlas          True if atlased
    * @param[in] maskTextureId     Optional texture ID to use to mask this image
    * @param[in] preMultiplyOnLoad if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
-   * @param[in] storageType       Whether the pixel data is stored in the cache, returned with PixelBuffer or uploaded to the GPU
    * @param[in] isAnimatedImage   The boolean value to know whether the request is for animated image or not
    * @param[in] frameIndex        The frame index of a frame to be loaded frame
    * @return                      A TextureId of a cached Texture if found. Or INVALID_TEXTURE_ID if not found.
@@ -780,7 +785,6 @@ private:
     const bool useAtlas,
     TextureId maskTextureId,
     MultiplyOnLoad preMultiplyOnLoad,
-    StorageType storageType,
     bool isAnimatedImage,
     uint32_t frameIndex );