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, Renderer renderer, uint32_t width, uint32_t height )
52 mResourceReadyTrigger( NULL ),
53 mPlayRange( 0.0f, 1.0f ),
60 mLoopCount( LOOP_FOREVER ),
65 mDestroyThread( false ),
66 mResourceReady( false ),
67 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
69 mVectorRenderer = VectorAnimationRenderer::New( mUrl, renderer, width, height );
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;
91 void VectorRasterizeThread::Run()
93 mLogFactory.InstallLogFunction();
95 //TODO: check the return value
98 while( IsThreadReady() )
104 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
106 ConditionalWait::ScopedLock lock( mConditionalWait );
107 mVectorRenderer.SetSize( width, height );
109 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
112 void VectorRasterizeThread::StartAnimation()
114 ConditionalWait::ScopedLock lock( mConditionalWait );
119 mConditionalWait.Notify( lock );
121 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartAnimation: Start\n" );
125 void VectorRasterizeThread::StopAnimation()
127 ConditionalWait::ScopedLock lock( mConditionalWait );
133 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
137 void VectorRasterizeThread::PauseAnimation()
139 ConditionalWait::ScopedLock lock( mConditionalWait );
140 if( mPlaying && !mPaused )
144 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
148 void VectorRasterizeThread::ResumeAnimation()
150 ConditionalWait::ScopedLock lock( mConditionalWait );
151 if( mPlaying && mPaused )
154 mConditionalWait.Notify( lock );
156 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::ResumeAnimation: Resume\n" );
160 void VectorRasterizeThread::RenderFrame()
162 ConditionalWait::ScopedLock lock( mConditionalWait );
164 mConditionalWait.Notify( lock );
166 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
169 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
171 ConditionalWait::ScopedLock lock( mConditionalWait );
172 mResourceReadyTrigger = callback;
175 void VectorRasterizeThread::SetLoopCount( int16_t count )
177 ConditionalWait::ScopedLock lock( mConditionalWait );
183 mCurrentFrame = mStartFrame;
186 void VectorRasterizeThread::SetPlayRange( Vector2 range )
188 ConditionalWait::ScopedLock lock( mConditionalWait );
192 if( mTotalFrame != 0 )
194 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
195 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
199 bool VectorRasterizeThread::IsThreadReady()
201 ConditionalWait::ScopedLock lock( mConditionalWait );
203 if( ( !mPlaying || mPaused ) && !mNeedRender && !mDestroyThread )
205 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
209 mCurrentFrame = mStartFrame;
213 mConditionalWait.Wait( lock );
216 // Keep the thread alive if this thread is NOT to be destroyed
217 return !mDestroyThread;
220 bool VectorRasterizeThread::StartRender()
222 //TODO: check the return value
223 mVectorRenderer.StartRender();
225 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
227 mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
228 mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
230 mCurrentFrame = mStartFrame;
232 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d (%d, %d)]\n", mTotalFrame, mStartFrame, mEndFrame );
237 void VectorRasterizeThread::Rasterize()
239 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
242 mVectorRenderer.Render( mCurrentFrame );
244 if( mPlaying && !mPaused )
246 if( ++mCurrentFrame >= mEndFrame )
251 mCurrentFrame = mStartFrame;
256 if( mCurrentLoop >= mLoopCount )
258 // Animation is finished
263 mCurrentFrame = mStartFrame;
271 if( !mResourceReady )
273 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
275 mResourceReadyTrigger->Trigger();
276 mResourceReady = true;
280 } // namespace Internal
282 } // namespace Toolkit