X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fanimated-vector-image%2Fvector-rasterize-thread.cpp;h=038b608484bccc4907c83b452d3dd796a0801788;hp=35f485f952577353732c47ca976931c395eb2f4d;hb=9768810e114ae68868a124aa8506f2f4e64fe9d1;hpb=7c1ca2d9ab3bc63f3dc164c4f5d4ea0d8bf5ba3e diff --git a/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.cpp b/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.cpp index 35f485f..038b608 100644 --- a/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.cpp +++ b/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * Copyright (c) 2019 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. @@ -22,8 +22,8 @@ #include #include #include - -// INTERNAL INCLUDES +#include +#include namespace Dali { @@ -38,23 +38,32 @@ namespace { 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" ); #endif +inline void ResetToStart( bool& updated, uint32_t& value, uint32_t startValue, ConditionalWait& conditionalWait ) +{ + ConditionalWait::ScopedLock lock( conditionalWait ); + if( !updated ) + { + value = startValue; + } +} + } // unnamed namespace VectorRasterizeThread::VectorRasterizeThread( const std::string& url ) : mUrl( url ), mVectorRenderer(), mConditionalWait(), - mMutex(), mResourceReadyTrigger(), mAnimationFinishedTrigger(), - mPlayRange( 0.0f, 1.0f ), mPlayState( DevelImageVisual::PlayState::STOPPED ), - mProgress( 0.0f ), + mFrameDurationNanoSeconds( 0 ), + mFrameRate( 60.0f ), mCurrentFrame( 0 ), mTotalFrame( 0 ), mStartFrame( 0 ), @@ -66,9 +75,10 @@ VectorRasterizeThread::VectorRasterizeThread( const std::string& url ) mNeedRender( false ), mDestroyThread( false ), mResourceReady( false ), + mCurrentFrameUpdated( false ), mLogFactory( Dali::Adaptor::Get().GetLogFactory() ) { - mVectorRenderer = VectorAnimationRenderer::New( mUrl ); + Initialize(); } VectorRasterizeThread::~VectorRasterizeThread() @@ -93,13 +103,12 @@ void VectorRasterizeThread::Run() SetThreadName( "VectorImageThread" ); mLogFactory.InstallLogFunction(); - //TODO: check the return value - StartRender(); - - while( IsThreadReady() ) + while( !mDestroyThread ) { Rasterize(); } + + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread\n" ); } void VectorRasterizeThread::SetRenderer( Renderer renderer ) @@ -130,15 +139,9 @@ void VectorRasterizeThread::SetSize( uint32_t width, uint32_t 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 ); @@ -153,6 +156,9 @@ void VectorRasterizeThread::StopAnimation() { mPlayState = DevelImageVisual::PlayState::STOPPED; + mCurrentFrame = mStartFrame; + mCurrentFrameUpdated = true; + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" ); } } @@ -171,10 +177,14 @@ void VectorRasterizeThread::PauseAnimation() 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 ) @@ -191,47 +201,89 @@ void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* c 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; + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d]\n", count ); + } } -int32_t VectorRasterizeThread::GetLoopCount() const +void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame ) { - return mLoopCount; + // Make sure the range specified is between 0 and the total frame number + if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) ) + { + // If the range is not in order swap values + if( startFrame > endFrame ) + { + uint32_t temp = startFrame; + startFrame = endFrame; + endFrame = temp; + } + + if( startFrame != mStartFrame || endFrame != mEndFrame ) + { + ConditionalWait::ScopedLock lock( mConditionalWait ); + + mStartFrame = startFrame; + mEndFrame = endFrame; + + // If the current frame is out of the range, change the current frame also. + if( mStartFrame > mCurrentFrame ) + { + mCurrentFrame = mStartFrame; + + mCurrentFrameUpdated = true; + mResourceReady = false; + } + else if( mEndFrame < mCurrentFrame ) + { + mCurrentFrame = mEndFrame; + + mCurrentFrameUpdated = true; + mResourceReady = false; + } + + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRangeInFrame: [%d, %d]\n", mStartFrame, mEndFrame ); + } + } } -void VectorRasterizeThread::SetPlayRange( Vector2 range ) +void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber ) { 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 ) + if( frameNumber >= mStartFrame && frameNumber <= mEndFrame ) { - Vector2 orderedRange( range ); - // If the range is not in order swap values - if( range.x > range.y ) - { - orderedRange = Vector2( range.y, range.x ); - } + mCurrentFrame = frameNumber; + mCurrentFrameUpdated = true; - mPlayRange = orderedRange; + mResourceReady = false; - if( mTotalFrame != 0 ) - { - mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f ); - mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f ); - } + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %f (%d)\n", mCurrentFrame ); + } + else + { + DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame ); } } -Vector2 VectorRasterizeThread::GetPlayRange() const +uint32_t VectorRasterizeThread::GetCurrentFrameNumber() const +{ + return mCurrentFrame; +} + +uint32_t VectorRasterizeThread::GetTotalFrameNumber() const +{ + return mTotalFrame; +} + +void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const { - return mPlayRange; + mVectorRenderer.GetDefaultSize( width, height ); } DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const @@ -244,83 +296,111 @@ bool VectorRasterizeThread::IsResourceReady() const return mResourceReady; } -bool VectorRasterizeThread::IsThreadReady() +void VectorRasterizeThread::Initialize() { - ConditionalWait::ScopedLock lock( mConditionalWait ); + mVectorRenderer = VectorAnimationRenderer::New( mUrl ); - if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread ) - { - DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" ); + mTotalFrame = mVectorRenderer.GetTotalFrameNumber(); - mConditionalWait.Wait( lock ); - } + mEndFrame = mTotalFrame; + + mFrameRate = mVectorRenderer.GetFrameRate(); + mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate; + + uint32_t width, height; + mVectorRenderer.GetDefaultSize( width, height ); + + SetSize( width, height ); - // Keep the thread alive if this thread is NOT to be destroyed - return !mDestroyThread; + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate ); } -bool VectorRasterizeThread::StartRender() +void VectorRasterizeThread::Rasterize() { - //TODO: check the return value - mVectorRenderer.StartRender(); + bool resourceReady; + uint32_t currentFrame, startFrame, endFrame; + int32_t loopCount; + DevelImageVisual::PlayState playState; - mTotalFrame = mVectorRenderer.GetTotalFrameNumber(); + { + ConditionalWait::ScopedLock lock( mConditionalWait ); - mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f ); - mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f ); + if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread ) + { + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" ); - mCurrentFrame = mStartFrame; + if( mPlayState == DevelImageVisual::PlayState::STOPPED ) + { + // Reset the current loop + mCurrentLoop = 0; + } + mConditionalWait.Wait( lock ); + } - DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame ); + resourceReady = mResourceReady; + currentFrame = mCurrentFrame++; + startFrame = mStartFrame; + endFrame = mEndFrame; + loopCount = mLoopCount; + playState = mPlayState; - return true; -} + mNeedRender = false; + mResourceReady = true; + mCurrentFrameUpdated = false; + } -void VectorRasterizeThread::Rasterize() -{ - DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame ); + auto currentFrameStartTime = std::chrono::system_clock::now(); // Rasterize - mVectorRenderer.Render( mCurrentFrame ); + mVectorRenderer.Render( currentFrame ); - if( mPlayState == DevelImageVisual::PlayState::PLAYING ) + if( playState == DevelImageVisual::PlayState::PLAYING ) { - if( ++mCurrentFrame >= mEndFrame ) + if( currentFrame >= endFrame ) { - if( mLoopCount < 0 ) + if( loopCount < 0 ) { // repeat forever - mCurrentFrame = mStartFrame; + ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it. } else { mCurrentLoop++; - if( mCurrentLoop >= mLoopCount ) + if( mCurrentLoop >= loopCount ) { - // Animation is finished mPlayState = DevelImageVisual::PlayState::STOPPED; + ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); + + // Animation is finished mAnimationFinishedTrigger->Trigger(); DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" ); } else { - mCurrentFrame = mStartFrame; + ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); } } } } - 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", currentFrame, sleepDuration.count() ); +#endif + + std::this_thread::sleep_until( timeToSleepUntil ); } } // namespace Internal