Cache animated image's frame index
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / texture-manager / texture-cache-manager.cpp
index a561161..01e43a4 100644 (file)
@@ -481,7 +481,8 @@ TextureCacheManager::TextureHash TextureCacheManager::GenerateHash(
   const Dali::SamplingMode::Type&       samplingMode,
   const TextureCacheManager::UseAtlas&  useAtlas,
   const TextureCacheManager::TextureId& maskTextureId,
-  const bool&                           cropToMask)
+  const bool&                           cropToMask,
+  const std::uint32_t&                  frameIndex)
 {
   std::vector<std::uint8_t> hashTarget(url.GetUrl().begin(), url.GetUrl().end());
   const size_t              urlLength = hashTarget.size();
@@ -543,6 +544,22 @@ TextureCacheManager::TextureHash TextureCacheManager::GenerateHash(
     *hashTargetPtr++ = (cropToMask ? 'C' : 'M');
   }
 
+  // Append the frameIndex. We don't do additional job when frameIndex = 0u due to the non-animated image case.
+  if(frameIndex > 0u)
+  {
+    auto textureIdIndex = hashTarget.size();
+    hashTarget.resize(hashTarget.size() + sizeof(std::uint32_t));
+    std::uint8_t* hashTargetPtr = reinterpret_cast<std::uint8_t*>(&(hashTarget[textureIdIndex]));
+
+    // Append the frame index to the end of the URL byte by byte:
+    std::uint32_t saltedFrameIndex = frameIndex;
+    for(size_t byteIter = 0; byteIter < sizeof(std::uint8_t); ++byteIter)
+    {
+      *hashTargetPtr++ = saltedFrameIndex & 0xff;
+      saltedFrameIndex >>= 8u;
+    }
+  }
+
   return Dali::CalculateHash(hashTarget);
 }
 
@@ -556,7 +573,8 @@ TextureCacheManager::TextureCacheIndex TextureCacheManager::FindCachedTexture(
   const TextureCacheManager::TextureId&      maskTextureId,
   const bool&                                cropToMask,
   const TextureCacheManager::MultiplyOnLoad& preMultiplyOnLoad,
-  const bool&                                isAnimatedImage)
+  const bool&                                isAnimatedImage,
+  const std::uint32_t&                       frameIndex)
 {
   // Iterate through our hashes to find a match.
   const auto& hashIterator = mTextureHashContainer.find(hash);
@@ -576,6 +594,7 @@ TextureCacheManager::TextureCacheIndex TextureCacheManager::FindCachedTexture(
            (cropToMask == textureInfo.cropToMask) &&
            (size == textureInfo.desiredSize) &&
            (isAnimatedImage == textureInfo.isAnimatedImageFormat) &&
+           (frameIndex == textureInfo.frameIndex) &&
            ((size.GetWidth() == 0 && size.GetHeight() == 0) ||
             (fittingMode == textureInfo.fittingMode &&
              samplingMode == textureInfo.samplingMode)))