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 ),
62 mLoopCount( LOOP_FOREVER ),
65 mDestroyThread( false ),
66 mResourceReady( false ),
67 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
69 mVectorRenderer = VectorAnimationRenderer::New( mUrl );
72 VectorRasterizeThread::~VectorRasterizeThread()
76 ConditionalWait::ScopedLock lock( mConditionalWait );
77 mDestroyThread = true;
78 mConditionalWait.Notify( lock );
80 // This should be called in the main thread to stop waiting for the dequeuable buffer.
81 mVectorRenderer.StopRender();
84 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
88 delete mResourceReadyTrigger;
89 delete mAnimationFinishedTrigger;
92 void VectorRasterizeThread::Run()
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 // Need to trigger resource ready again
112 mResourceReady = false;
114 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
117 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
119 ConditionalWait::ScopedLock lock( mConditionalWait );
120 mVectorRenderer.SetSize( width, height );
122 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
125 void VectorRasterizeThread::StartAnimation()
127 ConditionalWait::ScopedLock lock( mConditionalWait );
128 if( mPlayState != DevelImageVisual::PlayState::PLAYING )
130 if( mPlayState == DevelImageVisual::PlayState::STOPPED )
132 // Reset the current frame and the current loop
133 mCurrentFrame = mStartFrame;
137 mPlayState = DevelImageVisual::PlayState::PLAYING;
138 mConditionalWait.Notify( lock );
140 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartAnimation: Start\n" );
144 void VectorRasterizeThread::StopAnimation()
146 ConditionalWait::ScopedLock lock( mConditionalWait );
147 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
149 mPlayState = DevelImageVisual::PlayState::STOPPED;
151 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
155 void VectorRasterizeThread::PauseAnimation()
157 ConditionalWait::ScopedLock lock( mConditionalWait );
158 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
160 mPlayState = DevelImageVisual::PlayState::PAUSED;
162 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
166 void VectorRasterizeThread::RenderFrame()
168 ConditionalWait::ScopedLock lock( mConditionalWait );
170 mConditionalWait.Notify( lock );
172 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
175 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
177 ConditionalWait::ScopedLock lock( mConditionalWait );
178 mResourceReadyTrigger = callback;
181 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
183 ConditionalWait::ScopedLock lock( mConditionalWait );
184 mAnimationFinishedTrigger = callback;
187 void VectorRasterizeThread::SetLoopCount( int16_t count )
189 ConditionalWait::ScopedLock lock( mConditionalWait );
195 mCurrentFrame = mStartFrame;
198 void VectorRasterizeThread::SetPlayRange( Vector2 range )
200 ConditionalWait::ScopedLock lock( mConditionalWait );
204 if( mTotalFrame != 0 )
206 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
207 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
211 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState()
216 bool VectorRasterizeThread::IsThreadReady()
218 ConditionalWait::ScopedLock lock( mConditionalWait );
220 if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
222 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
224 mConditionalWait.Wait( lock );
227 // Keep the thread alive if this thread is NOT to be destroyed
228 return !mDestroyThread;
231 bool VectorRasterizeThread::StartRender()
233 //TODO: check the return value
234 mVectorRenderer.StartRender();
236 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
238 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
239 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
241 mCurrentFrame = mStartFrame;
243 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame );
248 void VectorRasterizeThread::Rasterize()
250 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
253 mVectorRenderer.Render( mCurrentFrame );
255 if( mPlayState == DevelImageVisual::PlayState::PLAYING )
257 if( ++mCurrentFrame >= mEndFrame )
262 mCurrentFrame = mStartFrame;
267 if( mCurrentLoop >= mLoopCount )
269 // Animation is finished
270 mPlayState = DevelImageVisual::PlayState::STOPPED;
272 mAnimationFinishedTrigger->Trigger();
274 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
278 mCurrentFrame = mStartFrame;
286 if( !mResourceReady )
288 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
290 mResourceReadyTrigger->Trigger();
291 mResourceReady = true;
295 } // namespace Internal
297 } // namespace Toolkit