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 #define CLAMP( x, low, high ) ( ( ( x ) > ( high ) ) ? ( high ) : ( ( ( x ) < ( low ) ) ? ( low ) : ( x ) ) )
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
49 template< typename T >
50 inline void ResetValue( bool& updated, T& value, T newValue, ConditionalWait& conditionalWait )
52 ConditionalWait::ScopedLock lock( conditionalWait );
60 } // unnamed namespace
62 VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url )
65 mVectorAnimationThread( factoryCache.GetVectorAnimationThread() ),
67 mAnimationFinishedTrigger(),
68 mPlayState( PlayState::STOPPED ),
69 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
70 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
71 mNextFrameStartTime(),
72 mFrameDurationNanoSeconds( 0 ),
80 mLoopCount( LOOP_FOREVER ),
82 mResourceReady( false ),
83 mCurrentFrameUpdated( false ),
84 mCurrentLoopUpdated( false ),
86 mUpdateFrameNumber( false ),
87 mNeedAnimationFinishedTrigger( true )
92 VectorAnimationTask::~VectorAnimationTask()
94 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
97 void VectorAnimationTask::Finalize()
99 ConditionalWait::ScopedLock lock( mConditionalWait );
101 // Release some objects in the main thread
102 if( mAnimationFinishedTrigger )
104 mAnimationFinishedTrigger.reset();
107 mVectorRenderer.Finalize();
110 void VectorAnimationTask::SetRenderer( Renderer renderer )
112 ConditionalWait::ScopedLock lock( mConditionalWait );
114 mVectorRenderer.SetRenderer( renderer );
116 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
119 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
121 if( mWidth != width || mHeight != height )
123 ConditionalWait::ScopedLock lock( mConditionalWait );
124 mVectorRenderer.SetSize( width, height );
129 mResourceReady = false;
131 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
135 void VectorAnimationTask::PlayAnimation()
137 ConditionalWait::ScopedLock lock( mConditionalWait );
139 if( mPlayState != PlayState::PLAYING )
141 mUpdateFrameNumber = false;
142 mPlayState = PlayState::PLAYING;
144 mVectorAnimationThread.AddTask( this );
146 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
150 void VectorAnimationTask::StopAnimation()
152 ConditionalWait::ScopedLock lock( mConditionalWait );
153 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
155 mNeedAnimationFinishedTrigger = false;
156 mPlayState = PlayState::STOPPING;
158 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
162 void VectorAnimationTask::PauseAnimation()
164 ConditionalWait::ScopedLock lock( mConditionalWait );
165 if( mPlayState == PlayState::PLAYING )
167 mPlayState = PlayState::PAUSED;
169 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
173 void VectorAnimationTask::RenderFrame()
175 ConditionalWait::ScopedLock lock( mConditionalWait );
177 if( !mResourceReady )
179 mVectorAnimationThread.AddTask( this );
181 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
185 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
187 ConditionalWait::ScopedLock lock( mConditionalWait );
190 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
194 void VectorAnimationTask::SetLoopCount( int32_t count )
196 if( mLoopCount != count )
198 ConditionalWait::ScopedLock lock( mConditionalWait );
202 mCurrentLoopUpdated = true;
204 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
208 void VectorAnimationTask::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
210 // Make sure the range specified is between 0 and the total frame number
211 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
213 // If the range is not in order swap values
214 if( startFrame > endFrame )
216 uint32_t temp = startFrame;
217 startFrame = endFrame;
221 if( startFrame != mStartFrame || endFrame != mEndFrame )
223 ConditionalWait::ScopedLock lock( mConditionalWait );
225 mStartFrame = startFrame;
226 mEndFrame = endFrame;
228 // If the current frame is out of the range, change the current frame also.
229 if( mStartFrame > mCurrentFrame )
231 mCurrentFrame = mStartFrame;
233 mCurrentFrameUpdated = true;
234 mResourceReady = false;
236 else if( mEndFrame < mCurrentFrame )
238 mCurrentFrame = mEndFrame;
240 mCurrentFrameUpdated = true;
241 mResourceReady = false;
244 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
249 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
251 startFrame = mStartFrame;
252 endFrame = mEndFrame;
255 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
257 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
261 case PlayState::PLAYING:
263 state = DevelImageVisual::PlayState::PLAYING;
266 case PlayState::PAUSED:
268 state = DevelImageVisual::PlayState::PAUSED;
271 case PlayState::STOPPING:
272 case PlayState::STOPPED:
274 state = DevelImageVisual::PlayState::STOPPED;
282 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
284 ConditionalWait::ScopedLock lock( mConditionalWait );
286 if( mCurrentFrame == frameNumber )
288 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
292 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
294 mCurrentFrame = frameNumber;
295 mCurrentFrameUpdated = true;
297 mUpdateFrameNumber = false;
298 mResourceReady = false;
300 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
304 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
308 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
310 return mCurrentFrame;
313 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
318 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
320 mVectorRenderer.GetDefaultSize( width, height );
323 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
325 ConditionalWait::ScopedLock lock( mConditionalWait );
326 mStopBehavior = stopBehavior;
328 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
331 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
333 ConditionalWait::ScopedLock lock( mConditionalWait );
334 mLoopingMode = loopingMode;
336 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
339 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
341 mVectorRenderer.GetLayerInfo( map );
344 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
346 return mVectorRenderer.UploadCompletedSignal();
349 void VectorAnimationTask::Initialize()
351 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
353 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
355 mEndFrame = mTotalFrame - 1;
357 mFrameRate = mVectorRenderer.GetFrameRate();
358 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
360 uint32_t width, height;
361 mVectorRenderer.GetDefaultSize( width, height );
363 SetSize( width, height );
365 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
368 bool VectorAnimationTask::Rasterize()
370 bool stopped = false, needAnimationFinishedTrigger, resourceReady;
371 uint32_t currentFrame, startFrame, endFrame;
372 int32_t loopCount, currentLoopCount;
376 ConditionalWait::ScopedLock lock( mConditionalWait );
378 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
380 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
381 mCurrentFrame = CLAMP( mCurrentFrame, mStartFrame, mEndFrame );
384 currentFrame = mCurrentFrame;
385 startFrame = mStartFrame;
386 endFrame = mEndFrame;
387 loopCount = mLoopCount;
388 currentLoopCount = mCurrentLoop;
389 needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
390 playState = mPlayState;
391 resourceReady = mResourceReady;
393 mResourceReady = true;
394 mCurrentFrameUpdated = false;
395 mCurrentLoopUpdated = false;
396 mUpdateFrameNumber = true;
397 mNeedAnimationFinishedTrigger = true;
400 if( playState == PlayState::STOPPING )
402 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
403 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
407 else if( playState == PlayState::PLAYING )
409 bool animationFinished = false;
411 if( currentFrame >= endFrame ) // last frame
413 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
419 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
421 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
422 mUpdateFrameNumber = false;
426 animationFinished = true; // end of animation
428 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
431 else if( currentFrame == startFrame && !mForward ) // first frame
433 if( loopCount < 0 || ++currentLoopCount < loopCount ) // repeat forever or before the last loop
439 animationFinished = true; // end of animation
441 ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
444 if( animationFinished )
446 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
452 mPlayState = PlayState::STOPPING;
458 bool renderSuccess = false;
459 if( mVectorRenderer )
461 renderSuccess = mVectorRenderer.Render( currentFrame );
464 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
465 mUpdateFrameNumber = false;
469 ConditionalWait::ScopedLock lock( mConditionalWait );
470 mResourceReady = false;
475 if( stopped && renderSuccess )
477 ConditionalWait::ScopedLock lock( mConditionalWait );
479 mPlayState = PlayState::STOPPED;
483 // Animation is finished
484 if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
486 mAnimationFinishedTrigger->Trigger();
489 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
492 bool keepAnimation = true;
493 if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
495 keepAnimation = false;
498 return keepAnimation;
501 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
503 uint32_t frame = currentFrame;
505 switch( mStopBehavior )
507 case DevelImageVisual::StopBehavior::FIRST_FRAME:
512 case DevelImageVisual::StopBehavior::LAST_FRAME:
514 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
524 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
526 frame = currentFrame;
534 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
536 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
537 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
538 // is casted to use the default duration.
539 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
540 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
541 auto current = std::chrono::system_clock::now();
542 if( renderNow || mNextFrameStartTime < current )
544 mNextFrameStartTime = current;
546 return mNextFrameStartTime;
549 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
551 return mNextFrameStartTime;
554 } // namespace Internal
556 } // namespace Toolkit