Add async task manager
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / async-image-loader-impl.h
1 #ifndef DALI_TOOLKIT_ASYNC_IMAGE_LOADER_IMPL_H
2 #define DALI_TOOLKIT_ASYNC_IMAGE_LOADER_IMPL_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/images/pixel-data.h>
22 #include <dali/public-api/object/base-object.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/devel-api/image-loader/async-image-loader-devel.h>
26 #include <dali-toolkit/internal/image-loader/loading-task.h>
27 #include <dali-toolkit/public-api/image-loader/async-image-loader.h>
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 namespace Internal
34 {
35 using LoadingTaskPtr = IntrusivePtr<LoadingTask>;
36
37 struct AsyncImageLoadingInfo
38 {
39   AsyncImageLoadingInfo(LoadingTaskPtr loadingTask,std::uint32_t loadId)
40   : loadingTask(loadingTask),
41     loadId(loadId)
42   {
43   }
44
45   LoadingTaskPtr loadingTask;
46   std::uint32_t  loadId;
47 };
48
49 class AsyncImageLoader : public BaseObject, public ConnectionTracker
50 {
51 public:
52   /**
53    * Constructor
54    */
55   AsyncImageLoader();
56
57   /**
58    * @copydoc Toolkit::AsyncImageLoader::New()
59    */
60   static IntrusivePtr<AsyncImageLoader> New();
61
62   /**
63    * @copydoc Toolkit::AsyncImageLoader::LoadAnimatedImage( Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
64    */
65   uint32_t LoadAnimatedImage(Dali::AnimatedImageLoading               animatedImageLoading,
66                              uint32_t                                 frameIndex,
67                              DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
68
69   /**
70    * @brief Starts an image loading task.
71    * @param[in] url The URL of the image file to load
72    * @param[in] dimensions The width and height to fit the loaded image to
73    * @param[in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter
74    * @param[in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size
75    * @param[in] orientationCorrection Reorient the image to respect any orientation metadata in its header
76    * @param[in] preMultiplyOnLoad ON if the image color should be multiplied by it's alpha. Set to OFF if there is no alpha or if the image need to be applied alpha mask.
77    * @param[in] loadPlanes true to load image planes or false to load bitmap image.
78    * @return The loading task id
79    */
80   uint32_t Load(const VisualUrl&                         url,
81                 ImageDimensions                          dimensions,
82                 FittingMode::Type                        fittingMode,
83                 SamplingMode::Type                       samplingMode,
84                 bool                                     orientationCorrection,
85                 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
86                 bool                                     loadPlanes);
87
88   /**
89    * @brief Starts an image loading task by encoded image buffer.
90    * @param[in] encodedImageBuffer The encoded buffer of the image to load
91    * @param[in] dimensions The width and height to fit the loaded image to
92    * @param[in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter
93    * @param[in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size
94    * @param[in] orientationCorrection Reorient the image to respect any orientation metadata in its header
95    * @param[in] preMultiplyOnLoad ON if the image color should be multiplied by it's alpha. Set to OFF if there is no alpha.
96    * @return The loading task id
97    */
98   uint32_t LoadEncodedImageBuffer(const EncodedImageBuffer&                encodedImageBuffer,
99                                   ImageDimensions                          dimensions,
100                                   FittingMode::Type                        fittingMode,
101                                   SamplingMode::Type                       samplingMode,
102                                   bool                                     orientationCorrection,
103                                   DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
104
105   /**
106    * @brief Starts an mask applying task.
107    * @param[in] pixelBuffer of the to be masked image
108    * @param[in] maskPixelBuffer of the mask image
109    * @param[in] contentScale The factor to scale the content
110    * @param[in] cropToMask Whether to crop the content to the mask size
111    * @param[in] preMultiplyOnLoad ON if the image color should be multiplied by it's alpha. Set to OFF if there is no alpha.
112    * @return The loading task id
113    */
114   uint32_t ApplyMask(Devel::PixelBuffer                       pixelBuffer,
115                      Devel::PixelBuffer                       maskPixelBuffer,
116                      float                                    contentScale,
117                      bool                                     cropToMask,
118                      DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
119
120   /**
121    * @copydoc Toolkit::AsyncImageLoader::ImageLoadedSignal
122    */
123   Toolkit::AsyncImageLoader::ImageLoadedSignalType& ImageLoadedSignal();
124
125   /**
126    * @copydoc Toolkit::AsyncImageLoader::PixelBufferLoadedSignal
127    */
128   Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& PixelBufferLoadedSignal();
129
130   /**
131    * @copydoc Toolkit::AsyncImageLoader::Cancel
132    */
133   bool Cancel(uint32_t loadingTaskId);
134
135   /**
136    * @copydoc Toolkit::AsyncImageLoader::CancelAll
137    */
138   void CancelAll();
139
140   /**
141    * Process the completed loading task from the worker thread.
142    */
143   void ProcessLoadedImage(LoadingTaskPtr task);
144
145 protected:
146   /**
147    * Destructor
148    */
149   ~AsyncImageLoader() override;
150
151 private:
152   /**
153    * Remove already completed tasks
154    */
155   void RemoveCompletedTask();
156
157 private:
158   Toolkit::AsyncImageLoader::ImageLoadedSignalType            mLoadedSignal;
159   Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType mPixelBufferLoadedSignal;
160   std::vector<AsyncImageLoadingInfo>                          mLoadingTasks;
161   std::vector<uint32_t>                                       mCompletedTaskIds;
162   uint32_t                                                    mLoadTaskId;
163 };
164
165 } // namespace Internal
166
167 inline const Internal::AsyncImageLoader& GetImplementation(const Toolkit::AsyncImageLoader& handle)
168 {
169   DALI_ASSERT_ALWAYS(handle && "AsyncImageLoader handle is empty");
170
171   const BaseObject& object = handle.GetBaseObject();
172
173   return static_cast<const Internal::AsyncImageLoader&>(object);
174 }
175
176 inline Internal::AsyncImageLoader& GetImplementation(Toolkit::AsyncImageLoader& handle)
177 {
178   DALI_ASSERT_ALWAYS(handle && "AsyncImageLoader handle is empty");
179
180   BaseObject& object = handle.GetBaseObject();
181
182   return static_cast<Internal::AsyncImageLoader&>(object);
183 }
184
185 } // namespace Toolkit
186
187 } // namespace Dali
188
189 #endif // DALI_TOOLKIT_ASYNC_IMAGE_LOADER_IMPL_H