Refactor to use closure in call to sort().
[chromium-blink-merge.git] / cc / debug / micro_benchmark_controller.cc
blobbf98100541921966b6b236ad869c7a169ecc1253
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"
7 #include <limits>
8 #include <string>
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"
20 namespace cc {
22 int MicroBenchmarkController::next_id_ = 1;
24 namespace {
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));
40 return nullptr;
43 class IsDonePredicate {
44 public:
45 typedef const MicroBenchmark* argument_type;
46 typedef bool result_type;
48 result_type operator()(argument_type benchmark) const {
49 return benchmark->IsDone();
53 } // namespace
55 MicroBenchmarkController::MicroBenchmarkController(LayerTreeHost* host)
56 : host_(host),
57 main_controller_message_loop_(base::MessageLoopProxy::current().get()) {
58 DCHECK(host_);
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();
74 return id;
76 return 0;
79 int MicroBenchmarkController::GetNextIdAndIncrement() {
80 int id = next_id_++;
81 // Wrap around to 1 if we overflow (very unlikely).
82 if (next_id_ == std::numeric_limits<int>::max())
83 next_id_ = 1;
84 return id;
87 bool MicroBenchmarkController::SendMessage(int id,
88 scoped_ptr<base::Value> value) {
89 for (ScopedPtrVector<MicroBenchmark>::iterator it = benchmarks_.begin();
90 it != benchmarks_.end();
91 ++it) {
92 if ((*it)->id() == id)
93 return (*it)->ProcessMessage(value.Pass());
95 return false;
98 void MicroBenchmarkController::ScheduleImplBenchmarks(
99 LayerTreeHostImpl* host_impl) {
100 for (ScopedPtrVector<MicroBenchmark>::iterator it = benchmarks_.begin();
101 it != benchmarks_.end();
102 ++it) {
103 scoped_ptr<MicroBenchmarkImpl> benchmark_impl;
104 if (!(*it)->ProcessedForBenchmarkImpl()) {
105 benchmark_impl =
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();
117 ++it) {
118 if (!(*it)->IsDone())
119 (*it)->DidUpdateLayers(host_);
122 CleanUpFinishedBenchmarks();
125 void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
126 benchmarks_.erase(
127 benchmarks_.partition(std::not1(IsDonePredicate())),
128 benchmarks_.end());
131 } // namespace cc