Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "cc/debug/invalidation_benchmark.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <limits> | |
| 9 | |
| 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/rect.h" | |
| 18 | |
| 19 namespace cc { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char* kDefaultInvalidationMode = "viewport"; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 InvalidationBenchmark::InvalidationBenchmark( | |
| 28 scoped_ptr<base::Value> value, | |
| 29 const MicroBenchmark::DoneCallback& callback) | |
| 30 : MicroBenchmark(callback), seed_(0) { | |
| 31 base::DictionaryValue* settings = NULL; | |
| 32 value->GetAsDictionary(&settings); | |
| 33 if (!settings) | |
| 34 return; | |
| 35 | |
| 36 std::string mode_string = kDefaultInvalidationMode; | |
| 37 | |
| 38 if (settings->HasKey("mode")) | |
| 39 settings->GetString("mode", &mode_string); | |
| 40 | |
| 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 {layer, viewport, random} expected."; | |
|
vmpstr
2014/05/30 18:50:43
Update this message please.
ernstm
2014/05/30 19:57:16
Done.
| |
| 58 } | |
| 59 } | |
| 60 | |
| 61 InvalidationBenchmark::~InvalidationBenchmark() { | |
| 62 } | |
| 63 | |
| 64 void InvalidationBenchmark::DidUpdateLayers(LayerTreeHost* host) { | |
| 65 LayerTreeHostCommon::CallFunctionForSubtree( | |
| 66 host->root_layer(), | |
| 67 base::Bind(&InvalidationBenchmark::Run, base::Unretained(this))); | |
| 68 } | |
| 69 | |
| 70 void InvalidationBenchmark::Run(Layer* layer) { | |
| 71 layer->RunMicroBenchmark(this); | |
| 72 } | |
| 73 | |
| 74 void InvalidationBenchmark::RunOnLayer(PictureLayer* layer) { | |
| 75 switch (mode_) { | |
| 76 case FIXED_SIZE: { | |
| 77 // Invalidation with a random mid point and the defined size (clipped to | |
| 78 // the visible content rect). | |
| 79 gfx::Rect visible_content_rect = layer->visible_content_rect(); | |
| 80 int center_x = LCGRandom() * visible_content_rect.width(); | |
|
vmpstr
2014/05/30 18:50:43
I was thinking maybe something like
int min_x = L
ernstm
2014/05/30 19:57:16
Yes, I was concerned about the uniformity of cover
| |
| 81 int center_y = LCGRandom() * visible_content_rect.height(); | |
| 82 int x_min = center_x - width_ / 2; | |
| 83 int x_max = center_x + width_ / 2; | |
| 84 int y_min = center_y - height_ / 2; | |
| 85 int y_max = center_y + height_ / 2; | |
| 86 gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min); | |
| 87 invalidation_rect.Intersect(visible_content_rect); | |
| 88 layer->SetNeedsDisplayRect(invalidation_rect); | |
| 89 break; | |
| 90 } | |
| 91 case LAYER: { | |
| 92 // Invalidate entire layer. | |
| 93 layer->SetNeedsDisplay(); | |
| 94 break; | |
| 95 } | |
| 96 case RANDOM: { | |
| 97 // Random invalidation inside the viewport. | |
| 98 gfx::Rect visible_content_rect = layer->visible_content_rect(); | |
| 99 int x_min = LCGRandom() * visible_content_rect.width(); | |
| 100 int x_max = LCGRandom() * visible_content_rect.width(); | |
| 101 int y_min = LCGRandom() * visible_content_rect.height(); | |
| 102 int y_max = LCGRandom() * visible_content_rect.height(); | |
| 103 if (x_min > x_max) | |
| 104 std::swap(x_min, x_max); | |
| 105 if (y_min > y_max) | |
| 106 std::swap(y_min, y_max); | |
| 107 gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min); | |
| 108 layer->SetNeedsDisplayRect(invalidation_rect); | |
| 109 break; | |
| 110 } | |
| 111 case VIEWPORT: { | |
| 112 // Invalidate entire viewport. | |
| 113 layer->SetNeedsDisplayRect(layer->visible_content_rect()); | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 bool InvalidationBenchmark::ProcessMessage(scoped_ptr<base::Value> value) { | |
| 120 base::DictionaryValue* message = NULL; | |
| 121 value->GetAsDictionary(&message); | |
| 122 if (!message) | |
| 123 return false; | |
| 124 | |
| 125 bool notify_done; | |
| 126 if (message->HasKey("notify_done")) { | |
| 127 message->GetBoolean("notify_done", ¬ify_done); | |
| 128 if (notify_done) | |
| 129 NotifyDone(scoped_ptr<base::Value>(base::Value::CreateNullValue())); | |
| 130 return true; | |
| 131 } | |
| 132 return false; | |
| 133 } | |
| 134 | |
| 135 // A simple linear congruential generator. The random numbers don't need to be | |
| 136 // high quality, but they need to be identical in each run. Therefore, we use a | |
| 137 // LCG and keep the state locally in the benchmark. | |
| 138 float InvalidationBenchmark::LCGRandom() { | |
| 139 const uint32 a = 1664525; | |
| 140 const uint32 c = 1013904223; | |
| 141 seed_ = a * seed_ + c; | |
| 142 return static_cast<float>(seed_) / std::numeric_limits<uint32>::max(); | |
| 143 } | |
| 144 | |
| 145 } // namespace cc | |
| OLD | NEW |