bug fixed about the rtl markup behavior.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-image-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-image-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
26 // INTERNAL INCLUDES
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
42 #endif
43
44 } // unnamed namespace
45
46 VectorRasterizeThread::VectorRasterizeThread( const std::string& url, Renderer renderer, uint32_t width, uint32_t height )
47 : mUrl( url ),
48   mVectorRenderer(),
49   mResourceReadyTrigger( NULL ),
50   mCurrentFrame( 0 ),
51   mTotalFrame( 0 ),
52   mWidth( width ),
53   mHeight( height ),
54   mNeedRender( false ),
55   mPlaying( false ),
56   mPaused( false ),
57   mDestroyThread( false ),
58   mResourceReady( false ),
59   mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
60 {
61   mVectorRenderer = VectorAnimationRenderer::New( mUrl, renderer, width, height );
62 }
63
64 VectorRasterizeThread::~VectorRasterizeThread()
65 {
66   // Stop the thread
67   {
68     ConditionalWait::ScopedLock lock( mConditionalWait );
69     mDestroyThread = true;
70     mConditionalWait.Notify( lock );
71
72     // This should be called in the main thread to stop waiting for the dequeuable buffer.
73     mVectorRenderer.StopRender();
74   }
75
76   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join\n" );
77
78   Join();
79
80   delete mResourceReadyTrigger;
81 }
82
83 void VectorRasterizeThread::Run()
84 {
85   SetThreadName( "VectorImageThread" );
86   mLogFactory.InstallLogFunction();
87
88   //TODO: check the return value
89   StartRender();
90
91   while( IsThreadReady() )
92   {
93     Rasterize();
94   }
95 }
96
97 void VectorRasterizeThread::StartAnimation()
98 {
99   ConditionalWait::ScopedLock lock( mConditionalWait );
100   if( !mPlaying )
101   {
102     mPlaying = true;
103     mPaused = false;
104     mConditionalWait.Notify( lock );
105
106     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartAnimation: Start\n" );
107   }
108 }
109
110 void VectorRasterizeThread::StopAnimation()
111 {
112   ConditionalWait::ScopedLock lock( mConditionalWait );
113   if( mPlaying )
114   {
115     mPlaying = false;
116     mPaused = false;
117
118     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StopAnimation: Stop\n" );
119   }
120 }
121
122 void VectorRasterizeThread::PauseAnimation()
123 {
124   ConditionalWait::ScopedLock lock( mConditionalWait );
125   if( mPlaying && !mPaused )
126   {
127     mPaused = true;
128
129     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::PauseAnimation: Pause\n" );
130   }
131 }
132
133 void VectorRasterizeThread::ResumeAnimation()
134 {
135   ConditionalWait::ScopedLock lock( mConditionalWait );
136   if( mPlaying && mPaused )
137   {
138     mPaused = false;
139     mConditionalWait.Notify( lock );
140
141     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::ResumeAnimation: Resume\n" );
142   }
143 }
144
145 void VectorRasterizeThread::RenderFrame()
146 {
147   ConditionalWait::ScopedLock lock( mConditionalWait );
148   mNeedRender = true;
149   mConditionalWait.Notify( lock );
150
151   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::RenderFrame: Render\n" );
152 }
153
154 void VectorRasterizeThread::SetResourceReadyCallback( EventThreadCallback* callback )
155 {
156   mResourceReadyTrigger = callback;
157 }
158
159 bool VectorRasterizeThread::IsThreadReady()
160 {
161   ConditionalWait::ScopedLock lock( mConditionalWait );
162
163   if( ( !mPlaying || mPaused ) && !mNeedRender && !mDestroyThread )
164   {
165     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::IsThreadReady: Wait\n" );
166
167     if( !mPlaying )
168     {
169       mCurrentFrame = 0;
170     }
171
172     mConditionalWait.Wait( lock );
173   }
174
175   // Keep the thread alive if this thread is NOT to be destroyed
176   return !mDestroyThread;
177 }
178
179 bool VectorRasterizeThread::StartRender()
180 {
181   //TODO: check the return value
182   mVectorRenderer.StartRender();
183
184   mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
185
186   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::StartRender: Renderer is started [%d]\n", mTotalFrame );
187
188   return true;
189 }
190
191 void VectorRasterizeThread::Rasterize()
192 {
193   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: [%d]\n", mCurrentFrame );
194
195   // Rasterize
196   mVectorRenderer.Render( mCurrentFrame );
197
198   if( mPlaying && !mPaused )
199   {
200     mCurrentFrame++;
201
202     if( mCurrentFrame >= mTotalFrame )
203     {
204       mCurrentFrame = 0;
205     }
206   }
207
208   mNeedRender = false;
209
210   if( !mResourceReady )
211   {
212     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::Rasterize: Resource ready trigger\n" );
213
214     mResourceReadyTrigger->Trigger();
215     mResourceReady = true;
216   }
217 }
218
219 } // namespace Internal
220
221 } // namespace Toolkit
222
223 } // namespace Dali