[dali_2.3.21] Merge branch 'devel/master'
[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 animated image loading task.
71    * @param[in] asyncImageLoader The ayncImageLoader
72    * @param[in] animatedImageLoading The AnimatedImageLoading to load animated image
73    * @param[in] frameIndex The frame index of a frame to be loaded frame
74    * @param[in] dimensions The width and height to fit the loaded image to
75    * @param[in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter
76    * @param[in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size
77    * @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.
78    * @return The loading task id
79    */
80   uint32_t LoadAnimatedImage(Dali::AnimatedImageLoading               animatedImageLoading,
81                              uint32_t                                 frameIndex,
82                              Dali::ImageDimensions                    desiredSize,
83                              Dali::FittingMode::Type                  fittingMode,
84                              Dali::SamplingMode::Type                 samplingMode,
85                              DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
86
87   /**
88    * @brief Starts an image loading task.
89    * @param[in] url The URL of the image file to load
90    * @param[in] dimensions The width and height to fit the loaded image to
91    * @param[in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter
92    * @param[in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size
93    * @param[in] orientationCorrection Reorient the image to respect any orientation metadata in its header
94    * @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.
95    * @param[in] loadPlanes true to load image planes or false to load bitmap image.
96    * @return The loading task id
97    */
98   uint32_t Load(const VisualUrl&                         url,
99                 ImageDimensions                          dimensions,
100                 FittingMode::Type                        fittingMode,
101                 SamplingMode::Type                       samplingMode,
102                 bool                                     orientationCorrection,
103                 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
104                 bool                                     loadPlanes);
105
106   /**
107    * @brief Starts an image loading task by encoded image buffer.
108    * @param[in] encodedImageBuffer The encoded buffer of the image to load
109    * @param[in] dimensions The width and height to fit the loaded image to
110    * @param[in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter
111    * @param[in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size
112    * @param[in] orientationCorrection Reorient the image to respect any orientation metadata in its header
113    * @param[in] preMultiplyOnLoad ON if the image color should be multiplied by it's alpha. Set to OFF if there is no alpha.
114    * @return The loading task id
115    */
116   uint32_t LoadEncodedImageBuffer(const EncodedImageBuffer&                encodedImageBuffer,
117                                   ImageDimensions                          dimensions,
118                                   FittingMode::Type                        fittingMode,
119                                   SamplingMode::Type                       samplingMode,
120                                   bool                                     orientationCorrection,
121                                   DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
122
123   /**
124    * @brief Starts an mask applying task.
125    * @param[in] pixelBuffer of the to be masked image
126    * @param[in] maskPixelBuffer of the mask image
127    * @param[in] contentScale The factor to scale the content
128    * @param[in] cropToMask Whether to crop the content to the mask size
129    * @param[in] preMultiplyOnLoad ON if the image color should be multiplied by it's alpha. Set to OFF if there is no alpha.
130    * @return The loading task id
131    */
132   uint32_t ApplyMask(Devel::PixelBuffer                       pixelBuffer,
133                      Devel::PixelBuffer                       maskPixelBuffer,
134                      float                                    contentScale,
135                      bool                                     cropToMask,
136                      DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
137
138   /**
139    * @copydoc Toolkit::AsyncImageLoader::ImageLoadedSignal
140    */
141   Toolkit::AsyncImageLoader::ImageLoadedSignalType& ImageLoadedSignal();
142
143   /**
144    * @copydoc Toolkit::AsyncImageLoader::PixelBufferLoadedSignal
145    */
146   Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& PixelBufferLoadedSignal();
147
148   /**
149    * @copydoc Toolkit::AsyncImageLoader::Cancel
150    */
151   bool Cancel(uint32_t loadingTaskId);
152
153   /**
154    * @copydoc Toolkit::AsyncImageLoader::CancelAll
155    */
156   void CancelAll();
157
158   /**
159    * Process the completed loading task from the worker thread.
160    */
161   void ProcessLoadedImage(LoadingTaskPtr task);
162
163 protected:
164   /**
165    * Destructor
166    */
167   ~AsyncImageLoader() override;
168
169 private:
170   /**
171    * Remove already completed tasks
172    */
173   void RemoveCompletedTask();
174
175 private:
176   Toolkit::AsyncImageLoader::ImageLoadedSignalType            mLoadedSignal;
177   Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType mPixelBufferLoadedSignal;
178   std::vector<AsyncImageLoadingInfo>                          mLoadingTasks;
179   std::vector<uint32_t>                                       mCompletedTaskIds;
180   uint32_t                                                    mLoadTaskId;
181 };
182
183 } // namespace Internal
184
185 inline const Internal::AsyncImageLoader& GetImplementation(const Toolkit::AsyncImageLoader& handle)
186 {
187   DALI_ASSERT_ALWAYS(handle && "AsyncImageLoader handle is empty");
188
189   const BaseObject& object = handle.GetBaseObject();
190
191   return static_cast<const Internal::AsyncImageLoader&>(object);
192 }
193
194 inline Internal::AsyncImageLoader& GetImplementation(Toolkit::AsyncImageLoader& handle)
195 {
196   DALI_ASSERT_ALWAYS(handle && "AsyncImageLoader handle is empty");
197
198   BaseObject& object = handle.GetBaseObject();
199
200   return static_cast<Internal::AsyncImageLoader&>(object);
201 }
202
203 } // namespace Toolkit
204
205 } // namespace Dali
206
207 #endif // DALI_TOOLKIT_ASYNC_IMAGE_LOADER_IMPL_H