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
3 // found in the LICENSE file.
5 #include "content/renderer/skia_benchmarking_extension.h"
7 #include "base/base64.h"
8 #include "base/time/time.h"
9 #include "base/values.h"
10 #include "cc/base/math_util.h"
11 #include "cc/resources/picture.h"
12 #include "content/public/renderer/v8_value_converter.h"
13 #include "skia/ext/benchmarking_canvas.h"
14 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
15 #include "third_party/WebKit/public/web/WebFrame.h"
16 #include "third_party/skia/include/core/SkBitmapDevice.h"
17 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "third_party/skia/include/core/SkColorPriv.h"
19 #include "third_party/skia/include/core/SkGraphics.h"
20 #include "third_party/skia/include/core/SkStream.h"
21 #include "third_party/skia/src/utils/debugger/SkDebugCanvas.h"
22 #include "third_party/skia/src/utils/debugger/SkDrawCommand.h"
23 #include "ui/gfx/rect_conversions.h"
24 #include "ui/gfx/skia_util.h"
25 #include "v8/include/v8.h"
27 using WebKit::WebFrame;
31 const char kSkiaBenchmarkingExtensionName[] = "v8/SkiaBenchmarking";
33 static scoped_ptr<base::Value> ParsePictureArg(v8::Handle<v8::Value> arg) {
34 scoped_ptr<content::V8ValueConverter> converter(
35 content::V8ValueConverter::create());
36 return scoped_ptr<base::Value>(
37 converter->FromV8Value(arg, v8::Context::GetCurrent()));
40 static scoped_refptr<cc::Picture> ParsePictureStr(v8::Handle<v8::Value> arg) {
41 scoped_ptr<base::Value> picture_value = ParsePictureArg(arg);
44 return cc::Picture::CreateFromSkpValue(picture_value.get());
47 static scoped_refptr<cc::Picture> ParsePictureHash(v8::Handle<v8::Value> arg) {
48 scoped_ptr<base::Value> picture_value = ParsePictureArg(arg);
51 return cc::Picture::CreateFromValue(picture_value.get());
54 class SkiaBenchmarkingWrapper : public v8::Extension {
56 SkiaBenchmarkingWrapper() :
57 v8::Extension(kSkiaBenchmarkingExtensionName,
58 "if (typeof(chrome) == 'undefined') {"
61 "if (typeof(chrome.skiaBenchmarking) == 'undefined') {"
62 " chrome.skiaBenchmarking = {};"
64 "chrome.skiaBenchmarking.rasterize = function(picture, params) {"
66 " Rasterizes a Picture JSON-encoded by cc::Picture::AsValue()."
67 " @param {Object} picture A json-encoded cc::Picture."
71 " 'overdraw': {Boolean},"
72 " 'clip': [Number, Number, Number, Number]"
73 " } (optional) Rasterization parameters."
76 " 'height': {Number},"
77 " 'data': {ArrayBuffer}"
79 " @returns undefined if the arguments are invalid or the picture"
80 " version is not supported."
82 " native function Rasterize();"
83 " return Rasterize(picture, params);"
85 "chrome.skiaBenchmarking.getOps = function(picture) {"
87 " Extracts the Skia draw commands from a JSON-encoded cc::Picture"
88 " @param {Object} picture A json-encoded cc::Picture."
89 " @returns [{ 'cmd': {String}, 'info': [String, ...] }, ...]"
90 " @returns undefined if the arguments are invalid or the picture"
91 " version is not supported."
93 " native function GetOps();"
94 " return GetOps(picture);"
96 "chrome.skiaBenchmarking.getOpTimings = function(picture) {"
98 " Returns timing information for the given picture."
99 " @param {Object} picture A json-encoded cc::Picture."
100 " @returns { 'total_time': {Number}, 'cmd_times': [Number, ...] }"
101 " @returns undefined if the arguments are invalid or the picture"
102 " version is not supported."
104 " native function GetOpTimings();"
105 " return GetOpTimings(picture);"
107 "chrome.skiaBenchmarking.getInfo = function(picture) {"
109 " Returns meta information for the given picture."
110 " @param {Object} picture A base64 encoded SKP."
111 " @returns { 'width': {Number}, 'height': {Number} }"
112 " @returns undefined if the picture version is not supported."
114 " native function GetInfo();"
115 " return GetInfo(picture);"
118 content::SkiaBenchmarkingExtension::InitSkGraphics();
121 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
122 v8::Handle<v8::String> name) OVERRIDE {
123 if (name->Equals(v8::String::New("Rasterize")))
124 return v8::FunctionTemplate::New(Rasterize);
125 if (name->Equals(v8::String::New("GetOps")))
126 return v8::FunctionTemplate::New(GetOps);
127 if (name->Equals(v8::String::New("GetOpTimings")))
128 return v8::FunctionTemplate::New(GetOpTimings);
129 if (name->Equals(v8::String::New("GetInfo")))
130 return v8::FunctionTemplate::New(GetInfo);
132 return v8::Handle<v8::FunctionTemplate>();
135 static void Rasterize(const v8::FunctionCallbackInfo<v8::Value>& args) {
136 if (args.Length() < 1)
139 scoped_refptr<cc::Picture> picture = ParsePictureHash(args[0]);
144 gfx::Rect clip_rect(picture->LayerRect());
146 bool overdraw = false;
148 if (args.Length() > 1) {
149 scoped_ptr<content::V8ValueConverter> converter(
150 content::V8ValueConverter::create());
151 scoped_ptr<base::Value> params_value(
152 converter->FromV8Value(args[1], v8::Context::GetCurrent()));
154 const base::DictionaryValue* params_dict = NULL;
155 if (params_value.get() && params_value->GetAsDictionary(¶ms_dict)) {
156 params_dict->GetDouble("scale", &scale);
157 params_dict->GetInteger("stop", &stop_index);
158 params_dict->GetBoolean("overdraw", &overdraw);
160 const base::Value* clip_value = NULL;
161 if (params_dict->Get("clip", &clip_value))
162 cc::MathUtil::FromValue(clip_value, &clip_rect);
166 gfx::RectF clip(clip_rect);
167 clip.Intersect(picture->LayerRect());
169 gfx::Rect snapped_clip = gfx::ToEnclosingRect(clip);
171 const int kMaxBitmapSize = 4096;
172 if (snapped_clip.width() > kMaxBitmapSize
173 || snapped_clip.height() > kMaxBitmapSize)
177 bitmap.setConfig(SkBitmap::kARGB_8888_Config, snapped_clip.width(),
178 snapped_clip.height());
179 if (!bitmap.allocPixels())
181 bitmap.eraseARGB(0, 0, 0, 0);
183 SkCanvas canvas(bitmap);
184 canvas.translate(SkFloatToScalar(-clip.x()),
185 SkFloatToScalar(-clip.y()));
186 canvas.clipRect(gfx::RectToSkRect(snapped_clip));
187 canvas.scale(scale, scale);
188 canvas.translate(picture->LayerRect().x(),
189 picture->LayerRect().y());
191 // First, build a debug canvas for the given picture.
192 SkDebugCanvas debug_canvas(picture->LayerRect().width(),
193 picture->LayerRect().height());
194 picture->Replay(&debug_canvas);
196 // Raster the requested command subset into the bitmap-backed canvas.
197 int last_index = debug_canvas.getSize() - 1;
198 if (last_index >= 0) {
199 debug_canvas.setOverdrawViz(overdraw);
200 debug_canvas.drawTo(&canvas, stop_index < 0
202 : std::min(last_index, stop_index));
205 WebKit::WebArrayBuffer buffer =
206 WebKit::WebArrayBuffer::create(bitmap.getSize(), 1);
207 uint32* packed_pixels = reinterpret_cast<uint32*>(bitmap.getPixels());
208 uint8* buffer_pixels = reinterpret_cast<uint8*>(buffer.data());
209 // Swizzle from native Skia format to RGBA as we copy out.
210 for (size_t i = 0; i < bitmap.getSize(); i += 4) {
211 uint32 c = packed_pixels[i >> 2];
212 buffer_pixels[i] = SkGetPackedR32(c);
213 buffer_pixels[i + 1] = SkGetPackedG32(c);
214 buffer_pixels[i + 2] = SkGetPackedB32(c);
215 buffer_pixels[i + 3] = SkGetPackedA32(c);
218 v8::Handle<v8::Object> result = v8::Object::New();
219 result->Set(v8::String::New("width"),
220 v8::Number::New(snapped_clip.width()));
221 result->Set(v8::String::New("height"),
222 v8::Number::New(snapped_clip.height()));
223 result->Set(v8::String::New("data"), buffer.toV8Value());
225 args.GetReturnValue().Set(result);
228 static void GetOps(const v8::FunctionCallbackInfo<v8::Value>& args) {
229 if (args.Length() != 1)
232 scoped_refptr<cc::Picture> picture = ParsePictureHash(args[0]);
236 gfx::Rect bounds = picture->LayerRect();
237 SkDebugCanvas canvas(bounds.width(), bounds.height());
238 picture->Replay(&canvas);
240 v8::Local<v8::Array> result = v8::Array::New(canvas.getSize());
241 for (int i = 0; i < canvas.getSize(); ++i) {
242 DrawType cmd_type = canvas.getDrawCommandAt(i)->getType();
243 v8::Handle<v8::Object> cmd = v8::Object::New();
244 cmd->Set(v8::String::New("cmd_type"), v8::Integer::New(cmd_type));
245 cmd->Set(v8::String::New("cmd_string"), v8::String::New(
246 SkDrawCommand::GetCommandString(cmd_type)));
248 SkTDArray<SkString*>* info = canvas.getCommandInfo(i);
251 v8::Local<v8::Array> v8_info = v8::Array::New(info->count());
252 for (int j = 0; j < info->count(); ++j) {
253 const SkString* info_str = (*info)[j];
255 v8_info->Set(j, v8::String::New(info_str->c_str()));
258 cmd->Set(v8::String::New("info"), v8_info);
263 args.GetReturnValue().Set(result);
266 static void GetOpTimings(const v8::FunctionCallbackInfo<v8::Value>& args) {
267 if (args.Length() != 1)
270 scoped_refptr<cc::Picture> picture = ParsePictureHash(args[0]);
274 gfx::Rect bounds = picture->LayerRect();
276 // Measure the total time by drawing straight into a bitmap-backed canvas.
277 skia::RefPtr<SkBaseDevice> device = skia::AdoptRef(
278 SkNEW_ARGS(SkBitmapDevice,
279 (SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height())));
280 SkCanvas bitmap_canvas(device.get());
281 bitmap_canvas.clear(SK_ColorTRANSPARENT);
282 base::TimeTicks t0 = base::TimeTicks::HighResNow();
283 picture->Replay(&bitmap_canvas);
284 base::TimeDelta total_time = base::TimeTicks::HighResNow() - t0;
286 // Gather per-op timing info by drawing into a BenchmarkingCanvas.
287 skia::BenchmarkingCanvas benchmarking_canvas(bounds.width(),
289 picture->Replay(&benchmarking_canvas);
291 v8::Local<v8::Array> op_times =
292 v8::Array::New(benchmarking_canvas.CommandCount());
293 for (size_t i = 0; i < benchmarking_canvas.CommandCount(); ++i)
294 op_times->Set(i, v8::Number::New(benchmarking_canvas.GetTime(i)));
296 v8::Handle<v8::Object> result = v8::Object::New();
297 result->Set(v8::String::New("total_time"),
298 v8::Number::New(total_time.InMillisecondsF()));
299 result->Set(v8::String::New("cmd_times"), op_times);
301 args.GetReturnValue().Set(result);
304 static void GetInfo(const v8::FunctionCallbackInfo<v8::Value>& args) {
305 if (args.Length() != 1)
308 scoped_refptr<cc::Picture> picture = ParsePictureStr(args[0]);
312 v8::Handle<v8::Object> result = v8::Object::New();
313 result->Set(v8::String::New("width"),
314 v8::Number::New(picture->LayerRect().width()));
315 result->Set(v8::String::New("height"),
316 v8::Number::New(picture->LayerRect().height()));
318 args.GetReturnValue().Set(result);
326 v8::Extension* SkiaBenchmarkingExtension::Get() {
327 return new SkiaBenchmarkingWrapper();
330 void SkiaBenchmarkingExtension::InitSkGraphics() {
331 // Always call on the main render thread.
332 // Does not need to be thread-safe, as long as the above holds.
333 // FIXME: remove this after Skia updates SkGraphics::Init() to be
334 // thread-safe and idempotent.
335 static bool skia_initialized = false;
336 if (!skia_initialized) {
338 skia_initialized = true;
342 } // namespace content