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 mParsedSvg = nsvgParseFromFile( mUrl.GetUrl().c_str(), UNITS, mDpi );
69 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
71 if( mParsedSvg != NULL && mWidth > 0u && mHeight > 0u )
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;
79 unsigned char* buffer = new unsigned char [bufferSize];
80 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
81 buffer, mWidth, mHeight,
84 mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
88 NSVGimage* RasterizingTask::GetParsedImage() const
93 SvgVisual* RasterizingTask::GetSvgVisual() const
95 return mSvgVisual.Get();
98 PixelData RasterizingTask::GetPixelData() const
103 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
104 : mTrigger( trigger ),
105 mIsThreadWaiting( false )
107 mRasterizer = nsvgCreateRasterizer();
110 SvgRasterizeThread::~SvgRasterizeThread()
113 nsvgDeleteRasterizer( mRasterizer );
117 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
121 // add an empty task would stop the thread from conditional wait.
122 thread->AddTask( RasterizingTaskPtr() );
131 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
133 bool wasEmpty = false;
136 // Lock while adding task to the queue
137 ConditionalWait::ScopedLock lock( mConditionalWait );
138 wasEmpty = mRasterizeTasks.empty();
139 if( !wasEmpty && task != NULL)
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 )
145 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
147 mRasterizeTasks.erase( it );
152 mRasterizeTasks.push_back( task );
157 // wake up the image loading thread
158 mConditionalWait.Notify();
162 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
164 // Lock while popping task out from the queue
165 Mutex::ScopedLock lock( mMutex );
167 if( mCompletedTasks.empty() )
169 return RasterizingTaskPtr();
172 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
173 RasterizingTaskPtr nextTask = *next;
174 mCompletedTasks.erase( next );
179 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
181 // Lock while remove task from the queue
182 ConditionalWait::ScopedLock lock( mConditionalWait );
183 if( !mRasterizeTasks.empty() )
185 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
187 if( (*it) && (*it)->GetSvgVisual() == visual )
189 mRasterizeTasks.erase( it );
196 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
198 // Lock while adding image to the delete queue
199 ConditionalWait::ScopedLock lock( mConditionalWait );
201 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
203 nsvgDelete( parsedSvg );
205 else // wait to delete until current rasterization completed.
207 mDeleteSvg.PushBack( parsedSvg );
211 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
213 // Lock while popping task out from the queue
214 ConditionalWait::ScopedLock lock( mConditionalWait );
216 // Delete the image here to make sure that it is not used in the nsvgRasterize()
217 if( !mDeleteSvg.Empty() )
219 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
229 while( mRasterizeTasks.empty() )
231 mIsThreadWaiting = true;
232 mConditionalWait.Wait( lock );
234 mIsThreadWaiting = false;
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 );
244 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
246 // Lock while adding task to the queue
247 Mutex::ScopedLock lock( mMutex );
248 mCompletedTasks.push_back( task );
250 // wake up the main thread
254 void SvgRasterizeThread::Run()
256 SetThreadName( "SVGThread" );
257 while( RasterizingTaskPtr task = NextTaskToProcess() )
260 task->Rasterize( mRasterizer );
261 AddCompletedTask( task );
265 } // namespace Internal
267 } // namespace Toolkit