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/integration-api/adaptors/adaptor.h>
23 #include <dali/integration-api/debug.h>
39 constexpr auto LOOP_FOREVER = -1;
40 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
46 } // unnamed namespace
48 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
53 mResourceReadyTrigger(),
54 mAnimationFinishedTrigger(),
55 mPlayRange( 0.0f, 1.0f ),
56 mPlayState( DevelImageVisual::PlayState::STOPPED ),
57 mFrameDurationNanoSeconds( 0 ),
66 mLoopCount( LOOP_FOREVER ),
69 mDestroyThread( false ),
70 mResourceReady( false ),
71 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
73 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
76 VectorRasterizeThread::~VectorRasterizeThread()
80 ConditionalWait::ScopedLock lock( mConditionalWait );
81 mDestroyThread = true;
82 mConditionalWait.Notify( lock );
84 // This should be called in the main thread to stop waiting for the dequeuable buffer.
85 mVectorRenderer.StopRender();
88 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
93 void VectorRasterizeThread::Run()
95 mLogFactory.InstallLogFunction();
97 //TODO: check the return value
100 while( IsThreadReady() )
106 void VectorRasterizeThread::SetRenderer( Renderer renderer )
108 ConditionalWait::ScopedLock lock( mConditionalWait );
110 mVectorRenderer.SetRenderer( renderer );
112 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
115 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
117 if( mWidth != width || mHeight != height )
119 ConditionalWait::ScopedLock lock( mConditionalWait );
120 mVectorRenderer.SetSize( width, height );
125 mResourceReady = false;
127 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
131 void VectorRasterizeThread::PlayAnimation()
133 ConditionalWait::ScopedLock lock( mConditionalWait );
134 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
136 mPlayState = DevelImageVisual::PlayState::PLAYING;
137 mConditionalWait.Notify( lock );
139 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
143 void VectorRasterizeThread::StopAnimation()
145 ConditionalWait::ScopedLock lock( mConditionalWait );
146 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
148 mPlayState = DevelImageVisual::PlayState::STOPPED;
150 // Reset the current frame and the current loop
151 mCurrentFrame = mStartFrame;
154 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
158 void VectorRasterizeThread::PauseAnimation()
160 ConditionalWait::ScopedLock lock( mConditionalWait );
161 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
163 mPlayState = DevelImageVisual::PlayState::PAUSED;
165 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
169 void VectorRasterizeThread::RenderFrame()
171 ConditionalWait::ScopedLock lock( mConditionalWait );
173 if( !mResourceReady )
176 mConditionalWait.Notify( lock );
178 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
182 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
184 ConditionalWait::ScopedLock lock( mConditionalWait );
185 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
188 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
190 ConditionalWait::ScopedLock lock( mConditionalWait );
191 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
194 void VectorRasterizeThread::SetLoopCount( int32_t count )
196 if( mLoopCount != count )
198 ConditionalWait::ScopedLock lock( mConditionalWait );
207 int32_t VectorRasterizeThread::GetLoopCount() const
212 void VectorRasterizeThread::SetPlayRange( Vector2 range )
214 // Make sure the range specified is between 0.0 and 1.0
215 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
217 Vector2 orderedRange( range );
218 // If the range is not in order swap values
219 if( range.x > range.y )
221 orderedRange = Vector2( range.y, range.x );
224 if( mPlayRange != orderedRange )
226 ConditionalWait::ScopedLock lock( mConditionalWait );
228 mPlayRange = orderedRange;
230 if( mTotalFrame != 0 )
232 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
233 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
235 // If the current frame is out of the range, change the current frame also.
236 if( mStartFrame > mCurrentFrame )
238 mCurrentFrame = mStartFrame;
240 mResourceReady = false;
242 else if( mEndFrame < mCurrentFrame )
244 mCurrentFrame = mEndFrame;
246 mResourceReady = false;
253 Vector2 VectorRasterizeThread::GetPlayRange() const
258 void VectorRasterizeThread::SetCurrentProgress( float progress )
260 ConditionalWait::ScopedLock lock( mConditionalWait );
262 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
264 mProgress = progress;
266 if( mTotalFrame != 0 )
268 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
271 mResourceReady = false;
273 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
277 float VectorRasterizeThread::GetCurrentProgress() const
279 return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
282 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
287 bool VectorRasterizeThread::IsResourceReady() const
289 return mResourceReady;
292 bool VectorRasterizeThread::IsThreadReady()
294 ConditionalWait::ScopedLock lock( mConditionalWait );
296 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
298 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
300 mConditionalWait.Wait( lock );
303 // Keep the thread alive if this thread is NOT to be destroyed
304 return !mDestroyThread;
307 bool VectorRasterizeThread::StartRender()
309 //TODO: check the return value
310 mVectorRenderer.StartRender();
312 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
314 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
315 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
317 mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
319 mFrameRate = mVectorRenderer.GetFrameRate();
320 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
322 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d, %f fps]\n", mTotalFrame, mFrameRate );
327 void VectorRasterizeThread::Rasterize()
329 bool needRender, resourceReady;
332 ConditionalWait::ScopedLock lock( mConditionalWait );
333 needRender = mNeedRender;
334 resourceReady = mResourceReady;
337 auto currentFrameStartTime = std::chrono::system_clock::now();
340 mVectorRenderer.Render( mCurrentFrame );
342 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
344 if( ++mCurrentFrame >= mEndFrame )
349 mCurrentFrame = mStartFrame;
354 if( mCurrentLoop >= mLoopCount )
356 // Animation is finished
357 mPlayState = DevelImageVisual::PlayState::STOPPED;
359 // Reset the current frame and the current loop
360 mCurrentFrame = mStartFrame;
363 mAnimationFinishedTrigger->Trigger();
365 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
369 mCurrentFrame = mStartFrame;
382 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
384 mResourceReadyTrigger->Trigger();
385 mResourceReady = true;
388 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
390 #if defined(DEBUG_ENABLED)
391 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
393 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
396 std::this_thread::sleep_until( timeToSleepUntil );
399 } // namespace Internal
401 } // namespace Toolkit