[4.0] (AnimatedVectorImageVisual) Use the content default size
[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
112 void VectorRasterizeThread::SetRenderer( Renderer renderer )
113 {
114   ConditionalWait::ScopedLock lock( mConditionalWait );
115
116   mVectorRenderer.SetRenderer( renderer );
117
118   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetRenderer\n" );
119 }
120
121 void VectorRasterizeThread::SetSize( uint32_t width, uint32_t height )
122 {
123   if( mWidth != width || mHeight != height )
124   {
125     ConditionalWait::ScopedLock lock( mConditionalWait );
126     mVectorRenderer.SetSize( width, height );
127
128     mWidth = width;
129     mHeight = height;
130
131     mResourceReady = false;
132
133     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetSize: width = %d, height = %d\n", width, height );
134   }
135 }
136
137 void VectorRasterizeThread::PlayAnimation()
138 {
139   ConditionalWait::ScopedLock lock( mConditionalWait );
140   if( mPlayState != DevelImageVisual::PlayState::PLAYING )
141   {
142     mPlayState = DevelImageVisual::PlayState::PLAYING;
143     mConditionalWait.Notify( lock );
144
145     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PlayAnimation: Start\n" );
146   }
147 }
148
149 void VectorRasterizeThread::StopAnimation()
150 {
151   ConditionalWait::ScopedLock lock( mConditionalWait );
152   if( mPlayState != DevelImageVisual::PlayState::STOPPED )
153   {
154     mPlayState = DevelImageVisual::PlayState::STOPPED;
155
156     mCurrentFrame = mStartFrame;
157     mCurrentFrameUpdated = true;
158
159     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
160   }
161 }
162
163 void VectorRasterizeThread::PauseAnimation()
164 {
165   ConditionalWait::ScopedLock lock( mConditionalWait );
166   if( mPlayState == DevelImageVisual::PlayState::PLAYING )
167   {
168     mPlayState = DevelImageVisual::PlayState::PAUSED;
169
170     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
171   }
172 }
173
174 void VectorRasterizeThread::RenderFrame()
175 {
176   ConditionalWait::ScopedLock lock( mConditionalWait );
177
178   if( !mResourceReady )
179   {
180     mNeedRender = true;
181     mConditionalWait.Notify( lock );
182
183     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
184   }
185 }
186
187 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
188 {
189   ConditionalWait::ScopedLock lock( mConditionalWait );
190   mResourceReadyTrigger = std::unique_ptr< EventThreadCallback >( callback );
191 }
192
193 void VectorRasterizeThread::SetAnimationFinishedCallback( EventThreadCallback* callback )
194 {
195   ConditionalWait::ScopedLock lock( mConditionalWait );
196   mAnimationFinishedTrigger = std::unique_ptr< EventThreadCallback >( callback );
197 }
198
199 void VectorRasterizeThread::SetLoopCount( int32_t count )
200 {
201   if( mLoopCount != count )
202   {
203     ConditionalWait::ScopedLock lock( mConditionalWait );
204
205     mLoopCount = count;
206   }
207 }
208
209 int32_t VectorRasterizeThread::GetLoopCount() const
210 {
211   return mLoopCount;
212 }
213
214 void VectorRasterizeThread::SetPlayRange( Vector2 range )
215 {
216   // Make sure the range specified is between 0.0 and 1.0
217   if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
218   {
219     Vector2 orderedRange( range );
220     // If the range is not in order swap values
221     if( range.x > range.y )
222     {
223       orderedRange = Vector2( range.y, range.x );
224     }
225
226     if( mPlayRange != orderedRange )
227     {
228       ConditionalWait::ScopedLock lock( mConditionalWait );
229
230       mPlayRange = orderedRange;
231
232       mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
233       mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
234
235       // If the current frame is out of the range, change the current frame also.
236       if( mStartFrame > mCurrentFrame )
237       {
238         mCurrentFrame = mStartFrame;
239
240         mCurrentFrameUpdated = true;
241         mResourceReady = false;
242       }
243       else if( mEndFrame < mCurrentFrame )
244       {
245         mCurrentFrame = mEndFrame;
246
247         mCurrentFrameUpdated = true;
248         mResourceReady = false;
249       }
250     }
251   }
252 }
253
254 Vector2 VectorRasterizeThread::GetPlayRange() const
255 {
256   return mPlayRange;
257 }
258
259 void VectorRasterizeThread::SetCurrentProgress( float progress )
260 {
261   ConditionalWait::ScopedLock lock( mConditionalWait );
262
263   if( progress >= mPlayRange.x && progress <= mPlayRange.y )
264   {
265     mProgress = progress;
266
267     mCurrentFrame = static_cast< uint32_t >( mTotalFrame * progress + 0.5f );
268     mCurrentFrameUpdated = true;
269
270     mResourceReady = false;
271
272     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::SetCurrentProgress: progress = %f (%d)\n", progress, mCurrentFrame );
273   }
274 }
275
276 float VectorRasterizeThread::GetCurrentProgress() const
277 {
278   return ( static_cast< float >( mCurrentFrame ) / static_cast< float >( mTotalFrame ) );
279 }
280
281 void VectorRasterizeThread::GetDefaultSize( uint32_t& width, uint32_t& height ) const
282 {
283   mVectorRenderer.GetDefaultSize( width, height );
284 }
285
286 DevelImageVisual::PlayState VectorRasterizeThread::GetPlayState() const
287 {
288   return mPlayState;
289 }
290
291 bool VectorRasterizeThread::IsResourceReady() const
292 {
293   return mResourceReady;
294 }
295
296 void VectorRasterizeThread::Initialize()
297 {
298   mVectorRenderer = VectorAnimationRenderer::New( mUrl );
299
300   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
301
302   mStartFrame = static_cast< uint32_t >( mPlayRange.x * mTotalFrame + 0.5f );
303   mEndFrame = static_cast< uint32_t >( mPlayRange.y * mTotalFrame + 0.5f );
304
305   mCurrentFrame = std::max( static_cast< uint32_t >( mTotalFrame * mProgress + 0.5f ), mStartFrame );
306
307   mFrameRate = mVectorRenderer.GetFrameRate();
308   mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
309
310   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Initialize: file = %s [%d frames, %f fps]\n", mUrl.c_str(), mTotalFrame, mFrameRate );
311 }
312
313 void VectorRasterizeThread::Rasterize()
314 {
315   bool resourceReady;
316   uint32_t currentFrame, startFrame, endFrame;
317   int32_t loopCount;
318   DevelImageVisual::PlayState playState;
319
320   {
321     ConditionalWait::ScopedLock lock( mConditionalWait );
322
323     if( mPlayState != DevelImageVisual::PlayState::PLAYING && !mNeedRender && !mDestroyThread )
324     {
325       DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Wait\n" );
326
327       if( mPlayState == DevelImageVisual::PlayState::STOPPED )
328       {
329         // Reset the current loop
330         mCurrentLoop = 0;
331       }
332       mConditionalWait.Wait( lock );
333     }
334
335     resourceReady = mResourceReady;
336     currentFrame = mCurrentFrame++;
337     startFrame = mStartFrame;
338     endFrame = mEndFrame;
339     loopCount = mLoopCount;
340     playState = mPlayState;
341
342     mNeedRender = false;
343     mResourceReady = true;
344     mCurrentFrameUpdated = false;
345   }
346
347   auto currentFrameStartTime = std::chrono::system_clock::now();
348
349   // Rasterize
350   mVectorRenderer.Render( currentFrame );
351
352   if( playState == DevelImageVisual::PlayState::PLAYING )
353   {
354     if( currentFrame >= endFrame )
355     {
356       if( loopCount < 0 )
357       {
358         // repeat forever
359         ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );  // If the current frame is changed in the event thread, don't overwrite it.
360       }
361       else
362       {
363         mCurrentLoop++;
364         if( mCurrentLoop >= loopCount )
365         {
366           mPlayState = DevelImageVisual::PlayState::STOPPED;
367
368           // Animation is finished
369           mAnimationFinishedTrigger->Trigger();
370
371           DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Animation is finished\n" );
372         }
373         else
374         {
375           ResetToStart( mCurrentFrameUpdated, mCurrentFrame, startFrame, mConditionalWait );
376         }
377       }
378     }
379   }
380
381   if( !resourceReady )
382   {
383     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
384
385     mResourceReadyTrigger->Trigger();
386   }
387
388   auto timeToSleepUntil = currentFrameStartTime + std::chrono::nanoseconds( mFrameDurationNanoSeconds );
389
390 #if defined(DEBUG_ENABLED)
391   auto sleepDuration = std::chrono::duration_cast< std::chrono::milliseconds >( timeToSleepUntil - std::chrono::system_clock::now() );
392
393   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [current = %d, sleep duration = %lld]\n", mCurrentFrame, sleepDuration.count() );
394 #endif
395
396   std::this_thread::sleep_until( timeToSleepUntil );
397 }
398
399 } // namespace Internal
400
401 } // namespace Toolkit
402
403 } // namespace Dali