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 )
112 void VectorRasterizeThread::SetRenderer( Renderer renderer )
114 ConditionalWait::ScopedLock lock( mConditionalWait );
116 mVectorRenderer.SetRenderer( renderer );
118 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
121 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
123 if( mWidth != width || mHeight != height )
125 ConditionalWait::ScopedLock lock( mConditionalWait );
126 mVectorRenderer.SetSize( width, height );
131 mResourceReady = false;
133 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
137 void VectorRasterizeThread::PlayAnimation()
139 ConditionalWait::ScopedLock lock( mConditionalWait );
140 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
142 mPlayState = DevelImageVisual::PlayState::PLAYING;
143 mConditionalWait.Notify( lock );
145 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
149 void VectorRasterizeThread::StopAnimation()
151 ConditionalWait::ScopedLock lock( mConditionalWait );
152 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
154 mPlayState = DevelImageVisual::PlayState::STOPPED;
156 mCurrentFrame = mStartFrame;
157 mCurrentFrameUpdated = true;
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 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 mCurrentFrameUpdated = true;
241 mResourceReady = false;
243 else if( mEndFrame < mCurrentFrame )
245 mCurrentFrame = mEndFrame;
247 mCurrentFrameUpdated = true;
248 mResourceReady = false;
254 Vector2 VectorRasterizeThread::GetPlayRange() const
259 void VectorRasterizeThread::SetCurrentProgress( float progress )
261 ConditionalWait::ScopedLock lock( mConditionalWait );
263 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
265 mProgress = progress;
267 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
268 mCurrentFrameUpdated = true;
270 mResourceReady = false;
272 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
276 float VectorRasterizeThread::GetCurrentProgress() const
278 return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
281 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
283 mVectorRenderer.GetDefaultSize( width, height );
286 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
291 bool VectorRasterizeThread::IsResourceReady() const
293 return mResourceReady;
296 void VectorRasterizeThread::Initialize()
298 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
300 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
302 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
303 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
305 mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
307 mFrameRate = mVectorRenderer.GetFrameRate();
308 mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
310 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
313 void VectorRasterizeThread::Rasterize()
316 uint32_t currentFrame, startFrame, endFrame;
318 DevelImageVisual::PlayState playState;
321 ConditionalWait::ScopedLock lock( mConditionalWait );
323 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
325 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
327 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
329 // Reset the current loop
332 mConditionalWait.Wait( lock );
335 resourceReady = mResourceReady;
336 currentFrame = mCurrentFrame++;
337 startFrame = mStartFrame;
338 endFrame = mEndFrame;
339 loopCount = mLoopCount;
340 playState = mPlayState;
343 mResourceReady = true;
344 mCurrentFrameUpdated = false;
347 auto currentFrameStartTime = std::chrono::system_clock::now();
350 mVectorRenderer.Render( currentFrame );
352 if( playState == DevelImageVisual::PlayState::PLAYING )
354 if( currentFrame >= endFrame )
359 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
364 if( mCurrentLoop >= loopCount )
366 mPlayState = DevelImageVisual::PlayState::STOPPED;
368 // Animation is finished
369 mAnimationFinishedTrigger->Trigger();
371 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
375 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
383 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
385 mResourceReadyTrigger->Trigger();
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