1 // Copyright (c) 2012 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 "content/renderer/all_rendering_benchmarks.h"
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time.h"
16 #include "content/renderer/rendering_benchmark.h"
17 #include "skia/ext/platform_canvas.h"
18 #include "third_party/skia/include/core/SkPicture.h"
19 #include "third_party/skia/include/utils/SkNullCanvas.h"
20 #include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
21 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewBenchmarkSupport.h"
24 using base::TimeDelta
;
25 using base::TimeTicks
;
26 using WebKit::WebSize
;
27 using WebKit::WebCanvas
;
28 using WebKit::WebViewBenchmarkSupport
;
29 using WebKit::WebRect
;
34 // This is a base class for timing the painting of the current webpage to
35 // custom WebCanvases.
36 class CustomPaintBenchmark
37 : public content::RenderingBenchmark
,
38 public WebViewBenchmarkSupport::PaintClient
{
40 CustomPaintBenchmark(const std::string
& name
,
41 WebViewBenchmarkSupport::PaintMode paint_mode
)
42 : content::RenderingBenchmark(name
),
43 paint_mode_(paint_mode
) { }
45 virtual WebCanvas
* willPaint(const WebSize
& size
) OVERRIDE
{
46 WebCanvas
* canvas
= createCanvas(size
);
47 before_time_
= TimeTicks::HighResNow();
51 virtual void didPaint(WebCanvas
* canvas
) OVERRIDE
{
52 paint_time_total_
+= (TimeTicks::HighResNow() - before_time_
);
56 virtual double Run(WebViewBenchmarkSupport
* support
) OVERRIDE
{
57 paint_time_total_
= TimeDelta();
58 support
->paint(this, paint_mode_
);
59 return paint_time_total_
.InMillisecondsF();
63 virtual WebCanvas
* createCanvas(const WebSize
& size
) = 0;
65 TimeTicks before_time_
;
66 TimeDelta paint_time_total_
;
67 const WebViewBenchmarkSupport::PaintMode paint_mode_
;
70 class BitmapCanvasPaintBenchmark
: public CustomPaintBenchmark
{
72 BitmapCanvasPaintBenchmark(const std::string
& name
,
73 WebViewBenchmarkSupport::PaintMode paint_mode
)
74 : CustomPaintBenchmark(name
, paint_mode
) { }
77 virtual WebCanvas
* createCanvas(const WebSize
& size
) OVERRIDE
{
78 return skia::CreateBitmapCanvas(size
.width
, size
.height
, false);
82 class CanvasCountBenchmark
83 : public content::RenderingBenchmark
,
84 public WebViewBenchmarkSupport::PaintClient
{
86 CanvasCountBenchmark(const std::string
& name
,
87 WebViewBenchmarkSupport::PaintMode paint_mode
)
88 : content::RenderingBenchmark(name
),
90 paint_mode_(paint_mode
) { }
92 virtual WebCanvas
* willPaint(const WebSize
& size
) OVERRIDE
{
94 return SkCreateNullCanvas();
97 virtual void didPaint(WebCanvas
* canvas
) OVERRIDE
{
101 virtual double Run(WebViewBenchmarkSupport
* support
) OVERRIDE
{
103 support
->paint(this, paint_mode_
);
104 return canvas_count_
;
108 const WebViewBenchmarkSupport::PaintMode paint_mode_
;
111 class NullCanvasPaintBenchmark
: public CustomPaintBenchmark
{
113 NullCanvasPaintBenchmark(const std::string
& name
,
114 WebViewBenchmarkSupport::PaintMode paint_mode
)
115 : CustomPaintBenchmark(name
, paint_mode
) { }
118 virtual WebCanvas
* createCanvas(const WebSize
& size
) OVERRIDE
{
119 return SkCreateNullCanvas();
123 class SkPicturePaintBenchmark
: public CustomPaintBenchmark
{
125 SkPicturePaintBenchmark(const std::string
& name
,
126 WebViewBenchmarkSupport::PaintMode paint_mode
)
127 : CustomPaintBenchmark(name
, paint_mode
) { }
129 virtual void didPaint(WebCanvas
* canvas
) OVERRIDE
{
130 DCHECK(picture_
.getRecordingCanvas() == canvas
);
131 picture_
.endRecording();
132 CustomPaintBenchmark::didPaint(NULL
);
136 virtual WebCanvas
* createCanvas(const WebSize
& size
) OVERRIDE
{
137 return picture_
.beginRecording(size
.width
, size
.height
);
143 // Base class for timing the replaying of the SkPicture into canvases.
144 class TiledReplayBenchmark
145 : public content::RenderingBenchmark
,
146 public WebViewBenchmarkSupport::PaintClient
{
148 TiledReplayBenchmark(const std::string
& name
,
149 WebViewBenchmarkSupport::PaintMode paint_mode
)
150 : RenderingBenchmark(name
),
151 paint_mode_(paint_mode
) {}
153 virtual WebCanvas
* willPaint(const WebSize
& size
) OVERRIDE
{
154 return picture_
.beginRecording(size
.width
, size
.height
);
157 virtual void didPaint(WebCanvas
* canvas
) OVERRIDE
{
158 DCHECK(picture_
.getRecordingCanvas() == canvas
);
159 picture_
.endRecording();
161 const vector
<WebRect
> repaint_tiles
= GetRepaintTiles(
162 WebSize(picture_
.width(), picture_
.height()));
164 vector
<WebRect
>::const_iterator it
;
165 for (it
= repaint_tiles
.begin(); it
!= repaint_tiles
.end(); ++it
) {
167 scoped_ptr
<WebCanvas
> canvas(
168 skia::CreateBitmapCanvas(tile
.width
, tile
.height
, false));
169 TimeTicks before_time
= TimeTicks::HighResNow();
170 canvas
->translate(-tile
.x
, -tile
.y
);
171 picture_
.draw(canvas
.get());
172 paint_time_total_
+= (TimeTicks::HighResNow() - before_time
);
176 virtual double Run(WebViewBenchmarkSupport
* support
) OVERRIDE
{
177 paint_time_total_
= TimeDelta();
178 support
->paint(this, paint_mode_
);
179 return paint_time_total_
.InMillisecondsF();
183 virtual vector
<WebRect
> GetRepaintTiles(const WebSize
& layer_size
) const = 0;
185 TimeDelta paint_time_total_
;
187 const WebViewBenchmarkSupport::PaintMode paint_mode_
;
190 class SquareTiledReplayBenchmark
: public TiledReplayBenchmark
{
192 SquareTiledReplayBenchmark(const std::string
& name
,
193 WebViewBenchmarkSupport::PaintMode paint_mode
,
195 : TiledReplayBenchmark(name
, paint_mode
),
196 tile_size_(tile_size
) {
197 CHECK_GT(tile_size
, 0);
201 virtual vector
<WebRect
> GetRepaintTiles(
202 const WebSize
& layer_size
) const OVERRIDE
{
203 vector
<WebRect
> tiles
;
204 for (int x
= 0; x
< layer_size
.width
; x
+= tile_size_
) {
205 for (int y
= 0; y
< layer_size
.height
; y
+= tile_size_
) {
206 int width
= std::min(layer_size
.width
- x
, tile_size_
);
207 int height
= std::min(layer_size
.height
- y
, tile_size_
);
208 tiles
.push_back(WebRect(x
, y
, width
, height
));
217 class LayerWidthTiledReplayBenchmark
: public TiledReplayBenchmark
{
219 LayerWidthTiledReplayBenchmark(const std::string
& name
,
220 WebViewBenchmarkSupport::PaintMode paint_mode
,
222 : TiledReplayBenchmark(name
, paint_mode
),
223 tile_height_(tile_height
) {
224 CHECK_GT(tile_height
, 0);
228 virtual vector
<WebRect
> GetRepaintTiles(
229 const WebSize
& layer_size
) const OVERRIDE
{
230 vector
<WebRect
> tiles
;
231 for (int y
= 0; y
< layer_size
.height
; y
+= tile_height_
) {
232 int height
= std::min(layer_size
.height
- y
, tile_height_
);
233 tiles
.push_back(WebRect(0, y
, layer_size
.width
, height
));
241 } // anonymous namespace
245 ScopedVector
<RenderingBenchmark
> AllRenderingBenchmarks() {
246 ScopedVector
<RenderingBenchmark
> benchmarks
;
247 benchmarks
.push_back(new BitmapCanvasPaintBenchmark(
248 "PaintEverythingToBitmapMs",
249 WebViewBenchmarkSupport::PaintModeEverything
));
250 benchmarks
.push_back(new NullCanvasPaintBenchmark(
251 "PaintEverythingToNullCanvasMs",
252 WebViewBenchmarkSupport::PaintModeEverything
));
253 benchmarks
.push_back(new CanvasCountBenchmark(
255 WebViewBenchmarkSupport::PaintModeEverything
));
256 benchmarks
.push_back(new SkPicturePaintBenchmark(
257 "PaintEverythingToSkPictureMs",
258 WebViewBenchmarkSupport::PaintModeEverything
));
259 benchmarks
.push_back(new SquareTiledReplayBenchmark(
260 "RepaintEverythingTo256x256BitmapMs",
261 WebViewBenchmarkSupport::PaintModeEverything
,
263 benchmarks
.push_back(new SquareTiledReplayBenchmark(
264 "RepaintEverythingTo128x128BitmapMs",
265 WebViewBenchmarkSupport::PaintModeEverything
,
267 benchmarks
.push_back(new SquareTiledReplayBenchmark(
268 "RepaintEverythingTo512x512BitmapMs",
269 WebViewBenchmarkSupport::PaintModeEverything
,
271 benchmarks
.push_back(new LayerWidthTiledReplayBenchmark(
272 "RepaintEverythingToLayerWidthx256BitmapMs",
273 WebViewBenchmarkSupport::PaintModeEverything
,
275 benchmarks
.push_back(new LayerWidthTiledReplayBenchmark(
276 "RepaintEverythingToLayerWidthx128BitmapMs",
277 WebViewBenchmarkSupport::PaintModeEverything
,
279 benchmarks
.push_back(new LayerWidthTiledReplayBenchmark(
280 "RepaintEverythingToLayerWidthx64BitmapMs",
281 WebViewBenchmarkSupport::PaintModeEverything
,
283 benchmarks
.push_back(new LayerWidthTiledReplayBenchmark(
284 "RepaintEverythingToLayerWidthx512BitmapMs",
285 WebViewBenchmarkSupport::PaintModeEverything
,
287 return benchmarks
.Pass();
290 } // namespace content