1 // Copyright 2013 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.
5 #include "cc/debug/picture_record_benchmark.h"
9 #include "base/basictypes.h"
10 #include "base/values.h"
11 #include "cc/layers/layer.h"
12 #include "cc/layers/picture_layer.h"
13 #include "cc/playback/picture.h"
14 #include "cc/trees/layer_tree_host.h"
15 #include "cc/trees/layer_tree_host_common.h"
16 #include "ui/gfx/geometry/rect.h"
22 const int kPositionIncrement
= 100;
23 const int kTileGridSize
= 512;
27 PictureRecordBenchmark::PictureRecordBenchmark(
28 scoped_ptr
<base::Value
> value
,
29 const MicroBenchmark::DoneCallback
& callback
)
30 : MicroBenchmark(callback
) {
34 base::ListValue
* list
= nullptr;
35 value
->GetAsList(&list
);
39 for (base::ListValue::iterator it
= list
->begin(); it
!= list
->end(); ++it
) {
40 base::DictionaryValue
* dictionary
= nullptr;
41 (*it
)->GetAsDictionary(&dictionary
);
43 !dictionary
->HasKey("width") ||
44 !dictionary
->HasKey("height"))
48 dictionary
->GetInteger("width", &width
);
49 dictionary
->GetInteger("height", &height
);
51 dimensions_
.push_back(std::make_pair(width
, height
));
55 PictureRecordBenchmark::~PictureRecordBenchmark() {}
57 void PictureRecordBenchmark::DidUpdateLayers(LayerTreeHost
* host
) {
58 LayerTreeHostCommon::CallFunctionForSubtree(
60 [this](Layer
* layer
) { layer
->RunMicroBenchmark(this); });
62 scoped_ptr
<base::ListValue
> results(new base::ListValue());
63 for (std::map
<std::pair
<int, int>, TotalTime
>::iterator it
= times_
.begin();
66 std::pair
<int, int> dimensions
= it
->first
;
67 base::TimeDelta total_time
= it
->second
.first
;
68 unsigned total_count
= it
->second
.second
;
70 double average_time
= 0.0;
72 average_time
= total_time
.InMillisecondsF() / total_count
;
74 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue());
75 result
->SetInteger("width", dimensions
.first
);
76 result
->SetInteger("height", dimensions
.second
);
77 result
->SetInteger("samples_count", total_count
);
78 result
->SetDouble("time_ms", average_time
);
80 results
->Append(result
.release());
83 NotifyDone(results
.Pass());
86 void PictureRecordBenchmark::RunOnLayer(PictureLayer
* layer
) {
87 ContentLayerClient
* painter
= layer
->client();
88 gfx::Size bounds
= layer
->bounds();
90 gfx::Size
tile_grid_size(kTileGridSize
, kTileGridSize
);
92 for (size_t i
= 0; i
< dimensions_
.size(); ++i
) {
93 std::pair
<int, int> dimensions
= dimensions_
[i
];
94 int width
= dimensions
.first
;
95 int height
= dimensions
.second
;
97 int y_limit
= std::max(1, bounds
.height() - height
);
98 int x_limit
= std::max(1, bounds
.width() - width
);
99 for (int y
= 0; y
< y_limit
; y
+= kPositionIncrement
) {
100 for (int x
= 0; x
< x_limit
; x
+= kPositionIncrement
) {
101 gfx::Rect rect
= gfx::Rect(x
, y
, width
, height
);
103 base::TimeTicks start
= base::TimeTicks::Now();
105 scoped_refptr
<Picture
> picture
=
106 Picture::Create(rect
, painter
, tile_grid_size
, false,
107 RecordingSource::RECORD_NORMALLY
);
109 base::TimeTicks end
= base::TimeTicks::Now();
110 base::TimeDelta duration
= end
- start
;
111 TotalTime
& total_time
= times_
[dimensions
];
112 total_time
.first
+= duration
;