Determine priority type for each task
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-task.cpp
index 302fce7..40f1a64 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.
@@ -46,7 +46,8 @@ Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging,
 } // 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()),
@@ -76,7 +77,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,6 +89,16 @@ 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);
@@ -105,14 +118,32 @@ 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)
 {
   if(!mVectorRenderer.Load(mUrl))
   {
     DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mUrl.c_str());
     mLoadRequest = false;
     mLoadFailed  = true;
-    mLoadCompletedTrigger->Trigger();
+    if(!synchronousLoading)
+    {
+      mLoadCompletedTrigger->Trigger();
+    }
     return false;
   }
 
@@ -124,7 +155,10 @@ bool VectorAnimationTask::Load()
   mFrameDurationMicroSeconds = MICROSECONDS_PER_SECOND / mFrameRate;
 
   mLoadRequest = false;
-  mLoadCompletedTrigger->Trigger();
+  if(!synchronousLoading)
+  {
+    mLoadCompletedTrigger->Trigger();
+  }
 
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Load: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this);
 
@@ -140,12 +174,22 @@ 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
@@ -385,11 +429,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);
@@ -398,11 +442,11 @@ bool VectorAnimationTask::Rasterize(bool& keepAnimation)
       // The task will be destroyed. We don't need rasterization.
       return false;
     }
+  }
 
-    if(mLoadRequest)
-    {
-      return Load();
-    }
+  if(mLoadRequest)
+  {
+    return Load(false);
   }
 
   if(mLoadFailed)
@@ -494,6 +538,12 @@ 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);
@@ -508,7 +558,7 @@ bool VectorAnimationTask::Rasterize(bool& keepAnimation)
 
   if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED)
   {
-    keepAnimation = true;
+    mKeepAnimation = true;
   }
 
   return true;
@@ -631,6 +681,14 @@ void VectorAnimationTask::ApplyAnimationData()
     mVectorRenderer.InvalidateBuffer();
   }
 
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_DYNAMIC_PROPERTY)
+  {
+    for(auto&& iter : mAnimationData[index].dynamicProperties)
+    {
+      mVectorRenderer.AddPropertyValueCallback(iter.keyPath, static_cast<VectorAnimationRenderer::VectorProperty>(iter.property), iter.callback, iter.id);
+    }
+  }
+
   if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
   {
     if(mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING)
@@ -647,6 +705,8 @@ void VectorAnimationTask::ApplyAnimationData()
     }
   }
 
+  // reset data
+  mAnimationData[index].dynamicProperties.clear();
   mAnimationData[index].resendFlag = 0;
 }