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