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>
25 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
26 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
40 constexpr auto LOOP_FOREVER = -1;
41 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
47 template< typename T >
48 inline void ResetValue( bool& updated, T& value, T newValue, ConditionalWait& conditionalWait )
50 ConditionalWait::ScopedLock lock( conditionalWait );
58 } // unnamed namespace
60 VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url )
63 mVectorAnimationThread( factoryCache.GetVectorAnimationThread() ),
65 mAnimationFinishedTrigger(),
66 mPlayState( PlayState::STOPPED ),
67 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
68 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
69 mNextFrameStartTime(),
70 mFrameDurationNanoSeconds( 0 ),
78 mLoopCount( LOOP_FOREVER ),
80 mResourceReady( false ),
81 mCurrentFrameUpdated( false ),
82 mCurrentLoopUpdated( false ),
84 mUpdateFrameNumber( false ),
85 mNeedAnimationFinishedTrigger( true )
90 VectorAnimationTask::~VectorAnimationTask()
92 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
95 void VectorAnimationTask::Finalize()
97 ConditionalWait::ScopedLock lock( mConditionalWait );
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( uint32_t startFrame, uint32_t endFrame )
208 // Make sure the range specified is between 0 and the total frame number
209 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
211 // If the range is not in order swap values
212 if( startFrame > endFrame )
214 uint32_t temp = startFrame;
215 startFrame = endFrame;
219 if( startFrame != mStartFrame || endFrame != mEndFrame )
221 ConditionalWait::ScopedLock lock( mConditionalWait );
223 mStartFrame = startFrame;
224 mEndFrame = endFrame;
226 // If the current frame is out of the range, change the current frame also.
227 if( mStartFrame > mCurrentFrame )
229 mCurrentFrame = mStartFrame;
231 mCurrentFrameUpdated = true;
232 mResourceReady = false;
234 else if( mEndFrame < mCurrentFrame )
236 mCurrentFrame = mEndFrame;
238 mCurrentFrameUpdated = true;
239 mResourceReady = false;
242 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
247 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
249 startFrame = mStartFrame;
250 endFrame = mEndFrame;
253 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
255 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
259 case PlayState::PLAYING:
261 state = DevelImageVisual::PlayState::PLAYING;
264 case PlayState::PAUSED:
266 state = DevelImageVisual::PlayState::PAUSED;
269 case PlayState::STOPPING:
270 case PlayState::STOPPED:
272 state = DevelImageVisual::PlayState::STOPPED;
280 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
282 ConditionalWait::ScopedLock lock( mConditionalWait );
284 if( mCurrentFrame == frameNumber )
286 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
290 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
292 mCurrentFrame = frameNumber;
293 mCurrentFrameUpdated = true;
295 mUpdateFrameNumber = false;
296 mResourceReady = false;
298 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
302 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
306 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
308 return mCurrentFrame;
311 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
316 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
318 mVectorRenderer.GetDefaultSize( width, height );
321 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
323 ConditionalWait::ScopedLock lock( mConditionalWait );
324 mStopBehavior = stopBehavior;
326 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
329 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
331 ConditionalWait::ScopedLock lock( mConditionalWait );
332 mLoopingMode = loopingMode;
334 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
337 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
339 mVectorRenderer.GetLayerInfo( map );
342 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
344 return mVectorRenderer.UploadCompletedSignal();
347 void VectorAnimationTask::Initialize()
349 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
351 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
353 mEndFrame = mTotalFrame - 1;
355 mFrameRate = mVectorRenderer.GetFrameRate();
356 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
358 uint32_t width, height;
359 mVectorRenderer.GetDefaultSize( width, height );
361 SetSize( width, height );
363 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
366 bool VectorAnimationTask::Rasterize()
368 bool stopped = false, needAnimationFinishedTrigger;
369 uint32_t currentFrame, startFrame, endFrame;
370 int32_t loopCount, currentLoopCount;
374 ConditionalWait::ScopedLock lock( mConditionalWait );
376 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
378 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
381 currentFrame = mCurrentFrame;
382 startFrame = mStartFrame;
383 endFrame = mEndFrame;
384 loopCount = mLoopCount;
385 currentLoopCount = mCurrentLoop;
386 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
387 playState = mPlayState;
389 mResourceReady = true;
390 mCurrentFrameUpdated = false;
391 mCurrentLoopUpdated = false;
392 mUpdateFrameNumber = true;
393 mNeedAnimationFinishedTrigger = true;
396 if( playState == PlayState::STOPPING )
398 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
399 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
403 else if( playState == PlayState::PLAYING )
405 bool animationFinished = false;
407 if( currentFrame >= endFrame ) // last frame
409 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
415 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
417 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
418 mUpdateFrameNumber = false;
422 animationFinished = true; // end of animation
424 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
427 else if( currentFrame == startFrame && !mForward ) // first frame
429 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
435 animationFinished = true; // end of animation
437 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
440 if( animationFinished )
442 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
448 mPlayState = PlayState::STOPPING;
454 bool renderSuccess = false;
455 if( mVectorRenderer )
457 renderSuccess = mVectorRenderer.Render( currentFrame );
460 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
461 mUpdateFrameNumber = false;
465 if( stopped && renderSuccess )
467 mPlayState = PlayState::STOPPED;
471 // Animation is finished
472 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
474 mAnimationFinishedTrigger->Trigger();
477 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
480 bool keepAnimation = true;
481 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
483 keepAnimation = false;
486 return keepAnimation;
489 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
491 uint32_t frame = currentFrame;
493 switch( mStopBehavior )
495 case DevelImageVisual::StopBehavior::FIRST_FRAME:
500 case DevelImageVisual::StopBehavior::LAST_FRAME:
502 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
512 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
514 frame = currentFrame;
522 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
524 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
525 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
526 // is casted to use the default duration.
527 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
528 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
529 auto current = std::chrono::system_clock::now();
530 if( renderNow || mNextFrameStartTime < current )
532 mNextFrameStartTime = current;
534 return mNextFrameStartTime;
537 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
539 return mNextFrameStartTime;
542 } // namespace Internal
544 } // namespace Toolkit