1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/worker_pool.h"
#include <algorithm>
#include "base/bind.h"
#include "base/stl_util.h"
#include "base/stringprintf.h"
#if defined(OS_ANDROID)
// TODO(epenner): Move thread priorities to base. (crbug.com/170549)
#include <sys/resource.h>
#endif
namespace cc {
namespace {
class WorkerPoolTaskImpl : public internal::WorkerPoolTask {
public:
WorkerPoolTaskImpl(const WorkerPool::Callback& task,
const base::Closure& reply)
: internal::WorkerPoolTask(reply),
task_(task) {}
virtual void Run(RenderingStats* rendering_stats) OVERRIDE {
task_.Run(rendering_stats);
}
private:
WorkerPool::Callback task_;
};
const char* kWorkerThreadNamePrefix = "Compositor";
// Allow two pending tasks per worker. This keeps resource usage
// low while making sure workers aren't unnecessarily idle.
const int kNumPendingTasksPerWorker = 2;
} // namespace
namespace internal {
WorkerPoolTask::WorkerPoolTask(const base::Closure& reply)
: reply_(reply) {
}
WorkerPoolTask::~WorkerPoolTask() {
}
void WorkerPoolTask::Completed() {
reply_.Run();
}
} // namespace internal
WorkerPool::Worker::Worker(
WorkerPool* worker_pool,
const std::string name,
scoped_ptr<RenderingStats> rendering_stats)
: base::Thread(name.c_str()),
worker_pool_(worker_pool),
weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
rendering_stats_(rendering_stats.Pass()) {
Start();
DCHECK(IsRunning());
}
WorkerPool::Worker::~Worker() {
DCHECK(!IsRunning());
DCHECK_EQ(pending_tasks_.size(), 0);
}
void WorkerPool::Worker::StopAfterCompletingAllPendingTasks() {
// Signals the thread to exit and returns once all pending tasks have run.
Stop();
// Complete all pending tasks. The Stop() call above guarantees that
// all tasks have finished running.
while (!pending_tasks_.empty())
OnTaskCompleted();
// Cancel all pending replies.
weak_ptr_factory_.InvalidateWeakPtrs();
}
void WorkerPool::Worker::PostTask(scoped_ptr<internal::WorkerPoolTask> task) {
DCHECK_LT(num_pending_tasks(), kNumPendingTasksPerWorker);
message_loop_proxy()->PostTaskAndReply(
FROM_HERE,
base::Bind(&Worker::RunTask,
base::Unretained(task.get()),
base::Unretained(rendering_stats_.get())),
base::Bind(&Worker::OnTaskCompleted, weak_ptr_factory_.GetWeakPtr()));
pending_tasks_.push_back(task.Pass());
worker_pool_->DidNumPendingTasksChange();
}
void WorkerPool::Worker::Init() {
#if defined(OS_ANDROID)
// TODO(epenner): Move thread priorities to base. (crbug.com/170549)
int nice_value = 10; // Idle priority.
setpriority(PRIO_PROCESS, base::PlatformThread::CurrentId(), nice_value);
#endif
}
// static
void WorkerPool::Worker::RunTask(
internal::WorkerPoolTask* task, RenderingStats* rendering_stats) {
task->Run(rendering_stats);
}
void WorkerPool::Worker::OnTaskCompleted() {
CHECK(!pending_tasks_.empty());
scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front();
task->Completed();
worker_pool_->DidNumPendingTasksChange();
}
WorkerPool::WorkerPool(size_t num_threads, bool record_rendering_stats)
: workers_need_sorting_(false),
shutdown_(false) {
const std::string thread_name_prefix = kWorkerThreadNamePrefix;
while (workers_.size() < num_threads) {
int thread_number = workers_.size() + 1;
scoped_ptr<RenderingStats> rendering_stats = record_rendering_stats ?
make_scoped_ptr(new RenderingStats) : scoped_ptr<RenderingStats>();
workers_.push_back(new Worker(
this,
thread_name_prefix + StringPrintf("Worker%d", thread_number).c_str(),
rendering_stats.Pass()));
}
}
WorkerPool::~WorkerPool() {
Shutdown();
STLDeleteElements(&workers_);
}
void WorkerPool::Shutdown() {
DCHECK(!shutdown_);
shutdown_ = true;
for (WorkerVector::iterator it = workers_.begin();
it != workers_.end(); it++) {
Worker* worker = *it;
worker->StopAfterCompletingAllPendingTasks();
}
}
void WorkerPool::PostTaskAndReply(
const Callback& task, const base::Closure& reply) {
Worker* worker = GetWorkerForNextTask();
worker->PostTask(
make_scoped_ptr(new WorkerPoolTaskImpl(
task,
reply)).PassAs<internal::WorkerPoolTask>());
}
bool WorkerPool::IsBusy() {
Worker* worker = GetWorkerForNextTask();
return worker->num_pending_tasks() >= kNumPendingTasksPerWorker;
}
void WorkerPool::GetRenderingStats(RenderingStats* stats) {
stats->totalRasterizeTime = base::TimeDelta();
stats->totalPixelsRasterized = 0;
stats->totalDeferredImageDecodeCount = 0;
stats->totalDeferredImageDecodeTime = base::TimeDelta();
for (WorkerVector::iterator it = workers_.begin();
it != workers_.end(); ++it) {
Worker* worker = *it;
CHECK(worker->rendering_stats());
stats->totalRasterizeTime +=
worker->rendering_stats()->totalRasterizeTime;
stats->totalPixelsRasterized +=
worker->rendering_stats()->totalPixelsRasterized;
stats->totalDeferredImageDecodeCount +=
worker->rendering_stats()->totalDeferredImageDecodeCount;
stats->totalDeferredImageDecodeTime +=
worker->rendering_stats()->totalDeferredImageDecodeTime;
}
}
WorkerPool::Worker* WorkerPool::GetWorkerForNextTask() {
CHECK(!shutdown_);
SortWorkersIfNeeded();
return workers_.front();
}
void WorkerPool::DidNumPendingTasksChange() {
workers_need_sorting_ = true;
}
void WorkerPool::SortWorkersIfNeeded() {
if (!workers_need_sorting_)
return;
std::sort(workers_.begin(), workers_.end(), NumPendingTasksComparator());
workers_need_sorting_ = false;
}
} // namespace cc
|