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"
27 const std::string TEXTURE_UNIFORM_NAME = "sTexture";
39 RasterizingTask::RasterizingTask( SvgRenderer* svgRenderer, NSVGimage* parsedSvg, unsigned int width, unsigned int height )
40 : mSvgRenderer( svgRenderer ),
41 mParsedSvg( parsedSvg ),
47 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
49 if( mWidth > 0u && mHeight > 0u )
51 float scaleX = static_cast<float>( mWidth ) / mParsedSvg->width;
52 float scaleY = static_cast<float>( mHeight ) / mParsedSvg->height;
53 float scale = scaleX < scaleY ? scaleX : scaleY;
54 unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
56 unsigned char* buffer = new unsigned char [bufferStride*mHeight];
57 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
58 buffer, mWidth, mHeight,
61 mPixelData = Dali::PixelData::New( buffer, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
65 SvgRenderer* RasterizingTask::GetSvgRenderer() const
67 return mSvgRenderer.Get();
70 PixelDataPtr RasterizingTask::GetPixelData() const
75 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
76 : mTrigger( trigger ),
77 mIsThreadWaiting( false )
79 mRasterizer = nsvgCreateRasterizer();
82 SvgRasterizeThread::~SvgRasterizeThread()
85 nsvgDeleteRasterizer( mRasterizer );
89 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
93 // add an empty task would stop the thread from conditional wait.
94 thread->AddTask( RasterizingTaskPtr() );
103 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
105 bool wasEmpty = false;
108 // Lock while adding task to the queue
109 ConditionalWait::ScopedLock lock( mConditionalWait );
110 wasEmpty = mRasterizeTasks.empty();
111 if( !wasEmpty && task != NULL)
113 // Remove the tasks with the same material.
114 // Older task which waiting to rasterize and apply the svg to the same material is expired.
115 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
117 if( (*it) && (*it)->GetSvgRenderer() == task->GetSvgRenderer() )
119 mRasterizeTasks.erase( it );
124 mRasterizeTasks.push_back( task );
129 // wake up the image loading thread
130 mConditionalWait.Notify();
134 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
136 // Lock while popping task out from the queue
137 Mutex::ScopedLock lock( mMutex );
139 if( mCompletedTasks.empty() )
141 return RasterizingTaskPtr();
144 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
145 RasterizingTaskPtr nextTask = *next;
146 mCompletedTasks.erase( next );
151 void SvgRasterizeThread::RemoveTask( SvgRenderer* renderer )
153 // Lock while remove task from the queue
154 ConditionalWait::ScopedLock lock( mConditionalWait );
155 if( !mRasterizeTasks.empty() )
157 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
159 if( (*it) && (*it)->GetSvgRenderer() == renderer )
161 mRasterizeTasks.erase( it );
168 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
170 // Lock while adding image to the delete queue
171 ConditionalWait::ScopedLock lock( mConditionalWait );
173 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
175 nsvgDelete( parsedSvg );
177 else // wait to delete until current rasterization completed.
179 mDeleteSvg.PushBack( parsedSvg );
183 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
185 // Lock while popping task out from the queue
186 ConditionalWait::ScopedLock lock( mConditionalWait );
188 // Delete the image here to make sure that it is not used in the nsvgRasterize()
189 if( !mDeleteSvg.Empty() )
191 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
201 while( mRasterizeTasks.empty() )
203 mIsThreadWaiting = true;
204 mConditionalWait.Wait( lock );
206 mIsThreadWaiting = false;
208 // pop out the next task from the queue
209 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
210 RasterizingTaskPtr nextTask = *next;
211 mRasterizeTasks.erase( next );
216 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
218 // Lock while adding task to the queue
219 Mutex::ScopedLock lock( mMutex );
220 mCompletedTasks.push_back( task );
222 // wake up the main thread
226 void SvgRasterizeThread::Run()
228 while( RasterizingTaskPtr task = NextTaskToProcess() )
230 task->Rasterize( mRasterizer );
231 AddCompletedTask( task );
235 } // namespace Internal
237 } // namespace Toolkit