d5515f911bbca5f0869bb199a3ac5d00c2384ecc
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
1 /*
2  * Copyright (c) 2018 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/integration-api/adaptors/adaptor.h>
23 #include <dali/integration-api/debug.h>
24 #include <chrono>
25 #include <thread>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal
34 {
35
36 namespace
37 {
38
39 constexpr auto LOOP_FOREVER = -1;
40 constexpr auto NANOSECONDS_PER_SECOND( 1e+9 );
41
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
44 #endif
45
46 inline void ResetToStart( bool& updated, uint32_t& value, uint32_t startValue, ConditionalWait& conditionalWait )
47 {
48   ConditionalWait::ScopedLock lock( conditionalWait );
49   if( !updated )
50   {
51     value = startValue;
52   }
53 }
54
55 } // unnamed namespace
56
57 VectorRasterizeThread::VectorRasterizeThread( const std::string& url )
58 : mUrl( url ),
59   mVectorRenderer(),
60   mConditionalWait(),
61   mResourceReadyTrigger(),
62   mAnimationFinishedTrigger(),
63   mPlayRange( 0.0f, 1.0f ),
64   mPlayState( DevelImageVisual::PlayState::STOPPED ),
65   mFrameDurationNanoSeconds( 0 ),
66   mProgress( 0.0f ),
67   mFrameRate( 60.0f ),
68   mCurrentFrame( 0 ),
69   mTotalFrame( 0 ),
70   mStartFrame( 0 ),
71   mEndFrame( 0 ),
72   mWidth( 0 ),
73   mHeight( 0 ),
74   mLoopCount( LOOP_FOREVER ),
75   mCurrentLoop( 0 ),
76   mNeedRender( false ),
77   mDestroyThread( false ),
78   mResourceReady( false ),
79   mCurrentFrameUpdated( false ),
80   mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
81 {
82   Initialize();
83 }
84
85 VectorRasterizeThread::~VectorRasterizeThread()
86 {
87   // Stop the thread
88   {
89     ConditionalWait::ScopedLock lock( mConditionalWait );
90     mDestroyThread = true;
91     mConditionalWait.Notify( lock );
92
93     // This should be called in the main thread to stop waiting for the dequeuable buffer.
94     mVectorRenderer.StopRender();
95   }
96
97   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
98
99   Join();
100 }
101
102 void VectorRasterizeThread::Run()
103 {
104   mLogFactory.InstallLogFunction();
105
106   while( !mDestroyThread )
107   {
108     Rasterize();
109   }
110
111   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Run: End of thread\n" );
112 }
113
114 void VectorRasterizeThread::SetRenderer( Renderer renderer )
115 {
116   ConditionalWait::ScopedLock lock( mConditionalWait );
117
118   mVectorRenderer.SetRenderer( renderer );
119
120   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
121 }
122
123 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
124 {
125   if( mWidth != width || mHeight != height )
126   {
127     ConditionalWait::ScopedLock lock( mConditionalWait );
128     mVectorRenderer.SetSize( width, height );
129
130     mWidth = width;
131     mHeight = height;
132
133     mResourceReady = false;
134
135     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
136   }
137 }
138
139 void VectorRasterizeThread::PlayAnimation()
140 {
141   ConditionalWait::ScopedLock lock( mConditionalWait );
142
143   if( mPlayState != DevelImageVisual::PlayState::PLAYING )
144   {
145     mPlayState = DevelImageVisual::PlayState::PLAYING;
146     mConditionalWait.Notify( lock );
147
148     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
149   }
150 }
151
152 void VectorRasterizeThread::StopAnimation()
153 {
154   ConditionalWait::ScopedLock lock( mConditionalWait );
155   if( mPlayState != DevelImageVisual::PlayState::STOPPED )
156   {
157     mPlayState = DevelImageVisual::PlayState::STOPPED;
158
159     mCurrentFrame = mStartFrame;
160     mCurrentFrameUpdated = true;
161
162     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
163   }
164 }
165
166 void VectorRasterizeThread::PauseAnimation()
167 {
168   ConditionalWait::ScopedLock lock( mConditionalWait );
169   if( mPlayState == DevelImageVisual::PlayState::PLAYING )
170   {
171     mPlayState = DevelImageVisual::PlayState::PAUSED;
172
173     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
174   }
175 }
176
177 void VectorRasterizeThread::RenderFrame()
178 {
179   ConditionalWait::ScopedLock lock( mConditionalWait );
180
181   if( !mResourceReady )
182   {
183     mNeedRender = true;
184     mConditionalWait.Notify( lock );
185
186     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
187   }
188 }
189
190 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
191 {
192   ConditionalWait::ScopedLock lock( mConditionalWait );
193   mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
194 }
195
196 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
197 {
198   ConditionalWait::ScopedLock lock( mConditionalWait );
199   mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
200 }
201
202 void VectorRasterizeThread::SetLoopCount( int32_t count )
203 {
204   if( mLoopCount != count )
205   {
206     ConditionalWait::ScopedLock lock( mConditionalWait );
207
208     mLoopCount = count;
209
210     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetLoopCount: [%d]\n", count );
211   }
212 }
213
214 int32_t VectorRasterizeThread::GetLoopCount() const
215 {
216   return mLoopCount;
217 }
218
219 void VectorRasterizeThread::SetPlayRange( Vector2 range )
220 {
221   // Make sure the range specified is between 0.0 and 1.0
222   if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
223   {
224     Vector2 orderedRange( range );
225     // If the range is not in order swap values
226     if( range.x > range.y )
227     {
228       orderedRange = Vector2( range.y, range.x );
229     }
230
231     if( mPlayRange != orderedRange )
232     {
233       ConditionalWait::ScopedLock lock( mConditionalWait );
234
235       mPlayRange = orderedRange;
236
237       mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
238       mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
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::SetPlayRange: [%d, %d]\n", mStartFrame, mEndFrame );
257     }
258   }
259 }
260
261 Vector2 VectorRasterizeThread::GetPlayRange() const
262 {
263   return mPlayRange;
264 }
265
266 void VectorRasterizeThread::SetCurrentProgress( float progress )
267 {
268   ConditionalWait::ScopedLock lock( mConditionalWait );
269
270   if( progress >= mPlayRange.x && progress <= mPlayRange.y )
271   {
272     mProgress = progress;
273
274     mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
275     mCurrentFrameUpdated = true;
276
277     mResourceReady = false;
278
279     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
280   }
281 }
282
283 float VectorRasterizeThread::GetCurrentProgress() const
284 {
285   return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
286 }
287
288 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
289 {
290   mVectorRenderer.GetDefaultSize( width, height );
291 }
292
293 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
294 {
295   return mPlayState;
296 }
297
298 bool VectorRasterizeThread::IsResourceReady() const
299 {
300   return mResourceReady;
301 }
302
303 void VectorRasterizeThread::Initialize()
304 {
305   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
306
307   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
308
309   mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
310   mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
311
312   mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
313
314   mFrameRate = mVectorRenderer.GetFrameRate();
315   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
316
317   uint32_t width, height;
318   mVectorRenderer.GetDefaultSize( width, height );
319
320   SetSize( width, height );
321
322   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
323 }
324
325 void VectorRasterizeThread::Rasterize()
326 {
327   bool resourceReady;
328   uint32_t currentFrame, startFrame, endFrame;
329   int32_t loopCount;
330   DevelImageVisual::PlayState playState;
331
332   {
333     ConditionalWait::ScopedLock lock( mConditionalWait );
334
335     if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
336     {
337       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
338
339       if( mPlayState == DevelImageVisual::PlayState::STOPPED )
340       {
341         // Reset the current loop
342         mCurrentLoop = 0;
343       }
344       mConditionalWait.Wait( lock );
345     }
346
347     resourceReady = mResourceReady;
348     currentFrame = mCurrentFrame++;
349     startFrame = mStartFrame;
350     endFrame = mEndFrame;
351     loopCount = mLoopCount;
352     playState = mPlayState;
353
354     mNeedRender = false;
355     mResourceReady = true;
356     mCurrentFrameUpdated = false;
357   }
358
359   auto currentFrameStartTime = std::chrono::system_clock::now();
360
361   // Rasterize
362   mVectorRenderer.Render( currentFrame );
363
364   if( playState == DevelImageVisual::PlayState::PLAYING )
365   {
366     if( currentFrame >= endFrame )
367     {
368       if( loopCount < 0 )
369       {
370         // repeat forever
371         ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );  // If the current frame is changed in the event thread, don't overwrite it.
372       }
373       else
374       {
375         mCurrentLoop++;
376         if( mCurrentLoop >= loopCount )
377         {
378           mPlayState = DevelImageVisual::PlayState::STOPPED;
379
380           // Animation is finished
381           mAnimationFinishedTrigger->Trigger();
382
383           DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
384         }
385         else
386         {
387           ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
388         }
389       }
390     }
391   }
392
393   if( !resourceReady )
394   {
395     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
396
397     mResourceReadyTrigger->Trigger();
398   }
399
400   auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
401
402 #if defined(DEBUG_ENABLED)
403   auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
404
405   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", currentFrame, sleepDuration.count() );
406 #endif
407
408   std::this_thread::sleep_until( timeToSleepUntil );
409 }
410
411 } // namespace Internal
412
413 } // namespace Toolkit
414
415 } // namespace Dali