Add async task manager
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / async-image-loader-impl.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 "async-image-loader-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/adaptor-framework/async-task-manager.h>
23 #include <dali/integration-api/adaptor-framework/adaptor.h>
24
25 namespace Dali
26 {
27 namespace Toolkit
28 {
29 namespace Internal
30 {
31 AsyncImageLoader::AsyncImageLoader()
32 : mLoadedSignal(),
33   mLoadTaskId(0u)
34 {
35 }
36
37 AsyncImageLoader::~AsyncImageLoader()
38 {
39   CancelAll();
40 }
41
42 IntrusivePtr<AsyncImageLoader> AsyncImageLoader::New()
43 {
44   IntrusivePtr<AsyncImageLoader> internal = new AsyncImageLoader();
45   return internal;
46 }
47
48 uint32_t AsyncImageLoader::LoadAnimatedImage(Dali::AnimatedImageLoading               animatedImageLoading,
49                                              uint32_t                                 frameIndex,
50                                              DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
51 {
52   LoadingTaskPtr loadingTask = new LoadingTask(++mLoadTaskId, animatedImageLoading, frameIndex, preMultiplyOnLoad,MakeCallback(this, &AsyncImageLoader::ProcessLoadedImage));
53   Dali::AsyncTaskManager::Get().AddTask(loadingTask);
54   return mLoadTaskId;
55 }
56
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,
63                                 bool                                     loadPlanes)
64 {
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));
68   return mLoadTaskId;
69 }
70
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)
77 {
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));
81   return mLoadTaskId;
82 }
83
84 uint32_t AsyncImageLoader::ApplyMask(Devel::PixelBuffer                       pixelBuffer,
85                                      Devel::PixelBuffer                       maskPixelBuffer,
86                                      float                                    contentScale,
87                                      bool                                     cropToMask,
88                                      DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
89 {
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));
93   return mLoadTaskId;
94 }
95
96 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
97 {
98   return mLoadedSignal;
99 }
100
101 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
102 {
103   return mPixelBufferLoadedSignal;
104 }
105
106 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
107 {
108   // Remove already completed tasks
109   RemoveCompletedTask();
110
111   auto end = mLoadingTasks.end();
112   for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
113   {
114     if((*iter).loadId == loadingTaskId)
115     {
116       Dali::AsyncTaskManager::Get().RemoveTask((*iter).loadingTask);
117       mLoadingTasks.erase(iter);
118       return true;
119     }
120   }
121
122   return false;
123 }
124
125 void AsyncImageLoader::CancelAll()
126 {
127   // Remove already completed tasks
128   RemoveCompletedTask();
129
130   auto end = mLoadingTasks.end();
131   for(std::vector<AsyncImageLoadingInfo>::iterator iter = mLoadingTasks.begin(); iter != end; ++iter)
132   {
133     if((*iter).loadingTask && Dali::AsyncTaskManager::Get())
134     {
135       Dali::AsyncTaskManager::Get().RemoveTask(((*iter).loadingTask));
136     }
137   }
138   mLoadingTasks.clear();
139 }
140
141 void AsyncImageLoader::ProcessLoadedImage(LoadingTaskPtr task)
142 {
143   if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
144   {
145     mPixelBufferLoadedSignal.Emit(task->id, task->pixelBuffers);
146   }
147   else if(mLoadedSignal.GetConnectionCount() > 0)
148   {
149     PixelData pixelData;
150     if(!task->pixelBuffers.empty())
151     {
152       pixelData = Devel::PixelBuffer::Convert(task->pixelBuffers[0]);
153     }
154     mLoadedSignal.Emit(task->id, pixelData);
155   }
156
157   mCompletedTaskIds.push_back(task->id);
158 }
159
160 void AsyncImageLoader::RemoveCompletedTask()
161 {
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)
166   {
167     loadingTaskId = (*iter).loadId;
168     for(auto iterCompleted = mCompletedTaskIds.begin(); iterCompleted != endCompletedIter; ++iterCompleted)
169     {
170       if((*iterCompleted) == loadingTaskId)
171       {
172         mLoadingTasks.erase(iter);
173       }
174     }
175   }
176 }
177
178 } // namespace Internal
179
180 } // namespace Toolkit
181
182 } // namespace Dali