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