Wrapper script for isolating telemetry_gpu_unittests.
[chromium-blink-merge.git] / cc / debug / invalidation_benchmark.cc
blob8a9fc883224829329f72d473a20fc6f708fd0892
1 // Copyright 2014 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/invalidation_benchmark.h"
7 #include <algorithm>
8 #include <limits>
10 #include "base/basictypes.h"
11 #include "base/rand_util.h"
12 #include "base/values.h"
13 #include "cc/layers/layer.h"
14 #include "cc/layers/picture_layer.h"
15 #include "cc/trees/layer_tree_host.h"
16 #include "cc/trees/layer_tree_host_common.h"
17 #include "ui/gfx/geometry/rect.h"
19 namespace cc {
21 namespace {
23 const char* kDefaultInvalidationMode = "viewport";
25 } // namespace
27 InvalidationBenchmark::InvalidationBenchmark(
28 scoped_ptr<base::Value> value,
29 const MicroBenchmark::DoneCallback& callback)
30 : MicroBenchmark(callback), seed_(0) {
31 base::DictionaryValue* settings = nullptr;
32 value->GetAsDictionary(&settings);
33 if (!settings)
34 return;
36 std::string mode_string = kDefaultInvalidationMode;
38 if (settings->HasKey("mode"))
39 settings->GetString("mode", &mode_string);
41 if (mode_string == "fixed_size") {
42 mode_ = FIXED_SIZE;
43 CHECK(settings->HasKey("width"))
44 << "Must provide a width for fixed_size mode.";
45 CHECK(settings->HasKey("height"))
46 << "Must provide a height for fixed_size mode.";
47 settings->GetInteger("width", &width_);
48 settings->GetInteger("height", &height_);
49 } else if (mode_string == "layer") {
50 mode_ = LAYER;
51 } else if (mode_string == "random") {
52 mode_ = RANDOM;
53 } else if (mode_string == "viewport") {
54 mode_ = VIEWPORT;
55 } else {
56 CHECK(false) << "Invalid mode: " << mode_string
57 << ". One of {fixed_size, layer, viewport, random} expected.";
61 InvalidationBenchmark::~InvalidationBenchmark() {
64 void InvalidationBenchmark::DidUpdateLayers(LayerTreeHost* host) {
65 LayerTreeHostCommon::CallFunctionForSubtree(
66 host->root_layer(),
67 [this](Layer* layer) { layer->RunMicroBenchmark(this); });
70 void InvalidationBenchmark::RunOnLayer(PictureLayer* layer) {
71 switch (mode_) {
72 case FIXED_SIZE: {
73 // Invalidation with a random position and fixed size.
74 gfx::Rect visible_layer_rect = layer->visible_layer_rect();
75 int x = LCGRandom() * (visible_layer_rect.width() - width_);
76 int y = LCGRandom() * (visible_layer_rect.height() - height_);
77 gfx::Rect invalidation_rect(x, y, width_, height_);
78 layer->SetNeedsDisplayRect(invalidation_rect);
79 break;
81 case LAYER: {
82 // Invalidate entire layer.
83 layer->SetNeedsDisplay();
84 break;
86 case RANDOM: {
87 // Random invalidation inside the viewport.
88 gfx::Rect visible_layer_rect = layer->visible_layer_rect();
89 int x_min = LCGRandom() * visible_layer_rect.width();
90 int x_max = LCGRandom() * visible_layer_rect.width();
91 int y_min = LCGRandom() * visible_layer_rect.height();
92 int y_max = LCGRandom() * visible_layer_rect.height();
93 if (x_min > x_max)
94 std::swap(x_min, x_max);
95 if (y_min > y_max)
96 std::swap(y_min, y_max);
97 gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min);
98 layer->SetNeedsDisplayRect(invalidation_rect);
99 break;
101 case VIEWPORT: {
102 // Invalidate entire viewport.
103 layer->SetNeedsDisplayRect(layer->visible_layer_rect());
104 break;
109 bool InvalidationBenchmark::ProcessMessage(scoped_ptr<base::Value> value) {
110 base::DictionaryValue* message = nullptr;
111 value->GetAsDictionary(&message);
112 if (!message)
113 return false;
115 bool notify_done;
116 if (message->HasKey("notify_done")) {
117 message->GetBoolean("notify_done", &notify_done);
118 if (notify_done)
119 NotifyDone(base::Value::CreateNullValue());
120 return true;
122 return false;
125 // A simple linear congruential generator. The random numbers don't need to be
126 // high quality, but they need to be identical in each run. Therefore, we use a
127 // LCG and keep the state locally in the benchmark.
128 float InvalidationBenchmark::LCGRandom() {
129 const uint32 a = 1664525;
130 const uint32 c = 1013904223;
131 seed_ = a * seed_ + c;
132 return static_cast<float>(seed_) / std::numeric_limits<uint32>::max();
135 } // namespace cc