2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h>
22 #include <dali/devel-api/adaptor-framework/thread-settings.h>
23 #include <dali/integration-api/adaptors/adaptor.h>
24 #include <dali/integration-api/debug.h>
40 constexpr auto LOOP_FOREVER = -1;
41 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
47 inline void ResetValue( bool& updated, uint32_t& value, uint32_t newValue, ConditionalWait& conditionalWait )
49 ConditionalWait::ScopedLock lock( conditionalWait );
57 } // unnamed namespace
59 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
63 mAnimationFinishedTrigger(),
64 mPlayState( PlayState::STOPPED ),
65 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
66 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
67 mFrameDurationNanoSeconds( 0 ),
75 mLoopCount( LOOP_FOREVER ),
78 mDestroyThread( false ),
79 mResourceReady( false ),
80 mCurrentFrameUpdated( false ),
82 mUpdateFrameNumber( false ),
83 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
88 VectorRasterizeThread::~VectorRasterizeThread()
92 ConditionalWait::ScopedLock lock( mConditionalWait );
93 mDestroyThread = true;
94 mConditionalWait.Notify( lock );
97 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this );
102 void VectorRasterizeThread::Run()
104 SetThreadName( "VectorImageThread" );
105 mLogFactory.InstallLogFunction();
107 while( !mDestroyThread )
112 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread [%p]\n", this );
115 void VectorRasterizeThread::SetRenderer( Renderer renderer )
117 ConditionalWait::ScopedLock lock( mConditionalWait );
119 mVectorRenderer.SetRenderer( renderer );
121 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer [%p]\n", this );
124 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
126 if( mWidth != width || mHeight != height )
128 ConditionalWait::ScopedLock lock( mConditionalWait );
129 mVectorRenderer.SetSize( width, height );
134 mResourceReady = false;
136 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d [%p]\n", width, height, this );
140 void VectorRasterizeThread::PlayAnimation()
142 ConditionalWait::ScopedLock lock( mConditionalWait );
144 if( mPlayState != PlayState::PLAYING )
147 mUpdateFrameNumber = false;
148 mPlayState = PlayState::PLAYING;
149 mConditionalWait.Notify( lock );
151 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play [%p]\n", this );
155 void VectorRasterizeThread::StopAnimation()
157 ConditionalWait::ScopedLock lock( mConditionalWait );
158 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
161 mPlayState = PlayState::STOPPING;
162 mConditionalWait.Notify( lock );
164 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop [%p]\n", this );
168 void VectorRasterizeThread::PauseAnimation()
170 ConditionalWait::ScopedLock lock( mConditionalWait );
171 if( mPlayState == PlayState::PLAYING )
173 mPlayState = PlayState::PAUSED;
175 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause [%p]\n", this );
179 void VectorRasterizeThread::RenderFrame()
181 ConditionalWait::ScopedLock lock( mConditionalWait );
183 if( !mResourceReady )
186 mConditionalWait.Notify( lock );
188 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render [%p]\n", this );
192 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
194 ConditionalWait::ScopedLock lock( mConditionalWait );
195 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
198 void VectorRasterizeThread::SetLoopCount( int32_t count )
200 if( mLoopCount != count )
202 ConditionalWait::ScopedLock lock( mConditionalWait );
206 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d] [%p]\n", count, this );
210 void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
212 // Make sure the range specified is between 0 and the total frame number
213 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
215 // If the range is not in order swap values
216 if( startFrame > endFrame )
218 uint32_t temp = startFrame;
219 startFrame = endFrame;
223 if( startFrame != mStartFrame || endFrame != mEndFrame )
225 ConditionalWait::ScopedLock lock( mConditionalWait );
227 mStartFrame = startFrame;
228 mEndFrame = endFrame;
230 // If the current frame is out of the range, change the current frame also.
231 if( mStartFrame > mCurrentFrame )
233 mCurrentFrame = mStartFrame;
235 mCurrentFrameUpdated = true;
236 mResourceReady = false;
238 else if( mEndFrame < mCurrentFrame )
240 mCurrentFrame = mEndFrame;
242 mCurrentFrameUpdated = true;
243 mResourceReady = false;
246 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
251 DevelImageVisual::PlayState::Type VectorRasterizeThread::GetPlayState() const
253 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
257 case PlayState::PLAYING:
259 state = DevelImageVisual::PlayState::PLAYING;
262 case PlayState::PAUSED:
264 state = DevelImageVisual::PlayState::PAUSED;
267 case PlayState::STOPPING:
268 case PlayState::STOPPED:
270 state = DevelImageVisual::PlayState::STOPPED;
278 bool VectorRasterizeThread::IsResourceReady() const
280 return mResourceReady;
283 void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
285 ConditionalWait::ScopedLock lock( mConditionalWait );
287 if( mCurrentFrame == frameNumber )
289 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
293 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
295 mCurrentFrame = frameNumber;
296 mCurrentFrameUpdated = true;
298 mResourceReady = false;
300 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
304 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
308 uint32_t VectorRasterizeThread::GetCurrentFrameNumber() const
310 return mCurrentFrame;
313 uint32_t VectorRasterizeThread::GetTotalFrameNumber() const
318 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
320 mVectorRenderer.GetDefaultSize( width, height );
323 void VectorRasterizeThread::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
325 ConditionalWait::ScopedLock lock( mConditionalWait );
326 mStopBehavior = stopBehavior;
328 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
331 void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
333 ConditionalWait::ScopedLock lock( mConditionalWait );
334 mLoopingMode = loopingMode;
336 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
339 VectorRasterizeThread::UploadCompletedSignalType& VectorRasterizeThread::UploadCompletedSignal()
341 return mVectorRenderer.UploadCompletedSignal();
344 void VectorRasterizeThread::Initialize()
346 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
348 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
350 mEndFrame = mTotalFrame - 1;
352 mFrameRate = mVectorRenderer.GetFrameRate();
353 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
355 uint32_t width, height;
356 mVectorRenderer.GetDefaultSize( width, height );
358 SetSize( width, height );
360 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
363 void VectorRasterizeThread::Rasterize()
365 bool stopped = false;
366 uint32_t currentFrame, startFrame, endFrame;
370 ConditionalWait::ScopedLock lock( mConditionalWait );
372 if( ( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED ) && !mNeedRender && !mDestroyThread )
374 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait [%p]\n", this );
375 mConditionalWait.Wait( lock );
378 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
380 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
383 currentFrame = mCurrentFrame;
384 startFrame = mStartFrame;
385 endFrame = mEndFrame;
386 loopCount = mLoopCount;
388 mResourceReady = true;
390 mCurrentFrameUpdated = false;
391 mUpdateFrameNumber = true;
394 auto currentFrameStartTime = std::chrono::system_clock::now();
396 if( mPlayState == PlayState::STOPPING )
398 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
399 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
403 else if( mPlayState == PlayState::PLAYING )
405 bool animationFinished = false;
407 if( currentFrame >= endFrame ) // last frame
409 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
415 if( loopCount < 0 || ++mCurrentLoop < loopCount ) // repeat forever or before the last loop
417 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
418 mUpdateFrameNumber = false;
422 animationFinished = true; // end of animation
426 else if( currentFrame == startFrame && !mForward ) // first frame
428 if( loopCount < 0 || ++mCurrentLoop < loopCount ) // repeat forever or before the last loop
434 animationFinished = true; // end of animation
438 if( animationFinished )
440 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
446 mPlayState = PlayState::STOPPING;
452 if( !mVectorRenderer.Render( currentFrame ) )
454 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
455 mUpdateFrameNumber = false;
460 mPlayState = PlayState::STOPPED;
464 // Animation is finished
465 mAnimationFinishedTrigger->Trigger();
467 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
470 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
472 #if defined(DEBUG_ENABLED)
473 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
475 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld] [%p]\n", currentFrame, sleepDuration.count(), this );
478 std::this_thread::sleep_until( timeToSleepUntil );
481 uint32_t VectorRasterizeThread::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
483 uint32_t frame = currentFrame;
485 switch( mStopBehavior )
487 case DevelImageVisual::StopBehavior::FIRST_FRAME:
492 case DevelImageVisual::StopBehavior::LAST_FRAME:
494 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
504 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
506 frame = currentFrame;
514 } // namespace Internal
516 } // namespace Toolkit