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() )
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 while( !mDestroyThread )
111 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread\n" );
114 void VectorRasterizeThread::SetRenderer( Renderer renderer )
116 ConditionalWait::ScopedLock lock( mConditionalWait );
118 mVectorRenderer.SetRenderer( renderer );
120 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
123 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
125 if( mWidth != width || mHeight != height )
127 ConditionalWait::ScopedLock lock( mConditionalWait );
128 mVectorRenderer.SetSize( width, height );
133 mResourceReady = false;
135 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
139 void VectorRasterizeThread::PlayAnimation()
141 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 );
210 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d]\n", count );
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 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;
256 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d]\n", mStartFrame, mEndFrame );
261 Vector2 VectorRasterizeThread::GetPlayRange() const
266 void VectorRasterizeThread::SetCurrentProgress( float progress )
268 ConditionalWait::ScopedLock lock( mConditionalWait );
270 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
272 mProgress = progress;
274 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
275 mCurrentFrameUpdated = true;
277 mResourceReady = false;
279 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
283 float VectorRasterizeThread::GetCurrentProgress() const
285 return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
288 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
290 mVectorRenderer.GetDefaultSize( width, height );
293 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
298 bool VectorRasterizeThread::IsResourceReady() const
300 return mResourceReady;
303 void VectorRasterizeThread::Initialize()
305 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
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 uint32_t width, height;
318 mVectorRenderer.GetDefaultSize( width, height );
320 SetSize( width, height );
322 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
325 void VectorRasterizeThread::Rasterize()
328 uint32_t currentFrame, startFrame, endFrame;
330 DevelImageVisual::PlayState playState;
333 ConditionalWait::ScopedLock lock( mConditionalWait );
335 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
337 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
339 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
341 // Reset the current loop
344 mConditionalWait.Wait( lock );
347 resourceReady = mResourceReady;
348 currentFrame = mCurrentFrame++;
349 startFrame = mStartFrame;
350 endFrame = mEndFrame;
351 loopCount = mLoopCount;
352 playState = mPlayState;
355 mResourceReady = true;
356 mCurrentFrameUpdated = false;
359 auto currentFrameStartTime = std::chrono::system_clock::now();
362 mVectorRenderer.Render( currentFrame );
364 if( playState == DevelImageVisual::PlayState::PLAYING )
366 if( currentFrame >= endFrame )
371 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
376 if( mCurrentLoop >= loopCount )
378 mPlayState = DevelImageVisual::PlayState::STOPPED;
380 // Animation is finished
381 mAnimationFinishedTrigger->Trigger();
383 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
387 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
395 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
397 mResourceReadyTrigger->Trigger();
400 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
402 #if defined(DEBUG_ENABLED)
403 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
405 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", currentFrame, sleepDuration.count() );
408 std::this_thread::sleep_until( timeToSleepUntil );
411 } // namespace Internal
413 } // namespace Toolkit