• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "cc/debug/micro_benchmark_controller.h"
6 
7 #include <string>
8 
9 #include "base/callback.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/values.h"
12 #include "cc/debug/picture_record_benchmark.h"
13 #include "cc/debug/rasterize_and_record_benchmark.h"
14 #include "cc/debug/unittest_only_benchmark.h"
15 #include "cc/trees/layer_tree_host.h"
16 #include "cc/trees/layer_tree_host_impl.h"
17 
18 namespace cc {
19 
20 namespace {
21 
CreateBenchmark(const std::string & name,scoped_ptr<base::Value> value,const MicroBenchmark::DoneCallback & callback)22 scoped_ptr<MicroBenchmark> CreateBenchmark(
23     const std::string& name,
24     scoped_ptr<base::Value> value,
25     const MicroBenchmark::DoneCallback& callback) {
26   if (name == "picture_record_benchmark") {
27     return scoped_ptr<MicroBenchmark>(
28         new PictureRecordBenchmark(value.Pass(), callback));
29   } else if (name == "rasterize_and_record_benchmark") {
30     return scoped_ptr<MicroBenchmark>(
31         new RasterizeAndRecordBenchmark(value.Pass(), callback));
32   } else if (name == "unittest_only_benchmark") {
33     return scoped_ptr<MicroBenchmark>(
34         new UnittestOnlyBenchmark(value.Pass(), callback));
35   }
36   return scoped_ptr<MicroBenchmark>();
37 }
38 
39 class IsDonePredicate {
40  public:
41   typedef const MicroBenchmark* argument_type;
42   typedef bool result_type;
43 
operator ()(argument_type benchmark) const44   result_type operator()(argument_type benchmark) const {
45     return benchmark->IsDone();
46   }
47 };
48 
49 }  // namespace
50 
MicroBenchmarkController(LayerTreeHost * host)51 MicroBenchmarkController::MicroBenchmarkController(LayerTreeHost* host)
52     : host_(host),
53       main_controller_message_loop_(base::MessageLoopProxy::current().get()) {
54   DCHECK(host_);
55 }
56 
~MicroBenchmarkController()57 MicroBenchmarkController::~MicroBenchmarkController() {}
58 
ScheduleRun(const std::string & micro_benchmark_name,scoped_ptr<base::Value> value,const MicroBenchmark::DoneCallback & callback)59 bool MicroBenchmarkController::ScheduleRun(
60     const std::string& micro_benchmark_name,
61     scoped_ptr<base::Value> value,
62     const MicroBenchmark::DoneCallback& callback) {
63   scoped_ptr<MicroBenchmark> benchmark =
64       CreateBenchmark(micro_benchmark_name, value.Pass(), callback);
65   if (benchmark.get()) {
66     benchmarks_.push_back(benchmark.Pass());
67     host_->SetNeedsCommit();
68     return true;
69   }
70   return false;
71 }
72 
ScheduleImplBenchmarks(LayerTreeHostImpl * host_impl)73 void MicroBenchmarkController::ScheduleImplBenchmarks(
74     LayerTreeHostImpl* host_impl) {
75   for (ScopedPtrVector<MicroBenchmark>::iterator it = benchmarks_.begin();
76        it != benchmarks_.end();
77        ++it) {
78     scoped_ptr<MicroBenchmarkImpl> benchmark_impl;
79     if (!(*it)->ProcessedForBenchmarkImpl()) {
80       benchmark_impl =
81           (*it)->GetBenchmarkImpl(main_controller_message_loop_);
82     }
83 
84     if (benchmark_impl.get())
85       host_impl->ScheduleMicroBenchmark(benchmark_impl.Pass());
86   }
87 }
88 
DidUpdateLayers()89 void MicroBenchmarkController::DidUpdateLayers() {
90   for (ScopedPtrVector<MicroBenchmark>::iterator it = benchmarks_.begin();
91        it != benchmarks_.end();
92        ++it) {
93     if (!(*it)->IsDone())
94       (*it)->DidUpdateLayers(host_);
95   }
96 
97   CleanUpFinishedBenchmarks();
98 }
99 
CleanUpFinishedBenchmarks()100 void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
101   benchmarks_.erase(
102       benchmarks_.partition(std::not1(IsDonePredicate())),
103       benchmarks_.end());
104 }
105 
106 }  // namespace cc
107