70a3302fd318934c9eb342707265a2c3ee3e7d55
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.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-rasterize-thread.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/thread-settings.h>
23 #include <dali/integration-api/adaptors/adaptor.h>
24 #include <dali/integration-api/debug.h>
25 #include <chrono>
26 #include <thread>
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 inline void ResetValue( bool& updated, uint32_t& value, uint32_t newValue, ConditionalWait& conditionalWait )
48 {
49   ConditionalWait::ScopedLock lock( conditionalWait );
50   if( !updated )
51   {
52     value = newValue;
53     updated = true;
54   }
55 }
56
57 } // unnamed namespace
58
59 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
60 : mUrl( url ),
61   mVectorRenderer(),
62   mConditionalWait(),
63   mResourceReadyTrigger(),
64   mAnimationFinishedTrigger(),
65   mPlayState( PlayState::STOPPED ),
66   mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
67   mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
68   mFrameDurationNanoSeconds( 0 ),
69   mFrameRate( 60.0f ),
70   mCurrentFrame( 0 ),
71   mTotalFrame( 0 ),
72   mStartFrame( 0 ),
73   mEndFrame( 0 ),
74   mWidth( 0 ),
75   mHeight( 0 ),
76   mLoopCount( LOOP_FOREVER ),
77   mCurrentLoop( 0 ),
78   mNeedRender( false ),
79   mDestroyThread( false ),
80   mResourceReady( false ),
81   mCurrentFrameUpdated( false ),
82   mForward( true ),
83   mUpdateFrameNumber( false ),
84   mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
85 {
86   Initialize();
87 }
88
89 VectorRasterizeThread::~VectorRasterizeThread()
90 {
91   // Stop the thread
92   {
93     ConditionalWait::ScopedLock lock( mConditionalWait );
94     mDestroyThread = true;
95     mConditionalWait.Notify( lock );
96
97     // This should be called in the main thread to stop waiting for the dequeuable buffer.
98     mVectorRenderer.StopRender();
99   }
100
101   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
102
103   Join();
104 }
105
106 void VectorRasterizeThread::Run()
107 {
108   SetThreadName( "VectorImageThread" );
109   mLogFactory.InstallLogFunction();
110
111   while( !mDestroyThread )
112   {
113     Rasterize();
114   }
115
116   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread\n" );
117 }
118
119 void VectorRasterizeThread::SetRenderer( Renderer renderer )
120 {
121   ConditionalWait::ScopedLock lock( mConditionalWait );
122
123   mVectorRenderer.SetRenderer( renderer );
124
125   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
126 }
127
128 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
129 {
130   if( mWidth != width || mHeight != height )
131   {
132     ConditionalWait::ScopedLock lock( mConditionalWait );
133     mVectorRenderer.SetSize( width, height );
134
135     mWidth = width;
136     mHeight = height;
137
138     mResourceReady = false;
139
140     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
141   }
142 }
143
144 void VectorRasterizeThread::PlayAnimation()
145 {
146   ConditionalWait::ScopedLock lock( mConditionalWait );
147
148   if( mPlayState != PlayState::PLAYING )
149   {
150     mNeedRender = true;
151     mUpdateFrameNumber = false;
152     mPlayState = PlayState::PLAYING;
153     mConditionalWait.Notify( lock );
154
155     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Play\n" );
156   }
157 }
158
159 void VectorRasterizeThread::StopAnimation()
160 {
161   ConditionalWait::ScopedLock lock( mConditionalWait );
162   if( mPlayState != PlayState::STOPPED && mPlayState != PlayState::STOPPING )
163   {
164     mNeedRender = true;
165     mPlayState = PlayState::STOPPING;
166     mConditionalWait.Notify( lock );
167
168     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
169   }
170 }
171
172 void VectorRasterizeThread::PauseAnimation()
173 {
174   ConditionalWait::ScopedLock lock( mConditionalWait );
175   if( mPlayState == PlayState::PLAYING )
176   {
177     mPlayState = PlayState::PAUSED;
178
179     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
180   }
181 }
182
183 void VectorRasterizeThread::RenderFrame()
184 {
185   ConditionalWait::ScopedLock lock( mConditionalWait );
186
187   if( !mResourceReady )
188   {
189     mNeedRender = true;
190     mConditionalWait.Notify( lock );
191
192     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
193   }
194 }
195
196 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
197 {
198   ConditionalWait::ScopedLock lock( mConditionalWait );
199   mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
200 }
201
202 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
203 {
204   ConditionalWait::ScopedLock lock( mConditionalWait );
205   mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
206 }
207
208 void VectorRasterizeThread::SetLoopCount( int32_t count )
209 {
210   if( mLoopCount != count )
211   {
212     ConditionalWait::ScopedLock lock( mConditionalWait );
213
214     mLoopCount = count;
215
216     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d]\n", count );
217   }
218 }
219
220 void VectorRasterizeThread::SetPlayRange( uint32_t startFrame, uint32_t endFrame )
221 {
222   // Make sure the range specified is between 0 and the total frame number
223   if( ( startFrame < mTotalFrame ) && ( endFrame < mTotalFrame ) )
224   {
225     // If the range is not in order swap values
226     if( startFrame > endFrame )
227     {
228       uint32_t temp = startFrame;
229       startFrame = endFrame;
230       endFrame = temp;
231     }
232
233     if( startFrame != mStartFrame || endFrame != mEndFrame )
234     {
235       ConditionalWait::ScopedLock lock( mConditionalWait );
236
237       mStartFrame = startFrame;
238       mEndFrame = endFrame;
239
240       // If the current frame is out of the range, change the current frame also.
241       if( mStartFrame > mCurrentFrame )
242       {
243         mCurrentFrame = mStartFrame;
244
245         mCurrentFrameUpdated = true;
246         mResourceReady = false;
247       }
248       else if( mEndFrame < mCurrentFrame )
249       {
250         mCurrentFrame = mEndFrame;
251
252         mCurrentFrameUpdated = true;
253         mResourceReady = false;
254       }
255
256       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetPlayRangeInFrame: [%d, %d]\n", mStartFrame, mEndFrame );
257     }
258   }
259 }
260
261 DevelImageVisual::PlayState::Type VectorRasterizeThread::GetPlayState() const
262 {
263   DevelImageVisual::PlayState::Type state = DevelImageVisual::PlayState::STOPPED;
264
265   switch( mPlayState )
266   {
267     case PlayState::PLAYING:
268     {
269       state = DevelImageVisual::PlayState::PLAYING;
270       break;
271     }
272     case PlayState::PAUSED:
273     {
274       state = DevelImageVisual::PlayState::PAUSED;
275       break;
276     }
277     case PlayState::STOPPING:
278     case PlayState::STOPPED:
279     {
280       state = DevelImageVisual::PlayState::STOPPED;
281       break;
282     }
283   }
284
285   return state;
286 }
287
288 bool VectorRasterizeThread::IsResourceReady() const
289 {
290   return mResourceReady;
291 }
292
293 void VectorRasterizeThread::SetCurrentFrameNumber( uint32_t frameNumber )
294 {
295   ConditionalWait::ScopedLock lock( mConditionalWait );
296
297   if( frameNumber >= mStartFrame && frameNumber <= mEndFrame )
298   {
299     mCurrentFrame = frameNumber;
300     mCurrentFrameUpdated = true;
301
302     mResourceReady = false;
303
304     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentFrameNumber: frame number = %d\n", mCurrentFrame );
305   }
306   else
307   {
308     DALI_LOG_ERROR( "Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame );
309   }
310 }
311
312 uint32_t VectorRasterizeThread::GetCurrentFrameNumber() const
313 {
314   return mCurrentFrame;
315 }
316
317 uint32_t VectorRasterizeThread::GetTotalFrameNumber() const
318 {
319   return mTotalFrame;
320 }
321
322 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
323 {
324   mVectorRenderer.GetDefaultSize( width, height );
325 }
326
327 void VectorRasterizeThread::SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior )
328 {
329   ConditionalWait::ScopedLock lock( mConditionalWait );
330   mStopBehavior = stopBehavior;
331
332   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetStopBehavior: stop behavor = %d\n", mStopBehavior );
333 }
334
335 void VectorRasterizeThread::SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode )
336 {
337   ConditionalWait::ScopedLock lock( mConditionalWait );
338   mLoopingMode = loopingMode;
339
340   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopingMode: looping mode = %d\n", mLoopingMode );
341 }
342
343 void VectorRasterizeThread::Initialize()
344 {
345   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
346
347   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
348
349   mEndFrame = mTotalFrame - 1;
350
351   mFrameRate = mVectorRenderer.GetFrameRate();
352   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
353
354   uint32_t width, height;
355   mVectorRenderer.GetDefaultSize( width, height );
356
357   SetSize( width, height );
358
359   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
360 }
361
362 void VectorRasterizeThread::Rasterize()
363 {
364   bool resourceReady, stopped = false;
365   uint32_t currentFrame, startFrame, endFrame;
366   int32_t loopCount;
367
368   {
369     ConditionalWait::ScopedLock lock( mConditionalWait );
370
371     if( ( mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED ) && !mNeedRender && !mDestroyThread )
372     {
373       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
374       mConditionalWait.Wait( lock );
375     }
376
377     if( mPlayState == PlayState::PLAYING && mUpdateFrameNumber )
378     {
379       mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
380     }
381
382     resourceReady = mResourceReady;
383     currentFrame = mCurrentFrame;
384     startFrame = mStartFrame;
385     endFrame = mEndFrame;
386     loopCount = mLoopCount;
387
388     mNeedRender = false;
389     mResourceReady = true;
390     mCurrentFrameUpdated = false;
391     mUpdateFrameNumber = true;
392   }
393
394   auto currentFrameStartTime = std::chrono::system_clock::now();
395
396   if( mPlayState == PlayState::STOPPING )
397   {
398     currentFrame = GetStoppedFrame( startFrame, endFrame, currentFrame );
399     ResetValue( mCurrentFrameUpdated, mCurrentFrame, currentFrame, mConditionalWait );
400
401     stopped = true;
402   }
403   else if( mPlayState == 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 || ++mCurrentLoop < 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       }
425     }
426     else if( currentFrame == startFrame && !mForward )  // first frame
427     {
428       if( loopCount < 0 || ++mCurrentLoop < loopCount )   // repeat forever or before the last loop
429       {
430         mForward = true;
431       }
432       else
433       {
434         animationFinished = true;   // end of animation
435       }
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   mVectorRenderer.Render( currentFrame );
453
454   if( !resourceReady )
455   {
456     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
457
458     mResourceReadyTrigger->Trigger();
459   }
460
461   if( stopped )
462   {
463     mPlayState = PlayState::STOPPED;
464     mForward = true;
465     mCurrentLoop = 0;
466
467     // Animation is finished
468     mAnimationFinishedTrigger->Trigger();
469
470     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
471   }
472
473   auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
474
475 #if defined(DEBUG_ENABLED)
476   auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
477
478   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", currentFrame, sleepDuration.count() );
479 #endif
480
481   std::this_thread::sleep_until( timeToSleepUntil );
482 }
483
484 uint32_t VectorRasterizeThread::GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame )
485 {
486   uint32_t frame = currentFrame;
487
488   switch( mStopBehavior )
489   {
490     case DevelImageVisual::StopBehavior::FIRST_FRAME:
491     {
492       frame = startFrame;
493       break;
494     }
495     case DevelImageVisual::StopBehavior::LAST_FRAME:
496     {
497       if( mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE )
498       {
499         frame = startFrame;
500       }
501       else
502       {
503         frame = endFrame;
504       }
505       break;
506     }
507     case DevelImageVisual::StopBehavior::CURRENT_FRAME:
508     {
509       frame = currentFrame;
510       break;
511     }
512   }
513
514   return frame;
515 }
516
517 } // namespace Internal
518
519 } // namespace Toolkit
520
521 } // namespace Dali