(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 6072ce5..302fce7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 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.
 
 // EXTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
-#include <dali/public-api/object/property-array.h>
 #include <dali/public-api/math/math-utils.h>
+#include <dali/public-api/object/property-array.h>
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
+#include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
+#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 namespace
 {
-
 constexpr auto LOOP_FOREVER = -1;
-constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
+constexpr auto MICROSECONDS_PER_SECOND(1e+6);
 
 #if defined(DEBUG_ENABLED)
-Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
+Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
 #endif
 
 } // unnamed namespace
 
-VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url )
-: mUrl( url ),
-  mVectorRenderer(),
+VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
+: mUrl(),
+  mVectorRenderer(VectorAnimationRenderer::New()),
   mAnimationData(),
-  mVectorAnimationThread( factoryCache.GetVectorAnimationThread() ),
+  mVectorAnimationThread(factoryCache.GetVectorAnimationManager().GetVectorAnimationThread()),
   mConditionalWait(),
+  mResourceReadySignal(),
   mAnimationFinishedTrigger(),
-  mPlayState( PlayState::STOPPED ),
-  mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
-  mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
+  mLoadCompletedTrigger(new EventThreadCallback(MakeCallback(this, &VectorAnimationTask::OnLoadCompleted))),
+  mPlayState(PlayState::STOPPED),
+  mStopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
+  mLoopingMode(DevelImageVisual::LoopingMode::RESTART),
   mNextFrameStartTime(),
-  mFrameDurationNanoSeconds( 0 ),
-  mFrameRate( 60.0f ),
-  mCurrentFrame( 0 ),
-  mTotalFrame( 0 ),
-  mStartFrame( 0 ),
-  mEndFrame( 0 ),
-  mWidth( 0 ),
-  mHeight( 0 ),
-  mAnimationDataIndex( 0 ),
-  mLoopCount( LOOP_FOREVER ),
-  mCurrentLoop( 0 ),
-  mResourceReady( false ),
-  mForward( true ),
-  mUpdateFrameNumber( false ),
-  mNeedAnimationFinishedTrigger( true ),
-  mAnimationDataUpdated( false )
-{
-  Initialize();
+  mFrameDurationMicroSeconds(MICROSECONDS_PER_SECOND / 60.0f),
+  mFrameRate(60.0f),
+  mCurrentFrame(0),
+  mTotalFrame(0),
+  mStartFrame(0),
+  mEndFrame(0),
+  mDroppedFrames(0),
+  mWidth(0),
+  mHeight(0),
+  mAnimationDataIndex(0),
+  mLoopCount(LOOP_FOREVER),
+  mCurrentLoop(0),
+  mForward(true),
+  mUpdateFrameNumber(false),
+  mNeedAnimationFinishedTrigger(true),
+  mAnimationDataUpdated(false),
+  mDestroyTask(false),
+  mLoadRequest(false),
+  mLoadFailed(false)
+{
+  mVectorRenderer.UploadCompletedSignal().Connect(this, &VectorAnimationTask::OnUploadCompleted);
 }
 
 VectorAnimationTask::~VectorAnimationTask()
 {
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
+  DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this);
 }
 
 void VectorAnimationTask::Finalize()
 {
-  ConditionalWait::ScopedLock lock( mConditionalWait );
+  ConditionalWait::ScopedLock lock(mConditionalWait);
 
   // Release some objects in the main thread
-  if( mAnimationFinishedTrigger )
+  if(mAnimationFinishedTrigger)
   {
     mAnimationFinishedTrigger.reset();
   }
+  if(mLoadCompletedTrigger)
+  {
+    mLoadCompletedTrigger.reset();
+  }
 
   mVectorRenderer.Finalize();
+
+  mDestroyTask = true;
 }
 
-void VectorAnimationTask::SetRenderer( Renderer renderer )
+bool VectorAnimationTask::Load()
 {
-  ConditionalWait::ScopedLock lock( mConditionalWait );
+  if(!mVectorRenderer.Load(mUrl))
+  {
+    DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mUrl.c_str());
+    mLoadRequest = false;
+    mLoadFailed  = true;
+    mLoadCompletedTrigger->Trigger();
+    return false;
+  }
+
+  mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
+
+  mEndFrame = mTotalFrame - 1;
 
-  mVectorRenderer.SetRenderer( renderer );
+  mFrameRate                 = mVectorRenderer.GetFrameRate();
+  mFrameDurationMicroSeconds = MICROSECONDS_PER_SECOND / mFrameRate;
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
+  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);
+
+  return true;
 }
 
-void VectorAnimationTask::SetAnimationData( const AnimationData& data )
+void VectorAnimationTask::SetRenderer(Renderer renderer)
 {
-  ConditionalWait::ScopedLock lock( mConditionalWait );
+  ConditionalWait::ScopedLock lock(mConditionalWait);
+
+  mVectorRenderer.SetRenderer(renderer);
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetAnimationData [%p]\n", this );
+  DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this);
+}
 
-  uint32_t index = mAnimationDataIndex == 0 ? 1 : 0;  // Use the other buffer
+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);
+
+  DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetAnimationData [%p]\n", this);
+
+  uint32_t index = mAnimationDataIndex == 0 ? 1 : 0; // Use the other buffer
 
   mAnimationData[index] = data;
   mAnimationDataUpdated = true;
 
-  if( data.resendFlag & VectorAnimationTask::RESEND_SIZE )
+  if(data.resendFlag & VectorAnimationTask::RESEND_SIZE)
   {
     // The size should be changed in the main thread.
-    SetSize( data.width, data.height );
+    SetSize(data.width, data.height);
   }
 
-  mVectorAnimationThread.AddTask( this );
+  mVectorAnimationThread.AddTask(this);
 }
 
-void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
+void VectorAnimationTask::SetSize(uint32_t width, uint32_t height)
 {
-  if( mWidth != width || mHeight != height )
+  if(mWidth != width || mHeight != height)
   {
-    mVectorRenderer.SetSize( width, height );
+    mVectorRenderer.SetSize(width, height);
 
-    mWidth = width;
+    mWidth  = width;
     mHeight = height;
 
-    mResourceReady = false;
-
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this);
   }
 }
 
 void VectorAnimationTask::PlayAnimation()
 {
-  if( mPlayState != PlayState::PLAYING )
+  if(mPlayState != PlayState::PLAYING)
   {
-    mUpdateFrameNumber = false;
-    mPlayState = PlayState::PLAYING;
+    mNeedAnimationFinishedTrigger = true;
+    mUpdateFrameNumber            = false;
+    mPlayState                    = PlayState::PLAYING;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this);
   }
 }
 
 void VectorAnimationTask::StopAnimation()
 {
-  if( mPlayState != PlayState::STOPPING )
+  if(mPlayState != PlayState::STOPPING)
   {
     mNeedAnimationFinishedTrigger = false;
-    mPlayState = PlayState::STOPPING;
+    mPlayState                    = PlayState::STOPPING;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this);
   }
 }
 
 void VectorAnimationTask::PauseAnimation()
 {
-  if( mPlayState == PlayState::PLAYING )
+  if(mPlayState == PlayState::PLAYING)
   {
     mPlayState = PlayState::PAUSED;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this);
   }
 }
 
-void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
+void VectorAnimationTask::SetAnimationFinishedCallback(EventThreadCallback* callback)
 {
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  if( callback )
+  ConditionalWait::ScopedLock lock(mConditionalWait);
+  if(callback)
   {
-    mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
+    mAnimationFinishedTrigger = std::unique_ptr<EventThreadCallback>(callback);
   }
 }
 
-void VectorAnimationTask::SetLoopCount( int32_t count )
+void VectorAnimationTask::SetLoopCount(int32_t count)
 {
-  if( mLoopCount != count )
+  if(mLoopCount != count)
   {
-    mLoopCount = count;
+    mLoopCount   = count;
     mCurrentLoop = 0;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this);
   }
 }
 
-void VectorAnimationTask::SetPlayRange( const Property::Array& playRange )
+void VectorAnimationTask::SetPlayRange(const Property::Array& playRange)
 {
-  bool valid = false;
+  bool     valid      = false;
   uint32_t startFrame = 0, endFrame = 0;
-  size_t count = playRange.Count();
+  size_t   count = playRange.Count();
 
-  if( count >= 2 )
+  if(count >= 2)
   {
     int32_t start = 0, end = 0;
-    if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
+    if(playRange.GetElementAt(0).Get(start) && playRange.GetElementAt(1).Get(end))
     {
-      startFrame = static_cast< uint32_t >( start );
-      endFrame = static_cast< uint32_t >( end );
-      valid = true;
+      startFrame = static_cast<uint32_t>(start);
+      endFrame   = static_cast<uint32_t>(end);
+      valid      = true;
     }
     else
     {
       std::string startMarker, endMarker;
-      if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
+      if(playRange.GetElementAt(0).Get(startMarker) && playRange.GetElementAt(1).Get(endMarker))
       {
-        if( mVectorRenderer )
+        if(mVectorRenderer)
         {
-          uint32_t frame;   // We don't use this later
-          if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
+          uint32_t frame; // We don't use this later
+          if(mVectorRenderer.GetMarkerInfo(startMarker, startFrame, frame) && mVectorRenderer.GetMarkerInfo(endMarker, frame, endFrame))
           {
             valid = true;
           }
@@ -224,88 +270,79 @@ void VectorAnimationTask::SetPlayRange( const Property::Array& playRange )
       }
     }
   }
-  else if( count == 1 )
+  else if(count == 1)
   {
     std::string marker;
-    if( playRange.GetElementAt( 0 ).Get( marker ) )
+    if(playRange.GetElementAt(0).Get(marker))
     {
-      if( mVectorRenderer )
+      if(mVectorRenderer && mVectorRenderer.GetMarkerInfo(marker, startFrame, endFrame))
       {
-        mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
         valid = true;
       }
     }
   }
 
-  if( !valid )
+  if(!valid)
   {
-    DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
+    DALI_LOG_ERROR("VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this);
     return;
   }
 
   // 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;
-        mResourceReady = false;
-      }
-      else if( mEndFrame < mCurrentFrame )
-      {
-        mCurrentFrame = mEndFrame;
-        mResourceReady = false;
-      }
-
-      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);
   }
 }
 
-void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
+void VectorAnimationTask::GetPlayRange(uint32_t& startFrame, uint32_t& endFrame)
 {
   startFrame = mStartFrame;
-  endFrame = mEndFrame;
+  endFrame   = mEndFrame;
 }
 
-void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
+void VectorAnimationTask::SetCurrentFrameNumber(uint32_t frameNumber)
 {
-  if( mCurrentFrame == frameNumber )
+  if(mCurrentFrame == frameNumber)
   {
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this);
     return;
   }
 
-  if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
+  if(frameNumber >= mStartFrame && frameNumber <= mEndFrame)
   {
-    mCurrentFrame = frameNumber;
+    mCurrentFrame      = frameNumber;
     mUpdateFrameNumber = false;
-    mResourceReady = false;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
+    DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this);
   }
   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);
   }
 }
 
@@ -319,118 +356,116 @@ uint32_t VectorAnimationTask::GetTotalFrameNumber() const
   return mTotalFrame;
 }
 
-void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
+void VectorAnimationTask::GetDefaultSize(uint32_t& width, uint32_t& height) const
 {
-  mVectorRenderer.GetDefaultSize( width, height );
+  mVectorRenderer.GetDefaultSize(width, height);
 }
 
-void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
+void VectorAnimationTask::SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior)
 {
   mStopBehavior = stopBehavior;
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
+  DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this);
 }
 
-void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
+void VectorAnimationTask::SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode)
 {
   mLoopingMode = loopingMode;
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
+  DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this);
 }
 
-void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
+void VectorAnimationTask::GetLayerInfo(Property::Map& map) const
 {
-  mVectorRenderer.GetLayerInfo( map );
+  mVectorRenderer.GetLayerInfo(map);
 }
 
-VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
+VectorAnimationTask::ResourceReadySignalType& VectorAnimationTask::ResourceReadySignal()
 {
-  return mVectorRenderer.UploadCompletedSignal();
+  return mResourceReadySignal;
 }
 
-void VectorAnimationTask::Initialize()
+bool VectorAnimationTask::Rasterize(bool& keepAnimation)
 {
-  mVectorRenderer = VectorAnimationRenderer::New( mUrl );
-
-  mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
-
-  mEndFrame = mTotalFrame - 1;
-
-  mFrameRate = mVectorRenderer.GetFrameRate();
-  mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
-
-  uint32_t width, height;
-  mVectorRenderer.GetDefaultSize( width, height );
+  bool     stopped = false;
+  uint32_t currentFrame;
+  keepAnimation = false;
 
-  SetSize( width, height );
+  {
+    ConditionalWait::ScopedLock lock(mConditionalWait);
+    if(mDestroyTask)
+    {
+      // The task will be destroyed. We don't need rasterization.
+      return false;
+    }
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
-}
+    if(mLoadRequest)
+    {
+      return Load();
+    }
+  }
 
-bool VectorAnimationTask::Rasterize()
-{
-  bool stopped = false;
-  uint32_t currentFrame;
+  if(mLoadFailed)
+  {
+    return false;
+  }
 
   ApplyAnimationData();
 
-  if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
+  if(mPlayState == PlayState::PLAYING && mUpdateFrameNumber)
   {
-    mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
-    Dali::ClampInPlace( mCurrentFrame, mStartFrame, mEndFrame );
+    mCurrentFrame = mForward ? mCurrentFrame + mDroppedFrames + 1 : (mCurrentFrame > mDroppedFrames ? mCurrentFrame - mDroppedFrames - 1 : 0);
+    Dali::ClampInPlace(mCurrentFrame, mStartFrame, mEndFrame);
   }
 
   currentFrame = mCurrentFrame;
 
-  // Reset values
-  mResourceReady = true;
   mUpdateFrameNumber = true;
-  mNeedAnimationFinishedTrigger = true;
 
-  if( mPlayState == PlayState::STOPPING )
+  if(mPlayState == PlayState::STOPPING)
   {
-    mCurrentFrame = GetStoppedFrame( mStartFrame, mEndFrame, mCurrentFrame );
-    currentFrame = mCurrentFrame;
-    stopped = true;
+    mCurrentFrame = GetStoppedFrame(mStartFrame, mEndFrame, mCurrentFrame);
+    currentFrame  = mCurrentFrame;
+    stopped       = true;
   }
-  else if( mPlayState == PlayState::PLAYING )
+  else if(mPlayState == PlayState::PLAYING)
   {
     bool animationFinished = false;
 
-    if( currentFrame >= mEndFrame )  // last frame
+    if(currentFrame >= mEndFrame) // last frame
     {
-      if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
+      if(mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE)
       {
         mForward = false;
       }
       else
       {
-        if( mLoopCount < 0 || ++mCurrentLoop < mLoopCount )   // repeat forever or before the last loop
+        if(mLoopCount < 0 || ++mCurrentLoop < mLoopCount) // repeat forever or before the last loop
         {
-          mCurrentFrame = mStartFrame;
+          mCurrentFrame      = mStartFrame;
           mUpdateFrameNumber = false;
         }
         else
         {
-          animationFinished = true;   // end of animation
+          animationFinished = true; // end of animation
         }
       }
     }
-    else if( currentFrame == mStartFrame && !mForward )  // first frame
+    else if(currentFrame == mStartFrame && !mForward) // first frame
     {
-      if( mLoopCount < 0 || ++mCurrentLoop < mLoopCount )   // repeat forever or before the last loop
+      if(mLoopCount < 0 || ++mCurrentLoop < mLoopCount) // repeat forever or before the last loop
       {
         mForward = true;
       }
       else
       {
-        animationFinished = true;   // end of animation
+        animationFinished = true; // end of animation
       }
     }
 
-    if( animationFinished )
+    if(animationFinished)
     {
-      if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
+      if(mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME)
       {
         stopped = true;
       }
@@ -443,53 +478,47 @@ bool VectorAnimationTask::Rasterize()
 
   // Rasterize
   bool renderSuccess = false;
-  if( mVectorRenderer )
+  if(mVectorRenderer)
   {
-    renderSuccess = mVectorRenderer.Render( currentFrame );
-    if( !renderSuccess )
+    renderSuccess = mVectorRenderer.Render(currentFrame);
+    if(!renderSuccess)
     {
-      DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
+      DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this);
       mUpdateFrameNumber = false;
-
-      if( !mResourceReady )
-      {
-        mResourceReady = false;
-      }
     }
   }
 
-  if( stopped && renderSuccess )
+  if(stopped && renderSuccess)
   {
-    mPlayState = PlayState::STOPPED;
-    mForward = true;
+    mPlayState   = PlayState::STOPPED;
+    mForward     = true;
     mCurrentLoop = 0;
 
     // Animation is finished
     {
-      ConditionalWait::ScopedLock lock( mConditionalWait );
-      if( mNeedAnimationFinishedTrigger && mAnimationFinishedTrigger )
+      ConditionalWait::ScopedLock lock(mConditionalWait);
+      if(mNeedAnimationFinishedTrigger && mAnimationFinishedTrigger)
       {
         mAnimationFinishedTrigger->Trigger();
       }
     }
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
+    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 )
+uint32_t VectorAnimationTask::GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame)
 {
   uint32_t frame = currentFrame;
 
-  switch( mStopBehavior )
+  switch(mStopBehavior)
   {
     case DevelImageVisual::StopBehavior::FIRST_FRAME:
     {
@@ -498,7 +527,7 @@ uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t end
     }
     case DevelImageVisual::StopBehavior::LAST_FRAME:
     {
-      if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
+      if(mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE)
       {
         frame = startFrame;
       }
@@ -518,22 +547,37 @@ uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t end
   return frame;
 }
 
-std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
+VectorAnimationTask::TimePoint VectorAnimationTask::CalculateNextFrameTime(bool renderNow)
 {
-  // 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< std::chrono::time_point< std::chrono::system_clock >::duration >(
-      mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
-  auto current = std::chrono::system_clock::now();
-  if( renderNow || mNextFrameStartTime < current )
+  mNextFrameStartTime = std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds));
+  auto current        = std::chrono::steady_clock::now();
+  mDroppedFrames      = 0;
+
+  if(renderNow)
+  {
+    mNextFrameStartTime = current;
+  }
+  else if(mNextFrameStartTime < current)
   {
+    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));
+    }
+
     mNextFrameStartTime = current;
+    mDroppedFrames      = droppedFrames;
   }
+
   return mNextFrameStartTime;
 }
 
-std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
+VectorAnimationTask::TimePoint VectorAnimationTask::GetNextFrameTime()
 {
   return mNextFrameStartTime;
 }
@@ -543,56 +587,61 @@ void VectorAnimationTask::ApplyAnimationData()
   uint32_t index;
 
   {
-    ConditionalWait::ScopedLock lock( mConditionalWait );
+    ConditionalWait::ScopedLock lock(mConditionalWait);
 
-    if( !mAnimationDataUpdated || mAnimationData[mAnimationDataIndex].resendFlag != 0 )
+    if(!mAnimationDataUpdated || mAnimationData[mAnimationDataIndex].resendFlag != 0)
     {
       // Data is not updated or the previous data is not applied yet.
       return;
     }
 
-    mAnimationDataIndex = mAnimationDataIndex == 0 ? 1 : 0;  // Swap index
+    mAnimationDataIndex   = mAnimationDataIndex == 0 ? 1 : 0; // Swap index
     mAnimationDataUpdated = false;
 
     index = mAnimationDataIndex;
   }
 
-  if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT )
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT)
   {
-    SetLoopCount( mAnimationData[index].loopCount );
+    SetLoopCount(mAnimationData[index].loopCount);
   }
 
-  if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE )
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE)
   {
-    SetPlayRange( mAnimationData[index].playRange );
+    SetPlayRange(mAnimationData[index].playRange);
   }
 
-  if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR )
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR)
   {
-    SetStopBehavior( mAnimationData[index].stopBehavior );
+    SetStopBehavior(mAnimationData[index].stopBehavior);
   }
 
-  if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE )
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE)
   {
-    SetLoopingMode( mAnimationData[index].loopingMode );
+    SetLoopingMode(mAnimationData[index].loopingMode);
   }
 
-  if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME )
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME)
   {
-    SetCurrentFrameNumber( mAnimationData[index].currentFrame );
+    SetCurrentFrameNumber(mAnimationData[index].currentFrame);
   }
 
-  if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE )
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_NEED_RESOURCE_READY)
   {
-    if( mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING )
+    mVectorRenderer.InvalidateBuffer();
+  }
+
+  if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
+  {
+    if(mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING)
     {
       PlayAnimation();
     }
-    else if( mAnimationData[index].playState == DevelImageVisual::PlayState::PAUSED )
+    else if(mAnimationData[index].playState == DevelImageVisual::PlayState::PAUSED)
     {
       PauseAnimation();
     }
-    else if( mAnimationData[index].playState == DevelImageVisual::PlayState::STOPPED )
+    else if(mAnimationData[index].playState == DevelImageVisual::PlayState::STOPPED)
     {
       StopAnimation();
     }
@@ -601,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