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 // Release some objects in the main thread
98 if( mAnimationFinishedTrigger )
100 mAnimationFinishedTrigger.reset();
103 mVectorRenderer.Finalize();
106 void VectorAnimationTask::SetRenderer( Renderer renderer )
108 ConditionalWait::ScopedLock lock( mConditionalWait );
110 mVectorRenderer.SetRenderer( renderer );
112 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
115 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
117 if( mWidth != width || mHeight != height )
119 ConditionalWait::ScopedLock lock( mConditionalWait );
120 mVectorRenderer.SetSize( width, height );
125 mResourceReady = false;
127 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
131 void VectorAnimationTask::PlayAnimation()
133 ConditionalWait::ScopedLock lock( mConditionalWait );
135 if( mPlayState != PlayState::PLAYING )
137 mUpdateFrameNumber = false;
138 mPlayState = PlayState::PLAYING;
140 mVectorAnimationThread.AddTask( this );
142 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
146 void VectorAnimationTask::StopAnimation()
148 ConditionalWait::ScopedLock lock( mConditionalWait );
149 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
151 mNeedAnimationFinishedTrigger = false;
152 mPlayState = PlayState::STOPPING;
154 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
158 void VectorAnimationTask::PauseAnimation()
160 ConditionalWait::ScopedLock lock( mConditionalWait );
161 if( mPlayState == PlayState::PLAYING )
163 mPlayState = PlayState::PAUSED;
165 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
169 void VectorAnimationTask::RenderFrame()
171 ConditionalWait::ScopedLock lock( mConditionalWait );
173 if( !mResourceReady )
175 mVectorAnimationThread.AddTask( this );
177 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
181 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
183 ConditionalWait::ScopedLock lock( mConditionalWait );
186 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
190 void VectorAnimationTask::SetLoopCount( int32_t count )
192 if( mLoopCount != count )
194 ConditionalWait::ScopedLock lock( mConditionalWait );
198 mCurrentLoopUpdated = true;
200 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
204 void VectorAnimationTask::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
206 // Make sure the range specified is between 0 and the total frame number
207 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
209 // If the range is not in order swap values
210 if( startFrame > endFrame )
212 uint32_t temp = startFrame;
213 startFrame = endFrame;
217 if( startFrame != mStartFrame || endFrame != mEndFrame )
219 ConditionalWait::ScopedLock lock( mConditionalWait );
221 mStartFrame = startFrame;
222 mEndFrame = endFrame;
224 // If the current frame is out of the range, change the current frame also.
225 if( mStartFrame > mCurrentFrame )
227 mCurrentFrame = mStartFrame;
229 mCurrentFrameUpdated = true;
230 mResourceReady = false;
232 else if( mEndFrame < mCurrentFrame )
234 mCurrentFrame = mEndFrame;
236 mCurrentFrameUpdated = true;
237 mResourceReady = false;
240 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
245 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
247 startFrame = mStartFrame;
248 endFrame = mEndFrame;
251 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
253 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
257 case PlayState::PLAYING:
259 state = DevelImageVisual::PlayState::PLAYING;
262 case PlayState::PAUSED:
264 state = DevelImageVisual::PlayState::PAUSED;
267 case PlayState::STOPPING:
268 case PlayState::STOPPED:
270 state = DevelImageVisual::PlayState::STOPPED;
278 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
280 ConditionalWait::ScopedLock lock( mConditionalWait );
282 if( mCurrentFrame == frameNumber )
284 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
288 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
290 mCurrentFrame = frameNumber;
291 mCurrentFrameUpdated = true;
293 mUpdateFrameNumber = false;
294 mResourceReady = false;
296 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
300 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
304 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
306 return mCurrentFrame;
309 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
314 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
316 mVectorRenderer.GetDefaultSize( width, height );
319 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
321 ConditionalWait::ScopedLock lock( mConditionalWait );
322 mStopBehavior = stopBehavior;
324 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
327 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
329 ConditionalWait::ScopedLock lock( mConditionalWait );
330 mLoopingMode = loopingMode;
332 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
335 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
337 mVectorRenderer.GetLayerInfo( map );
340 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
342 return mVectorRenderer.UploadCompletedSignal();
345 void VectorAnimationTask::Initialize()
347 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
349 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
351 mEndFrame = mTotalFrame - 1;
353 mFrameRate = mVectorRenderer.GetFrameRate();
354 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
356 uint32_t width, height;
357 mVectorRenderer.GetDefaultSize( width, height );
359 SetSize( width, height );
361 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
364 bool VectorAnimationTask::Rasterize()
366 bool stopped = false, needAnimationFinishedTrigger;
367 uint32_t currentFrame, startFrame, endFrame;
368 int32_t loopCount, currentLoopCount;
372 ConditionalWait::ScopedLock lock( mConditionalWait );
374 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
376 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
379 currentFrame = mCurrentFrame;
380 startFrame = mStartFrame;
381 endFrame = mEndFrame;
382 loopCount = mLoopCount;
383 currentLoopCount = mCurrentLoop;
384 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
385 playState = mPlayState;
387 mResourceReady = true;
388 mCurrentFrameUpdated = false;
389 mCurrentLoopUpdated = false;
390 mUpdateFrameNumber = true;
391 mNeedAnimationFinishedTrigger = true;
394 if( playState == PlayState::STOPPING )
396 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
397 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
401 else if( playState == PlayState::PLAYING )
403 bool animationFinished = false;
405 if( currentFrame >= endFrame ) // last frame
407 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
413 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
415 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
416 mUpdateFrameNumber = false;
420 animationFinished = true; // end of animation
422 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
425 else if( currentFrame == startFrame && !mForward ) // first frame
427 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
433 animationFinished = true; // end of animation
435 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
438 if( animationFinished )
440 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
446 mPlayState = PlayState::STOPPING;
452 bool renderSuccess = false;
453 if( mVectorRenderer )
455 renderSuccess = mVectorRenderer.Render( currentFrame );
458 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
459 mUpdateFrameNumber = false;
463 if( stopped && renderSuccess )
465 mPlayState = PlayState::STOPPED;
469 // Animation is finished
470 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
472 mAnimationFinishedTrigger->Trigger();
475 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
478 bool keepAnimation = true;
479 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
481 keepAnimation = false;
484 return keepAnimation;
487 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
489 uint32_t frame = currentFrame;
491 switch( mStopBehavior )
493 case DevelImageVisual::StopBehavior::FIRST_FRAME:
498 case DevelImageVisual::StopBehavior::LAST_FRAME:
500 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
510 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
512 frame = currentFrame;
520 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
522 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
523 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
524 // is casted to use the default duration.
525 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
526 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
527 auto current = std::chrono::system_clock::now();
528 if( renderNow || mNextFrameStartTime < current )
530 mNextFrameStartTime = current;
532 return mNextFrameStartTime;
535 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
537 return mNextFrameStartTime;
540 } // namespace Internal
542 } // namespace Toolkit