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 "async-image-loader-impl.h"
22 #include <dali/integration-api/adaptor-framework/adaptor.h>
23 #include <dali/public-api/adaptor-framework/async-task-manager.h>
31 AsyncImageLoader::AsyncImageLoader()
37 AsyncImageLoader::~AsyncImageLoader()
42 IntrusivePtr<AsyncImageLoader> AsyncImageLoader::New()
44 IntrusivePtr<AsyncImageLoader> internal = new AsyncImageLoader();
48 uint32_t AsyncImageLoader::LoadAnimatedImage(Dali::AnimatedImageLoading animatedImageLoading,
50 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
52 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, animatedImageLoading, frameIndex, preMultiplyOnLoad, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
53 Dali::AsyncTaskManager::Get().AddTask(loadingTask);
57 uint32_t AsyncImageLoader::LoadAnimatedImage(Dali::AnimatedImageLoading animatedImageLoading,
59 Dali::ImageDimensions desiredSize,
60 Dali::FittingMode::Type fittingMode,
61 Dali::SamplingMode::Type samplingMode,
62 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
64 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, animatedImageLoading, frameIndex, desiredSize, fittingMode, samplingMode, preMultiplyOnLoad, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
65 Dali::AsyncTaskManager::Get().AddTask(loadingTask);
69 uint32_t AsyncImageLoader::Load(const VisualUrl& url,
70 ImageDimensions dimensions,
71 FittingMode::Type fittingMode,
72 SamplingMode::Type samplingMode,
73 bool orientationCorrection,
74 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
77 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad, loadPlanes, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
78 AsyncTaskManager::Get().AddTask(loadingTask);
79 mLoadingTasks.push_back(AsyncImageLoadingInfo(loadingTask, mLoadTaskId));
83 uint32_t AsyncImageLoader::LoadEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer,
84 ImageDimensions dimensions,
85 FittingMode::Type fittingMode,
86 SamplingMode::Type samplingMode,
87 bool orientationCorrection,
88 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
90 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, encodedImageBuffer, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
91 Dali::AsyncTaskManager::Get().AddTask(loadingTask);
92 mLoadingTasks.push_back(AsyncImageLoadingInfo(loadingTask, mLoadTaskId));
96 uint32_t AsyncImageLoader::ApplyMask(Devel::PixelBuffer pixelBuffer,
97 Devel::PixelBuffer maskPixelBuffer,
100 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
102 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
103 Dali::AsyncTaskManager::Get().AddTask(loadingTask);
104 mLoadingTasks.push_back(AsyncImageLoadingInfo(loadingTask, mLoadTaskId));
108 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
110 return mLoadedSignal;
113 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
115 return mPixelBufferLoadedSignal;
118 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
120 // Remove already completed tasks
121 RemoveCompletedTask();
123 auto end = mLoadingTasks.end();
124 for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
126 if((*iter).loadId == loadingTaskId)
128 Dali::AsyncTaskManager::Get().RemoveTask((*iter).loadingTask);
129 mLoadingTasks.erase(iter);
137 void AsyncImageLoader::CancelAll()
139 // Remove already completed tasks
140 RemoveCompletedTask();
142 auto end = mLoadingTasks.end();
143 for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
145 if((*iter).loadingTask && Dali::AsyncTaskManager::Get())
147 Dali::AsyncTaskManager::Get().RemoveTask(((*iter).loadingTask));
150 mLoadingTasks.clear();
153 void AsyncImageLoader::ProcessLoadedImage(LoadingTaskPtr task)
155 if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
157 mPixelBufferLoadedSignal.Emit(task->id, task->pixelBuffers);
159 else if(mLoadedSignal.GetConnectionCount() > 0)
162 if(!task->pixelBuffers.empty())
164 pixelData = Devel::PixelBuffer::Convert(task->pixelBuffers[0]);
166 mLoadedSignal.Emit(task->id, pixelData);
169 mCompletedTaskIds.push_back(task->id);
172 void AsyncImageLoader::RemoveCompletedTask()
174 std::uint32_t loadingTaskId;
175 auto end = mLoadingTasks.end();
176 auto endCompletedIter = mCompletedTaskIds.end();
177 for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
179 loadingTaskId = (*iter).loadId;
180 for(auto iterCompleted = mCompletedTaskIds.begin(); iterCompleted != endCompletedIter; ++iterCompleted)
182 if((*iterCompleted) == loadingTaskId)
184 mLoadingTasks.erase(iter);
190 } // namespace Internal
192 } // namespace Toolkit