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" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "third_party/skia/include/core/SkStream.h" | 27 #include "third_party/skia/include/core/SkStream.h" |
28 #include "ui/gfx/geometry/rect_conversions.h" | 28 #include "ui/gfx/geometry/rect_conversions.h" |
29 #include "ui/gfx/skia_util.h" | 29 #include "ui/gfx/skia_util.h" |
30 #include "v8/include/v8.h" | 30 #include "v8/include/v8.h" |
31 | 31 |
32 namespace content { | 32 namespace content { |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 scoped_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate, | 36 scoped_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate, |
37 v8::Handle<v8::Value> arg) { | 37 v8::Local<v8::Value> arg) { |
38 scoped_ptr<content::V8ValueConverter> converter( | 38 scoped_ptr<content::V8ValueConverter> converter( |
39 content::V8ValueConverter::create()); | 39 content::V8ValueConverter::create()); |
40 return scoped_ptr<base::Value>( | 40 return scoped_ptr<base::Value>( |
41 converter->FromV8Value(arg, isolate->GetCurrentContext())); | 41 converter->FromV8Value(arg, isolate->GetCurrentContext())); |
42 } | 42 } |
43 | 43 |
44 scoped_refptr<cc::Picture> ParsePictureStr(v8::Isolate* isolate, | 44 scoped_refptr<cc::Picture> ParsePictureStr(v8::Isolate* isolate, |
45 v8::Handle<v8::Value> arg) { | 45 v8::Local<v8::Value> arg) { |
46 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); | 46 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); |
47 if (!picture_value) | 47 if (!picture_value) |
48 return NULL; | 48 return NULL; |
49 return cc::Picture::CreateFromSkpValue(picture_value.get()); | 49 return cc::Picture::CreateFromSkpValue(picture_value.get()); |
50 } | 50 } |
51 | 51 |
52 scoped_refptr<cc::Picture> ParsePictureHash(v8::Isolate* isolate, | 52 scoped_refptr<cc::Picture> ParsePictureHash(v8::Isolate* isolate, |
53 v8::Handle<v8::Value> arg) { | 53 v8::Local<v8::Value> arg) { |
54 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); | 54 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); |
55 if (!picture_value) | 55 if (!picture_value) |
56 return NULL; | 56 return NULL; |
57 return cc::Picture::CreateFromValue(picture_value.get()); | 57 return cc::Picture::CreateFromValue(picture_value.get()); |
58 } | 58 } |
59 | 59 |
60 class PicturePlaybackController : public SkPicture::AbortCallback { | 60 class PicturePlaybackController : public SkPicture::AbortCallback { |
61 public: | 61 public: |
62 PicturePlaybackController(const skia::BenchmarkingCanvas& canvas, | 62 PicturePlaybackController(const skia::BenchmarkingCanvas& canvas, |
63 size_t count) | 63 size_t count) |
64 : canvas_(canvas), playback_count_(count) {} | 64 : canvas_(canvas), playback_count_(count) {} |
65 | 65 |
66 bool abort() override { return canvas_.CommandCount() > playback_count_; } | 66 bool abort() override { return canvas_.CommandCount() > playback_count_; } |
67 | 67 |
68 private: | 68 private: |
69 const skia::BenchmarkingCanvas& canvas_; | 69 const skia::BenchmarkingCanvas& canvas_; |
70 size_t playback_count_; | 70 size_t playback_count_; |
71 }; | 71 }; |
72 | 72 |
73 } // namespace | 73 } // namespace |
74 | 74 |
75 gin::WrapperInfo SkiaBenchmarking::kWrapperInfo = {gin::kEmbedderNativeGin}; | 75 gin::WrapperInfo SkiaBenchmarking::kWrapperInfo = {gin::kEmbedderNativeGin}; |
76 | 76 |
77 // static | 77 // static |
78 void SkiaBenchmarking::Install(blink::WebFrame* frame) { | 78 void SkiaBenchmarking::Install(blink::WebFrame* frame) { |
79 v8::Isolate* isolate = blink::mainThreadIsolate(); | 79 v8::Isolate* isolate = blink::mainThreadIsolate(); |
80 v8::HandleScope handle_scope(isolate); | 80 v8::HandleScope handle_scope(isolate); |
81 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 81 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
82 if (context.IsEmpty()) | 82 if (context.IsEmpty()) |
83 return; | 83 return; |
84 | 84 |
85 v8::Context::Scope context_scope(context); | 85 v8::Context::Scope context_scope(context); |
86 | 86 |
87 gin::Handle<SkiaBenchmarking> controller = | 87 gin::Handle<SkiaBenchmarking> controller = |
88 gin::CreateHandle(isolate, new SkiaBenchmarking()); | 88 gin::CreateHandle(isolate, new SkiaBenchmarking()); |
89 if (controller.IsEmpty()) | 89 if (controller.IsEmpty()) |
90 return; | 90 return; |
91 | 91 |
92 v8::Handle<v8::Object> chrome = GetOrCreateChromeObject(isolate, | 92 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate, |
93 context->Global()); | 93 context->Global()); |
94 chrome->Set(gin::StringToV8(isolate, "skiaBenchmarking"), controller.ToV8()); | 94 chrome->Set(gin::StringToV8(isolate, "skiaBenchmarking"), controller.ToV8()); |
95 } | 95 } |
96 | 96 |
97 // static | 97 // static |
98 void SkiaBenchmarking::Initialize() { | 98 void SkiaBenchmarking::Initialize() { |
99 DCHECK(RenderThreadImpl::current()); | 99 DCHECK(RenderThreadImpl::current()); |
100 // FIXME: remove this after Skia updates SkGraphics::Init() to be | 100 // FIXME: remove this after Skia updates SkGraphics::Init() to be |
101 // thread-safe and idempotent. | 101 // thread-safe and idempotent. |
102 static bool skia_initialized = false; | 102 static bool skia_initialized = false; |
(...skipping 16 matching lines...) Expand all Loading... |
119 .SetMethod("rasterize", &SkiaBenchmarking::Rasterize) | 119 .SetMethod("rasterize", &SkiaBenchmarking::Rasterize) |
120 .SetMethod("getOps", &SkiaBenchmarking::GetOps) | 120 .SetMethod("getOps", &SkiaBenchmarking::GetOps) |
121 .SetMethod("getOpTimings", &SkiaBenchmarking::GetOpTimings) | 121 .SetMethod("getOpTimings", &SkiaBenchmarking::GetOpTimings) |
122 .SetMethod("getInfo", &SkiaBenchmarking::GetInfo); | 122 .SetMethod("getInfo", &SkiaBenchmarking::GetInfo); |
123 } | 123 } |
124 | 124 |
125 void SkiaBenchmarking::Rasterize(gin::Arguments* args) { | 125 void SkiaBenchmarking::Rasterize(gin::Arguments* args) { |
126 v8::Isolate* isolate = args->isolate(); | 126 v8::Isolate* isolate = args->isolate(); |
127 if (args->PeekNext().IsEmpty()) | 127 if (args->PeekNext().IsEmpty()) |
128 return; | 128 return; |
129 v8::Handle<v8::Value> picture_handle; | 129 v8::Local<v8::Value> picture_handle; |
130 args->GetNext(&picture_handle); | 130 args->GetNext(&picture_handle); |
131 scoped_refptr<cc::Picture> picture = | 131 scoped_refptr<cc::Picture> picture = |
132 ParsePictureHash(isolate, picture_handle); | 132 ParsePictureHash(isolate, picture_handle); |
133 if (!picture.get()) | 133 if (!picture.get()) |
134 return; | 134 return; |
135 | 135 |
136 double scale = 1.0; | 136 double scale = 1.0; |
137 gfx::Rect clip_rect(picture->LayerRect()); | 137 gfx::Rect clip_rect(picture->LayerRect()); |
138 int stop_index = -1; | 138 int stop_index = -1; |
139 bool overdraw = false; | 139 bool overdraw = false; |
140 | 140 |
141 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); | 141 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
142 if (!args->PeekNext().IsEmpty()) { | 142 if (!args->PeekNext().IsEmpty()) { |
143 v8::Handle<v8::Value> params; | 143 v8::Local<v8::Value> params; |
144 args->GetNext(¶ms); | 144 args->GetNext(¶ms); |
145 scoped_ptr<content::V8ValueConverter> converter( | 145 scoped_ptr<content::V8ValueConverter> converter( |
146 content::V8ValueConverter::create()); | 146 content::V8ValueConverter::create()); |
147 scoped_ptr<base::Value> params_value( | 147 scoped_ptr<base::Value> params_value( |
148 converter->FromV8Value(params, context)); | 148 converter->FromV8Value(params, context)); |
149 | 149 |
150 const base::DictionaryValue* params_dict = NULL; | 150 const base::DictionaryValue* params_dict = NULL; |
151 if (params_value.get() && params_value->GetAsDictionary(¶ms_dict)) { | 151 if (params_value.get() && params_value->GetAsDictionary(¶ms_dict)) { |
152 params_dict->GetDouble("scale", &scale); | 152 params_dict->GetDouble("scale", &scale); |
153 params_dict->GetInteger("stop", &stop_index); | 153 params_dict->GetInteger("stop", &stop_index); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 uint8* buffer_pixels = reinterpret_cast<uint8*>(buffer.data()); | 189 uint8* buffer_pixels = reinterpret_cast<uint8*>(buffer.data()); |
190 // Swizzle from native Skia format to RGBA as we copy out. | 190 // Swizzle from native Skia format to RGBA as we copy out. |
191 for (size_t i = 0; i < bitmap.getSize(); i += 4) { | 191 for (size_t i = 0; i < bitmap.getSize(); i += 4) { |
192 uint32 c = packed_pixels[i >> 2]; | 192 uint32 c = packed_pixels[i >> 2]; |
193 buffer_pixels[i] = SkGetPackedR32(c); | 193 buffer_pixels[i] = SkGetPackedR32(c); |
194 buffer_pixels[i + 1] = SkGetPackedG32(c); | 194 buffer_pixels[i + 1] = SkGetPackedG32(c); |
195 buffer_pixels[i + 2] = SkGetPackedB32(c); | 195 buffer_pixels[i + 2] = SkGetPackedB32(c); |
196 buffer_pixels[i + 3] = SkGetPackedA32(c); | 196 buffer_pixels[i + 3] = SkGetPackedA32(c); |
197 } | 197 } |
198 | 198 |
199 v8::Handle<v8::Object> result = v8::Object::New(isolate); | 199 v8::Local<v8::Object> result = v8::Object::New(isolate); |
200 result->Set(v8::String::NewFromUtf8(isolate, "width"), | 200 result->Set(v8::String::NewFromUtf8(isolate, "width"), |
201 v8::Number::New(isolate, snapped_clip.width())); | 201 v8::Number::New(isolate, snapped_clip.width())); |
202 result->Set(v8::String::NewFromUtf8(isolate, "height"), | 202 result->Set(v8::String::NewFromUtf8(isolate, "height"), |
203 v8::Number::New(isolate, snapped_clip.height())); | 203 v8::Number::New(isolate, snapped_clip.height())); |
204 result->Set(v8::String::NewFromUtf8(isolate, "data"), | 204 result->Set(v8::String::NewFromUtf8(isolate, "data"), |
205 blink::WebArrayBufferConverter::toV8Value( | 205 blink::WebArrayBufferConverter::toV8Value( |
206 &buffer, context->Global(), isolate)); | 206 &buffer, context->Global(), isolate)); |
207 | 207 |
208 args->Return(result); | 208 args->Return(result); |
209 } | 209 } |
210 | 210 |
211 void SkiaBenchmarking::GetOps(gin::Arguments* args) { | 211 void SkiaBenchmarking::GetOps(gin::Arguments* args) { |
212 v8::Isolate* isolate = args->isolate(); | 212 v8::Isolate* isolate = args->isolate(); |
213 if (args->PeekNext().IsEmpty()) | 213 if (args->PeekNext().IsEmpty()) |
214 return; | 214 return; |
215 v8::Handle<v8::Value> picture_handle; | 215 v8::Local<v8::Value> picture_handle; |
216 args->GetNext(&picture_handle); | 216 args->GetNext(&picture_handle); |
217 scoped_refptr<cc::Picture> picture = | 217 scoped_refptr<cc::Picture> picture = |
218 ParsePictureHash(isolate, picture_handle); | 218 ParsePictureHash(isolate, picture_handle); |
219 if (!picture.get()) | 219 if (!picture.get()) |
220 return; | 220 return; |
221 | 221 |
222 SkCanvas canvas(picture->LayerRect().width(), picture->LayerRect().height()); | 222 SkCanvas canvas(picture->LayerRect().width(), picture->LayerRect().height()); |
223 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); | 223 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); |
224 picture->Replay(&benchmarking_canvas); | 224 picture->Replay(&benchmarking_canvas); |
225 | 225 |
226 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); | 226 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
227 scoped_ptr<content::V8ValueConverter> converter( | 227 scoped_ptr<content::V8ValueConverter> converter( |
228 content::V8ValueConverter::create()); | 228 content::V8ValueConverter::create()); |
229 | 229 |
230 args->Return(converter->ToV8Value(&benchmarking_canvas.Commands(), context)); | 230 args->Return(converter->ToV8Value(&benchmarking_canvas.Commands(), context)); |
231 } | 231 } |
232 | 232 |
233 void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) { | 233 void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) { |
234 v8::Isolate* isolate = args->isolate(); | 234 v8::Isolate* isolate = args->isolate(); |
235 if (args->PeekNext().IsEmpty()) | 235 if (args->PeekNext().IsEmpty()) |
236 return; | 236 return; |
237 v8::Handle<v8::Value> picture_handle; | 237 v8::Local<v8::Value> picture_handle; |
238 args->GetNext(&picture_handle); | 238 args->GetNext(&picture_handle); |
239 scoped_refptr<cc::Picture> picture = | 239 scoped_refptr<cc::Picture> picture = |
240 ParsePictureHash(isolate, picture_handle); | 240 ParsePictureHash(isolate, picture_handle); |
241 if (!picture.get()) | 241 if (!picture.get()) |
242 return; | 242 return; |
243 | 243 |
244 gfx::Rect bounds = picture->LayerRect(); | 244 gfx::Rect bounds = picture->LayerRect(); |
245 | 245 |
246 // 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. |
247 SkBitmap bitmap; | 247 SkBitmap bitmap; |
248 bitmap.allocN32Pixels(bounds.width(), bounds.height()); | 248 bitmap.allocN32Pixels(bounds.width(), bounds.height()); |
249 SkCanvas bitmap_canvas(bitmap); | 249 SkCanvas bitmap_canvas(bitmap); |
250 bitmap_canvas.clear(SK_ColorTRANSPARENT); | 250 bitmap_canvas.clear(SK_ColorTRANSPARENT); |
251 base::TimeTicks t0 = base::TimeTicks::Now(); | 251 base::TimeTicks t0 = base::TimeTicks::Now(); |
252 picture->Replay(&bitmap_canvas); | 252 picture->Replay(&bitmap_canvas); |
253 base::TimeDelta total_time = base::TimeTicks::Now() - t0; | 253 base::TimeDelta total_time = base::TimeTicks::Now() - t0; |
254 | 254 |
255 // Gather per-op timing info by drawing into a BenchmarkingCanvas. | 255 // Gather per-op timing info by drawing into a BenchmarkingCanvas. |
256 SkCanvas canvas(bitmap); | 256 SkCanvas canvas(bitmap); |
257 canvas.clear(SK_ColorTRANSPARENT); | 257 canvas.clear(SK_ColorTRANSPARENT); |
258 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); | 258 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); |
259 picture->Replay(&benchmarking_canvas); | 259 picture->Replay(&benchmarking_canvas); |
260 | 260 |
261 v8::Local<v8::Array> op_times = | 261 v8::Local<v8::Array> op_times = |
262 v8::Array::New(isolate, benchmarking_canvas.CommandCount()); | 262 v8::Array::New(isolate, benchmarking_canvas.CommandCount()); |
263 for (size_t i = 0; i < benchmarking_canvas.CommandCount(); ++i) { | 263 for (size_t i = 0; i < benchmarking_canvas.CommandCount(); ++i) { |
264 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 } | 265 } |
266 | 266 |
267 v8::Handle<v8::Object> result = v8::Object::New(isolate); | 267 v8::Local<v8::Object> result = v8::Object::New(isolate); |
268 result->Set(v8::String::NewFromUtf8(isolate, "total_time"), | 268 result->Set(v8::String::NewFromUtf8(isolate, "total_time"), |
269 v8::Number::New(isolate, total_time.InMillisecondsF())); | 269 v8::Number::New(isolate, total_time.InMillisecondsF())); |
270 result->Set(v8::String::NewFromUtf8(isolate, "cmd_times"), op_times); | 270 result->Set(v8::String::NewFromUtf8(isolate, "cmd_times"), op_times); |
271 | 271 |
272 args->Return(result); | 272 args->Return(result); |
273 } | 273 } |
274 | 274 |
275 void SkiaBenchmarking::GetInfo(gin::Arguments* args) { | 275 void SkiaBenchmarking::GetInfo(gin::Arguments* args) { |
276 v8::Isolate* isolate = args->isolate(); | 276 v8::Isolate* isolate = args->isolate(); |
277 if (args->PeekNext().IsEmpty()) | 277 if (args->PeekNext().IsEmpty()) |
278 return; | 278 return; |
279 v8::Handle<v8::Value> picture_handle; | 279 v8::Local<v8::Value> picture_handle; |
280 args->GetNext(&picture_handle); | 280 args->GetNext(&picture_handle); |
281 scoped_refptr<cc::Picture> picture = | 281 scoped_refptr<cc::Picture> picture = |
282 ParsePictureStr(isolate, picture_handle); | 282 ParsePictureStr(isolate, picture_handle); |
283 if (!picture.get()) | 283 if (!picture.get()) |
284 return; | 284 return; |
285 | 285 |
286 v8::Handle<v8::Object> result = v8::Object::New(isolate); | 286 v8::Local<v8::Object> result = v8::Object::New(isolate); |
287 result->Set(v8::String::NewFromUtf8(isolate, "width"), | 287 result->Set(v8::String::NewFromUtf8(isolate, "width"), |
288 v8::Number::New(isolate, picture->LayerRect().width())); | 288 v8::Number::New(isolate, picture->LayerRect().width())); |
289 result->Set(v8::String::NewFromUtf8(isolate, "height"), | 289 result->Set(v8::String::NewFromUtf8(isolate, "height"), |
290 v8::Number::New(isolate, picture->LayerRect().height())); | 290 v8::Number::New(isolate, picture->LayerRect().height())); |
291 | 291 |
292 args->Return(result); | 292 args->Return(result); |
293 } | 293 } |
294 | 294 |
295 } // namespace content | 295 } // namespace content |
OLD | NEW |