2 * Copyright (c) 2018 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 ResetToStart( bool& updated, uint32_t& value, uint32_t startValue, ConditionalWait& conditionalWait )
49 ConditionalWait::ScopedLock lock( conditionalWait );
56 } // unnamed namespace
58 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
62 mResourceReadyTrigger(),
63 mAnimationFinishedTrigger(),
64 mPlayRange( 0.0f, 1.0f ),
65 mPlayState( DevelImageVisual::PlayState::STOPPED ),
66 mFrameDurationNanoSeconds( 0 ),
75 mLoopCount( LOOP_FOREVER ),
78 mDestroyThread( false ),
79 mResourceReady( false ),
80 mCurrentFrameUpdated( false ),
81 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
86 VectorRasterizeThread::~VectorRasterizeThread()
90 ConditionalWait::ScopedLock lock( mConditionalWait );
91 mDestroyThread = true;
92 mConditionalWait.Notify( lock );
94 // This should be called in the main thread to stop waiting for the dequeuable buffer.
95 mVectorRenderer.StopRender();
98 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
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\n" );
116 void VectorRasterizeThread::SetRenderer( Renderer renderer )
118 ConditionalWait::ScopedLock lock( mConditionalWait );
120 mVectorRenderer.SetRenderer( renderer );
122 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
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\n", width, height );
141 void VectorRasterizeThread::PlayAnimation()
143 ConditionalWait::ScopedLock lock( mConditionalWait );
145 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
147 mPlayState = DevelImageVisual::PlayState::PLAYING;
148 mConditionalWait.Notify( lock );
150 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
154 void VectorRasterizeThread::StopAnimation()
156 ConditionalWait::ScopedLock lock( mConditionalWait );
157 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
159 mPlayState = DevelImageVisual::PlayState::STOPPED;
161 mCurrentFrame = mStartFrame;
162 mCurrentFrameUpdated = true;
164 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
168 void VectorRasterizeThread::PauseAnimation()
170 ConditionalWait::ScopedLock lock( mConditionalWait );
171 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
173 mPlayState = DevelImageVisual::PlayState::PAUSED;
175 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
179 void VectorRasterizeThread::RenderFrame()
181 ConditionalWait::ScopedLock lock( mConditionalWait );
183 if( !mResourceReady )
186 mConditionalWait.Notify( lock );
188 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
192 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
194 ConditionalWait::ScopedLock lock( mConditionalWait );
195 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
198 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
200 ConditionalWait::ScopedLock lock( mConditionalWait );
201 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
204 void VectorRasterizeThread::SetLoopCount( int32_t count )
206 if( mLoopCount != count )
208 ConditionalWait::ScopedLock lock( mConditionalWait );
212 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d]\n", count );
216 int32_t VectorRasterizeThread::GetLoopCount() const
221 void VectorRasterizeThread::SetPlayRange( Vector2 range )
223 // Make sure the range specified is between 0.0 and 1.0
224 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
226 Vector2 orderedRange( range );
227 // If the range is not in order swap values
228 if( range.x > range.y )
230 orderedRange = Vector2( range.y, range.x );
233 if( mPlayRange != orderedRange )
235 ConditionalWait::ScopedLock lock( mConditionalWait );
237 mPlayRange = orderedRange;
239 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
240 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
242 // If the current frame is out of the range, change the current frame also.
243 if( mStartFrame > mCurrentFrame )
245 mCurrentFrame = mStartFrame;
247 mCurrentFrameUpdated = true;
248 mResourceReady = false;
250 else if( mEndFrame < mCurrentFrame )
252 mCurrentFrame = mEndFrame;
254 mCurrentFrameUpdated = true;
255 mResourceReady = false;
258 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d]\n", mStartFrame, mEndFrame );
263 Vector2 VectorRasterizeThread::GetPlayRange() const
268 void VectorRasterizeThread::SetCurrentProgress( float progress )
270 ConditionalWait::ScopedLock lock( mConditionalWait );
272 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
274 mProgress = progress;
276 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
277 mCurrentFrameUpdated = true;
279 mResourceReady = false;
281 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
285 float VectorRasterizeThread::GetCurrentProgress() const
287 return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
290 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
292 mVectorRenderer.GetDefaultSize( width, height );
295 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
300 bool VectorRasterizeThread::IsResourceReady() const
302 return mResourceReady;
305 void VectorRasterizeThread::Initialize()
307 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
309 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
311 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
312 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
314 mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
316 mFrameRate = mVectorRenderer.GetFrameRate();
317 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
319 uint32_t width, height;
320 mVectorRenderer.GetDefaultSize( width, height );
322 SetSize( width, height );
324 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
327 void VectorRasterizeThread::Rasterize()
330 uint32_t currentFrame, startFrame, endFrame;
332 DevelImageVisual::PlayState playState;
335 ConditionalWait::ScopedLock lock( mConditionalWait );
337 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
339 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
341 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
343 // Reset the current loop
346 mConditionalWait.Wait( lock );
349 resourceReady = mResourceReady;
350 currentFrame = mCurrentFrame++;
351 startFrame = mStartFrame;
352 endFrame = mEndFrame;
353 loopCount = mLoopCount;
354 playState = mPlayState;
357 mResourceReady = true;
358 mCurrentFrameUpdated = false;
361 auto currentFrameStartTime = std::chrono::system_clock::now();
364 mVectorRenderer.Render( currentFrame );
366 if( playState == DevelImageVisual::PlayState::PLAYING )
368 if( currentFrame >= endFrame )
373 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
378 if( mCurrentLoop >= loopCount )
380 mPlayState = DevelImageVisual::PlayState::STOPPED;
382 // Animation is finished
383 mAnimationFinishedTrigger->Trigger();
385 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
389 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
397 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
399 mResourceReadyTrigger->Trigger();
402 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
404 #if defined(DEBUG_ENABLED)
405 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
407 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", currentFrame, sleepDuration.count() );
410 std::this_thread::sleep_until( timeToSleepUntil );
413 } // namespace Internal
415 } // namespace Toolkit