(AnimatedVector) Support TizenVectorAnimationRenderer load from data 99/300599/7
authorEunki Hong <eunkiki.hong@samsung.com>
Sat, 28 Oct 2023 16:43:59 +0000 (01:43 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Tue, 31 Oct 2023 05:44:40 +0000 (14:44 +0900)
Let we support AnimatedVectorImage load from buffer
by this API, rlottie::Animation::loadFromData

Change-Id: Ib7c1289fad1a4a130ff6c347a00945ca77f9603b
Signed-off-by: Eunki Hong <eunkiki.hong@samsung.com>
dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer.cpp
dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer.h
dali-extension/vector-animation-renderer/tizen-vector-animation-renderer.cpp
dali-extension/vector-animation-renderer/tizen-vector-animation-renderer.h

index 2dc8851..fd7fd92 100644 (file)
@@ -103,7 +103,6 @@ void RiveAnimationRenderer::ClearRiveAnimations()
 
 void RiveAnimationRenderer::LoadRiveFile(const std::string& filename)
 {
-  std::streampos        length = 0;
   Dali::Vector<uint8_t> bytes;
 
   if(!Dali::FileLoader::ReadFile(filename, bytes))
@@ -112,16 +111,21 @@ void RiveAnimationRenderer::LoadRiveFile(const std::string& filename)
     return;
   }
 
+  LoadRiveData(bytes);
+}
+
+void RiveAnimationRenderer::LoadRiveData(const Dali::Vector<uint8_t>& bytes)
+{
   if(bytes.Size() == 0)
   {
-    DALI_LOG_ERROR("Failed to load: empty file %s", filename.c_str());
+    DALI_LOG_ERROR("Failed to load: empty file");
     return;
   }
 
   ClearRiveAnimations();
-  if(!mRiveTizenAdapter->loadRiveResource(&bytes[0], bytes.Size()))
+  if(!mRiveTizenAdapter->loadRiveResource(const_cast<uint8_t*>(&bytes[0]), bytes.Size()))
   {
-    DALI_LOG_ERROR("Failed to load resource file %s", filename.c_str());
+    DALI_LOG_ERROR("Failed to load resource file");
     return;
   }
 
@@ -154,6 +158,16 @@ bool RiveAnimationRenderer::Load(const std::string& url)
   return true;
 }
 
+bool RiveAnimationRenderer::Load(const Dali::Vector<uint8_t>& data)
+{
+  LoadRiveData(data);
+  RiveAnimationRendererManager::Get().AddEventHandler(*this);
+
+  DALI_LOG_INFO(gRiveAnimationLogFilter, Debug::Verbose, "RiveAnimationRenderer::Initialize: data [data size : %zu byte] [%p]\n", data.Size(), this);
+
+  return true;
+}
+
 void RiveAnimationRenderer::Finalize()
 {
   Dali::Mutex::ScopedLock lock(mMutex);
index 760dd1e..cb50961 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_EXTENSION_INTERNAL_RIVE_ANIMATION_RENDERER_H
 
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
@@ -22,6 +22,7 @@
 #include <dali/devel-api/adaptor-framework/native-image-source-queue.h>
 #include <dali/devel-api/threading/mutex.h>
 #include <dali/integration-api/debug.h> ///< note : Debug::DebugPriority::DEBUG can be removed due to <rive/rive_types.hpp>.
+#include <dali/public-api/common/dali-vector.h>
 #include <dali/public-api/common/vector-wrapper.h>
 #include <dali/public-api/rendering/renderer.h>
 #include <tbm_surface.h>
@@ -75,6 +76,14 @@ public:
   bool Load(const std::string& url);
 
   /**
+   * @brief Loads the animation file from data.
+   *
+   * @param[in] data The url of the vector animation file
+   * @return True if loading success, false otherwise.
+   */
+  bool Load(const Dali::Vector<uint8_t>& data);
+
+  /**
    * @brief Finalizes the renderer. It will be called in the main thread.
    */
   void Finalize();
@@ -292,6 +301,11 @@ private:
   void LoadRiveFile(const std::string& filename);
 
   /**
+   * @brief Load rive resource data for artboard.
+   */
+  void LoadRiveData(const Dali::Vector<uint8_t>& data);
+
+  /**
    * @brief Clear Loaded Animations.
    */
   void ClearRiveAnimations();
index 66b6778..bc9b078 100644 (file)
@@ -20,6 +20,7 @@
 
 // EXTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/native-image-source-queue.h>
+#include <dali/devel-api/common/hash.h>
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/object/property-array.h>
 #include <tbm_surface_internal.h>
@@ -129,6 +130,34 @@ bool TizenVectorAnimationRenderer::Load(const std::string& url)
   return true;
 }
 
+bool TizenVectorAnimationRenderer::Load(const Dali::Vector<uint8_t>& data)
+{
+  Dali::Mutex::ScopedLock lock(mMutex);
+
+  std::string jsonData(data.Begin(), data.End());    ///< Convert from raw buffer to string.
+  auto        hashValue = Dali::CalculateHash(data); ///< Will be used for rlottie internal cache system.
+
+  mVectorRenderer = rlottie::Animation::loadFromData(jsonData, std::to_string(hashValue));
+  if(!mVectorRenderer)
+  {
+    DALI_LOG_ERROR("Failed to load a Lottie data [data size : %zu byte] [%p]\n", data.Size(), this);
+    mLoadFailed = true;
+    return false;
+  }
+
+  mTotalFrameNumber = static_cast<uint32_t>(mVectorRenderer->totalFrame());
+  mFrameRate        = static_cast<float>(mVectorRenderer->frameRate());
+
+  size_t w, h;
+  mVectorRenderer->size(w, h);
+  mDefaultWidth  = static_cast<uint32_t>(w);
+  mDefaultHeight = static_cast<uint32_t>(h);
+
+  DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "data [data size : %zu byte] [%p]\n", data.Size(), this);
+
+  return true;
+}
+
 void TizenVectorAnimationRenderer::SetRenderer(Renderer renderer)
 {
   mRenderer      = renderer;
index 6f08892..ae7c415 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TIZEN_VECTOR_ANIMATION_RENDERER_PLUGIN_H
 
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
@@ -62,6 +62,11 @@ public:
   bool Load(const std::string& url) override;
 
   /**
+   * @copydoc Dali::VectorAnimationRendererPlugin::Load()
+   */
+  bool Load(const Dali::Vector<uint8_t>& data) override;
+
+  /**
    * @copydoc Dali::VectorAnimationRendererPlugin::SetRenderer()
    */
   void SetRenderer(Renderer renderer) override;