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 ConditionalWait::ScopedLock lock( mConditionalWait );
100 // Release some objects in the main thread
101 if( mAnimationFinishedTrigger )
103 mAnimationFinishedTrigger.reset();
106 mVectorRenderer.Finalize();
109 void VectorAnimationTask::SetRenderer( Renderer renderer )
111 ConditionalWait::ScopedLock lock( mConditionalWait );
113 mVectorRenderer.SetRenderer( renderer );
115 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
118 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
120 if( mWidth != width || mHeight != height )
122 ConditionalWait::ScopedLock lock( mConditionalWait );
123 mVectorRenderer.SetSize( width, height );
128 mResourceReady = false;
130 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
134 void VectorAnimationTask::PlayAnimation()
136 ConditionalWait::ScopedLock lock( mConditionalWait );
138 if( mPlayState != PlayState::PLAYING )
140 mUpdateFrameNumber = false;
141 mPlayState = PlayState::PLAYING;
143 mVectorAnimationThread.AddTask( this );
145 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
149 void VectorAnimationTask::StopAnimation()
151 ConditionalWait::ScopedLock lock( mConditionalWait );
152 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
154 mNeedAnimationFinishedTrigger = false;
155 mPlayState = PlayState::STOPPING;
157 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
161 void VectorAnimationTask::PauseAnimation()
163 ConditionalWait::ScopedLock lock( mConditionalWait );
164 if( mPlayState == PlayState::PLAYING )
166 mPlayState = PlayState::PAUSED;
168 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
172 void VectorAnimationTask::RenderFrame()
174 ConditionalWait::ScopedLock lock( mConditionalWait );
176 if( !mResourceReady )
178 mVectorAnimationThread.AddTask( this );
180 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
184 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
186 ConditionalWait::ScopedLock lock( mConditionalWait );
189 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
193 void VectorAnimationTask::SetLoopCount( int32_t count )
195 if( mLoopCount != count )
197 ConditionalWait::ScopedLock lock( mConditionalWait );
201 mCurrentLoopUpdated = true;
203 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
207 void VectorAnimationTask::SetPlayRange( Property::Array& playRange )
209 ConditionalWait::ScopedLock lock( mConditionalWait );
212 uint32_t startFrame = 0, endFrame = 0;
213 size_t count = playRange.Count();
217 int32_t start = 0, end = 0;
218 if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
220 startFrame = static_cast< uint32_t >( start );
221 endFrame = static_cast< uint32_t >( end );
226 std::string startMarker, endMarker;
227 if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
229 if( mVectorRenderer )
231 uint32_t frame; // We don't use this later
232 if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
240 else if( count == 1 )
243 if( playRange.GetElementAt( 0 ).Get( marker ) )
245 if( mVectorRenderer )
247 mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
255 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
259 // Make sure the range specified is between 0 and the total frame number
260 if( startFrame >= 0 && startFrame < mTotalFrame && endFrame >= 0 && endFrame < mTotalFrame )
262 // If the range is not in order swap values
263 if( startFrame > endFrame )
265 uint32_t temp = startFrame;
266 startFrame = endFrame;
270 if( startFrame != mStartFrame || endFrame != mEndFrame )
272 mStartFrame = startFrame;
273 mEndFrame = endFrame;
275 // If the current frame is out of the range, change the current frame also.
276 if( mStartFrame > mCurrentFrame )
278 mCurrentFrame = mStartFrame;
280 mCurrentFrameUpdated = true;
281 mResourceReady = false;
283 else if( mEndFrame < mCurrentFrame )
285 mCurrentFrame = mEndFrame;
287 mCurrentFrameUpdated = true;
288 mResourceReady = false;
291 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
296 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this );
301 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
303 startFrame = mStartFrame;
304 endFrame = mEndFrame;
307 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
309 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
313 case PlayState::PLAYING:
315 state = DevelImageVisual::PlayState::PLAYING;
318 case PlayState::PAUSED:
320 state = DevelImageVisual::PlayState::PAUSED;
323 case PlayState::STOPPING:
324 case PlayState::STOPPED:
326 state = DevelImageVisual::PlayState::STOPPED;
334 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
336 ConditionalWait::ScopedLock lock( mConditionalWait );
338 if( mCurrentFrame == frameNumber )
340 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
344 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
346 mCurrentFrame = frameNumber;
347 mCurrentFrameUpdated = true;
349 mUpdateFrameNumber = false;
350 mResourceReady = false;
352 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
356 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
360 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
362 return mCurrentFrame;
365 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
370 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
372 mVectorRenderer.GetDefaultSize( width, height );
375 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
377 ConditionalWait::ScopedLock lock( mConditionalWait );
378 mStopBehavior = stopBehavior;
380 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
383 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
385 ConditionalWait::ScopedLock lock( mConditionalWait );
386 mLoopingMode = loopingMode;
388 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
391 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
393 mVectorRenderer.GetLayerInfo( map );
396 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
398 return mVectorRenderer.UploadCompletedSignal();
401 void VectorAnimationTask::Initialize()
403 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
405 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
407 mEndFrame = mTotalFrame - 1;
409 mFrameRate = mVectorRenderer.GetFrameRate();
410 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
412 uint32_t width, height;
413 mVectorRenderer.GetDefaultSize( width, height );
415 SetSize( width, height );
417 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
420 bool VectorAnimationTask::Rasterize()
422 bool stopped = false, needAnimationFinishedTrigger, resourceReady;
423 uint32_t currentFrame, startFrame, endFrame;
424 int32_t loopCount, currentLoopCount;
428 ConditionalWait::ScopedLock lock( mConditionalWait );
430 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
432 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
435 currentFrame = mCurrentFrame;
436 startFrame = mStartFrame;
437 endFrame = mEndFrame;
438 loopCount = mLoopCount;
439 currentLoopCount = mCurrentLoop;
440 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
441 playState = mPlayState;
442 resourceReady = mResourceReady;
444 mResourceReady = true;
445 mCurrentFrameUpdated = false;
446 mCurrentLoopUpdated = false;
447 mUpdateFrameNumber = true;
448 mNeedAnimationFinishedTrigger = true;
451 if( playState == PlayState::STOPPING )
453 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
454 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
458 else if( playState == PlayState::PLAYING )
460 bool animationFinished = false;
462 if( currentFrame >= endFrame ) // last frame
464 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
470 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
472 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
473 mUpdateFrameNumber = false;
477 animationFinished = true; // end of animation
479 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
482 else if( currentFrame == startFrame && !mForward ) // first frame
484 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
490 animationFinished = true; // end of animation
492 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
495 if( animationFinished )
497 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
503 mPlayState = PlayState::STOPPING;
509 bool renderSuccess = false;
510 if( mVectorRenderer )
512 renderSuccess = mVectorRenderer.Render( currentFrame );
515 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
516 mUpdateFrameNumber = false;
520 ConditionalWait::ScopedLock lock( mConditionalWait );
521 mResourceReady = false;
526 if( stopped && renderSuccess )
528 ConditionalWait::ScopedLock lock( mConditionalWait );
530 mPlayState = PlayState::STOPPED;
534 // Animation is finished
535 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
537 mAnimationFinishedTrigger->Trigger();
540 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
543 bool keepAnimation = true;
544 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
546 keepAnimation = false;
549 return keepAnimation;
552 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
554 uint32_t frame = currentFrame;
556 switch( mStopBehavior )
558 case DevelImageVisual::StopBehavior::FIRST_FRAME:
563 case DevelImageVisual::StopBehavior::LAST_FRAME:
565 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
575 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
577 frame = currentFrame;
585 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
587 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
588 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
589 // is casted to use the default duration.
590 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
591 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
592 auto current = std::chrono::system_clock::now();
593 if( renderNow || mNextFrameStartTime < current )
595 mNextFrameStartTime = current;
597 return mNextFrameStartTime;
600 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
602 return mNextFrameStartTime;
605 } // namespace Internal
607 } // namespace Toolkit