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