[dali_1.4.11] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h>
20
21 // EXTERNAL INCLUDES
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>
25 #include <chrono>
26 #include <thread>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 constexpr auto LOOP_FOREVER = -1;
41 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
42
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
45 #endif
46
47 } // unnamed namespace
48
49 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
50 : mUrl( url ),
51   mVectorRenderer(),
52   mConditionalWait(),
53   mMutex(),
54   mResourceReadyTrigger(),
55   mAnimationFinishedTrigger(),
56   mPlayRange( 0.0f, 1.0f ),
57   mPlayState( DevelImageVisual::PlayState::STOPPED ),
58   mFrameDurationNanoSeconds( 0 ),
59   mProgress( 0.0f ),
60   mFrameRate( 60.0f ),
61   mCurrentFrame( 0 ),
62   mTotalFrame( 0 ),
63   mStartFrame( 0 ),
64   mEndFrame( 0 ),
65   mWidth( 0 ),
66   mHeight( 0 ),
67   mLoopCount( LOOP_FOREVER ),
68   mCurrentLoop( 0 ),
69   mNeedRender( false ),
70   mDestroyThread( false ),
71   mResourceReady( false ),
72   mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
73 {
74   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
75 }
76
77 VectorRasterizeThread::~VectorRasterizeThread()
78 {
79   // Stop the thread
80   {
81     ConditionalWait::ScopedLock lock( mConditionalWait );
82     mDestroyThread = true;
83     mConditionalWait.Notify( lock );
84
85     // This should be called in the main thread to stop waiting for the dequeuable buffer.
86     mVectorRenderer.StopRender();
87   }
88
89   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
90
91   Join();
92 }
93
94 void VectorRasterizeThread::Run()
95 {
96   SetThreadName( "VectorImageThread" );
97   mLogFactory.InstallLogFunction();
98
99   //TODO: check the return value
100   StartRender();
101
102   while( IsThreadReady() )
103   {
104     Rasterize();
105   }
106 }
107
108 void VectorRasterizeThread::SetRenderer( Renderer renderer )
109 {
110   ConditionalWait::ScopedLock lock( mConditionalWait );
111
112   mVectorRenderer.SetRenderer( renderer );
113
114   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
115 }
116
117 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
118 {
119   if( mWidth != width || mHeight != height )
120   {
121     ConditionalWait::ScopedLock lock( mConditionalWait );
122     mVectorRenderer.SetSize( width, height );
123
124     mWidth = width;
125     mHeight = height;
126
127     mResourceReady = false;
128
129     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
130   }
131 }
132
133 void VectorRasterizeThread::PlayAnimation()
134 {
135   ConditionalWait::ScopedLock lock( mConditionalWait );
136   if( mPlayState != DevelImageVisual::PlayState::PLAYING )
137   {
138     mPlayState = DevelImageVisual::PlayState::PLAYING;
139     mConditionalWait.Notify( lock );
140
141     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
142   }
143 }
144
145 void VectorRasterizeThread::StopAnimation()
146 {
147   ConditionalWait::ScopedLock lock( mConditionalWait );
148   if( mPlayState != DevelImageVisual::PlayState::STOPPED )
149   {
150     mPlayState = DevelImageVisual::PlayState::STOPPED;
151
152     // Reset the current frame and the current loop
153     mCurrentFrame = mStartFrame;
154     mCurrentLoop = 0;
155
156     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
157   }
158 }
159
160 void VectorRasterizeThread::PauseAnimation()
161 {
162   ConditionalWait::ScopedLock lock( mConditionalWait );
163   if( mPlayState == DevelImageVisual::PlayState::PLAYING )
164   {
165     mPlayState = DevelImageVisual::PlayState::PAUSED;
166
167     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
168   }
169 }
170
171 void VectorRasterizeThread::RenderFrame()
172 {
173   ConditionalWait::ScopedLock lock( mConditionalWait );
174
175   if( !mResourceReady )
176   {
177     mNeedRender = true;
178     mConditionalWait.Notify( lock );
179
180     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
181   }
182 }
183
184 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
185 {
186   ConditionalWait::ScopedLock lock( mConditionalWait );
187   mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
188 }
189
190 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
191 {
192   ConditionalWait::ScopedLock lock( mConditionalWait );
193   mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
194 }
195
196 void VectorRasterizeThread::SetLoopCount( int32_t count )
197 {
198   if( mLoopCount != count )
199   {
200     ConditionalWait::ScopedLock lock( mConditionalWait );
201
202     mLoopCount = count;
203
204     // Reset progress
205     mCurrentLoop = 0;
206   }
207 }
208
209 int32_t VectorRasterizeThread::GetLoopCount() const
210 {
211   return mLoopCount;
212 }
213
214 void VectorRasterizeThread::SetPlayRange( Vector2 range )
215 {
216   // Make sure the range specified is between 0.0 and 1.0
217   if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
218   {
219     Vector2 orderedRange( range );
220     // If the range is not in order swap values
221     if( range.x > range.y )
222     {
223       orderedRange = Vector2( range.y, range.x );
224     }
225
226     if( mPlayRange != orderedRange )
227     {
228       ConditionalWait::ScopedLock lock( mConditionalWait );
229
230       mPlayRange = orderedRange;
231
232       if( mTotalFrame != 0 )
233       {
234         mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
235         mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
236
237         // If the current frame is out of the range, change the current frame also.
238         if( mStartFrame > mCurrentFrame )
239         {
240           mCurrentFrame = mStartFrame;
241
242           mResourceReady = false;
243         }
244         else if( mEndFrame < mCurrentFrame )
245         {
246           mCurrentFrame = mEndFrame;
247
248           mResourceReady = false;
249         }
250       }
251     }
252   }
253 }
254
255 Vector2 VectorRasterizeThread::GetPlayRange() const
256 {
257   return mPlayRange;
258 }
259
260 void VectorRasterizeThread::SetCurrentProgress( float progress )
261 {
262   ConditionalWait::ScopedLock lock( mConditionalWait );
263
264   if( progress >= mPlayRange.x && progress <= mPlayRange.y )
265   {
266     mProgress = progress;
267
268     if( mTotalFrame != 0 )
269     {
270       mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
271     }
272
273     mResourceReady = false;
274
275     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
276   }
277 }
278
279 float VectorRasterizeThread::GetCurrentProgress() const
280 {
281   return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
282 }
283
284 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
285 {
286   return mPlayState;
287 }
288
289 bool VectorRasterizeThread::IsResourceReady() const
290 {
291   return mResourceReady;
292 }
293
294 bool VectorRasterizeThread::IsThreadReady()
295 {
296   ConditionalWait::ScopedLock lock( mConditionalWait );
297
298   if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
299   {
300     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
301
302     mConditionalWait.Wait( lock );
303   }
304
305   // Keep the thread alive if this thread is NOT to be destroyed
306   return !mDestroyThread;
307 }
308
309 bool VectorRasterizeThread::StartRender()
310 {
311   //TODO: check the return value
312   mVectorRenderer.StartRender();
313
314   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
315
316   mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
317   mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
318
319   mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
320
321   mFrameRate = mVectorRenderer.GetFrameRate();
322   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
323
324   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d, %f fps]\n", mTotalFrame, mFrameRate );
325
326   return true;
327 }
328
329 void VectorRasterizeThread::Rasterize()
330 {
331   bool needRender, resourceReady;
332
333   {
334     ConditionalWait::ScopedLock lock( mConditionalWait );
335     needRender = mNeedRender;
336     resourceReady = mResourceReady;
337   }
338
339   auto currentFrameStartTime = std::chrono::system_clock::now();
340
341   // Rasterize
342   mVectorRenderer.Render( mCurrentFrame );
343
344   if( mPlayState == DevelImageVisual::PlayState::PLAYING )
345   {
346     if( ++mCurrentFrame >= mEndFrame )
347     {
348       if( mLoopCount < 0 )
349       {
350         // repeat forever
351         mCurrentFrame = mStartFrame;
352       }
353       else
354       {
355         mCurrentLoop++;
356         if( mCurrentLoop >= mLoopCount )
357         {
358           // Animation is finished
359           mPlayState = DevelImageVisual::PlayState::STOPPED;
360
361           // Reset the current frame and the current loop
362           mCurrentFrame = mStartFrame;
363           mCurrentLoop = 0;
364
365           mAnimationFinishedTrigger->Trigger();
366
367           DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
368         }
369         else
370         {
371           mCurrentFrame = mStartFrame;
372         }
373       }
374     }
375   }
376
377   if( needRender )
378   {
379     mNeedRender = false;
380   }
381
382   if( !resourceReady )
383   {
384     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
385
386     mResourceReadyTrigger->Trigger();
387     mResourceReady = true;
388   }
389
390   auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
391
392 #if defined(DEBUG_ENABLED)
393   auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
394
395   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
396 #endif
397
398   std::this_thread::sleep_until( timeToSleepUntil );
399 }
400
401 } // namespace Internal
402
403 } // namespace Toolkit
404
405 } // namespace Dali