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() )
83 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
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 //TODO: check the return value
111 while( !mDestroyThread )
117 void VectorRasterizeThread::SetRenderer( Renderer renderer )
119 ConditionalWait::ScopedLock lock( mConditionalWait );
121 mVectorRenderer.SetRenderer( renderer );
123 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
126 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
128 if( mWidth != width || mHeight != height )
130 ConditionalWait::ScopedLock lock( mConditionalWait );
131 mVectorRenderer.SetSize( width, height );
136 mResourceReady = false;
138 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
142 void VectorRasterizeThread::PlayAnimation()
144 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 );
214 int32_t VectorRasterizeThread::GetLoopCount() const
219 void VectorRasterizeThread::SetPlayRange( Vector2 range )
221 // Make sure the range specified is between 0.0 and 1.0
222 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
224 Vector2 orderedRange( range );
225 // If the range is not in order swap values
226 if( range.x > range.y )
228 orderedRange = Vector2( range.y, range.x );
231 if( mPlayRange != orderedRange )
233 ConditionalWait::ScopedLock lock( mConditionalWait );
235 mPlayRange = orderedRange;
237 if( mTotalFrame != 0 )
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;
262 Vector2 VectorRasterizeThread::GetPlayRange() const
267 void VectorRasterizeThread::SetCurrentProgress( float progress )
269 ConditionalWait::ScopedLock lock( mConditionalWait );
271 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
273 mProgress = progress;
275 if( mTotalFrame != 0 )
277 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
278 mCurrentFrameUpdated = true;
281 mResourceReady = false;
283 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
287 float VectorRasterizeThread::GetCurrentProgress() const
289 return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
292 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
297 bool VectorRasterizeThread::IsResourceReady() const
299 return mResourceReady;
302 bool VectorRasterizeThread::StartRender()
304 //TODO: check the return value
305 mVectorRenderer.StartRender();
307 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
309 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
310 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
312 mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
314 mFrameRate = mVectorRenderer.GetFrameRate();
315 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
317 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d, %f fps]\n", mTotalFrame, mFrameRate );
322 void VectorRasterizeThread::Rasterize()
325 uint32_t currentFrame, startFrame, endFrame;
327 DevelImageVisual::PlayState playState;
330 ConditionalWait::ScopedLock lock( mConditionalWait );
332 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
334 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
336 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
338 // Reset the current loop
341 mConditionalWait.Wait( lock );
344 resourceReady = mResourceReady;
345 currentFrame = mCurrentFrame++;
346 startFrame = mStartFrame;
347 endFrame = mEndFrame;
348 loopCount = mLoopCount;
349 playState = mPlayState;
352 mResourceReady = true;
353 mCurrentFrameUpdated = false;
356 auto currentFrameStartTime = std::chrono::system_clock::now();
359 mVectorRenderer.Render( currentFrame );
361 if( playState == DevelImageVisual::PlayState::PLAYING )
363 if( currentFrame >= endFrame )
368 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
373 if( mCurrentLoop >= loopCount )
375 mPlayState = DevelImageVisual::PlayState::STOPPED;
377 // Animation is finished
378 mAnimationFinishedTrigger->Trigger();
380 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
384 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
392 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
394 mResourceReadyTrigger->Trigger();
397 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
399 #if defined(DEBUG_ENABLED)
400 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
402 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
405 std::this_thread::sleep_until( timeToSleepUntil );
408 } // namespace Internal
410 } // namespace Toolkit