Remove useless iteration when debug mode
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch-loader.cpp
index a7734da..4c6471e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
@@ -52,98 +52,61 @@ NPatchData::NPatchDataId NPatchLoader::GenerateUniqueNPatchDataId()
   return mCurrentNPatchDataId++;
 }
 
-std::size_t NPatchLoader::Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect<int>& border, bool& preMultiplyOnLoad, bool synchronousLoading)
+std::size_t NPatchLoader::Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad, bool synchronousLoading)
 {
-  std::size_t                                 hash  = CalculateHash(url);
-  OwnerContainer<NPatchData*>::SizeType       index = UNINITIALIZED_ID;
-  const OwnerContainer<NPatchData*>::SizeType count = mCache.Count();
+  NPatchData* data = GetNPatchData(url, border, preMultiplyOnLoad);
 
-  for(; index < count; ++index)
+  DALI_ASSERT_ALWAYS(data && "NPatchData creation failed!");
+
+  if(data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
   {
-    if(mCache[index]->GetHash() == hash)
+    if(!synchronousLoading)
     {
-      // hash match, check url as well in case of hash collision
-      if(mCache[index]->GetUrl() == url)
+      // NotifyObserver already done, so
+      // data will not iterate observer list.
+      // We need to call LoadComplete directly.
+      data->NotifyObserver(textureObserver, true);
+    }
+  }
+  else // if NOT_STARTED or LOADING or LOAD_FAILED, try to reload.
+  {
+    if(!synchronousLoading)
+    {
+      data->AddObserver(textureObserver);
+      // If still LOADING and async, don't need to request reload. Fast return.
+      if(data->GetLoadingState() == NPatchData::LoadingState::LOADING)
       {
-        // Use cached data
-        if(mCache[index]->GetBorder() == border)
-        {
-          if(mCache[index]->GetLoadingState() == NPatchData::LoadingState::LOADING)
-          {
-            mCache[index]->AddObserver(textureObserver);
-          }
-          return mCache[index]->GetId(); // valid indices are from 1 onwards
-        }
-        else
-        {
-          if(mCache[index]->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
-          {
-            // Same url but border is different - use the existing texture
-            NPatchData* newData = new NPatchData();
-            newData->SetId(GenerateUniqueNPatchDataId());
-            newData->SetHash(hash);
-            newData->SetUrl(url);
-            newData->SetCroppedWidth(mCache[index]->GetCroppedWidth());
-            newData->SetCroppedHeight(mCache[index]->GetCroppedHeight());
-
-            newData->SetTextures(mCache[index]->GetTextures());
-
-            NPatchUtility::StretchRanges stretchRangesX;
-            stretchRangesX.PushBack(Uint16Pair(border.left, ((newData->GetCroppedWidth() >= static_cast<unsigned int>(border.right)) ? newData->GetCroppedHeight() - border.right : 0)));
-
-            NPatchUtility::StretchRanges stretchRangesY;
-            stretchRangesY.PushBack(Uint16Pair(border.top, ((newData->GetCroppedWidth() >= static_cast<unsigned int>(border.bottom)) ? newData->GetCroppedHeight() - border.bottom : 0)));
-
-            newData->SetStretchPixelsX(stretchRangesX);
-            newData->SetStretchPixelsY(stretchRangesY);
-            newData->SetBorder(border);
+        return data->GetId();
+      }
+    }
 
-            newData->SetPreMultiplyOnLoad(mCache[index]->IsPreMultiplied());
+    data->SetLoadingState(NPatchData::LoadingState::LOADING);
 
-            newData->SetLoadingState(NPatchData::LoadingState::LOAD_COMPLETE);
-            newData->AddObserver(textureObserver);
+    auto preMultiplyOnLoading = preMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
+                                                  : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
 
-            mCache.PushBack(newData);
+    Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(url, Dali::ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, synchronousLoading, data, true, preMultiplyOnLoading);
 
-            return newData->GetId(); // valid ids start from 1u
-          }
-        }
-      }
+    if(pixelBuffer)
+    {
+      preMultiplyOnLoad = (preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD) ? true : false;
+      data->SetLoadedNPatchData(pixelBuffer, preMultiplyOnLoad);
+    }
+    else if(synchronousLoading)
+    {
+      data->SetLoadingState(NPatchData::LoadingState::LOAD_FAILED);
     }
   }
-
-  // If this is new image loading, make new cache data
-  NPatchData* data;
-  data = new NPatchData();
-  data->SetId(GenerateUniqueNPatchDataId());
-  data->SetHash(hash);
-  data->SetUrl(url);
-  data->SetBorder(border);
-  data->SetPreMultiplyOnLoad(preMultiplyOnLoad);
-  data->AddObserver(textureObserver);
-  mCache.PushBack(data);
-
-  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, data, true, preMultiplyOnLoading);
-
-  if(pixelBuffer)
-  {
-    preMultiplyOnLoad = (preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD) ? true : false;
-    data->SetLoadedNPatchData(pixelBuffer, preMultiplyOnLoad);
-  }
-
   return data->GetId();
 }
 
 int32_t NPatchLoader::GetCacheIndexFromId(const NPatchData::NPatchDataId id)
 {
-  const unsigned int size = mCache.Count();
+  const unsigned int size = mCache.size();
 
   for(unsigned int i = 0; i < size; ++i)
   {
-    if(mCache[i]->GetId() == id)
+    if(mCache[i].mData->GetId() == id)
     {
       return i;
     }
@@ -157,7 +120,7 @@ bool NPatchLoader::GetNPatchData(const NPatchData::NPatchDataId id, const NPatch
   int32_t cacheIndex = GetCacheIndexFromId(id);
   if(cacheIndex != INVALID_CACHE_INDEX)
   {
-    data = mCache[cacheIndex];
+    data = mCache[cacheIndex].mData;
     return true;
   }
   data = nullptr;
@@ -172,17 +135,119 @@ void NPatchLoader::Remove(std::size_t id, TextureUploadObserver* textureObserver
     return;
   }
 
-  NPatchData* data;
-  data = mCache[cacheIndex];
+  NPatchInfo& info(mCache[cacheIndex]);
 
-  data->RemoveObserver(textureObserver);
+  info.mData->RemoveObserver(textureObserver);
 
-  if(data->GetObserverCount() == 0)
+  if(--info.mReferenceCount <= 0)
   {
-    mCache.Erase(mCache.Begin() + cacheIndex);
+    mCache.erase(mCache.begin() + cacheIndex);
   }
 }
 
+NPatchData* NPatchLoader::GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad)
+{
+  std::size_t                              hash  = CalculateHash(url.GetUrl());
+  std::vector<NPatchInfo>::size_type       index = UNINITIALIZED_ID;
+  const std::vector<NPatchInfo>::size_type count = mCache.size();
+
+  NPatchInfo* infoPtr = nullptr;
+
+  for(; index < count; ++index)
+  {
+    if(mCache[index].mData->GetHash() == hash)
+    {
+      // hash match, check url as well in case of hash collision
+      if(mCache[index].mData->GetUrl().GetUrl() == url.GetUrl())
+      {
+        // Use cached data. Need to fast-out return.
+        if(mCache[index].mData->GetBorder() == border)
+        {
+          mCache[index].mReferenceCount++;
+          return mCache[index].mData;
+        }
+        else
+        {
+          if(mCache[index].mData->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
+          {
+            // If we only found LOAD_FAILED case, replace current data. We can reuse texture
+            if(infoPtr == nullptr || infoPtr->mData->GetLoadingState() != NPatchData::LoadingState::LOAD_COMPLETE)
+            {
+              infoPtr = &mCache[index];
+            }
+          }
+          // Still loading pixel buffer. We cannot reuse cached texture yet. Skip checking
+          else if(mCache[index].mData->GetLoadingState() == NPatchData::LoadingState::LOADING)
+          {
+            continue;
+          }
+          // if LOAD_FAILED, reuse this cached NPatchData, and try to load again.
+          else
+          {
+            if(infoPtr == nullptr)
+            {
+              infoPtr = &mCache[index];
+            }
+          }
+        }
+      }
+    }
+  }
+
+  // If this is new image loading, make new cache data
+  if(infoPtr == nullptr)
+  {
+    NPatchInfo info(new NPatchData());
+    info.mData->SetId(GenerateUniqueNPatchDataId());
+    info.mData->SetHash(hash);
+    info.mData->SetUrl(url);
+    info.mData->SetBorder(border);
+    info.mData->SetPreMultiplyOnLoad(preMultiplyOnLoad);
+
+    mCache.emplace_back(std::move(info));
+    infoPtr = &mCache.back();
+  }
+  // Else if LOAD_COMPLETE, Same url but border is different - use the existing texture
+  else if(infoPtr->mData->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
+  {
+    NPatchInfo info(new NPatchData());
+
+    info.mData->SetId(GenerateUniqueNPatchDataId());
+    info.mData->SetHash(hash);
+    info.mData->SetUrl(url);
+    info.mData->SetCroppedWidth(infoPtr->mData->GetCroppedWidth());
+    info.mData->SetCroppedHeight(infoPtr->mData->GetCroppedHeight());
+
+    info.mData->SetTextures(infoPtr->mData->GetTextures());
+
+    NPatchUtility::StretchRanges stretchRangesX;
+    stretchRangesX.PushBack(Uint16Pair(border.left, ((info.mData->GetCroppedWidth() >= static_cast<unsigned int>(border.right)) ? info.mData->GetCroppedHeight() - border.right : 0)));
+
+    NPatchUtility::StretchRanges stretchRangesY;
+    stretchRangesY.PushBack(Uint16Pair(border.top, ((info.mData->GetCroppedWidth() >= static_cast<unsigned int>(border.bottom)) ? info.mData->GetCroppedHeight() - border.bottom : 0)));
+
+    info.mData->SetStretchPixelsX(stretchRangesX);
+    info.mData->SetStretchPixelsY(stretchRangesY);
+    info.mData->SetBorder(border);
+
+    info.mData->SetPreMultiplyOnLoad(infoPtr->mData->IsPreMultiplied());
+
+    info.mData->SetLoadingState(NPatchData::LoadingState::LOAD_COMPLETE);
+
+    mCache.emplace_back(std::move(info));
+    infoPtr = &mCache.back();
+  }
+  // Else, LOAD_FAILED. just increase reference so we can reuse it.
+  else
+  {
+    infoPtr->mReferenceCount++;
+  }
+
+  DALI_ASSERT_ALWAYS(infoPtr && "NPatchInfo creation failed!");
+
+  return infoPtr->mData;
+}
+
 } // namespace Internal
 
 } // namespace Toolkit