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