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>
24 #include <dali/devel-api/adaptor-framework/thread-settings.h>
35 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, unsigned int width, unsigned int height )
36 : mSvgVisual( svgRenderer ),
37 mParsedSvg( parsedSvg ),
43 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
45 if( mWidth > 0u && mHeight > 0u )
47 float scaleX = static_cast<float>( mWidth ) / mParsedSvg->width;
48 float scaleY = static_cast<float>( mHeight ) / mParsedSvg->height;
49 float scale = scaleX < scaleY ? scaleX : scaleY;
50 unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
51 unsigned int bufferSize = bufferStride * mHeight;
53 unsigned char* buffer = new unsigned char [bufferSize];
54 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
55 buffer, mWidth, mHeight,
58 mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
62 SvgVisual* RasterizingTask::GetSvgVisual() const
64 return mSvgVisual.Get();
67 PixelData RasterizingTask::GetPixelData() const
72 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
73 : mTrigger( trigger ),
74 mIsThreadWaiting( false )
76 mRasterizer = nsvgCreateRasterizer();
79 SvgRasterizeThread::~SvgRasterizeThread()
82 nsvgDeleteRasterizer( mRasterizer );
86 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
90 // add an empty task would stop the thread from conditional wait.
91 thread->AddTask( RasterizingTaskPtr() );
100 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
102 bool wasEmpty = false;
105 // Lock while adding task to the queue
106 ConditionalWait::ScopedLock lock( mConditionalWait );
107 wasEmpty = mRasterizeTasks.empty();
108 if( !wasEmpty && task != NULL)
110 // Remove the tasks with the same renderer.
111 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
112 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
114 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
116 mRasterizeTasks.erase( it );
121 mRasterizeTasks.push_back( task );
126 // wake up the image loading thread
127 mConditionalWait.Notify();
131 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
133 // Lock while popping task out from the queue
134 Mutex::ScopedLock lock( mMutex );
136 if( mCompletedTasks.empty() )
138 return RasterizingTaskPtr();
141 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
142 RasterizingTaskPtr nextTask = *next;
143 mCompletedTasks.erase( next );
148 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
150 // Lock while remove task from the queue
151 ConditionalWait::ScopedLock lock( mConditionalWait );
152 if( !mRasterizeTasks.empty() )
154 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
156 if( (*it) && (*it)->GetSvgVisual() == visual )
158 mRasterizeTasks.erase( it );
165 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
167 // Lock while adding image to the delete queue
168 ConditionalWait::ScopedLock lock( mConditionalWait );
170 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
172 nsvgDelete( parsedSvg );
174 else // wait to delete until current rasterization completed.
176 mDeleteSvg.PushBack( parsedSvg );
180 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
182 // Lock while popping task out from the queue
183 ConditionalWait::ScopedLock lock( mConditionalWait );
185 // Delete the image here to make sure that it is not used in the nsvgRasterize()
186 if( !mDeleteSvg.Empty() )
188 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
198 while( mRasterizeTasks.empty() )
200 mIsThreadWaiting = true;
201 mConditionalWait.Wait( lock );
203 mIsThreadWaiting = false;
205 // pop out the next task from the queue
206 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
207 RasterizingTaskPtr nextTask = *next;
208 mRasterizeTasks.erase( next );
213 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
215 // Lock while adding task to the queue
216 Mutex::ScopedLock lock( mMutex );
217 mCompletedTasks.push_back( task );
219 // wake up the main thread
223 void SvgRasterizeThread::Run()
225 SetThreadName( "SVGThread" );
226 while( RasterizingTaskPtr task = NextTaskToProcess() )
228 task->Rasterize( mRasterizer );
229 AddCompletedTask( task );
233 } // namespace Internal
235 } // namespace Toolkit