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, Renderer renderer, uint32_t width, uint32_t height )
53 mResourceReadyTrigger( NULL ),
54 mPlayRange( 0.0f, 1.0f ),
61 mLoopCount( LOOP_FOREVER ),
66 mDestroyThread( false ),
67 mResourceReady( false ),
68 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
70 mVectorRenderer = VectorAnimationRenderer::New( mUrl, renderer, width, height );
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;
92 void VectorRasterizeThread::Run()
94 SetThreadName( "VectorImageThread" );
95 mLogFactory.InstallLogFunction();
97 //TODO: check the return value
100 while( IsThreadReady() )
106 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
108 ConditionalWait::ScopedLock lock( mConditionalWait );
109 mVectorRenderer.SetSize( width, height );
111 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
114 void VectorRasterizeThread::StartAnimation()
116 ConditionalWait::ScopedLock lock( mConditionalWait );
121 mConditionalWait.Notify( lock );
123 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartAnimation: Start\n" );
127 void VectorRasterizeThread::StopAnimation()
129 ConditionalWait::ScopedLock lock( mConditionalWait );
135 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
139 void VectorRasterizeThread::PauseAnimation()
141 ConditionalWait::ScopedLock lock( mConditionalWait );
142 if( mPlaying && !mPaused )
146 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
150 void VectorRasterizeThread::ResumeAnimation()
152 ConditionalWait::ScopedLock lock( mConditionalWait );
153 if( mPlaying && mPaused )
156 mConditionalWait.Notify( lock );
158 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::ResumeAnimation: Resume\n" );
162 void VectorRasterizeThread::RenderFrame()
164 ConditionalWait::ScopedLock lock( mConditionalWait );
166 mConditionalWait.Notify( lock );
168 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
171 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
173 ConditionalWait::ScopedLock lock( mConditionalWait );
174 mResourceReadyTrigger = callback;
177 void VectorRasterizeThread::SetLoopCount( int16_t count )
179 ConditionalWait::ScopedLock lock( mConditionalWait );
185 mCurrentFrame = mStartFrame;
188 void VectorRasterizeThread::SetPlayRange( Vector2 range )
190 ConditionalWait::ScopedLock lock( mConditionalWait );
194 if( mTotalFrame != 0 )
196 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
197 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
201 bool VectorRasterizeThread::IsThreadReady()
203 ConditionalWait::ScopedLock lock( mConditionalWait );
205 if( ( !mPlaying || mPaused ) && !mNeedRender && !mDestroyThread )
207 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
211 mCurrentFrame = mStartFrame;
215 mConditionalWait.Wait( lock );
218 // Keep the thread alive if this thread is NOT to be destroyed
219 return !mDestroyThread;
222 bool VectorRasterizeThread::StartRender()
224 //TODO: check the return value
225 mVectorRenderer.StartRender();
227 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
229 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
230 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
232 mCurrentFrame = mStartFrame;
234 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame );
239 void VectorRasterizeThread::Rasterize()
241 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
244 mVectorRenderer.Render( mCurrentFrame );
246 if( mPlaying && !mPaused )
248 if( ++mCurrentFrame >= mEndFrame )
253 mCurrentFrame = mStartFrame;
258 if( mCurrentLoop >= mLoopCount )
260 // Animation is finished
265 mCurrentFrame = mStartFrame;
273 if( !mResourceReady )
275 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
277 mResourceReadyTrigger->Trigger();
278 mResourceReady = true;
282 } // namespace Internal
284 } // namespace Toolkit