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::SetRenderer( Renderer renderer )
97 ConditionalWait::ScopedLock lock( mConditionalWait );
99 mVectorRenderer.SetRenderer( renderer );
101 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
104 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
106 if( mWidth != width || mHeight != height )
108 ConditionalWait::ScopedLock lock( mConditionalWait );
109 mVectorRenderer.SetSize( width, height );
114 mResourceReady = false;
116 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
120 void VectorAnimationTask::PlayAnimation()
122 ConditionalWait::ScopedLock lock( mConditionalWait );
124 if( mPlayState != PlayState::PLAYING )
126 mUpdateFrameNumber = false;
127 mPlayState = PlayState::PLAYING;
129 mVectorAnimationThread.AddTask( this );
131 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
135 void VectorAnimationTask::StopAnimation()
137 ConditionalWait::ScopedLock lock( mConditionalWait );
138 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
140 mNeedAnimationFinishedTrigger = false;
141 mPlayState = PlayState::STOPPING;
143 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
147 void VectorAnimationTask::PauseAnimation()
149 ConditionalWait::ScopedLock lock( mConditionalWait );
150 if( mPlayState == PlayState::PLAYING )
152 mPlayState = PlayState::PAUSED;
154 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
158 void VectorAnimationTask::RenderFrame()
160 ConditionalWait::ScopedLock lock( mConditionalWait );
162 if( !mResourceReady )
164 mVectorAnimationThread.AddTask( this );
166 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
170 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
172 ConditionalWait::ScopedLock lock( mConditionalWait );
173 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
176 void VectorAnimationTask::SetLoopCount( int32_t count )
178 if( mLoopCount != count )
180 ConditionalWait::ScopedLock lock( mConditionalWait );
184 mCurrentLoopUpdated = true;
186 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
190 void VectorAnimationTask::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
192 // Make sure the range specified is between 0 and the total frame number
193 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
195 // If the range is not in order swap values
196 if( startFrame > endFrame )
198 uint32_t temp = startFrame;
199 startFrame = endFrame;
203 if( startFrame != mStartFrame || endFrame != mEndFrame )
205 ConditionalWait::ScopedLock lock( mConditionalWait );
207 mStartFrame = startFrame;
208 mEndFrame = endFrame;
210 // If the current frame is out of the range, change the current frame also.
211 if( mStartFrame > mCurrentFrame )
213 mCurrentFrame = mStartFrame;
215 mCurrentFrameUpdated = true;
216 mResourceReady = false;
218 else if( mEndFrame < mCurrentFrame )
220 mCurrentFrame = mEndFrame;
222 mCurrentFrameUpdated = true;
223 mResourceReady = false;
226 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
231 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
233 startFrame = mStartFrame;
234 endFrame = mEndFrame;
237 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
239 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
243 case PlayState::PLAYING:
245 state = DevelImageVisual::PlayState::PLAYING;
248 case PlayState::PAUSED:
250 state = DevelImageVisual::PlayState::PAUSED;
253 case PlayState::STOPPING:
254 case PlayState::STOPPED:
256 state = DevelImageVisual::PlayState::STOPPED;
264 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
266 ConditionalWait::ScopedLock lock( mConditionalWait );
268 if( mCurrentFrame == frameNumber )
270 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
274 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
276 mCurrentFrame = frameNumber;
277 mCurrentFrameUpdated = true;
279 mResourceReady = false;
281 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
285 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
289 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
291 return mCurrentFrame;
294 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
299 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
301 mVectorRenderer.GetDefaultSize( width, height );
304 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
306 ConditionalWait::ScopedLock lock( mConditionalWait );
307 mStopBehavior = stopBehavior;
309 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
312 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
314 ConditionalWait::ScopedLock lock( mConditionalWait );
315 mLoopingMode = loopingMode;
317 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
320 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
322 mVectorRenderer.GetLayerInfo( map );
325 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
327 return mVectorRenderer.UploadCompletedSignal();
330 void VectorAnimationTask::Initialize()
332 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
334 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
336 mEndFrame = mTotalFrame - 1;
338 mFrameRate = mVectorRenderer.GetFrameRate();
339 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
341 uint32_t width, height;
342 mVectorRenderer.GetDefaultSize( width, height );
344 SetSize( width, height );
346 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
349 bool VectorAnimationTask::Rasterize()
351 bool stopped = false, needAnimationFinishedTrigger;
352 uint32_t currentFrame, startFrame, endFrame;
353 int32_t loopCount, currentLoopCount;
357 ConditionalWait::ScopedLock lock( mConditionalWait );
359 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
361 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
364 currentFrame = mCurrentFrame;
365 startFrame = mStartFrame;
366 endFrame = mEndFrame;
367 loopCount = mLoopCount;
368 currentLoopCount = mCurrentLoop;
369 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
370 playState = mPlayState;
372 mResourceReady = true;
373 mCurrentFrameUpdated = false;
374 mCurrentLoopUpdated = false;
375 mUpdateFrameNumber = true;
376 mNeedAnimationFinishedTrigger = true;
379 if( playState == PlayState::STOPPING )
381 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
382 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
386 else if( playState == PlayState::PLAYING )
388 bool animationFinished = false;
390 if( currentFrame >= endFrame ) // last frame
392 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
398 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
400 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
401 mUpdateFrameNumber = false;
405 animationFinished = true; // end of animation
407 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
410 else if( currentFrame == startFrame && !mForward ) // first frame
412 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
418 animationFinished = true; // end of animation
420 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
423 if( animationFinished )
425 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
431 mPlayState = PlayState::STOPPING;
437 bool renderSuccess = mVectorRenderer.Render( currentFrame );
440 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
441 mUpdateFrameNumber = false;
444 if( stopped && renderSuccess )
446 mPlayState = PlayState::STOPPED;
450 // Animation is finished
451 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
453 mAnimationFinishedTrigger->Trigger();
456 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
459 bool keepAnimation = true;
460 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
462 keepAnimation = false;
465 return keepAnimation;
468 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
470 uint32_t frame = currentFrame;
472 switch( mStopBehavior )
474 case DevelImageVisual::StopBehavior::FIRST_FRAME:
479 case DevelImageVisual::StopBehavior::LAST_FRAME:
481 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
491 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
493 frame = currentFrame;
501 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
503 mNextFrameStartTime = mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
504 auto current = std::chrono::system_clock::now();
505 if( renderNow || mNextFrameStartTime < current )
507 mNextFrameStartTime = current;
509 return mNextFrameStartTime;
512 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
514 return mNextFrameStartTime;
517 } // namespace Internal
519 } // namespace Toolkit