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/public-api/adaptor-framework/async-task-manager.h>
23 #include <dali/integration-api/adaptor-framework/adaptor.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::Load(const VisualUrl& url,
58 ImageDimensions dimensions,
59 FittingMode::Type fittingMode,
60 SamplingMode::Type samplingMode,
61 bool orientationCorrection,
62 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
65 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad, loadPlanes, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
66 AsyncTaskManager::Get().AddTask(loadingTask);
67 mLoadingTasks.push_back(AsyncImageLoadingInfo(loadingTask,mLoadTaskId));
71 uint32_t AsyncImageLoader::LoadEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer,
72 ImageDimensions dimensions,
73 FittingMode::Type fittingMode,
74 SamplingMode::Type samplingMode,
75 bool orientationCorrection,
76 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
78 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, encodedImageBuffer, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
79 Dali::AsyncTaskManager::Get().AddTask(loadingTask);
80 mLoadingTasks.push_back(AsyncImageLoadingInfo(loadingTask,mLoadTaskId));
84 uint32_t AsyncImageLoader::ApplyMask(Devel::PixelBuffer pixelBuffer,
85 Devel::PixelBuffer maskPixelBuffer,
88 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
90 LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad, MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
91 Dali::AsyncTaskManager::Get().AddTask(loadingTask);
92 mLoadingTasks.push_back(AsyncImageLoadingInfo(loadingTask,mLoadTaskId));
96 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
101 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
103 return mPixelBufferLoadedSignal;
106 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
108 // Remove already completed tasks
109 RemoveCompletedTask();
111 auto end = mLoadingTasks.end();
112 for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
114 if((*iter).loadId == loadingTaskId)
116 Dali::AsyncTaskManager::Get().RemoveTask((*iter).loadingTask);
117 mLoadingTasks.erase(iter);
125 void AsyncImageLoader::CancelAll()
127 // Remove already completed tasks
128 RemoveCompletedTask();
130 auto end = mLoadingTasks.end();
131 for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
133 if((*iter).loadingTask && Dali::AsyncTaskManager::Get())
135 Dali::AsyncTaskManager::Get().RemoveTask(((*iter).loadingTask));
138 mLoadingTasks.clear();
141 void AsyncImageLoader::ProcessLoadedImage(LoadingTaskPtr task)
143 if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
145 mPixelBufferLoadedSignal.Emit(task->id, task->pixelBuffers);
147 else if(mLoadedSignal.GetConnectionCount() > 0)
150 if(!task->pixelBuffers.empty())
152 pixelData = Devel::PixelBuffer::Convert(task->pixelBuffers[0]);
154 mLoadedSignal.Emit(task->id, pixelData);
157 mCompletedTaskIds.push_back(task->id);
160 void AsyncImageLoader::RemoveCompletedTask()
162 std::uint32_t loadingTaskId;
163 auto end = mLoadingTasks.end();
164 auto endCompletedIter = mCompletedTaskIds.end();
165 for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
167 loadingTaskId = (*iter).loadId;
168 for(auto iterCompleted = mCompletedTaskIds.begin(); iterCompleted != endCompletedIter; ++iterCompleted)
170 if((*iterCompleted) == loadingTaskId)
172 mLoadingTasks.erase(iter);
178 } // namespace Internal
180 } // namespace Toolkit