2 * Copyright (c) 2020 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 )
92 VectorAnimationTask::~VectorAnimationTask()
94 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
97 void VectorAnimationTask::Finalize()
99 ConditionalWait::ScopedLock lock( mConditionalWait );
101 // Release some objects in the main thread
102 if( mAnimationFinishedTrigger )
104 mAnimationFinishedTrigger.reset();
107 mVectorRenderer.Finalize();
110 void VectorAnimationTask::SetRenderer( Renderer renderer )
112 ConditionalWait::ScopedLock lock( mConditionalWait );
114 mVectorRenderer.SetRenderer( renderer );
116 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
119 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
121 if( mWidth != width || mHeight != height )
123 ConditionalWait::ScopedLock lock( mConditionalWait );
124 mVectorRenderer.SetSize( width, height );
129 mResourceReady = false;
131 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
135 void VectorAnimationTask::PlayAnimation()
137 ConditionalWait::ScopedLock lock( mConditionalWait );
139 if( mPlayState != PlayState::PLAYING )
141 mUpdateFrameNumber = false;
142 mPlayState = PlayState::PLAYING;
144 mVectorAnimationThread.AddTask( this );
146 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
150 void VectorAnimationTask::StopAnimation()
152 ConditionalWait::ScopedLock lock( mConditionalWait );
153 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
155 mNeedAnimationFinishedTrigger = false;
156 mPlayState = PlayState::STOPPING;
158 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
162 void VectorAnimationTask::PauseAnimation()
164 ConditionalWait::ScopedLock lock( mConditionalWait );
165 if( mPlayState == PlayState::PLAYING )
167 mPlayState = PlayState::PAUSED;
169 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
173 void VectorAnimationTask::RenderFrame()
175 ConditionalWait::ScopedLock lock( mConditionalWait );
177 if( !mResourceReady )
179 mVectorAnimationThread.AddTask( this );
181 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
185 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
187 ConditionalWait::ScopedLock lock( mConditionalWait );
190 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
194 void VectorAnimationTask::SetLoopCount( int32_t count )
196 if( mLoopCount != count )
198 ConditionalWait::ScopedLock lock( mConditionalWait );
202 mCurrentLoopUpdated = true;
204 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
208 void VectorAnimationTask::SetPlayRange( Property::Array& playRange )
210 ConditionalWait::ScopedLock lock( mConditionalWait );
213 uint32_t startFrame = 0, endFrame = 0;
214 size_t count = playRange.Count();
218 int32_t start = 0, end = 0;
219 if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
221 startFrame = static_cast< uint32_t >( start );
222 endFrame = static_cast< uint32_t >( end );
227 std::string startMarker, endMarker;
228 if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
230 if( mVectorRenderer )
232 uint32_t frame; // We don't use this later
233 if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
241 else if( count == 1 )
244 if( playRange.GetElementAt( 0 ).Get( marker ) )
246 if( mVectorRenderer )
248 mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
256 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
260 // Make sure the range specified is between 0 and the total frame number
261 if( startFrame < mTotalFrame && endFrame < mTotalFrame )
263 // If the range is not in order swap values
264 if( startFrame > endFrame )
266 uint32_t temp = startFrame;
267 startFrame = endFrame;
271 if( startFrame != mStartFrame || endFrame != mEndFrame )
273 mStartFrame = startFrame;
274 mEndFrame = endFrame;
276 // If the current frame is out of the range, change the current frame also.
277 if( mStartFrame > mCurrentFrame )
279 mCurrentFrame = mStartFrame;
281 mCurrentFrameUpdated = true;
282 mResourceReady = false;
284 else if( mEndFrame < mCurrentFrame )
286 mCurrentFrame = mEndFrame;
288 mCurrentFrameUpdated = true;
289 mResourceReady = false;
292 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
297 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this );
302 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
304 startFrame = mStartFrame;
305 endFrame = mEndFrame;
308 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
310 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
314 case PlayState::PLAYING:
316 state = DevelImageVisual::PlayState::PLAYING;
319 case PlayState::PAUSED:
321 state = DevelImageVisual::PlayState::PAUSED;
324 case PlayState::STOPPING:
325 case PlayState::STOPPED:
327 state = DevelImageVisual::PlayState::STOPPED;
335 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
337 ConditionalWait::ScopedLock lock( mConditionalWait );
339 if( mCurrentFrame == frameNumber )
341 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
345 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
347 mCurrentFrame = frameNumber;
348 mCurrentFrameUpdated = true;
350 mUpdateFrameNumber = false;
351 mResourceReady = false;
353 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
357 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
361 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
363 return mCurrentFrame;
366 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
371 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
373 mVectorRenderer.GetDefaultSize( width, height );
376 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
378 ConditionalWait::ScopedLock lock( mConditionalWait );
379 mStopBehavior = stopBehavior;
381 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
384 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
386 ConditionalWait::ScopedLock lock( mConditionalWait );
387 mLoopingMode = loopingMode;
389 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
392 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
394 mVectorRenderer.GetLayerInfo( map );
397 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
399 return mVectorRenderer.UploadCompletedSignal();
402 void VectorAnimationTask::Initialize()
404 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
406 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
408 mEndFrame = mTotalFrame - 1;
410 mFrameRate = mVectorRenderer.GetFrameRate();
411 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
413 uint32_t width, height;
414 mVectorRenderer.GetDefaultSize( width, height );
416 SetSize( width, height );
418 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
421 bool VectorAnimationTask::Rasterize()
423 bool stopped = false, needAnimationFinishedTrigger, resourceReady;
424 uint32_t currentFrame, startFrame, endFrame;
425 int32_t loopCount, currentLoopCount;
429 ConditionalWait::ScopedLock lock( mConditionalWait );
431 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
433 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
434 Dali::ClampInPlace( mCurrentFrame, mStartFrame, mEndFrame );
437 currentFrame = mCurrentFrame;
438 startFrame = mStartFrame;
439 endFrame = mEndFrame;
440 loopCount = mLoopCount;
441 currentLoopCount = mCurrentLoop;
442 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
443 playState = mPlayState;
444 resourceReady = mResourceReady;
446 mResourceReady = true;
447 mCurrentFrameUpdated = false;
448 mCurrentLoopUpdated = false;
449 mUpdateFrameNumber = true;
450 mNeedAnimationFinishedTrigger = true;
453 if( playState == PlayState::STOPPING )
455 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
456 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
460 else if( playState == PlayState::PLAYING )
462 bool animationFinished = false;
464 if( currentFrame >= endFrame ) // last frame
466 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
472 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
474 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
475 mUpdateFrameNumber = false;
479 animationFinished = true; // end of animation
481 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
484 else if( currentFrame == startFrame && !mForward ) // first frame
486 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
492 animationFinished = true; // end of animation
494 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
497 if( animationFinished )
499 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
505 mPlayState = PlayState::STOPPING;
511 bool renderSuccess = false;
512 if( mVectorRenderer )
514 renderSuccess = mVectorRenderer.Render( currentFrame );
517 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
518 mUpdateFrameNumber = false;
522 ConditionalWait::ScopedLock lock( mConditionalWait );
523 mResourceReady = false;
528 if( stopped && renderSuccess )
530 ConditionalWait::ScopedLock lock( mConditionalWait );
532 mPlayState = PlayState::STOPPED;
536 // Animation is finished
537 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
539 mAnimationFinishedTrigger->Trigger();
542 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
545 bool keepAnimation = true;
546 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
548 keepAnimation = false;
551 return keepAnimation;
554 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
556 uint32_t frame = currentFrame;
558 switch( mStopBehavior )
560 case DevelImageVisual::StopBehavior::FIRST_FRAME:
565 case DevelImageVisual::StopBehavior::LAST_FRAME:
567 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
577 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
579 frame = currentFrame;
587 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
589 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
590 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
591 // is casted to use the default duration.
592 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
593 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
594 auto current = std::chrono::system_clock::now();
595 if( renderNow || mNextFrameStartTime < current )
597 mNextFrameStartTime = current;
599 return mNextFrameStartTime;
602 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
604 return mNextFrameStartTime;
607 } // namespace Internal
609 } // namespace Toolkit