[Tizen](Vector) Add task even when StopAnimation is called
[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 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/math/math-utils.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
28 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41
42 constexpr auto LOOP_FOREVER = -1;
43 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
44
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
47 #endif
48
49 template< typename T >
50 inline void ResetValue( bool& updated, T& value, T newValue, ConditionalWait& conditionalWait )
51 {
52   ConditionalWait::ScopedLock lock( conditionalWait );
53   if( !updated )
54   {
55     value = newValue;
56     updated = true;
57   }
58 }
59
60 } // unnamed namespace
61
62 VectorAnimationTask::VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url )
63 : mUrl( url ),
64   mVectorRenderer(),
65   mVectorAnimationThread( factoryCache.GetVectorAnimationThread() ),
66   mConditionalWait(),
67   mAnimationFinishedTrigger(),
68   mPlayState( PlayState::STOPPED ),
69   mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
70   mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
71   mNextFrameStartTime(),
72   mFrameDurationNanoSeconds( 0 ),
73   mFrameRate( 60.0f ),
74   mCurrentFrame( 0 ),
75   mTotalFrame( 0 ),
76   mStartFrame( 0 ),
77   mEndFrame( 0 ),
78   mWidth( 0 ),
79   mHeight( 0 ),
80   mLoopCount( LOOP_FOREVER ),
81   mCurrentLoop( 0 ),
82   mResourceReady( false ),
83   mCurrentFrameUpdated( false ),
84   mCurrentLoopUpdated( false ),
85   mForward( true ),
86   mUpdateFrameNumber( false ),
87   mNeedAnimationFinishedTrigger( true ),
88   mForceRender( false )
89 {
90   Initialize();
91 }
92
93 VectorAnimationTask::~VectorAnimationTask()
94 {
95   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this );
96 }
97
98 void VectorAnimationTask::Finalize()
99 {
100   ConditionalWait::ScopedLock lock( mConditionalWait );
101
102   // Release some objects in the main thread
103   if( mAnimationFinishedTrigger )
104   {
105     mAnimationFinishedTrigger.reset();
106   }
107
108   mVectorRenderer.Finalize();
109 }
110
111 void VectorAnimationTask::SetRenderer( Renderer renderer )
112 {
113   ConditionalWait::ScopedLock lock( mConditionalWait );
114
115   mVectorRenderer.SetRenderer( renderer );
116
117   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this );
118 }
119
120 void VectorAnimationTask::SetAnimationData( const AnimationData& data )
121 {
122   ConditionalWait::ScopedLock lock( mConditionalWait );
123
124   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetAnimationData [%p]\n", this );
125
126   if( data.resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT )
127   {
128     SetLoopCount( data.loopCount );
129   }
130
131   if( data.resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE )
132   {
133     SetPlayRange( data.playRange );
134   }
135
136   if( data.resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR )
137   {
138     SetStopBehavior( data.stopBehavior );
139   }
140
141   if( data.resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE )
142   {
143     SetLoopingMode( data.loopingMode );
144   }
145
146   if( data.resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME )
147   {
148     SetCurrentFrameNumber( data.currentFrame );
149   }
150
151   if( data.resendFlag & VectorAnimationTask::RESEND_SIZE )
152   {
153     SetSize( data.width, data.height );
154   }
155
156   if( data.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE )
157   {
158     if( data.playState == DevelImageVisual::PlayState::PLAYING )
159     {
160       PlayAnimation();
161     }
162     else if( data.playState == DevelImageVisual::PlayState::PAUSED )
163     {
164       PauseAnimation();
165       RenderFrame();
166     }
167     else if( data.playState == DevelImageVisual::PlayState::STOPPED )
168     {
169       StopAnimation();
170     }
171   }
172   else
173   {
174     if( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED )
175     {
176       RenderFrame();
177     }
178   }
179 }
180
181 void VectorAnimationTask::SetSize( uint32_t width, uint32_t height )
182 {
183   if( mWidth != width || mHeight != height )
184   {
185     mVectorRenderer.SetSize( width, height );
186
187     mWidth = width;
188     mHeight = height;
189
190     mResourceReady = false;
191
192     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this );
193   }
194 }
195
196 void VectorAnimationTask::PlayAnimation()
197 {
198   if( mPlayState != PlayState::PLAYING )
199   {
200     mUpdateFrameNumber = false;
201     mPlayState = PlayState::PLAYING;
202
203     mVectorAnimationThread.AddTask( this );
204
205     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this );
206   }
207 }
208
209 void VectorAnimationTask::StopAnimation()
210 {
211   if( mPlayState != PlayState::STOPPING )
212   {
213     mNeedAnimationFinishedTrigger = false;
214     mPlayState = PlayState::STOPPING;
215
216     mVectorAnimationThread.AddTask( this );
217
218     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this );
219   }
220 }
221
222 void VectorAnimationTask::PauseAnimation()
223 {
224   if( mPlayState == PlayState::PLAYING )
225   {
226     mPlayState = PlayState::PAUSED;
227
228     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this );
229   }
230 }
231
232 void VectorAnimationTask::RenderFrame()
233 {
234   if( !mResourceReady )
235   {
236     mVectorAnimationThread.AddTask( this );
237
238     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::RenderFrame: Render [%p]\n", this );
239   }
240 }
241
242 void VectorAnimationTask::SetAnimationFinishedCallback( EventThreadCallback* callback )
243 {
244   ConditionalWait::ScopedLock lock( mConditionalWait );
245   if( callback )
246   {
247     mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
248   }
249 }
250
251 void VectorAnimationTask::SetLoopCount( int32_t count )
252 {
253   if( mLoopCount != count )
254   {
255     mLoopCount = count;
256     mCurrentLoop = 0;
257     mCurrentLoopUpdated = true;
258
259     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this );
260   }
261 }
262
263 void VectorAnimationTask::SetPlayRange( const Property::Array& playRange )
264 {
265   bool valid = false;
266   uint32_t startFrame = 0, endFrame = 0;
267   size_t count = playRange.Count();
268
269   if( count >= 2 )
270   {
271     int32_t start = 0, end = 0;
272     if( playRange.GetElementAt( 0 ).Get( start ) && playRange.GetElementAt( 1 ).Get( end ) )
273     {
274       startFrame = static_cast< uint32_t >( start );
275       endFrame = static_cast< uint32_t >( end );
276       valid = true;
277     }
278     else
279     {
280       std::string startMarker, endMarker;
281       if( playRange.GetElementAt( 0 ).Get( startMarker ) && playRange.GetElementAt( 1 ).Get( endMarker ) )
282       {
283         if( mVectorRenderer )
284         {
285           uint32_t frame;   // We don't use this later
286           if( mVectorRenderer.GetMarkerInfo( startMarker, startFrame, frame ) && mVectorRenderer.GetMarkerInfo( endMarker, frame, endFrame ) )
287           {
288             valid = true;
289           }
290         }
291       }
292     }
293   }
294   else if( count == 1 )
295   {
296     std::string marker;
297     if( playRange.GetElementAt( 0 ).Get( marker ) )
298     {
299       if( mVectorRenderer )
300       {
301         mVectorRenderer.GetMarkerInfo( marker, startFrame, endFrame );
302         valid = true;
303       }
304     }
305   }
306
307   if( !valid )
308   {
309     DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this );
310     return;
311   }
312
313   // Make sure the range specified is between 0 and the total frame number
314   if( startFrame < mTotalFrame && endFrame < mTotalFrame )
315   {
316     // If the range is not in order swap values
317     if( startFrame > endFrame )
318     {
319       uint32_t temp = startFrame;
320       startFrame = endFrame;
321       endFrame = temp;
322     }
323
324     if( startFrame != mStartFrame || endFrame != mEndFrame )
325     {
326       mStartFrame = startFrame;
327       mEndFrame = endFrame;
328
329       // If the current frame is out of the range, change the current frame also.
330       if( mStartFrame > mCurrentFrame )
331       {
332         mCurrentFrame = mStartFrame;
333
334         mCurrentFrameUpdated = true;
335         mResourceReady = false;
336       }
337       else if( mEndFrame < mCurrentFrame )
338       {
339         mCurrentFrame = mEndFrame;
340
341         mCurrentFrameUpdated = true;
342         mResourceReady = false;
343       }
344
345       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this );
346     }
347   }
348   else
349   {
350     DALI_LOG_ERROR( "VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this );
351     return;
352   }
353 }
354
355 void VectorAnimationTask::GetPlayRange( uint32_t& startFrame, uint32_t& endFrame )
356 {
357   startFrame = mStartFrame;
358   endFrame = mEndFrame;
359 }
360
361 DevelImageVisual::PlayState::Type VectorAnimationTask::GetPlayState() const
362 {
363   DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
364
365   switch( mPlayState )
366   {
367     case PlayState::PLAYING:
368     {
369       state = DevelImageVisual::PlayState::PLAYING;
370       break;
371     }
372     case PlayState::PAUSED:
373     {
374       state = DevelImageVisual::PlayState::PAUSED;
375       break;
376     }
377     case PlayState::STOPPING:
378     case PlayState::STOPPED:
379     {
380       state = DevelImageVisual::PlayState::STOPPED;
381       break;
382     }
383   }
384
385   return state;
386 }
387
388 void VectorAnimationTask::SetCurrentFrameNumber( uint32_t frameNumber )
389 {
390   if( mCurrentFrame == frameNumber )
391   {
392     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this );
393     return;
394   }
395
396   if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
397   {
398     mCurrentFrame = frameNumber;
399     mCurrentFrameUpdated = true;
400
401     mUpdateFrameNumber = false;
402     mResourceReady = false;
403
404     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this );
405   }
406   else
407   {
408     DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
409   }
410 }
411
412 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
413 {
414   return mCurrentFrame;
415 }
416
417 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
418 {
419   return mTotalFrame;
420 }
421
422 void VectorAnimationTask::GetDefaultSize( uint32_t& width, uint32_t& height ) const
423 {
424   mVectorRenderer.GetDefaultSize( width, height );
425 }
426
427 void VectorAnimationTask::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
428 {
429   mStopBehavior = stopBehavior;
430
431   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this );
432 }
433
434 void VectorAnimationTask::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
435 {
436   mLoopingMode = loopingMode;
437
438   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this );
439 }
440
441 void VectorAnimationTask::GetLayerInfo( Property::Map& map ) const
442 {
443   mVectorRenderer.GetLayerInfo( map );
444 }
445
446 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
447 {
448   return mVectorRenderer.UploadCompletedSignal();
449 }
450
451 void VectorAnimationTask::Initialize()
452 {
453   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
454
455   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
456
457   mEndFrame = mTotalFrame - 1;
458
459   mFrameRate = mVectorRenderer.GetFrameRate();
460   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
461
462   uint32_t width, height;
463   mVectorRenderer.GetDefaultSize( width, height );
464
465   SetSize( width, height );
466
467   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
468 }
469
470 bool VectorAnimationTask::Rasterize()
471 {
472   bool stopped = false, needAnimationFinishedTrigger, resourceReady;
473   uint32_t currentFrame, startFrame, endFrame;
474   int32_t loopCount, currentLoopCount;
475   PlayState playState;
476
477   {
478     ConditionalWait::ScopedLock lock( mConditionalWait );
479
480     if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
481     {
482       mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
483       Dali::ClampInPlace( mCurrentFrame, mStartFrame, mEndFrame );
484     }
485
486     currentFrame = mCurrentFrame;
487     startFrame = mStartFrame;
488     endFrame = mEndFrame;
489     loopCount = mLoopCount;
490     currentLoopCount = mCurrentLoop;
491     needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
492     playState = mPlayState;
493     resourceReady = mResourceReady;
494
495     mResourceReady = true;
496     mCurrentFrameUpdated = false;
497     mCurrentLoopUpdated = false;
498     mUpdateFrameNumber = true;
499     mNeedAnimationFinishedTrigger = true;
500   }
501
502   if( playState == PlayState::STOPPING )
503   {
504     currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
505     ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
506
507     stopped = true;
508   }
509   else if( playState == PlayState::PLAYING )
510   {
511     bool animationFinished = false;
512
513     if( currentFrame >= endFrame )  // last frame
514     {
515       if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
516       {
517         mForward = false;
518       }
519       else
520       {
521         if( loopCount < 0 || ++currentLoopCount < loopCount )   // repeat forever or before the last loop
522         {
523           ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );  // If the current frame is changed in the event thread, don't overwrite it.
524           mUpdateFrameNumber = false;
525         }
526         else
527         {
528           animationFinished = true;   // end of animation
529         }
530         ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
531       }
532     }
533     else if( currentFrame == startFrame && !mForward )  // first frame
534     {
535       if( loopCount < 0 || ++currentLoopCount < loopCount )   // repeat forever or before the last loop
536       {
537         mForward = true;
538       }
539       else
540       {
541         animationFinished = true;   // end of animation
542       }
543       ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
544     }
545
546     if( animationFinished )
547     {
548       if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
549       {
550         stopped = true;
551       }
552       else
553       {
554         mPlayState = PlayState::STOPPING;
555       }
556     }
557   }
558
559   // Rasterize
560   bool renderSuccess = false;
561   if( mVectorRenderer )
562   {
563     renderSuccess = mVectorRenderer.Render( currentFrame );
564     if( !renderSuccess )
565     {
566       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
567
568       if( mForceRender )
569       {
570         // Ignore previous frames and retry
571         mVectorRenderer.IgnoreRenderedFrames();
572         renderSuccess = mVectorRenderer.Render( currentFrame );
573         mForceRender = false;
574       }
575       else
576       {
577         mForceRender = true;
578       }
579
580       if( !renderSuccess )
581       {
582         mUpdateFrameNumber = false;
583         if( !resourceReady )
584         {
585           ConditionalWait::ScopedLock lock( mConditionalWait );
586           mResourceReady = false;
587         }
588       }
589     }
590     else
591     {
592       mForceRender = false;
593     }
594   }
595
596   if( stopped && renderSuccess )
597   {
598     ConditionalWait::ScopedLock lock( mConditionalWait );
599
600     mPlayState = PlayState::STOPPED;
601     mForward = true;
602     mCurrentLoop = 0;
603
604     // Animation is finished
605     if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
606     {
607       mAnimationFinishedTrigger->Trigger();
608     }
609
610     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
611   }
612
613   bool keepAnimation = true;
614   if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
615   {
616     keepAnimation = false;
617   }
618
619   return keepAnimation;
620 }
621
622 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
623 {
624   uint32_t frame = currentFrame;
625
626   switch( mStopBehavior )
627   {
628     case DevelImageVisual::StopBehavior::FIRST_FRAME:
629     {
630       frame = startFrame;
631       break;
632     }
633     case DevelImageVisual::StopBehavior::LAST_FRAME:
634     {
635       if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
636       {
637         frame = startFrame;
638       }
639       else
640       {
641         frame = endFrame;
642       }
643       break;
644     }
645     case DevelImageVisual::StopBehavior::CURRENT_FRAME:
646     {
647       frame = currentFrame;
648       break;
649     }
650   }
651
652   return frame;
653 }
654
655 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
656 {
657   // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
658   // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
659   // is casted to use the default duration.
660   mNextFrameStartTime =  std::chrono::time_point_cast< std::chrono::time_point< std::chrono::system_clock >::duration >(
661       mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds ) );
662   auto current = std::chrono::system_clock::now();
663   if( renderNow || mNextFrameStartTime < current )
664   {
665     mNextFrameStartTime = current;
666   }
667   return mNextFrameStartTime;
668 }
669
670 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
671 {
672   return mNextFrameStartTime;
673 }
674
675 } // namespace Internal
676
677 } // namespace Toolkit
678
679 } // namespace Dali