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 inline void ResetToStart( bool& updated, uint32_t& value, uint32_t startValue, ConditionalWait& conditionalWait )
48 ConditionalWait::ScopedLock lock( conditionalWait );
55 } // unnamed namespace
57 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
61 mResourceReadyTrigger(),
62 mAnimationFinishedTrigger(),
63 mPlayRange( 0.0f, 1.0f ),
64 mPlayState( DevelImageVisual::PlayState::STOPPED ),
65 mFrameDurationNanoSeconds( 0 ),
74 mLoopCount( LOOP_FOREVER ),
77 mDestroyThread( false ),
78 mResourceReady( false ),
79 mCurrentFrameUpdated( false ),
80 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
82 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
85 VectorRasterizeThread::~VectorRasterizeThread()
89 ConditionalWait::ScopedLock lock( mConditionalWait );
90 mDestroyThread = true;
91 mConditionalWait.Notify( lock );
93 // This should be called in the main thread to stop waiting for the dequeuable buffer.
94 mVectorRenderer.StopRender();
97 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
102 void VectorRasterizeThread::Run()
104 mLogFactory.InstallLogFunction();
106 //TODO: check the return value
109 while( !mDestroyThread )
115 void VectorRasterizeThread::SetRenderer( Renderer renderer )
117 ConditionalWait::ScopedLock lock( mConditionalWait );
119 mVectorRenderer.SetRenderer( renderer );
121 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
124 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
126 if( mWidth != width || mHeight != height )
128 ConditionalWait::ScopedLock lock( mConditionalWait );
129 mVectorRenderer.SetSize( width, height );
134 mResourceReady = false;
136 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
140 void VectorRasterizeThread::PlayAnimation()
142 ConditionalWait::ScopedLock lock( mConditionalWait );
143 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
145 mPlayState = DevelImageVisual::PlayState::PLAYING;
146 mConditionalWait.Notify( lock );
148 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
152 void VectorRasterizeThread::StopAnimation()
154 ConditionalWait::ScopedLock lock( mConditionalWait );
155 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
157 mPlayState = DevelImageVisual::PlayState::STOPPED;
159 mCurrentFrame = mStartFrame;
160 mCurrentFrameUpdated = true;
162 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
166 void VectorRasterizeThread::PauseAnimation()
168 ConditionalWait::ScopedLock lock( mConditionalWait );
169 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
171 mPlayState = DevelImageVisual::PlayState::PAUSED;
173 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
177 void VectorRasterizeThread::RenderFrame()
179 ConditionalWait::ScopedLock lock( mConditionalWait );
181 if( !mResourceReady )
184 mConditionalWait.Notify( lock );
186 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
190 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
192 ConditionalWait::ScopedLock lock( mConditionalWait );
193 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
196 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
198 ConditionalWait::ScopedLock lock( mConditionalWait );
199 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
202 void VectorRasterizeThread::SetLoopCount( int32_t count )
204 if( mLoopCount != count )
206 ConditionalWait::ScopedLock lock( mConditionalWait );
212 int32_t VectorRasterizeThread::GetLoopCount() const
217 void VectorRasterizeThread::SetPlayRange( Vector2 range )
219 // Make sure the range specified is between 0.0 and 1.0
220 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
222 Vector2 orderedRange( range );
223 // If the range is not in order swap values
224 if( range.x > range.y )
226 orderedRange = Vector2( range.y, range.x );
229 if( mPlayRange != orderedRange )
231 ConditionalWait::ScopedLock lock( mConditionalWait );
233 mPlayRange = orderedRange;
235 if( mTotalFrame != 0 )
237 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
238 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
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;
260 Vector2 VectorRasterizeThread::GetPlayRange() const
265 void VectorRasterizeThread::SetCurrentProgress( float progress )
267 ConditionalWait::ScopedLock lock( mConditionalWait );
269 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
271 mProgress = progress;
273 if( mTotalFrame != 0 )
275 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
276 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 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
295 bool VectorRasterizeThread::IsResourceReady() const
297 return mResourceReady;
300 bool VectorRasterizeThread::StartRender()
302 //TODO: check the return value
303 mVectorRenderer.StartRender();
305 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
307 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
308 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
310 mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
312 mFrameRate = mVectorRenderer.GetFrameRate();
313 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
315 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d, %f fps]\n", mTotalFrame, mFrameRate );
320 void VectorRasterizeThread::Rasterize()
323 uint32_t currentFrame, startFrame, endFrame;
325 DevelImageVisual::PlayState playState;
328 ConditionalWait::ScopedLock lock( mConditionalWait );
330 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
332 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
334 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
336 // Reset the current loop
339 mConditionalWait.Wait( lock );
342 resourceReady = mResourceReady;
343 currentFrame = mCurrentFrame++;
344 startFrame = mStartFrame;
345 endFrame = mEndFrame;
346 loopCount = mLoopCount;
347 playState = mPlayState;
350 mResourceReady = true;
351 mCurrentFrameUpdated = false;
354 auto currentFrameStartTime = std::chrono::system_clock::now();
357 mVectorRenderer.Render( currentFrame );
359 if( playState == DevelImageVisual::PlayState::PLAYING )
361 if( currentFrame >= endFrame )
366 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
371 if( mCurrentLoop >= loopCount )
373 mPlayState = DevelImageVisual::PlayState::STOPPED;
375 // Animation is finished
376 mAnimationFinishedTrigger->Trigger();
378 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
382 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
390 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
392 mResourceReadyTrigger->Trigger();
395 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
397 #if defined(DEBUG_ENABLED)
398 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
400 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
403 std::this_thread::sleep_until( timeToSleepUntil );
406 } // namespace Internal
408 } // namespace Toolkit