[dali_2.3.21] Merge branch 'devel/master'
[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) 2023 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    * @brief Set the Texture Id
152    */
153   void SetTextureId(TextureManagerType::TextureId id);
154
155 public: // Implementation of AsyncTask
156   /**
157    * @copydoc Dali::AsyncTask::Process()
158    */
159   void Process() override;
160
161   /**
162    * @copydoc Dali::AsyncTask::IsReady()
163    */
164   bool IsReady() override;
165
166   /**
167    * @copydoc Dali::AsyncTask::GetTaskName()
168    */
169   std::string_view GetTaskName() const override
170   {
171     return "LoadingTask";
172   }
173
174 private:
175   // Undefined
176   LoadingTask(const LoadingTask& queue);
177
178   // Undefined
179   LoadingTask& operator=(const LoadingTask& queue);
180
181   /**
182    * Load the image
183    */
184   void Load();
185
186   /**
187    * Apply mask
188    */
189   void ApplyMask();
190
191   /**
192    * Multiply alpha
193    */
194   void MultiplyAlpha();
195
196 public:
197   std::vector<Devel::PixelBuffer> pixelBuffers{};              ///< pixelBuffer handle after successful load
198                                                                ///< or pixelBuffer to be masked image in the mask task
199   VisualUrl                                url;                ///< url of the image to load
200   EncodedImageBuffer                       encodedImageBuffer; ///< encoded buffer of the image to load
201   uint32_t                                 id;                 ///< The unique id associated with this task.
202   TextureManagerType::TextureId            textureId;          ///< textureId for loading
203   ImageDimensions                          dimensions;         ///< dimensions to load
204   FittingMode::Type                        fittingMode;        ///< fitting options
205   SamplingMode::Type                       samplingMode;       ///< sampling options
206   DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad;  ///< if the image's color should be multiplied by it's alpha
207
208   Devel::PixelBuffer         maskPixelBuffer; ///< pixelBuffer of mask image
209   float                      contentScale;    ///< The factor to scale the content
210   Dali::AnimatedImageLoading animatedImageLoading;
211   uint32_t                   frameIndex;
212
213   bool orientationCorrection : 1; ///< if orientation correction is needed
214   bool isMaskTask : 1;            ///< whether this task is for mask or not
215   bool cropToMask : 1;            ///< Whether to crop the content to the mask size
216   bool loadPlanes : 1;            ///< Whether to load image planes
217   bool isReady : 1;               ///< Whether this task ready to run
218 };
219
220 } // namespace Internal
221
222 } // namespace Toolkit
223
224 } // namespace Dali
225
226 #endif // DALI_TOOLKIT_IMAGE_LOAD_THREAD_H