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