(Vector) Reset the current frame when stopped
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
index da46e1e..543ca0d 100644 (file)
@@ -80,7 +80,7 @@ VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
   mCurrentFrameUpdated( false ),
   mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
 {
-  mVectorRenderer = VectorAnimationRenderer::New( mUrl );
+  Initialize();
 }
 
 VectorRasterizeThread::~VectorRasterizeThread()
@@ -105,13 +105,12 @@ void VectorRasterizeThread::Run()
   SetThreadName( "VectorImageThread" );
   mLogFactory.InstallLogFunction();
 
-  //TODO: check the return value
-  StartRender();
-
   while( !mDestroyThread )
   {
     Rasterize();
   }
+
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread\n" );
 }
 
 void VectorRasterizeThread::SetRenderer( Renderer renderer )
@@ -142,6 +141,7 @@ void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
 void VectorRasterizeThread::PlayAnimation()
 {
   ConditionalWait::ScopedLock lock( mConditionalWait );
+
   if( mPlayState != DevelImageVisual::PlayState::PLAYING )
   {
     mPlayState = DevelImageVisual::PlayState::PLAYING;
@@ -158,6 +158,9 @@ void VectorRasterizeThread::StopAnimation()
   {
     mPlayState = DevelImageVisual::PlayState::STOPPED;
 
+    mCurrentFrame = mStartFrame;
+    mCurrentFrameUpdated = true;
+
     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
   }
 }
@@ -205,6 +208,8 @@ void VectorRasterizeThread::SetLoopCount( int32_t count )
     ConditionalWait::ScopedLock lock( mConditionalWait );
 
     mLoopCount = count;
+
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d]\n", count );
   }
 }
 
@@ -231,27 +236,26 @@ void VectorRasterizeThread::SetPlayRange( Vector2 range )
 
       mPlayRange = orderedRange;
 
-      if( mTotalFrame != 0 )
-      {
-        mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
-        mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
+      mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
+      mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
 
-        // If the current frame is out of the range, change the current frame also.
-        if( mStartFrame > mCurrentFrame )
-        {
-          mCurrentFrame = mStartFrame;
+      // If the current frame is out of the range, change the current frame also.
+      if( mStartFrame > mCurrentFrame )
+      {
+        mCurrentFrame = mStartFrame;
 
-          mCurrentFrameUpdated = true;
-          mResourceReady = false;
-        }
-        else if( mEndFrame < mCurrentFrame )
-        {
-          mCurrentFrame = mEndFrame;
+        mCurrentFrameUpdated = true;
+        mResourceReady = false;
+      }
+      else if( mEndFrame < mCurrentFrame )
+      {
+        mCurrentFrame = mEndFrame;
 
-          mCurrentFrameUpdated = true;
-          mResourceReady = false;
-        }
+        mCurrentFrameUpdated = true;
+        mResourceReady = false;
       }
+
+      DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d]\n", mStartFrame, mEndFrame );
     }
   }
 }
@@ -269,11 +273,8 @@ void VectorRasterizeThread::SetCurrentProgress( float progress )
   {
     mProgress = progress;
 
-    if( mTotalFrame != 0 )
-    {
-      mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
-      mCurrentFrameUpdated = true;
-    }
+    mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
+    mCurrentFrameUpdated = true;
 
     mResourceReady = false;
 
@@ -286,6 +287,11 @@ float VectorRasterizeThread::GetCurrentProgress() const
   return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
 }
 
+void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
+{
+  mVectorRenderer.GetDefaultSize( width, height );
+}
+
 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
 {
   return mPlayState;
@@ -296,10 +302,9 @@ bool VectorRasterizeThread::IsResourceReady() const
   return mResourceReady;
 }
 
-bool VectorRasterizeThread::StartRender()
+void VectorRasterizeThread::Initialize()
 {
-  //TODO: check the return value
-  mVectorRenderer.StartRender();
+  mVectorRenderer = VectorAnimationRenderer::New( mUrl );
 
   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
 
@@ -311,9 +316,12 @@ bool VectorRasterizeThread::StartRender()
   mFrameRate = mVectorRenderer.GetFrameRate();
   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d, %f fps]\n", mTotalFrame, mFrameRate );
+  uint32_t width, height;
+  mVectorRenderer.GetDefaultSize( width, height );
+
+  SetSize( width, height );
 
-  return true;
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
 }
 
 void VectorRasterizeThread::Rasterize()
@@ -332,8 +340,7 @@ void VectorRasterizeThread::Rasterize()
 
       if( mPlayState == DevelImageVisual::PlayState::STOPPED )
       {
-        // Reset the current frame and the current loop
-        mCurrentFrame = mStartFrame;
+        // Reset the current loop
         mCurrentLoop = 0;
       }
       mConditionalWait.Wait( lock );
@@ -372,6 +379,8 @@ void VectorRasterizeThread::Rasterize()
         {
           mPlayState = DevelImageVisual::PlayState::STOPPED;
 
+          ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
+
           // Animation is finished
           mAnimationFinishedTrigger->Trigger();
 
@@ -397,7 +406,7 @@ void VectorRasterizeThread::Rasterize()
 #if defined(DEBUG_ENABLED)
   auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", currentFrame, sleepDuration.count() );
 #endif
 
   std::this_thread::sleep_until( timeToSleepUntil );