9469daea88915f5d36db4767423bad7bf1b5fd49
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
1 /*
2  * Copyright (c) 2019 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 inline void ResetValue( bool& updated, uint32_t& value, uint32_t newValue, ConditionalWait& conditionalWait )
48 {
49   ConditionalWait::ScopedLock lock( conditionalWait );
50   if( !updated )
51   {
52     value = newValue;
53     updated = true;
54   }
55 }
56
57 } // unnamed namespace
58
59 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
60 : mUrl( url ),
61   mVectorRenderer(),
62   mConditionalWait(),
63   mResourceReadyTrigger(),
64   mAnimationFinishedTrigger(),
65   mPlayState( PlayState::STOPPED ),
66   mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
67   mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
68   mFrameDurationNanoSeconds( 0 ),
69   mFrameRate( 60.0f ),
70   mCurrentFrame( 0 ),
71   mTotalFrame( 0 ),
72   mStartFrame( 0 ),
73   mEndFrame( 0 ),
74   mWidth( 0 ),
75   mHeight( 0 ),
76   mLoopCount( LOOP_FOREVER ),
77   mCurrentLoop( 0 ),
78   mNeedRender( false ),
79   mDestroyThread( false ),
80   mResourceReady( false ),
81   mCurrentFrameUpdated( false ),
82   mForward( true ),
83   mUpdateFrameNumber( false ),
84   mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
85 {
86   Initialize();
87 }
88
89 VectorRasterizeThread::~VectorRasterizeThread()
90 {
91   // Stop the thread
92   {
93     ConditionalWait::ScopedLock lock( mConditionalWait );
94     mDestroyThread = true;
95     mConditionalWait.Notify( lock );
96   }
97
98   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this );
99
100   Join();
101 }
102
103 void VectorRasterizeThread::Run()
104 {
105   SetThreadName( "VectorImageThread" );
106   mLogFactory.InstallLogFunction();
107
108   while( !mDestroyThread )
109   {
110     Rasterize();
111   }
112
113   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread [%p]\n", this );
114 }
115
116 void VectorRasterizeThread::SetRenderer( Renderer renderer )
117 {
118   ConditionalWait::ScopedLock lock( mConditionalWait );
119
120   mVectorRenderer.SetRenderer( renderer );
121
122   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer [%p]\n", this );
123 }
124
125 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
126 {
127   if( mWidth != width || mHeight != height )
128   {
129     ConditionalWait::ScopedLock lock( mConditionalWait );
130     mVectorRenderer.SetSize( width, height );
131
132     mWidth = width;
133     mHeight = height;
134
135     mResourceReady = false;
136
137     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d [%p]\n", width, height, this );
138   }
139 }
140
141 void VectorRasterizeThread::PlayAnimation()
142 {
143   ConditionalWait::ScopedLock lock( mConditionalWait );
144
145   if( mPlayState != PlayState::PLAYING )
146   {
147     mNeedRender = true;
148     mUpdateFrameNumber = false;
149     mPlayState = PlayState::PLAYING;
150     mConditionalWait.Notify( lock );
151
152     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play [%p]\n", this );
153   }
154 }
155
156 void VectorRasterizeThread::StopAnimation()
157 {
158   ConditionalWait::ScopedLock lock( mConditionalWait );
159   if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
160   {
161     mNeedRender = true;
162     mPlayState = PlayState::STOPPING;
163     mConditionalWait.Notify( lock );
164
165     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop [%p]\n", this );
166   }
167 }
168
169 void VectorRasterizeThread::PauseAnimation()
170 {
171   ConditionalWait::ScopedLock lock( mConditionalWait );
172   if( mPlayState == PlayState::PLAYING )
173   {
174     mPlayState = PlayState::PAUSED;
175
176     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause [%p]\n", this );
177   }
178 }
179
180 void VectorRasterizeThread::RenderFrame()
181 {
182   ConditionalWait::ScopedLock lock( mConditionalWait );
183
184   if( !mResourceReady )
185   {
186     mNeedRender = true;
187     mConditionalWait.Notify( lock );
188
189     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render [%p]\n", this );
190   }
191 }
192
193 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
194 {
195   ConditionalWait::ScopedLock lock( mConditionalWait );
196   mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
197 }
198
199 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
200 {
201   ConditionalWait::ScopedLock lock( mConditionalWait );
202   mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
203 }
204
205 void VectorRasterizeThread::SetLoopCount( int32_t count )
206 {
207   if( mLoopCount != count )
208   {
209     ConditionalWait::ScopedLock lock( mConditionalWait );
210
211     mLoopCount = count;
212
213     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d] [%p]\n", count, this );
214   }
215 }
216
217 void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
218 {
219   // Make sure the range specified is between 0 and the total frame number
220   if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
221   {
222     // If the range is not in order swap values
223     if( startFrame > endFrame )
224     {
225       uint32_t temp = startFrame;
226       startFrame = endFrame;
227       endFrame = temp;
228     }
229
230     if( startFrame != mStartFrame || endFrame != mEndFrame )
231     {
232       ConditionalWait::ScopedLock lock( mConditionalWait );
233
234       mStartFrame = startFrame;
235       mEndFrame = endFrame;
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         mCurrentFrameUpdated = true;
243         mResourceReady = false;
244       }
245       else if( mEndFrame < mCurrentFrame )
246       {
247         mCurrentFrame = mEndFrame;
248
249         mCurrentFrameUpdated = true;
250         mResourceReady = false;
251       }
252
253       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
254     }
255   }
256 }
257
258 DevelImageVisual::PlayState::Type VectorRasterizeThread::GetPlayState() const
259 {
260   DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
261
262   switch( mPlayState )
263   {
264     case PlayState::PLAYING:
265     {
266       state = DevelImageVisual::PlayState::PLAYING;
267       break;
268     }
269     case PlayState::PAUSED:
270     {
271       state = DevelImageVisual::PlayState::PAUSED;
272       break;
273     }
274     case PlayState::STOPPING:
275     case PlayState::STOPPED:
276     {
277       state = DevelImageVisual::PlayState::STOPPED;
278       break;
279     }
280   }
281
282   return state;
283 }
284
285 bool VectorRasterizeThread::IsResourceReady() const
286 {
287   return mResourceReady;
288 }
289
290 void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
291 {
292   ConditionalWait::ScopedLock lock( mConditionalWait );
293
294   if( frameNumber >= mStartFrame && frameNumber <= mEndFrame && mCurrentFrame != frameNumber )
295   {
296     mCurrentFrame = frameNumber;
297     mCurrentFrameUpdated = true;
298
299     mResourceReady = false;
300
301     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
302   }
303   else
304   {
305     DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
306   }
307 }
308
309 uint32_t VectorRasterizeThread::GetCurrentFrameNumber() const
310 {
311   return mCurrentFrame;
312 }
313
314 uint32_t VectorRasterizeThread::GetTotalFrameNumber() const
315 {
316   return mTotalFrame;
317 }
318
319 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
320 {
321   mVectorRenderer.GetDefaultSize( width, height );
322 }
323
324 void VectorRasterizeThread::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
325 {
326   ConditionalWait::ScopedLock lock( mConditionalWait );
327   mStopBehavior = stopBehavior;
328
329   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
330 }
331
332 void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
333 {
334   ConditionalWait::ScopedLock lock( mConditionalWait );
335   mLoopingMode = loopingMode;
336
337   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
338 }
339
340 void VectorRasterizeThread::Initialize()
341 {
342   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
343
344   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
345
346   mEndFrame = mTotalFrame - 1;
347
348   mFrameRate = mVectorRenderer.GetFrameRate();
349   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
350
351   uint32_t width, height;
352   mVectorRenderer.GetDefaultSize( width, height );
353
354   SetSize( width, height );
355
356   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
357 }
358
359 void VectorRasterizeThread::Rasterize()
360 {
361   bool resourceReady, stopped = false;
362   uint32_t currentFrame, startFrame, endFrame;
363   int32_t loopCount;
364
365   {
366     ConditionalWait::ScopedLock lock( mConditionalWait );
367
368     if( ( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED ) && !mNeedRender && !mDestroyThread )
369     {
370       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait [%p]\n", this );
371       mConditionalWait.Wait( lock );
372     }
373
374     if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
375     {
376       mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
377     }
378
379     resourceReady = mResourceReady;
380     currentFrame = mCurrentFrame;
381     startFrame = mStartFrame;
382     endFrame = mEndFrame;
383     loopCount = mLoopCount;
384
385     mNeedRender = false;
386     mResourceReady = true;
387     mCurrentFrameUpdated = false;
388     mUpdateFrameNumber = true;
389   }
390
391   auto currentFrameStartTime = std::chrono::system_clock::now();
392
393   if( mPlayState == PlayState::STOPPING )
394   {
395     currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
396     ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
397
398     stopped = true;
399   }
400   else if( mPlayState == PlayState::PLAYING )
401   {
402     bool animationFinished = false;
403
404     if( currentFrame >= endFrame )  // last frame
405     {
406       if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
407       {
408         mForward = false;
409       }
410       else
411       {
412         if( loopCount < 0 || ++mCurrentLoop < loopCount )   // repeat forever or before the last loop
413         {
414           ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );  // If the current frame is changed in the event thread, don't overwrite it.
415           mUpdateFrameNumber = false;
416         }
417         else
418         {
419           animationFinished = true;   // end of animation
420         }
421       }
422     }
423     else if( currentFrame == startFrame && !mForward )  // first frame
424     {
425       if( loopCount < 0 || ++mCurrentLoop < loopCount )   // repeat forever or before the last loop
426       {
427         mForward = true;
428       }
429       else
430       {
431         animationFinished = true;   // end of animation
432       }
433     }
434
435     if( animationFinished )
436     {
437       if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
438       {
439         stopped = true;
440       }
441       else
442       {
443         mPlayState = PlayState::STOPPING;
444       }
445     }
446   }
447
448   // Rasterize
449   if( !mVectorRenderer.Render( currentFrame ) )
450   {
451     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
452     mUpdateFrameNumber = false;
453   }
454
455   if( !resourceReady )
456   {
457     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger [current = %d] [%p]\n", currentFrame, this );
458
459     mResourceReadyTrigger->Trigger();
460   }
461
462   if( stopped )
463   {
464     mPlayState = PlayState::STOPPED;
465     mForward = true;
466     mCurrentLoop = 0;
467
468     // Animation is finished
469     mAnimationFinishedTrigger->Trigger();
470
471     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
472   }
473
474   auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
475
476 #if defined(DEBUG_ENABLED)
477   auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
478
479   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld] [%p]\n", currentFrame, sleepDuration.count(), this );
480 #endif
481
482   std::this_thread::sleep_until( timeToSleepUntil );
483 }
484
485 uint32_t VectorRasterizeThread::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
486 {
487   uint32_t frame = currentFrame;
488
489   switch( mStopBehavior )
490   {
491     case DevelImageVisual::StopBehavior::FIRST_FRAME:
492     {
493       frame = startFrame;
494       break;
495     }
496     case DevelImageVisual::StopBehavior::LAST_FRAME:
497     {
498       if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
499       {
500         frame = startFrame;
501       }
502       else
503       {
504         frame = endFrame;
505       }
506       break;
507     }
508     case DevelImageVisual::StopBehavior::CURRENT_FRAME:
509     {
510       frame = currentFrame;
511       break;
512     }
513   }
514
515   return frame;
516 }
517
518 } // namespace Internal
519
520 } // namespace Toolkit
521
522 } // namespace Dali