Fix build break
[chromium-blink-merge.git] / content / renderer / all_rendering_benchmarks.cc
blobcc8092a251442976b30045ae8919bd4a5e97efb0
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"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
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;
30 using std::vector;
32 namespace {
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 {
39 public:
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();
48 return canvas;
51 virtual void didPaint(WebCanvas* canvas) OVERRIDE {
52 paint_time_total_ += (TimeTicks::HighResNow() - before_time_);
53 delete canvas;
56 virtual double Run(WebViewBenchmarkSupport* support) OVERRIDE {
57 paint_time_total_ = TimeDelta();
58 support->paint(this, paint_mode_);
59 return paint_time_total_.InMillisecondsF();
62 private:
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 {
71 public:
72 BitmapCanvasPaintBenchmark(const std::string& name,
73 WebViewBenchmarkSupport::PaintMode paint_mode)
74 : CustomPaintBenchmark(name, paint_mode) { }
76 private:
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 {
85 public:
86 CanvasCountBenchmark(const std::string& name,
87 WebViewBenchmarkSupport::PaintMode paint_mode)
88 : content::RenderingBenchmark(name),
89 canvas_count_(0),
90 paint_mode_(paint_mode) { }
92 virtual WebCanvas* willPaint(const WebSize& size) OVERRIDE {
93 ++canvas_count_;
94 return SkCreateNullCanvas();
97 virtual void didPaint(WebCanvas* canvas) OVERRIDE {
98 delete canvas;
101 virtual double Run(WebViewBenchmarkSupport* support) OVERRIDE {
102 canvas_count_ = 0;
103 support->paint(this, paint_mode_);
104 return canvas_count_;
106 private:
107 int canvas_count_;
108 const WebViewBenchmarkSupport::PaintMode paint_mode_;
111 class NullCanvasPaintBenchmark : public CustomPaintBenchmark {
112 public:
113 NullCanvasPaintBenchmark(const std::string& name,
114 WebViewBenchmarkSupport::PaintMode paint_mode)
115 : CustomPaintBenchmark(name, paint_mode) { }
117 private:
118 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE {
119 return SkCreateNullCanvas();
123 class SkPicturePaintBenchmark : public CustomPaintBenchmark {
124 public:
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);
135 private:
136 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE {
137 return picture_.beginRecording(size.width, size.height);
140 SkPicture picture_;
143 // Base class for timing the replaying of the SkPicture into canvases.
144 class TiledReplayBenchmark
145 : public content::RenderingBenchmark,
146 public WebViewBenchmarkSupport::PaintClient {
147 public:
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) {
166 WebRect tile = *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();
182 private:
183 virtual vector<WebRect> GetRepaintTiles(const WebSize& layer_size) const = 0;
185 TimeDelta paint_time_total_;
186 SkPicture picture_;
187 const WebViewBenchmarkSupport::PaintMode paint_mode_;
190 class SquareTiledReplayBenchmark : public TiledReplayBenchmark {
191 public:
192 SquareTiledReplayBenchmark(const std::string& name,
193 WebViewBenchmarkSupport::PaintMode paint_mode,
194 int tile_size)
195 : TiledReplayBenchmark(name, paint_mode),
196 tile_size_(tile_size) {
197 CHECK_GT(tile_size, 0);
200 private:
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));
211 return tiles;
214 int tile_size_;
217 class LayerWidthTiledReplayBenchmark : public TiledReplayBenchmark {
218 public:
219 LayerWidthTiledReplayBenchmark(const std::string& name,
220 WebViewBenchmarkSupport::PaintMode paint_mode,
221 int tile_height)
222 : TiledReplayBenchmark(name, paint_mode),
223 tile_height_(tile_height) {
224 CHECK_GT(tile_height, 0);
227 private:
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));
235 return tiles;
238 int tile_height_;
241 } // anonymous namespace
243 namespace content {
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(
254 "LayerCount",
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,
262 256));
263 benchmarks.push_back(new SquareTiledReplayBenchmark(
264 "RepaintEverythingTo128x128BitmapMs",
265 WebViewBenchmarkSupport::PaintModeEverything,
266 128));
267 benchmarks.push_back(new SquareTiledReplayBenchmark(
268 "RepaintEverythingTo512x512BitmapMs",
269 WebViewBenchmarkSupport::PaintModeEverything,
270 512));
271 benchmarks.push_back(new LayerWidthTiledReplayBenchmark(
272 "RepaintEverythingToLayerWidthx256BitmapMs",
273 WebViewBenchmarkSupport::PaintModeEverything,
274 256));
275 benchmarks.push_back(new LayerWidthTiledReplayBenchmark(
276 "RepaintEverythingToLayerWidthx128BitmapMs",
277 WebViewBenchmarkSupport::PaintModeEverything,
278 128));
279 benchmarks.push_back(new LayerWidthTiledReplayBenchmark(
280 "RepaintEverythingToLayerWidthx64BitmapMs",
281 WebViewBenchmarkSupport::PaintModeEverything,
282 64));
283 benchmarks.push_back(new LayerWidthTiledReplayBenchmark(
284 "RepaintEverythingToLayerWidthx512BitmapMs",
285 WebViewBenchmarkSupport::PaintModeEverything,
286 512));
287 return benchmarks.Pass();
290 } // namespace content