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 )
92 VectorAnimationTask::~VectorAnimationTask()
94 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
97 void VectorAnimationTask::Finalize()
99 // Release some objects in the main thread
100 if( mAnimationFinishedTrigger )
102 mAnimationFinishedTrigger.reset();
105 mVectorRenderer.Finalize();
108 void VectorAnimationTask::SetRenderer( Renderer renderer )
110 ConditionalWait::ScopedLock lock( mConditionalWait );
112 mVectorRenderer.SetRenderer( renderer );
114 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
117 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
119 if( mWidth != width || mHeight != height )
121 ConditionalWait::ScopedLock lock( mConditionalWait );
122 mVectorRenderer.SetSize( width, height );
127 mResourceReady = false;
129 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
133 void VectorAnimationTask::PlayAnimation()
135 ConditionalWait::ScopedLock lock( mConditionalWait );
137 if( mPlayState != PlayState::PLAYING )
139 mUpdateFrameNumber = false;
140 mPlayState = PlayState::PLAYING;
142 mVectorAnimationThread.AddTask( this );
144 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
148 void VectorAnimationTask::StopAnimation()
150 ConditionalWait::ScopedLock lock( mConditionalWait );
151 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
153 mNeedAnimationFinishedTrigger = false;
154 mPlayState = PlayState::STOPPING;
156 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
160 void VectorAnimationTask::PauseAnimation()
162 ConditionalWait::ScopedLock lock( mConditionalWait );
163 if( mPlayState == PlayState::PLAYING )
165 mPlayState = PlayState::PAUSED;
167 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
171 void VectorAnimationTask::RenderFrame()
173 ConditionalWait::ScopedLock lock( mConditionalWait );
175 if( !mResourceReady )
177 mVectorAnimationThread.AddTask( this );
179 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
183 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
185 ConditionalWait::ScopedLock lock( mConditionalWait );
188 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
192 void VectorAnimationTask::SetLoopCount( int32_t count )
194 if( mLoopCount != count )
196 ConditionalWait::ScopedLock lock( mConditionalWait );
200 mCurrentLoopUpdated = true;
202 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
206 void VectorAnimationTask::SetPlayRange( Property::Array& playRange )
208 ConditionalWait::ScopedLock lock( mConditionalWait );
211 uint32_t startFrame = 0, endFrame = 0;
212 size_t count = playRange.Count();
216 int32_t start = 0, end = 0;
217 if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
219 startFrame = static_cast< uint32_t >( start );
220 endFrame = static_cast< uint32_t >( end );
225 std::string startMarker, endMarker;
226 if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
228 if( mVectorRenderer )
230 uint32_t frame; // We don't use this later
231 if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
239 else if( count == 1 )
242 if( playRange.GetElementAt( 0 ).Get( marker ) )
244 if( mVectorRenderer )
246 mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
254 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
258 // Make sure the range specified is between 0 and the total frame number
259 if( startFrame >= 0 && startFrame < mTotalFrame && endFrame >= 0 && endFrame < mTotalFrame )
261 // If the range is not in order swap values
262 if( startFrame > endFrame )
264 uint32_t temp = startFrame;
265 startFrame = endFrame;
269 if( startFrame != mStartFrame || endFrame != mEndFrame )
271 mStartFrame = startFrame;
272 mEndFrame = endFrame;
274 // If the current frame is out of the range, change the current frame also.
275 if( mStartFrame > mCurrentFrame )
277 mCurrentFrame = mStartFrame;
279 mCurrentFrameUpdated = true;
280 mResourceReady = false;
282 else if( mEndFrame < mCurrentFrame )
284 mCurrentFrame = mEndFrame;
286 mCurrentFrameUpdated = true;
287 mResourceReady = false;
290 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
295 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this );
300 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
302 startFrame = mStartFrame;
303 endFrame = mEndFrame;
306 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
308 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
312 case PlayState::PLAYING:
314 state = DevelImageVisual::PlayState::PLAYING;
317 case PlayState::PAUSED:
319 state = DevelImageVisual::PlayState::PAUSED;
322 case PlayState::STOPPING:
323 case PlayState::STOPPED:
325 state = DevelImageVisual::PlayState::STOPPED;
333 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
335 ConditionalWait::ScopedLock lock( mConditionalWait );
337 if( mCurrentFrame == frameNumber )
339 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
343 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
345 mCurrentFrame = frameNumber;
346 mCurrentFrameUpdated = true;
348 mUpdateFrameNumber = false;
349 mResourceReady = false;
351 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
355 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
359 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
361 return mCurrentFrame;
364 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
369 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
371 mVectorRenderer.GetDefaultSize( width, height );
374 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
376 ConditionalWait::ScopedLock lock( mConditionalWait );
377 mStopBehavior = stopBehavior;
379 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
382 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
384 ConditionalWait::ScopedLock lock( mConditionalWait );
385 mLoopingMode = loopingMode;
387 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
390 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
392 mVectorRenderer.GetLayerInfo( map );
395 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
397 return mVectorRenderer.UploadCompletedSignal();
400 void VectorAnimationTask::Initialize()
402 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
404 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
406 mEndFrame = mTotalFrame - 1;
408 mFrameRate = mVectorRenderer.GetFrameRate();
409 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
411 uint32_t width, height;
412 mVectorRenderer.GetDefaultSize( width, height );
414 SetSize( width, height );
416 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
419 bool VectorAnimationTask::Rasterize()
421 bool stopped = false, needAnimationFinishedTrigger;
422 uint32_t currentFrame, startFrame, endFrame;
423 int32_t loopCount, currentLoopCount;
427 ConditionalWait::ScopedLock lock( mConditionalWait );
429 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
431 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
432 Dali::ClampInPlace( mCurrentFrame, mStartFrame, mEndFrame );
435 currentFrame = mCurrentFrame;
436 startFrame = mStartFrame;
437 endFrame = mEndFrame;
438 loopCount = mLoopCount;
439 currentLoopCount = mCurrentLoop;
440 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
441 playState = mPlayState;
443 mResourceReady = true;
444 mCurrentFrameUpdated = false;
445 mCurrentLoopUpdated = false;
446 mUpdateFrameNumber = true;
447 mNeedAnimationFinishedTrigger = true;
450 if( playState == PlayState::STOPPING )
452 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
453 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
457 else if( playState == PlayState::PLAYING )
459 bool animationFinished = false;
461 if( currentFrame >= endFrame ) // last frame
463 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
469 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
471 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
472 mUpdateFrameNumber = false;
476 animationFinished = true; // end of animation
478 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
481 else if( currentFrame == startFrame && !mForward ) // first frame
483 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
489 animationFinished = true; // end of animation
491 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
494 if( animationFinished )
496 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
502 mPlayState = PlayState::STOPPING;
508 bool renderSuccess = false;
509 if( mVectorRenderer )
511 renderSuccess = mVectorRenderer.Render( currentFrame );
514 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
515 mUpdateFrameNumber = false;
519 if( stopped && renderSuccess )
521 mPlayState = PlayState::STOPPED;
525 // Animation is finished
526 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
528 mAnimationFinishedTrigger->Trigger();
531 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
534 bool keepAnimation = true;
535 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
537 keepAnimation = false;
540 return keepAnimation;
543 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
545 uint32_t frame = currentFrame;
547 switch( mStopBehavior )
549 case DevelImageVisual::StopBehavior::FIRST_FRAME:
554 case DevelImageVisual::StopBehavior::LAST_FRAME:
556 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
566 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
568 frame = currentFrame;
576 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
578 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
579 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
580 // is casted to use the default duration.
581 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
582 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
583 auto current = std::chrono::system_clock::now();
584 if( renderNow || mNextFrameStartTime < current )
586 mNextFrameStartTime = current;
588 return mNextFrameStartTime;
591 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
593 return mNextFrameStartTime;
596 } // namespace Internal
598 } // namespace Toolkit