Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / cc / debug / micro_benchmark_controller.cc
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 <limits>
8 #include <string>
9
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"
19
20 namespace cc {
21
22 int MicroBenchmarkController::next_id_ = 1;
23
24 namespace {
25
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));
39   }
40   return nullptr;
41 }
42
43 class IsDonePredicate {
44  public:
45   typedef const MicroBenchmark* argument_type;
46   typedef bool result_type;
47
48   result_type operator()(argument_type benchmark) const {
49     return benchmark->IsDone();
50   }
51 };
52
53 }  // namespace
54
55 MicroBenchmarkController::MicroBenchmarkController(LayerTreeHost* host)
56     : host_(host),
57       main_controller_message_loop_(base::MessageLoopProxy::current().get()) {
58   DCHECK(host_);
59 }
60
61 MicroBenchmarkController::~MicroBenchmarkController() {}
62
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;
75   }
76   return 0;
77 }
78
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;
85 }
86
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());
94   }
95   return false;
96 }
97
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_);
107     }
108
109     if (benchmark_impl.get())
110       host_impl->ScheduleMicroBenchmark(benchmark_impl.Pass());
111   }
112 }
113
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_);
120   }
121
122   CleanUpFinishedBenchmarks();
123 }
124
125 void MicroBenchmarkController::CleanUpFinishedBenchmarks() {
126   benchmarks_.erase(
127       benchmarks_.partition(std::not1(IsDonePredicate())),
128       benchmarks_.end());
129 }
130
131 }  // namespace cc