2 * Copyright (c) 2020 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>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/math/math-utils.h>
27 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
28 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
42 constexpr auto LOOP_FOREVER = -1;
43 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
49 } // unnamed namespace
51 VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url )
55 mVectorAnimationThread( factoryCache.GetVectorAnimationThread() ),
57 mAnimationFinishedTrigger(),
58 mPlayState( PlayState::STOPPED ),
59 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
60 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
61 mNextFrameStartTime(),
62 mFrameDurationNanoSeconds( 0 ),
70 mAnimationDataIndex( 0 ),
71 mLoopCount( LOOP_FOREVER ),
73 mResourceReady( false ),
75 mUpdateFrameNumber( false ),
76 mNeedAnimationFinishedTrigger( true ),
77 mAnimationDataUpdated( false )
82 VectorAnimationTask::~VectorAnimationTask()
84 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
87 void VectorAnimationTask::Finalize()
89 ConditionalWait::ScopedLock lock( mConditionalWait );
91 // Release some objects in the main thread
92 if( mAnimationFinishedTrigger )
94 mAnimationFinishedTrigger.reset();
97 mVectorRenderer.Finalize();
100 void VectorAnimationTask::SetRenderer( Renderer renderer )
102 ConditionalWait::ScopedLock lock( mConditionalWait );
104 mVectorRenderer.SetRenderer( renderer );
106 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
109 void VectorAnimationTask::SetAnimationData( const AnimationData& data )
111 ConditionalWait::ScopedLock lock( mConditionalWait );
113 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetAnimationData [%p]\n", this );
115 uint32_t index = mAnimationDataIndex == 0 ? 1 : 0; // Use the other buffer
117 mAnimationData[index] = data;
118 mAnimationDataUpdated = true;
120 if( data.resendFlag & VectorAnimationTask::RESEND_SIZE )
122 // The size should be changed in the main thread.
123 SetSize( data.width, data.height );
126 mVectorAnimationThread.AddTask( this );
129 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
131 if( mWidth != width || mHeight != height )
133 mVectorRenderer.SetSize( width, height );
138 mResourceReady = false;
140 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
144 void VectorAnimationTask::PlayAnimation()
146 if( mPlayState != PlayState::PLAYING )
148 mUpdateFrameNumber = false;
149 mPlayState = PlayState::PLAYING;
151 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
155 void VectorAnimationTask::StopAnimation()
157 if( mPlayState != PlayState::STOPPING )
159 mNeedAnimationFinishedTrigger = false;
160 mPlayState = PlayState::STOPPING;
162 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
166 void VectorAnimationTask::PauseAnimation()
168 if( mPlayState == PlayState::PLAYING )
170 mPlayState = PlayState::PAUSED;
172 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
176 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
178 ConditionalWait::ScopedLock lock( mConditionalWait );
181 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
185 void VectorAnimationTask::SetLoopCount( int32_t count )
187 if( mLoopCount != count )
192 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
196 void VectorAnimationTask::SetPlayRange( const Property::Array& playRange )
199 uint32_t startFrame = 0, endFrame = 0;
200 size_t count = playRange.Count();
204 int32_t start = 0, end = 0;
205 if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
207 startFrame = static_cast< uint32_t >( start );
208 endFrame = static_cast< uint32_t >( end );
213 std::string startMarker, endMarker;
214 if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
216 if( mVectorRenderer )
218 uint32_t frame; // We don't use this later
219 if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
227 else if( count == 1 )
230 if( playRange.GetElementAt( 0 ).Get( marker ) )
232 if( mVectorRenderer )
234 mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
242 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
246 // Make sure the range specified is between 0 and the total frame number
247 if( startFrame < mTotalFrame && endFrame < mTotalFrame )
249 // If the range is not in order swap values
250 if( startFrame > endFrame )
252 uint32_t temp = startFrame;
253 startFrame = endFrame;
257 if( startFrame != mStartFrame || endFrame != mEndFrame )
259 mStartFrame = startFrame;
260 mEndFrame = endFrame;
262 // If the current frame is out of the range, change the current frame also.
263 if( mStartFrame > mCurrentFrame )
265 mCurrentFrame = mStartFrame;
266 mResourceReady = false;
268 else if( mEndFrame < mCurrentFrame )
270 mCurrentFrame = mEndFrame;
271 mResourceReady = false;
274 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
279 DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this );
284 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
286 startFrame = mStartFrame;
287 endFrame = mEndFrame;
290 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
292 if( mCurrentFrame == frameNumber )
294 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
298 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
300 mCurrentFrame = frameNumber;
301 mUpdateFrameNumber = false;
302 mResourceReady = false;
304 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
308 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
312 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
314 return mCurrentFrame;
317 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
322 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
324 mVectorRenderer.GetDefaultSize( width, height );
327 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
329 mStopBehavior = stopBehavior;
331 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
334 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
336 mLoopingMode = loopingMode;
338 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
341 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
343 mVectorRenderer.GetLayerInfo( map );
346 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
348 return mVectorRenderer.UploadCompletedSignal();
351 void VectorAnimationTask::Initialize()
353 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
355 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
357 mEndFrame = mTotalFrame - 1;
359 mFrameRate = mVectorRenderer.GetFrameRate();
360 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
362 uint32_t width, height;
363 mVectorRenderer.GetDefaultSize( width, height );
365 SetSize( width, height );
367 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
370 bool VectorAnimationTask::Rasterize()
372 bool stopped = false;
373 uint32_t currentFrame;
375 ApplyAnimationData();
377 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
379 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
380 Dali::ClampInPlace( mCurrentFrame, mStartFrame, mEndFrame );
383 currentFrame = mCurrentFrame;
386 mResourceReady = true;
387 mUpdateFrameNumber = true;
388 mNeedAnimationFinishedTrigger = true;
390 if( mPlayState == PlayState::STOPPING )
392 mCurrentFrame = GetStoppedFrame( mStartFrame, mEndFrame, mCurrentFrame );
393 currentFrame = mCurrentFrame;
396 else if( mPlayState == PlayState::PLAYING )
398 bool animationFinished = false;
400 if( currentFrame >= mEndFrame ) // last frame
402 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
408 if( mLoopCount < 0 || ++mCurrentLoop < mLoopCount ) // repeat forever or before the last loop
410 mCurrentFrame = mStartFrame;
411 mUpdateFrameNumber = false;
415 animationFinished = true; // end of animation
419 else if( currentFrame == mStartFrame && !mForward ) // first frame
421 if( mLoopCount < 0 || ++mCurrentLoop < mLoopCount ) // repeat forever or before the last loop
427 animationFinished = true; // end of animation
431 if( animationFinished )
433 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
439 mPlayState = PlayState::STOPPING;
445 bool renderSuccess = false;
446 if( mVectorRenderer )
448 renderSuccess = mVectorRenderer.Render( currentFrame );
451 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
452 mUpdateFrameNumber = false;
454 if( !mResourceReady )
456 mResourceReady = false;
461 if( stopped && renderSuccess )
463 mPlayState = PlayState::STOPPED;
467 // Animation is finished
469 ConditionalWait::ScopedLock lock( mConditionalWait );
470 if( mNeedAnimationFinishedTrigger && mAnimationFinishedTrigger )
472 mAnimationFinishedTrigger->Trigger();
476 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
479 bool keepAnimation = true;
480 if( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED )
482 keepAnimation = false;
485 return keepAnimation;
488 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
490 uint32_t frame = currentFrame;
492 switch( mStopBehavior )
494 case DevelImageVisual::StopBehavior::FIRST_FRAME:
499 case DevelImageVisual::StopBehavior::LAST_FRAME:
501 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
511 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
513 frame = currentFrame;
521 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
523 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
524 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
525 // is casted to use the default duration.
526 mNextFrameStartTime = std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
527 mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
528 auto current = std::chrono::system_clock::now();
529 if( renderNow || mNextFrameStartTime < current )
531 mNextFrameStartTime = current;
533 return mNextFrameStartTime;
536 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
538 return mNextFrameStartTime;
541 void VectorAnimationTask::ApplyAnimationData()
546 ConditionalWait::ScopedLock lock( mConditionalWait );
548 if( !mAnimationDataUpdated || mAnimationData[mAnimationDataIndex].resendFlag != 0 )
550 // Data is not updated or the previous data is not applied yet.
554 mAnimationDataIndex = mAnimationDataIndex == 0 ? 1 : 0; // Swap index
555 mAnimationDataUpdated = false;
557 index = mAnimationDataIndex;
560 if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT )
562 SetLoopCount( mAnimationData[index].loopCount );
565 if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE )
567 SetPlayRange( mAnimationData[index].playRange );
570 if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR )
572 SetStopBehavior( mAnimationData[index].stopBehavior );
575 if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE )
577 SetLoopingMode( mAnimationData[index].loopingMode );
580 if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME )
582 SetCurrentFrameNumber( mAnimationData[index].currentFrame );
585 if( mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE )
587 if( mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING )
591 else if( mAnimationData[index].playState == DevelImageVisual::PlayState::PAUSED )
595 else if( mAnimationData[index].playState == DevelImageVisual::PlayState::STOPPED )
601 mAnimationData[index].resendFlag = 0;
604 } // namespace Internal
606 } // namespace Toolkit