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-toolkit/third-party/nanosvg/nanosvgrast.h>
23 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
34 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, unsigned int width, unsigned int height )
35 : mSvgVisual( 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 );
50 unsigned int bufferSize = bufferStride * mHeight;
52 unsigned char* buffer = new unsigned char [bufferSize];
53 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
54 buffer, mWidth, mHeight,
57 mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
61 SvgVisual* RasterizingTask::GetSvgVisual() const
63 return mSvgVisual.Get();
66 PixelData RasterizingTask::GetPixelData() const
71 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
72 : mTrigger( trigger ),
73 mIsThreadWaiting( false )
75 mRasterizer = nsvgCreateRasterizer();
78 SvgRasterizeThread::~SvgRasterizeThread()
81 nsvgDeleteRasterizer( mRasterizer );
85 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
89 // add an empty task would stop the thread from conditional wait.
90 thread->AddTask( RasterizingTaskPtr() );
99 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
101 bool wasEmpty = false;
104 // Lock while adding task to the queue
105 ConditionalWait::ScopedLock lock( mConditionalWait );
106 wasEmpty = mRasterizeTasks.empty();
107 if( !wasEmpty && task != NULL)
109 // Remove the tasks with the same renderer.
110 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
111 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
113 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
115 mRasterizeTasks.erase( it );
120 mRasterizeTasks.push_back( task );
125 // wake up the image loading thread
126 mConditionalWait.Notify();
130 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
132 // Lock while popping task out from the queue
133 Mutex::ScopedLock lock( mMutex );
135 if( mCompletedTasks.empty() )
137 return RasterizingTaskPtr();
140 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
141 RasterizingTaskPtr nextTask = *next;
142 mCompletedTasks.erase( next );
147 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
149 // Lock while remove task from the queue
150 ConditionalWait::ScopedLock lock( mConditionalWait );
151 if( !mRasterizeTasks.empty() )
153 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
155 if( (*it) && (*it)->GetSvgVisual() == visual )
157 mRasterizeTasks.erase( it );
164 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
166 // Lock while adding image to the delete queue
167 ConditionalWait::ScopedLock lock( mConditionalWait );
169 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
171 nsvgDelete( parsedSvg );
173 else // wait to delete until current rasterization completed.
175 mDeleteSvg.PushBack( parsedSvg );
179 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
181 // Lock while popping task out from the queue
182 ConditionalWait::ScopedLock lock( mConditionalWait );
184 // Delete the image here to make sure that it is not used in the nsvgRasterize()
185 if( !mDeleteSvg.Empty() )
187 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
197 while( mRasterizeTasks.empty() )
199 mIsThreadWaiting = true;
200 mConditionalWait.Wait( lock );
202 mIsThreadWaiting = false;
204 // pop out the next task from the queue
205 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
206 RasterizingTaskPtr nextTask = *next;
207 mRasterizeTasks.erase( next );
212 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
214 // Lock while adding task to the queue
215 Mutex::ScopedLock lock( mMutex );
216 mCompletedTasks.push_back( task );
218 // wake up the main thread
222 void SvgRasterizeThread::Run()
224 while( RasterizingTaskPtr task = NextTaskToProcess() )
226 task->Rasterize( mRasterizer );
227 AddCompletedTask( task );
231 } // namespace Internal
233 } // namespace Toolkit