Chromium Code Reviews| Index: cc/debug/invalidation_benchmark.cc |
| diff --git a/cc/debug/invalidation_benchmark.cc b/cc/debug/invalidation_benchmark.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8501cf561a2a985844c55448bcacafdd3617be6c |
| --- /dev/null |
| +++ b/cc/debug/invalidation_benchmark.cc |
| @@ -0,0 +1,145 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cc/debug/invalidation_benchmark.h" |
| + |
| +#include <algorithm> |
| +#include <limits> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/rand_util.h" |
| +#include "base/values.h" |
| +#include "cc/layers/layer.h" |
| +#include "cc/layers/picture_layer.h" |
| +#include "cc/trees/layer_tree_host.h" |
| +#include "cc/trees/layer_tree_host_common.h" |
| +#include "ui/gfx/rect.h" |
| + |
| +namespace cc { |
| + |
| +namespace { |
| + |
| +const char* kDefaultInvalidationMode = "viewport"; |
| + |
| +} // namespace |
| + |
| +InvalidationBenchmark::InvalidationBenchmark( |
| + scoped_ptr<base::Value> value, |
| + const MicroBenchmark::DoneCallback& callback) |
| + : MicroBenchmark(callback), seed_(0) { |
| + base::DictionaryValue* settings = NULL; |
| + value->GetAsDictionary(&settings); |
| + if (!settings) |
| + return; |
| + |
| + std::string mode_string = kDefaultInvalidationMode; |
| + |
| + if (settings->HasKey("mode")) |
| + settings->GetString("mode", &mode_string); |
| + |
| + if (mode_string == "fixed_size") { |
| + mode_ = FIXED_SIZE; |
| + CHECK(settings->HasKey("width")) |
| + << "Must provide a width for fixed_size mode."; |
| + CHECK(settings->HasKey("height")) |
| + << "Must provide a height for fixed_size mode."; |
| + settings->GetInteger("width", &width_); |
| + settings->GetInteger("height", &height_); |
| + } else if (mode_string == "layer") { |
| + mode_ = LAYER; |
| + } else if (mode_string == "random") { |
| + mode_ = RANDOM; |
| + } else if (mode_string == "viewport") { |
| + mode_ = VIEWPORT; |
| + } else { |
| + CHECK(false) << "Invalid mode: " << mode_string |
| + << ". 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.
|
| + } |
| +} |
| + |
| +InvalidationBenchmark::~InvalidationBenchmark() { |
| +} |
| + |
| +void InvalidationBenchmark::DidUpdateLayers(LayerTreeHost* host) { |
| + LayerTreeHostCommon::CallFunctionForSubtree( |
| + host->root_layer(), |
| + base::Bind(&InvalidationBenchmark::Run, base::Unretained(this))); |
| +} |
| + |
| +void InvalidationBenchmark::Run(Layer* layer) { |
| + layer->RunMicroBenchmark(this); |
| +} |
| + |
| +void InvalidationBenchmark::RunOnLayer(PictureLayer* layer) { |
| + switch (mode_) { |
| + case FIXED_SIZE: { |
| + // Invalidation with a random mid point and the defined size (clipped to |
| + // the visible content rect). |
| + gfx::Rect visible_content_rect = layer->visible_content_rect(); |
| + 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
|
| + int center_y = LCGRandom() * visible_content_rect.height(); |
| + int x_min = center_x - width_ / 2; |
| + int x_max = center_x + width_ / 2; |
| + int y_min = center_y - height_ / 2; |
| + int y_max = center_y + height_ / 2; |
| + gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min); |
| + invalidation_rect.Intersect(visible_content_rect); |
| + layer->SetNeedsDisplayRect(invalidation_rect); |
| + break; |
| + } |
| + case LAYER: { |
| + // Invalidate entire layer. |
| + layer->SetNeedsDisplay(); |
| + break; |
| + } |
| + case RANDOM: { |
| + // Random invalidation inside the viewport. |
| + gfx::Rect visible_content_rect = layer->visible_content_rect(); |
| + int x_min = LCGRandom() * visible_content_rect.width(); |
| + int x_max = LCGRandom() * visible_content_rect.width(); |
| + int y_min = LCGRandom() * visible_content_rect.height(); |
| + int y_max = LCGRandom() * visible_content_rect.height(); |
| + if (x_min > x_max) |
| + std::swap(x_min, x_max); |
| + if (y_min > y_max) |
| + std::swap(y_min, y_max); |
| + gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min); |
| + layer->SetNeedsDisplayRect(invalidation_rect); |
| + break; |
| + } |
| + case VIEWPORT: { |
| + // Invalidate entire viewport. |
| + layer->SetNeedsDisplayRect(layer->visible_content_rect()); |
| + break; |
| + } |
| + } |
| +} |
| + |
| +bool InvalidationBenchmark::ProcessMessage(scoped_ptr<base::Value> value) { |
| + base::DictionaryValue* message = NULL; |
| + value->GetAsDictionary(&message); |
| + if (!message) |
| + return false; |
| + |
| + bool notify_done; |
| + if (message->HasKey("notify_done")) { |
| + message->GetBoolean("notify_done", ¬ify_done); |
| + if (notify_done) |
| + NotifyDone(scoped_ptr<base::Value>(base::Value::CreateNullValue())); |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +// A simple linear congruential generator. The random numbers don't need to be |
| +// high quality, but they need to be identical in each run. Therefore, we use a |
| +// LCG and keep the state locally in the benchmark. |
| +float InvalidationBenchmark::LCGRandom() { |
| + const uint32 a = 1664525; |
| + const uint32 c = 1013904223; |
| + seed_ = a * seed_ + c; |
| + return static_cast<float>(seed_) / std::numeric_limits<uint32>::max(); |
| +} |
| + |
| +} // namespace cc |