#include <dali/devel-api/adaptor-framework/thread-settings.h>
#include <dali/integration-api/adaptors/adaptor.h>
#include <dali/integration-api/debug.h>
-
-// INTERNAL INCLUDES
+#include <chrono>
+#include <thread>
namespace Dali
{
{
constexpr auto LOOP_FOREVER = -1;
+constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
#if defined(DEBUG_ENABLED)
Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
} // unnamed namespace
-VectorRasterizeThread::VectorRasterizeThread( const std::string& url, Renderer renderer, uint32_t width, uint32_t height )
+VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
: mUrl( url ),
mVectorRenderer(),
mConditionalWait(),
mAnimationFinishedTrigger(),
mPlayRange( 0.0f, 1.0f ),
mPlayState( DevelImageVisual::PlayState::STOPPED ),
+ mFrameDurationNanoSeconds( 0 ),
+ mProgress( 0.0f ),
+ mFrameRate( 60.0f ),
mCurrentFrame( 0 ),
mTotalFrame( 0 ),
mStartFrame( 0 ),
mEndFrame( 0 ),
- mWidth( width ),
- mHeight( height ),
+ mWidth( 0 ),
+ mHeight( 0 ),
mLoopCount( LOOP_FOREVER ),
mCurrentLoop( 0 ),
mNeedRender( false ),
mResourceReady( false ),
mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
{
- mVectorRenderer = VectorAnimationRenderer::New( mUrl, renderer, width, height );
+ mVectorRenderer = VectorAnimationRenderer::New( mUrl );
}
VectorRasterizeThread::~VectorRasterizeThread()
}
}
-void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
+void VectorRasterizeThread::SetRenderer( Renderer renderer )
{
ConditionalWait::ScopedLock lock( mConditionalWait );
- mVectorRenderer.SetSize( width, height );
- DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
+ mVectorRenderer.SetRenderer( renderer );
+
+ DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
}
-void VectorRasterizeThread::StartAnimation()
+void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
+{
+ if( mWidth != width || mHeight != height )
+ {
+ ConditionalWait::ScopedLock lock( mConditionalWait );
+ mVectorRenderer.SetSize( width, height );
+
+ mWidth = width;
+ mHeight = height;
+
+ mResourceReady = false;
+
+ DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
+ }
+}
+
+void VectorRasterizeThread::PlayAnimation()
{
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" );
+ DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
}
}
{
mPlayState = DevelImageVisual::PlayState::STOPPED;
+ // Reset the current frame and the current loop
+ mCurrentFrame = mStartFrame;
+ mCurrentLoop = 0;
+
DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
}
}
void VectorRasterizeThread::RenderFrame()
{
ConditionalWait::ScopedLock lock( mConditionalWait );
- mNeedRender = true;
- mConditionalWait.Notify( lock );
- DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
+ if( !mResourceReady )
+ {
+ mNeedRender = true;
+ mConditionalWait.Notify( lock );
+
+ DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
+ }
}
void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
}
-void VectorRasterizeThread::SetLoopCount( int16_t count )
+void VectorRasterizeThread::SetLoopCount( int32_t count )
{
- ConditionalWait::ScopedLock lock( mConditionalWait );
+ if( mLoopCount != count )
+ {
+ ConditionalWait::ScopedLock lock( mConditionalWait );
- mLoopCount = count;
+ mLoopCount = count;
- // Reset progress
- mCurrentLoop = 0;
- mCurrentFrame = mStartFrame;
+ // Reset progress
+ mCurrentLoop = 0;
+ }
+}
+
+int32_t VectorRasterizeThread::GetLoopCount() const
+{
+ return mLoopCount;
}
void VectorRasterizeThread::SetPlayRange( Vector2 range )
{
- ConditionalWait::ScopedLock lock( mConditionalWait );
+ // Make sure the range specified is between 0.0 and 1.0
+ if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
+ {
+ Vector2 orderedRange( range );
+ // If the range is not in order swap values
+ if( range.x > range.y )
+ {
+ orderedRange = Vector2( range.y, range.x );
+ }
+
+ if( mPlayRange != orderedRange )
+ {
+ ConditionalWait::ScopedLock lock( mConditionalWait );
+
+ 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 );
- mPlayRange = range;
+ // If the current frame is out of the range, change the current frame also.
+ if( mStartFrame > mCurrentFrame )
+ {
+ mCurrentFrame = mStartFrame;
- if( mTotalFrame != 0 )
+ mResourceReady = false;
+ }
+ else if( mEndFrame < mCurrentFrame )
+ {
+ mCurrentFrame = mEndFrame;
+
+ mResourceReady = false;
+ }
+ }
+ }
+ }
+}
+
+Vector2 VectorRasterizeThread::GetPlayRange() const
+{
+ return mPlayRange;
+}
+
+void VectorRasterizeThread::SetCurrentProgress( float progress )
+{
+ ConditionalWait::ScopedLock lock( mConditionalWait );
+
+ if( progress >= mPlayRange.x && progress <= mPlayRange.y )
{
- mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
- mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
+ mProgress = progress;
+
+ if( mTotalFrame != 0 )
+ {
+ mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
+ }
+
+ mResourceReady = false;
+
+ DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
}
}
-DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState()
+float VectorRasterizeThread::GetCurrentProgress() const
+{
+ return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
+}
+
+DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
{
return mPlayState;
}
+bool VectorRasterizeThread::IsResourceReady() const
+{
+ return mResourceReady;
+}
+
bool VectorRasterizeThread::IsThreadReady()
{
ConditionalWait::ScopedLock lock( mConditionalWait );
mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
- mCurrentFrame = mStartFrame;
+ mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
- DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame );
+ 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 );
return true;
}
void VectorRasterizeThread::Rasterize()
{
- DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
+ bool needRender, resourceReady;
+
+ {
+ ConditionalWait::ScopedLock lock( mConditionalWait );
+ needRender = mNeedRender;
+ resourceReady = mResourceReady;
+ }
+
+ auto currentFrameStartTime = std::chrono::system_clock::now();
// Rasterize
mVectorRenderer.Render( mCurrentFrame );
// Animation is finished
mPlayState = DevelImageVisual::PlayState::STOPPED;
+ // Reset the current frame and the current loop
+ mCurrentFrame = mStartFrame;
+ mCurrentLoop = 0;
+
mAnimationFinishedTrigger->Trigger();
DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
}
}
- mNeedRender = false;
+ if( needRender )
+ {
+ mNeedRender = false;
+ }
- if( !mResourceReady )
+ if( !resourceReady )
{
DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
mResourceReadyTrigger->Trigger();
mResourceReady = true;
}
+
+ auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
+
+#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() );
+#endif
+
+ std::this_thread::sleep_until( timeToSleepUntil );
}
} // namespace Internal