Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / cc / debug / micro_benchmark_controller.cc
bloba38e5ad01a31b7116c3b507e7348e6b8dde62078
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/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"
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_task_runner_(base::ThreadTaskRunnerHandle::IsSet()
58 ? base::ThreadTaskRunnerHandle::Get()
59 : nullptr) {
60 DCHECK(host_);
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();
76 return id;
78 return 0;
81 int MicroBenchmarkController::GetNextIdAndIncrement() {
82 int id = next_id_++;
83 // Wrap around to 1 if we overflow (very unlikely).
84 if (next_id_ == std::numeric_limits<int>::max())
85 next_id_ = 1;
86 return id;
89 bool MicroBenchmarkController::SendMessage(int id,
90 scoped_ptr<base::Value> value) {
91 for (ScopedPtrVector<MicroBenchmark>::iterator it = benchmarks_.begin();
92 it != benchmarks_.end();
93 ++it) {
94 if ((*it)->id() == id)
95 return (*it)->ProcessMessage(value.Pass());
97 return false;
100 void MicroBenchmarkController::ScheduleImplBenchmarks(
101 LayerTreeHostImpl* host_impl) {
102 for (ScopedPtrVector<MicroBenchmark>::iterator it = benchmarks_.begin();
103 it != benchmarks_.end();
104 ++it) {
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();
118 ++it) {
119 if (!(*it)->IsDone())
120 (*it)->DidUpdateLayers(host_);
123 CleanUpFinishedBenchmarks();
126 void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
127 benchmarks_.erase(
128 benchmarks_.partition(std::not1(IsDonePredicate())),
129 benchmarks_.end());
132 } // namespace cc