Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / skia_benchmarking_extension.cc
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.
4
5 #include "content/renderer/skia_benchmarking_extension.h"
6
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 "content/renderer/render_thread_impl.h"
14 #include "gin/arguments.h"
15 #include "gin/handle.h"
16 #include "gin/object_template_builder.h"
17 #include "skia/ext/benchmarking_canvas.h"
18 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
19 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebKit.h"
22 #include "third_party/skia/include/core/SkBitmapDevice.h"
23 #include "third_party/skia/include/core/SkCanvas.h"
24 #include "third_party/skia/include/core/SkColorPriv.h"
25 #include "third_party/skia/include/core/SkGraphics.h"
26 #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/rect_conversions.h"
30 #include "ui/gfx/skia_util.h"
31 #include "v8/include/v8.h"
32
33
34 namespace content {
35
36 namespace {
37
38 scoped_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate,
39                                         v8::Handle<v8::Value> arg) {
40   scoped_ptr<content::V8ValueConverter> converter(
41       content::V8ValueConverter::create());
42   return scoped_ptr<base::Value>(
43       converter->FromV8Value(arg, isolate->GetCurrentContext()));
44 }
45
46 scoped_refptr<cc::Picture> ParsePictureStr(v8::Isolate* isolate,
47                                            v8::Handle<v8::Value> arg) {
48   scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg);
49   if (!picture_value)
50     return NULL;
51   return cc::Picture::CreateFromSkpValue(picture_value.get());
52 }
53
54 scoped_refptr<cc::Picture> ParsePictureHash(v8::Isolate* isolate,
55                                             v8::Handle<v8::Value> arg) {
56   scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg);
57   if (!picture_value)
58     return NULL;
59   return cc::Picture::CreateFromValue(picture_value.get());
60 }
61
62 }  // namespace
63
64 gin::WrapperInfo SkiaBenchmarking::kWrapperInfo = {gin::kEmbedderNativeGin};
65
66 // static
67 void SkiaBenchmarking::Install(blink::WebFrame* frame) {
68   v8::Isolate* isolate = blink::mainThreadIsolate();
69   v8::HandleScope handle_scope(isolate);
70   v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
71   if (context.IsEmpty())
72     return;
73
74   v8::Context::Scope context_scope(context);
75
76   gin::Handle<SkiaBenchmarking> controller =
77       gin::CreateHandle(isolate, new SkiaBenchmarking());
78   v8::Handle<v8::Object> global = context->Global();
79   v8::Handle<v8::Object> chrome =
80       global->Get(gin::StringToV8(isolate, "chrome"))->ToObject();
81   if (chrome.IsEmpty()) {
82     chrome = v8::Object::New(isolate);
83     global->Set(gin::StringToV8(isolate, "chrome"), chrome);
84   }
85   chrome->Set(gin::StringToV8(isolate, "skiaBenchmarking"), controller.ToV8());
86 }
87
88 // static
89 void SkiaBenchmarking::Initialize() {
90   DCHECK(RenderThreadImpl::current());
91   // FIXME: remove this after Skia updates SkGraphics::Init() to be
92   //        thread-safe and idempotent.
93   static bool skia_initialized = false;
94   if (!skia_initialized) {
95     LOG(WARNING) << "Enabling unsafe Skia benchmarking extension.";
96     SkGraphics::Init();
97     skia_initialized = true;
98   }
99 }
100
101 SkiaBenchmarking::SkiaBenchmarking() {
102   Initialize();
103 }
104
105 SkiaBenchmarking::~SkiaBenchmarking() {}
106
107 gin::ObjectTemplateBuilder SkiaBenchmarking::GetObjectTemplateBuilder(
108     v8::Isolate* isolate) {
109   return gin::Wrappable<SkiaBenchmarking>::GetObjectTemplateBuilder(isolate)
110       .SetMethod("rasterize", &SkiaBenchmarking::Rasterize)
111       .SetMethod("getOps", &SkiaBenchmarking::GetOps)
112       .SetMethod("getOpTimings", &SkiaBenchmarking::GetOpTimings)
113       .SetMethod("getInfo", &SkiaBenchmarking::GetInfo);
114 }
115
116 void SkiaBenchmarking::Rasterize(gin::Arguments* args) {
117   v8::Isolate* isolate = args->isolate();
118   if (args->PeekNext().IsEmpty())
119     return;
120   v8::Handle<v8::Value> picture_handle;
121   args->GetNext(&picture_handle);
122   scoped_refptr<cc::Picture> picture =
123       ParsePictureHash(isolate, picture_handle);
124   if (!picture.get())
125     return;
126
127   double scale = 1.0;
128   gfx::Rect clip_rect(picture->LayerRect());
129   int stop_index = -1;
130   bool overdraw = false;
131
132   if (!args->PeekNext().IsEmpty()) {
133     v8::Handle<v8::Value> params;
134     args->GetNext(&params);
135     scoped_ptr<content::V8ValueConverter> converter(
136         content::V8ValueConverter::create());
137     scoped_ptr<base::Value> params_value(
138         converter->FromV8Value(params, isolate->GetCurrentContext()));
139
140     const base::DictionaryValue* params_dict = NULL;
141     if (params_value.get() && params_value->GetAsDictionary(&params_dict)) {
142       params_dict->GetDouble("scale", &scale);
143       params_dict->GetInteger("stop", &stop_index);
144       params_dict->GetBoolean("overdraw", &overdraw);
145
146       const base::Value* clip_value = NULL;
147       if (params_dict->Get("clip", &clip_value))
148         cc::MathUtil::FromValue(clip_value, &clip_rect);
149     }
150   }
151
152   gfx::RectF clip(clip_rect);
153   clip.Intersect(picture->LayerRect());
154   clip.Scale(scale);
155   gfx::Rect snapped_clip = gfx::ToEnclosingRect(clip);
156
157   SkBitmap bitmap;
158   if (!bitmap.setConfig(SkBitmap::kARGB_8888_Config, snapped_clip.width(),
159                         snapped_clip.height()))
160     return;
161   if (!bitmap.allocPixels())
162     return;
163   bitmap.eraseARGB(0, 0, 0, 0);
164
165   SkCanvas canvas(bitmap);
166   canvas.translate(SkFloatToScalar(-clip.x()), SkFloatToScalar(-clip.y()));
167   canvas.clipRect(gfx::RectToSkRect(snapped_clip));
168   canvas.scale(scale, scale);
169   canvas.translate(picture->LayerRect().x(), picture->LayerRect().y());
170
171   // First, build a debug canvas for the given picture.
172   SkDebugCanvas debug_canvas(picture->LayerRect().width(),
173                              picture->LayerRect().height());
174   picture->Replay(&debug_canvas);
175
176   // Raster the requested command subset into the bitmap-backed canvas.
177   int last_index = debug_canvas.getSize() - 1;
178   if (last_index >= 0) {
179     debug_canvas.setOverdrawViz(overdraw);
180     debug_canvas.drawTo(
181         &canvas,
182         stop_index < 0 ? last_index : std::min(last_index, stop_index));
183   }
184
185   blink::WebArrayBuffer buffer =
186       blink::WebArrayBuffer::create(bitmap.getSize(), 1);
187   uint32* packed_pixels = reinterpret_cast<uint32*>(bitmap.getPixels());
188   uint8* buffer_pixels = reinterpret_cast<uint8*>(buffer.data());
189   // Swizzle from native Skia format to RGBA as we copy out.
190   for (size_t i = 0; i < bitmap.getSize(); i += 4) {
191     uint32 c = packed_pixels[i >> 2];
192     buffer_pixels[i] = SkGetPackedR32(c);
193     buffer_pixels[i + 1] = SkGetPackedG32(c);
194     buffer_pixels[i + 2] = SkGetPackedB32(c);
195     buffer_pixels[i + 3] = SkGetPackedA32(c);
196   }
197
198   v8::Handle<v8::Object> result = v8::Object::New(isolate);
199   result->Set(v8::String::NewFromUtf8(isolate, "width"),
200               v8::Number::New(isolate, snapped_clip.width()));
201   result->Set(v8::String::NewFromUtf8(isolate, "height"),
202               v8::Number::New(isolate, snapped_clip.height()));
203   result->Set(v8::String::NewFromUtf8(isolate, "data"),
204               blink::WebArrayBufferConverter::toV8Value(&buffer));
205
206   args->Return(result);
207 }
208
209 void SkiaBenchmarking::GetOps(gin::Arguments* args) {
210   v8::Isolate* isolate = args->isolate();
211   if (args->PeekNext().IsEmpty())
212     return;
213   v8::Handle<v8::Value> picture_handle;
214   args->GetNext(&picture_handle);
215   scoped_refptr<cc::Picture> picture =
216       ParsePictureHash(isolate, picture_handle);
217   if (!picture.get())
218     return;
219
220   gfx::Rect bounds = picture->LayerRect();
221   SkDebugCanvas canvas(bounds.width(), bounds.height());
222   picture->Replay(&canvas);
223
224   v8::Handle<v8::Array> result = v8::Array::New(isolate, canvas.getSize());
225   for (int i = 0; i < canvas.getSize(); ++i) {
226     DrawType cmd_type = canvas.getDrawCommandAt(i)->getType();
227     v8::Handle<v8::Object> cmd = v8::Object::New(isolate);
228     cmd->Set(v8::String::NewFromUtf8(isolate, "cmd_type"),
229              v8::Integer::New(isolate, cmd_type));
230     cmd->Set(v8::String::NewFromUtf8(isolate, "cmd_string"),
231              v8::String::NewFromUtf8(
232                  isolate, SkDrawCommand::GetCommandString(cmd_type)));
233
234     SkTDArray<SkString*>* info = canvas.getCommandInfo(i);
235     DCHECK(info);
236
237     v8::Local<v8::Array> v8_info = v8::Array::New(isolate, info->count());
238     for (int j = 0; j < info->count(); ++j) {
239       const SkString* info_str = (*info)[j];
240       DCHECK(info_str);
241       v8_info->Set(j, v8::String::NewFromUtf8(isolate, info_str->c_str()));
242     }
243
244     cmd->Set(v8::String::NewFromUtf8(isolate, "info"), v8_info);
245
246     result->Set(i, cmd);
247   }
248
249   args->Return(result.As<v8::Object>());
250 }
251
252 void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) {
253   v8::Isolate* isolate = args->isolate();
254   if (args->PeekNext().IsEmpty())
255     return;
256   v8::Handle<v8::Value> picture_handle;
257   args->GetNext(&picture_handle);
258   scoped_refptr<cc::Picture> picture =
259       ParsePictureHash(isolate, picture_handle);
260   if (!picture.get())
261     return;
262
263   gfx::Rect bounds = picture->LayerRect();
264
265   // Measure the total time by drawing straight into a bitmap-backed canvas.
266   skia::RefPtr<SkBaseDevice> device = skia::AdoptRef(SkNEW_ARGS(
267       SkBitmapDevice,
268       (SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height())));
269   SkCanvas bitmap_canvas(device.get());
270   bitmap_canvas.clear(SK_ColorTRANSPARENT);
271   base::TimeTicks t0 = base::TimeTicks::HighResNow();
272   picture->Replay(&bitmap_canvas);
273   base::TimeDelta total_time = base::TimeTicks::HighResNow() - t0;
274
275   // Gather per-op timing info by drawing into a BenchmarkingCanvas.
276   skia::BenchmarkingCanvas benchmarking_canvas(bounds.width(), bounds.height());
277   picture->Replay(&benchmarking_canvas);
278
279   v8::Local<v8::Array> op_times =
280       v8::Array::New(isolate, benchmarking_canvas.CommandCount());
281   for (size_t i = 0; i < benchmarking_canvas.CommandCount(); ++i)
282     op_times->Set(i, v8::Number::New(isolate, benchmarking_canvas.GetTime(i)));
283
284   v8::Handle<v8::Object> result = v8::Object::New(isolate);
285   result->Set(v8::String::NewFromUtf8(isolate, "total_time"),
286               v8::Number::New(isolate, total_time.InMillisecondsF()));
287   result->Set(v8::String::NewFromUtf8(isolate, "cmd_times"), op_times);
288
289   args->Return(result);
290 }
291
292 void SkiaBenchmarking::GetInfo(gin::Arguments* args) {
293   v8::Isolate* isolate = args->isolate();
294   if (args->PeekNext().IsEmpty())
295     return;
296   v8::Handle<v8::Value> picture_handle;
297   args->GetNext(&picture_handle);
298   scoped_refptr<cc::Picture> picture =
299       ParsePictureStr(isolate, picture_handle);
300   if (!picture.get())
301     return;
302
303   v8::Handle<v8::Object> result = v8::Object::New(isolate);
304   result->Set(v8::String::NewFromUtf8(isolate, "width"),
305               v8::Number::New(isolate, picture->LayerRect().width()));
306   result->Set(v8::String::NewFromUtf8(isolate, "height"),
307               v8::Number::New(isolate, picture->LayerRect().height()));
308
309   args->Return(result);
310 }
311
312 } // namespace content