Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_HWUI_TASK_MANAGER_H |
| 18 | #define ANDROID_HWUI_TASK_MANAGER_H |
| 19 | |
| 20 | #include <utils/Mutex.h> |
| 21 | #include <utils/String8.h> |
| 22 | #include <utils/Thread.h> |
| 23 | #include <utils/Vector.h> |
| 24 | |
| 25 | #include "Signal.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | template <typename T> |
| 31 | class Task; |
| 32 | class TaskBase; |
| 33 | |
| 34 | template <typename T> |
| 35 | class TaskProcessor; |
| 36 | class TaskProcessorBase; |
| 37 | |
| 38 | class TaskManager { |
| 39 | public: |
| 40 | TaskManager(); |
| 41 | ~TaskManager(); |
| 42 | |
| 43 | /** |
| 44 | * Returns true if this task manager can run tasks, |
| 45 | * false otherwise. This method will typically return |
| 46 | * true on a single CPU core device. |
| 47 | */ |
| 48 | bool canRunTasks() const; |
| 49 | |
| 50 | private: |
| 51 | template <typename T> |
| 52 | friend class TaskProcessor; |
| 53 | |
| 54 | template<typename T> |
| 55 | bool addTask(const sp<Task<T> >& task, const sp<TaskProcessor<T> >& processor) { |
| 56 | return addTaskBase(sp<TaskBase>(task), sp<TaskProcessorBase>(processor)); |
| 57 | } |
| 58 | |
| 59 | bool addTaskBase(const sp<TaskBase>& task, const sp<TaskProcessorBase>& processor); |
| 60 | |
| 61 | struct TaskWrapper { |
| 62 | TaskWrapper(): mTask(), mProcessor() { } |
| 63 | |
| 64 | TaskWrapper(const sp<TaskBase>& task, const sp<TaskProcessorBase>& processor): |
| 65 | mTask(task), mProcessor(processor) { |
| 66 | } |
| 67 | |
| 68 | sp<TaskBase> mTask; |
| 69 | sp<TaskProcessorBase> mProcessor; |
| 70 | }; |
| 71 | |
| 72 | class WorkerThread: public Thread { |
| 73 | public: |
| 74 | WorkerThread(const String8 name): mSignal(Condition::WAKE_UP_ONE), mName(name) { } |
| 75 | |
| 76 | bool addTask(TaskWrapper task); |
| 77 | size_t getTaskCount() const; |
| 78 | void exit(); |
| 79 | |
| 80 | private: |
| 81 | virtual bool threadLoop(); |
| 82 | |
| 83 | // Lock for the list of tasks |
| 84 | mutable Mutex mLock; |
| 85 | Vector<TaskWrapper> mTasks; |
| 86 | |
| 87 | // Signal used to wake up the thread when a new |
| 88 | // task is available in the list |
| 89 | mutable Signal mSignal; |
| 90 | |
| 91 | const String8 mName; |
| 92 | }; |
| 93 | |
| 94 | Vector<sp<WorkerThread> > mThreads; |
| 95 | }; |
| 96 | |
| 97 | }; // namespace uirenderer |
| 98 | }; // namespace android |
| 99 | |
| 100 | #endif // ANDROID_HWUI_TASK_MANAGER_H |