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 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
163 void VectorRasterizeThread::PauseAnimation()
165 ConditionalWait::ScopedLock lock( mConditionalWait );
166 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
168 mPlayState = DevelImageVisual::PlayState::PAUSED;
170 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
174 void VectorRasterizeThread::RenderFrame()
176 ConditionalWait::ScopedLock lock( mConditionalWait );
178 if( !mResourceReady )
181 mConditionalWait.Notify( lock );
183 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
187 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
189 ConditionalWait::ScopedLock lock( mConditionalWait );
190 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
193 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
195 ConditionalWait::ScopedLock lock( mConditionalWait );
196 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
199 void VectorRasterizeThread::SetLoopCount( int32_t count )
201 if( mLoopCount != count )
203 ConditionalWait::ScopedLock lock( mConditionalWait );
209 int32_t VectorRasterizeThread::GetLoopCount() const
214 void VectorRasterizeThread::SetPlayRange( Vector2 range )
216 // Make sure the range specified is between 0.0 and 1.0
217 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
219 Vector2 orderedRange( range );
220 // If the range is not in order swap values
221 if( range.x > range.y )
223 orderedRange = Vector2( range.y, range.x );
226 if( mPlayRange != orderedRange )
228 ConditionalWait::ScopedLock lock( mConditionalWait );
230 mPlayRange = orderedRange;
232 if( mTotalFrame != 0 )
234 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
235 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
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;
257 Vector2 VectorRasterizeThread::GetPlayRange() const
262 void VectorRasterizeThread::SetCurrentProgress( float progress )
264 ConditionalWait::ScopedLock lock( mConditionalWait );
266 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
268 mProgress = progress;
270 if( mTotalFrame != 0 )
272 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
273 mCurrentFrameUpdated = true;
276 mResourceReady = false;
278 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
282 float VectorRasterizeThread::GetCurrentProgress() const
284 return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
287 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
292 bool VectorRasterizeThread::IsResourceReady() const
294 return mResourceReady;
297 bool VectorRasterizeThread::StartRender()
299 //TODO: check the return value
300 mVectorRenderer.StartRender();
302 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
304 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
305 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
307 mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
309 mFrameRate = mVectorRenderer.GetFrameRate();
310 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
312 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d, %f fps]\n", mTotalFrame, mFrameRate );
317 void VectorRasterizeThread::Rasterize()
320 uint32_t currentFrame, startFrame, endFrame;
322 DevelImageVisual::PlayState playState;
325 ConditionalWait::ScopedLock lock( mConditionalWait );
327 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
329 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
331 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
333 // Reset the current frame and the current loop
334 mCurrentFrame = mStartFrame;
337 mConditionalWait.Wait( lock );
340 resourceReady = mResourceReady;
341 currentFrame = mCurrentFrame++;
342 startFrame = mStartFrame;
343 endFrame = mEndFrame;
344 loopCount = mLoopCount;
345 playState = mPlayState;
348 mResourceReady = true;
349 mCurrentFrameUpdated = false;
352 auto currentFrameStartTime = std::chrono::system_clock::now();
355 mVectorRenderer.Render( currentFrame );
357 if( playState == DevelImageVisual::PlayState::PLAYING )
359 if( currentFrame >= endFrame )
364 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
369 if( mCurrentLoop >= loopCount )
371 mPlayState = DevelImageVisual::PlayState::STOPPED;
373 // Animation is finished
374 mAnimationFinishedTrigger->Trigger();
376 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
380 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
388 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
390 mResourceReadyTrigger->Trigger();
393 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
395 #if defined(DEBUG_ENABLED)
396 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
398 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
401 std::this_thread::sleep_until( timeToSleepUntil );
404 } // namespace Internal
406 } // namespace Toolkit