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