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=a5de71a7102005fe6fd75494a49b31cb400809d9;hb=9768810e114ae68868a124aa8506f2f4e64fe9d1;hpb=c7b9e9c65ace4c1210dabd95d0c2b71fb3c17e0b 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 a5de71a..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. @@ -44,19 +44,25 @@ constexpr auto NANOSECONDS_PER_SECOND( 1e+9 ); 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 ), mFrameDurationNanoSeconds( 0 ), - mProgress( 0.0f ), mFrameRate( 60.0f ), mCurrentFrame( 0 ), mTotalFrame( 0 ), @@ -69,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() @@ -96,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 ) @@ -133,6 +139,7 @@ void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height ) void VectorRasterizeThread::PlayAnimation() { ConditionalWait::ScopedLock lock( mConditionalWait ); + if( mPlayState != DevelImageVisual::PlayState::PLAYING ) { mPlayState = DevelImageVisual::PlayState::PLAYING; @@ -149,9 +156,8 @@ void VectorRasterizeThread::StopAnimation() { mPlayState = DevelImageVisual::PlayState::STOPPED; - // Reset the current frame and the current loop mCurrentFrame = mStartFrame; - mCurrentLoop = 0; + mCurrentFrameUpdated = true; DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" ); } @@ -201,190 +207,189 @@ void VectorRasterizeThread::SetLoopCount( int32_t count ) mLoopCount = count; - // Reset progress - mCurrentLoop = 0; + 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; -} - -void VectorRasterizeThread::SetPlayRange( Vector2 range ) -{ - // 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 ) + // Make sure the range specified is between 0 and the total frame number + if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) ) { - Vector2 orderedRange( range ); // If the range is not in order swap values - if( range.x > range.y ) + if( startFrame > endFrame ) { - orderedRange = Vector2( range.y, range.x ); + uint32_t temp = startFrame; + startFrame = endFrame; + endFrame = temp; } - if( mPlayRange != orderedRange ) + if( startFrame != mStartFrame || endFrame != mEndFrame ) { ConditionalWait::ScopedLock lock( mConditionalWait ); - mPlayRange = orderedRange; + mStartFrame = startFrame; + mEndFrame = endFrame; - if( mTotalFrame != 0 ) + // If the current frame is out of the range, change the current frame also. + if( mStartFrame > mCurrentFrame ) { - mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f ); - mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f ); - - // If the current frame is out of the range, change the current frame also. - if( mStartFrame > mCurrentFrame ) - { - mCurrentFrame = mStartFrame; + mCurrentFrame = mStartFrame; - mResourceReady = false; - } - else if( mEndFrame < mCurrentFrame ) - { - mCurrentFrame = mEndFrame; + mCurrentFrameUpdated = true; + mResourceReady = false; + } + else if( mEndFrame < mCurrentFrame ) + { + mCurrentFrame = mEndFrame; - mResourceReady = false; - } + mCurrentFrameUpdated = true; + mResourceReady = false; } + + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRangeInFrame: [%d, %d]\n", mStartFrame, mEndFrame ); } } } -Vector2 VectorRasterizeThread::GetPlayRange() const -{ - return mPlayRange; -} - -void VectorRasterizeThread::SetCurrentProgress( float progress ) +void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber ) { ConditionalWait::ScopedLock lock( mConditionalWait ); - if( progress >= mPlayRange.x && progress <= mPlayRange.y ) + if( frameNumber >= mStartFrame && frameNumber <= mEndFrame ) { - mProgress = progress; - - if( mTotalFrame != 0 ) - { - mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f ); - } + mCurrentFrame = frameNumber; + mCurrentFrameUpdated = true; mResourceReady = false; - DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame ); + 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 ); } } -float VectorRasterizeThread::GetCurrentProgress() const +uint32_t VectorRasterizeThread::GetCurrentFrameNumber() const { - return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) ); + return mCurrentFrame; } -DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const +uint32_t VectorRasterizeThread::GetTotalFrameNumber() const { - return mPlayState; + return mTotalFrame; } -bool VectorRasterizeThread::IsResourceReady() const +void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const { - return mResourceReady; + mVectorRenderer.GetDefaultSize( width, height ); } -bool VectorRasterizeThread::IsThreadReady() +DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const { - ConditionalWait::ScopedLock lock( mConditionalWait ); - - if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread ) - { - DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" ); - - mConditionalWait.Wait( lock ); - } + return mPlayState; +} - // Keep the thread alive if this thread is NOT to be destroyed - return !mDestroyThread; +bool VectorRasterizeThread::IsResourceReady() const +{ + return mResourceReady; } -bool VectorRasterizeThread::StartRender() +void VectorRasterizeThread::Initialize() { - //TODO: check the return value - mVectorRenderer.StartRender(); + mVectorRenderer = VectorAnimationRenderer::New( mUrl ); mTotalFrame = mVectorRenderer.GetTotalFrameNumber(); - mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f ); - mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f ); - - mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame ); + mEndFrame = mTotalFrame; 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 ); + uint32_t width, height; + mVectorRenderer.GetDefaultSize( width, height ); - return true; + SetSize( width, height ); + + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate ); } void VectorRasterizeThread::Rasterize() { - bool needRender, resourceReady; + bool resourceReady; + uint32_t currentFrame, startFrame, endFrame; + int32_t loopCount; + DevelImageVisual::PlayState playState; { ConditionalWait::ScopedLock lock( mConditionalWait ); - needRender = mNeedRender; + + if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread ) + { + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" ); + + if( mPlayState == DevelImageVisual::PlayState::STOPPED ) + { + // Reset the current loop + mCurrentLoop = 0; + } + mConditionalWait.Wait( lock ); + } + resourceReady = mResourceReady; + currentFrame = mCurrentFrame++; + startFrame = mStartFrame; + endFrame = mEndFrame; + loopCount = mLoopCount; + playState = mPlayState; + + mNeedRender = false; + mResourceReady = true; + mCurrentFrameUpdated = false; } 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; - // Reset the current frame and the current loop - mCurrentFrame = mStartFrame; - mCurrentLoop = 0; + 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 ); } } } } - if( needRender ) - { - mNeedRender = false; - } - 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 ); @@ -392,7 +397,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", mCurrentFrame, sleepDuration.count() ); + DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", currentFrame, sleepDuration.count() ); #endif std::this_thread::sleep_until( timeToSleepUntil );