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 "nanosvg/nanosvgrast.h"
23 #include "svg-renderer.h"
34 RasterizingTask::RasterizingTask( SvgRenderer* svgRenderer, NSVGimage* parsedSvg, unsigned int width, unsigned int height )
35 : mSvgRenderer( svgRenderer ),
36 mParsedSvg( parsedSvg ),
42 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
44 if( mWidth > 0u && mHeight > 0u )
46 float scaleX = static_cast<float>( mWidth ) / mParsedSvg->width;
47 float scaleY = static_cast<float>( mHeight ) / mParsedSvg->height;
48 float scale = scaleX < scaleY ? scaleX : scaleY;
49 unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
51 unsigned char* buffer = new unsigned char [bufferStride*mHeight];
52 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
53 buffer, mWidth, mHeight,
56 mPixelData = Dali::PixelData::New( buffer, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
60 SvgRenderer* RasterizingTask::GetSvgRenderer() const
62 return mSvgRenderer.Get();
65 PixelDataPtr RasterizingTask::GetPixelData() const
70 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
71 : mTrigger( trigger ),
72 mIsThreadWaiting( false )
74 mRasterizer = nsvgCreateRasterizer();
77 SvgRasterizeThread::~SvgRasterizeThread()
80 nsvgDeleteRasterizer( mRasterizer );
84 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
88 // add an empty task would stop the thread from conditional wait.
89 thread->AddTask( RasterizingTaskPtr() );
98 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
100 bool wasEmpty = false;
103 // Lock while adding task to the queue
104 ConditionalWait::ScopedLock lock( mConditionalWait );
105 wasEmpty = mRasterizeTasks.empty();
106 if( !wasEmpty && task != NULL)
108 // Remove the tasks with the same renderer.
109 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
110 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
112 if( (*it) && (*it)->GetSvgRenderer() == task->GetSvgRenderer() )
114 mRasterizeTasks.erase( it );
119 mRasterizeTasks.push_back( task );
124 // wake up the image loading thread
125 mConditionalWait.Notify();
129 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
131 // Lock while popping task out from the queue
132 Mutex::ScopedLock lock( mMutex );
134 if( mCompletedTasks.empty() )
136 return RasterizingTaskPtr();
139 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
140 RasterizingTaskPtr nextTask = *next;
141 mCompletedTasks.erase( next );
146 void SvgRasterizeThread::RemoveTask( SvgRenderer* renderer )
148 // Lock while remove task from the queue
149 ConditionalWait::ScopedLock lock( mConditionalWait );
150 if( !mRasterizeTasks.empty() )
152 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
154 if( (*it) && (*it)->GetSvgRenderer() == renderer )
156 mRasterizeTasks.erase( it );
163 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
165 // Lock while adding image to the delete queue
166 ConditionalWait::ScopedLock lock( mConditionalWait );
168 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
170 nsvgDelete( parsedSvg );
172 else // wait to delete until current rasterization completed.
174 mDeleteSvg.PushBack( parsedSvg );
178 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
180 // Lock while popping task out from the queue
181 ConditionalWait::ScopedLock lock( mConditionalWait );
183 // Delete the image here to make sure that it is not used in the nsvgRasterize()
184 if( !mDeleteSvg.Empty() )
186 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
196 while( mRasterizeTasks.empty() )
198 mIsThreadWaiting = true;
199 mConditionalWait.Wait( lock );
201 mIsThreadWaiting = false;
203 // pop out the next task from the queue
204 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
205 RasterizingTaskPtr nextTask = *next;
206 mRasterizeTasks.erase( next );
211 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
213 // Lock while adding task to the queue
214 Mutex::ScopedLock lock( mMutex );
215 mCompletedTasks.push_back( task );
217 // wake up the main thread
221 void SvgRasterizeThread::Run()
223 while( RasterizingTaskPtr task = NextTaskToProcess() )
225 task->Rasterize( mRasterizer );
226 AddCompletedTask( task );
230 } // namespace Internal
232 } // namespace Toolkit