[Tize] Change svg visual to load file using nanosvg
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-rasterize-thread.cpp
1 /*
2  * Copyright (c) 2016 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 "svg-rasterize-thread.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/thread-settings.h>
23 #include <dali/devel-api/adaptor-framework/file-loader.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/third-party/nanosvg/nanosvgrast.h>
28 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41 const char * const UNITS("px");
42 }
43
44 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height)
45 : mSvgVisual( svgRenderer ),
46   mParsedSvg( parsedSvg ),
47   mUrl( url ),
48   mDpi( dpi ),
49   mWidth( width ),
50   mHeight( height )
51 {
52 }
53
54 void RasterizingTask::Load()
55 {
56   if( mParsedSvg != NULL)
57   {
58     return;
59   }
60
61   if( !mUrl.IsLocalResource() )
62   {
63     Dali::Vector<uint8_t> remoteBuffer;
64
65     mParsedSvg = nsvgParseFromFile( mUrl.GetUrl().c_str(), UNITS, mDpi );
66   }
67 }
68
69 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
70 {
71   if( mParsedSvg != NULL && mWidth > 0u && mHeight > 0u )
72   {
73     float scaleX = static_cast<float>( mWidth ) /  mParsedSvg->width;
74     float scaleY = static_cast<float>( mHeight ) /  mParsedSvg->height;
75     float scale = scaleX < scaleY ? scaleX : scaleY;
76     unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
77     unsigned int bufferSize = bufferStride * mHeight;
78
79     unsigned char* buffer = new unsigned char [bufferSize];
80     nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
81         buffer, mWidth, mHeight,
82         bufferStride );
83
84     mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
85   }
86 }
87
88 NSVGimage* RasterizingTask::GetParsedImage() const
89 {
90   return mParsedSvg;
91 }
92
93 SvgVisual* RasterizingTask::GetSvgVisual() const
94 {
95   return mSvgVisual.Get();
96 }
97
98 PixelData RasterizingTask::GetPixelData() const
99 {
100   return mPixelData;
101 }
102
103 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
104 : mTrigger( trigger ),
105   mIsThreadWaiting( false )
106 {
107   mRasterizer = nsvgCreateRasterizer();
108 }
109
110 SvgRasterizeThread::~SvgRasterizeThread()
111 {
112
113   nsvgDeleteRasterizer( mRasterizer );
114   delete mTrigger;
115 }
116
117 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
118 {
119   if( thread )
120   {
121     // add an empty task would stop the thread from conditional wait.
122     thread->AddTask( RasterizingTaskPtr() );
123     // stop the thread
124     thread->Join();
125     // delete the thread
126     delete thread;
127     thread = NULL;
128   }
129 }
130
131 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
132 {
133   bool wasEmpty = false;
134
135   {
136     // Lock while adding task to the queue
137     ConditionalWait::ScopedLock lock( mConditionalWait );
138     wasEmpty = mRasterizeTasks.empty();
139     if( !wasEmpty && task != NULL)
140     {
141       // Remove the tasks with the same renderer.
142       // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
143       for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
144       {
145         if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
146         {
147           mRasterizeTasks.erase( it );
148           break;
149         }
150       }
151     }
152     mRasterizeTasks.push_back( task );
153   }
154
155   if( wasEmpty)
156   {
157     // wake up the image loading thread
158     mConditionalWait.Notify();
159   }
160 }
161
162 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
163 {
164   // Lock while popping task out from the queue
165   Mutex::ScopedLock lock( mMutex );
166
167   if( mCompletedTasks.empty() )
168   {
169     return RasterizingTaskPtr();
170   }
171
172   std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
173   RasterizingTaskPtr nextTask = *next;
174   mCompletedTasks.erase( next );
175
176   return nextTask;
177 }
178
179 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
180 {
181   // Lock while remove task from the queue
182   ConditionalWait::ScopedLock lock( mConditionalWait );
183   if( !mRasterizeTasks.empty() )
184   {
185     for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
186     {
187       if( (*it) &&  (*it)->GetSvgVisual() == visual )
188       {
189         mRasterizeTasks.erase( it );
190         break;
191       }
192     }
193   }
194 }
195
196 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
197 {
198   // Lock while adding image to the delete queue
199   ConditionalWait::ScopedLock lock( mConditionalWait );
200
201   if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
202   {
203     nsvgDelete( parsedSvg );
204   }
205   else // wait to delete until current rasterization completed.
206   {
207     mDeleteSvg.PushBack( parsedSvg );
208   }
209 }
210
211 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
212 {
213   // Lock while popping task out from the queue
214   ConditionalWait::ScopedLock lock( mConditionalWait );
215
216   // Delete the image here to make sure that it is not used in the nsvgRasterize()
217   if( !mDeleteSvg.Empty() )
218   {
219     for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
220         it != endIt;
221         ++it )
222     {
223       nsvgDelete( *it );
224     }
225     mDeleteSvg.Clear();
226   }
227
228   // conditional wait
229   while( mRasterizeTasks.empty() )
230   {
231     mIsThreadWaiting = true;
232     mConditionalWait.Wait( lock );
233   }
234   mIsThreadWaiting = false;
235
236   // pop out the next task from the queue
237   std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
238   RasterizingTaskPtr nextTask = *next;
239   mRasterizeTasks.erase( next );
240
241   return nextTask;
242 }
243
244 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
245 {
246   // Lock while adding task to the queue
247   Mutex::ScopedLock lock( mMutex );
248   mCompletedTasks.push_back( task );
249
250   // wake up the main thread
251   mTrigger->Trigger();
252 }
253
254 void SvgRasterizeThread::Run()
255 {
256   SetThreadName( "SVGThread" );
257   while( RasterizingTaskPtr task = NextTaskToProcess() )
258   {
259     task->Load( );
260     task->Rasterize( mRasterizer );
261     AddCompletedTask( task );
262   }
263 }
264
265 } // namespace Internal
266
267 } // namespace Toolkit
268
269 } // namespace Dali