Trigger svg loading early
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-rasterize-thread.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "svg-rasterize-thread.h"
20
21 // EXTERNAL INCLUDES
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>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Internal
35 {
36 SvgTask::SvgTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer)
37 : mSvgVisual(svgVisual),
38   mVectorRenderer(vectorRenderer),
39   mHasSucceeded(false)
40 {
41 }
42
43 SvgVisual* SvgTask::GetSvgVisual() const
44 {
45   return mSvgVisual.Get();
46 }
47
48 PixelData SvgTask::GetPixelData() const
49 {
50   return PixelData();
51 }
52
53 bool SvgTask::HasSucceeded() const
54 {
55   return mHasSucceeded;
56 }
57
58 SvgLoadingTask::SvgLoadingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi)
59 : SvgTask(svgVisual, vectorRenderer),
60   mUrl(url),
61   mDpi(dpi)
62 {
63 }
64
65 SvgLoadingTask::~SvgLoadingTask()
66 {
67 }
68
69 void SvgLoadingTask::Process()
70 {
71   if(mVectorRenderer.IsLoaded())
72   {
73     // Already loaded
74     mHasSucceeded = true;
75     return;
76   }
77
78   Dali::Vector<uint8_t> buffer;
79
80   if(!mUrl.IsLocalResource())
81   {
82     if(!Dali::FileLoader::DownloadFileSynchronously(mUrl.GetUrl(), buffer))
83     {
84       DALI_LOG_ERROR("Failed to download file! [%s]\n", mUrl.GetUrl().c_str());
85       return;
86     }
87   }
88   else
89   {
90     if(!Dali::FileLoader::ReadFile(mUrl.GetUrl(), buffer))
91     {
92       DALI_LOG_ERROR("Failed to read file! [%s]\n", mUrl.GetUrl().c_str());
93       return;
94     }
95   }
96
97   buffer.PushBack('\0');
98
99   if(!mVectorRenderer.Load(buffer, mDpi))
100   {
101     DALI_LOG_ERROR("Failed to load data! [%s]\n", mUrl.GetUrl().c_str());
102     return;
103   }
104
105   mHasSucceeded = true;
106 }
107
108 SvgRasterizingTask::SvgRasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, unsigned int width, unsigned int height)
109 : SvgTask(svgVisual, vectorRenderer),
110   mWidth(width),
111   mHeight(height)
112 {
113 }
114
115 SvgRasterizingTask::~SvgRasterizingTask()
116 {
117 }
118
119 void SvgRasterizingTask::Process()
120 {
121   if(!mVectorRenderer.IsLoaded())
122   {
123     DALI_LOG_ERROR("File is not loaded!\n");
124     return;
125   }
126
127   Devel::PixelBuffer pixelBuffer = mVectorRenderer.Rasterize(mWidth, mHeight);
128   if(!pixelBuffer)
129   {
130     DALI_LOG_ERROR("Rasterize is failed!\n");
131     return;
132   }
133
134   mPixelData    = Devel::PixelBuffer::Convert(pixelBuffer);
135   mHasSucceeded = true;
136 }
137
138 PixelData SvgRasterizingTask::GetPixelData() const
139 {
140   return mPixelData;
141 }
142
143 SvgRasterizeThread::SvgRasterizeThread()
144 : mTrigger(new EventThreadCallback(MakeCallback(this, &SvgRasterizeThread::ApplyRasterizedSVGToSampler))),
145   mLogFactory(Dali::Adaptor::Get().GetLogFactory()),
146   mIsThreadWaiting(false),
147   mProcessorRegistered(false)
148 {
149 }
150
151 SvgRasterizeThread::~SvgRasterizeThread()
152 {
153   if(mProcessorRegistered)
154   {
155     Adaptor::Get().UnregisterProcessor(*this);
156   }
157 }
158
159 void SvgRasterizeThread::TerminateThread(SvgRasterizeThread*& thread)
160 {
161   if(thread)
162   {
163     // add an empty task would stop the thread from conditional wait.
164     thread->AddTask(SvgTaskPtr());
165     // stop the thread
166     thread->Join();
167     // delete the thread
168     delete thread;
169     thread = NULL;
170   }
171 }
172
173 void SvgRasterizeThread::AddTask(SvgTaskPtr task)
174 {
175   bool wasEmpty = false;
176
177   {
178     // Lock while adding task to the queue
179     ConditionalWait::ScopedLock lock(mConditionalWait);
180     wasEmpty = mRasterizeTasks.empty();
181     if(!wasEmpty && task)
182     {
183       // Remove the tasks with the same renderer.
184       // Older task which waiting to rasterize and apply the svg to the same renderer is expired.
185       // Rasterizing task only, loading task is not duplicated.
186       for(std::vector<SvgTaskPtr>::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it)
187       {
188         if((*it) && (*it)->GetSvgVisual() == task->GetSvgVisual())
189         {
190           SvgRasterizingTask* oldTask = dynamic_cast<SvgRasterizingTask*>(it->Get());
191           SvgRasterizingTask* newTask = dynamic_cast<SvgRasterizingTask*>(task.Get());
192           if(oldTask && newTask)
193           {
194             mRasterizeTasks.erase(it);
195             break;
196           }
197         }
198       }
199     }
200     mRasterizeTasks.push_back(task);
201
202     if(!mProcessorRegistered)
203     {
204       Adaptor::Get().RegisterProcessor(*this);
205       mProcessorRegistered = true;
206     }
207   }
208
209   if(wasEmpty)
210   {
211     // wake up the image loading thread
212     mConditionalWait.Notify();
213   }
214 }
215
216 SvgTaskPtr SvgRasterizeThread::NextCompletedTask()
217 {
218   // Lock while popping task out from the queue
219   Mutex::ScopedLock lock(mMutex);
220
221   if(mCompletedTasks.empty())
222   {
223     return SvgTaskPtr();
224   }
225
226   std::vector<SvgTaskPtr>::iterator next     = mCompletedTasks.begin();
227   SvgTaskPtr                        nextTask = *next;
228   mCompletedTasks.erase(next);
229
230   return nextTask;
231 }
232
233 void SvgRasterizeThread::RemoveTask(SvgVisual* visual)
234 {
235   // Lock while remove task from the queue
236   ConditionalWait::ScopedLock lock(mConditionalWait);
237   if(!mRasterizeTasks.empty())
238   {
239     for(std::vector<SvgTaskPtr>::iterator it = mRasterizeTasks.begin(), endIt = mRasterizeTasks.end(); it != endIt; ++it)
240     {
241       if((*it) && (*it)->GetSvgVisual() == visual)
242       {
243         mRasterizeTasks.erase(it);
244       }
245     }
246   }
247
248   UnregisterProcessor();
249 }
250
251 SvgTaskPtr SvgRasterizeThread::NextTaskToProcess()
252 {
253   // Lock while popping task out from the queue
254   ConditionalWait::ScopedLock lock(mConditionalWait);
255
256   // conditional wait
257   while(mRasterizeTasks.empty())
258   {
259     mIsThreadWaiting = true;
260     mConditionalWait.Wait(lock);
261   }
262   mIsThreadWaiting = false;
263
264   // pop out the next task from the queue
265   std::vector<SvgTaskPtr>::iterator next     = mRasterizeTasks.begin();
266   SvgTaskPtr                        nextTask = *next;
267   mRasterizeTasks.erase(next);
268
269   return nextTask;
270 }
271
272 void SvgRasterizeThread::AddCompletedTask(SvgTaskPtr task)
273 {
274   // Lock while adding task to the queue
275   Mutex::ScopedLock lock(mMutex);
276   mCompletedTasks.push_back(task);
277
278   // wake up the main thread
279   mTrigger->Trigger();
280 }
281
282 void SvgRasterizeThread::Run()
283 {
284   SetThreadName("SVGThread");
285   mLogFactory.InstallLogFunction();
286
287   while(SvgTaskPtr task = NextTaskToProcess())
288   {
289     task->Process();
290     AddCompletedTask(task);
291   }
292 }
293
294 void SvgRasterizeThread::ApplyRasterizedSVGToSampler()
295 {
296   while(SvgTaskPtr task = NextCompletedTask())
297   {
298     task->GetSvgVisual()->ApplyRasterizedImage(task->GetPixelData(), task->HasSucceeded());
299   }
300
301   UnregisterProcessor();
302 }
303
304 void SvgRasterizeThread::Process(bool postProcessor)
305 {
306   ApplyRasterizedSVGToSampler();
307 }
308
309 void SvgRasterizeThread::UnregisterProcessor()
310 {
311   if(mProcessorRegistered)
312   {
313     if(mRasterizeTasks.empty() && mCompletedTasks.empty())
314     {
315       Adaptor::Get().UnregisterProcessor(*this);
316       mProcessorRegistered = false;
317     }
318   }
319 }
320
321 } // namespace Internal
322
323 } // namespace Toolkit
324
325 } // namespace Dali