Move MultiplyColorByAlpha() from main thread to resource thread
[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/integration-api/adaptors/log-factory-interface.h>
30 #include <dali-toolkit/internal/visuals/visual-url.h>
31 #include <dali-toolkit/devel-api/image-loader/async-image-loader-devel.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.
56    */
57   LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
58                FittingMode::Type fittingMode, SamplingMode::Type samplingMode,
59                bool orientationCorrection,
60                DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad );
61
62   /**
63    * Load the image
64    */
65   void Load();
66
67 private:
68
69   // Undefined
70   LoadingTask( const LoadingTask& queue );
71
72   // Undefined
73   LoadingTask& operator=( const LoadingTask& queue );
74
75 public:
76
77   Devel::PixelBuffer pixelBuffer;   ///< pixelBuffer handle after successful load
78   VisualUrl          url;           ///< url of the image to load
79   uint32_t           id;            ///< The unique id associated with this task.
80   ImageDimensions    dimensions;    ///< dimensions to load
81   FittingMode::Type  fittingMode;   ///< fitting options
82   SamplingMode::Type samplingMode;  ///< sampling options
83   bool               orientationCorrection:1; ///< if orientation correction is needed
84   DevelAsyncImageLoader::PreMultiplyOnLoad            preMultiplyOnLoad; //< if the image's color should be multiplied by it's alpha
85 };
86
87
88 /**
89  * The worker thread for image loading.
90  */
91 class ImageLoadThread : public Thread
92 {
93 public:
94
95   /**
96    * Constructor.
97    *
98    * @param[in] mTrigger The trigger to wake up the main thread.
99    */
100   ImageLoadThread( EventThreadCallback* mTrigger );
101
102   /**
103    * Destructor.
104    */
105   virtual ~ImageLoadThread();
106
107   /**
108    * Add a task in to the loading queue
109    *
110    * @param[in] task The task added to the queue.
111    *
112    * @note This class takes ownership of the task object
113    */
114   void AddTask( LoadingTask* task );
115
116   /**
117    * Pop the next task out from the completed queue.
118    *
119    * @return The next task to be processed.
120    */
121   LoadingTask* NextCompletedTask();
122
123   /**
124    * Remove the loading task from the waiting queue.
125    */
126   bool CancelTask( uint32_t loadingTaskId );
127
128   /**
129    * Remove all the loading tasks in the waiting queue.
130    */
131   void CancelAll();
132
133 private:
134
135   /**
136    * Pop the next loading task out from the queue to process.
137    *
138    * @return The next task to be processed.
139    */
140   LoadingTask* NextTaskToProcess();
141
142   /**
143    * Add a task in to the loading queue
144    *
145    * @param[in] task The task added to the queue.
146    */
147   void AddCompletedTask( LoadingTask* task );
148
149 protected:
150
151   /**
152    * The entry function of the worker thread.
153    * It fetches loading task from the loadQueue, loads the image and adds to the completeQueue.
154    */
155   virtual void Run();
156
157 private:
158
159   // Undefined
160   ImageLoadThread( const ImageLoadThread& thread );
161
162   // Undefined
163   ImageLoadThread& operator=( const ImageLoadThread& thread );
164
165 private:
166
167   Vector< LoadingTask* > mLoadQueue;     ///<The task queue with images for loading.
168   Vector< LoadingTask* > mCompleteQueue; ///<The task queue with images loaded.
169   EventThreadCallback*   mTrigger;
170   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
171
172   ConditionalWait        mConditionalWait;
173   Dali::Mutex            mMutex;
174 };
175
176 } // namespace Internal
177
178 } // namespace Toolkit
179
180 } // namespace Dali
181
182 #endif // DALI_TOOLKIT_IMAGE_LOAD_THREAD_H