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