[dali_1.4.41] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
index 70a3302..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();
@@ -93,12 +95,9 @@ VectorRasterizeThread::~VectorRasterizeThread()
     ConditionalWait::ScopedLock lock( mConditionalWait );
     mDestroyThread = true;
     mConditionalWait.Notify( lock );
-
-    // This should be called in the main thread to stop waiting for the dequeuable buffer.
-    mVectorRenderer.StopRender();
   }
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this );
 
   Join();
 }
@@ -113,7 +112,7 @@ void VectorRasterizeThread::Run()
     Rasterize();
   }
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread\n" );
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread [%p]\n", this );
 }
 
 void VectorRasterizeThread::SetRenderer( Renderer renderer )
@@ -122,7 +121,7 @@ void VectorRasterizeThread::SetRenderer( Renderer renderer )
 
   mVectorRenderer.SetRenderer( renderer );
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer [%p]\n", this );
 }
 
 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
@@ -137,7 +136,7 @@ void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
 
     mResourceReady = false;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d [%p]\n", width, height, this );
   }
 }
 
@@ -152,7 +151,7 @@ void VectorRasterizeThread::PlayAnimation()
     mPlayState = PlayState::PLAYING;
     mConditionalWait.Notify( lock );
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play\n" );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play [%p]\n", this );
   }
 }
 
@@ -162,10 +161,11 @@ void VectorRasterizeThread::StopAnimation()
   if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
   {
     mNeedRender = true;
+    mNeedAnimationFinishedTrigger = false;
     mPlayState = PlayState::STOPPING;
     mConditionalWait.Notify( lock );
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop [%p]\n", this );
   }
 }
 
@@ -176,7 +176,7 @@ void VectorRasterizeThread::PauseAnimation()
   {
     mPlayState = PlayState::PAUSED;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause [%p]\n", this );
   }
 }
 
@@ -189,16 +189,10 @@ void VectorRasterizeThread::RenderFrame()
     mNeedRender = true;
     mConditionalWait.Notify( lock );
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render [%p]\n", this );
   }
 }
 
-void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
-}
-
 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
 {
   ConditionalWait::ScopedLock lock( mConditionalWait );
@@ -212,8 +206,10 @@ 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]\n", count );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d] [%p]\n", count, this );
   }
 }
 
@@ -253,7 +249,7 @@ void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame
         mResourceReady = false;
       }
 
-      DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRangeInFrame: [%d, %d]\n", mStartFrame, mEndFrame );
+      DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
     }
   }
 }
@@ -294,6 +290,12 @@ void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
 {
   ConditionalWait::ScopedLock lock( mConditionalWait );
 
+  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;
@@ -301,7 +303,7 @@ void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
 
     mResourceReady = false;
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %d\n", mCurrentFrame );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
   }
   else
   {
@@ -329,7 +331,7 @@ void VectorRasterizeThread::SetStopBehavior( DevelImageVisual::StopBehavior::Typ
   ConditionalWait::ScopedLock lock( mConditionalWait );
   mStopBehavior = stopBehavior;
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d\n", mStopBehavior );
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
 }
 
 void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
@@ -337,7 +339,12 @@ void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type
   ConditionalWait::ScopedLock lock( mConditionalWait );
   mLoopingMode = loopingMode;
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d\n", mLoopingMode );
+  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()
@@ -356,21 +363,21 @@ void VectorRasterizeThread::Initialize()
 
   SetSize( width, height );
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
 }
 
 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 );
 
     if( ( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED ) && !mNeedRender && !mDestroyThread )
     {
-      DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
+      DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait [%p]\n", this );
       mConditionalWait.Wait( lock );
     }
 
@@ -379,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();
@@ -412,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;
@@ -421,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;
       }
@@ -433,6 +444,7 @@ void VectorRasterizeThread::Rasterize()
       {
         animationFinished = true;   // end of animation
       }
+      ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
     }
 
     if( animationFinished )
@@ -449,13 +461,10 @@ void VectorRasterizeThread::Rasterize()
   }
 
   // Rasterize
-  mVectorRenderer.Render( currentFrame );
-
-  if( !resourceReady )
+  if( !mVectorRenderer.Render( currentFrame ) )
   {
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
-
-    mResourceReadyTrigger->Trigger();
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
+    mUpdateFrameNumber = false;
   }
 
   if( stopped )
@@ -465,9 +474,12 @@ 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\n" );
+    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
   }
 
   auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
@@ -475,7 +487,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", currentFrame, sleepDuration.count() );
+  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld] [%p]\n", currentFrame, sleepDuration.count(), this );
 #endif
 
   std::this_thread::sleep_until( timeToSleepUntil );