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 "image-load-thread.h"
22 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/devel-api/adaptor-framework/thread-settings.h>
25 #include <dali/integration-api/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/debug.h>
34 LoadingTask::LoadingTask(uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex)
42 orientationCorrection(),
43 preMultiplyOnLoad(DevelAsyncImageLoader::PreMultiplyOnLoad::OFF),
48 animatedImageLoading(animatedImageLoading),
49 frameIndex(frameIndex)
53 LoadingTask::LoadingTask(uint32_t id, const VisualUrl& url, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
58 dimensions(dimensions),
59 fittingMode(fittingMode),
60 samplingMode(samplingMode),
61 orientationCorrection(orientationCorrection),
62 preMultiplyOnLoad(preMultiplyOnLoad),
67 animatedImageLoading(),
72 LoadingTask::LoadingTask(uint32_t id, const EncodedImageBuffer& encodedImageBuffer, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
75 encodedImageBuffer(encodedImageBuffer),
77 dimensions(dimensions),
78 fittingMode(fittingMode),
79 samplingMode(samplingMode),
80 orientationCorrection(orientationCorrection),
81 preMultiplyOnLoad(preMultiplyOnLoad),
86 animatedImageLoading(),
91 LoadingTask::LoadingTask(uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
92 : pixelBuffer(pixelBuffer),
99 orientationCorrection(),
100 preMultiplyOnLoad(preMultiplyOnLoad),
102 maskPixelBuffer(maskPixelBuffer),
103 contentScale(contentScale),
104 cropToMask(cropToMask),
105 animatedImageLoading(),
110 void LoadingTask::Load()
112 if(animatedImageLoading)
114 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
116 else if(encodedImageBuffer)
118 pixelBuffer = Dali::LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), dimensions, fittingMode, samplingMode, orientationCorrection);
120 else if(url.IsValid() && url.IsLocalResource())
122 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
124 else if(url.IsValid())
126 pixelBuffer = Dali::DownloadImageSynchronously(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
131 DALI_LOG_ERROR("LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str());
135 void LoadingTask::ApplyMask()
137 pixelBuffer.ApplyMask(maskPixelBuffer, contentScale, cropToMask);
140 void LoadingTask::MultiplyAlpha()
142 if(pixelBuffer && Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
144 if(preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON)
146 pixelBuffer.MultiplyColorByAlpha();
151 ImageLoadThread::ImageLoadThread(EventThreadCallback* trigger)
153 mLogFactory(Dali::Adaptor::Get().GetLogFactory())
157 ImageLoadThread::~ImageLoadThread()
159 // add an empty task would stop the thread from conditional wait.
166 for(auto&& iter : mLoadQueue)
172 for(auto&& iter : mCompleteQueue)
176 mCompleteQueue.Clear();
179 void ImageLoadThread::Run()
181 SetThreadName("ImageLoadThread");
182 mLogFactory.InstallLogFunction();
184 while(LoadingTask* task = NextTaskToProcess())
186 if(!task->isMaskTask)
194 task->MultiplyAlpha();
196 AddCompletedTask(task);
200 void ImageLoadThread::AddTask(LoadingTask* task)
202 bool wasEmpty = false;
204 // Lock while adding task to the queue
205 ConditionalWait::ScopedLock lock(mConditionalWait);
206 wasEmpty = mLoadQueue.Empty();
207 mLoadQueue.PushBack(task);
212 // wake up the image loading thread
213 mConditionalWait.Notify();
217 LoadingTask* ImageLoadThread::NextCompletedTask()
219 // Lock while popping task out from the queue
220 Mutex::ScopedLock lock(mMutex);
222 if(mCompleteQueue.Empty())
227 Vector<LoadingTask*>::Iterator next = mCompleteQueue.Begin();
228 LoadingTask* nextTask = *next;
229 mCompleteQueue.Erase(next);
234 bool ImageLoadThread::CancelTask(uint32_t loadingTaskId)
236 // Lock while remove task from the queue
237 ConditionalWait::ScopedLock lock(mConditionalWait);
239 for(Vector<LoadingTask*>::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter)
241 if((*iter)->id == loadingTaskId)
244 mLoadQueue.Erase(iter);
252 void ImageLoadThread::CancelAll()
254 // Lock while remove task from the queue
255 ConditionalWait::ScopedLock lock(mConditionalWait);
257 for(Vector<LoadingTask*>::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter)
264 LoadingTask* ImageLoadThread::NextTaskToProcess()
266 // Lock while popping task out from the queue
267 ConditionalWait::ScopedLock lock(mConditionalWait);
269 while(mLoadQueue.Empty())
271 mConditionalWait.Wait(lock);
274 Vector<LoadingTask*>::Iterator next = mLoadQueue.Begin();
275 LoadingTask* nextTask = *next;
276 mLoadQueue.Erase(next);
281 void ImageLoadThread::AddCompletedTask(LoadingTask* task)
283 // Lock while adding task to the queue
284 Mutex::ScopedLock lock(mMutex);
285 mCompleteQueue.PushBack(task);
287 // wake up the main thread
291 } // namespace Internal
293 } // namespace Toolkit