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-rasterize-thread.h>
22 #include <dali/devel-api/adaptor-framework/thread-settings.h>
23 #include <dali/integration-api/adaptors/adaptor.h>
24 #include <dali/integration-api/debug.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 inline void ResetValue( bool& updated, uint32_t& value, uint32_t newValue, ConditionalWait& conditionalWait )
49 ConditionalWait::ScopedLock lock( conditionalWait );
57 } // unnamed namespace
59 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
63 mResourceReadyTrigger(),
64 mAnimationFinishedTrigger(),
65 mPlayState( PlayState::STOPPED ),
66 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
67 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
68 mFrameDurationNanoSeconds( 0 ),
76 mLoopCount( LOOP_FOREVER ),
79 mDestroyThread( false ),
80 mResourceReady( false ),
81 mCurrentFrameUpdated( false ),
83 mUpdateFrameNumber( false ),
84 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
89 VectorRasterizeThread::~VectorRasterizeThread()
93 ConditionalWait::ScopedLock lock( mConditionalWait );
94 mDestroyThread = true;
95 mConditionalWait.Notify( lock );
98 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this );
103 void VectorRasterizeThread::Run()
105 SetThreadName( "VectorImageThread" );
106 mLogFactory.InstallLogFunction();
108 while( !mDestroyThread )
113 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread [%p]\n", this );
116 void VectorRasterizeThread::SetRenderer( Renderer renderer )
118 ConditionalWait::ScopedLock lock( mConditionalWait );
120 mVectorRenderer.SetRenderer( renderer );
122 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer [%p]\n", this );
125 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
127 if( mWidth != width || mHeight != height )
129 ConditionalWait::ScopedLock lock( mConditionalWait );
130 mVectorRenderer.SetSize( width, height );
135 mResourceReady = false;
137 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d [%p]\n", width, height, this );
141 void VectorRasterizeThread::PlayAnimation()
143 ConditionalWait::ScopedLock lock( mConditionalWait );
145 if( mPlayState != PlayState::PLAYING )
148 mUpdateFrameNumber = false;
149 mPlayState = PlayState::PLAYING;
150 mConditionalWait.Notify( lock );
152 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play [%p]\n", this );
156 void VectorRasterizeThread::StopAnimation()
158 ConditionalWait::ScopedLock lock( mConditionalWait );
159 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
162 mPlayState = PlayState::STOPPING;
163 mConditionalWait.Notify( lock );
165 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop [%p]\n", this );
169 void VectorRasterizeThread::PauseAnimation()
171 ConditionalWait::ScopedLock lock( mConditionalWait );
172 if( mPlayState == PlayState::PLAYING )
174 mPlayState = PlayState::PAUSED;
176 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause [%p]\n", this );
180 void VectorRasterizeThread::RenderFrame()
182 ConditionalWait::ScopedLock lock( mConditionalWait );
184 if( !mResourceReady )
187 mConditionalWait.Notify( lock );
189 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render [%p]\n", this );
193 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
195 ConditionalWait::ScopedLock lock( mConditionalWait );
196 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
199 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
201 ConditionalWait::ScopedLock lock( mConditionalWait );
202 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
205 void VectorRasterizeThread::SetLoopCount( int32_t count )
207 if( mLoopCount != count )
209 ConditionalWait::ScopedLock lock( mConditionalWait );
213 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d] [%p]\n", count, this );
217 void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
219 // Make sure the range specified is between 0 and the total frame number
220 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
222 // If the range is not in order swap values
223 if( startFrame > endFrame )
225 uint32_t temp = startFrame;
226 startFrame = endFrame;
230 if( startFrame != mStartFrame || endFrame != mEndFrame )
232 ConditionalWait::ScopedLock lock( mConditionalWait );
234 mStartFrame = startFrame;
235 mEndFrame = endFrame;
237 // If the current frame is out of the range, change the current frame also.
238 if( mStartFrame > mCurrentFrame )
240 mCurrentFrame = mStartFrame;
242 mCurrentFrameUpdated = true;
243 mResourceReady = false;
245 else if( mEndFrame < mCurrentFrame )
247 mCurrentFrame = mEndFrame;
249 mCurrentFrameUpdated = true;
250 mResourceReady = false;
253 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
258 DevelImageVisual::PlayState::Type VectorRasterizeThread::GetPlayState() const
260 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
264 case PlayState::PLAYING:
266 state = DevelImageVisual::PlayState::PLAYING;
269 case PlayState::PAUSED:
271 state = DevelImageVisual::PlayState::PAUSED;
274 case PlayState::STOPPING:
275 case PlayState::STOPPED:
277 state = DevelImageVisual::PlayState::STOPPED;
285 bool VectorRasterizeThread::IsResourceReady() const
287 return mResourceReady;
290 void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
292 ConditionalWait::ScopedLock lock( mConditionalWait );
294 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame && mCurrentFrame != frameNumber )
296 mCurrentFrame = frameNumber;
297 mCurrentFrameUpdated = true;
299 mResourceReady = false;
301 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
305 DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
309 uint32_t VectorRasterizeThread::GetCurrentFrameNumber() const
311 return mCurrentFrame;
314 uint32_t VectorRasterizeThread::GetTotalFrameNumber() const
319 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
321 mVectorRenderer.GetDefaultSize( width, height );
324 void VectorRasterizeThread::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
326 ConditionalWait::ScopedLock lock( mConditionalWait );
327 mStopBehavior = stopBehavior;
329 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
332 void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
334 ConditionalWait::ScopedLock lock( mConditionalWait );
335 mLoopingMode = loopingMode;
337 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
340 void VectorRasterizeThread::Initialize()
342 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
344 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
346 mEndFrame = mTotalFrame - 1;
348 mFrameRate = mVectorRenderer.GetFrameRate();
349 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
351 uint32_t width, height;
352 mVectorRenderer.GetDefaultSize( width, height );
354 SetSize( width, height );
356 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
359 void VectorRasterizeThread::Rasterize()
361 bool resourceReady, stopped = false;
362 uint32_t currentFrame, startFrame, endFrame;
366 ConditionalWait::ScopedLock lock( mConditionalWait );
368 if( ( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED ) && !mNeedRender && !mDestroyThread )
370 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait [%p]\n", this );
371 mConditionalWait.Wait( lock );
374 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
376 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
379 resourceReady = mResourceReady;
380 currentFrame = mCurrentFrame;
381 startFrame = mStartFrame;
382 endFrame = mEndFrame;
383 loopCount = mLoopCount;
386 mResourceReady = true;
387 mCurrentFrameUpdated = false;
388 mUpdateFrameNumber = true;
391 auto currentFrameStartTime = std::chrono::system_clock::now();
393 if( mPlayState == PlayState::STOPPING )
395 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
396 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
400 else if( mPlayState == PlayState::PLAYING )
402 bool animationFinished = false;
404 if( currentFrame >= endFrame ) // last frame
406 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
412 if( loopCount < 0 || ++mCurrentLoop < loopCount ) // repeat forever or before the last loop
414 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
415 mUpdateFrameNumber = false;
419 animationFinished = true; // end of animation
423 else if( currentFrame == startFrame && !mForward ) // first frame
425 if( loopCount < 0 || ++mCurrentLoop < loopCount ) // repeat forever or before the last loop
431 animationFinished = true; // end of animation
435 if( animationFinished )
437 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
443 mPlayState = PlayState::STOPPING;
449 if( !mVectorRenderer.Render( currentFrame ) )
451 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
452 mUpdateFrameNumber = false;
457 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger [current = %d] [%p]\n", currentFrame, this );
459 mResourceReadyTrigger->Trigger();
464 mPlayState = PlayState::STOPPED;
468 // Animation is finished
469 mAnimationFinishedTrigger->Trigger();
471 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
474 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
476 #if defined(DEBUG_ENABLED)
477 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
479 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld] [%p]\n", currentFrame, sleepDuration.count(), this );
482 std::this_thread::sleep_until( timeToSleepUntil );
485 uint32_t VectorRasterizeThread::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
487 uint32_t frame = currentFrame;
489 switch( mStopBehavior )
491 case DevelImageVisual::StopBehavior::FIRST_FRAME:
496 case DevelImageVisual::StopBehavior::LAST_FRAME:
498 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
508 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
510 frame = currentFrame;
518 } // namespace Internal
520 } // namespace Toolkit