OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/renderer/all_rendering_benchmarks.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
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/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/public/platform/WebRect.h" | |
21 #include "third_party/WebKit/public/platform/WebSize.h" | |
22 #include "third_party/WebKit/public/web/WebViewBenchmarkSupport.h" | |
23 | |
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; | |
31 | |
32 namespace { | |
33 | |
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) { } | |
44 | |
45 virtual WebCanvas* willPaint(const WebSize& size) OVERRIDE { | |
46 WebCanvas* canvas = createCanvas(size); | |
47 before_time_ = TimeTicks::HighResNow(); | |
48 return canvas; | |
49 } | |
50 | |
51 virtual void didPaint(WebCanvas* canvas) OVERRIDE { | |
52 paint_time_total_ += (TimeTicks::HighResNow() - before_time_); | |
53 delete canvas; | |
54 } | |
55 | |
56 virtual double Run(WebViewBenchmarkSupport* support) OVERRIDE { | |
57 paint_time_total_ = TimeDelta(); | |
58 support->paint(this, paint_mode_); | |
59 return paint_time_total_.InMillisecondsF(); | |
60 } | |
61 | |
62 private: | |
63 virtual WebCanvas* createCanvas(const WebSize& size) = 0; | |
64 | |
65 TimeTicks before_time_; | |
66 TimeDelta paint_time_total_; | |
67 const WebViewBenchmarkSupport::PaintMode paint_mode_; | |
68 }; | |
69 | |
70 class BitmapCanvasPaintBenchmark : public CustomPaintBenchmark { | |
71 public: | |
72 BitmapCanvasPaintBenchmark(const std::string& name, | |
73 WebViewBenchmarkSupport::PaintMode paint_mode) | |
74 : CustomPaintBenchmark(name, paint_mode) { } | |
75 | |
76 private: | |
77 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { | |
78 return skia::CreateBitmapCanvas(size.width, size.height, false); | |
79 } | |
80 }; | |
81 | |
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) { } | |
91 | |
92 virtual WebCanvas* willPaint(const WebSize& size) OVERRIDE { | |
93 ++canvas_count_; | |
94 return SkCreateNullCanvas(); | |
95 } | |
96 | |
97 virtual void didPaint(WebCanvas* canvas) OVERRIDE { | |
98 delete canvas; | |
99 } | |
100 | |
101 virtual double Run(WebViewBenchmarkSupport* support) OVERRIDE { | |
102 canvas_count_ = 0; | |
103 support->paint(this, paint_mode_); | |
104 return canvas_count_; | |
105 } | |
106 private: | |
107 int canvas_count_; | |
108 const WebViewBenchmarkSupport::PaintMode paint_mode_; | |
109 }; | |
110 | |
111 class NullCanvasPaintBenchmark : public CustomPaintBenchmark { | |
112 public: | |
113 NullCanvasPaintBenchmark(const std::string& name, | |
114 WebViewBenchmarkSupport::PaintMode paint_mode) | |
115 : CustomPaintBenchmark(name, paint_mode) { } | |
116 | |
117 private: | |
118 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { | |
119 return SkCreateNullCanvas(); | |
120 } | |
121 }; | |
122 | |
123 class SkPicturePaintBenchmark : public CustomPaintBenchmark { | |
124 public: | |
125 SkPicturePaintBenchmark(const std::string& name, | |
126 WebViewBenchmarkSupport::PaintMode paint_mode) | |
127 : CustomPaintBenchmark(name, paint_mode) { } | |
128 | |
129 virtual void didPaint(WebCanvas* canvas) OVERRIDE { | |
130 DCHECK(picture_.getRecordingCanvas() == canvas); | |
131 picture_.endRecording(); | |
132 CustomPaintBenchmark::didPaint(NULL); | |
133 } | |
134 | |
135 private: | |
136 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { | |
137 return picture_.beginRecording(size.width, size.height); | |
138 } | |
139 | |
140 SkPicture picture_; | |
141 }; | |
142 | |
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) {} | |
152 | |
153 virtual WebCanvas* willPaint(const WebSize& size) OVERRIDE { | |
154 return picture_.beginRecording(size.width, size.height); | |
155 } | |
156 | |
157 virtual void didPaint(WebCanvas* canvas) OVERRIDE { | |
158 DCHECK(picture_.getRecordingCanvas() == canvas); | |
159 picture_.endRecording(); | |
160 | |
161 const vector<WebRect> repaint_tiles = GetRepaintTiles( | |
162 WebSize(picture_.width(), picture_.height())); | |
163 | |
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); | |
173 } | |
174 } | |
175 | |
176 virtual double Run(WebViewBenchmarkSupport* support) OVERRIDE { | |
177 paint_time_total_ = TimeDelta(); | |
178 support->paint(this, paint_mode_); | |
179 return paint_time_total_.InMillisecondsF(); | |
180 } | |
181 | |
182 private: | |
183 virtual vector<WebRect> GetRepaintTiles(const WebSize& layer_size) const = 0; | |
184 | |
185 TimeDelta paint_time_total_; | |
186 SkPicture picture_; | |
187 const WebViewBenchmarkSupport::PaintMode paint_mode_; | |
188 }; | |
189 | |
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); | |
198 } | |
199 | |
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)); | |
209 } | |
210 } | |
211 return tiles; | |
212 } | |
213 | |
214 int tile_size_; | |
215 }; | |
216 | |
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); | |
225 } | |
226 | |
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)); | |
234 } | |
235 return tiles; | |
236 } | |
237 | |
238 int tile_height_; | |
239 }; | |
240 | |
241 } // anonymous namespace | |
242 | |
243 namespace content { | |
244 | |
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(); | |
288 } | |
289 | |
290 } // namespace content | |
OLD | NEW |