DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / loading-task.h
1 #ifndef DALI_TOOLKIT_IMAGE_LOADING_TASK_H
2 #define DALI_TOOLKIT_IMAGE_LOADING_TASK_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-toolkit/devel-api/image-loader/async-image-loader-devel.h>
22 #include <dali-toolkit/internal/texture-manager/texture-manager-type.h>
23 #include <dali-toolkit/internal/visuals/visual-url.h>
24 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
25 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
26 #include <dali/devel-api/threading/conditional-wait.h>
27 #include <dali/devel-api/threading/mutex.h>
28 #include <dali/devel-api/threading/thread.h>
29 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
30 #include <dali/public-api/adaptor-framework/async-task-manager.h>
31 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
32 #include <dali/public-api/common/dali-vector.h>
33 #include <dali/public-api/images/image-operations.h>
34 #include <dali/public-api/object/ref-object.h>
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42 class LoadingTask;
43 using LoadingTaskPtr = IntrusivePtr<LoadingTask>;
44
45 /**
46  * The task of loading and packing an image into the atlas
47  */
48 class LoadingTask : public AsyncTask
49 {
50 public:
51   /**
52    * Constructor.
53    * @param [in] id of the task
54    * @param [in] animatedImageLoading The AnimatedImageLoading to load animated image
55    * @param [in] frameIndex The frame index of a frame to be loaded frame
56    * @param [in] preMultiplyOnLoad ON if the image's 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.
57    * @param [in] callback The callback that is called when the operation is completed.
58    */
59   LoadingTask(uint32_t                                 id,
60               Dali::AnimatedImageLoading               animatedImageLoading,
61               uint32_t                                 frameIndex,
62               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
63               CallbackBase*                            callback);
64
65   /**
66    * Constructor.
67    * @param [in] id of the task
68    * @param [in] animatedImageLoading The AnimatedImageLoading to load animated image
69    * @param [in] frameIndex The frame index of a frame to be loaded frame
70    * @param [in] dimensions The width and height to fit the loaded image to
71    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter
72    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size
73    * @param [in] preMultiplyOnLoad ON if the image's 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.
74    * @param [in] callback The callback that is called when the operation is completed.
75    */
76   LoadingTask(uint32_t                                 id,
77               Dali::AnimatedImageLoading               animatedImageLoading,
78               uint32_t                                 frameIndex,
79               Dali::ImageDimensions                    dimensions,
80               Dali::FittingMode::Type                  fittingMode,
81               Dali::SamplingMode::Type                 samplingMode,
82               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
83               CallbackBase*                            callback);
84
85   /**
86    * Constructor.
87    * @param [in] id of the task
88    * @param [in] url The URL of the image file to load.
89    * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
90    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
91    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
92    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
93    * @param [in] preMultiplyOnLoad ON if the image's 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.
94    * @param [in] loadPlanes true to load image planes or false to load bitmap image.
95    * @param [in] callback The callback that is called when the operation is completed.
96    */
97   LoadingTask(uint32_t                                 id,
98               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               CallbackBase*                            callback);
106
107   /**
108    * Constructor.
109    * @param [in] id of the task
110    * @param [in] encodedImageBuffer The encoded buffer of the image to load.
111    * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
112    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
113    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
114    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
115    * @param [in] preMultiplyOnLoad ON if the image's 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.
116    * @param [in] callback The callback that is called when the operation is completed.
117    */
118   LoadingTask(uint32_t                                 id,
119               const EncodedImageBuffer&                encodedImageBuffer,
120               ImageDimensions                          dimensions,
121               FittingMode::Type                        fittingMode,
122               SamplingMode::Type                       samplingMode,
123               bool                                     orientationCorrection,
124               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
125               CallbackBase*                            callback);
126
127   /**
128    * Constructor.
129    * @param [in] id of the task
130    * @param [in] pixelBuffer of the to be masked image
131    * @param [in] maskPixelBuffer of the mask image
132    * @param [in] contentScale The factor to scale the content
133    * @param [in] cropToMask Whether to crop the content to the mask size
134    * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
135    * @param [in] callback The callback that is called when the operation is completed.
136    */
137   LoadingTask(uint32_t                                 id,
138               Devel::PixelBuffer                       pixelBuffer,
139               Devel::PixelBuffer                       maskPixelBuffer,
140               float                                    contentScale,
141               bool                                     cropToMask,
142               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
143               CallbackBase*                            callback);
144
145   /**
146    * Destructor.
147    */
148   ~LoadingTask() override;
149
150   /**
151    * Process the task accodring to the type
152    */
153   void Process() override;
154
155   /**
156    * Whether the task is ready to process.
157    * @return True if the task is ready to process.
158    */
159   bool IsReady() override;
160
161   /**
162    * @brief Set the Texture Id
163    *
164    */
165   void SetTextureId(TextureManagerType::TextureId id);
166
167 private:
168   // Undefined
169   LoadingTask(const LoadingTask& queue);
170
171   // Undefined
172   LoadingTask& operator=(const LoadingTask& queue);
173
174   /**
175    * Load the image
176    */
177   void Load();
178
179   /**
180    * Apply mask
181    */
182   void ApplyMask();
183
184   /**
185    * Multiply alpha
186    */
187   void MultiplyAlpha();
188
189 public:
190   std::vector<Devel::PixelBuffer> pixelBuffers{};              ///< pixelBuffer handle after successful load
191                                                                ///< or pixelBuffer to be masked image in the mask task
192   VisualUrl                                url;                ///< url of the image to load
193   EncodedImageBuffer                       encodedImageBuffer; ///< encoded buffer of the image to load
194   uint32_t                                 id;                 ///< The unique id associated with this task.
195   TextureManagerType::TextureId            textureId;          ///< textureId for loading
196   ImageDimensions                          dimensions;         ///< dimensions to load
197   FittingMode::Type                        fittingMode;        ///< fitting options
198   SamplingMode::Type                       samplingMode;       ///< sampling options
199   DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad;  ///< if the image's color should be multiplied by it's alpha
200
201   Devel::PixelBuffer         maskPixelBuffer; ///< pixelBuffer of mask image
202   float                      contentScale;    ///< The factor to scale the content
203   Dali::AnimatedImageLoading animatedImageLoading;
204   uint32_t                   frameIndex;
205
206   bool orientationCorrection : 1; ///< if orientation correction is needed
207   bool isMaskTask : 1;            ///< whether this task is for mask or not
208   bool cropToMask : 1;            ///< Whether to crop the content to the mask size
209   bool loadPlanes : 1;            ///< Whether to load image planes
210   bool isReady : 1;               ///< Whether this task ready to run
211 };
212
213 } // namespace Internal
214
215 } // namespace Toolkit
216
217 } // namespace Dali
218
219 #endif // DALI_TOOLKIT_IMAGE_LOAD_THREAD_H