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 template< typename T >
48 inline void ResetValue( bool& updated, T& value, T newValue, ConditionalWait& conditionalWait )
50 ConditionalWait::ScopedLock lock( conditionalWait );
58 } // unnamed namespace
60 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
64 mAnimationFinishedTrigger(),
65 mPlayState( PlayState::STOPPED ),
66 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
67 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
68 mFrameDurationNanoSeconds( 0 ),
76 mLoopCount( LOOP_FOREVER ),
79 mDestroyThread( false ),
80 mResourceReady( false ),
81 mCurrentFrameUpdated( false ),
82 mCurrentLoopUpdated( false ),
84 mUpdateFrameNumber( false ),
85 mNeedAnimationFinishedTrigger( true ),
86 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
91 VectorRasterizeThread::~VectorRasterizeThread()
95 ConditionalWait::ScopedLock lock( mConditionalWait );
96 mDestroyThread = true;
97 mConditionalWait.Notify( lock );
100 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this );
105 void VectorRasterizeThread::Run()
107 SetThreadName( "VectorImageThread" );
108 mLogFactory.InstallLogFunction();
110 while( !mDestroyThread )
115 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread [%p]\n", this );
118 void VectorRasterizeThread::SetRenderer( Renderer renderer )
120 ConditionalWait::ScopedLock lock( mConditionalWait );
122 mVectorRenderer.SetRenderer( renderer );
124 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer [%p]\n", this );
127 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
129 if( mWidth != width || mHeight != height )
131 ConditionalWait::ScopedLock lock( mConditionalWait );
132 mVectorRenderer.SetSize( width, height );
137 mResourceReady = false;
139 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d [%p]\n", width, height, this );
143 void VectorRasterizeThread::PlayAnimation()
145 ConditionalWait::ScopedLock lock( mConditionalWait );
147 if( mPlayState != PlayState::PLAYING )
150 mUpdateFrameNumber = false;
151 mPlayState = PlayState::PLAYING;
152 mConditionalWait.Notify( lock );
154 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play [%p]\n", this );
158 void VectorRasterizeThread::StopAnimation()
160 ConditionalWait::ScopedLock lock( mConditionalWait );
161 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
164 mNeedAnimationFinishedTrigger = false;
165 mPlayState = PlayState::STOPPING;
166 mConditionalWait.Notify( lock );
168 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop [%p]\n", this );
172 void VectorRasterizeThread::PauseAnimation()
174 ConditionalWait::ScopedLock lock( mConditionalWait );
175 if( mPlayState == PlayState::PLAYING )
177 mPlayState = PlayState::PAUSED;
179 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause [%p]\n", this );
183 void VectorRasterizeThread::RenderFrame()
185 ConditionalWait::ScopedLock lock( mConditionalWait );
187 if( !mResourceReady )
190 mConditionalWait.Notify( lock );
192 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render [%p]\n", this );
196 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
198 ConditionalWait::ScopedLock lock( mConditionalWait );
199 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
202 void VectorRasterizeThread::SetLoopCount( int32_t count )
204 if( mLoopCount != count )
206 ConditionalWait::ScopedLock lock( mConditionalWait );
210 mCurrentLoopUpdated = true;
212 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d] [%p]\n", count, this );
216 void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
218 // Make sure the range specified is between 0 and the total frame number
219 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
221 // If the range is not in order swap values
222 if( startFrame > endFrame )
224 uint32_t temp = startFrame;
225 startFrame = endFrame;
229 if( startFrame != mStartFrame || endFrame != mEndFrame )
231 ConditionalWait::ScopedLock lock( mConditionalWait );
233 mStartFrame = startFrame;
234 mEndFrame = endFrame;
236 // If the current frame is out of the range, change the current frame also.
237 if( mStartFrame > mCurrentFrame )
239 mCurrentFrame = mStartFrame;
241 mCurrentFrameUpdated = true;
242 mResourceReady = false;
244 else if( mEndFrame < mCurrentFrame )
246 mCurrentFrame = mEndFrame;
248 mCurrentFrameUpdated = true;
249 mResourceReady = false;
252 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
257 DevelImageVisual::PlayState::Type VectorRasterizeThread::GetPlayState() const
259 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
263 case PlayState::PLAYING:
265 state = DevelImageVisual::PlayState::PLAYING;
268 case PlayState::PAUSED:
270 state = DevelImageVisual::PlayState::PAUSED;
273 case PlayState::STOPPING:
274 case PlayState::STOPPED:
276 state = DevelImageVisual::PlayState::STOPPED;
284 bool VectorRasterizeThread::IsResourceReady() const
286 return mResourceReady;
289 void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
291 ConditionalWait::ScopedLock lock( mConditionalWait );
293 if( mCurrentFrame == frameNumber )
295 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
299 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
301 mCurrentFrame = frameNumber;
302 mCurrentFrameUpdated = true;
304 mResourceReady = false;
306 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
310 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
314 uint32_t VectorRasterizeThread::GetCurrentFrameNumber() const
316 return mCurrentFrame;
319 uint32_t VectorRasterizeThread::GetTotalFrameNumber() const
324 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
326 mVectorRenderer.GetDefaultSize( width, height );
329 void VectorRasterizeThread::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
331 ConditionalWait::ScopedLock lock( mConditionalWait );
332 mStopBehavior = stopBehavior;
334 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
337 void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
339 ConditionalWait::ScopedLock lock( mConditionalWait );
340 mLoopingMode = loopingMode;
342 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
345 VectorRasterizeThread::UploadCompletedSignalType& VectorRasterizeThread::UploadCompletedSignal()
347 return mVectorRenderer.UploadCompletedSignal();
350 void VectorRasterizeThread::Initialize()
352 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
354 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
356 mEndFrame = mTotalFrame - 1;
358 mFrameRate = mVectorRenderer.GetFrameRate();
359 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
361 uint32_t width, height;
362 mVectorRenderer.GetDefaultSize( width, height );
364 SetSize( width, height );
366 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
369 void VectorRasterizeThread::Rasterize()
371 bool stopped = false, needAnimationFinishedTrigger;
372 uint32_t currentFrame, startFrame, endFrame;
373 int32_t loopCount, currentLoopCount;
376 ConditionalWait::ScopedLock lock( mConditionalWait );
378 if( ( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED ) && !mNeedRender && !mDestroyThread )
380 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait [%p]\n", this );
381 mConditionalWait.Wait( lock );
384 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
386 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
389 currentFrame = mCurrentFrame;
390 startFrame = mStartFrame;
391 endFrame = mEndFrame;
392 loopCount = mLoopCount;
393 currentLoopCount = mCurrentLoop;
394 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
396 mResourceReady = true;
398 mCurrentFrameUpdated = false;
399 mCurrentLoopUpdated = false;
400 mUpdateFrameNumber = true;
401 mNeedAnimationFinishedTrigger = true;
404 auto currentFrameStartTime = std::chrono::system_clock::now();
406 if( mPlayState == PlayState::STOPPING )
408 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
409 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
413 else if( mPlayState == PlayState::PLAYING )
415 bool animationFinished = false;
417 if( currentFrame >= endFrame ) // last frame
419 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
425 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
427 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
428 mUpdateFrameNumber = false;
432 animationFinished = true; // end of animation
434 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
437 else if( currentFrame == startFrame && !mForward ) // first frame
439 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
445 animationFinished = true; // end of animation
447 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
450 if( animationFinished )
452 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
458 mPlayState = PlayState::STOPPING;
464 if( !mVectorRenderer.Render( currentFrame ) )
466 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
467 mUpdateFrameNumber = false;
472 mPlayState = PlayState::STOPPED;
476 // Animation is finished
477 if( needAnimationFinishedTrigger )
479 mAnimationFinishedTrigger->Trigger();
482 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
485 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
487 #if defined(DEBUG_ENABLED)
488 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
490 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld] [%p]\n", currentFrame, sleepDuration.count(), this );
493 std::this_thread::sleep_until( timeToSleepUntil );
496 uint32_t VectorRasterizeThread::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
498 uint32_t frame = currentFrame;
500 switch( mStopBehavior )
502 case DevelImageVisual::StopBehavior::FIRST_FRAME:
507 case DevelImageVisual::StopBehavior::LAST_FRAME:
509 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
519 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
521 frame = currentFrame;
529 } // namespace Internal
531 } // namespace Toolkit