- add sources.
[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 "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"
26
27 using WebKit::WebFrame;
28
29 namespace {
30
31 const char kSkiaBenchmarkingExtensionName[] = "v8/SkiaBenchmarking";
32
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()));
38 }
39
40 static scoped_refptr<cc::Picture> ParsePictureStr(v8::Handle<v8::Value> arg) {
41   scoped_ptr<base::Value> picture_value = ParsePictureArg(arg);
42   if (!picture_value)
43     return NULL;
44   return cc::Picture::CreateFromSkpValue(picture_value.get());
45 }
46
47 static scoped_refptr<cc::Picture> ParsePictureHash(v8::Handle<v8::Value> arg) {
48   scoped_ptr<base::Value> picture_value = ParsePictureArg(arg);
49   if (!picture_value)
50     return NULL;
51   return cc::Picture::CreateFromValue(picture_value.get());
52 }
53
54 class SkiaBenchmarkingWrapper : public v8::Extension {
55  public:
56   SkiaBenchmarkingWrapper() :
57       v8::Extension(kSkiaBenchmarkingExtensionName,
58         "if (typeof(chrome) == 'undefined') {"
59         "  chrome = {};"
60         "};"
61         "if (typeof(chrome.skiaBenchmarking) == 'undefined') {"
62         "  chrome.skiaBenchmarking = {};"
63         "};"
64         "chrome.skiaBenchmarking.rasterize = function(picture, params) {"
65         "  /* "
66         "     Rasterizes a Picture JSON-encoded by cc::Picture::AsValue()."
67         "     @param {Object} picture A json-encoded cc::Picture."
68         "     @param {"
69         "                 'scale':    {Number},"
70         "                 'stop':     {Number},"
71         "                 'overdraw': {Boolean},"
72         "                 'clip':     [Number, Number, Number, Number]"
73         "     } (optional) Rasterization parameters."
74         "     @returns {"
75         "                 'width':    {Number},"
76         "                 'height':   {Number},"
77         "                 'data':     {ArrayBuffer}"
78         "     }"
79         "     @returns undefined if the arguments are invalid or the picture"
80         "                        version is not supported."
81         "   */"
82         "  native function Rasterize();"
83         "  return Rasterize(picture, params);"
84         "};"
85         "chrome.skiaBenchmarking.getOps = function(picture) {"
86         "  /* "
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."
92         "   */"
93         "  native function GetOps();"
94         "  return GetOps(picture);"
95         "};"
96         "chrome.skiaBenchmarking.getOpTimings = function(picture) {"
97         "  /* "
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."
103         "   */"
104         "  native function GetOpTimings();"
105         "  return GetOpTimings(picture);"
106         "};"
107         "chrome.skiaBenchmarking.getInfo = function(picture) {"
108         "  /* "
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."
113         "   */"
114         "  native function GetInfo();"
115         "  return GetInfo(picture);"
116         "};"
117         ) {
118       content::SkiaBenchmarkingExtension::InitSkGraphics();
119   }
120
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);
131
132     return v8::Handle<v8::FunctionTemplate>();
133   }
134
135   static void Rasterize(const v8::FunctionCallbackInfo<v8::Value>& args) {
136     if (args.Length() < 1)
137       return;
138
139     scoped_refptr<cc::Picture> picture = ParsePictureHash(args[0]);
140     if (!picture.get())
141       return;
142
143     double scale = 1.0;
144     gfx::Rect clip_rect(picture->LayerRect());
145     int stop_index = -1;
146     bool overdraw = false;
147
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()));
153
154       const base::DictionaryValue* params_dict = NULL;
155       if (params_value.get() && params_value->GetAsDictionary(&params_dict)) {
156         params_dict->GetDouble("scale", &scale);
157         params_dict->GetInteger("stop", &stop_index);
158         params_dict->GetBoolean("overdraw", &overdraw);
159
160         const base::Value* clip_value = NULL;
161         if (params_dict->Get("clip", &clip_value))
162           cc::MathUtil::FromValue(clip_value, &clip_rect);
163       }
164     }
165
166     gfx::RectF clip(clip_rect);
167     clip.Intersect(picture->LayerRect());
168     clip.Scale(scale);
169     gfx::Rect snapped_clip = gfx::ToEnclosingRect(clip);
170
171     const int kMaxBitmapSize = 4096;
172     if (snapped_clip.width() > kMaxBitmapSize
173         || snapped_clip.height() > kMaxBitmapSize)
174       return;
175
176     SkBitmap bitmap;
177     bitmap.setConfig(SkBitmap::kARGB_8888_Config, snapped_clip.width(),
178                      snapped_clip.height());
179     if (!bitmap.allocPixels())
180       return;
181     bitmap.eraseARGB(0, 0, 0, 0);
182
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());
190
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);
195
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
201                             ? last_index
202                             : std::min(last_index, stop_index));
203     }
204
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);
216     }
217
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());
224
225     args.GetReturnValue().Set(result);
226   }
227
228   static void GetOps(const v8::FunctionCallbackInfo<v8::Value>& args) {
229     if (args.Length() != 1)
230       return;
231
232     scoped_refptr<cc::Picture> picture = ParsePictureHash(args[0]);
233     if (!picture.get())
234       return;
235
236     gfx::Rect bounds = picture->LayerRect();
237     SkDebugCanvas canvas(bounds.width(), bounds.height());
238     picture->Replay(&canvas);
239
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)));
247
248       SkTDArray<SkString*>* info = canvas.getCommandInfo(i);
249       DCHECK(info);
250
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];
254         DCHECK(info_str);
255         v8_info->Set(j, v8::String::New(info_str->c_str()));
256       }
257
258       cmd->Set(v8::String::New("info"), v8_info);
259
260       result->Set(i, cmd);
261     }
262
263     args.GetReturnValue().Set(result);
264   }
265
266   static void GetOpTimings(const v8::FunctionCallbackInfo<v8::Value>& args) {
267     if (args.Length() != 1)
268       return;
269
270     scoped_refptr<cc::Picture> picture = ParsePictureHash(args[0]);
271     if (!picture.get())
272       return;
273
274     gfx::Rect bounds = picture->LayerRect();
275
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;
285
286     // Gather per-op timing info by drawing into a BenchmarkingCanvas.
287     skia::BenchmarkingCanvas benchmarking_canvas(bounds.width(),
288                                                  bounds.height());
289     picture->Replay(&benchmarking_canvas);
290
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)));
295
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);
300
301     args.GetReturnValue().Set(result);
302   }
303
304   static void GetInfo(const v8::FunctionCallbackInfo<v8::Value>& args) {
305     if (args.Length() != 1)
306       return;
307
308     scoped_refptr<cc::Picture> picture = ParsePictureStr(args[0]);
309     if (!picture.get())
310       return;
311
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()));
317
318     args.GetReturnValue().Set(result);
319   }
320 };
321
322 } // namespace
323
324 namespace content {
325
326 v8::Extension* SkiaBenchmarkingExtension::Get() {
327   return new SkiaBenchmarkingWrapper();
328 }
329
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) {
337       SkGraphics::Init();
338       skia_initialized = true;
339     }
340 }
341
342 } // namespace content