OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/layer_tree_host.h" | 5 #include "cc/layer_tree_host.h" |
6 | 6 |
| 7 #include "base/base_paths.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/string_piece.h" |
7 #include "cc/content_layer.h" | 13 #include "cc/content_layer.h" |
8 #include "cc/nine_patch_layer.h" | 14 #include "cc/nine_patch_layer.h" |
9 #include "cc/solid_color_layer.h" | 15 #include "cc/solid_color_layer.h" |
10 #include "cc/test/fake_content_layer_client.h" | 16 #include "cc/test/fake_content_layer_client.h" |
11 #include "cc/test/layer_tree_test_common.h" | 17 #include "cc/test/layer_tree_test_common.h" |
12 | 18 |
13 namespace cc { | 19 namespace cc { |
14 namespace { | 20 namespace { |
15 | 21 |
16 class LayerTreeHostPerfTest : public WebKitTests::ThreadedTest {}; | 22 static const int kTimeLimitMillis = 2000; |
17 | 23 |
18 class LayerTreeHostPerfTestSevenTabSwitcher : public LayerTreeHostPerfTest { | 24 class LayerTreeHostPerfTest : public WebKitTests::ThreadedTest { |
19 public: | 25 public: |
20 LayerTreeHostPerfTestSevenTabSwitcher() : num_draws_(0) { | 26 LayerTreeHostPerfTest() |
| 27 : num_draws_(0) { |
21 fake_delegate_.setPaintAllOpaque(true); | 28 fake_delegate_.setPaintAllOpaque(true); |
22 } | 29 } |
23 | 30 |
| 31 virtual void beginTest() OVERRIDE { |
| 32 buildTree(); |
| 33 start_time_ = base::TimeTicks::HighResNow(); |
| 34 postSetNeedsCommitToMainThread(); |
| 35 } |
| 36 |
| 37 virtual void drawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE { |
| 38 ++num_draws_; |
| 39 if ((base::TimeTicks::HighResNow() - start_time_) >= |
| 40 base::TimeDelta::FromMilliseconds(kTimeLimitMillis)) |
| 41 endTest(); |
| 42 impl->setNeedsRedraw(); |
| 43 } |
| 44 |
| 45 virtual void buildTree() {} |
| 46 |
| 47 virtual void afterTest() OVERRIDE { |
| 48 base::TimeDelta elapsed = base::TimeTicks::HighResNow() - start_time_; |
| 49 // Format matches chrome/test/perf/perf_test.h:PrintResult |
| 50 printf("*RESULT %s: frames= %.2f runs/s\n", |
| 51 test_name_.c_str(), |
| 52 num_draws_ / elapsed.InSecondsF()); |
| 53 } |
| 54 |
24 scoped_refptr<Layer> CreateLayer(float x, float y, int width, int height) { | 55 scoped_refptr<Layer> CreateLayer(float x, float y, int width, int height) { |
25 scoped_refptr<Layer> layer = Layer::create(); | 56 scoped_refptr<Layer> layer = Layer::create(); |
26 layer->setAnchorPoint(gfx::Point()); | 57 layer->setAnchorPoint(gfx::Point()); |
27 layer->setPosition(gfx::PointF(x, y)); | 58 layer->setPosition(gfx::PointF(x, y)); |
28 layer->setBounds(gfx::Size(width, height)); | 59 layer->setBounds(gfx::Size(width, height)); |
29 return layer; | 60 return layer; |
30 } | 61 } |
31 | 62 |
32 scoped_refptr<ContentLayer> CreateContentLayer(float x, float y, int width, in
t height) { | 63 scoped_refptr<ContentLayer> CreateContentLayer(float x, float y, int width, in
t height) { |
33 scoped_refptr<ContentLayer> layer = ContentLayer::create(&fake_delegate_); | 64 scoped_refptr<ContentLayer> layer = ContentLayer::create(&fake_delegate_); |
34 layer->setAnchorPoint(gfx::Point()); | 65 layer->setAnchorPoint(gfx::Point()); |
35 layer->setPosition(gfx::PointF(x, y)); | 66 layer->setPosition(gfx::PointF(x, y)); |
36 layer->setBounds(gfx::Size(width, height)); | 67 layer->setBounds(gfx::Size(width, height)); |
37 layer->setIsDrawable(true); | 68 layer->setIsDrawable(true); |
38 return layer; | 69 return layer; |
39 } | 70 } |
40 | 71 |
41 scoped_refptr<SolidColorLayer> CreateColorLayer(float x, float y, int width, i
nt height) { | 72 scoped_refptr<SolidColorLayer> CreateColorLayer(float x, float y, int width, i
nt height) { |
42 scoped_refptr<SolidColorLayer> layer = SolidColorLayer::create(); | 73 scoped_refptr<SolidColorLayer> layer = SolidColorLayer::create(); |
43 layer->setAnchorPoint(gfx::Point()); | 74 layer->setAnchorPoint(gfx::Point()); |
44 layer->setPosition(gfx::PointF(x, y)); | 75 layer->setPosition(gfx::PointF(x, y)); |
45 layer->setBounds(gfx::Size(width, height)); | 76 layer->setBounds(gfx::Size(width, height)); |
46 layer->setIsDrawable(true); | 77 layer->setIsDrawable(true); |
47 return layer; | 78 return layer; |
48 } | 79 } |
49 | 80 |
50 scoped_refptr<NinePatchLayer> CreateDecorationLayer(float x, float y, int widt
h, int height) { | 81 scoped_refptr<NinePatchLayer> CreateDecorationLayer(float x, float y, int widt
h, int height) { |
| 82 return CreateDecorationLayer(x, y, width, height, gfx::Rect(0, 0, width, hei
ght)); |
| 83 } |
| 84 |
| 85 scoped_refptr<NinePatchLayer> CreateDecorationLayer(float x, float y, int widt
h, int height, gfx::Rect aperture) { |
51 scoped_refptr<NinePatchLayer> layer = NinePatchLayer::create(); | 86 scoped_refptr<NinePatchLayer> layer = NinePatchLayer::create(); |
52 layer->setAnchorPoint(gfx::Point()); | 87 layer->setAnchorPoint(gfx::Point()); |
53 layer->setPosition(gfx::PointF(x, y)); | 88 layer->setPosition(gfx::PointF(x, y)); |
54 layer->setBounds(gfx::Size(width, height)); | 89 layer->setBounds(gfx::Size(width, height)); |
55 layer->setIsDrawable(true); | 90 layer->setIsDrawable(true); |
56 | 91 |
57 SkBitmap bitmap; | 92 SkBitmap bitmap; |
58 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); | 93 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
59 bitmap.allocPixels(NULL, NULL); | 94 bitmap.allocPixels(NULL, NULL); |
60 layer->setBitmap(bitmap, gfx::Rect(0, 0, width, height)); | 95 layer->setBitmap(bitmap, aperture); |
61 | 96 |
62 return layer; | 97 return layer; |
63 } | 98 } |
64 | 99 |
65 scoped_refptr<Layer> addChild(scoped_refptr<Layer> parent, scoped_refptr<Layer
> child) { | 100 scoped_refptr<Layer> addChild(scoped_refptr<Layer> parent, scoped_refptr<Layer
> child) { |
66 parent->addChild(child); | 101 parent->addChild(child); |
67 return child; | 102 return child; |
68 } | 103 } |
69 | 104 |
70 virtual void beginTest() OVERRIDE { | 105 protected: |
| 106 base::TimeTicks start_time_; |
| 107 int num_draws_; |
| 108 std::string test_name_; |
| 109 WebKitTests::FakeContentLayerClient fake_delegate_; |
| 110 }; |
| 111 |
| 112 class LayerTreeHostPerfTestSevenTabSwitcher : public LayerTreeHostPerfTest { |
| 113 public: |
| 114 LayerTreeHostPerfTestSevenTabSwitcher() |
| 115 : LayerTreeHostPerfTest() { |
| 116 test_name_ = "SevenTabSwitcher"; |
| 117 } |
| 118 |
| 119 virtual void buildTree() OVERRIDE { |
71 scoped_refptr<Layer> root = CreateLayer(0, 0, 720, 1038); // 1 | 120 scoped_refptr<Layer> root = CreateLayer(0, 0, 720, 1038); // 1 |
72 scoped_refptr<Layer> layer; | 121 scoped_refptr<Layer> layer; |
73 | 122 |
74 gfx::Transform down_scale_matrix; | 123 gfx::Transform down_scale_matrix; |
75 down_scale_matrix.Scale(0.747, 0.747); | 124 down_scale_matrix.Scale(0.747, 0.747); |
76 | 125 |
77 layer = addChild(root, CreateLayer(0, 0, 0, 0)); // 2 | 126 layer = addChild(root, CreateLayer(0, 0, 0, 0)); // 2 |
78 | 127 |
79 layer = addChild(root, CreateLayer(628, 15, 0, 0)); // 5 | 128 layer = addChild(root, CreateLayer(628, 15, 0, 0)); // 5 |
80 layer = addChild(root, CreateDecorationLayer(564, -49, 665, 274)); // 13 | 129 layer = addChild(root, CreateDecorationLayer(564, -49, 665, 274)); // 13 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 layer->setTransform(down_scale_matrix); | 218 layer->setTransform(down_scale_matrix); |
170 | 219 |
171 layer = addChild(root, CreateLayer(30, 617, 0, 0)); // 120 | 220 layer = addChild(root, CreateLayer(30, 617, 0, 0)); // 120 |
172 layer = addChild(root, CreateDecorationLayer(14, 601, 569, 807)); // 124 | 221 layer = addChild(root, CreateDecorationLayer(14, 601, 569, 807)); // 124 |
173 layer = addChild(root, CreateDecorationLayer(229.135986f, 601, 354, 96)); //
123 | 222 layer = addChild(root, CreateDecorationLayer(229.135986f, 601, 354, 96)); //
123 |
174 layer = addChild(root, CreateContentLayer(30, 617, 0, 0)); // 122 | 223 layer = addChild(root, CreateContentLayer(30, 617, 0, 0)); // 122 |
175 layer = addChild(root, CreateContentLayer(30, 617, 0, 0)); // 121 | 224 layer = addChild(root, CreateContentLayer(30, 617, 0, 0)); // 121 |
176 | 225 |
177 m_layerTreeHost->setViewportSize(gfx::Size(720, 1038), gfx::Size(720, 1038))
; | 226 m_layerTreeHost->setViewportSize(gfx::Size(720, 1038), gfx::Size(720, 1038))
; |
178 m_layerTreeHost->setRootLayer(root); | 227 m_layerTreeHost->setRootLayer(root); |
179 postSetNeedsCommitToMainThread(); | 228 } |
| 229 }; |
| 230 |
| 231 TEST_F(LayerTreeHostPerfTestSevenTabSwitcher, runSingleThread) { |
| 232 runTest(false); |
| 233 } |
| 234 |
| 235 class LayerTreeHostPerfTestJsonReader : public LayerTreeHostPerfTest { |
| 236 public: |
| 237 LayerTreeHostPerfTestJsonReader() |
| 238 : LayerTreeHostPerfTest() { |
180 } | 239 } |
181 | 240 |
182 virtual void drawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE { | 241 void readTestFile(std::string name) { |
183 impl->setNeedsRedraw(); | 242 test_name_ = name; |
184 ++num_draws_; | 243 FilePath filepath; |
185 if (num_draws_ > 120) | 244 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &filepath)); |
186 endTest(); | 245 filepath = filepath.AppendASCII("cc").AppendASCII("test") |
| 246 .AppendASCII("data").AppendASCII(name + ".json"); |
| 247 std::string json; |
| 248 ASSERT_TRUE(file_util::ReadFileToString(filepath, &json)); |
| 249 tree_.reset(base::JSONReader::Read(json)); |
| 250 ASSERT_TRUE(tree_); |
187 } | 251 } |
188 | 252 |
189 virtual void afterTest() OVERRIDE {} | 253 scoped_refptr<Layer> parseLayer(base::Value* val) { |
| 254 DictionaryValue* dict; |
| 255 bool success = true; |
| 256 success &= val->GetAsDictionary(&dict); |
| 257 std::string layer_type; |
| 258 success &= dict->GetString("LayerType", &layer_type); |
| 259 ListValue* list; |
| 260 success &= dict->GetList("Bounds", &list); |
| 261 int width, height; |
| 262 success &= list->GetInteger(0, &width); |
| 263 success &= list->GetInteger(1, &height); |
| 264 success &= dict->GetList("Position", &list); |
| 265 double position_x, position_y; |
| 266 success &= list->GetDouble(0, &position_x); |
| 267 success &= list->GetDouble(1, &position_y); |
| 268 |
| 269 scoped_refptr<Layer> new_layer; |
| 270 if (layer_type == "SolidColorLayer") { |
| 271 new_layer = CreateColorLayer(position_x, position_y, width, height); |
| 272 } else if (layer_type == "ContentLayer") { |
| 273 new_layer = CreateContentLayer(position_x, position_y, width, height); |
| 274 } else if (layer_type == "NinePatchLayer") { |
| 275 success &= dict->GetList("ImageAperture", &list); |
| 276 int aperture_x, aperture_y, aperture_width, aperture_height; |
| 277 success &= list->GetInteger(0, &aperture_x); |
| 278 success &= list->GetInteger(1, &aperture_y); |
| 279 success &= list->GetInteger(2, &aperture_width); |
| 280 success &= list->GetInteger(3, &aperture_height); |
| 281 |
| 282 new_layer = CreateDecorationLayer( |
| 283 position_x, position_y, width, height, |
| 284 gfx::Rect(aperture_x, aperture_y, aperture_width, aperture_height)); |
| 285 |
| 286 } else { // Type "Layer" or "unknown" |
| 287 new_layer = CreateLayer(position_x, position_y, width, height); |
| 288 } |
| 289 |
| 290 success &= dict->GetList("DrawTransform", &list); |
| 291 double transform[16]; |
| 292 for (int i = 0; i < 16; ++i) |
| 293 success &= list->GetDouble(i, &transform[i]); |
| 294 |
| 295 gfx::Transform gfxTransform; |
| 296 gfxTransform.matrix().setColMajord(transform); |
| 297 new_layer->setTransform(gfxTransform); |
| 298 |
| 299 success &= dict->GetList("Children", &list); |
| 300 for (ListValue::const_iterator it = list->begin(); |
| 301 it != list->end(); ++it) { |
| 302 new_layer->addChild(parseLayer(*it)); |
| 303 } |
| 304 |
| 305 if (!success) |
| 306 ADD_FAILURE() << "Could not parse json data"; |
| 307 |
| 308 return new_layer; |
| 309 } |
| 310 |
| 311 virtual void buildTree() OVERRIDE { |
| 312 gfx::Size viewport = gfx::Size(720, 1038); |
| 313 m_layerTreeHost->setViewportSize(viewport, viewport); |
| 314 m_layerTreeHost->setRootLayer(parseLayer(tree_.get())); |
| 315 } |
190 | 316 |
191 private: | 317 private: |
192 int num_draws_; | 318 scoped_ptr<base::Value> tree_; |
193 WebKitTests::FakeContentLayerClient fake_delegate_; | |
194 }; | 319 }; |
195 | 320 |
196 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostPerfTestSevenTabSwitcher); | 321 TEST_F(LayerTreeHostPerfTestJsonReader, tenTenSingleThread) { |
| 322 readTestFile("10_10_layer_tree"); |
| 323 runTest(false); |
| 324 } |
197 | 325 |
198 } // namespace | 326 } // namespace |
199 } // namespace cc | 327 } // namespace cc |
OLD | NEW |