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 {fixed_size, layer, viewport, random} expected."; | |
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 position and fixed size (clipped to the | |
78 // visible content rect). | |
79 gfx::Rect visible_content_rect = layer->visible_content_rect(); | |
80 int x = LCGRandom() * (visible_content_rect.width() - width_); | |
81 int y = LCGRandom() * (visible_content_rect.height() - height_); | |
82 gfx::Rect invalidation_rect(x, y, width_, height_); | |
83 invalidation_rect.Intersect(visible_content_rect); | |
vmpstr
2014/05/30 20:27:26
nit: this might be reduntant
ernstm
2014/05/30 21:44:46
Done.
| |
84 layer->SetNeedsDisplayRect(invalidation_rect); | |
85 break; | |
86 } | |
87 case LAYER: { | |
88 // Invalidate entire layer. | |
89 layer->SetNeedsDisplay(); | |
90 break; | |
91 } | |
92 case RANDOM: { | |
93 // Random invalidation inside the viewport. | |
94 gfx::Rect visible_content_rect = layer->visible_content_rect(); | |
95 int x_min = LCGRandom() * visible_content_rect.width(); | |
96 int x_max = LCGRandom() * visible_content_rect.width(); | |
97 int y_min = LCGRandom() * visible_content_rect.height(); | |
98 int y_max = LCGRandom() * visible_content_rect.height(); | |
99 if (x_min > x_max) | |
100 std::swap(x_min, x_max); | |
101 if (y_min > y_max) | |
102 std::swap(y_min, y_max); | |
103 gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min); | |
104 layer->SetNeedsDisplayRect(invalidation_rect); | |
105 break; | |
106 } | |
107 case VIEWPORT: { | |
108 // Invalidate entire viewport. | |
109 layer->SetNeedsDisplayRect(layer->visible_content_rect()); | |
110 break; | |
111 } | |
112 } | |
113 } | |
114 | |
115 bool InvalidationBenchmark::ProcessMessage(scoped_ptr<base::Value> value) { | |
116 base::DictionaryValue* message = NULL; | |
117 value->GetAsDictionary(&message); | |
118 if (!message) | |
119 return false; | |
120 | |
121 bool notify_done; | |
122 if (message->HasKey("notify_done")) { | |
123 message->GetBoolean("notify_done", ¬ify_done); | |
124 if (notify_done) | |
125 NotifyDone(scoped_ptr<base::Value>(base::Value::CreateNullValue())); | |
126 return true; | |
127 } | |
128 return false; | |
129 } | |
130 | |
131 // A simple linear congruential generator. The random numbers don't need to be | |
132 // high quality, but they need to be identical in each run. Therefore, we use a | |
133 // LCG and keep the state locally in the benchmark. | |
134 float InvalidationBenchmark::LCGRandom() { | |
135 const uint32 a = 1664525; | |
136 const uint32 c = 1013904223; | |
137 seed_ = a * seed_ + c; | |
138 return static_cast<float>(seed_) / std::numeric_limits<uint32>::max(); | |
139 } | |
140 | |
141 } // namespace cc | |
OLD | NEW |