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