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 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_message_loop_(base::MessageLoopProxy::current().get()) {
61 MicroBenchmarkController::~MicroBenchmarkController() {}
63 int MicroBenchmarkController::ScheduleRun(
64 const std::string
& micro_benchmark_name
,
65 scoped_ptr
<base::Value
> value
,
66 const MicroBenchmark::DoneCallback
& callback
) {
67 scoped_ptr
<MicroBenchmark
> benchmark
=
68 CreateBenchmark(micro_benchmark_name
, value
.Pass(), callback
);
69 if (benchmark
.get()) {
70 int id
= GetNextIdAndIncrement();
71 benchmark
->set_id(id
);
72 benchmarks_
.push_back(benchmark
.Pass());
73 host_
->SetNeedsCommit();
79 int MicroBenchmarkController::GetNextIdAndIncrement() {
81 // Wrap around to 1 if we overflow (very unlikely).
82 if (next_id_
== std::numeric_limits
<int>::max())
87 bool MicroBenchmarkController::SendMessage(int id
,
88 scoped_ptr
<base::Value
> value
) {
89 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
90 it
!= benchmarks_
.end();
92 if ((*it
)->id() == id
)
93 return (*it
)->ProcessMessage(value
.Pass());
98 void MicroBenchmarkController::ScheduleImplBenchmarks(
99 LayerTreeHostImpl
* host_impl
) {
100 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
101 it
!= benchmarks_
.end();
103 scoped_ptr
<MicroBenchmarkImpl
> benchmark_impl
;
104 if (!(*it
)->ProcessedForBenchmarkImpl()) {
106 (*it
)->GetBenchmarkImpl(main_controller_message_loop_
);
109 if (benchmark_impl
.get())
110 host_impl
->ScheduleMicroBenchmark(benchmark_impl
.Pass());
114 void MicroBenchmarkController::DidUpdateLayers() {
115 for (ScopedPtrVector
<MicroBenchmark
>::iterator it
= benchmarks_
.begin();
116 it
!= benchmarks_
.end();
118 if (!(*it
)->IsDone())
119 (*it
)->DidUpdateLayers(host_
);
122 CleanUpFinishedBenchmarks();
125 void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
127 benchmarks_
.partition(std::not1(IsDonePredicate())),