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() )
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 while( !mDestroyThread )
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 );
142 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
144 mPlayState = DevelImageVisual::PlayState::PLAYING;
145 mConditionalWait.Notify( lock );
147 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
151 void VectorRasterizeThread::StopAnimation()
153 ConditionalWait::ScopedLock lock( mConditionalWait );
154 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
156 mPlayState = DevelImageVisual::PlayState::STOPPED;
158 mCurrentFrame = mStartFrame;
159 mCurrentFrameUpdated = true;
161 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
165 void VectorRasterizeThread::PauseAnimation()
167 ConditionalWait::ScopedLock lock( mConditionalWait );
168 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
170 mPlayState = DevelImageVisual::PlayState::PAUSED;
172 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
176 void VectorRasterizeThread::RenderFrame()
178 ConditionalWait::ScopedLock lock( mConditionalWait );
180 if( !mResourceReady )
183 mConditionalWait.Notify( lock );
185 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
189 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
191 ConditionalWait::ScopedLock lock( mConditionalWait );
192 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
195 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
197 ConditionalWait::ScopedLock lock( mConditionalWait );
198 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
201 void VectorRasterizeThread::SetLoopCount( int32_t count )
203 if( mLoopCount != count )
205 ConditionalWait::ScopedLock lock( mConditionalWait );
211 int32_t VectorRasterizeThread::GetLoopCount() const
216 void VectorRasterizeThread::SetPlayRange( Vector2 range )
218 // Make sure the range specified is between 0.0 and 1.0
219 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
221 Vector2 orderedRange( range );
222 // If the range is not in order swap values
223 if( range.x > range.y )
225 orderedRange = Vector2( range.y, range.x );
228 if( mPlayRange != orderedRange )
230 ConditionalWait::ScopedLock lock( mConditionalWait );
232 mPlayRange = orderedRange;
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;
256 Vector2 VectorRasterizeThread::GetPlayRange() const
261 void VectorRasterizeThread::SetCurrentProgress( float progress )
263 ConditionalWait::ScopedLock lock( mConditionalWait );
265 if( progress >= mPlayRange.x && progress <= mPlayRange.y )
267 mProgress = progress;
269 mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
270 mCurrentFrameUpdated = true;
272 mResourceReady = false;
274 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
278 float VectorRasterizeThread::GetCurrentProgress() const
280 return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
283 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
285 mVectorRenderer.GetDefaultSize( width, height );
288 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
293 bool VectorRasterizeThread::IsResourceReady() const
295 return mResourceReady;
298 void VectorRasterizeThread::Initialize()
300 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
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::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
315 void VectorRasterizeThread::Rasterize()
318 uint32_t currentFrame, startFrame, endFrame;
320 DevelImageVisual::PlayState playState;
323 ConditionalWait::ScopedLock lock( mConditionalWait );
325 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
327 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
329 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
331 // Reset the current loop
334 mConditionalWait.Wait( lock );
337 resourceReady = mResourceReady;
338 currentFrame = mCurrentFrame++;
339 startFrame = mStartFrame;
340 endFrame = mEndFrame;
341 loopCount = mLoopCount;
342 playState = mPlayState;
345 mResourceReady = true;
346 mCurrentFrameUpdated = false;
349 auto currentFrameStartTime = std::chrono::system_clock::now();
352 mVectorRenderer.Render( currentFrame );
354 if( playState == DevelImageVisual::PlayState::PLAYING )
356 if( currentFrame >= endFrame )
361 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait ); // If the current frame is changed in the event thread, don't overwrite it.
366 if( mCurrentLoop >= loopCount )
368 mPlayState = DevelImageVisual::PlayState::STOPPED;
370 // Animation is finished
371 mAnimationFinishedTrigger->Trigger();
373 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
377 ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
385 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
387 mResourceReadyTrigger->Trigger();
390 auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
392 #if defined(DEBUG_ENABLED)
393 auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
395 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
398 std::this_thread::sleep_until( timeToSleepUntil );
401 } // namespace Internal
403 } // namespace Toolkit