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