1 // Copyright 2014 The Chromium Authors
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/benchmarks/invalidation_benchmark.h"
14 #include "base/rand_util.h"
15 #include "base/values.h"
16 #include "cc/base/math_util.h"
17 #include "cc/layers/layer.h"
18 #include "cc/layers/picture_layer.h"
19 #include "cc/trees/draw_property_utils.h"
20 #include "cc/trees/layer_tree_host.h"
21 #include "third_party/abseil-cpp/absl/types/optional.h"
22 #include "ui/gfx/geometry/rect.h"
28 const char* kDefaultInvalidationMode = "viewport";
32 InvalidationBenchmark::InvalidationBenchmark(
33 base::Value::Dict settings,
34 MicroBenchmark::DoneCallback callback)
35 : MicroBenchmark(std::move(callback)) {
36 std::string mode_string = kDefaultInvalidationMode;
38 auto* mode_string_from_settings = settings.FindString("mode");
39 if (mode_string_from_settings)
40 mode_string = *mode_string_from_settings;
42 if (mode_string == "fixed_size") {
44 auto width = settings.FindInt("width");
45 auto height = settings.FindInt("height");
46 CHECK(width.has_value()) << "Must provide a width for fixed_size mode.";
47 CHECK(height.has_value()) << "Must provide a height for fixed_size mode.";
50 } else if (mode_string == "layer") {
52 } else if (mode_string == "random") {
54 } else if (mode_string == "viewport") {
57 CHECK(false) << "Invalid mode: " << mode_string
58 << ". One of {fixed_size, layer, viewport, random} expected.";
62 InvalidationBenchmark::~InvalidationBenchmark() = default;
64 void InvalidationBenchmark::DidUpdateLayers(LayerTreeHost* layer_tree_host) {
65 for (auto* layer : *layer_tree_host)
66 layer->RunMicroBenchmark(this);
69 void InvalidationBenchmark::RunOnLayer(PictureLayer* layer) {
70 gfx::Rect visible_layer_rect = gfx::Rect(layer->bounds());
71 gfx::Transform from_screen =
72 layer->ScreenSpaceTransform().InverseOrIdentity();
73 gfx::Rect viewport_rect = MathUtil::ProjectEnclosingClippedRect(
74 from_screen, layer->layer_tree_host()->device_viewport_rect());
75 visible_layer_rect.Intersect(viewport_rect);
78 // Invalidation with a random position and fixed size.
79 int x = LCGRandom() * (visible_layer_rect.width() - width_);
80 int y = LCGRandom() * (visible_layer_rect.height() - height_);
81 gfx::Rect invalidation_rect(x, y, width_, height_);
82 layer->SetNeedsDisplayRect(invalidation_rect);
86 // Invalidate entire layer.
87 layer->SetNeedsDisplay();
91 // Random invalidation inside the viewport.
92 int x_min = LCGRandom() * visible_layer_rect.width();
93 int x_max = LCGRandom() * visible_layer_rect.width();
94 int y_min = LCGRandom() * visible_layer_rect.height();
95 int y_max = LCGRandom() * visible_layer_rect.height();
97 std::swap(x_min, x_max);
99 std::swap(y_min, y_max);
100 gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min);
101 layer->SetNeedsDisplayRect(invalidation_rect);
105 // Invalidate entire viewport.
106 layer->SetNeedsDisplayRect(visible_layer_rect);
112 bool InvalidationBenchmark::ProcessMessage(base::Value::Dict message) {
113 auto notify_done = message.FindBool("notify_done");
114 if (notify_done.has_value()) {
115 if (notify_done.value()) {
116 NotifyDone(base::Value::Dict());
123 // A simple linear congruential generator. The random numbers don't need to be
124 // high quality, but they need to be identical in each run. Therefore, we use a
125 // LCG and keep the state locally in the benchmark.
126 float InvalidationBenchmark::LCGRandom() {
127 constexpr uint32_t a = 1664525;
128 constexpr uint32_t c = 1013904223;
129 seed_ = a * seed_ + c;
130 return static_cast<float>(seed_) /
131 static_cast<float>(std::numeric_limits<uint32_t>::max());