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>
41 const char * const UNITS("px");
44 RasterizingTask::RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height)
45 : mSvgVisual( svgRenderer ),
46 mParsedSvg( parsedSvg ),
52 mRasterizer = nsvgCreateRasterizer();
55 RasterizingTask::~RasterizingTask()
57 nsvgDeleteRasterizer( mRasterizer );
60 void RasterizingTask::Load()
62 if( mParsedSvg != NULL)
67 if( !mUrl.IsLocalResource() )
69 Dali::Vector<uint8_t> remoteBuffer;
71 if( !Dali::FileLoader::DownloadFileSynchronously( mUrl.GetUrl(), remoteBuffer ))
73 DALI_LOG_ERROR("Failed to download file!\n");
77 remoteBuffer.PushBack( '\0' );
78 mParsedSvg = nsvgParse( reinterpret_cast<char*>(remoteBuffer.begin()), UNITS, mDpi );
82 void RasterizingTask::Rasterize( )
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(mRasterizer, 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 )
122 SvgRasterizeThread::~SvgRasterizeThread()
127 void SvgRasterizeThread::TerminateThread( SvgRasterizeThread*& thread )
131 // add an empty task would stop the thread from conditional wait.
132 thread->AddTask( RasterizingTaskPtr() );
141 void SvgRasterizeThread::AddTask( RasterizingTaskPtr task )
143 bool wasEmpty = false;
146 // Lock while adding task to the queue
147 ConditionalWait::ScopedLock lock( mConditionalWait );
148 wasEmpty = mRasterizeTasks.empty();
149 if( !wasEmpty && task != NULL)
151 // Remove the tasks with the same renderer.
152 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
153 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
155 if( (*it) && (*it)->GetSvgVisual() == task->GetSvgVisual() )
157 mRasterizeTasks.erase( it );
162 mRasterizeTasks.push_back( task );
167 // wake up the image loading thread
168 mConditionalWait.Notify();
172 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
174 // Lock while popping task out from the queue
175 Mutex::ScopedLock lock( mMutex );
177 if( mCompletedTasks.empty() )
179 return RasterizingTaskPtr();
182 std::vector< RasterizingTaskPtr >::iterator next = mCompletedTasks.begin();
183 RasterizingTaskPtr nextTask = *next;
184 mCompletedTasks.erase( next );
189 void SvgRasterizeThread::RemoveTask( SvgVisual* visual )
191 // Lock while remove task from the queue
192 ConditionalWait::ScopedLock lock( mConditionalWait );
193 if( !mRasterizeTasks.empty() )
195 for( std::vector< RasterizingTaskPtr >::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it )
197 if( (*it) && (*it)->GetSvgVisual() == visual )
199 mRasterizeTasks.erase( it );
206 void SvgRasterizeThread::DeleteImage( NSVGimage* parsedSvg )
208 // Lock while adding image to the delete queue
209 ConditionalWait::ScopedLock lock( mConditionalWait );
211 if( mIsThreadWaiting ) // no rasterization is ongoing, save to delete
213 nsvgDelete( parsedSvg );
215 else // wait to delete until current rasterization completed.
217 mDeleteSvg.PushBack( parsedSvg );
221 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
223 // Lock while popping task out from the queue
224 ConditionalWait::ScopedLock lock( mConditionalWait );
226 // Delete the image here to make sure that it is not used in the nsvgRasterize()
227 if( !mDeleteSvg.Empty() )
229 for( Vector< NSVGimage* >::Iterator it = mDeleteSvg.Begin(), endIt = mDeleteSvg.End();
239 while( mRasterizeTasks.empty() )
241 mIsThreadWaiting = true;
242 mConditionalWait.Wait( lock );
244 mIsThreadWaiting = false;
246 // pop out the next task from the queue
247 std::vector< RasterizingTaskPtr >::iterator next = mRasterizeTasks.begin();
248 RasterizingTaskPtr nextTask = *next;
249 mRasterizeTasks.erase( next );
254 void SvgRasterizeThread::AddCompletedTask( RasterizingTaskPtr task )
256 // Lock while adding task to the queue
257 Mutex::ScopedLock lock( mMutex );
258 mCompletedTasks.push_back( task );
260 // wake up the main thread
264 void SvgRasterizeThread::Run()
266 SetThreadName( "SVGThread" );
267 while( RasterizingTaskPtr task = NextTaskToProcess() )
271 AddCompletedTask( task );
275 } // namespace Internal
277 } // namespace Toolkit