c3f230c21f48f5e84fc83cc413d58e8db680a0d7
[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) 2017 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
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 /**
42  * The task of loading and packing an image into the atlas.
43  */
44 struct LoadingTask
45 {
46   /**
47    * Constructor.
48    * @param [in] id of the task
49    * @param [in] url The URL of the image file to load.
50    * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
51    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
52    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
53    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
54    */
55   LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
56                FittingMode::Type fittingMode, SamplingMode::Type samplingMode,
57                bool orientationCorrection );
58
59   /**
60    * Load the image
61    */
62   void Load();
63
64 private:
65
66   // Undefined
67   LoadingTask( const LoadingTask& queue );
68
69   // Undefined
70   LoadingTask& operator=( const LoadingTask& queue );
71
72 public:
73
74   Devel::PixelBuffer pixelBuffer;   ///< pixelBuffer handle after successful load
75   VisualUrl          url;           ///< url of the image to load
76   uint32_t           id;            ///< The unique id associated with this task.
77   ImageDimensions    dimensions;    ///< dimensions to load
78   FittingMode::Type  fittingMode;   ///< fitting options
79   SamplingMode::Type samplingMode;  ///< sampling options
80   bool               orientationCorrection:1; ///< if orientation correction is needed
81
82 };
83
84
85 /**
86  * The worker thread for image loading.
87  */
88 class ImageLoadThread : public Thread
89 {
90 public:
91
92   /**
93    * Constructor.
94    *
95    * @param[in] mTrigger The trigger to wake up the main thread.
96    */
97   ImageLoadThread( EventThreadCallback* mTrigger );
98
99   /**
100    * Destructor.
101    */
102   virtual ~ImageLoadThread();
103
104   /**
105    * Add a task in to the loading queue
106    *
107    * @param[in] task The task added to the queue.
108    *
109    * @note This class takes ownership of the task object
110    */
111   void AddTask( LoadingTask* task );
112
113   /**
114    * Pop the next task out from the completed queue.
115    *
116    * @return The next task to be processed.
117    */
118   LoadingTask* NextCompletedTask();
119
120   /**
121    * Remove the loading task from the waiting queue.
122    */
123   bool CancelTask( uint32_t loadingTaskId );
124
125   /**
126    * Remove all the loading tasks in the waiting queue.
127    */
128   void CancelAll();
129
130 private:
131
132   /**
133    * Pop the next loading task out from the queue to process.
134    *
135    * @return The next task to be processed.
136    */
137   LoadingTask* NextTaskToProcess();
138
139   /**
140    * Add a task in to the loading queue
141    *
142    * @param[in] task The task added to the queue.
143    */
144   void AddCompletedTask( LoadingTask* task );
145
146 protected:
147
148   /**
149    * The entry function of the worker thread.
150    * It fetches loading task from the loadQueue, loads the image and adds to the completeQueue.
151    */
152   virtual void Run();
153
154 private:
155
156   // Undefined
157   ImageLoadThread( const ImageLoadThread& thread );
158
159   // Undefined
160   ImageLoadThread& operator=( const ImageLoadThread& thread );
161
162 private:
163
164   Vector< LoadingTask* > mLoadQueue;     ///<The task queue with images for loading.
165   Vector< LoadingTask* > mCompleteQueue; ///<The task queue with images loaded.
166   EventThreadCallback*   mTrigger;
167   const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
168
169   ConditionalWait        mConditionalWait;
170   Dali::Mutex            mMutex;
171 };
172
173 } // namespace Internal
174
175 } // namespace Toolkit
176
177 } // namespace Dali
178
179 #endif /* __DALI_TOOLKIT_IMAGE_LOAD_THREAD_H__ */