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 == "layer") { | |
42 mode_ = LAYER; | |
43 } else if (mode_string == "viewport") { | |
44 mode_ = VIEWPORT; | |
45 } else if (mode_string == "random") { | |
46 mode_ = RANDOM; | |
47 } else { | |
48 CHECK(false) << "Invalid mode: " << mode_string | |
49 << ". One of {layer, viewport, random} expected"; | |
50 } | |
51 } | |
52 | |
53 InvalidationBenchmark::~InvalidationBenchmark() { | |
54 } | |
55 | |
56 void InvalidationBenchmark::DidUpdateLayers(LayerTreeHost* host) { | |
57 LayerTreeHostCommon::CallFunctionForSubtree( | |
58 host->root_layer(), | |
59 base::Bind(&InvalidationBenchmark::Run, base::Unretained(this))); | |
60 } | |
61 | |
62 void InvalidationBenchmark::Run(Layer* layer) { | |
63 layer->RunMicroBenchmark(this); | |
64 } | |
65 | |
66 void InvalidationBenchmark::RunOnLayer(PictureLayer* layer) { | |
67 switch (mode_) { | |
68 case LAYER: | |
69 // Invalidate entire layer. | |
70 layer->SetNeedsDisplay(); | |
71 break; | |
72 case VIEWPORT: | |
73 // Invalidate entire viewport. | |
74 layer->SetNeedsDisplayRect(layer->visible_content_rect()); | |
75 break; | |
76 case RANDOM: { | |
77 // Random invalidation inside the viewport. | |
78 gfx::Rect visible_content_rect = layer->visible_content_rect(); | |
79 int x_min = LCGRandom() * visible_content_rect.width(); | |
80 int x_max = LCGRandom() * visible_content_rect.width(); | |
81 int y_min = LCGRandom() * visible_content_rect.height(); | |
82 int y_max = LCGRandom() * visible_content_rect.height(); | |
83 if (x_min > x_max) | |
84 std::swap(x_min, x_max); | |
85 if (y_min > y_max) | |
86 std::swap(y_min, y_max); | |
87 gfx::Rect invalidation_rect(x_min, y_min, x_max - x_min, y_max - y_min); | |
88 layer->SetNeedsDisplayRect(invalidation_rect); | |
89 break; | |
90 } | |
91 default: | |
vmpstr
2014/05/27 21:54:18
nit: you can remove the default, so that if we add
ernstm
2014/05/27 21:57:11
Done.
| |
92 NOTREACHED(); | |
93 break; | |
94 } | |
95 } | |
96 | |
97 // A simple linear congruential generator. The random numbers don't need to be | |
98 // high quality, but they need to be identical in each run. Therefore, we use a | |
99 // LCG and keep the state locally in the benchmark. | |
100 float InvalidationBenchmark::LCGRandom() { | |
101 const uint32 a = 1664525; | |
102 const uint32 c = 1013904223; | |
103 seed_ = a * seed_ + c; | |
104 return static_cast<float>(seed_) / std::numeric_limits<uint32>::max(); | |
105 } | |
106 | |
107 } // namespace cc | |
OLD | NEW |