2 * Copyright (c) 2021 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/file-loader.h>
23 #include <dali/devel-api/adaptor-framework/thread-settings.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
36 RasterizingTask::RasterizingTask(SvgVisual* svgRenderer, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height)
37 : mSvgVisual(svgRenderer),
38 mVectorRenderer(vectorRenderer),
47 RasterizingTask::~RasterizingTask()
51 void RasterizingTask::Load()
53 if(!mUrl.IsLocalResource())
55 Dali::Vector<uint8_t> remoteBuffer;
56 if(!Dali::FileLoader::DownloadFileSynchronously(mUrl.GetUrl(), remoteBuffer))
58 DALI_LOG_ERROR("RasterizingTask::Load: Failed to download file! [%s]\n", mUrl.GetUrl().c_str());
62 remoteBuffer.PushBack('\0');
64 if(!mVectorRenderer.Load(remoteBuffer, mDpi))
66 DALI_LOG_ERROR("RasterizingTask::Load:Failed to load data! [%s]\n", mUrl.GetUrl().c_str());
78 void RasterizingTask::Rasterize()
80 if(mWidth <= 0u || mHeight <= 0u)
82 DALI_LOG_ERROR("RasterizingTask::Rasterize: Size is zero!\n");
86 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Dali::Pixel::RGBA8888);
88 uint32_t defaultWidth, defaultHeight;
89 mVectorRenderer.GetDefaultSize(defaultWidth, defaultHeight);
91 float scaleX = static_cast<float>(mWidth) / static_cast<float>(defaultWidth);
92 float scaleY = static_cast<float>(mHeight) / static_cast<float>(defaultHeight);
93 float scale = scaleX < scaleY ? scaleX : scaleY;
95 if(!mVectorRenderer.Rasterize(pixelBuffer, scale))
97 DALI_LOG_ERROR("RasterizingTask::Rasterize: Rasterize is failed! [%s]\n", mUrl.GetUrl().c_str());
101 mPixelData = Devel::PixelBuffer::Convert(pixelBuffer);
104 VectorImageRenderer RasterizingTask::GetVectorRenderer() const
106 return mVectorRenderer;
109 bool RasterizingTask::IsLoaded() const
114 SvgVisual* RasterizingTask::GetSvgVisual() const
116 return mSvgVisual.Get();
119 PixelData RasterizingTask::GetPixelData() const
124 SvgRasterizeThread::SvgRasterizeThread()
125 : mTrigger(new EventThreadCallback(MakeCallback(this, &SvgRasterizeThread::ApplyRasterizedSVGToSampler))),
126 mLogFactory(Dali::Adaptor::Get().GetLogFactory()),
127 mIsThreadWaiting(false),
128 mProcessorRegistered(false)
132 SvgRasterizeThread::~SvgRasterizeThread()
134 if(mProcessorRegistered)
136 Adaptor::Get().UnregisterProcessor(*this);
140 void SvgRasterizeThread::TerminateThread(SvgRasterizeThread*& thread)
144 // add an empty task would stop the thread from conditional wait.
145 thread->AddTask(RasterizingTaskPtr());
154 void SvgRasterizeThread::AddTask(RasterizingTaskPtr task)
156 bool wasEmpty = false;
159 // Lock while adding task to the queue
160 ConditionalWait::ScopedLock lock(mConditionalWait);
161 wasEmpty = mRasterizeTasks.empty();
162 if(!wasEmpty && task != NULL)
164 // Remove the tasks with the same renderer.
165 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
166 for(std::vector<RasterizingTaskPtr>::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it)
168 if((*it) && (*it)->GetSvgVisual() == task->GetSvgVisual())
170 mRasterizeTasks.erase(it);
175 mRasterizeTasks.push_back(task);
177 if(!mProcessorRegistered)
179 Adaptor::Get().RegisterProcessor(*this);
180 mProcessorRegistered = true;
186 // wake up the image loading thread
187 mConditionalWait.Notify();
191 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
193 // Lock while popping task out from the queue
194 Mutex::ScopedLock lock(mMutex);
196 if(mCompletedTasks.empty())
198 return RasterizingTaskPtr();
201 std::vector<RasterizingTaskPtr>::iterator next = mCompletedTasks.begin();
202 RasterizingTaskPtr nextTask = *next;
203 mCompletedTasks.erase(next);
208 void SvgRasterizeThread::RemoveTask(SvgVisual* visual)
210 // Lock while remove task from the queue
211 ConditionalWait::ScopedLock lock(mConditionalWait);
212 if(!mRasterizeTasks.empty())
214 for(std::vector<RasterizingTaskPtr>::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it)
216 if((*it) && (*it)->GetSvgVisual() == visual)
218 mRasterizeTasks.erase(it);
224 UnregisterProcessor();
227 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
229 // Lock while popping task out from the queue
230 ConditionalWait::ScopedLock lock(mConditionalWait);
233 while(mRasterizeTasks.empty())
235 mIsThreadWaiting = true;
236 mConditionalWait.Wait(lock);
238 mIsThreadWaiting = false;
240 // pop out the next task from the queue
241 std::vector<RasterizingTaskPtr>::iterator next = mRasterizeTasks.begin();
242 RasterizingTaskPtr nextTask = *next;
243 mRasterizeTasks.erase(next);
248 void SvgRasterizeThread::AddCompletedTask(RasterizingTaskPtr task)
250 // Lock while adding task to the queue
251 Mutex::ScopedLock lock(mMutex);
252 mCompletedTasks.push_back(task);
254 // wake up the main thread
258 void SvgRasterizeThread::Run()
260 SetThreadName("SVGThread");
261 mLogFactory.InstallLogFunction();
263 while(RasterizingTaskPtr task = NextTaskToProcess())
267 AddCompletedTask(task);
271 void SvgRasterizeThread::ApplyRasterizedSVGToSampler()
273 while(RasterizingTaskPtr task = NextCompletedTask())
275 task->GetSvgVisual()->ApplyRasterizedImage(task->GetVectorRenderer(), task->GetPixelData(), task->IsLoaded());
278 UnregisterProcessor();
281 void SvgRasterizeThread::Process()
283 ApplyRasterizedSVGToSampler();
286 void SvgRasterizeThread::UnregisterProcessor()
288 if(mProcessorRegistered)
290 if(mRasterizeTasks.empty() && mCompletedTasks.empty())
292 Adaptor::Get().UnregisterProcessor(*this);
293 mProcessorRegistered = false;
298 } // namespace Internal
300 } // namespace Toolkit