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/message_loop/message_loop_proxy.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 scoped_ptr
<MicroBenchmark
>(
32 new InvalidationBenchmark(value
.Pass(), callback
));
33 } else if (name
== "picture_record_benchmark") {
34 return scoped_ptr
<MicroBenchmark
>(
35 new PictureRecordBenchmark(value
.Pass(), callback
));
36 } else if (name
== "rasterize_and_record_benchmark") {
37 return scoped_ptr
<MicroBenchmark
>(
38 new RasterizeAndRecordBenchmark(value
.Pass(), callback
));
39 } else if (name
== "unittest_only_benchmark") {
40 return scoped_ptr
<MicroBenchmark
>(
41 new UnittestOnlyBenchmark(value
.Pass(), callback
));
43 return scoped_ptr
<MicroBenchmark
>();
46 class IsDonePredicate
{
48 typedef const MicroBenchmark
* argument_type
;
49 typedef bool result_type
;
51 result_type
operator()(argument_type benchmark
) const {
52 return benchmark
->IsDone();
58 MicroBenchmarkController::MicroBenchmarkController(LayerTreeHost
* host
)
60 main_controller_message_loop_(base::MessageLoopProxy::current().get()) {
64 MicroBenchmarkController::~MicroBenchmarkController() {}
66 int MicroBenchmarkController::ScheduleRun(
67 const std::string
& micro_benchmark_name
,
68 scoped_ptr
<base::Value
> value
,
69 const MicroBenchmark::DoneCallback
& callback
) {
70 scoped_ptr
<MicroBenchmark
> benchmark
=
71 CreateBenchmark(micro_benchmark_name
, value
.Pass(), callback
);
72 if (benchmark
.get()) {
73 int id
= GetNextIdAndIncrement();
74 benchmark
->set_id(id
);
75 benchmarks_
.push_back(benchmark
.Pass());
76 host_
->SetNeedsCommit();
82 int MicroBenchmarkController::GetNextIdAndIncrement() {
84 // Wrap around to 1 if we overflow (very unlikely).
85 if (next_id_
== std::numeric_limits
<int>::max())
90 bool MicroBenchmarkController::SendMessage(int id
,
91 scoped_ptr
<base::Value
> value
) {
92 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
93 it
!= benchmarks_
.end();
95 if ((*it
)->id() == id
)
96 return (*it
)->ProcessMessage(value
.Pass());
101 void MicroBenchmarkController::ScheduleImplBenchmarks(
102 LayerTreeHostImpl
* host_impl
) {
103 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
104 it
!= benchmarks_
.end();
106 scoped_ptr
<MicroBenchmarkImpl
> benchmark_impl
;
107 if (!(*it
)->ProcessedForBenchmarkImpl()) {
109 (*it
)->GetBenchmarkImpl(main_controller_message_loop_
);
112 if (benchmark_impl
.get())
113 host_impl
->ScheduleMicroBenchmark(benchmark_impl
.Pass());
117 void MicroBenchmarkController::DidUpdateLayers() {
118 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
119 it
!= benchmarks_
.end();
121 if (!(*it
)->IsDone())
122 (*it
)->DidUpdateLayers(host_
);
125 CleanUpFinishedBenchmarks();
128 void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
130 benchmarks_
.partition(std::not1(IsDonePredicate())),