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>
38 const char * const UNITS("px");
40 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height)
41 : mSvgVisual( svgRenderer ),
42 mParsedSvg( parsedSvg ),
50 void RasterizingTask::Load()
52 if( mParsedSvg != NULL)
57 if( mUrl.IsLocalResource() )
59 Dali::Vector<char> buffer;
60 if ( !Dali::FileLoader::ReadFile( mUrl.GetUrl(), buffer ) )
62 DALI_LOG_ERROR("Failed to read file!\n");
66 mParsedSvg = nsvgParse( buffer.begin(), UNITS, mDpi );
70 Dali::Vector<uint8_t> remoteBuffer;
72 if( !Dali::FileLoader::DownloadFileSynchronously( mUrl.GetUrl(), remoteBuffer ))
74 DALI_LOG_ERROR("Failed to download file!\n");
78 mParsedSvg = nsvgParse( reinterpret_cast<char*>(remoteBuffer.begin()), UNITS, mDpi );
82 void RasterizingTask::Rasterize( NSVGrasterizer* rasterizer )
84 if( mParsedSvg != NULL && mWidth > 0u && mHeight > 0u )
86 float scaleX = static_cast<float>( mWidth ) / mParsedSvg->width;
87 float scaleY = static_cast<float>( mHeight ) / mParsedSvg->height;
88 float scale = scaleX < scaleY ? scaleX : scaleY;
89 unsigned int bufferStride = mWidth*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
90 unsigned int bufferSize = bufferStride * mHeight;
92 unsigned char* buffer = new unsigned char [bufferSize];
93 nsvgRasterize(rasterizer, mParsedSvg, 0.f,0.f,scale,
94 buffer, mWidth, mHeight,
97 mPixelData = Dali::PixelData::New( buffer, bufferSize, mWidth, mHeight, Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY );
101 NSVGimage* RasterizingTask::GetParsedImage() const
106 SvgVisual* RasterizingTask::GetSvgVisual() const
108 return mSvgVisual.Get();
111 PixelData RasterizingTask::GetPixelData() const
116 SvgRasterizeThread::SvgRasterizeThread( EventThreadCallback* trigger )
117 : mTrigger( trigger ),
118 mIsThreadWaiting( false )
120 mRasterizer = nsvgCreateRasterizer();
123 SvgRasterizeThread::~SvgRasterizeThread()
126 nsvgDeleteRasterizer( mRasterizer );
130 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
134 // add an empty task would stop the thread from conditional wait.
135 thread->AddTask( RasterizingTaskPtr() );
144 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
146 bool wasEmpty = false;
149 // Lock while adding task to the queue
150 ConditionalWait::ScopedLock lock( mConditionalWait );
151 wasEmpty = mRasterizeTasks.empty();
152 if( !wasEmpty && task != NULL)
154 // Remove the tasks with the same renderer.
155 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
156 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
158 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
160 mRasterizeTasks.erase( it );
165 mRasterizeTasks.push_back( task );
170 // wake up the image loading thread
171 mConditionalWait.Notify();
175 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
177 // Lock while popping task out from the queue
178 Mutex::ScopedLock lock( mMutex );
180 if( mCompletedTasks.empty() )
182 return RasterizingTaskPtr();
185 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
186 RasterizingTaskPtr nextTask = *next;
187 mCompletedTasks.erase( next );
192 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
194 // Lock while remove task from the queue
195 ConditionalWait::ScopedLock lock( mConditionalWait );
196 if( !mRasterizeTasks.empty() )
198 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
200 if( (*it) && (*it)->GetSvgVisual() == visual )
202 mRasterizeTasks.erase( it );
209 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
211 // Lock while adding image to the delete queue
212 ConditionalWait::ScopedLock lock( mConditionalWait );
214 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
216 nsvgDelete( parsedSvg );
218 else // wait to delete until current rasterization completed.
220 mDeleteSvg.PushBack( parsedSvg );
224 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
226 // Lock while popping task out from the queue
227 ConditionalWait::ScopedLock lock( mConditionalWait );
229 // Delete the image here to make sure that it is not used in the nsvgRasterize()
230 if( !mDeleteSvg.Empty() )
232 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
242 while( mRasterizeTasks.empty() )
244 mIsThreadWaiting = true;
245 mConditionalWait.Wait( lock );
247 mIsThreadWaiting = false;
249 // pop out the next task from the queue
250 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
251 RasterizingTaskPtr nextTask = *next;
252 mRasterizeTasks.erase( next );
257 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
259 // Lock while adding task to the queue
260 Mutex::ScopedLock lock( mMutex );
261 mCompletedTasks.push_back( task );
263 // wake up the main thread
267 void SvgRasterizeThread::Run()
269 SetThreadName( "SVGThread" );
270 while( RasterizingTaskPtr task = NextTaskToProcess() )
273 task->Rasterize( mRasterizer );
274 AddCompletedTask( task );
278 } // namespace Internal
280 } // namespace Toolkit