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;
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
46 } // unnamed namespace
48 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
53 mResourceReadyTrigger(),
54 mAnimationFinishedTrigger(),
55 mPlayRange( 0.0f, 1.0f ),
56 mPlayState( DevelImageVisual::PlayState::STOPPED ),
64 mLoopCount( LOOP_FOREVER ),
67 mDestroyThread( false ),
68 mResourceReady( false ),
69 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
71 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
74 VectorRasterizeThread::~VectorRasterizeThread()
78 ConditionalWait::ScopedLock lock( mConditionalWait );
79 mDestroyThread = true;
80 mConditionalWait.Notify( lock );
82 // This should be called in the main thread to stop waiting for the dequeuable buffer.
83 mVectorRenderer.StopRender();
86 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
91 void VectorRasterizeThread::Run()
93 SetThreadName( "VectorImageThread" );
94 mLogFactory.InstallLogFunction();
96 //TODO: check the return value
99 while( IsThreadReady() )
105 void VectorRasterizeThread::SetRenderer( Renderer renderer )
107 ConditionalWait::ScopedLock lock( mConditionalWait );
109 mVectorRenderer.SetRenderer( renderer );
111 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
114 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
116 if( mWidth != width || mHeight != height )
118 ConditionalWait::ScopedLock lock( mConditionalWait );
119 mVectorRenderer.SetSize( width, height );
124 mResourceReady = false;
126 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
130 void VectorRasterizeThread::PlayAnimation()
132 ConditionalWait::ScopedLock lock( mConditionalWait );
133 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
135 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
137 // Reset the current frame and the current loop
138 mCurrentFrame = mStartFrame;
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 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
160 void VectorRasterizeThread::PauseAnimation()
162 ConditionalWait::ScopedLock lock( mConditionalWait );
163 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
165 mPlayState = DevelImageVisual::PlayState::PAUSED;
167 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
171 void VectorRasterizeThread::RenderFrame()
173 ConditionalWait::ScopedLock lock( mConditionalWait );
175 mConditionalWait.Notify( lock );
177 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
180 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
182 ConditionalWait::ScopedLock lock( mConditionalWait );
183 mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
186 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
188 ConditionalWait::ScopedLock lock( mConditionalWait );
189 mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
192 void VectorRasterizeThread::SetLoopCount( int32_t count )
194 ConditionalWait::ScopedLock lock( mConditionalWait );
200 mCurrentFrame = mStartFrame;
203 int32_t VectorRasterizeThread::GetLoopCount() const
208 void VectorRasterizeThread::SetPlayRange( Vector2 range )
210 ConditionalWait::ScopedLock lock( mConditionalWait );
212 // Make sure the range specified is between 0.0 and 1.0
213 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
215 Vector2 orderedRange( range );
216 // If the range is not in order swap values
217 if( range.x > range.y )
219 orderedRange = Vector2( range.y, range.x );
222 mPlayRange = orderedRange;
224 if( mTotalFrame != 0 )
226 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
227 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
232 Vector2 VectorRasterizeThread::GetPlayRange() const
237 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
242 bool VectorRasterizeThread::IsResourceReady() const
244 return mResourceReady;
247 bool VectorRasterizeThread::IsThreadReady()
249 ConditionalWait::ScopedLock lock( mConditionalWait );
251 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
253 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
255 mConditionalWait.Wait( lock );
258 // Keep the thread alive if this thread is NOT to be destroyed
259 return !mDestroyThread;
262 bool VectorRasterizeThread::StartRender()
264 //TODO: check the return value
265 mVectorRenderer.StartRender();
267 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
269 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
270 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
272 mCurrentFrame = mStartFrame;
274 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame );
279 void VectorRasterizeThread::Rasterize()
281 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
284 mVectorRenderer.Render( mCurrentFrame );
286 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
288 if( ++mCurrentFrame >= mEndFrame )
293 mCurrentFrame = mStartFrame;
298 if( mCurrentLoop >= mLoopCount )
300 // Animation is finished
301 mPlayState = DevelImageVisual::PlayState::STOPPED;
303 mAnimationFinishedTrigger->Trigger();
305 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
309 mCurrentFrame = mStartFrame;
317 if( !mResourceReady )
319 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
321 mResourceReadyTrigger->Trigger();
322 mResourceReady = true;
326 } // namespace Internal
328 } // namespace Toolkit