Merge "C# CustomView Implementation (C++ wrappers, manual bindings, C# wrappers)...
[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/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    * @note This class takes ownership of the task object
88    */
89   void AddTask( LoadingTask* task );
90
91   /**
92    * Pop the next task out from the completed queue.
93    *
94    * @return The next task to be processed.
95    */
96   LoadingTask* NextCompletedTask();
97
98   /**
99    * Remove the loading task from the waiting queue.
100    */
101   bool CancelTask( uint32_t loadingTaskId );
102
103   /**
104    * Remove all the loading tasks in the waiting queue.
105    */
106   void CancelAll();
107
108 private:
109
110   /**
111    * Pop the next loading task out from the queue to process.
112    *
113    * @return The next task to be processed.
114    */
115   LoadingTask* NextTaskToProcess();
116
117   /**
118    * Add a task in to the loading queue
119    *
120    * @param[in] task The task added to the queue.
121    */
122   void AddCompletedTask( LoadingTask* task );
123
124 protected:
125
126   /**
127    * The entry function of the worker thread.
128    * It fetches loading task from the loadQueue, loads the image and adds to the completeQueue.
129    */
130   virtual void Run();
131
132 private:
133
134   // Undefined
135   ImageLoadThread( const ImageLoadThread& thread );
136
137   // Undefined
138   ImageLoadThread& operator=( const ImageLoadThread& thread );
139
140 private:
141
142   Vector< LoadingTask* > mLoadQueue;     ///<The task queue with images for loading.
143   Vector< LoadingTask* > mCompleteQueue; ///<The task queue with images loaded.
144
145   ConditionalWait        mConditionalWait;
146   Dali::Mutex            mMutex;
147   EventThreadCallback*   mTrigger;
148 };
149
150 } // namespace Internal
151
152 } // namespace Toolkit
153
154 } // namespace Dali
155
156 #endif /* __DALI_TOOLKIT_IMAGE_LOAD_THREAD_H__ */