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