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> |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 23 | |
| 24 | #include "Signal.h" |
| 25 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | template <typename T> |
| 32 | class Task; |
| 33 | class TaskBase; |
| 34 | |
| 35 | template <typename T> |
| 36 | class TaskProcessor; |
| 37 | class TaskProcessorBase; |
| 38 | |
| 39 | class TaskManager { |
| 40 | public: |
| 41 | TaskManager(); |
| 42 | ~TaskManager(); |
| 43 | |
| 44 | /** |
| 45 | * Returns true if this task manager can run tasks, |
| 46 | * false otherwise. This method will typically return |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 47 | * false on a single CPU core device. |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 48 | */ |
| 49 | bool canRunTasks() const; |
| 50 | |
Romain Guy | c5cbee7 | 2013-03-20 19:15:02 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Stops all allocated threads. Adding tasks will start |
| 53 | * the threads again as necessary. |
| 54 | */ |
| 55 | void stop(); |
| 56 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 57 | private: |
| 58 | template <typename T> |
| 59 | friend class TaskProcessor; |
| 60 | |
| 61 | template<typename T> |
| 62 | bool addTask(const sp<Task<T> >& task, const sp<TaskProcessor<T> >& processor) { |
| 63 | return addTaskBase(sp<TaskBase>(task), sp<TaskProcessorBase>(processor)); |
| 64 | } |
| 65 | |
| 66 | bool addTaskBase(const sp<TaskBase>& task, const sp<TaskProcessorBase>& processor); |
| 67 | |
| 68 | struct TaskWrapper { |
| 69 | TaskWrapper(): mTask(), mProcessor() { } |
| 70 | |
| 71 | TaskWrapper(const sp<TaskBase>& task, const sp<TaskProcessorBase>& processor): |
| 72 | mTask(task), mProcessor(processor) { |
| 73 | } |
| 74 | |
| 75 | sp<TaskBase> mTask; |
| 76 | sp<TaskProcessorBase> mProcessor; |
| 77 | }; |
| 78 | |
| 79 | class WorkerThread: public Thread { |
| 80 | public: |
| 81 | WorkerThread(const String8 name): mSignal(Condition::WAKE_UP_ONE), mName(name) { } |
| 82 | |
| 83 | bool addTask(TaskWrapper task); |
| 84 | size_t getTaskCount() const; |
| 85 | void exit(); |
| 86 | |
| 87 | private: |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 88 | virtual status_t readyToRun() override; |
| 89 | virtual bool threadLoop() override; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 90 | |
| 91 | // Lock for the list of tasks |
| 92 | mutable Mutex mLock; |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 93 | std::vector<TaskWrapper> mTasks; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 94 | |
| 95 | // Signal used to wake up the thread when a new |
| 96 | // task is available in the list |
| 97 | mutable Signal mSignal; |
| 98 | |
| 99 | const String8 mName; |
| 100 | }; |
| 101 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 102 | std::vector<sp<WorkerThread> > mThreads; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | }; // namespace uirenderer |
| 106 | }; // namespace android |
| 107 | |
| 108 | #endif // ANDROID_HWUI_TASK_MANAGER_H |