(Vector) Flush lottie update informations
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-task.cpp
index 4d31135..6bb0d13 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -20,6 +20,7 @@
 
 // EXTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/trace.h>
 #include <dali/public-api/math/math-utils.h>
 #include <dali/public-api/object/property-array.h>
 
 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
 
+#ifdef TRACE_ENABLED
+#include <sstream>
+#endif
+
 namespace Dali
 {
 namespace Toolkit
@@ -43,17 +48,19 @@ constexpr auto MICROSECONDS_PER_SECOND(1e+6);
 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
 #endif
 
+DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false);
+
 } // unnamed namespace
 
 VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
-: mUrl(),
+: AsyncTask(MakeCallback(this, &VectorAnimationTask::TaskCompleted), AsyncTask::PriorityType::HIGH, AsyncTask::ThreadType::WORKER_THREAD),
+  mUrl(),
   mVectorRenderer(VectorAnimationRenderer::New()),
   mAnimationData(),
   mVectorAnimationThread(factoryCache.GetVectorAnimationManager().GetVectorAnimationThread()),
   mConditionalWait(),
   mResourceReadySignal(),
-  mAnimationFinishedTrigger(),
-  mLoadCompletedTrigger(new EventThreadCallback(MakeCallback(this, &VectorAnimationTask::OnLoadCompleted))),
+  mLoadCompletedCallback(MakeCallback(this, &VectorAnimationTask::OnLoadCompleted)),
   mPlayState(PlayState::STOPPED),
   mStopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
   mLoopingMode(DevelImageVisual::LoopingMode::RESTART),
@@ -76,7 +83,9 @@ VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
   mAnimationDataUpdated(false),
   mDestroyTask(false),
   mLoadRequest(false),
-  mLoadFailed(false)
+  mLoadFailed(false),
+  mRasterized(false),
+  mKeepAnimation(false)
 {
   mVectorRenderer.UploadCompletedSignal().Connect(this, &VectorAnimationTask::OnUploadCompleted);
 }
@@ -86,14 +95,30 @@ VectorAnimationTask::~VectorAnimationTask()
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this);
 }
 
+void VectorAnimationTask::Process()
+{
+  mRasterized = Rasterize();
+}
+
+bool VectorAnimationTask::IsReady()
+{
+  return true;
+}
+
 void VectorAnimationTask::Finalize()
 {
   ConditionalWait::ScopedLock lock(mConditionalWait);
 
   // Release some objects in the main thread
-  if(mAnimationFinishedTrigger)
+  if(mAnimationFinishedCallback)
   {
-    mAnimationFinishedTrigger.reset();
+    mVectorAnimationThread.RemoveEventTriggerCallback(mAnimationFinishedCallback.get());
+    mAnimationFinishedCallback.reset();
+  }
+  if(mLoadCompletedCallback)
+  {
+    mVectorAnimationThread.RemoveEventTriggerCallback(mLoadCompletedCallback.get());
+    mLoadCompletedCallback.reset();
   }
 
   mVectorRenderer.Finalize();
@@ -101,14 +126,34 @@ void VectorAnimationTask::Finalize()
   mDestroyTask = true;
 }
 
-bool VectorAnimationTask::Load()
+void VectorAnimationTask::TaskCompleted(VectorAnimationTaskPtr task)
 {
+  mVectorAnimationThread.OnTaskCompleted(task, task->IsRasterized(), task->IsAnimating());
+}
+
+bool VectorAnimationTask::IsRasterized()
+{
+  return mRasterized;
+}
+
+bool VectorAnimationTask::IsAnimating()
+{
+  return mKeepAnimation;
+}
+
+bool VectorAnimationTask::Load(bool synchronousLoading)
+{
+  DALI_TRACE_SCOPE(gTraceFilter, "DALI_LOTTIE_LOADING_TASK");
+
   if(!mVectorRenderer.Load(mUrl))
   {
     DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mUrl.c_str());
     mLoadRequest = false;
     mLoadFailed  = true;
-    mLoadCompletedTrigger->Trigger();
+    if(!synchronousLoading && mLoadCompletedCallback)
+    {
+      mVectorAnimationThread.AddEventTriggerCallback(mLoadCompletedCallback.get());
+    }
     return false;
   }
 
@@ -120,7 +165,10 @@ bool VectorAnimationTask::Load()
   mFrameDurationMicroSeconds = MICROSECONDS_PER_SECOND / mFrameRate;
 
   mLoadRequest = false;
-  mLoadCompletedTrigger->Trigger();
+  if(!synchronousLoading && mLoadCompletedCallback)
+  {
+    mVectorAnimationThread.AddEventTriggerCallback(mLoadCompletedCallback.get());
+  }
 
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Load: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this);
 
@@ -136,12 +184,27 @@ void VectorAnimationTask::SetRenderer(Renderer renderer)
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this);
 }
 
-void VectorAnimationTask::RequestLoad(const std::string& url)
+void VectorAnimationTask::RequestLoad(const std::string& url, bool synchronousLoading)
 {
-  mUrl         = url;
-  mLoadRequest = true;
+  mUrl = url;
 
-  mVectorAnimationThread.AddTask(this);
+  if(!synchronousLoading)
+  {
+    mLoadRequest = true;
+
+    mVectorAnimationThread.AddTask(this);
+  }
+  else
+  {
+    Load(true);
+
+    OnLoadCompleted();
+  }
+}
+
+bool VectorAnimationTask::IsLoadRequested() const
+{
+  return mLoadRequest;
 }
 
 void VectorAnimationTask::SetAnimationData(const AnimationData& data)
@@ -152,7 +215,7 @@ void VectorAnimationTask::SetAnimationData(const AnimationData& data)
 
   uint32_t index = mAnimationDataIndex == 0 ? 1 : 0; // Use the other buffer
 
-  mAnimationData[index] = data;
+  mAnimationData[index].push_back(data);
   mAnimationDataUpdated = true;
 
   if(data.resendFlag & VectorAnimationTask::RESEND_SIZE)
@@ -210,13 +273,10 @@ void VectorAnimationTask::PauseAnimation()
   }
 }
 
-void VectorAnimationTask::SetAnimationFinishedCallback(EventThreadCallback* callback)
+void VectorAnimationTask::SetAnimationFinishedCallback(CallbackBase* callback)
 {
   ConditionalWait::ScopedLock lock(mConditionalWait);
-  if(callback)
-  {
-    mAnimationFinishedTrigger = std::unique_ptr<EventThreadCallback>(callback);
-  }
+  mAnimationFinishedCallback = std::unique_ptr<CallbackBase>(callback);
 }
 
 void VectorAnimationTask::SetLoopCount(int32_t count)
@@ -280,38 +340,33 @@ void VectorAnimationTask::SetPlayRange(const Property::Array& playRange)
   }
 
   // Make sure the range specified is between 0 and the total frame number
-  if(startFrame < mTotalFrame && endFrame < mTotalFrame)
+  startFrame = std::min(startFrame, mTotalFrame - 1);
+  endFrame   = std::min(endFrame, mTotalFrame - 1);
+
+  // If the range is not in order swap values
+  if(startFrame > endFrame)
   {
-    // If the range is not in order swap values
-    if(startFrame > endFrame)
+    uint32_t temp = startFrame;
+    startFrame    = endFrame;
+    endFrame      = temp;
+  }
+
+  if(startFrame != mStartFrame || endFrame != mEndFrame)
+  {
+    mStartFrame = startFrame;
+    mEndFrame   = endFrame;
+
+    // If the current frame is out of the range, change the current frame also.
+    if(mStartFrame > mCurrentFrame)
     {
-      uint32_t temp = startFrame;
-      startFrame    = endFrame;
-      endFrame      = temp;
+      mCurrentFrame = mStartFrame;
     }
-
-    if(startFrame != mStartFrame || endFrame != mEndFrame)
+    else if(mEndFrame < mCurrentFrame)
     {
-      mStartFrame = startFrame;
-      mEndFrame   = endFrame;
-
-      // If the current frame is out of the range, change the current frame also.
-      if(mStartFrame > mCurrentFrame)
-      {
-        mCurrentFrame = mStartFrame;
-      }
-      else if(mEndFrame < mCurrentFrame)
-      {
-        mCurrentFrame = mEndFrame;
-      }
-
-      DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%s] [%p]\n", mStartFrame, mEndFrame, mUrl.c_str(), this);
+      mCurrentFrame = mEndFrame;
     }
-  }
-  else
-  {
-    DALI_LOG_ERROR("VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%s] [%p]\n", startFrame, endFrame, mUrl.c_str(), this);
-    return;
+
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%s] [%p]\n", mStartFrame, mEndFrame, mUrl.c_str(), this);
   }
 }
 
@@ -381,11 +436,11 @@ VectorAnimationTask::ResourceReadySignalType& VectorAnimationTask::ResourceReady
   return mResourceReadySignal;
 }
 
-bool VectorAnimationTask::Rasterize(bool& keepAnimation)
+bool VectorAnimationTask::Rasterize()
 {
   bool     stopped = false;
   uint32_t currentFrame;
-  keepAnimation = false;
+  mKeepAnimation = false;
 
   {
     ConditionalWait::ScopedLock lock(mConditionalWait);
@@ -394,15 +449,11 @@ bool VectorAnimationTask::Rasterize(bool& keepAnimation)
       // The task will be destroyed. We don't need rasterization.
       return false;
     }
+  }
 
-    if(mLoadRequest)
-    {
-      bool result = Load();
-      if(!result)
-      {
-        return false;
-      }
-    }
+  if(mLoadRequest)
+  {
+    return Load(false);
   }
 
   if(mLoadFailed)
@@ -410,6 +461,8 @@ bool VectorAnimationTask::Rasterize(bool& keepAnimation)
     return false;
   }
 
+  DALI_TRACE_BEGIN(gTraceFilter, "DALI_LOTTIE_RASTERIZE_TASK");
+
   ApplyAnimationData();
 
   if(mPlayState == PlayState::PLAYING && mUpdateFrameNumber)
@@ -494,12 +547,18 @@ bool VectorAnimationTask::Rasterize(bool& keepAnimation)
     mForward     = true;
     mCurrentLoop = 0;
 
+    if(mVectorRenderer)
+    {
+      // Notify the Renderer that rendering is stopped.
+      mVectorRenderer.RenderStopped();
+    }
+
     // Animation is finished
     {
       ConditionalWait::ScopedLock lock(mConditionalWait);
-      if(mNeedAnimationFinishedTrigger && mAnimationFinishedTrigger)
+      if(mNeedAnimationFinishedTrigger && mAnimationFinishedCallback)
       {
-        mAnimationFinishedTrigger->Trigger();
+        mVectorAnimationThread.AddEventTriggerCallback(mAnimationFinishedCallback.get());
       }
     }
 
@@ -508,9 +567,21 @@ bool VectorAnimationTask::Rasterize(bool& keepAnimation)
 
   if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED)
   {
-    keepAnimation = true;
+    mKeepAnimation = true;
   }
 
+#ifdef TRACE_ENABLED
+  if(gTraceFilter && gTraceFilter->IsTraceEnabled())
+  {
+    std::ostringstream oss;
+    oss << "[size: " << mWidth << " x " << mHeight << ", ";
+    oss << "frame: " << mCurrentFrame << ", ";
+    oss << "loop: " << mCurrentLoop << ", ";
+    oss << "state : " << mPlayState << "]";
+    DALI_TRACE_END_WITH_MESSAGE(gTraceFilter, "DALI_LOTTIE_RASTERIZE_TASK", oss.str().c_str());
+  }
+#endif
+
   return true;
 }
 
@@ -589,7 +660,7 @@ void VectorAnimationTask::ApplyAnimationData()
   {
     ConditionalWait::ScopedLock lock(mConditionalWait);
 
-    if(!mAnimationDataUpdated || mAnimationData[mAnimationDataIndex].resendFlag != 0)
+    if(!mAnimationDataUpdated || mAnimationData[mAnimationDataIndex].size() != 0)
     {
       // Data is not updated or the previous data is not applied yet.
       return;
@@ -601,78 +672,81 @@ void VectorAnimationTask::ApplyAnimationData()
     index = mAnimationDataIndex;
   }
 
-  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT)
+  for(const auto& animationData : mAnimationData[index])
   {
-    SetLoopCount(mAnimationData[index].loopCount);
-  }
-
-  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE)
-  {
-    SetPlayRange(mAnimationData[index].playRange);
-  }
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT)
+    {
+      SetLoopCount(animationData.loopCount);
+    }
 
-  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR)
-  {
-    SetStopBehavior(mAnimationData[index].stopBehavior);
-  }
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE)
+    {
+      SetPlayRange(animationData.playRange);
+    }
 
-  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE)
-  {
-    SetLoopingMode(mAnimationData[index].loopingMode);
-  }
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR)
+    {
+      SetStopBehavior(animationData.stopBehavior);
+    }
 
-  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME)
-  {
-    SetCurrentFrameNumber(mAnimationData[index].currentFrame);
-  }
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE)
+    {
+      SetLoopingMode(animationData.loopingMode);
+    }
 
-  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_NEED_RESOURCE_READY)
-  {
-    mVectorRenderer.InvalidateBuffer();
-  }
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME)
+    {
+      SetCurrentFrameNumber(animationData.currentFrame);
+    }
 
-  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
-  {
-    if(mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING)
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_NEED_RESOURCE_READY)
     {
-      PlayAnimation();
+      mVectorRenderer.InvalidateBuffer();
     }
-    else if(mAnimationData[index].playState == DevelImageVisual::PlayState::PAUSED)
+
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_DYNAMIC_PROPERTY)
     {
-      PauseAnimation();
+      for(auto&& iter : animationData.dynamicProperties)
+      {
+        mVectorRenderer.AddPropertyValueCallback(iter.keyPath, static_cast<VectorAnimationRenderer::VectorProperty>(iter.property), iter.callback, iter.id);
+      }
     }
-    else if(mAnimationData[index].playState == DevelImageVisual::PlayState::STOPPED)
+
+    if(animationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
     {
-      StopAnimation();
+      if(animationData.playState == DevelImageVisual::PlayState::PLAYING)
+      {
+        PlayAnimation();
+      }
+      else if(animationData.playState == DevelImageVisual::PlayState::PAUSED)
+      {
+        PauseAnimation();
+      }
+      else if(animationData.playState == DevelImageVisual::PlayState::STOPPED)
+      {
+        StopAnimation();
+      }
     }
   }
 
-  mAnimationData[index].resendFlag = 0;
+  // reset data list
+  mAnimationData[index].clear();
 }
 
 void VectorAnimationTask::OnUploadCompleted()
 {
-  mResourceReadySignal.Emit(true);
+  mResourceReadySignal.Emit(ResourceStatus::READY);
 }
 
 void VectorAnimationTask::OnLoadCompleted()
 {
   if(!mLoadFailed)
   {
-    if(mWidth == 0 && mHeight == 0)
-    {
-      uint32_t width, height;
-      mVectorRenderer.GetDefaultSize(width, height);
-
-      SetSize(width, height);
-
-      mVectorAnimationThread.AddTask(this);
-    }
+    mResourceReadySignal.Emit(ResourceStatus::LOADED);
   }
   else
   {
-    // Load failed
-    mResourceReadySignal.Emit(false);
+    mResourceReadySignal.Emit(ResourceStatus::FAILED);
   }
 }
 } // namespace Internal