diff options
author | fmalita@chromium.org <fmalita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-14 11:10:05 +0000 |
---|---|---|
committer | fmalita@chromium.org <fmalita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-14 11:10:05 +0000 |
commit | a8228251b80841509838bf34cbd047d5d4718823 (patch) | |
tree | bca7c5caa4b2e4ad05830793a038865907f24b9b /content/renderer/skia_benchmarking_extension.cc | |
parent | 7c80ec65bb9693dfd28e8cd5cc306bdcef480210 (diff) | |
download | chromium_src-a8228251b80841509838bf34cbd047d5d4718823.zip chromium_src-a8228251b80841509838bf34cbd047d5d4718823.tar.gz chromium_src-a8228251b80841509838bf34cbd047d5d4718823.tar.bz2 |
Add Skia benchmarking extension.
This extension is intended to expose some of the Skia debugger backend
functionality to JS. Enabled at runtime by --enable-skia-benchmarking
(disabled by default).
Currently, only the rasterize() method is implemented:
rasterize: function(base64Picture) {
return { width, height, RGBA8 ArrayBuffer};
}
R=nduca@chromium.org,pdr@chromium.org
BUG=240465
Review URL: https://chromiumcodereview.appspot.com/14698011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@199963 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/renderer/skia_benchmarking_extension.cc')
-rw-r--r-- | content/renderer/skia_benchmarking_extension.cc | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/content/renderer/skia_benchmarking_extension.cc b/content/renderer/skia_benchmarking_extension.cc new file mode 100644 index 0000000..7adb77d --- /dev/null +++ b/content/renderer/skia_benchmarking_extension.cc @@ -0,0 +1,102 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/renderer/skia_benchmarking_extension.h" + +#include "cc/resources/picture.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBuffer.h" +#include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/core/SkGraphics.h" +#include "v8/include/v8.h" + +namespace { + +const char kSkiaBenchmarkingExtensionName[] = "v8/SkiaBenchmarking"; + +class SkiaBenchmarkingWrapper : public v8::Extension { + public: + SkiaBenchmarkingWrapper() : + v8::Extension(kSkiaBenchmarkingExtensionName, + "if (typeof(chrome) == 'undefined') {" + " chrome = {};" + "};" + "if (typeof(chrome.skiaBenchmarking) == 'undefined') {" + " chrome.skiaBenchmarking = {};" + "};" + "chrome.skiaBenchmarking.rasterize = function(picture) {" + " /* " + " Rasterizes a base64-encoded Picture." + " @param {String} picture Base64-encoded Picture. " + " @returns { 'width': {Number}, 'height': {Number}," + " 'data': {ArrayBuffer} }" + " */" + " native function Rasterize();" + " return Rasterize(picture);" + "};" + ) { + content::SkiaBenchmarkingExtension::InitSkGraphics(); + } + + virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( + v8::Handle<v8::String> name) OVERRIDE { + if (name->Equals(v8::String::New("Rasterize"))) + return v8::FunctionTemplate::New(Rasterize); + + return v8::Handle<v8::FunctionTemplate>(); + } + + static v8::Handle<v8::Value> Rasterize(const v8::Arguments& args) { + if (args.Length() != 1) + return v8::Undefined(); + + v8::String::AsciiValue base64_picture(args[0]); + scoped_refptr<cc::Picture> picture = + cc::Picture::CreateFromBase64String(*base64_picture); + if (!picture) + return v8::Undefined(); + + gfx::Rect rect = picture->LayerRect(); + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); + if (!bitmap.allocPixels()) + return v8::Undefined(); + + bitmap.eraseARGB(0, 0, 0, 0); + SkCanvas canvas(bitmap); + picture->Raster(&canvas, rect, 1, true); + + WebKit::WebArrayBuffer buffer = + WebKit::WebArrayBuffer::create(bitmap.getSize(), 1); + memcpy(buffer.data(), bitmap.getPixels(), bitmap.getSize()); + + v8::Handle<v8::Object> result = v8::Object::New(); + result->Set(v8::String::New("width"), v8::Number::New(rect.width())); + result->Set(v8::String::New("height"), v8::Number::New(rect.height())); + result->Set(v8::String::New("data"), buffer.toV8Value()); + + return result; + } +}; + +} // namespace + +namespace content { + +v8::Extension* SkiaBenchmarkingExtension::Get() { + return new SkiaBenchmarkingWrapper(); +} + +void SkiaBenchmarkingExtension::InitSkGraphics() { + // Always call on the main render thread. + // Does not need to be thread-safe, as long as the above holds. + // FIXME: remove this after Skia updates SkGraphics::Init() to be + // thread-safe and idempotent. + static bool skia_initialized = false; + if (!skia_initialized) { + SkGraphics::Init(); + skia_initialized = true; + } +} + +} // namespace content |