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>
26 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
27 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
41 constexpr auto LOOP_FOREVER = -1;
42 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
44 #if defined(DEBUG_ENABLED)
45 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
48 template< typename T >
49 inline void ResetValue( bool& updated, T& value, T newValue, ConditionalWait& conditionalWait )
51 ConditionalWait::ScopedLock lock( conditionalWait );
59 } // unnamed namespace
61 VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url )
64 mVectorAnimationThread( factoryCache.GetVectorAnimationThread() ),
66 mAnimationFinishedTrigger(),
67 mPlayState( PlayState::STOPPED ),
68 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
69 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
70 mNextFrameStartTime(),
71 mFrameDurationNanoSeconds( 0 ),
79 mLoopCount( LOOP_FOREVER ),
81 mResourceReady( false ),
82 mCurrentFrameUpdated( false ),
83 mCurrentLoopUpdated( false ),
85 mUpdateFrameNumber( false ),
86 mNeedAnimationFinishedTrigger( true )
91 VectorAnimationTask::~VectorAnimationTask()
93 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
96 void VectorAnimationTask::Finalize()
98 // Release some objects in the main thread
99 if( mAnimationFinishedTrigger )
101 mAnimationFinishedTrigger.reset();
104 mVectorRenderer.Finalize();
107 void VectorAnimationTask::SetRenderer( Renderer renderer )
109 ConditionalWait::ScopedLock lock( mConditionalWait );
111 mVectorRenderer.SetRenderer( renderer );
113 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
116 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
118 if( mWidth != width || mHeight != height )
120 ConditionalWait::ScopedLock lock( mConditionalWait );
121 mVectorRenderer.SetSize( width, height );
126 mResourceReady = false;
128 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
132 void VectorAnimationTask::PlayAnimation()
134 ConditionalWait::ScopedLock lock( mConditionalWait );
136 if( mPlayState != PlayState::PLAYING )
138 mUpdateFrameNumber = false;
139 mPlayState = PlayState::PLAYING;
141 mVectorAnimationThread.AddTask( this );
143 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
147 void VectorAnimationTask::StopAnimation()
149 ConditionalWait::ScopedLock lock( mConditionalWait );
150 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
152 mNeedAnimationFinishedTrigger = false;
153 mPlayState = PlayState::STOPPING;
155 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
159 void VectorAnimationTask::PauseAnimation()
161 ConditionalWait::ScopedLock lock( mConditionalWait );
162 if( mPlayState == PlayState::PLAYING )
164 mPlayState = PlayState::PAUSED;
166 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
170 void VectorAnimationTask::RenderFrame()
172 ConditionalWait::ScopedLock lock( mConditionalWait );
174 if( !mResourceReady )
176 mVectorAnimationThread.AddTask( this );
178 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
182 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
184 ConditionalWait::ScopedLock lock( mConditionalWait );
187 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
191 void VectorAnimationTask::SetLoopCount( int32_t count )
193 if( mLoopCount != count )
195 ConditionalWait::ScopedLock lock( mConditionalWait );
199 mCurrentLoopUpdated = true;
201 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
205 void VectorAnimationTask::SetPlayRange( Property::Array& playRange )
207 ConditionalWait::ScopedLock lock( mConditionalWait );
210 uint32_t startFrame = 0, endFrame = 0;
211 size_t count = playRange.Count();
215 int32_t start = 0, end = 0;
216 if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
218 startFrame = static_cast< uint32_t >( start );
219 endFrame = static_cast< uint32_t >( end );
224 std::string startMarker, endMarker;
225 if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
227 if( mVectorRenderer )
229 uint32_t frame; // We don't use this later
230 if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
238 else if( count == 1 )
241 if( playRange.GetElementAt( 0 ).Get( marker ) )
243 if( mVectorRenderer )
245 mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
253 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
257 // Make sure the range specified is between 0 and the total frame number
258 if( startFrame >= 0 && startFrame < mTotalFrame && endFrame >= 0 && endFrame < mTotalFrame )
260 // If the range is not in order swap values
261 if( startFrame > endFrame )
263 uint32_t temp = startFrame;
264 startFrame = endFrame;
268 if( startFrame != mStartFrame || endFrame != mEndFrame )
270 mStartFrame = startFrame;
271 mEndFrame = endFrame;
273 // If the current frame is out of the range, change the current frame also.
274 if( mStartFrame > mCurrentFrame )
276 mCurrentFrame = mStartFrame;
278 mCurrentFrameUpdated = true;
279 mResourceReady = false;
281 else if( mEndFrame < mCurrentFrame )
283 mCurrentFrame = mEndFrame;
285 mCurrentFrameUpdated = true;
286 mResourceReady = false;
289 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
294 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this );
299 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
301 startFrame = mStartFrame;
302 endFrame = mEndFrame;
305 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
307 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
311 case PlayState::PLAYING:
313 state = DevelImageVisual::PlayState::PLAYING;
316 case PlayState::PAUSED:
318 state = DevelImageVisual::PlayState::PAUSED;
321 case PlayState::STOPPING:
322 case PlayState::STOPPED:
324 state = DevelImageVisual::PlayState::STOPPED;
332 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
334 ConditionalWait::ScopedLock lock( mConditionalWait );
336 if( mCurrentFrame == frameNumber )
338 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
342 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
344 mCurrentFrame = frameNumber;
345 mCurrentFrameUpdated = true;
347 mUpdateFrameNumber = false;
348 mResourceReady = false;
350 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
354 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
358 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
360 return mCurrentFrame;
363 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
368 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
370 mVectorRenderer.GetDefaultSize( width, height );
373 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
375 ConditionalWait::ScopedLock lock( mConditionalWait );
376 mStopBehavior = stopBehavior;
378 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
381 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
383 ConditionalWait::ScopedLock lock( mConditionalWait );
384 mLoopingMode = loopingMode;
386 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
389 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
391 mVectorRenderer.GetLayerInfo( map );
394 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
396 return mVectorRenderer.UploadCompletedSignal();
399 void VectorAnimationTask::Initialize()
401 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
403 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
405 mEndFrame = mTotalFrame - 1;
407 mFrameRate = mVectorRenderer.GetFrameRate();
408 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
410 uint32_t width, height;
411 mVectorRenderer.GetDefaultSize( width, height );
413 SetSize( width, height );
415 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
418 bool VectorAnimationTask::Rasterize()
420 bool stopped = false, needAnimationFinishedTrigger;
421 uint32_t currentFrame, startFrame, endFrame;
422 int32_t loopCount, currentLoopCount;
426 ConditionalWait::ScopedLock lock( mConditionalWait );
428 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
430 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
433 currentFrame = mCurrentFrame;
434 startFrame = mStartFrame;
435 endFrame = mEndFrame;
436 loopCount = mLoopCount;
437 currentLoopCount = mCurrentLoop;
438 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
439 playState = mPlayState;
441 mResourceReady = true;
442 mCurrentFrameUpdated = false;
443 mCurrentLoopUpdated = false;
444 mUpdateFrameNumber = true;
445 mNeedAnimationFinishedTrigger = true;
448 if( playState == PlayState::STOPPING )
450 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
451 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
455 else if( playState == PlayState::PLAYING )
457 bool animationFinished = false;
459 if( currentFrame >= endFrame ) // last frame
461 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
467 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
469 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
470 mUpdateFrameNumber = false;
474 animationFinished = true; // end of animation
476 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
479 else if( currentFrame == startFrame && !mForward ) // first frame
481 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
487 animationFinished = true; // end of animation
489 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
492 if( animationFinished )
494 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
500 mPlayState = PlayState::STOPPING;
506 bool renderSuccess = false;
507 if( mVectorRenderer )
509 renderSuccess = mVectorRenderer.Render( currentFrame );
512 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
513 mUpdateFrameNumber = false;
517 if( stopped && renderSuccess )
519 mPlayState = PlayState::STOPPED;
523 // Animation is finished
524 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
526 mAnimationFinishedTrigger->Trigger();
529 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
532 bool keepAnimation = true;
533 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
535 keepAnimation = false;
538 return keepAnimation;
541 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
543 uint32_t frame = currentFrame;
545 switch( mStopBehavior )
547 case DevelImageVisual::StopBehavior::FIRST_FRAME:
552 case DevelImageVisual::StopBehavior::LAST_FRAME:
554 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
564 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
566 frame = currentFrame;
574 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
576 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
577 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
578 // is casted to use the default duration.
579 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
580 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
581 auto current = std::chrono::system_clock::now();
582 if( renderNow || mNextFrameStartTime < current )
584 mNextFrameStartTime = current;
586 return mNextFrameStartTime;
589 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
591 return mNextFrameStartTime;
594 } // namespace Internal
596 } // namespace Toolkit