blob: 9d7c00dc6e5f6e67b16c3f3439e2de34ef5d32e1 [file] [log] [blame]
[email protected]a8228252013-05-14 11:10:051// 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.
4
5#include "content/renderer/skia_benchmarking_extension.h"
6
avi1023d012015-12-25 02:39:147#include <stddef.h>
8#include <stdint.h>
9
[email protected]c883e8b2013-09-01 23:36:3110#include "base/base64.h"
[email protected]64975672013-07-30 18:47:1311#include "base/time/time.h"
[email protected]dc5407d42013-05-24 22:33:0312#include "base/values.h"
[email protected]ef20a402013-07-02 04:30:4313#include "cc/base/math_util.h"
donndbd8a6612015-12-02 02:39:5814#include "content/public/renderer/chrome_object_extensions_utils.h"
John Abd-El-Malek312a30bb2017-10-23 19:51:5215#include "content/public/renderer/v8_value_converter.h"
[email protected]cdb6b1e2014-01-23 00:06:4916#include "content/renderer/render_thread_impl.h"
17#include "gin/arguments.h"
jbroman5967a862017-04-27 16:52:4518#include "gin/data_object_builder.h"
[email protected]cdb6b1e2014-01-23 00:06:4919#include "gin/handle.h"
20#include "gin/object_template_builder.h"
[email protected]64975672013-07-30 18:47:1321#include "skia/ext/benchmarking_canvas.h"
yukishiinoecc88862014-12-04 03:53:2022#include "third_party/WebKit/public/web/WebArrayBuffer.h"
[email protected]8d5e5142014-02-06 20:10:3723#include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
[email protected]cdb6b1e2014-01-23 00:06:4924#include "third_party/WebKit/public/web/WebKit.h"
lukaszadf18ba762017-06-09 22:24:3025#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]a8228252013-05-14 11:10:0526#include "third_party/skia/include/core/SkCanvas.h"
[email protected]3fcc7b42013-05-14 20:20:5027#include "third_party/skia/include/core/SkColorPriv.h"
[email protected]a8228252013-05-14 11:10:0528#include "third_party/skia/include/core/SkGraphics.h"
fmalitaf5190e02015-02-26 15:48:4229#include "third_party/skia/include/core/SkPicture.h"
[email protected]c883e8b2013-09-01 23:36:3130#include "third_party/skia/include/core/SkStream.h"
vmpstre22c5082015-09-24 20:57:5431#include "ui/gfx/codec/jpeg_codec.h"
32#include "ui/gfx/codec/png_codec.h"
tfarina3b0452d2014-12-31 15:20:0933#include "ui/gfx/geometry/rect_conversions.h"
[email protected]ef20a402013-07-02 04:30:4334#include "ui/gfx/skia_util.h"
[email protected]a8228252013-05-14 11:10:0535#include "v8/include/v8.h"
36
[email protected]cdb6b1e2014-01-23 00:06:4937namespace content {
[email protected]dc5407d42013-05-24 22:33:0338
[email protected]a8228252013-05-14 11:10:0539namespace {
40
vmpstre22c5082015-09-24 20:57:5441class Picture {
42 public:
43 gfx::Rect layer_rect;
fmalita9e89d082016-03-28 15:44:4944 sk_sp<SkPicture> picture;
vmpstre22c5082015-09-24 20:57:5445};
46
dchengcedca5612016-04-09 01:40:1547std::unique_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate,
48 v8::Local<v8::Value> arg) {
rdevlin.cronin694c6052017-06-13 22:07:3549 return content::V8ValueConverter::Create()->FromV8Value(
50 arg, isolate->GetCurrentContext());
[email protected]c883e8b2013-09-01 23:36:3151}
52
dchengcedca5612016-04-09 01:40:1553std::unique_ptr<Picture> CreatePictureFromEncodedString(
54 const std::string& encoded) {
vmpstre22c5082015-09-24 20:57:5455 std::string decoded;
56 base::Base64Decode(encoded, &decoded);
fmalita9e89d082016-03-28 15:44:4957 sk_sp<SkPicture> skpicture =
Mike Reed051e7bd2017-12-15 03:55:3158 SkPicture::MakeFromData(decoded.data(), decoded.size());
vmpstre22c5082015-09-24 20:57:5459 if (!skpicture)
60 return nullptr;
61
dchengcedca5612016-04-09 01:40:1562 std::unique_ptr<Picture> picture(new Picture);
vmpstre22c5082015-09-24 20:57:5463 picture->layer_rect = gfx::SkIRectToRect(skpicture->cullRect().roundOut());
fmalita9e89d082016-03-28 15:44:4964 picture->picture = std::move(skpicture);
vmpstre22c5082015-09-24 20:57:5465 return picture;
[email protected]c883e8b2013-09-01 23:36:3166}
[email protected]cb9118b32013-06-26 14:15:0767
dchengcedca5612016-04-09 01:40:1568std::unique_ptr<Picture> ParsePictureStr(v8::Isolate* isolate,
69 v8::Local<v8::Value> arg) {
70 std::unique_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg);
[email protected]c883e8b2013-09-01 23:36:3171 if (!picture_value)
vmpstre22c5082015-09-24 20:57:5472 return nullptr;
73 // Decode the picture from base64.
74 std::string encoded;
75 if (!picture_value->GetAsString(&encoded))
76 return nullptr;
77 return CreatePictureFromEncodedString(encoded);
78}
79
dchengcedca5612016-04-09 01:40:1580std::unique_ptr<Picture> ParsePictureHash(v8::Isolate* isolate,
81 v8::Local<v8::Value> arg) {
82 std::unique_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg);
vmpstre22c5082015-09-24 20:57:5483 if (!picture_value)
84 return nullptr;
85 const base::DictionaryValue* value = nullptr;
86 if (!picture_value->GetAsDictionary(&value))
87 return nullptr;
88 // Decode the picture from base64.
89 std::string encoded;
90 if (!value->GetString("skp64", &encoded))
91 return nullptr;
92 return CreatePictureFromEncodedString(encoded);
[email protected]cb9118b32013-06-26 14:15:0793}
94
fmalitaf5190e02015-02-26 15:48:4295class PicturePlaybackController : public SkPicture::AbortCallback {
96 public:
97 PicturePlaybackController(const skia::BenchmarkingCanvas& canvas,
98 size_t count)
99 : canvas_(canvas), playback_count_(count) {}
100
101 bool abort() override { return canvas_.CommandCount() > playback_count_; }
102
103 private:
104 const skia::BenchmarkingCanvas& canvas_;
105 size_t playback_count_;
106};
107
[email protected]cdb6b1e2014-01-23 00:06:49108} // namespace
109
110gin::WrapperInfo SkiaBenchmarking::kWrapperInfo = {gin::kEmbedderNativeGin};
111
112// static
lukaszadf18ba762017-06-09 22:24:30113void SkiaBenchmarking::Install(blink::WebLocalFrame* frame) {
Blink Reformat1c4d759e2017-04-09 16:34:54114 v8::Isolate* isolate = blink::MainThreadIsolate();
[email protected]cdb6b1e2014-01-23 00:06:49115 v8::HandleScope handle_scope(isolate);
Blink Reformat1c4d759e2017-04-09 16:34:54116 v8::Local<v8::Context> context = frame->MainWorldScriptContext();
[email protected]cdb6b1e2014-01-23 00:06:49117 if (context.IsEmpty())
118 return;
119
120 v8::Context::Scope context_scope(context);
121
122 gin::Handle<SkiaBenchmarking> controller =
123 gin::CreateHandle(isolate, new SkiaBenchmarking());
[email protected]ad4d2032014-04-28 13:50:59124 if (controller.IsEmpty())
125 return;
126
deepak.s750d68f2015-04-30 07:32:41127 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate,
kolczyke1b79c752014-10-01 10:06:56128 context->Global());
[email protected]cdb6b1e2014-01-23 00:06:49129 chrome->Set(gin::StringToV8(isolate, "skiaBenchmarking"), controller.ToV8());
[email protected]a8228252013-05-14 11:10:05130}
131
[email protected]cdb6b1e2014-01-23 00:06:49132// static
133void SkiaBenchmarking::Initialize() {
134 DCHECK(RenderThreadImpl::current());
135 // FIXME: remove this after Skia updates SkGraphics::Init() to be
136 // thread-safe and idempotent.
137 static bool skia_initialized = false;
138 if (!skia_initialized) {
139 LOG(WARNING) << "Enabling unsafe Skia benchmarking extension.";
140 SkGraphics::Init();
141 skia_initialized = true;
142 }
143}
144
145SkiaBenchmarking::SkiaBenchmarking() {
146 Initialize();
147}
148
149SkiaBenchmarking::~SkiaBenchmarking() {}
150
151gin::ObjectTemplateBuilder SkiaBenchmarking::GetObjectTemplateBuilder(
152 v8::Isolate* isolate) {
153 return gin::Wrappable<SkiaBenchmarking>::GetObjectTemplateBuilder(isolate)
154 .SetMethod("rasterize", &SkiaBenchmarking::Rasterize)
155 .SetMethod("getOps", &SkiaBenchmarking::GetOps)
156 .SetMethod("getOpTimings", &SkiaBenchmarking::GetOpTimings)
157 .SetMethod("getInfo", &SkiaBenchmarking::GetInfo);
158}
159
160void SkiaBenchmarking::Rasterize(gin::Arguments* args) {
161 v8::Isolate* isolate = args->isolate();
162 if (args->PeekNext().IsEmpty())
163 return;
deepak.s750d68f2015-04-30 07:32:41164 v8::Local<v8::Value> picture_handle;
[email protected]cdb6b1e2014-01-23 00:06:49165 args->GetNext(&picture_handle);
dchengcedca5612016-04-09 01:40:15166 std::unique_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle);
[email protected]cdb6b1e2014-01-23 00:06:49167 if (!picture.get())
168 return;
169
170 double scale = 1.0;
vmpstre22c5082015-09-24 20:57:54171 gfx::Rect clip_rect(picture->layer_rect);
[email protected]cdb6b1e2014-01-23 00:06:49172 int stop_index = -1;
[email protected]cdb6b1e2014-01-23 00:06:49173
deepak.s750d68f2015-04-30 07:32:41174 v8::Local<v8::Context> context = isolate->GetCurrentContext();
[email protected]cdb6b1e2014-01-23 00:06:49175 if (!args->PeekNext().IsEmpty()) {
deepak.s750d68f2015-04-30 07:32:41176 v8::Local<v8::Value> params;
[email protected]cdb6b1e2014-01-23 00:06:49177 args->GetNext(&params);
rdevlin.cronin694c6052017-06-13 22:07:35178 std::unique_ptr<base::Value> params_value =
179 content::V8ValueConverter::Create()->FromV8Value(params, context);
[email protected]cdb6b1e2014-01-23 00:06:49180
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28181 const base::DictionaryValue* params_dict = nullptr;
[email protected]cdb6b1e2014-01-23 00:06:49182 if (params_value.get() && params_value->GetAsDictionary(&params_dict)) {
183 params_dict->GetDouble("scale", &scale);
184 params_dict->GetInteger("stop", &stop_index);
[email protected]cdb6b1e2014-01-23 00:06:49185
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28186 const base::Value* clip_value = nullptr;
[email protected]cdb6b1e2014-01-23 00:06:49187 if (params_dict->Get("clip", &clip_value))
188 cc::MathUtil::FromValue(clip_value, &clip_rect);
[email protected]a8228252013-05-14 11:10:05189 }
[email protected]cdb6b1e2014-01-23 00:06:49190 }
191
vmpstre22c5082015-09-24 20:57:54192 clip_rect.Intersect(picture->layer_rect);
danakj4606f6332015-08-31 23:56:56193 gfx::Rect snapped_clip = gfx::ScaleToEnclosingRect(clip_rect, scale);
[email protected]cdb6b1e2014-01-23 00:06:49194
[email protected]cdb6b1e2014-01-23 00:06:49195 SkBitmap bitmap;
reed2247b151d2014-09-05 16:26:47196 if (!bitmap.tryAllocN32Pixels(snapped_clip.width(), snapped_clip.height()))
[email protected]cdb6b1e2014-01-23 00:06:49197 return;
198 bitmap.eraseARGB(0, 0, 0, 0);
199
200 SkCanvas canvas(bitmap);
danakj4606f6332015-08-31 23:56:56201 canvas.translate(SkIntToScalar(-clip_rect.x()),
202 SkIntToScalar(-clip_rect.y()));
[email protected]cdb6b1e2014-01-23 00:06:49203 canvas.clipRect(gfx::RectToSkRect(snapped_clip));
204 canvas.scale(scale, scale);
vmpstre22c5082015-09-24 20:57:54205 canvas.translate(picture->layer_rect.x(), picture->layer_rect.y());
[email protected]cdb6b1e2014-01-23 00:06:49206
fmalita6ddebad2016-10-03 18:56:55207 skia::BenchmarkingCanvas benchmarking_canvas(&canvas);
fmalitaf5190e02015-02-26 15:48:42208 size_t playback_count =
209 (stop_index < 0) ? std::numeric_limits<size_t>::max() : stop_index;
210 PicturePlaybackController controller(benchmarking_canvas, playback_count);
vmpstre22c5082015-09-24 20:57:54211 picture->picture->playback(&benchmarking_canvas, &controller);
[email protected]cdb6b1e2014-01-23 00:06:49212
213 blink::WebArrayBuffer buffer =
Mike Reed8eac88c42017-10-12 21:24:38214 blink::WebArrayBuffer::Create(bitmap.computeByteSize(), 1);
avi1023d012015-12-25 02:39:14215 uint32_t* packed_pixels = reinterpret_cast<uint32_t*>(bitmap.getPixels());
Blink Reformat1c4d759e2017-04-09 16:34:54216 uint8_t* buffer_pixels = reinterpret_cast<uint8_t*>(buffer.Data());
[email protected]cdb6b1e2014-01-23 00:06:49217 // Swizzle from native Skia format to RGBA as we copy out.
Mike Reed8eac88c42017-10-12 21:24:38218 for (size_t i = 0; i < bitmap.computeByteSize(); i += 4) {
avi1023d012015-12-25 02:39:14219 uint32_t c = packed_pixels[i >> 2];
[email protected]cdb6b1e2014-01-23 00:06:49220 buffer_pixels[i] = SkGetPackedR32(c);
221 buffer_pixels[i + 1] = SkGetPackedG32(c);
222 buffer_pixels[i + 2] = SkGetPackedB32(c);
223 buffer_pixels[i + 3] = SkGetPackedA32(c);
224 }
225
jbroman5967a862017-04-27 16:52:45226 args->Return(gin::DataObjectBuilder(isolate)
227 .Set("width", snapped_clip.width())
228 .Set("height", snapped_clip.height())
229 .Set("data", blink::WebArrayBufferConverter::ToV8Value(
230 &buffer, context->Global(), isolate))
231 .Build());
[email protected]cdb6b1e2014-01-23 00:06:49232}
233
234void SkiaBenchmarking::GetOps(gin::Arguments* args) {
235 v8::Isolate* isolate = args->isolate();
236 if (args->PeekNext().IsEmpty())
237 return;
deepak.s750d68f2015-04-30 07:32:41238 v8::Local<v8::Value> picture_handle;
[email protected]cdb6b1e2014-01-23 00:06:49239 args->GetNext(&picture_handle);
dchengcedca5612016-04-09 01:40:15240 std::unique_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle);
[email protected]cdb6b1e2014-01-23 00:06:49241 if (!picture.get())
242 return;
243
vmpstre22c5082015-09-24 20:57:54244 SkCanvas canvas(picture->layer_rect.width(), picture->layer_rect.height());
fmalitaf5190e02015-02-26 15:48:42245 skia::BenchmarkingCanvas benchmarking_canvas(&canvas);
vmpstre22c5082015-09-24 20:57:54246 picture->picture->playback(&benchmarking_canvas);
[email protected]cdb6b1e2014-01-23 00:06:49247
deepak.s750d68f2015-04-30 07:32:41248 v8::Local<v8::Context> context = isolate->GetCurrentContext();
[email protected]cdb6b1e2014-01-23 00:06:49249
rdevlin.cronin694c6052017-06-13 22:07:35250 args->Return(content::V8ValueConverter::Create()->ToV8Value(
251 &benchmarking_canvas.Commands(), context));
[email protected]cdb6b1e2014-01-23 00:06:49252}
253
254void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) {
255 v8::Isolate* isolate = args->isolate();
256 if (args->PeekNext().IsEmpty())
257 return;
deepak.s750d68f2015-04-30 07:32:41258 v8::Local<v8::Value> picture_handle;
[email protected]cdb6b1e2014-01-23 00:06:49259 args->GetNext(&picture_handle);
dchengcedca5612016-04-09 01:40:15260 std::unique_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle);
[email protected]cdb6b1e2014-01-23 00:06:49261 if (!picture.get())
262 return;
263
vmpstre22c5082015-09-24 20:57:54264 gfx::Rect bounds = picture->layer_rect;
[email protected]cdb6b1e2014-01-23 00:06:49265
266 // Measure the total time by drawing straight into a bitmap-backed canvas.
[email protected]e758a2372014-03-05 20:21:52267 SkBitmap bitmap;
268 bitmap.allocN32Pixels(bounds.width(), bounds.height());
269 SkCanvas bitmap_canvas(bitmap);
[email protected]cdb6b1e2014-01-23 00:06:49270 bitmap_canvas.clear(SK_ColorTRANSPARENT);
charliea3be839702015-01-26 17:35:41271 base::TimeTicks t0 = base::TimeTicks::Now();
vmpstre22c5082015-09-24 20:57:54272 picture->picture->playback(&bitmap_canvas);
charliea3be839702015-01-26 17:35:41273 base::TimeDelta total_time = base::TimeTicks::Now() - t0;
[email protected]cdb6b1e2014-01-23 00:06:49274
275 // Gather per-op timing info by drawing into a BenchmarkingCanvas.
fmalitaf5190e02015-02-26 15:48:42276 SkCanvas canvas(bitmap);
277 canvas.clear(SK_ColorTRANSPARENT);
278 skia::BenchmarkingCanvas benchmarking_canvas(&canvas);
vmpstre22c5082015-09-24 20:57:54279 picture->picture->playback(&benchmarking_canvas);
[email protected]cdb6b1e2014-01-23 00:06:49280
281 v8::Local<v8::Array> op_times =
282 v8::Array::New(isolate, benchmarking_canvas.CommandCount());
fmalitaf5190e02015-02-26 15:48:42283 for (size_t i = 0; i < benchmarking_canvas.CommandCount(); ++i) {
[email protected]cdb6b1e2014-01-23 00:06:49284 op_times->Set(i, v8::Number::New(isolate, benchmarking_canvas.GetTime(i)));
fmalitaf5190e02015-02-26 15:48:42285 }
[email protected]cdb6b1e2014-01-23 00:06:49286
deepak.s750d68f2015-04-30 07:32:41287 v8::Local<v8::Object> result = v8::Object::New(isolate);
[email protected]cdb6b1e2014-01-23 00:06:49288 result->Set(v8::String::NewFromUtf8(isolate, "total_time"),
289 v8::Number::New(isolate, total_time.InMillisecondsF()));
290 result->Set(v8::String::NewFromUtf8(isolate, "cmd_times"), op_times);
291
292 args->Return(result);
293}
294
295void SkiaBenchmarking::GetInfo(gin::Arguments* args) {
296 v8::Isolate* isolate = args->isolate();
297 if (args->PeekNext().IsEmpty())
298 return;
deepak.s750d68f2015-04-30 07:32:41299 v8::Local<v8::Value> picture_handle;
[email protected]cdb6b1e2014-01-23 00:06:49300 args->GetNext(&picture_handle);
dchengcedca5612016-04-09 01:40:15301 std::unique_ptr<Picture> picture = ParsePictureStr(isolate, picture_handle);
[email protected]cdb6b1e2014-01-23 00:06:49302 if (!picture.get())
303 return;
304
deepak.s750d68f2015-04-30 07:32:41305 v8::Local<v8::Object> result = v8::Object::New(isolate);
[email protected]cdb6b1e2014-01-23 00:06:49306 result->Set(v8::String::NewFromUtf8(isolate, "width"),
vmpstre22c5082015-09-24 20:57:54307 v8::Number::New(isolate, picture->layer_rect.width()));
[email protected]cdb6b1e2014-01-23 00:06:49308 result->Set(v8::String::NewFromUtf8(isolate, "height"),
vmpstre22c5082015-09-24 20:57:54309 v8::Number::New(isolate, picture->layer_rect.height()));
[email protected]cdb6b1e2014-01-23 00:06:49310
311 args->Return(result);
[email protected]a8228252013-05-14 11:10:05312}
313
314} // namespace content