1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/debug/micro_benchmark_controller.h"
10 #include "base/callback.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/values.h"
13 #include "cc/debug/invalidation_benchmark.h"
14 #include "cc/debug/picture_record_benchmark.h"
15 #include "cc/debug/rasterize_and_record_benchmark.h"
16 #include "cc/debug/unittest_only_benchmark.h"
17 #include "cc/trees/layer_tree_host.h"
18 #include "cc/trees/layer_tree_host_impl.h"
22 int MicroBenchmarkController::next_id_
= 1;
26 scoped_ptr
<MicroBenchmark
> CreateBenchmark(
27 const std::string
& name
,
28 scoped_ptr
<base::Value
> value
,
29 const MicroBenchmark::DoneCallback
& callback
) {
30 if (name
== "invalidation_benchmark") {
31 return make_scoped_ptr(new InvalidationBenchmark(value
.Pass(), callback
));
32 } else if (name
== "picture_record_benchmark") {
33 return make_scoped_ptr(new PictureRecordBenchmark(value
.Pass(), callback
));
34 } else if (name
== "rasterize_and_record_benchmark") {
35 return make_scoped_ptr(
36 new RasterizeAndRecordBenchmark(value
.Pass(), callback
));
37 } else if (name
== "unittest_only_benchmark") {
38 return make_scoped_ptr(new UnittestOnlyBenchmark(value
.Pass(), callback
));
43 class IsDonePredicate
{
45 typedef const MicroBenchmark
* argument_type
;
46 typedef bool result_type
;
48 result_type
operator()(argument_type benchmark
) const {
49 return benchmark
->IsDone();
55 MicroBenchmarkController::MicroBenchmarkController(LayerTreeHost
* host
)
57 main_controller_task_runner_(base::ThreadTaskRunnerHandle::IsSet()
58 ? base::ThreadTaskRunnerHandle::Get()
63 MicroBenchmarkController::~MicroBenchmarkController() {}
65 int MicroBenchmarkController::ScheduleRun(
66 const std::string
& micro_benchmark_name
,
67 scoped_ptr
<base::Value
> value
,
68 const MicroBenchmark::DoneCallback
& callback
) {
69 scoped_ptr
<MicroBenchmark
> benchmark
=
70 CreateBenchmark(micro_benchmark_name
, value
.Pass(), callback
);
71 if (benchmark
.get()) {
72 int id
= GetNextIdAndIncrement();
73 benchmark
->set_id(id
);
74 benchmarks_
.push_back(benchmark
.Pass());
75 host_
->SetNeedsCommit();
81 int MicroBenchmarkController::GetNextIdAndIncrement() {
83 // Wrap around to 1 if we overflow (very unlikely).
84 if (next_id_
== std::numeric_limits
<int>::max())
89 bool MicroBenchmarkController::SendMessage(int id
,
90 scoped_ptr
<base::Value
> value
) {
91 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
92 it
!= benchmarks_
.end();
94 if ((*it
)->id() == id
)
95 return (*it
)->ProcessMessage(value
.Pass());
100 void MicroBenchmarkController::ScheduleImplBenchmarks(
101 LayerTreeHostImpl
* host_impl
) {
102 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
103 it
!= benchmarks_
.end();
105 scoped_ptr
<MicroBenchmarkImpl
> benchmark_impl
;
106 if (!(*it
)->ProcessedForBenchmarkImpl()) {
107 benchmark_impl
= (*it
)->GetBenchmarkImpl(main_controller_task_runner_
);
110 if (benchmark_impl
.get())
111 host_impl
->ScheduleMicroBenchmark(benchmark_impl
.Pass());
115 void MicroBenchmarkController::DidUpdateLayers() {
116 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
117 it
!= benchmarks_
.end();
119 if (!(*it
)->IsDone())
120 (*it
)->DidUpdateLayers(host_
);
123 CleanUpFinishedBenchmarks();
126 void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
128 benchmarks_
.partition(std::not1(IsDonePredicate())),