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-animation-task.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/math/math-utils.h>
27 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
28 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
42 constexpr auto LOOP_FOREVER = -1;
43 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
49 template< typename T >
50 inline void ResetValue( bool& updated, T& value, T newValue, ConditionalWait& conditionalWait )
52 ConditionalWait::ScopedLock lock( conditionalWait );
60 } // unnamed namespace
62 VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url )
65 mVectorAnimationThread( factoryCache.GetVectorAnimationThread() ),
67 mAnimationFinishedTrigger(),
68 mPlayState( PlayState::STOPPED ),
69 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
70 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
71 mNextFrameStartTime(),
72 mFrameDurationNanoSeconds( 0 ),
80 mLoopCount( LOOP_FOREVER ),
82 mResourceReady( false ),
83 mCurrentFrameUpdated( false ),
84 mCurrentLoopUpdated( false ),
86 mUpdateFrameNumber( false ),
87 mNeedAnimationFinishedTrigger( true ),
93 VectorAnimationTask::~VectorAnimationTask()
95 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
98 void VectorAnimationTask::Finalize()
100 ConditionalWait::ScopedLock lock( mConditionalWait );
102 // Release some objects in the main thread
103 if( mAnimationFinishedTrigger )
105 mAnimationFinishedTrigger.reset();
108 mVectorRenderer.Finalize();
111 void VectorAnimationTask::SetRenderer( Renderer renderer )
113 ConditionalWait::ScopedLock lock( mConditionalWait );
115 mVectorRenderer.SetRenderer( renderer );
117 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
120 void VectorAnimationTask::SetAnimationData( const AnimationData& data )
122 ConditionalWait::ScopedLock lock( mConditionalWait );
124 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetAnimationData [%p]\n", this );
126 if( data.resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT )
128 SetLoopCount( data.loopCount );
131 if( data.resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE )
133 SetPlayRange( data.playRange );
136 if( data.resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR )
138 SetStopBehavior( data.stopBehavior );
141 if( data.resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE )
143 SetLoopingMode( data.loopingMode );
146 if( data.resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME )
148 SetCurrentFrameNumber( data.currentFrame );
151 if( data.resendFlag & VectorAnimationTask::RESEND_SIZE )
153 SetSize( data.width, data.height );
156 if( data.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE )
158 if( data.playState == DevelImageVisual::PlayState::PLAYING )
162 else if( data.playState == DevelImageVisual::PlayState::PAUSED )
167 else if( data.playState == DevelImageVisual::PlayState::STOPPED )
174 if( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED )
181 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
183 if( mWidth != width || mHeight != height )
185 mVectorRenderer.SetSize( width, height );
190 mResourceReady = false;
192 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
196 void VectorAnimationTask::PlayAnimation()
198 if( mPlayState != PlayState::PLAYING )
200 mUpdateFrameNumber = false;
201 mPlayState = PlayState::PLAYING;
203 mVectorAnimationThread.AddTask( this );
205 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
209 void VectorAnimationTask::StopAnimation()
211 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
213 mNeedAnimationFinishedTrigger = false;
214 mPlayState = PlayState::STOPPING;
216 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
220 void VectorAnimationTask::PauseAnimation()
222 if( mPlayState == PlayState::PLAYING )
224 mPlayState = PlayState::PAUSED;
226 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
230 void VectorAnimationTask::RenderFrame()
232 if( !mResourceReady )
234 mVectorAnimationThread.AddTask( this );
236 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
240 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
242 ConditionalWait::ScopedLock lock( mConditionalWait );
245 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
249 void VectorAnimationTask::SetLoopCount( int32_t count )
251 if( mLoopCount != count )
255 mCurrentLoopUpdated = true;
257 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
261 void VectorAnimationTask::SetPlayRange( const Property::Array& playRange )
264 uint32_t startFrame = 0, endFrame = 0;
265 size_t count = playRange.Count();
269 int32_t start = 0, end = 0;
270 if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
272 startFrame = static_cast< uint32_t >( start );
273 endFrame = static_cast< uint32_t >( end );
278 std::string startMarker, endMarker;
279 if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
281 if( mVectorRenderer )
283 uint32_t frame; // We don't use this later
284 if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
292 else if( count == 1 )
295 if( playRange.GetElementAt( 0 ).Get( marker ) )
297 if( mVectorRenderer )
299 mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
307 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
311 // Make sure the range specified is between 0 and the total frame number
312 if( startFrame < mTotalFrame && endFrame < mTotalFrame )
314 // If the range is not in order swap values
315 if( startFrame > endFrame )
317 uint32_t temp = startFrame;
318 startFrame = endFrame;
322 if( startFrame != mStartFrame || endFrame != mEndFrame )
324 mStartFrame = startFrame;
325 mEndFrame = endFrame;
327 // If the current frame is out of the range, change the current frame also.
328 if( mStartFrame > mCurrentFrame )
330 mCurrentFrame = mStartFrame;
332 mCurrentFrameUpdated = true;
333 mResourceReady = false;
335 else if( mEndFrame < mCurrentFrame )
337 mCurrentFrame = mEndFrame;
339 mCurrentFrameUpdated = true;
340 mResourceReady = false;
343 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
348 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this );
353 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
355 startFrame = mStartFrame;
356 endFrame = mEndFrame;
359 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
361 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
365 case PlayState::PLAYING:
367 state = DevelImageVisual::PlayState::PLAYING;
370 case PlayState::PAUSED:
372 state = DevelImageVisual::PlayState::PAUSED;
375 case PlayState::STOPPING:
376 case PlayState::STOPPED:
378 state = DevelImageVisual::PlayState::STOPPED;
386 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
388 if( mCurrentFrame == frameNumber )
390 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
394 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
396 mCurrentFrame = frameNumber;
397 mCurrentFrameUpdated = true;
399 mUpdateFrameNumber = false;
400 mResourceReady = false;
402 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
406 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
410 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
412 return mCurrentFrame;
415 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
420 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
422 mVectorRenderer.GetDefaultSize( width, height );
425 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
427 mStopBehavior = stopBehavior;
429 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
432 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
434 mLoopingMode = loopingMode;
436 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
439 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
441 mVectorRenderer.GetLayerInfo( map );
444 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
446 return mVectorRenderer.UploadCompletedSignal();
449 void VectorAnimationTask::Initialize()
451 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
453 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
455 mEndFrame = mTotalFrame - 1;
457 mFrameRate = mVectorRenderer.GetFrameRate();
458 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
460 uint32_t width, height;
461 mVectorRenderer.GetDefaultSize( width, height );
463 SetSize( width, height );
465 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
468 bool VectorAnimationTask::Rasterize()
470 bool stopped = false, needAnimationFinishedTrigger, resourceReady;
471 uint32_t currentFrame, startFrame, endFrame;
472 int32_t loopCount, currentLoopCount;
476 ConditionalWait::ScopedLock lock( mConditionalWait );
478 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
480 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
481 Dali::ClampInPlace( mCurrentFrame, mStartFrame, mEndFrame );
484 currentFrame = mCurrentFrame;
485 startFrame = mStartFrame;
486 endFrame = mEndFrame;
487 loopCount = mLoopCount;
488 currentLoopCount = mCurrentLoop;
489 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
490 playState = mPlayState;
491 resourceReady = mResourceReady;
493 mResourceReady = true;
494 mCurrentFrameUpdated = false;
495 mCurrentLoopUpdated = false;
496 mUpdateFrameNumber = true;
497 mNeedAnimationFinishedTrigger = true;
500 if( playState == PlayState::STOPPING )
502 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
503 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
507 else if( playState == PlayState::PLAYING )
509 bool animationFinished = false;
511 if( currentFrame >= endFrame ) // last frame
513 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
519 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
521 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
522 mUpdateFrameNumber = false;
526 animationFinished = true; // end of animation
528 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
531 else if( currentFrame == startFrame && !mForward ) // first frame
533 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
539 animationFinished = true; // end of animation
541 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
544 if( animationFinished )
546 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
552 mPlayState = PlayState::STOPPING;
558 bool renderSuccess = false;
559 if( mVectorRenderer )
561 renderSuccess = mVectorRenderer.Render( currentFrame );
564 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
568 // Ignore previous frames and retry
569 mVectorRenderer.IgnoreRenderedFrames();
570 renderSuccess = mVectorRenderer.Render( currentFrame );
571 mForceRender = false;
580 mUpdateFrameNumber = false;
583 ConditionalWait::ScopedLock lock( mConditionalWait );
584 mResourceReady = false;
590 mForceRender = false;
594 if( stopped && renderSuccess )
596 ConditionalWait::ScopedLock lock( mConditionalWait );
598 mPlayState = PlayState::STOPPED;
602 // Animation is finished
603 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
605 mAnimationFinishedTrigger->Trigger();
608 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
611 bool keepAnimation = true;
612 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
614 keepAnimation = false;
617 return keepAnimation;
620 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
622 uint32_t frame = currentFrame;
624 switch( mStopBehavior )
626 case DevelImageVisual::StopBehavior::FIRST_FRAME:
631 case DevelImageVisual::StopBehavior::LAST_FRAME:
633 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
643 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
645 frame = currentFrame;
653 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
655 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
656 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
657 // is casted to use the default duration.
658 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
659 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
660 auto current = std::chrono::system_clock::now();
661 if( renderNow || mNextFrameStartTime < current )
663 mNextFrameStartTime = current;
665 return mNextFrameStartTime;
668 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
670 return mNextFrameStartTime;
673 } // namespace Internal
675 } // namespace Toolkit