Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / cc / benchmarks / invalidation_benchmark.cc
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.
4
5 #include "cc/benchmarks/invalidation_benchmark.h"
6
7 #include <stdint.h>
8
9 #include <algorithm>
10 #include <limits>
11 #include <string>
12 #include <utility>
13
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"
23
24 namespace cc {
25
26 namespace {
27
28 const char* kDefaultInvalidationMode = "viewport";
29
30 }  // namespace
31
32 InvalidationBenchmark::InvalidationBenchmark(
33     base::Value::Dict settings,
34     MicroBenchmark::DoneCallback callback)
35     : MicroBenchmark(std::move(callback)) {
36   std::string mode_string = kDefaultInvalidationMode;
37
38   auto* mode_string_from_settings = settings.FindString("mode");
39   if (mode_string_from_settings)
40     mode_string = *mode_string_from_settings;
41
42   if (mode_string == "fixed_size") {
43     mode_ = 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.";
48     width_ = *width;
49     height_ = *height;
50   } else if (mode_string == "layer") {
51     mode_ = LAYER;
52   } else if (mode_string == "random") {
53     mode_ = RANDOM;
54   } else if (mode_string == "viewport") {
55     mode_ = VIEWPORT;
56   } else {
57     CHECK(false) << "Invalid mode: " << mode_string
58                  << ". One of {fixed_size, layer, viewport, random} expected.";
59   }
60 }
61
62 InvalidationBenchmark::~InvalidationBenchmark() = default;
63
64 void InvalidationBenchmark::DidUpdateLayers(LayerTreeHost* layer_tree_host) {
65   for (auto* layer : *layer_tree_host)
66     layer->RunMicroBenchmark(this);
67 }
68
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);
76   switch (mode_) {
77     case FIXED_SIZE: {
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);
83       break;
84     }
85     case LAYER: {
86       // Invalidate entire layer.
87       layer->SetNeedsDisplay();
88       break;
89     }
90     case RANDOM: {
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();
96       if (x_min > x_max)
97         std::swap(x_min, x_max);
98       if (y_min > y_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);
102       break;
103     }
104     case VIEWPORT: {
105       // Invalidate entire viewport.
106       layer->SetNeedsDisplayRect(visible_layer_rect);
107       break;
108     }
109   }
110 }
111
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());
117     }
118     return true;
119   }
120   return false;
121 }
122
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());
132 }
133
134 }  // namespace cc