Merge "TextController refactor." 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) 2015 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/devel-api/threading/conditional-wait.h>
24 #include <dali/devel-api/threading/mutex.h>
25 #include <dali/devel-api/threading/thread.h>
26 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
27 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 /**
39  * The task of loading and packing an image into the atlas.
40  */
41 struct LoadingTask
42 {
43   /**
44    * Constructor.
45    */
46   LoadingTask( uint32_t id, BitmapLoader loader );
47
48 private:
49
50   // Undefined
51   LoadingTask( const LoadingTask& queue );
52
53   // Undefined
54   LoadingTask& operator=( const LoadingTask& queue );
55
56 public:
57
58   BitmapLoader loader;    ///< The loader used to load the bitmap from URL
59   uint32_t     id;        ///< The id associated with this task.
60 };
61
62
63 /**
64  * The worker thread for image loading.
65  */
66 class ImageLoadThread : public Thread
67 {
68 public:
69
70   /**
71    * Constructor.
72    *
73    * @param[in] mTrigger The trigger to wake up the main thread.
74    */
75   ImageLoadThread( EventThreadCallback* mTrigger );
76
77   /**
78    * Destructor.
79    */
80   virtual ~ImageLoadThread();
81
82   /**
83    * Add a task in to the loading queue
84    *
85    * @param[in] task The task added to the queue.
86    */
87   void AddTask( LoadingTask* task );
88
89   /**
90    * Pop the next task out from the completed queue.
91    *
92    * @return The next task to be processed.
93    */
94   LoadingTask* NextCompletedTask();
95
96   /**
97    * Remove the loading task from the waiting queue.
98    */
99   bool CancelTask( uint32_t loadingTaskId );
100
101   /**
102    * Remove all the loading tasks in the waiting queue.
103    */
104   void CancelAll();
105
106 private:
107
108   /**
109    * Pop the next loading task out from the queue to process.
110    *
111    * @return The next task to be processed.
112    */
113   LoadingTask* NextTaskToProcess();
114
115   /**
116    * Add a task in to the loading queue
117    *
118    * @param[in] task The task added to the queue.
119    */
120   void AddCompletedTask( LoadingTask* task );
121
122 protected:
123
124   /**
125    * The entry function of the worker thread.
126    * It fetches loading task from the loadQueue, loads the image and adds to the completeQueue.
127    */
128   virtual void Run();
129
130 private:
131
132   // Undefined
133   ImageLoadThread( const ImageLoadThread& thread );
134
135   // Undefined
136   ImageLoadThread& operator=( const ImageLoadThread& thread );
137
138 private:
139
140   Vector< LoadingTask* > mLoadQueue;     ///<The task queue with images for loading.
141   Vector< LoadingTask* > mCompleteQueue; ///<The task queue with images loaded.
142
143   ConditionalWait        mConditionalWait;
144   Dali::Mutex            mMutex;
145   EventThreadCallback*   mTrigger;
146 };
147
148 } // namespace Internal
149
150 } // namespace Toolkit
151
152 } // namespace Dali
153
154 #endif /* __DALI_TOOLKIT_IMAGE_LOAD_THREAD_H__ */