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 );
97 // This should be called in the main thread to stop waiting for the dequeuable buffer.
98 mVectorRenderer.StopRender();
101 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this );
106 void VectorRasterizeThread::Run()
108 SetThreadName( "VectorImageThread" );
109 mLogFactory.InstallLogFunction();
111 while( !mDestroyThread )
116 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread [%p]\n", this );
119 void VectorRasterizeThread::SetRenderer( Renderer renderer )
121 ConditionalWait::ScopedLock lock( mConditionalWait );
123 mVectorRenderer.SetRenderer( renderer );
125 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer [%p]\n", this );
128 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
130 if( mWidth != width || mHeight != height )
132 ConditionalWait::ScopedLock lock( mConditionalWait );
133 mVectorRenderer.SetSize( width, height );
138 mResourceReady = false;
140 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d [%p]\n", width, height, this );
144 void VectorRasterizeThread::PlayAnimation()
146 ConditionalWait::ScopedLock lock( mConditionalWait );
148 if( mPlayState != PlayState::PLAYING )
151 mUpdateFrameNumber = false;
152 mPlayState = PlayState::PLAYING;
153 mConditionalWait.Notify( lock );
155 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play [%p]\n", this );
159 void VectorRasterizeThread::StopAnimation()
161 ConditionalWait::ScopedLock lock( mConditionalWait );
162 if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
165 mPlayState = PlayState::STOPPING;
166 mConditionalWait.Notify( lock );
168 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop [%p]\n", this );
172 void VectorRasterizeThread::PauseAnimation()
174 ConditionalWait::ScopedLock lock( mConditionalWait );
175 if( mPlayState == PlayState::PLAYING )
177 mPlayState = PlayState::PAUSED;
179 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause [%p]\n", this );
183 void VectorRasterizeThread::RenderFrame()
185 ConditionalWait::ScopedLock lock( mConditionalWait );
187 if( !mResourceReady )
190 mConditionalWait.Notify( lock );
192 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render [%p]\n", this );
196 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
198 ConditionalWait::ScopedLock lock( mConditionalWait );
199 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
202 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
204 ConditionalWait::ScopedLock lock( mConditionalWait );
205 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
208 void VectorRasterizeThread::SetLoopCount( int32_t count )
210 if( mLoopCount != count )
212 ConditionalWait::ScopedLock lock( mConditionalWait );
216 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d] [%p]\n", count, this );
220 void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
222 // Make sure the range specified is between 0 and the total frame number
223 if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
225 // If the range is not in order swap values
226 if( startFrame > endFrame )
228 uint32_t temp = startFrame;
229 startFrame = endFrame;
233 if( startFrame != mStartFrame || endFrame != mEndFrame )
235 ConditionalWait::ScopedLock lock( mConditionalWait );
237 mStartFrame = startFrame;
238 mEndFrame = endFrame;
240 // If the current frame is out of the range, change the current frame also.
241 if( mStartFrame > mCurrentFrame )
243 mCurrentFrame = mStartFrame;
245 mCurrentFrameUpdated = true;
246 mResourceReady = false;
248 else if( mEndFrame < mCurrentFrame )
250 mCurrentFrame = mEndFrame;
252 mCurrentFrameUpdated = true;
253 mResourceReady = false;
256 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
261 DevelImageVisual::PlayState::Type VectorRasterizeThread::GetPlayState() const
263 DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
267 case PlayState::PLAYING:
269 state = DevelImageVisual::PlayState::PLAYING;
272 case PlayState::PAUSED:
274 state = DevelImageVisual::PlayState::PAUSED;
277 case PlayState::STOPPING:
278 case PlayState::STOPPED:
280 state = DevelImageVisual::PlayState::STOPPED;
288 bool VectorRasterizeThread::IsResourceReady() const
290 return mResourceReady;
293 void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
295 ConditionalWait::ScopedLock lock( mConditionalWait );
297 if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
299 mCurrentFrame = frameNumber;
300 mCurrentFrameUpdated = true;
302 mResourceReady = false;
304 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::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 VectorRasterizeThread::GetCurrentFrameNumber() const
314 return mCurrentFrame;
317 uint32_t VectorRasterizeThread::GetTotalFrameNumber() const
322 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
324 mVectorRenderer.GetDefaultSize( width, height );
327 void VectorRasterizeThread::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
329 ConditionalWait::ScopedLock lock( mConditionalWait );
330 mStopBehavior = stopBehavior;
332 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
335 void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
337 ConditionalWait::ScopedLock lock( mConditionalWait );
338 mLoopingMode = loopingMode;
340 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
343 void VectorRasterizeThread::Initialize()
345 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
347 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
349 mEndFrame = mTotalFrame - 1;
351 mFrameRate = mVectorRenderer.GetFrameRate();
352 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
354 uint32_t width, height;
355 mVectorRenderer.GetDefaultSize( width, height );
357 SetSize( width, height );
359 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
362 void VectorRasterizeThread::Rasterize()
364 bool resourceReady, stopped = false;
365 uint32_t currentFrame, startFrame, endFrame;
369 ConditionalWait::ScopedLock lock( mConditionalWait );
371 if( ( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED ) && !mNeedRender && !mDestroyThread )
373 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait [%p]\n", this );
374 mConditionalWait.Wait( lock );
377 if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
379 mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
382 resourceReady = mResourceReady;
383 currentFrame = mCurrentFrame;
384 startFrame = mStartFrame;
385 endFrame = mEndFrame;
386 loopCount = mLoopCount;
389 mResourceReady = true;
390 mCurrentFrameUpdated = false;
391 mUpdateFrameNumber = true;
394 auto currentFrameStartTime = std::chrono::system_clock::now();
396 if( mPlayState == PlayState::STOPPING )
398 currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
399 ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
403 else if( mPlayState == PlayState::PLAYING )
405 bool animationFinished = false;
407 if( currentFrame >= endFrame ) // last frame
409 if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
415 if( loopCount < 0 || ++mCurrentLoop < loopCount ) // repeat forever or before the last loop
417 ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
418 mUpdateFrameNumber = false;
422 animationFinished = true; // end of animation
426 else if( currentFrame == startFrame && !mForward ) // first frame
428 if( loopCount < 0 || ++mCurrentLoop < loopCount ) // repeat forever or before the last loop
434 animationFinished = true; // end of animation
438 if( animationFinished )
440 if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
446 mPlayState = PlayState::STOPPING;
452 if( !mVectorRenderer.Render( currentFrame ) )
454 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
455 mUpdateFrameNumber = false;
460 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger [%p]\n", this );
462 mResourceReadyTrigger->Trigger();
467 mPlayState = PlayState::STOPPED;
471 // Animation is finished
472 mAnimationFinishedTrigger->Trigger();
474 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished [%p]\n", this );
477 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
479 #if defined(DEBUG_ENABLED)
480 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
482 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld] [%p]\n", currentFrame, sleepDuration.count(), this );
485 std::this_thread::sleep_until( timeToSleepUntil );
488 uint32_t VectorRasterizeThread::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 } // namespace Internal
523 } // namespace Toolkit