2 * Copyright (c) 2022 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 Devel::PixelBuffer pixelBuffer = mVectorRenderer.Rasterize(mWidth, mHeight);
83 DALI_LOG_ERROR("Rasterize is failed! [%s]\n", mUrl.GetUrl().c_str());
87 mPixelData = Devel::PixelBuffer::Convert(pixelBuffer);
90 VectorImageRenderer RasterizingTask::GetVectorRenderer() const
92 return mVectorRenderer;
95 bool RasterizingTask::IsLoaded() const
100 SvgVisual* RasterizingTask::GetSvgVisual() const
102 return mSvgVisual.Get();
105 PixelData RasterizingTask::GetPixelData() const
110 SvgRasterizeThread::SvgRasterizeThread()
111 : mTrigger(new EventThreadCallback(MakeCallback(this, &SvgRasterizeThread::ApplyRasterizedSVGToSampler))),
112 mLogFactory(Dali::Adaptor::Get().GetLogFactory()),
113 mIsThreadWaiting(false),
114 mProcessorRegistered(false)
118 SvgRasterizeThread::~SvgRasterizeThread()
120 if(mProcessorRegistered)
122 Adaptor::Get().UnregisterProcessor(*this);
126 void SvgRasterizeThread::TerminateThread(SvgRasterizeThread*& thread)
130 // add an empty task would stop the thread from conditional wait.
131 thread->AddTask(RasterizingTaskPtr());
140 void SvgRasterizeThread::AddTask(RasterizingTaskPtr task)
142 bool wasEmpty = false;
145 // Lock while adding task to the queue
146 ConditionalWait::ScopedLock lock(mConditionalWait);
147 wasEmpty = mRasterizeTasks.empty();
148 if(!wasEmpty && task)
150 // Remove the tasks with the same renderer.
151 // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
152 for(std::vector<RasterizingTaskPtr>::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it)
154 if((*it) && (*it)->GetSvgVisual() == task->GetSvgVisual())
156 mRasterizeTasks.erase(it);
161 mRasterizeTasks.push_back(task);
163 if(!mProcessorRegistered)
165 Adaptor::Get().RegisterProcessor(*this);
166 mProcessorRegistered = true;
172 // wake up the image loading thread
173 mConditionalWait.Notify();
177 RasterizingTaskPtr SvgRasterizeThread::NextCompletedTask()
179 // Lock while popping task out from the queue
180 Mutex::ScopedLock lock(mMutex);
182 if(mCompletedTasks.empty())
184 return RasterizingTaskPtr();
187 std::vector<RasterizingTaskPtr>::iterator next = mCompletedTasks.begin();
188 RasterizingTaskPtr nextTask = *next;
189 mCompletedTasks.erase(next);
194 void SvgRasterizeThread::RemoveTask(SvgVisual* visual)
196 // Lock while remove task from the queue
197 ConditionalWait::ScopedLock lock(mConditionalWait);
198 if(!mRasterizeTasks.empty())
200 for(std::vector<RasterizingTaskPtr>::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it)
202 if((*it) && (*it)->GetSvgVisual() == visual)
204 mRasterizeTasks.erase(it);
210 UnregisterProcessor();
213 RasterizingTaskPtr SvgRasterizeThread::NextTaskToProcess()
215 // Lock while popping task out from the queue
216 ConditionalWait::ScopedLock lock(mConditionalWait);
219 while(mRasterizeTasks.empty())
221 mIsThreadWaiting = true;
222 mConditionalWait.Wait(lock);
224 mIsThreadWaiting = false;
226 // pop out the next task from the queue
227 std::vector<RasterizingTaskPtr>::iterator next = mRasterizeTasks.begin();
228 RasterizingTaskPtr nextTask = *next;
229 mRasterizeTasks.erase(next);
234 void SvgRasterizeThread::AddCompletedTask(RasterizingTaskPtr task)
236 // Lock while adding task to the queue
237 Mutex::ScopedLock lock(mMutex);
238 mCompletedTasks.push_back(task);
240 // wake up the main thread
244 void SvgRasterizeThread::Run()
246 SetThreadName("SVGThread");
247 mLogFactory.InstallLogFunction();
249 while(RasterizingTaskPtr task = NextTaskToProcess())
253 AddCompletedTask(task);
257 void SvgRasterizeThread::ApplyRasterizedSVGToSampler()
259 while(RasterizingTaskPtr task = NextCompletedTask())
261 task->GetSvgVisual()->ApplyRasterizedImage(task->GetVectorRenderer(), task->GetPixelData(), task->IsLoaded());
264 UnregisterProcessor();
267 void SvgRasterizeThread::Process(bool postProcessor)
269 ApplyRasterizedSVGToSampler();
272 void SvgRasterizeThread::UnregisterProcessor()
274 if(mProcessorRegistered)
276 if(mRasterizeTasks.empty() && mCompletedTasks.empty())
278 Adaptor::Get().UnregisterProcessor(*this);
279 mProcessorRegistered = false;
284 } // namespace Internal
286 } // namespace Toolkit