(Vector) Fix some issues
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
index 9469dae..2c39333 100644 (file)
@@ -44,7 +44,8 @@ constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
 #endif
 
-inline void ResetValue( bool& updated, uint32_t& value, uint32_t newValue, ConditionalWait& conditionalWait )
+template< typename T >
+inline void ResetValue( bool& updated, T& value, T newValue, ConditionalWait& conditionalWait )
 {
   ConditionalWait::ScopedLock lock( conditionalWait );
   if( !updated )
@@ -60,7 +61,6 @@ VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
 : mUrl( url ),
   mVectorRenderer(),
   mConditionalWait(),
-  mResourceReadyTrigger(),
   mAnimationFinishedTrigger(),
   mPlayState( PlayState::STOPPED ),
   mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
@@ -79,8 +79,10 @@ VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
   mDestroyThread( false ),
   mResourceReady( false ),
   mCurrentFrameUpdated( false ),
+  mCurrentLoopUpdated( false ),
   mForward( true ),
   mUpdateFrameNumber( false ),
+  mNeedAnimationFinishedTrigger( true ),
   mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
 {
   Initialize();
@@ -159,6 +161,7 @@ void VectorRasterizeThread::StopAnimation()
   if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
   {
     mNeedRender = true;
+    mNeedAnimationFinishedTrigger = false;
     mPlayState = PlayState::STOPPING;
     mConditionalWait.Notify( lock );
 
@@ -190,12 +193,6 @@ void VectorRasterizeThread::RenderFrame()
   }
 }
 
-void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
-}
-
 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
 {
   ConditionalWait::ScopedLock lock( mConditionalWait );
@@ -209,6 +206,8 @@ void VectorRasterizeThread::SetLoopCount( int32_t count )
     ConditionalWait::ScopedLock lock( mConditionalWait );
 
     mLoopCount = count;
+    mCurrentLoop = 0;
+    mCurrentLoopUpdated = true;
 
     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d] [%p]\n", count, this );
   }
@@ -291,7 +290,13 @@ void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
 {
   ConditionalWait::ScopedLock lock( mConditionalWait );
 
-  if( frameNumber >= mStartFrame && frameNumber <= mEndFrame && mCurrentFrame != frameNumber )
+  if( mCurrentFrame == frameNumber )
+  {
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
+    return;
+  }
+
+  if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
   {
     mCurrentFrame = frameNumber;
     mCurrentFrameUpdated = true;
@@ -337,6 +342,11 @@ void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type
   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
 }
 
+VectorRasterizeThread::UploadCompletedSignalType& VectorRasterizeThread::UploadCompletedSignal()
+{
+  return mVectorRenderer.UploadCompletedSignal();
+}
+
 void VectorRasterizeThread::Initialize()
 {
   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
@@ -358,9 +368,9 @@ void VectorRasterizeThread::Initialize()
 
 void VectorRasterizeThread::Rasterize()
 {
-  bool resourceReady, stopped = false;
+  bool stopped = false, needAnimationFinishedTrigger;
   uint32_t currentFrame, startFrame, endFrame;
-  int32_t loopCount;
+  int32_t loopCount, currentLoopCount;
 
   {
     ConditionalWait::ScopedLock lock( mConditionalWait );
@@ -376,16 +386,19 @@ void VectorRasterizeThread::Rasterize()
       mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
     }
 
-    resourceReady = mResourceReady;
     currentFrame = mCurrentFrame;
     startFrame = mStartFrame;
     endFrame = mEndFrame;
     loopCount = mLoopCount;
+    currentLoopCount = mCurrentLoop;
+    needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
 
-    mNeedRender = false;
     mResourceReady = true;
+    mNeedRender = false;
     mCurrentFrameUpdated = false;
+    mCurrentLoopUpdated = false;
     mUpdateFrameNumber = true;
+    mNeedAnimationFinishedTrigger = true;
   }
 
   auto currentFrameStartTime = std::chrono::system_clock::now();
@@ -409,7 +422,7 @@ void VectorRasterizeThread::Rasterize()
       }
       else
       {
-        if( loopCount < 0 || ++mCurrentLoop < loopCount )   // repeat forever or before the last loop
+        if( loopCount < 0 || ++currentLoopCount < loopCount )   // repeat forever or before the last loop
         {
           ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );  // If the current frame is changed in the event thread, don't overwrite it.
           mUpdateFrameNumber = false;
@@ -418,11 +431,12 @@ void VectorRasterizeThread::Rasterize()
         {
           animationFinished = true;   // end of animation
         }
+        ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
       }
     }
     else if( currentFrame == startFrame && !mForward )  // first frame
     {
-      if( loopCount < 0 || ++mCurrentLoop < loopCount )   // repeat forever or before the last loop
+      if( loopCount < 0 || ++currentLoopCount < loopCount )   // repeat forever or before the last loop
       {
         mForward = true;
       }
@@ -430,6 +444,7 @@ void VectorRasterizeThread::Rasterize()
       {
         animationFinished = true;   // end of animation
       }
+      ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
     }
 
     if( animationFinished )
@@ -452,13 +467,6 @@ void VectorRasterizeThread::Rasterize()
     mUpdateFrameNumber = false;
   }
 
-  if( !resourceReady )
-  {
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger [current = %d] [%p]\n", currentFrame, this );
-
-    mResourceReadyTrigger->Trigger();
-  }
-
   if( stopped )
   {
     mPlayState = PlayState::STOPPED;
@@ -466,7 +474,10 @@ void VectorRasterizeThread::Rasterize()
     mCurrentLoop = 0;
 
     // Animation is finished
-    mAnimationFinishedTrigger->Trigger();
+    if( needAnimationFinishedTrigger )
+    {
+      mAnimationFinishedTrigger->Trigger();
+    }
 
     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
   }