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 mParsedSvg = nsvgParse( reinterpret_cast<char*>(remoteBuffer.begin()), UNITS, mDpi );
75 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
77 if( mParsedSvg != NULL && mWidth > 0u && mHeight > 0u )
79 float scaleX = static_cast<float>( mWidth ) / mParsedSvg->width;
80 float scaleY = static_cast<float>( mHeight ) / mParsedSvg->height;
81 float scale = scaleX < scaleY ? scaleX : scaleY;
82 unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
83 unsigned int bufferSize = bufferStride * mHeight;
85 unsigned char* buffer = new unsigned char [bufferSize];
86 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
87 buffer, mWidth, mHeight,
90 mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
94 NSVGimage* RasterizingTask::GetParsedImage() const
99 SvgVisual* RasterizingTask::GetSvgVisual() const
101 return mSvgVisual.Get();
104 PixelData RasterizingTask::GetPixelData() const
109 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
110 : mTrigger( trigger ),
111 mIsThreadWaiting( false )
113 mRasterizer = nsvgCreateRasterizer();
116 SvgRasterizeThread::~SvgRasterizeThread()
119 nsvgDeleteRasterizer( mRasterizer );
123 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
127 // add an empty task would stop the thread from conditional wait.
128 thread->AddTask( RasterizingTaskPtr() );
137 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
139 bool wasEmpty = false;
142 // Lock while adding task to the queue
143 ConditionalWait::ScopedLock lock( mConditionalWait );
144 wasEmpty = mRasterizeTasks.empty();
145 if( !wasEmpty && task != NULL)
147 // Remove the tasks with the same renderer.
148 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
149 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
151 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
153 mRasterizeTasks.erase( it );
158 mRasterizeTasks.push_back( task );
163 // wake up the image loading thread
164 mConditionalWait.Notify();
168 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
170 // Lock while popping task out from the queue
171 Mutex::ScopedLock lock( mMutex );
173 if( mCompletedTasks.empty() )
175 return RasterizingTaskPtr();
178 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
179 RasterizingTaskPtr nextTask = *next;
180 mCompletedTasks.erase( next );
185 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
187 // Lock while remove task from the queue
188 ConditionalWait::ScopedLock lock( mConditionalWait );
189 if( !mRasterizeTasks.empty() )
191 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
193 if( (*it) && (*it)->GetSvgVisual() == visual )
195 mRasterizeTasks.erase( it );
202 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
204 // Lock while adding image to the delete queue
205 ConditionalWait::ScopedLock lock( mConditionalWait );
207 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
209 nsvgDelete( parsedSvg );
211 else // wait to delete until current rasterization completed.
213 mDeleteSvg.PushBack( parsedSvg );
217 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
219 // Lock while popping task out from the queue
220 ConditionalWait::ScopedLock lock( mConditionalWait );
222 // Delete the image here to make sure that it is not used in the nsvgRasterize()
223 if( !mDeleteSvg.Empty() )
225 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
235 while( mRasterizeTasks.empty() )
237 mIsThreadWaiting = true;
238 mConditionalWait.Wait( lock );
240 mIsThreadWaiting = false;
242 // pop out the next task from the queue
243 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
244 RasterizingTaskPtr nextTask = *next;
245 mRasterizeTasks.erase( next );
250 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
252 // Lock while adding task to the queue
253 Mutex::ScopedLock lock( mMutex );
254 mCompletedTasks.push_back( task );
256 // wake up the main thread
260 void SvgRasterizeThread::Run()
262 SetThreadName( "SVGThread" );
263 while( RasterizingTaskPtr task = NextTaskToProcess() )
266 task->Rasterize( mRasterizer );
267 AddCompletedTask( task );
271 } // namespace Internal
273 } // namespace Toolkit