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 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
322 return mVectorRenderer.UploadCompletedSignal();
325 void VectorAnimationTask::Initialize()
327 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
329 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
331 mEndFrame = mTotalFrame - 1;
333 mFrameRate = mVectorRenderer.GetFrameRate();
334 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
336 uint32_t width, height;
337 mVectorRenderer.GetDefaultSize( width, height );
339 SetSize( width, height );
341 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
344 bool VectorAnimationTask::Rasterize()
346 bool stopped = false, needAnimationFinishedTrigger;
347 uint32_t currentFrame, startFrame, endFrame;
348 int32_t loopCount, currentLoopCount;
352 ConditionalWait::ScopedLock lock( mConditionalWait );
354 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
356 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
359 currentFrame = mCurrentFrame;
360 startFrame = mStartFrame;
361 endFrame = mEndFrame;
362 loopCount = mLoopCount;
363 currentLoopCount = mCurrentLoop;
364 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
365 playState = mPlayState;
367 mResourceReady = true;
368 mCurrentFrameUpdated = false;
369 mCurrentLoopUpdated = false;
370 mUpdateFrameNumber = true;
371 mNeedAnimationFinishedTrigger = true;
374 if( playState == PlayState::STOPPING )
376 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
377 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
381 else if( playState == PlayState::PLAYING )
383 bool animationFinished = false;
385 if( currentFrame >= endFrame ) // last frame
387 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
393 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
395 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
396 mUpdateFrameNumber = false;
400 animationFinished = true; // end of animation
402 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
405 else if( currentFrame == startFrame && !mForward ) // first frame
407 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
413 animationFinished = true; // end of animation
415 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
418 if( animationFinished )
420 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
426 mPlayState = PlayState::STOPPING;
432 bool renderSuccess = mVectorRenderer.Render( currentFrame );
435 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
436 mUpdateFrameNumber = false;
439 if( stopped && renderSuccess )
441 mPlayState = PlayState::STOPPED;
445 // Animation is finished
446 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
448 mAnimationFinishedTrigger->Trigger();
451 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
454 bool keepAnimation = true;
455 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
457 keepAnimation = false;
460 return keepAnimation;
463 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
465 uint32_t frame = currentFrame;
467 switch( mStopBehavior )
469 case DevelImageVisual::StopBehavior::FIRST_FRAME:
474 case DevelImageVisual::StopBehavior::LAST_FRAME:
476 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
486 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
488 frame = currentFrame;
496 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
498 mNextFrameStartTime = mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
499 auto current = std::chrono::system_clock::now();
500 if( renderNow || mNextFrameStartTime < current )
502 mNextFrameStartTime = current;
504 return mNextFrameStartTime;
507 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
509 return mNextFrameStartTime;
512 } // namespace Internal
514 } // namespace Toolkit