Add post processor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
index 536c208..9a3f909 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 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/devel-api/adaptor-framework/thread-settings.h>
-#include <dali/integration-api/adaptors/adaptor.h>
+#include <dali/integration-api/adaptor-framework/adaptor.h>
 #include <dali/integration-api/debug.h>
-
-// INTERNAL INCLUDES
+#include <chrono>
+#include <thread>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 namespace
 {
-
-constexpr auto LOOP_FOREVER = -1;
-
 #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
 
-VectorRasterizeThread::VectorRasterizeThread( const std::string& url, Renderer renderer, uint32_t width, uint32_t height )
-: mUrl( url ),
-  mVectorRenderer(),
+VectorRasterizeThread::VectorRasterizeThread()
+: mRasterizeTasks(),
   mConditionalWait(),
-  mMutex(),
-  mResourceReadyTrigger(),
-  mAnimationFinishedTrigger(),
-  mPlayRange( 0.0f, 1.0f ),
-  mPlayState( DevelImageVisual::PlayState::STOPPED ),
-  mCurrentFrame( 0 ),
-  mTotalFrame( 0 ),
-  mStartFrame( 0 ),
-  mEndFrame( 0 ),
-  mWidth( width ),
-  mHeight( height ),
-  mLoopCount( LOOP_FOREVER ),
-  mCurrentLoop( 0 ),
-  mNeedRender( false ),
-  mDestroyThread( false ),
-  mResourceReady( false ),
-  mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
+  mCompletedCallback(),
+  mDestroyThread(false),
+  mIsThreadStarted(false),
+  mLogFactory(Dali::Adaptor::Get().GetLogFactory())
 {
-  mVectorRenderer = VectorAnimationRenderer::New( mUrl, renderer, width, height );
 }
 
 VectorRasterizeThread::~VectorRasterizeThread()
 {
   // Stop the thread
   {
-    ConditionalWait::ScopedLock lock( mConditionalWait );
+    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();
+    mConditionalWait.Notify(lock);
   }
 
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
+  DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this);
 
   Join();
 }
 
-void VectorRasterizeThread::Run()
+void VectorRasterizeThread::SetCompletedCallback(CallbackBase* callback)
 {
-  SetThreadName( "VectorImageThread" );
-  mLogFactory.InstallLogFunction();
-
-  //TODO: check the return value
-  StartRender();
+  ConditionalWait::ScopedLock lock(mConditionalWait);
 
-  while( IsThreadReady() )
-  {
-    Rasterize();
-  }
+  mCompletedCallback = std::unique_ptr<CallbackBase>(callback);
 }
 
-void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
+void VectorRasterizeThread::AddTask(VectorAnimationTaskPtr task)
 {
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  mVectorRenderer.SetSize( width, height );
-
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
-}
-
-void VectorRasterizeThread::StartAnimation()
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  if( mPlayState != DevelImageVisual::PlayState::PLAYING )
-  {
-    if( mPlayState == DevelImageVisual::PlayState::STOPPED )
-    {
-      // Reset the current frame and the current loop
-      mCurrentFrame = mStartFrame;
-      mCurrentLoop = 0;
-    }
-
-    mPlayState = DevelImageVisual::PlayState::PLAYING;
-    mConditionalWait.Notify( lock );
-
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartAnimation: Start\n" );
-  }
-}
+  // Lock while adding task to the queue
+  ConditionalWait::ScopedLock lock(mConditionalWait);
 
-void VectorRasterizeThread::StopAnimation()
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  if( mPlayState != DevelImageVisual::PlayState::STOPPED )
+  if(!mIsThreadStarted)
   {
-    mPlayState = DevelImageVisual::PlayState::STOPPED;
-
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
+    Start();
+    mIsThreadStarted = true;
   }
-}
 
-void VectorRasterizeThread::PauseAnimation()
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  if( mPlayState == DevelImageVisual::PlayState::PLAYING )
+  if(mRasterizeTasks.end() == std::find(mRasterizeTasks.begin(), mRasterizeTasks.end(), task))
   {
-    mPlayState = DevelImageVisual::PlayState::PAUSED;
+    mRasterizeTasks.push_back(task);
 
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
+    // wake up the animation thread
+    mConditionalWait.Notify(lock);
   }
 }
 
-void VectorRasterizeThread::RenderFrame()
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  mNeedRender = true;
-  mConditionalWait.Notify( lock );
-
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
-}
-
-void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
-}
-
-void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-  mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
-}
-
-void VectorRasterizeThread::SetLoopCount( int16_t count )
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-
-  mLoopCount = count;
-
-  // Reset progress
-  mCurrentLoop = 0;
-  mCurrentFrame = mStartFrame;
-}
-
-void VectorRasterizeThread::SetPlayRange( Vector2 range )
+void VectorRasterizeThread::Run()
 {
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-
-  mPlayRange = range;
+  SetThreadName("VectorRasterizeThread");
+  mLogFactory.InstallLogFunction();
 
-  if( mTotalFrame != 0 )
+  while(!mDestroyThread)
   {
-    mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
-    mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
+    Rasterize();
   }
 }
 
-DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState()
-{
-  return mPlayState;
-}
-
-bool VectorRasterizeThread::IsThreadReady()
-{
-  ConditionalWait::ScopedLock lock( mConditionalWait );
-
-  if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
-  {
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
-
-    mConditionalWait.Wait( lock );
-  }
-
-  // Keep the thread alive if this thread is NOT to be destroyed
-  return !mDestroyThread;
-}
-
-bool VectorRasterizeThread::StartRender()
-{
-  //TODO: check the return value
-  mVectorRenderer.StartRender();
-
-  mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
-
-  mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
-  mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
-
-  mCurrentFrame = mStartFrame;
-
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame );
-
-  return true;
-}
-
 void VectorRasterizeThread::Rasterize()
 {
-  DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
-
-  // Rasterize
-  mVectorRenderer.Render( mCurrentFrame );
-
-  if( mPlayState == DevelImageVisual::PlayState::PLAYING )
+  VectorAnimationTaskPtr nextTask;
   {
-    if( ++mCurrentFrame >= mEndFrame )
-    {
-      if( mLoopCount < 0 )
-      {
-        // repeat forever
-        mCurrentFrame = mStartFrame;
-      }
-      else
-      {
-        mCurrentLoop++;
-        if( mCurrentLoop >= mLoopCount )
-        {
-          // Animation is finished
-          mPlayState = DevelImageVisual::PlayState::STOPPED;
+    // Lock while popping task out from the queue
+    ConditionalWait::ScopedLock lock(mConditionalWait);
 
-          mAnimationFinishedTrigger->Trigger();
+    // conditional wait
+    if(mRasterizeTasks.empty())
+    {
+      mConditionalWait.Wait(lock);
+    }
 
-          DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
-        }
-        else
-        {
-          mCurrentFrame = mStartFrame;
-        }
-      }
+    // pop out the next task from the queue
+    if(!mRasterizeTasks.empty())
+    {
+      std::vector<VectorAnimationTaskPtr>::iterator next = mRasterizeTasks.begin();
+      nextTask                                           = *next;
+      mRasterizeTasks.erase(next);
     }
   }
 
-  mNeedRender = false;
-
-  if( !mResourceReady )
+  if(nextTask)
   {
-    DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
+    bool keepAnimation = nextTask->Rasterize();
 
-    mResourceReadyTrigger->Trigger();
-    mResourceReady = true;
+    if(mCompletedCallback)
+    {
+      CallbackBase::Execute(*mCompletedCallback, nextTask, keepAnimation);
+    }
   }
 }