Merge "Remove cubemap cache temperary + Fix bug when Uploaded PixelData resue after...
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / common / image-resource-loader.cpp
index 99b7ed5..bc1a21d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
 #include <dali-scene3d/internal/common/image-resource-loader.h>
 
 // EXTERNAL INCLUDES
-#include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
+#include <dali/devel-api/adaptor-framework/image-loading.h>
 #include <dali/devel-api/adaptor-framework/lifecycle-controller.h>
+#include <dali/devel-api/adaptor-framework/pixel-buffer.h>
 #include <dali/devel-api/common/hash.h>
 #include <dali/devel-api/common/map-wrapper.h>
 #include <dali/devel-api/threading/mutex.h>
 #include <dali/integration-api/adaptor-framework/adaptor.h>
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/pixel-data-integ.h>
 #include <dali/public-api/adaptor-framework/timer.h>
 #include <dali/public-api/common/vector-wrapper.h>
 #include <dali/public-api/object/base-object.h>
@@ -34,6 +36,7 @@
 #include <functional> ///< for std::function
 #include <memory>     ///< for std::shared_ptr
 #include <mutex>
+#include <sstream>
 #include <string>
 #include <utility> ///< for std::pair
 
@@ -48,6 +51,35 @@ constexpr uint32_t GC_PERIOD_MILLISECONDS                     = 1000u;
 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_IMAGE_RESOURCE_LOADER");
 #endif
 
+bool IsDefaultPixelData(const Dali::PixelData& pixelData)
+{
+  if(pixelData == Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataWhiteRGB() ||
+     pixelData == Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataWhiteRGBA() ||
+     pixelData == Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataZAxisRGB() ||
+     pixelData == Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataZAxisAndAlphaRGBA())
+  {
+    return true;
+  }
+  return false;
+}
+
+bool SupportPixelDataCache(const Dali::PixelData& pixelData)
+{
+  // Check given pixelData support to release data after upload.
+  // This is cause we need to reduce CPU memory usage.
+  if(Dali::Integration::IsPixelDataReleaseAfterUpload(pixelData))
+  {
+    return true;
+  }
+
+  // Check given pixelData is default pixelData.
+  if(IsDefaultPixelData(pixelData))
+  {
+    return true;
+  }
+  return false;
+}
+
 struct ImageInformation
 {
   ImageInformation(const std::string           url,
@@ -115,26 +147,19 @@ std::size_t GenerateHash(const Dali::PixelData& pixelData, bool mipmapRequired)
   return reinterpret_cast<std::size_t>(static_cast<void*>(pixelData.GetObjectPtr())) ^ (static_cast<std::size_t>(mipmapRequired) << (sizeof(std::size_t) * 4));
 }
 
-std::size_t GenerateHash(const std::vector<std::vector<Dali::PixelData>>& pixelDataList, bool mipmapRequired)
-{
-  std::size_t result = 0x12345678u + pixelDataList.size();
-  for(const auto& mipmapPixelDataList : pixelDataList)
-  {
-    result += (result << 5) + mipmapPixelDataList.size();
-    for(const auto& pixelData : mipmapPixelDataList)
-    {
-      result += (result << 5) + GenerateHash(pixelData, false);
-    }
-  }
-
-  return result ^ (static_cast<std::size_t>(mipmapRequired) << (sizeof(std::size_t) * 4));
-}
-
 // Item Creation functor list
 
-Dali::PixelData CreatePixelDataFromImageInfo(const ImageInformation& info, bool /* Not used */)
+Dali::PixelData CreatePixelDataFromImageInfo(const ImageInformation& info, bool releasePixelData)
 {
-  return Dali::Toolkit::SyncImageLoader::Load(info.mUrl, info.mDimensions, info.mFittingMode, info.mSamplingMode, info.mOrientationCorrection);
+  Dali::PixelData pixelData;
+
+  // Load the image synchronously (block the thread here).
+  Dali::Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromFile(info.mUrl, info.mDimensions, info.mFittingMode, info.mSamplingMode, info.mOrientationCorrection);
+  if(pixelBuffer)
+  {
+    pixelData = Dali::Devel::PixelBuffer::Convert(pixelBuffer, releasePixelData);
+  }
+  return pixelData;
 }
 
 Dali::Texture CreateTextureFromPixelData(const Dali::PixelData& pixelData, bool mipmapRequired)
@@ -152,27 +177,17 @@ Dali::Texture CreateTextureFromPixelData(const Dali::PixelData& pixelData, bool
   return texture;
 }
 
-Dali::Texture CreateCubeTextureFromPixelDataList(const std::vector<std::vector<Dali::PixelData>>& pixelDataList, bool mipmapRequired)
+// Check function whether we can collect given data as garbage, or not.
+bool PixelDataCacheCollectable(const ImageInformation& info, const Dali::PixelData& pixelData)
 {
-  Dali::Texture texture;
-  if(!pixelDataList.empty() && !pixelDataList[0].empty())
-  {
-    texture = Dali::Texture::New(Dali::TextureType::TEXTURE_CUBE, pixelDataList[0][0].GetPixelFormat(), pixelDataList[0][0].GetWidth(), pixelDataList[0][0].GetHeight());
-    for(size_t iSide = 0u, iEndSize = pixelDataList.size(); iSide < iEndSize; ++iSide)
-    {
-      auto& side = pixelDataList[iSide];
-      for(size_t iMipLevel = 0u, iEndMipLevel = pixelDataList[0].size(); iMipLevel < iEndMipLevel; ++iMipLevel)
-      {
-        texture.Upload(side[iMipLevel], Dali::CubeMapLayer::POSITIVE_X + iSide, iMipLevel, 0u, 0u, side[iMipLevel].GetWidth(), side[iMipLevel].GetHeight());
-      }
-    }
-    if(mipmapRequired)
-    {
-      texture.GenerateMipmaps();
-    }
-  }
+  return pixelData.GetBaseObject().ReferenceCount() <= 1;
+}
 
-  return texture;
+bool TextureCacheCollectable(const Dali::PixelData& pixelData, const Dali::Texture& texture)
+{
+  return !IsDefaultPixelData(pixelData) &&                  ///< If key is not default pixelData
+         pixelData.GetBaseObject().ReferenceCount() <= 2 && ///< And it have reference count as 2 (1 is for the key of this container, and other is PixelData cache.)
+         texture.GetBaseObject().ReferenceCount() <= 1;     ///< And nobody use this texture, except this contianer.
 }
 
 // Forward declare, for signal connection.
@@ -187,15 +202,12 @@ public:
   CacheImpl()
   : mPixelDataCache{},
     mTextureCache{},
-    mCubeTextureCache{},
     mTimer{},
     mLatestCollectedPixelDataIter{mPixelDataCache.begin()},
     mLatestCollectedTextureIter{mTextureCache.begin()},
-    mLatestCollectedCubeTextureIter{mCubeTextureCache.begin()},
+    mDataMutex{},
     mPixelDataContainerUpdated{false},
     mTextureContainerUpdated{false},
-    mCubeTextureContainerUpdated{false},
-    mDataMutex{},
     mDestroyed{false},
     mFullCollectRequested{false}
   {
@@ -214,17 +226,14 @@ public:
     {
       mDataMutex.lock();
 
-      mDestroyed                      = true;
-      mPixelDataContainerUpdated      = false;
-      mTextureContainerUpdated        = false;
-      mCubeTextureContainerUpdated    = false;
-      mLatestCollectedPixelDataIter   = decltype(mLatestCollectedPixelDataIter)();   // Invalidate iterator
-      mLatestCollectedTextureIter     = decltype(mLatestCollectedTextureIter)();     // Invalidate iterator
-      mLatestCollectedCubeTextureIter = decltype(mLatestCollectedCubeTextureIter){}; // Invalidate iterator
+      mDestroyed                    = true;
+      mPixelDataContainerUpdated    = false;
+      mTextureContainerUpdated      = false;
+      mLatestCollectedPixelDataIter = decltype(mLatestCollectedPixelDataIter)(); // Invalidate iterator
+      mLatestCollectedTextureIter   = decltype(mLatestCollectedTextureIter)();   // Invalidate iterator
 
       mPixelDataCache.clear();
       mTextureCache.clear();
-      mCubeTextureCache.clear();
 
       mDataMutex.unlock();
     }
@@ -240,9 +249,8 @@ public:
 
 private: // Unified API for this class
   // Let compare with hash first. And then, check detail keys after.
-  using PixelDataCacheContainer   = std::map<std::size_t, std::vector<std::pair<ImageInformation, Dali::PixelData>>>;
-  using TextureCacheContainer     = std::map<std::size_t, std::vector<std::pair<Dali::PixelData, Dali::Texture>>>;
-  using CubeTextureCacheContainer = std::map<std::size_t, std::vector<std::pair<std::vector<std::vector<Dali::PixelData>>, Dali::Texture>>>;
+  using PixelDataCacheContainer = std::map<std::size_t, std::vector<std::pair<ImageInformation, Dali::PixelData>>>;
+  using TextureCacheContainer   = std::map<std::size_t, std::vector<std::pair<Dali::PixelData, Dali::Texture>>>;
 
   /**
    * @brief Try to get cached item, or create new handle if there is no item.
@@ -269,6 +277,7 @@ private: // Unified API for this class
       bool found = false;
 
       auto iter = cacheContainer.lower_bound(hashValue);
+      DALI_LOG_INFO(gLogFilter, Debug::Verbose, "HashValue : %zu\n", hashValue);
       if((iter == cacheContainer.end()) || (hashValue != iter->first))
       {
         containerUpdated = true;
@@ -311,6 +320,7 @@ private: // Unified API for this class
 
     return returnItem;
   }
+
   /**
    * @brief Try to collect garbages, which reference counts are 1.
    *
@@ -322,15 +332,10 @@ private: // Unified API for this class
    * @oaram[in, out] checkedCount The number of iteration checked total.
    * @return True if we iterate whole container, so we don't need to check anymore. False otherwise
    */
-  template<bool needMutex, typename ContainerType, typename Iterator = typename ContainerType::iterator>
-  bool CollectGarbages(ContainerType& cacheContainer, bool fullCollect, bool& containerUpdated, Iterator& lastIterator, uint32_t& checkedCount)
+  template<typename KeyType, typename ValueType, bool (*Collectable)(const KeyType&, const ValueType&), typename ContainerType, typename Iterator = typename ContainerType::iterator>
+  bool CollectGarbages(ContainerType& cacheContainer, bool fullCollect, bool& containerUpdated, Iterator& lastIterator, uint32_t& checkedCount, uint32_t& collectedCount)
   {
-    if constexpr(needMutex)
-    {
-      mDataMutex.lock();
-    }
-
-    DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Collect Garbages : %zu\n", cacheContainer.size());
+    DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Collect Garbages : %zu (checkedCount : %d, fullCollect? %d)\n", cacheContainer.size(), checkedCount, fullCollect);
     // Container changed. We should re-collect garbage from begin again.
     if(fullCollect || containerUpdated)
     {
@@ -346,9 +351,11 @@ private: // Unified API for this class
       {
         auto& item = jter->second;
         DALI_LOG_INFO(gLogFilter, Debug::Verbose, "item : %p, ref count : %u\n", item.GetObjectPtr(), (item ? item.GetBaseObject().ReferenceCount() : 0u));
-        if(!item || (item.GetBaseObject().ReferenceCount() == 1u))
+        if(!item || Collectable(jter->first, item))
         {
+          DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GC!!!\n");
           // This item is garbage! just remove it.
+          ++collectedCount;
           jter = cachePairList.erase(jter);
         }
         else
@@ -367,11 +374,6 @@ private: // Unified API for this class
       }
     }
 
-    if constexpr(needMutex)
-    {
-      mDataMutex.unlock();
-    }
-
     return (lastIterator != cacheContainer.end());
   }
 
@@ -390,19 +392,6 @@ public: // Called by main thread.
   }
 
   /**
-   * @brief Try to get cached cube texture, or newly create if there is no cube texture that already cached.
-   *
-   * @param[in] pixelDataList The pixelData list of image.
-   * @param[in] mipmapRequired True if result texture need to generate mipmap.
-   * @return Texture that has been cached. Or empty handle if we fail to found cached item.
-   */
-  Dali::Texture GetOrCreateCachedCubeTexture(const std::vector<std::vector<Dali::PixelData>>& pixelDataList, bool mipmapRequired)
-  {
-    auto hashValue = GenerateHash(pixelDataList, mipmapRequired);
-    return GetOrCreateCachedItem<false, std::vector<std::vector<Dali::PixelData>>, Dali::Texture, CreateCubeTextureFromPixelDataList>(mCubeTextureCache, hashValue, pixelDataList, mipmapRequired, mCubeTextureContainerUpdated);
-  }
-
-  /**
    * @brief Request incremental gargabe collect.
    *
    * @param[in] fullCollect True if we will collect whole items, or incrementally.
@@ -422,12 +411,9 @@ public: // Called by main thread.
       if(!mTimer.IsRunning())
       {
         // Restart container interating.
-        if(!mPixelDataContainerUpdated)
-        {
-          mDataMutex.lock();
-          mPixelDataContainerUpdated = true;
-          mDataMutex.unlock();
-        }
+        mDataMutex.lock();
+        mPixelDataContainerUpdated = true;
+        mDataMutex.unlock();
         mTextureContainerUpdated = true;
         mTimer.Start();
       }
@@ -439,12 +425,13 @@ public: // Can be called by worker thread
    * @brief Try to get cached pixel data, or newly create if there is no pixel data that already cached.
    *
    * @param[in] info The informations of image to load.
+   * @param[in] releasePixelData Whether we need to release pixel data after upload, or not.
    * @return Texture that has been cached. Or empty handle if we fail to found cached item.
    */
-  Dali::PixelData GetOrCreateCachedPixelData(const ImageInformation& info)
+  Dali::PixelData GetOrCreateCachedPixelData(const ImageInformation& info, bool releasePixelData)
   {
     auto hashValue = GenerateHash(info);
-    return GetOrCreateCachedItem<true, ImageInformation, Dali::PixelData, CreatePixelDataFromImageInfo>(mPixelDataCache, hashValue, info, false, mPixelDataContainerUpdated);
+    return GetOrCreateCachedItem<true, ImageInformation, Dali::PixelData, CreatePixelDataFromImageInfo>(mPixelDataCache, hashValue, info, releasePixelData, mPixelDataContainerUpdated);
   }
 
 private: // Called by main thread
@@ -466,40 +453,44 @@ private: // Called by main thread
   bool IncrementalGarbageCollect(bool fullCollect)
   {
     bool continueTimer = false;
+    DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GC start\n");
 
     // Try to collect Texture GC first, due to the reference count of pixelData who become key of textures.
     // After all texture GC finished, then check PixelData cache.
-    uint32_t checkedCount = 0u;
+    uint32_t checkedCount   = 0u;
+    uint32_t collectedCount = 0u;
 
-    // GC Cube Texture
-    continueTimer |= CollectGarbages<false>(mCubeTextureCache, fullCollect, mCubeTextureContainerUpdated, mLatestCollectedCubeTextureIter, checkedCount);
+    // We should lock mutex during GC pixelData.
+    mDataMutex.lock();
 
     // GC Texture
-    continueTimer |= CollectGarbages<false>(mTextureCache, fullCollect, mTextureContainerUpdated, mLatestCollectedTextureIter, checkedCount);
+    continueTimer |= CollectGarbages<Dali::PixelData, Dali::Texture, TextureCacheCollectable>(mTextureCache, fullCollect, mTextureContainerUpdated, mLatestCollectedTextureIter, checkedCount, collectedCount);
+
+    // GC PixelData last. If there are some collected Texture before, we should full-collect.
+    // (Since most of PixelData use 'ReleaseAfterUpload' flags).
+    continueTimer |= CollectGarbages<ImageInformation, Dali::PixelData, PixelDataCacheCollectable>(mPixelDataCache, fullCollect || (collectedCount > 0u), mPixelDataContainerUpdated, mLatestCollectedPixelDataIter, checkedCount, collectedCount);
 
-    // GC PixelData. We should lock mutex during GC pixelData.
-    continueTimer |= CollectGarbages<true>(mPixelDataCache, fullCollect, mPixelDataContainerUpdated, mLatestCollectedPixelDataIter, checkedCount);
+    mDataMutex.unlock();
+
+    DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GC finished. checkedCount : %u, continueTimer : %d\n", checkedCount, continueTimer);
 
     return continueTimer;
   }
 
 private:
-  PixelDataCacheContainer   mPixelDataCache;
-  TextureCacheContainer     mTextureCache;
-  CubeTextureCacheContainer mCubeTextureCache;
+  PixelDataCacheContainer mPixelDataCache;
+  TextureCacheContainer   mTextureCache;
 
   Dali::Timer mTimer;
 
   // Be used when we garbage collection.
-  PixelDataCacheContainer::iterator   mLatestCollectedPixelDataIter;
-  TextureCacheContainer::iterator     mLatestCollectedTextureIter;
-  CubeTextureCacheContainer::iterator mLatestCollectedCubeTextureIter;
+  PixelDataCacheContainer::iterator mLatestCollectedPixelDataIter;
+  TextureCacheContainer::iterator   mLatestCollectedTextureIter;
+
+  std::mutex mDataMutex;
 
   bool mPixelDataContainerUpdated;
   bool mTextureContainerUpdated;
-  bool mCubeTextureContainerUpdated;
-
-  std::mutex mDataMutex;
 
   bool mDestroyed : 1;
   bool mFullCollectRequested : 1;
@@ -507,6 +498,7 @@ private:
 
 static std::shared_ptr<CacheImpl> gCacheImpl{nullptr};
 static Dali::Texture              gEmptyTextureWhiteRGB{};
+static Dali::Texture              gEmptyCubeTextureWhiteRGB{};
 
 std::shared_ptr<CacheImpl> GetCacheImpl()
 {
@@ -523,6 +515,7 @@ void DestroyCacheImpl()
 
   // Remove texture object when application stopped.
   gEmptyTextureWhiteRGB.Reset();
+  gEmptyCubeTextureWhiteRGB.Reset();
 }
 
 } // namespace
@@ -532,12 +525,6 @@ namespace Dali::Scene3D::Internal
 namespace ImageResourceLoader
 {
 // Called by main thread..
-Dali::PixelData GetEmptyPixelDataWhiteRGB()
-{
-  static Dali::PixelData emptyPixelData = PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY);
-  return emptyPixelData;
-}
-
 Dali::Texture GetEmptyTextureWhiteRGB()
 {
   if(!gEmptyTextureWhiteRGB)
@@ -549,14 +536,30 @@ Dali::Texture GetEmptyTextureWhiteRGB()
   return gEmptyTextureWhiteRGB;
 }
 
-Dali::Texture GetCachedTexture(Dali::PixelData pixelData, bool mipmapRequired)
+Dali::Texture GetEmptyCubeTextureWhiteRGB()
 {
-  return GetCacheImpl()->GetOrCreateCachedTexture(pixelData, mipmapRequired);
+  if(!gEmptyCubeTextureWhiteRGB)
+  {
+    Dali::PixelData emptyPixelData = GetEmptyPixelDataWhiteRGB();
+    gEmptyCubeTextureWhiteRGB      = Texture::New(TextureType::TEXTURE_CUBE, emptyPixelData.GetPixelFormat(), emptyPixelData.GetWidth(), emptyPixelData.GetHeight());
+    for(size_t iSide = 0u; iSide < 6; ++iSide)
+    {
+      gEmptyCubeTextureWhiteRGB.Upload(emptyPixelData, CubeMapLayer::POSITIVE_X + iSide, 0u, 0u, 0u, emptyPixelData.GetWidth(), emptyPixelData.GetHeight());
+    }
+  }
+  return gEmptyCubeTextureWhiteRGB;
 }
 
-Dali::Texture GetCachedCubeTexture(const std::vector<std::vector<Dali::PixelData>>& pixelDataList, bool mipmapRequired)
+Dali::Texture GetCachedTexture(Dali::PixelData pixelData, bool mipmapRequired)
 {
-  return GetCacheImpl()->GetOrCreateCachedCubeTexture(pixelDataList, mipmapRequired);
+  if(SupportPixelDataCache(pixelData))
+  {
+    return GetCacheImpl()->GetOrCreateCachedTexture(pixelData, mipmapRequired);
+  }
+  else
+  {
+    return CreateTextureFromPixelData(pixelData, mipmapRequired);
+  }
 }
 
 void RequestGarbageCollect(bool fullCollect)
@@ -570,6 +573,30 @@ void EnsureResourceLoaderCreated()
 }
 
 // Can be called by worker thread.
+Dali::PixelData GetEmptyPixelDataWhiteRGB()
+{
+  static Dali::PixelData emptyPixelData = PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY);
+  return emptyPixelData;
+}
+
+Dali::PixelData GetEmptyPixelDataWhiteRGBA()
+{
+  static Dali::PixelData emptyPixelData = PixelData::New(new uint8_t[4]{0xff, 0xff, 0xff, 0xff}, 4, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY);
+  return emptyPixelData;
+}
+
+Dali::PixelData GetEmptyPixelDataZAxisRGB()
+{
+  static Dali::PixelData emptyPixelData = PixelData::New(new uint8_t[3]{0x7f, 0x7f, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY);
+  return emptyPixelData;
+}
+
+Dali::PixelData GetEmptyPixelDataZAxisAndAlphaRGBA()
+{
+  static Dali::PixelData emptyPixelData = PixelData::New(new uint8_t[4]{0x7f, 0x7f, 0xff, 0xff}, 4, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY);
+  return emptyPixelData;
+}
+
 Dali::PixelData GetCachedPixelData(const std::string& url)
 {
   return GetCachedPixelData(url, ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, true);
@@ -589,7 +616,7 @@ Dali::PixelData GetCachedPixelData(const std::string& url,
   }
   else
   {
-    return GetCacheImpl()->GetOrCreateCachedPixelData(info);
+    return GetCacheImpl()->GetOrCreateCachedPixelData(info, true);
   }
 }
 } // namespace ImageResourceLoader