[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / resource-loader / resource-thread-base.h
1 #ifndef __DALI_SLP_PLATFORM_RESOURCE_THREAD_BASE_H__
2 #define __DALI_SLP_PLATFORM_RESOURCE_THREAD_BASE_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include "resource-loader.h"
21 #include <deque>
22 #include <boost/thread.hpp>
23
24 #include <dali/integration-api/platform-abstraction.h>
25 #include <dali/integration-api/resource-cache.h>
26
27 namespace Dali
28 {
29
30 namespace SlpPlatform
31 {
32
33 /**
34  * Resource loader worker thread
35  */
36 class ResourceThreadBase
37 {
38 public:
39   // typedefs and enums
40
41   /** Client threads send work to resource threads through Requests, for which
42    *  this type identifies the action to be taken on the resource thread. */
43   enum RequestType
44   {
45     /** Pull a resource out of the platform's file system. */
46     RequestLoad,
47     /** Pull a resource out of a memory buffer. */
48     RequestDecode,
49     /** Push a resource's data out to the file system. */
50     RequestSave
51   };
52
53   typedef std::pair<Integration::ResourceRequest, RequestType>  RequestInfo;
54   typedef std::deque<RequestInfo>                               RequestQueue;
55   typedef RequestQueue::iterator                                RequestQueueIter;
56
57 public:
58   // C'tors and D'tors
59   ResourceThreadBase(ResourceLoader& resourceLoader);
60   virtual ~ResourceThreadBase();
61
62 protected:
63   void TerminateThread();
64
65 public:
66   /**
67    * Add a resource request to the back of the queue
68    * @param[in] request The requested resource/file url and attributes
69    * @param[in] type    Load or save flag
70    */
71   void AddRequest(const Integration::ResourceRequest& request, const RequestType type);
72
73   /**
74    * Cancel a resource request. Removes the request from the queue.
75    * @param[in] resourceId ID of the resource to be canceled
76    */
77   void CancelRequest(Integration::ResourceId  resourceId);
78
79   /**
80    * Pause starting new work in the background, but keep that work queued.
81    */
82   void Pause();
83
84   /**
85    * Resume processing tasks on background thread.
86    */
87   void Resume();
88
89
90 protected:
91   /**
92    * Main control loop for the thread.
93    * The thread is terminated when this function exits
94    */
95   void ThreadLoop();
96
97   /**
98    * Wait for an incoming resource request or termination
99    */
100   void WaitForRequests();
101
102   /**
103    * Process the resource request at the head of the queue
104    */
105   void ProcessNextRequest();
106
107   /**
108    * Install a logging function in to core for this thread.
109    */
110   void InstallLogging();
111
112   /**
113    * Uninstall a logging function.
114    */
115   void UninstallLogging();
116
117   /**
118    * Load a resource
119    * @param[in] request  The requested resource/file url and attributes
120    */
121   virtual void Load(const Integration::ResourceRequest& request) = 0;
122
123   /**
124    * Decode a resource exactly as if it were being loaded but source its data
125    * from a memory buffer attached directly to the request object.
126    * @param[in] request  The requested resource data and attributes
127    */
128   virtual void Decode(const Integration::ResourceRequest& request);
129
130   /**
131    * Save a resource
132    * @param[in] request  The requested resource/file url and attributes
133    */
134   virtual void Save(const Integration::ResourceRequest& request) = 0;
135
136 protected:
137   ResourceLoader& mResourceLoader;
138   boost::thread* mThread;                       ///< thread instance
139   boost::condition_variable mCondition;         ///< condition variable
140   boost::mutex mMutex;                          ///< used to protect mQueue
141   RequestQueue mQueue;                          ///< Request queue
142   bool mPaused;                                 ///< Whether to process work in mQueue
143
144 #if defined(DEBUG_ENABLED)
145 public:
146   Integration::Log::Filter* mLogFilter;
147 #endif
148 }; // class ResourceThreadBase
149
150 } // namespace SlpPlatform
151
152 } // namespace Dali
153
154 #endif // __DALI_SLPPLATFORM_RESOURCE_THREAD_BASE_H__