(Vector) Change SetPlayRange and fix a crash
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-task.cpp
index a1659bf..302fce7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
@@ -51,7 +51,9 @@ VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
   mAnimationData(),
   mVectorAnimationThread(factoryCache.GetVectorAnimationManager().GetVectorAnimationThread()),
   mConditionalWait(),
+  mResourceReadySignal(),
   mAnimationFinishedTrigger(),
+  mLoadCompletedTrigger(new EventThreadCallback(MakeCallback(this, &VectorAnimationTask::OnLoadCompleted))),
   mPlayState(PlayState::STOPPED),
   mStopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
   mLoopingMode(DevelImageVisual::LoopingMode::RESTART),
@@ -72,8 +74,11 @@ VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
   mUpdateFrameNumber(false),
   mNeedAnimationFinishedTrigger(true),
   mAnimationDataUpdated(false),
-  mDestroyTask(false)
+  mDestroyTask(false),
+  mLoadRequest(false),
+  mLoadFailed(false)
 {
+  mVectorRenderer.UploadCompletedSignal().Connect(this, &VectorAnimationTask::OnUploadCompleted);
 }
 
 VectorAnimationTask::~VectorAnimationTask()
@@ -90,19 +95,24 @@ void VectorAnimationTask::Finalize()
   {
     mAnimationFinishedTrigger.reset();
   }
+  if(mLoadCompletedTrigger)
+  {
+    mLoadCompletedTrigger.reset();
+  }
 
   mVectorRenderer.Finalize();
 
   mDestroyTask = true;
 }
 
-bool VectorAnimationTask::Load(const std::string& url)
+bool VectorAnimationTask::Load()
 {
-  mUrl = url;
-
   if(!mVectorRenderer.Load(mUrl))
   {
     DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mUrl.c_str());
+    mLoadRequest = false;
+    mLoadFailed  = true;
+    mLoadCompletedTrigger->Trigger();
     return false;
   }
 
@@ -113,10 +123,8 @@ bool VectorAnimationTask::Load(const std::string& url)
   mFrameRate                 = mVectorRenderer.GetFrameRate();
   mFrameDurationMicroSeconds = MICROSECONDS_PER_SECOND / mFrameRate;
 
-  uint32_t width, height;
-  mVectorRenderer.GetDefaultSize(width, height);
-
-  SetSize(width, height);
+  mLoadRequest = false;
+  mLoadCompletedTrigger->Trigger();
 
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Load: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this);
 
@@ -132,6 +140,19 @@ void VectorAnimationTask::SetRenderer(Renderer renderer)
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this);
 }
 
+void VectorAnimationTask::RequestLoad(const std::string& url)
+{
+  mUrl         = url;
+  mLoadRequest = true;
+
+  mVectorAnimationThread.AddTask(this);
+}
+
+bool VectorAnimationTask::IsLoadRequested() const
+{
+  return mLoadRequest;
+}
+
 void VectorAnimationTask::SetAnimationData(const AnimationData& data)
 {
   ConditionalWait::ScopedLock lock(mConditionalWait);
@@ -254,9 +275,8 @@ void VectorAnimationTask::SetPlayRange(const Property::Array& playRange)
     std::string marker;
     if(playRange.GetElementAt(0).Get(marker))
     {
-      if(mVectorRenderer)
+      if(mVectorRenderer && mVectorRenderer.GetMarkerInfo(marker, startFrame, endFrame))
       {
-        mVectorRenderer.GetMarkerInfo(marker, startFrame, endFrame);
         valid = true;
       }
     }
@@ -269,38 +289,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] [%p]\n", mStartFrame, mEndFrame, this);
+      mCurrentFrame = mEndFrame;
     }
-  }
-  else
-  {
-    DALI_LOG_ERROR("VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this);
-    return;
+
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%s] [%p]\n", mStartFrame, mEndFrame, mUrl.c_str(), this);
   }
 }
 
@@ -327,7 +342,7 @@ void VectorAnimationTask::SetCurrentFrameNumber(uint32_t frameNumber)
   }
   else
   {
-    DALI_LOG_ERROR("Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame);
+    DALI_LOG_ERROR("Invalid frame number [%d (%d, %d)] [%p]\n", frameNumber, mStartFrame, mEndFrame, this);
   }
 }
 
@@ -365,15 +380,16 @@ void VectorAnimationTask::GetLayerInfo(Property::Map& map) const
   mVectorRenderer.GetLayerInfo(map);
 }
 
-VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
+VectorAnimationTask::ResourceReadySignalType& VectorAnimationTask::ResourceReadySignal()
 {
-  return mVectorRenderer.UploadCompletedSignal();
+  return mResourceReadySignal;
 }
 
-bool VectorAnimationTask::Rasterize()
+bool VectorAnimationTask::Rasterize(bool& keepAnimation)
 {
   bool     stopped = false;
-  uint32_t currentFrame, droppedFrames = 0;
+  uint32_t currentFrame;
+  keepAnimation = false;
 
   {
     ConditionalWait::ScopedLock lock(mConditionalWait);
@@ -382,14 +398,23 @@ bool VectorAnimationTask::Rasterize()
       // The task will be destroyed. We don't need rasterization.
       return false;
     }
-    droppedFrames = mDroppedFrames;
+
+    if(mLoadRequest)
+    {
+      return Load();
+    }
+  }
+
+  if(mLoadFailed)
+  {
+    return false;
   }
 
   ApplyAnimationData();
 
   if(mPlayState == PlayState::PLAYING && mUpdateFrameNumber)
   {
-    mCurrentFrame = mForward ? mCurrentFrame + droppedFrames + 1 : mCurrentFrame - droppedFrames - 1;
+    mCurrentFrame = mForward ? mCurrentFrame + mDroppedFrames + 1 : (mCurrentFrame > mDroppedFrames ? mCurrentFrame - mDroppedFrames - 1 : 0);
     Dali::ClampInPlace(mCurrentFrame, mStartFrame, mEndFrame);
   }
 
@@ -481,13 +506,12 @@ bool VectorAnimationTask::Rasterize()
     DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this);
   }
 
-  bool keepAnimation = true;
-  if(mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED)
+  if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED)
   {
-    keepAnimation = false;
+    keepAnimation = true;
   }
 
-  return keepAnimation;
+  return true;
 }
 
 uint32_t VectorAnimationTask::GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame)
@@ -525,31 +549,29 @@ uint32_t VectorAnimationTask::GetStoppedFrame(uint32_t startFrame, uint32_t endF
 
 VectorAnimationTask::TimePoint VectorAnimationTask::CalculateNextFrameTime(bool renderNow)
 {
-  uint32_t droppedFrames = 0;
-
-  // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
+  // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::steady_clock supported
   // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
   // is casted to use the default duration.
   mNextFrameStartTime = std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds));
-  auto current        = std::chrono::system_clock::now();
+  auto current        = std::chrono::steady_clock::now();
+  mDroppedFrames      = 0;
+
   if(renderNow)
   {
     mNextFrameStartTime = current;
   }
   else if(mNextFrameStartTime < current)
   {
-    while(current > std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds)))
+    uint32_t droppedFrames = 0;
+
+    while(current > std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds)) && droppedFrames < mTotalFrame)
     {
       droppedFrames++;
       mNextFrameStartTime = std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds));
     }
 
-    {
-      ConditionalWait::ScopedLock lock(mConditionalWait);
-      mDroppedFrames = droppedFrames;
-    }
-
     mNextFrameStartTime = current;
+    mDroppedFrames      = droppedFrames;
   }
 
   return mNextFrameStartTime;
@@ -604,6 +626,11 @@ void VectorAnimationTask::ApplyAnimationData()
     SetCurrentFrameNumber(mAnimationData[index].currentFrame);
   }
 
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_NEED_RESOURCE_READY)
+  {
+    mVectorRenderer.InvalidateBuffer();
+  }
+
   if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
   {
     if(mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING)
@@ -623,6 +650,22 @@ void VectorAnimationTask::ApplyAnimationData()
   mAnimationData[index].resendFlag = 0;
 }
 
+void VectorAnimationTask::OnUploadCompleted()
+{
+  mResourceReadySignal.Emit(ResourceStatus::READY);
+}
+
+void VectorAnimationTask::OnLoadCompleted()
+{
+  if(!mLoadFailed)
+  {
+    mResourceReadySignal.Emit(ResourceStatus::LOADED);
+  }
+  else
+  {
+    mResourceReadySignal.Emit(ResourceStatus::FAILED);
+  }
+}
 } // namespace Internal
 
 } // namespace Toolkit