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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
// 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/resources/raster_worker_pool.h"
#include <algorithm>
#include "base/debug/trace_event_synthetic_delay.h"
#include "base/lazy_instance.h"
#include "base/strings/stringprintf.h"
#include "base/threading/simple_thread.h"
#include "cc/base/scoped_ptr_deque.h"
namespace cc {
namespace {
class RasterTaskGraphRunner : public TaskGraphRunner,
public base::DelegateSimpleThread::Delegate {
public:
RasterTaskGraphRunner()
: synthetic_delay_(base::debug::TraceEventSyntheticDelay::Lookup(
"cc.RasterRequiredForActivation")) {
size_t num_threads = RasterWorkerPool::GetNumRasterThreads();
while (workers_.size() < num_threads) {
scoped_ptr<base::DelegateSimpleThread> worker =
make_scoped_ptr(new base::DelegateSimpleThread(
this,
base::StringPrintf("CompositorRasterWorker%u",
static_cast<unsigned>(workers_.size() + 1))
.c_str()));
worker->Start();
#if defined(OS_ANDROID) || defined(OS_LINUX)
worker->SetThreadPriority(base::kThreadPriority_Background);
#endif
workers_.push_back(worker.Pass());
}
}
virtual ~RasterTaskGraphRunner() { NOTREACHED(); }
base::debug::TraceEventSyntheticDelay* synthetic_delay() {
return synthetic_delay_;
}
private:
// Overridden from base::DelegateSimpleThread::Delegate:
virtual void Run() OVERRIDE {
TaskGraphRunner::Run();
}
ScopedPtrDeque<base::DelegateSimpleThread> workers_;
base::debug::TraceEventSyntheticDelay* synthetic_delay_;
};
base::LazyInstance<RasterTaskGraphRunner>::Leaky g_task_graph_runner =
LAZY_INSTANCE_INITIALIZER;
const int kDefaultNumRasterThreads = 1;
int g_num_raster_threads = 0;
class RasterFinishedTaskImpl : public RasterizerTask {
public:
explicit RasterFinishedTaskImpl(
base::SequencedTaskRunner* task_runner,
const base::Closure& on_raster_finished_callback)
: task_runner_(task_runner),
on_raster_finished_callback_(on_raster_finished_callback) {}
// Overridden from Task:
virtual void RunOnWorkerThread() OVERRIDE {
TRACE_EVENT0("cc", "RasterFinishedTaskImpl::RunOnWorkerThread");
RasterFinished();
}
// Overridden from RasterizerTask:
virtual void ScheduleOnOriginThread(RasterizerTaskClient* client) OVERRIDE {}
virtual void CompleteOnOriginThread(RasterizerTaskClient* client) OVERRIDE {}
virtual void RunReplyOnOriginThread() OVERRIDE {}
protected:
virtual ~RasterFinishedTaskImpl() {}
void RasterFinished() {
task_runner_->PostTask(FROM_HERE, on_raster_finished_callback_);
}
private:
scoped_refptr<base::SequencedTaskRunner> task_runner_;
const base::Closure on_raster_finished_callback_;
DISALLOW_COPY_AND_ASSIGN(RasterFinishedTaskImpl);
};
class RasterRequiredForActivationFinishedTaskImpl
: public RasterFinishedTaskImpl {
public:
RasterRequiredForActivationFinishedTaskImpl(
base::SequencedTaskRunner* task_runner,
const base::Closure& on_raster_finished_callback,
size_t tasks_required_for_activation_count)
: RasterFinishedTaskImpl(task_runner, on_raster_finished_callback),
tasks_required_for_activation_count_(
tasks_required_for_activation_count) {
if (tasks_required_for_activation_count_) {
g_task_graph_runner.Get().synthetic_delay()->BeginParallel(
&activation_delay_end_time_);
}
}
// Overridden from Task:
virtual void RunOnWorkerThread() OVERRIDE {
TRACE_EVENT0(
"cc", "RasterRequiredForActivationFinishedTaskImpl::RunOnWorkerThread");
if (tasks_required_for_activation_count_) {
g_task_graph_runner.Get().synthetic_delay()->EndParallel(
activation_delay_end_time_);
}
RasterFinished();
}
private:
virtual ~RasterRequiredForActivationFinishedTaskImpl() {}
base::TimeTicks activation_delay_end_time_;
const size_t tasks_required_for_activation_count_;
DISALLOW_COPY_AND_ASSIGN(RasterRequiredForActivationFinishedTaskImpl);
};
} // namespace
// This allows an external rasterize on-demand system to run raster tasks
// with highest priority using the same task graph runner instance.
unsigned RasterWorkerPool::kOnDemandRasterTaskPriority = 0u;
// This allows a micro benchmark system to run tasks with highest priority,
// since it should finish as quickly as possible.
unsigned RasterWorkerPool::kBenchmarkRasterTaskPriority = 0u;
// Task priorities that make sure raster finished tasks run before any
// remaining raster tasks.
unsigned RasterWorkerPool::kRasterFinishedTaskPriority = 2u;
unsigned RasterWorkerPool::kRasterRequiredForActivationFinishedTaskPriority =
1u;
unsigned RasterWorkerPool::kRasterTaskPriorityBase = 3u;
RasterWorkerPool::RasterWorkerPool() {}
RasterWorkerPool::~RasterWorkerPool() {}
// static
void RasterWorkerPool::SetNumRasterThreads(int num_threads) {
DCHECK_LT(0, num_threads);
DCHECK_EQ(0, g_num_raster_threads);
g_num_raster_threads = num_threads;
}
// static
int RasterWorkerPool::GetNumRasterThreads() {
if (!g_num_raster_threads)
g_num_raster_threads = kDefaultNumRasterThreads;
return g_num_raster_threads;
}
// static
TaskGraphRunner* RasterWorkerPool::GetTaskGraphRunner() {
return g_task_graph_runner.Pointer();
}
// static
scoped_refptr<RasterizerTask> RasterWorkerPool::CreateRasterFinishedTask(
base::SequencedTaskRunner* task_runner,
const base::Closure& on_raster_finished_callback) {
return make_scoped_refptr(
new RasterFinishedTaskImpl(task_runner, on_raster_finished_callback));
}
// static
scoped_refptr<RasterizerTask>
RasterWorkerPool::CreateRasterRequiredForActivationFinishedTask(
size_t tasks_required_for_activation_count,
base::SequencedTaskRunner* task_runner,
const base::Closure& on_raster_finished_callback) {
return make_scoped_refptr(new RasterRequiredForActivationFinishedTaskImpl(
task_runner,
on_raster_finished_callback,
tasks_required_for_activation_count));
}
// static
void RasterWorkerPool::ScheduleTasksOnOriginThread(RasterizerTaskClient* client,
TaskGraph* graph) {
TRACE_EVENT0("cc", "Rasterizer::ScheduleTasksOnOriginThread");
for (TaskGraph::Node::Vector::iterator it = graph->nodes.begin();
it != graph->nodes.end();
++it) {
TaskGraph::Node& node = *it;
RasterizerTask* task = static_cast<RasterizerTask*>(node.task);
if (!task->HasBeenScheduled()) {
task->WillSchedule();
task->ScheduleOnOriginThread(client);
task->DidSchedule();
}
}
}
// static
void RasterWorkerPool::InsertNodeForTask(TaskGraph* graph,
RasterizerTask* task,
unsigned priority,
size_t dependencies) {
DCHECK(std::find_if(graph->nodes.begin(),
graph->nodes.end(),
TaskGraph::Node::TaskComparator(task)) ==
graph->nodes.end());
graph->nodes.push_back(TaskGraph::Node(task, priority, dependencies));
}
// static
void RasterWorkerPool::InsertNodesForRasterTask(
TaskGraph* graph,
RasterTask* raster_task,
const ImageDecodeTask::Vector& decode_tasks,
unsigned priority) {
size_t dependencies = 0u;
// Insert image decode tasks.
for (ImageDecodeTask::Vector::const_iterator it = decode_tasks.begin();
it != decode_tasks.end();
++it) {
ImageDecodeTask* decode_task = it->get();
// Skip if already decoded.
if (decode_task->HasCompleted())
continue;
dependencies++;
// Add decode task if it doesn't already exists in graph.
TaskGraph::Node::Vector::iterator decode_it =
std::find_if(graph->nodes.begin(),
graph->nodes.end(),
TaskGraph::Node::TaskComparator(decode_task));
if (decode_it == graph->nodes.end())
InsertNodeForTask(graph, decode_task, priority, 0u);
graph->edges.push_back(TaskGraph::Edge(decode_task, raster_task));
}
InsertNodeForTask(graph, raster_task, priority, dependencies);
}
} // namespace cc
|