(Vector) Ensure not to add duplicated task 57/237857/2
authorHeeyong Song <heeyong.song@samsung.com>
Mon, 6 Jul 2020 06:41:28 +0000 (15:41 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Wed, 8 Jul 2020 05:46:18 +0000 (14:46 +0900)
Change-Id: I3f023b6615ca36b40ddc3534fd8faab7985eb08e

automated-tests/src/dali-toolkit/utc-Dali-AnimatedVectorImageVisual.cpp
dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.cpp
dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h
dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.cpp
dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h

index 90bb27b..10f7dfa 100644 (file)
@@ -270,6 +270,8 @@ int UtcDaliAnimatedVectorImageVisualGetPropertyMap01(void)
   application.SendNotification();
   application.Render();
 
+  std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) );    // wait for next rasterize thread run
+
   Property::Map resultMap;
   resultMap = actor.GetProperty< Property::Map >( DummyControl::Property::TEST_VISUAL );
 
@@ -679,6 +681,8 @@ int UtcDaliAnimatedVectorImageVisualPlayRange(void)
   application.SendNotification();
   application.Render();
 
+  std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) );    // wait for next rasterize thread run
+
   map = actor.GetProperty< Property::Map >( DummyControl::Property::TEST_VISUAL );
   value = map.Find( DevelImageVisual::Property::PLAY_RANGE );
 
index 55968e1..bae4fbc 100644 (file)
@@ -70,7 +70,6 @@ VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, cons
   mAnimationDataIndex( 0 ),
   mLoopCount( LOOP_FOREVER ),
   mCurrentLoop( 0 ),
-  mResourceReady( false ),
   mForward( true ),
   mUpdateFrameNumber( false ),
   mNeedAnimationFinishedTrigger( true ),
@@ -138,8 +137,6 @@ void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
     mWidth = width;
     mHeight = height;
 
-    mResourceReady = false;
-
     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
   }
 }
@@ -148,6 +145,7 @@ void VectorAnimationTask::PlayAnimation()
 {
   if( mPlayState != PlayState::PLAYING )
   {
+    mNeedAnimationFinishedTrigger = true;
     mUpdateFrameNumber = false;
     mPlayState = PlayState::PLAYING;
 
@@ -266,12 +264,10 @@ void VectorAnimationTask::SetPlayRange( const Property::Array& playRange )
       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 );
@@ -302,7 +298,6 @@ void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
   {
     mCurrentFrame = frameNumber;
     mUpdateFrameNumber = false;
-    mResourceReady = false;
 
     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
   }
@@ -394,10 +389,7 @@ bool VectorAnimationTask::Rasterize()
 
   currentFrame = mCurrentFrame;
 
-  // Reset values
-  mResourceReady = true;
   mUpdateFrameNumber = true;
-  mNeedAnimationFinishedTrigger = true;
 
   if( mPlayState == PlayState::STOPPING )
   {
@@ -462,11 +454,6 @@ bool VectorAnimationTask::Rasterize()
     {
       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
       mUpdateFrameNumber = false;
-
-      if( !mResourceReady )
-      {
-        mResourceReady = false;
-      }
     }
   }
 
index 79c3ee2..844d378 100644 (file)
@@ -309,7 +309,6 @@ private:
   uint32_t                               mAnimationDataIndex;
   int32_t                                mLoopCount;
   int32_t                                mCurrentLoop;
-  bool                                   mResourceReady;
   bool                                   mForward;
   bool                                   mUpdateFrameNumber;
   bool                                   mNeedAnimationFinishedTrigger;
index 8403e6a..c80e00a 100644 (file)
@@ -59,6 +59,7 @@ Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging,
 VectorAnimationThread::VectorAnimationThread()
 : mAnimationTasks(),
   mCompletedTasks(),
+  mWorkingTasks(),
   mRasterizers( GetNumberOfThreads( NUMBER_OF_RASTERIZE_THREADS_ENV, DEFAULT_NUMBER_OF_RASTERIZE_THREADS ), [&]() { return RasterizeHelper( *this ); } ),
   mSleepThread( MakeCallback( this, &VectorAnimationThread::OnAwakeFromSleep ) ),
   mConditionalWait(),
@@ -75,6 +76,7 @@ VectorAnimationThread::~VectorAnimationThread()
   {
     ConditionalWait::ScopedLock lock( mConditionalWait );
     mDestroyThread = true;
+    mNeedToSleep = false;
     mConditionalWait.Notify( lock );
   }
 
@@ -108,6 +110,7 @@ void VectorAnimationThread::AddTask( VectorAnimationTaskPtr task )
       mAnimationTasks.push_back( task );
     }
 
+    mNeedToSleep = false;
     // wake up the animation thread
     mConditionalWait.Notify( lock );
   }
@@ -115,14 +118,35 @@ void VectorAnimationThread::AddTask( VectorAnimationTaskPtr task )
 
 void VectorAnimationThread::OnTaskCompleted( VectorAnimationTaskPtr task, bool keepAnimation )
 {
-  if( keepAnimation && !mDestroyThread )
+  if( !mDestroyThread )
   {
     ConditionalWait::ScopedLock lock( mConditionalWait );
+    bool needRasterize = false;
+
+    auto workingTask = std::find( mWorkingTasks.begin(), mWorkingTasks.end(), task );
+    if( workingTask != mWorkingTasks.end() )
+    {
+      mWorkingTasks.erase( workingTask );
+    }
 
-    if( mCompletedTasks.end() == std::find( mCompletedTasks.begin(), mCompletedTasks.end(), task ) )
+    // Check pending task
+    if( mAnimationTasks.end() != std::find( mAnimationTasks.begin(), mAnimationTasks.end(), task ) )
     {
-      mCompletedTasks.push_back( task );
+      needRasterize = true;
+    }
+
+    if( keepAnimation )
+    {
+      if( mCompletedTasks.end() == std::find( mCompletedTasks.begin(), mCompletedTasks.end(), task ) )
+      {
+        mCompletedTasks.push_back( task );
+        needRasterize = true;
+      }
+    }
 
+    if( needRasterize )
+    {
+      mNeedToSleep = false;
       // wake up the animation thread
       mConditionalWait.Notify( lock );
     }
@@ -156,12 +180,12 @@ void VectorAnimationThread::Rasterize()
   ConditionalWait::ScopedLock lock( mConditionalWait );
 
   // conditional wait
-  if( (mAnimationTasks.empty() && mCompletedTasks.empty() ) || mNeedToSleep )
+  if( mNeedToSleep )
   {
     mConditionalWait.Wait( lock );
   }
 
-  mNeedToSleep = false;
+  mNeedToSleep = true;
 
   // Process completed tasks
   for( auto&& task : mCompletedTasks )
@@ -192,10 +216,9 @@ void VectorAnimationThread::Rasterize()
   mCompletedTasks.clear();
 
   // pop out the next task from the queue
-  while( !mAnimationTasks.empty() && !mNeedToSleep )
+  for( auto it = mAnimationTasks.begin(); it != mAnimationTasks.end(); )
   {
-    std::vector< VectorAnimationTaskPtr >::iterator next = mAnimationTasks.begin();
-    VectorAnimationTaskPtr nextTask = *next;
+    VectorAnimationTaskPtr nextTask = *it;
 
     auto currentTime = std::chrono::system_clock::now();
     auto nextFrameTime = nextTask->GetNextFrameTime();
@@ -208,17 +231,28 @@ void VectorAnimationThread::Rasterize()
 
     if( nextFrameTime <= currentTime )
     {
-      mAnimationTasks.erase( next );
+      // If the task is not in the working list
+      if( std::find( mWorkingTasks.begin(), mWorkingTasks.end(), nextTask ) == mWorkingTasks.end() )
+      {
+        it = mAnimationTasks.erase( it );
+
+        // Add it to the working list
+        mWorkingTasks.push_back( nextTask );
 
-      auto rasterizerHelperIt = mRasterizers.GetNext();
-      DALI_ASSERT_ALWAYS( rasterizerHelperIt != mRasterizers.End() );
+        auto rasterizerHelperIt = mRasterizers.GetNext();
+        DALI_ASSERT_ALWAYS( rasterizerHelperIt != mRasterizers.End() );
 
-      rasterizerHelperIt->Rasterize( nextTask );
+        rasterizerHelperIt->Rasterize( nextTask );
+      }
+      else
+      {
+        it++;
+      }
     }
     else
     {
-      mNeedToSleep = true;
       mSleepThread.SleepUntil( nextFrameTime );
+      break;
     }
   }
 }
index 8d3dd64..964f662 100755 (executable)
@@ -183,6 +183,7 @@ private:
 
   std::vector< VectorAnimationTaskPtr >      mAnimationTasks;
   std::vector< VectorAnimationTaskPtr >      mCompletedTasks;
+  std::vector< VectorAnimationTaskPtr >      mWorkingTasks;
   RoundRobinContainerView< RasterizeHelper > mRasterizers;
   SleepThread                                mSleepThread;
   ConditionalWait                            mConditionalWait;