(Vector) Refactor vector to use thread pool
[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 VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
321 {
322   return mVectorRenderer.UploadCompletedSignal();
323 }
324
325 void VectorAnimationTask::Initialize()
326 {
327   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
328
329   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
330
331   mEndFrame = mTotalFrame - 1;
332
333   mFrameRate = mVectorRenderer.GetFrameRate();
334   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
335
336   uint32_t width, height;
337   mVectorRenderer.GetDefaultSize( width, height );
338
339   SetSize( width, height );
340
341   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this );
342 }
343
344 bool VectorAnimationTask::Rasterize()
345 {
346   bool stopped = false, needAnimationFinishedTrigger;
347   uint32_t currentFrame, startFrame, endFrame;
348   int32_t loopCount, currentLoopCount;
349   PlayState playState;
350
351   {
352     ConditionalWait::ScopedLock lock( mConditionalWait );
353
354     if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
355     {
356       mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
357     }
358
359     currentFrame = mCurrentFrame;
360     startFrame = mStartFrame;
361     endFrame = mEndFrame;
362     loopCount = mLoopCount;
363     currentLoopCount = mCurrentLoop;
364     needAnimationFinishedTrigger = mNeedAnimationFinishedTrigger;
365     playState = mPlayState;
366
367     mResourceReady = true;
368     mCurrentFrameUpdated = false;
369     mCurrentLoopUpdated = false;
370     mUpdateFrameNumber = true;
371     mNeedAnimationFinishedTrigger = true;
372   }
373
374   if( playState == PlayState::STOPPING )
375   {
376     currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
377     ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
378
379     stopped = true;
380   }
381   else if( playState == PlayState::PLAYING )
382   {
383     bool animationFinished = false;
384
385     if( currentFrame >= endFrame )  // last frame
386     {
387       if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
388       {
389         mForward = false;
390       }
391       else
392       {
393         if( loopCount < 0 || ++currentLoopCount < loopCount )   // repeat forever or before the last loop
394         {
395           ResetValue( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );  // If the current frame is changed in the event thread, don't overwrite it.
396           mUpdateFrameNumber = false;
397         }
398         else
399         {
400           animationFinished = true;   // end of animation
401         }
402         ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
403       }
404     }
405     else if( currentFrame == startFrame && !mForward )  // first frame
406     {
407       if( loopCount < 0 || ++currentLoopCount < loopCount )   // repeat forever or before the last loop
408       {
409         mForward = true;
410       }
411       else
412       {
413         animationFinished = true;   // end of animation
414       }
415       ResetValue( mCurrentLoopUpdated, mCurrentLoop, currentLoopCount, mConditionalWait );
416     }
417
418     if( animationFinished )
419     {
420       if( mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME )
421       {
422         stopped = true;
423       }
424       else
425       {
426         mPlayState = PlayState::STOPPING;
427       }
428     }
429   }
430
431   // Rasterize
432   bool renderSuccess = mVectorRenderer.Render( currentFrame );
433   if( !renderSuccess )
434   {
435     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this );
436     mUpdateFrameNumber = false;
437   }
438
439   if( stopped && renderSuccess )
440   {
441     mPlayState = PlayState::STOPPED;
442     mForward = true;
443     mCurrentLoop = 0;
444
445     // Animation is finished
446     if( needAnimationFinishedTrigger && mAnimationFinishedTrigger )
447     {
448       mAnimationFinishedTrigger->Trigger();
449     }
450
451     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this );
452   }
453
454   bool keepAnimation = true;
455   if( playState == PlayState::PAUSED || playState == PlayState::STOPPED )
456   {
457     keepAnimation = false;
458   }
459
460   return keepAnimation;
461 }
462
463 uint32_t VectorAnimationTask::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
464 {
465   uint32_t frame = currentFrame;
466
467   switch( mStopBehavior )
468   {
469     case DevelImageVisual::StopBehavior::FIRST_FRAME:
470     {
471       frame = startFrame;
472       break;
473     }
474     case DevelImageVisual::StopBehavior::LAST_FRAME:
475     {
476       if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
477       {
478         frame = startFrame;
479       }
480       else
481       {
482         frame = endFrame;
483       }
484       break;
485     }
486     case DevelImageVisual::StopBehavior::CURRENT_FRAME:
487     {
488       frame = currentFrame;
489       break;
490     }
491   }
492
493   return frame;
494 }
495
496 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::CalculateNextFrameTime( bool renderNow )
497 {
498   mNextFrameStartTime = mNextFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
499   auto current = std::chrono::system_clock::now();
500   if( renderNow || mNextFrameStartTime < current )
501   {
502     mNextFrameStartTime = current;
503   }
504   return mNextFrameStartTime;
505 }
506
507 std::chrono::time_point< std::chrono::system_clock > VectorAnimationTask::GetNextFrameTime()
508 {
509   return mNextFrameStartTime;
510 }
511
512 } // namespace Internal
513
514 } // namespace Toolkit
515
516 } // namespace Dali