OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/skia_benchmarking_extension.h" | 5 #include "content/renderer/skia_benchmarking_extension.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "cc/base/math_util.h" | 10 #include "cc/base/math_util.h" |
11 #include "cc/resources/picture.h" | 11 #include "cc/resources/picture.h" |
12 #include "content/public/child/v8_value_converter.h" | 12 #include "content/public/child/v8_value_converter.h" |
13 #include "content/renderer/chrome_object_extensions_utils.h" | 13 #include "content/renderer/chrome_object_extensions_utils.h" |
14 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
15 #include "gin/arguments.h" | 15 #include "gin/arguments.h" |
16 #include "gin/handle.h" | 16 #include "gin/handle.h" |
17 #include "gin/object_template_builder.h" | 17 #include "gin/object_template_builder.h" |
18 #include "skia/ext/benchmarking_canvas.h" | 18 #include "skia/ext/benchmarking_canvas.h" |
19 #include "third_party/WebKit/public/web/WebArrayBuffer.h" | 19 #include "third_party/WebKit/public/web/WebArrayBuffer.h" |
20 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h" | 20 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h" |
21 #include "third_party/WebKit/public/web/WebFrame.h" | 21 #include "third_party/WebKit/public/web/WebFrame.h" |
22 #include "third_party/WebKit/public/web/WebKit.h" | 22 #include "third_party/WebKit/public/web/WebKit.h" |
23 #include "third_party/skia/include/core/SkCanvas.h" | 23 #include "third_party/skia/include/core/SkCanvas.h" |
24 #include "third_party/skia/include/core/SkColorPriv.h" | 24 #include "third_party/skia/include/core/SkColorPriv.h" |
25 #include "third_party/skia/include/core/SkGraphics.h" | 25 #include "third_party/skia/include/core/SkGraphics.h" |
| 26 #include "third_party/skia/include/core/SkPicture.h" |
26 #include "third_party/skia/include/core/SkStream.h" | 27 #include "third_party/skia/include/core/SkStream.h" |
27 #include "third_party/skia/src/utils/debugger/SkDebugCanvas.h" | |
28 #include "third_party/skia/src/utils/debugger/SkDrawCommand.h" | |
29 #include "ui/gfx/geometry/rect_conversions.h" | 28 #include "ui/gfx/geometry/rect_conversions.h" |
30 #include "ui/gfx/skia_util.h" | 29 #include "ui/gfx/skia_util.h" |
31 #include "v8/include/v8.h" | 30 #include "v8/include/v8.h" |
32 | 31 |
33 namespace content { | 32 namespace content { |
34 | 33 |
35 namespace { | 34 namespace { |
36 | 35 |
37 scoped_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate, | 36 scoped_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate, |
38 v8::Handle<v8::Value> arg) { | 37 v8::Handle<v8::Value> arg) { |
(...skipping 12 matching lines...) Expand all Loading... |
51 } | 50 } |
52 | 51 |
53 scoped_refptr<cc::Picture> ParsePictureHash(v8::Isolate* isolate, | 52 scoped_refptr<cc::Picture> ParsePictureHash(v8::Isolate* isolate, |
54 v8::Handle<v8::Value> arg) { | 53 v8::Handle<v8::Value> arg) { |
55 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); | 54 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); |
56 if (!picture_value) | 55 if (!picture_value) |
57 return NULL; | 56 return NULL; |
58 return cc::Picture::CreateFromValue(picture_value.get()); | 57 return cc::Picture::CreateFromValue(picture_value.get()); |
59 } | 58 } |
60 | 59 |
| 60 class PicturePlaybackController : public SkPicture::AbortCallback { |
| 61 public: |
| 62 PicturePlaybackController(const skia::BenchmarkingCanvas& canvas, |
| 63 size_t count) |
| 64 : canvas_(canvas), playback_count_(count) {} |
| 65 |
| 66 bool abort() override { return canvas_.CommandCount() > playback_count_; } |
| 67 |
| 68 private: |
| 69 const skia::BenchmarkingCanvas& canvas_; |
| 70 size_t playback_count_; |
| 71 }; |
| 72 |
61 } // namespace | 73 } // namespace |
62 | 74 |
63 gin::WrapperInfo SkiaBenchmarking::kWrapperInfo = {gin::kEmbedderNativeGin}; | 75 gin::WrapperInfo SkiaBenchmarking::kWrapperInfo = {gin::kEmbedderNativeGin}; |
64 | 76 |
65 // static | 77 // static |
66 void SkiaBenchmarking::Install(blink::WebFrame* frame) { | 78 void SkiaBenchmarking::Install(blink::WebFrame* frame) { |
67 v8::Isolate* isolate = blink::mainThreadIsolate(); | 79 v8::Isolate* isolate = blink::mainThreadIsolate(); |
68 v8::HandleScope handle_scope(isolate); | 80 v8::HandleScope handle_scope(isolate); |
69 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 81 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
70 if (context.IsEmpty()) | 82 if (context.IsEmpty()) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 if (!bitmap.tryAllocN32Pixels(snapped_clip.width(), snapped_clip.height())) | 168 if (!bitmap.tryAllocN32Pixels(snapped_clip.width(), snapped_clip.height())) |
157 return; | 169 return; |
158 bitmap.eraseARGB(0, 0, 0, 0); | 170 bitmap.eraseARGB(0, 0, 0, 0); |
159 | 171 |
160 SkCanvas canvas(bitmap); | 172 SkCanvas canvas(bitmap); |
161 canvas.translate(SkFloatToScalar(-clip.x()), SkFloatToScalar(-clip.y())); | 173 canvas.translate(SkFloatToScalar(-clip.x()), SkFloatToScalar(-clip.y())); |
162 canvas.clipRect(gfx::RectToSkRect(snapped_clip)); | 174 canvas.clipRect(gfx::RectToSkRect(snapped_clip)); |
163 canvas.scale(scale, scale); | 175 canvas.scale(scale, scale); |
164 canvas.translate(picture->LayerRect().x(), picture->LayerRect().y()); | 176 canvas.translate(picture->LayerRect().x(), picture->LayerRect().y()); |
165 | 177 |
166 // First, build a debug canvas for the given picture. | 178 skia::BenchmarkingCanvas benchmarking_canvas( |
167 SkDebugCanvas debug_canvas(picture->LayerRect().width(), | 179 &canvas, |
168 picture->LayerRect().height()); | 180 overdraw ? skia::BenchmarkingCanvas::kOverdrawVisualization_Flag : 0); |
169 picture->Replay(&debug_canvas); | 181 size_t playback_count = |
170 | 182 (stop_index < 0) ? std::numeric_limits<size_t>::max() : stop_index; |
171 // Raster the requested command subset into the bitmap-backed canvas. | 183 PicturePlaybackController controller(benchmarking_canvas, playback_count); |
172 int last_index = debug_canvas.getSize() - 1; | 184 picture->Replay(&benchmarking_canvas, &controller); |
173 if (last_index >= 0) { | |
174 debug_canvas.setOverdrawViz(overdraw); | |
175 debug_canvas.drawTo( | |
176 &canvas, | |
177 stop_index < 0 ? last_index : std::min(last_index, stop_index)); | |
178 } | |
179 | 185 |
180 blink::WebArrayBuffer buffer = | 186 blink::WebArrayBuffer buffer = |
181 blink::WebArrayBuffer::create(bitmap.getSize(), 1); | 187 blink::WebArrayBuffer::create(bitmap.getSize(), 1); |
182 uint32* packed_pixels = reinterpret_cast<uint32*>(bitmap.getPixels()); | 188 uint32* packed_pixels = reinterpret_cast<uint32*>(bitmap.getPixels()); |
183 uint8* buffer_pixels = reinterpret_cast<uint8*>(buffer.data()); | 189 uint8* buffer_pixels = reinterpret_cast<uint8*>(buffer.data()); |
184 // Swizzle from native Skia format to RGBA as we copy out. | 190 // Swizzle from native Skia format to RGBA as we copy out. |
185 for (size_t i = 0; i < bitmap.getSize(); i += 4) { | 191 for (size_t i = 0; i < bitmap.getSize(); i += 4) { |
186 uint32 c = packed_pixels[i >> 2]; | 192 uint32 c = packed_pixels[i >> 2]; |
187 buffer_pixels[i] = SkGetPackedR32(c); | 193 buffer_pixels[i] = SkGetPackedR32(c); |
188 buffer_pixels[i + 1] = SkGetPackedG32(c); | 194 buffer_pixels[i + 1] = SkGetPackedG32(c); |
(...skipping 17 matching lines...) Expand all Loading... |
206 v8::Isolate* isolate = args->isolate(); | 212 v8::Isolate* isolate = args->isolate(); |
207 if (args->PeekNext().IsEmpty()) | 213 if (args->PeekNext().IsEmpty()) |
208 return; | 214 return; |
209 v8::Handle<v8::Value> picture_handle; | 215 v8::Handle<v8::Value> picture_handle; |
210 args->GetNext(&picture_handle); | 216 args->GetNext(&picture_handle); |
211 scoped_refptr<cc::Picture> picture = | 217 scoped_refptr<cc::Picture> picture = |
212 ParsePictureHash(isolate, picture_handle); | 218 ParsePictureHash(isolate, picture_handle); |
213 if (!picture.get()) | 219 if (!picture.get()) |
214 return; | 220 return; |
215 | 221 |
216 gfx::Rect bounds = picture->LayerRect(); | 222 SkCanvas canvas(picture->LayerRect().width(), picture->LayerRect().height()); |
217 SkDebugCanvas canvas(bounds.width(), bounds.height()); | 223 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); |
218 picture->Replay(&canvas); | 224 picture->Replay(&benchmarking_canvas); |
219 | 225 |
220 v8::Handle<v8::Array> result = v8::Array::New(isolate, canvas.getSize()); | 226 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); |
221 for (int i = 0; i < canvas.getSize(); ++i) { | 227 scoped_ptr<content::V8ValueConverter> converter( |
222 SkDrawCommand::OpType cmd_type = canvas.getDrawCommandAt(i)->getType(); | 228 content::V8ValueConverter::create()); |
223 v8::Handle<v8::Object> cmd = v8::Object::New(isolate); | |
224 cmd->Set(v8::String::NewFromUtf8(isolate, "cmd_type"), | |
225 v8::Integer::New(isolate, cmd_type)); | |
226 cmd->Set(v8::String::NewFromUtf8(isolate, "cmd_string"), | |
227 v8::String::NewFromUtf8( | |
228 isolate, SkDrawCommand::GetCommandString(cmd_type))); | |
229 | 229 |
230 const SkTDArray<SkString*>* info = canvas.getCommandInfo(i); | 230 args->Return(converter->ToV8Value(&benchmarking_canvas.Commands(), context)); |
231 DCHECK(info); | |
232 | |
233 v8::Local<v8::Array> v8_info = v8::Array::New(isolate, info->count()); | |
234 for (int j = 0; j < info->count(); ++j) { | |
235 const SkString* info_str = (*info)[j]; | |
236 DCHECK(info_str); | |
237 v8_info->Set(j, v8::String::NewFromUtf8(isolate, info_str->c_str())); | |
238 } | |
239 | |
240 cmd->Set(v8::String::NewFromUtf8(isolate, "info"), v8_info); | |
241 | |
242 result->Set(i, cmd); | |
243 } | |
244 | |
245 args->Return(result.As<v8::Object>()); | |
246 } | 231 } |
247 | 232 |
248 void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) { | 233 void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) { |
249 v8::Isolate* isolate = args->isolate(); | 234 v8::Isolate* isolate = args->isolate(); |
250 if (args->PeekNext().IsEmpty()) | 235 if (args->PeekNext().IsEmpty()) |
251 return; | 236 return; |
252 v8::Handle<v8::Value> picture_handle; | 237 v8::Handle<v8::Value> picture_handle; |
253 args->GetNext(&picture_handle); | 238 args->GetNext(&picture_handle); |
254 scoped_refptr<cc::Picture> picture = | 239 scoped_refptr<cc::Picture> picture = |
255 ParsePictureHash(isolate, picture_handle); | 240 ParsePictureHash(isolate, picture_handle); |
256 if (!picture.get()) | 241 if (!picture.get()) |
257 return; | 242 return; |
258 | 243 |
259 gfx::Rect bounds = picture->LayerRect(); | 244 gfx::Rect bounds = picture->LayerRect(); |
260 | 245 |
261 // Measure the total time by drawing straight into a bitmap-backed canvas. | 246 // Measure the total time by drawing straight into a bitmap-backed canvas. |
262 SkBitmap bitmap; | 247 SkBitmap bitmap; |
263 bitmap.allocN32Pixels(bounds.width(), bounds.height()); | 248 bitmap.allocN32Pixels(bounds.width(), bounds.height()); |
264 SkCanvas bitmap_canvas(bitmap); | 249 SkCanvas bitmap_canvas(bitmap); |
265 bitmap_canvas.clear(SK_ColorTRANSPARENT); | 250 bitmap_canvas.clear(SK_ColorTRANSPARENT); |
266 base::TimeTicks t0 = base::TimeTicks::Now(); | 251 base::TimeTicks t0 = base::TimeTicks::Now(); |
267 picture->Replay(&bitmap_canvas); | 252 picture->Replay(&bitmap_canvas); |
268 base::TimeDelta total_time = base::TimeTicks::Now() - t0; | 253 base::TimeDelta total_time = base::TimeTicks::Now() - t0; |
269 | 254 |
270 // Gather per-op timing info by drawing into a BenchmarkingCanvas. | 255 // Gather per-op timing info by drawing into a BenchmarkingCanvas. |
271 skia::BenchmarkingCanvas benchmarking_canvas(bounds.width(), bounds.height()); | 256 SkCanvas canvas(bitmap); |
| 257 canvas.clear(SK_ColorTRANSPARENT); |
| 258 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); |
272 picture->Replay(&benchmarking_canvas); | 259 picture->Replay(&benchmarking_canvas); |
273 | 260 |
274 v8::Local<v8::Array> op_times = | 261 v8::Local<v8::Array> op_times = |
275 v8::Array::New(isolate, benchmarking_canvas.CommandCount()); | 262 v8::Array::New(isolate, benchmarking_canvas.CommandCount()); |
276 for (size_t i = 0; i < benchmarking_canvas.CommandCount(); ++i) | 263 for (size_t i = 0; i < benchmarking_canvas.CommandCount(); ++i) { |
277 op_times->Set(i, v8::Number::New(isolate, benchmarking_canvas.GetTime(i))); | 264 op_times->Set(i, v8::Number::New(isolate, benchmarking_canvas.GetTime(i))); |
| 265 } |
278 | 266 |
279 v8::Handle<v8::Object> result = v8::Object::New(isolate); | 267 v8::Handle<v8::Object> result = v8::Object::New(isolate); |
280 result->Set(v8::String::NewFromUtf8(isolate, "total_time"), | 268 result->Set(v8::String::NewFromUtf8(isolate, "total_time"), |
281 v8::Number::New(isolate, total_time.InMillisecondsF())); | 269 v8::Number::New(isolate, total_time.InMillisecondsF())); |
282 result->Set(v8::String::NewFromUtf8(isolate, "cmd_times"), op_times); | 270 result->Set(v8::String::NewFromUtf8(isolate, "cmd_times"), op_times); |
283 | 271 |
284 args->Return(result); | 272 args->Return(result); |
285 } | 273 } |
286 | 274 |
287 void SkiaBenchmarking::GetInfo(gin::Arguments* args) { | 275 void SkiaBenchmarking::GetInfo(gin::Arguments* args) { |
(...skipping 10 matching lines...) Expand all Loading... |
298 v8::Handle<v8::Object> result = v8::Object::New(isolate); | 286 v8::Handle<v8::Object> result = v8::Object::New(isolate); |
299 result->Set(v8::String::NewFromUtf8(isolate, "width"), | 287 result->Set(v8::String::NewFromUtf8(isolate, "width"), |
300 v8::Number::New(isolate, picture->LayerRect().width())); | 288 v8::Number::New(isolate, picture->LayerRect().width())); |
301 result->Set(v8::String::NewFromUtf8(isolate, "height"), | 289 result->Set(v8::String::NewFromUtf8(isolate, "height"), |
302 v8::Number::New(isolate, picture->LayerRect().height())); | 290 v8::Number::New(isolate, picture->LayerRect().height())); |
303 | 291 |
304 args->Return(result); | 292 args->Return(result); |
305 } | 293 } |
306 | 294 |
307 } // namespace content | 295 } // namespace content |
OLD | NEW |