OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/renderer/all_rendering_benchmarks.h" | 5 #include "content/renderer/all_rendering_benchmarks.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <string> | 8 #include <string> |
| 9 #include <vector> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/callback.h" | 12 #include "base/callback.h" |
11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/scoped_ptr.h" |
12 #include "base/time.h" | 15 #include "base/time.h" |
13 #include "content/renderer/rendering_benchmark.h" | 16 #include "content/renderer/rendering_benchmark.h" |
14 #include "content/renderer/rendering_benchmark_results.h" | 17 #include "content/renderer/rendering_benchmark_results.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "skia/ext/platform_canvas.h" |
16 #include "third_party/skia/include/core/SkDevice.h" | 19 #include "third_party/skia/include/core/SkPicture.h" |
17 #include "third_party/skia/include/utils/SkNullCanvas.h" | 20 #include "third_party/skia/include/utils/SkNullCanvas.h" |
18 #include "third_party/WebKit/Source/Platform/chromium/public/WebCanvas.h" | 21 #include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h" |
19 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" | 22 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewBenchmarkSuppo
rt.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewBenchmarkSuppo
rt.h" |
21 | 24 |
22 using base::TimeDelta; | 25 using base::TimeDelta; |
23 using base::TimeTicks; | 26 using base::TimeTicks; |
24 using WebKit::WebSize; | 27 using WebKit::WebSize; |
25 using WebKit::WebCanvas; | 28 using WebKit::WebCanvas; |
26 using WebKit::WebViewBenchmarkSupport; | 29 using WebKit::WebViewBenchmarkSupport; |
| 30 using WebKit::WebRect; |
| 31 using std::vector; |
27 | 32 |
28 namespace { | 33 namespace { |
29 | 34 |
| 35 // This is a base class for timing the painting of the current webpage to |
| 36 // custom WebCanvases. |
30 class CustomPaintBenchmark | 37 class CustomPaintBenchmark |
31 : public content::RenderingBenchmark, | 38 : public content::RenderingBenchmark, |
32 public WebViewBenchmarkSupport::PaintClient { | 39 public WebViewBenchmarkSupport::PaintClient { |
33 public: | 40 public: |
34 CustomPaintBenchmark(const std::string& name, | 41 CustomPaintBenchmark(const std::string& name, |
35 WebViewBenchmarkSupport::PaintMode paint_mode) | 42 WebViewBenchmarkSupport::PaintMode paint_mode) |
36 : content::RenderingBenchmark(name), | 43 : content::RenderingBenchmark(name), |
37 paint_mode_(paint_mode) { } | 44 paint_mode_(paint_mode) { } |
38 | 45 |
39 virtual WebCanvas* willPaint(const WebSize& size) OVERRIDE { | 46 virtual WebCanvas* willPaint(const WebSize& size) OVERRIDE { |
40 WebCanvas* canvas = createCanvas(size); | 47 WebCanvas* canvas = createCanvas(size); |
41 before_time_ = TimeTicks::Now(); | 48 before_time_ = TimeTicks::HighResNow(); |
42 return canvas; | 49 return canvas; |
43 } | 50 } |
44 | 51 |
45 virtual void didPaint(WebCanvas* canvas) OVERRIDE { | 52 virtual void didPaint(WebCanvas* canvas) OVERRIDE { |
46 paint_time_total_ += (TimeTicks::Now() - before_time_); | 53 paint_time_total_ += (TimeTicks::HighResNow() - before_time_); |
47 delete canvas; | 54 delete canvas; |
48 } | 55 } |
49 | 56 |
50 virtual void Run(content::RenderingBenchmarkResults* results, | 57 virtual void Run(content::RenderingBenchmarkResults* results, |
51 WebViewBenchmarkSupport* support) { | 58 WebViewBenchmarkSupport* support) OVERRIDE { |
52 paint_time_total_ = TimeDelta(); | 59 paint_time_total_ = TimeDelta(); |
53 support->paint(this, paint_mode_); | 60 support->paint(this, paint_mode_); |
54 results->AddResult(name(), | 61 results->AddResult(name(), |
55 "paintTime", | 62 "paintTime", |
56 "s", | 63 "s", |
57 paint_time_total_.InSecondsF()); | 64 paint_time_total_.InSecondsF()); |
58 } | 65 } |
59 | 66 |
60 private: | 67 private: |
61 virtual WebCanvas* createCanvas(const WebSize& size) = 0; | 68 virtual WebCanvas* createCanvas(const WebSize& size) = 0; |
62 | 69 |
63 TimeTicks before_time_; | 70 TimeTicks before_time_; |
64 TimeDelta paint_time_total_; | 71 TimeDelta paint_time_total_; |
65 const WebViewBenchmarkSupport::PaintMode paint_mode_; | 72 const WebViewBenchmarkSupport::PaintMode paint_mode_; |
66 }; | 73 }; |
67 | 74 |
68 class BitmapCanvasPaintBenchmark : public CustomPaintBenchmark { | 75 class BitmapCanvasPaintBenchmark : public CustomPaintBenchmark { |
69 public: | 76 public: |
70 BitmapCanvasPaintBenchmark(const std::string& name, | 77 BitmapCanvasPaintBenchmark(const std::string& name, |
71 WebViewBenchmarkSupport::PaintMode paint_mode) | 78 WebViewBenchmarkSupport::PaintMode paint_mode) |
72 : CustomPaintBenchmark(name, paint_mode) { } | 79 : CustomPaintBenchmark(name, paint_mode) { } |
73 | 80 |
74 private: | 81 private: |
75 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { | 82 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { |
76 SkDevice* device = new SkDevice(SkBitmap::kARGB_8888_Config, | 83 return skia::CreateBitmapCanvas(size.width, size.height, false); |
77 size.width, | |
78 size.height, | |
79 false); | |
80 WebCanvas* canvas = new WebCanvas(device); | |
81 device->unref(); | |
82 return canvas; | |
83 } | 84 } |
84 }; | 85 }; |
85 | 86 |
86 class NullCanvasPaintBenchmark : public CustomPaintBenchmark { | 87 class NullCanvasPaintBenchmark : public CustomPaintBenchmark { |
87 public: | 88 public: |
88 NullCanvasPaintBenchmark(const std::string& name, | 89 NullCanvasPaintBenchmark(const std::string& name, |
89 WebViewBenchmarkSupport::PaintMode paint_mode) | 90 WebViewBenchmarkSupport::PaintMode paint_mode) |
90 : CustomPaintBenchmark(name, paint_mode) { } | 91 : CustomPaintBenchmark(name, paint_mode), |
| 92 canvas_count_(0) { } |
| 93 |
| 94 virtual void Run(content::RenderingBenchmarkResults* results, |
| 95 WebViewBenchmarkSupport* support) OVERRIDE { |
| 96 canvas_count_ = 0; |
| 97 CustomPaintBenchmark::Run(results, support); |
| 98 results->AddResult(name(), |
| 99 "canvasCount", |
| 100 "i", |
| 101 canvas_count_); |
| 102 } |
91 | 103 |
92 private: | 104 private: |
93 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { | 105 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { |
| 106 ++canvas_count_; |
94 return SkCreateNullCanvas(); | 107 return SkCreateNullCanvas(); |
95 } | 108 } |
| 109 |
| 110 int canvas_count_; |
96 }; | 111 }; |
| 112 |
| 113 class SkPicturePaintBenchmark : public CustomPaintBenchmark { |
| 114 public: |
| 115 SkPicturePaintBenchmark(const std::string& name, |
| 116 WebViewBenchmarkSupport::PaintMode paint_mode) |
| 117 : CustomPaintBenchmark(name, paint_mode) { } |
| 118 |
| 119 virtual void didPaint(WebCanvas* canvas) OVERRIDE { |
| 120 DCHECK(picture_.getRecordingCanvas() == canvas); |
| 121 picture_.endRecording(); |
| 122 CustomPaintBenchmark::didPaint(NULL); |
| 123 } |
| 124 |
| 125 private: |
| 126 virtual WebCanvas* createCanvas(const WebSize& size) OVERRIDE { |
| 127 return picture_.beginRecording(size.width, size.height); |
| 128 } |
| 129 |
| 130 SkPicture picture_; |
| 131 }; |
| 132 |
| 133 // Base class for timing the replaying of the SkPicture into canvases. |
| 134 class TiledReplayBenchmark |
| 135 : public content::RenderingBenchmark, |
| 136 public WebViewBenchmarkSupport::PaintClient { |
| 137 public: |
| 138 TiledReplayBenchmark(const std::string& name, |
| 139 WebViewBenchmarkSupport::PaintMode paint_mode) |
| 140 : RenderingBenchmark(name), |
| 141 paint_mode_(paint_mode) {} |
| 142 |
| 143 virtual WebCanvas* willPaint(const WebSize& size) OVERRIDE { |
| 144 return picture_.beginRecording(size.width, size.height); |
| 145 } |
| 146 |
| 147 virtual void didPaint(WebCanvas* canvas) OVERRIDE { |
| 148 DCHECK(picture_.getRecordingCanvas() == canvas); |
| 149 picture_.endRecording(); |
| 150 |
| 151 const vector<WebRect> repaint_tiles = GetRepaintTiles( |
| 152 WebSize(picture_.width(), picture_.height())); |
| 153 |
| 154 vector<WebRect>::const_iterator it; |
| 155 for (it = repaint_tiles.begin(); it != repaint_tiles.end(); ++it) { |
| 156 WebRect tile = *it; |
| 157 scoped_ptr<WebCanvas> canvas( |
| 158 skia::CreateBitmapCanvas(tile.width, tile.height, false)); |
| 159 TimeTicks before_time = TimeTicks::HighResNow(); |
| 160 canvas->translate(-tile.x, -tile.y); |
| 161 picture_.draw(canvas.get()); |
| 162 paint_time_total_ += (TimeTicks::HighResNow() - before_time); |
| 163 } |
| 164 } |
| 165 |
| 166 virtual void Run(content::RenderingBenchmarkResults* results, |
| 167 WebViewBenchmarkSupport* support) { |
| 168 paint_time_total_ = TimeDelta(); |
| 169 support->paint(this, paint_mode_); |
| 170 results->AddResult(name(), |
| 171 "repaintTime", |
| 172 "s", |
| 173 paint_time_total_.InSecondsF()); |
| 174 } |
| 175 |
| 176 private: |
| 177 virtual vector<WebRect> GetRepaintTiles(const WebSize& layer_size) const = 0; |
| 178 |
| 179 TimeDelta paint_time_total_; |
| 180 SkPicture picture_; |
| 181 const WebViewBenchmarkSupport::PaintMode paint_mode_; |
| 182 }; |
| 183 |
| 184 class SquareTiledReplayBenchmark : public TiledReplayBenchmark { |
| 185 public: |
| 186 SquareTiledReplayBenchmark(const std::string& name, |
| 187 WebViewBenchmarkSupport::PaintMode paint_mode, |
| 188 int tile_size) |
| 189 : TiledReplayBenchmark(name, paint_mode), |
| 190 tile_size_(tile_size) { |
| 191 CHECK_GT(tile_size, 0); |
| 192 } |
| 193 |
| 194 private: |
| 195 virtual vector<WebRect> GetRepaintTiles( |
| 196 const WebSize& layer_size) const OVERRIDE { |
| 197 vector<WebRect> tiles; |
| 198 for (int x = 0; x < layer_size.width; x += tile_size_) { |
| 199 for (int y = 0; y < layer_size.height; y += tile_size_) { |
| 200 int width = std::min(layer_size.width - x, tile_size_); |
| 201 int height = std::min(layer_size.height - y, tile_size_); |
| 202 tiles.push_back(WebRect(x, y, width, height)); |
| 203 } |
| 204 } |
| 205 return tiles; |
| 206 } |
| 207 |
| 208 int tile_size_; |
| 209 }; |
| 210 |
| 211 class LayerWidthTiledReplayBenchmark : public TiledReplayBenchmark { |
| 212 public: |
| 213 LayerWidthTiledReplayBenchmark(const std::string& name, |
| 214 WebViewBenchmarkSupport::PaintMode paint_mode, |
| 215 int tile_height) |
| 216 : TiledReplayBenchmark(name, paint_mode), |
| 217 tile_height_(tile_height) { |
| 218 CHECK_GT(tile_height, 0); |
| 219 } |
| 220 |
| 221 private: |
| 222 virtual vector<WebRect> GetRepaintTiles( |
| 223 const WebSize& layer_size) const OVERRIDE { |
| 224 vector<WebRect> tiles; |
| 225 for (int y = 0; y < layer_size.height; y += tile_height_) { |
| 226 int height = std::min(layer_size.height - y, tile_height_); |
| 227 tiles.push_back(WebRect(0, y, layer_size.width, height)); |
| 228 } |
| 229 return tiles; |
| 230 } |
| 231 |
| 232 int tile_height_; |
| 233 }; |
| 234 |
97 } // anonymous namespace | 235 } // anonymous namespace |
98 | 236 |
99 namespace content { | 237 namespace content { |
100 | 238 |
101 ScopedVector<RenderingBenchmark> AllRenderingBenchmarks() { | 239 ScopedVector<RenderingBenchmark> AllRenderingBenchmarks() { |
102 ScopedVector<RenderingBenchmark> benchmarks; | 240 ScopedVector<RenderingBenchmark> benchmarks; |
103 benchmarks.push_back(new BitmapCanvasPaintBenchmark( | 241 benchmarks.push_back(new BitmapCanvasPaintBenchmark( |
104 "PaintEverythingToBitmap", | 242 "PaintEverythingToBitmap", |
105 WebViewBenchmarkSupport::PaintModeEverything)); | 243 WebViewBenchmarkSupport::PaintModeEverything)); |
106 benchmarks.push_back(new NullCanvasPaintBenchmark( | 244 benchmarks.push_back(new NullCanvasPaintBenchmark( |
107 "PaintEverythingToNullCanvas", | 245 "PaintEverythingToNullCanvas", |
108 WebViewBenchmarkSupport::PaintModeEverything)); | 246 WebViewBenchmarkSupport::PaintModeEverything)); |
| 247 benchmarks.push_back(new SkPicturePaintBenchmark( |
| 248 "PaintEverythingToSkPicture", |
| 249 WebViewBenchmarkSupport::PaintModeEverything)); |
| 250 benchmarks.push_back(new SquareTiledReplayBenchmark( |
| 251 "RepaintEverythingTo256x256Bitmap", |
| 252 WebViewBenchmarkSupport::PaintModeEverything, |
| 253 256)); |
| 254 benchmarks.push_back(new SquareTiledReplayBenchmark( |
| 255 "RepaintEverythingTo128x128Bitmap", |
| 256 WebViewBenchmarkSupport::PaintModeEverything, |
| 257 128)); |
| 258 benchmarks.push_back(new SquareTiledReplayBenchmark( |
| 259 "RepaintEverythingTo512x512Bitmap", |
| 260 WebViewBenchmarkSupport::PaintModeEverything, |
| 261 512)); |
| 262 benchmarks.push_back(new LayerWidthTiledReplayBenchmark( |
| 263 "RepaintEverythingToLayerWidthx256Bitmap", |
| 264 WebViewBenchmarkSupport::PaintModeEverything, |
| 265 256)); |
| 266 benchmarks.push_back(new LayerWidthTiledReplayBenchmark( |
| 267 "RepaintEverythingToLayerWidthx128Bitmap", |
| 268 WebViewBenchmarkSupport::PaintModeEverything, |
| 269 128)); |
| 270 benchmarks.push_back(new LayerWidthTiledReplayBenchmark( |
| 271 "RepaintEverythingToLayerWidthx64Bitmap", |
| 272 WebViewBenchmarkSupport::PaintModeEverything, |
| 273 64)); |
| 274 benchmarks.push_back(new LayerWidthTiledReplayBenchmark( |
| 275 "RepaintEverythingToLayerWidthx512Bitmap", |
| 276 WebViewBenchmarkSupport::PaintModeEverything, |
| 277 512)); |
109 return benchmarks.Pass(); | 278 return benchmarks.Pass(); |
110 } | 279 } |
111 | 280 |
112 } // namespace content | 281 } // namespace content |
OLD | NEW |