2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "svg-rasterize-thread.h"
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>
28 #include <dali-toolkit/third-party/nanosvg/nanosvgrast.h>
29 #endif /* NO_THORVG */
30 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
43 const char * const UNITS("px");
47 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height)
48 : mSvgVisual( svgRenderer ),
49 mParsedSvg( parsedSvg ),
55 mRasterizer = nsvgCreateRasterizer();
58 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height, bool loaded)
59 : mSvgVisual( svgRenderer ),
60 mVectorRenderer( vectorRenderer ),
69 #endif /* NO_THORVG */
71 RasterizingTask::~RasterizingTask()
74 nsvgDeleteRasterizer( mRasterizer );
75 #endif /* NO_THORVG */
78 void RasterizingTask::Load()
81 if( mParsedSvg != NULL)
86 if( !mUrl.IsLocalResource() )
88 Dali::Vector<uint8_t> remoteBuffer;
90 if( !Dali::FileLoader::DownloadFileSynchronously( mUrl.GetUrl(), remoteBuffer ))
92 DALI_LOG_ERROR("Failed to download file!\n");
96 remoteBuffer.PushBack( '\0' );
97 mParsedSvg = nsvgParse( reinterpret_cast<char*>(remoteBuffer.begin()), UNITS, mDpi );
100 if( !mLoaded && !mUrl.IsLocalResource() )
102 Dali::Vector<uint8_t> remoteBuffer;
104 if( !Dali::FileLoader::DownloadFileSynchronously( mUrl.GetUrl(), remoteBuffer ))
106 DALI_LOG_ERROR("Failed to download file!\n");
110 remoteBuffer.PushBack( '\0' );
111 char *data = reinterpret_cast<char*>(remoteBuffer.begin());
112 if ( !mVectorRenderer.Load( data, remoteBuffer.Size()))
114 DALI_LOG_ERROR( "Failed to load data!\n" );
120 #endif /* NO_THORVG */
123 void RasterizingTask::Rasterize( )
126 if( mParsedSvg != NULL && mWidth > 0u && mHeight > 0u )
128 float scaleX = static_cast<float>( mWidth ) / mParsedSvg->width;
129 float scaleY = static_cast<float>( mHeight ) / mParsedSvg->height;
130 float scale = scaleX < scaleY ? scaleX : scaleY;
131 unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
132 unsigned int bufferSize = bufferStride * mHeight;
134 unsigned char* buffer = new unsigned char [bufferSize];
135 nsvgRasterize(mRasterizer, mParsedSvg, 0.f,0.f,scale,
136 buffer, mWidth, mHeight,
139 mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
141 #else /* NO_THORVG */
142 if ( mWidth <= 0u || mHeight <= 0u )
144 DALI_LOG_ERROR( "Size is zero!\n" );
148 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New( mWidth, mHeight, Dali::Pixel::RGBA8888 );
149 mVectorRenderer.SetBuffer( pixelBuffer );
151 uint32_t defaultWidth, defaultHeight;
152 mVectorRenderer.GetDefaultSize( defaultWidth, defaultHeight );
154 float scaleX = static_cast<float>( mWidth ) / static_cast<float>( defaultWidth );
155 float scaleY = static_cast<float>( mHeight ) / static_cast<float>( defaultHeight );
156 float scale = scaleX < scaleY ? scaleX : scaleY;
158 if ( !mVectorRenderer.Render( scale ) )
160 DALI_LOG_ERROR( "SVG Render Fail!\n" );
164 mPixelData = Devel::PixelBuffer::Convert( pixelBuffer );
167 DALI_LOG_ERROR( "Pixel Data is null\n" );
170 #endif /* NO_THORVG */
174 NSVGimage* RasterizingTask::GetParsedImage() const
178 #else /* NO_THORVG */
179 VectorImageRenderer RasterizingTask::GetVectorRenderer() const
181 return mVectorRenderer;
184 bool RasterizingTask::IsLoaded() const
188 #endif /* NO_THORVG */
190 SvgVisual* RasterizingTask::GetSvgVisual() const
192 return mSvgVisual.Get();
195 PixelData RasterizingTask::GetPixelData() const
200 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
201 : mTrigger( trigger ),
202 mIsThreadWaiting( false )
206 SvgRasterizeThread::~SvgRasterizeThread()
211 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
215 // add an empty task would stop the thread from conditional wait.
216 thread->AddTask( RasterizingTaskPtr() );
225 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
227 bool wasEmpty = false;
230 // Lock while adding task to the queue
231 ConditionalWait::ScopedLock lock( mConditionalWait );
232 wasEmpty = mRasterizeTasks.empty();
233 if( !wasEmpty && task != NULL)
235 // Remove the tasks with the same renderer.
236 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
237 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
239 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
241 mRasterizeTasks.erase( it );
246 mRasterizeTasks.push_back( task );
251 // wake up the image loading thread
252 mConditionalWait.Notify();
256 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
258 // Lock while popping task out from the queue
259 Mutex::ScopedLock lock( mMutex );
261 if( mCompletedTasks.empty() )
263 return RasterizingTaskPtr();
266 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
267 RasterizingTaskPtr nextTask = *next;
268 mCompletedTasks.erase( next );
273 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
275 // Lock while remove task from the queue
276 ConditionalWait::ScopedLock lock( mConditionalWait );
277 if( !mRasterizeTasks.empty() )
279 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
281 if( (*it) && (*it)->GetSvgVisual() == visual )
283 mRasterizeTasks.erase( it );
291 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
293 // Lock while adding image to the delete queue
294 ConditionalWait::ScopedLock lock( mConditionalWait );
296 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
298 nsvgDelete( parsedSvg );
300 else // wait to delete until current rasterization completed.
302 mDeleteSvg.PushBack( parsedSvg );
305 #else /* NO_THORVG */
306 void SvgRasterizeThread::DeleteImage( VectorImageRenderer vectorRenderer )
308 // Lock while adding image to the delete queue
309 ConditionalWait::ScopedLock lock( mConditionalWait );
311 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
315 else // wait to delete until current rasterization completed.
317 mDeleteSvg.PushBack( &vectorRenderer );
320 #endif /* NO_THORVG */
322 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
324 // Lock while popping task out from the queue
325 ConditionalWait::ScopedLock lock( mConditionalWait );
327 // Delete the image here to make sure that it is not used in the nsvgRasterize()
328 if( !mDeleteSvg.Empty() )
331 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
337 #endif /* NO_THORVG */
342 while( mRasterizeTasks.empty() )
344 mIsThreadWaiting = true;
345 mConditionalWait.Wait( lock );
347 mIsThreadWaiting = false;
349 // pop out the next task from the queue
350 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
351 RasterizingTaskPtr nextTask = *next;
352 mRasterizeTasks.erase( next );
357 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
359 // Lock while adding task to the queue
360 Mutex::ScopedLock lock( mMutex );
361 mCompletedTasks.push_back( task );
363 // wake up the main thread
367 void SvgRasterizeThread::Run()
369 SetThreadName( "SVGThread" );
370 while( RasterizingTaskPtr task = NextTaskToProcess() )
374 AddCompletedTask( task );
378 } // namespace Internal
380 } // namespace Toolkit