Merge "Fix shader compile crash issue" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / image-load-thread.h
1 #ifndef DALI_TOOLKIT_IMAGE_LOAD_THREAD_H
2 #define DALI_TOOLKIT_IMAGE_LOAD_THREAD_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/devel-api/adaptor-framework/event-thread-callback.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali/devel-api/threading/conditional-wait.h>
26 #include <dali/devel-api/threading/mutex.h>
27 #include <dali/devel-api/threading/thread.h>
28 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
29 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
30 #include <dali/public-api/common/dali-vector.h>
31 #include <dali/public-api/images/image-operations.h>
32 #include <dali/public-api/object/ref-object.h>
33
34 namespace Dali
35 {
36 namespace Toolkit
37 {
38 namespace Internal
39 {
40 /**
41  * The task of loading and packing an image into the atlas.
42  */
43 struct LoadingTask
44 {
45   /**
46    * Constructor.
47    * @param [in] id of the task
48    * @param [in] animatedImageLoading The AnimatedImageLoading to load animated image
49    * @param [in] frameIndex The frame index of a frame to be loaded frame
50    * @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.
51    */
52   LoadingTask(uint32_t                                 id,
53               Dali::AnimatedImageLoading               animatedImageLoading,
54               uint32_t                                 frameIndex,
55               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
56
57   /**
58    * Constructor.
59    * @param [in] id of the task
60    * @param [in] url The URL of the image file to load.
61    * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
62    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
63    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
64    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
65    * @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.
66    * @param [in] loadPlanes true to load image planes or false to load bitmap image.
67    */
68   LoadingTask(uint32_t                                 id,
69               const VisualUrl&                         url,
70               ImageDimensions                          dimensions,
71               FittingMode::Type                        fittingMode,
72               SamplingMode::Type                       samplingMode,
73               bool                                     orientationCorrection,
74               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad,
75               bool                                     loadPlanes);
76
77   /**
78    * Constructor.
79    * @param [in] id of the task
80    * @param [in] encodedImageBuffer The encoded buffer of the image to load.
81    * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
82    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
83    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
84    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
85    * @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.
86    */
87   LoadingTask(uint32_t                                 id,
88               const EncodedImageBuffer&                encodedImageBuffer,
89               ImageDimensions                          dimensions,
90               FittingMode::Type                        fittingMode,
91               SamplingMode::Type                       samplingMode,
92               bool                                     orientationCorrection,
93               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
94
95   /**
96    * Constructor.
97    * @param [in] id of the task
98    * @param [in] pixelBuffer of the to be masked image
99    * @param [in] maskPixelBuffer of the mask image
100    * @param [in] contentScale The factor to scale the content
101    * @param [in] cropToMask Whether to crop the content to the mask size
102    * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
103    */
104   LoadingTask(uint32_t                                 id,
105               Devel::PixelBuffer                       pixelBuffer,
106               Devel::PixelBuffer                       maskPixelBuffer,
107               float                                    contentScale,
108               bool                                     cropToMask,
109               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
110
111   /**
112    * Load the image
113    */
114   void Load();
115
116   /**
117    * Apply mask
118    */
119   void ApplyMask();
120
121   /**
122    * Multiply alpha
123    */
124   void MultiplyAlpha();
125
126 private:
127   // Undefined
128   LoadingTask(const LoadingTask& queue);
129
130   // Undefined
131   LoadingTask& operator=(const LoadingTask& queue);
132
133 public:
134   std::vector<Devel::PixelBuffer> pixelBuffers{};              ///< pixelBuffer handle after successful load
135                                                                ///< or pixelBuffer to be masked image in the mask task
136   VisualUrl                                url;                ///< url of the image to load
137   EncodedImageBuffer                       encodedImageBuffer; ///< encoded buffer of the image to load
138   uint32_t                                 id;                 ///< The unique id associated with this task.
139   ImageDimensions                          dimensions;         ///< dimensions to load
140   FittingMode::Type                        fittingMode;        ///< fitting options
141   SamplingMode::Type                       samplingMode;       ///< sampling options
142   DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad;  ///< if the image's color should be multiplied by it's alpha
143
144   Devel::PixelBuffer         maskPixelBuffer; ///< pixelBuffer of mask image
145   float                      contentScale;    ///< The factor to scale the content
146   Dali::AnimatedImageLoading animatedImageLoading;
147   uint32_t                   frameIndex;
148
149   bool orientationCorrection : 1; ///< if orientation correction is needed
150   bool isMaskTask : 1;            ///< whether this task is for mask or not
151   bool cropToMask : 1;            ///< Whether to crop the content to the mask size
152   bool loadPlanes : 1;            ///< Whether to load image planes
153 };
154
155 /**
156  * The worker thread for image loading.
157  */
158 class ImageLoadThread : public Thread
159 {
160 public:
161   /**
162    * Constructor.
163    *
164    * @param[in] mTrigger The trigger to wake up the main thread.
165    */
166   ImageLoadThread(EventThreadCallback* mTrigger);
167
168   /**
169    * Destructor.
170    */
171   ~ImageLoadThread() override;
172
173   /**
174    * Add a task in to the loading queue
175    *
176    * @param[in] task The task added to the queue.
177    *
178    * @note This class takes ownership of the task object
179    */
180   void AddTask(LoadingTask* task);
181
182   /**
183    * Pop the next task out from the completed queue.
184    *
185    * @return The next task to be processed.
186    */
187   LoadingTask* NextCompletedTask();
188
189   /**
190    * Remove the loading task from the waiting queue.
191    */
192   bool CancelTask(uint32_t loadingTaskId);
193
194   /**
195    * Remove all the loading tasks in the waiting queue.
196    */
197   void CancelAll();
198
199 private:
200   /**
201    * Pop the next loading task out from the queue to process.
202    *
203    * @return The next task to be processed.
204    */
205   LoadingTask* NextTaskToProcess();
206
207   /**
208    * Add a task in to the loading queue
209    *
210    * @param[in] task The task added to the queue.
211    */
212   void AddCompletedTask(LoadingTask* task);
213
214 protected:
215   /**
216    * The entry function of the worker thread.
217    * It fetches loading task from the loadQueue, loads the image and adds to the completeQueue.
218    */
219   void Run() override;
220
221 private:
222   // Undefined
223   ImageLoadThread(const ImageLoadThread& thread);
224
225   // Undefined
226   ImageLoadThread& operator=(const ImageLoadThread& thread);
227
228 private:
229   Vector<LoadingTask*>             mLoadQueue;     ///<The task queue with images for loading.
230   Vector<LoadingTask*>             mCompleteQueue; ///<The task queue with images loaded.
231   EventThreadCallback*             mTrigger;
232   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
233
234   ConditionalWait mConditionalWait;
235   Dali::Mutex     mMutex;
236 };
237
238 } // namespace Internal
239
240 } // namespace Toolkit
241
242 } // namespace Dali
243
244 #endif // DALI_TOOLKIT_IMAGE_LOAD_THREAD_H