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>
27 #include <dali-toolkit/third-party/nanosvg/nanosvgrast.h>
28 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
41 const char * const UNITS("px");
44 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height)
45 : mSvgVisual( svgRenderer ),
46 mParsedSvg( parsedSvg ),
54 void RasterizingTask::Load()
56 if( mParsedSvg != NULL)
61 if( !mUrl.IsLocalResource() )
63 Dali::Vector<uint8_t> remoteBuffer;
65 if( !Dali::FileLoader::DownloadFileSynchronously( mUrl.GetUrl(), remoteBuffer ))
67 DALI_LOG_ERROR("Failed to download file!\n");
71 remoteBuffer.PushBack( '\0' );
72 mParsedSvg = nsvgParse( reinterpret_cast<char*>(remoteBuffer.begin()), UNITS, mDpi );
76 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
78 if( mParsedSvg != NULL && mWidth > 0u && mHeight > 0u )
80 float scaleX = static_cast<float>( mWidth ) / mParsedSvg->width;
81 float scaleY = static_cast<float>( mHeight ) / mParsedSvg->height;
82 float scale = scaleX < scaleY ? scaleX : scaleY;
83 unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
84 unsigned int bufferSize = bufferStride * mHeight;
86 unsigned char* buffer = new unsigned char [bufferSize];
87 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
88 buffer, mWidth, mHeight,
91 mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
95 NSVGimage* RasterizingTask::GetParsedImage() const
100 SvgVisual* RasterizingTask::GetSvgVisual() const
102 return mSvgVisual.Get();
105 PixelData RasterizingTask::GetPixelData() const
110 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
111 : mTrigger( trigger ),
112 mIsThreadWaiting( false )
114 mRasterizer = nsvgCreateRasterizer();
117 SvgRasterizeThread::~SvgRasterizeThread()
120 nsvgDeleteRasterizer( mRasterizer );
124 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
128 // add an empty task would stop the thread from conditional wait.
129 thread->AddTask( RasterizingTaskPtr() );
138 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
140 bool wasEmpty = false;
143 // Lock while adding task to the queue
144 ConditionalWait::ScopedLock lock( mConditionalWait );
145 wasEmpty = mRasterizeTasks.empty();
146 if( !wasEmpty && task != NULL)
148 // Remove the tasks with the same renderer.
149 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
150 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
152 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
154 mRasterizeTasks.erase( it );
159 mRasterizeTasks.push_back( task );
164 // wake up the image loading thread
165 mConditionalWait.Notify();
169 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
171 // Lock while popping task out from the queue
172 Mutex::ScopedLock lock( mMutex );
174 if( mCompletedTasks.empty() )
176 return RasterizingTaskPtr();
179 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
180 RasterizingTaskPtr nextTask = *next;
181 mCompletedTasks.erase( next );
186 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
188 // Lock while remove task from the queue
189 ConditionalWait::ScopedLock lock( mConditionalWait );
190 if( !mRasterizeTasks.empty() )
192 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
194 if( (*it) && (*it)->GetSvgVisual() == visual )
196 mRasterizeTasks.erase( it );
203 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
205 // Lock while adding image to the delete queue
206 ConditionalWait::ScopedLock lock( mConditionalWait );
208 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
210 nsvgDelete( parsedSvg );
212 else // wait to delete until current rasterization completed.
214 mDeleteSvg.PushBack( parsedSvg );
218 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
220 // Lock while popping task out from the queue
221 ConditionalWait::ScopedLock lock( mConditionalWait );
223 // Delete the image here to make sure that it is not used in the nsvgRasterize()
224 if( !mDeleteSvg.Empty() )
226 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
236 while( mRasterizeTasks.empty() )
238 mIsThreadWaiting = true;
239 mConditionalWait.Wait( lock );
241 mIsThreadWaiting = false;
243 // pop out the next task from the queue
244 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
245 RasterizingTaskPtr nextTask = *next;
246 mRasterizeTasks.erase( next );
251 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
253 // Lock while adding task to the queue
254 Mutex::ScopedLock lock( mMutex );
255 mCompletedTasks.push_back( task );
257 // wake up the main thread
261 void SvgRasterizeThread::Run()
263 SetThreadName( "SVGThread" );
264 while( RasterizingTaskPtr task = NextTaskToProcess() )
267 task->Rasterize( mRasterizer );
268 AddCompletedTask( task );
272 } // namespace Internal
274 } // namespace Toolkit