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;
41 #if defined(DEBUG_ENABLED)
42 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
45 } // unnamed namespace
47 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
52 mResourceReadyTrigger( NULL ),
53 mAnimationFinishedTrigger( NULL ),
54 mPlayRange( 0.0f, 1.0f ),
55 mPlayState( DevelImageVisual::PlayState::STOPPED ),
63 mLoopCount( LOOP_FOREVER ),
66 mDestroyThread( false ),
67 mResourceReady( false ),
68 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
70 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
73 VectorRasterizeThread::~VectorRasterizeThread()
77 ConditionalWait::ScopedLock lock( mConditionalWait );
78 mDestroyThread = true;
79 mConditionalWait.Notify( lock );
81 // This should be called in the main thread to stop waiting for the dequeuable buffer.
82 mVectorRenderer.StopRender();
85 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
89 delete mResourceReadyTrigger;
90 delete mAnimationFinishedTrigger;
93 void VectorRasterizeThread::Run()
95 mLogFactory.InstallLogFunction();
97 //TODO: check the return value
100 while( IsThreadReady() )
106 void VectorRasterizeThread::SetRenderer( Renderer renderer )
108 ConditionalWait::ScopedLock lock( mConditionalWait );
110 mVectorRenderer.SetRenderer( renderer );
112 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
115 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
117 if( mWidth != width || mHeight != height )
119 ConditionalWait::ScopedLock lock( mConditionalWait );
120 mVectorRenderer.SetSize( width, height );
125 mResourceReady = false;
127 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
131 void VectorRasterizeThread::PlayAnimation()
133 ConditionalWait::ScopedLock lock( mConditionalWait );
134 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
136 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
138 // Reset the current frame and the current loop
139 mCurrentFrame = mStartFrame;
143 mPlayState = DevelImageVisual::PlayState::PLAYING;
144 mConditionalWait.Notify( lock );
146 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
150 void VectorRasterizeThread::StopAnimation()
152 ConditionalWait::ScopedLock lock( mConditionalWait );
153 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
155 mPlayState = DevelImageVisual::PlayState::STOPPED;
157 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
161 void VectorRasterizeThread::PauseAnimation()
163 ConditionalWait::ScopedLock lock( mConditionalWait );
164 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
166 mPlayState = DevelImageVisual::PlayState::PAUSED;
168 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
172 void VectorRasterizeThread::RenderFrame()
174 ConditionalWait::ScopedLock lock( mConditionalWait );
176 mConditionalWait.Notify( lock );
178 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
181 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
183 ConditionalWait::ScopedLock lock( mConditionalWait );
184 mResourceReadyTrigger = callback;
187 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
189 ConditionalWait::ScopedLock lock( mConditionalWait );
190 mAnimationFinishedTrigger = callback;
193 void VectorRasterizeThread::SetLoopCount( int32_t count )
195 ConditionalWait::ScopedLock lock( mConditionalWait );
201 mCurrentFrame = mStartFrame;
204 int32_t VectorRasterizeThread::GetLoopCount() const
209 void VectorRasterizeThread::SetPlayRange( Vector2 range )
211 ConditionalWait::ScopedLock lock( mConditionalWait );
213 // Make sure the range specified is between 0.0 and 1.0
214 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
216 Vector2 orderedRange( range );
217 // If the range is not in order swap values
218 if( range.x > range.y )
220 orderedRange = Vector2( range.y, range.x );
223 mPlayRange = orderedRange;
225 if( mTotalFrame != 0 )
227 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
228 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
233 Vector2 VectorRasterizeThread::GetPlayRange() const
238 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
243 bool VectorRasterizeThread::IsResourceReady() const
245 return mResourceReady;
248 bool VectorRasterizeThread::IsThreadReady()
250 ConditionalWait::ScopedLock lock( mConditionalWait );
252 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
254 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
256 mConditionalWait.Wait( lock );
259 // Keep the thread alive if this thread is NOT to be destroyed
260 return !mDestroyThread;
263 bool VectorRasterizeThread::StartRender()
265 //TODO: check the return value
266 mVectorRenderer.StartRender();
268 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
270 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
271 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
273 mCurrentFrame = mStartFrame;
275 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame );
280 void VectorRasterizeThread::Rasterize()
282 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
285 mVectorRenderer.Render( mCurrentFrame );
287 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
289 if( ++mCurrentFrame >= mEndFrame )
294 mCurrentFrame = mStartFrame;
299 if( mCurrentLoop >= mLoopCount )
301 // Animation is finished
302 mPlayState = DevelImageVisual::PlayState::STOPPED;
304 mAnimationFinishedTrigger->Trigger();
306 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
310 mCurrentFrame = mStartFrame;
318 if( !mResourceReady )
320 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
322 mResourceReadyTrigger->Trigger();
323 mResourceReady = true;
327 } // namespace Internal
329 } // namespace Toolkit