1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
|
// Copyright (c) 2012 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/gpu/gpu_benchmarking_extension.h"
#include <string>
#include "base/base64.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_vector.h"
#include "base/strings/string_number_conversions.h"
#include "cc/layers/layer.h"
#include "content/common/input/synthetic_gesture_params.h"
#include "content/common/input/synthetic_pinch_gesture_params.h"
#include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
#include "content/common/input/synthetic_tap_gesture_params.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/v8_value_converter.h"
#include "content/renderer/chrome_object_extensions_utils.h"
#include "content/renderer/gpu/render_widget_compositor.h"
#include "content/renderer/render_thread_impl.h"
#include "content/renderer/render_view_impl.h"
#include "content/renderer/skia_benchmarking_extension.h"
#include "gin/arguments.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "third_party/WebKit/public/web/WebImageCache.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkGraphics.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "third_party/skia/include/core/SkPixelSerializer.h"
#include "third_party/skia/include/core/SkStream.h"
#include "ui/gfx/codec/png_codec.h"
#include "v8/include/v8.h"
using blink::WebCanvas;
using blink::WebLocalFrame;
using blink::WebImageCache;
using blink::WebPrivatePtr;
using blink::WebSize;
using blink::WebView;
namespace content {
namespace {
class PNGSerializer : public SkPixelSerializer {
protected:
bool onUseEncodedData(const void* data, size_t len) override { return true; }
SkData* onEncodePixels(const SkImageInfo& info,
const void* pixels,
size_t row_bytes) override {
SkBitmap bm;
// The const_cast is fine, since we only read from the bitmap.
if (bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) {
std::vector<unsigned char> vector;
if (gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &vector)) {
return SkData::NewWithCopy(&vector.front(), vector.size());
}
}
return nullptr;
}
};
class SkPictureSerializer {
public:
explicit SkPictureSerializer(const base::FilePath& dirpath)
: dirpath_(dirpath),
layer_id_(0) {
// Let skia register known effect subclasses. This basically enables
// reflection on those subclasses required for picture serialization.
SkiaBenchmarking::Initialize();
}
// Recursively serializes the layer tree.
// Each layer in the tree is serialized into a separate skp file
// in the given directory.
void Serialize(const cc::Layer* layer) {
const cc::LayerList& children = layer->children();
for (size_t i = 0; i < children.size(); ++i) {
Serialize(children[i].get());
}
skia::RefPtr<SkPicture> picture = layer->GetPicture();
if (!picture)
return;
// Serialize picture to file.
// TODO(alokp): Note that for this to work Chrome needs to be launched with
// --no-sandbox command-line flag. Get rid of this limitation.
// CRBUG: 139640.
std::string filename = "layer_" + base::IntToString(layer_id_++) + ".skp";
std::string filepath = dirpath_.AppendASCII(filename).MaybeAsASCII();
DCHECK(!filepath.empty());
SkFILEWStream file(filepath.c_str());
DCHECK(file.isValid());
PNGSerializer serializer;
picture->serialize(&file, &serializer);
}
private:
base::FilePath dirpath_;
int layer_id_;
};
template <typename T>
bool GetArg(gin::Arguments* args, T* value) {
if (!args->GetNext(value)) {
args->ThrowError();
return false;
}
return true;
}
template <>
bool GetArg(gin::Arguments* args, int* value) {
float number;
bool ret = GetArg(args, &number);
*value = number;
return ret;
}
template <typename T>
bool GetOptionalArg(gin::Arguments* args, T* value) {
if (args->PeekNext().IsEmpty())
return true;
if (args->PeekNext()->IsUndefined()) {
args->Skip();
return true;
}
return GetArg(args, value);
}
class CallbackAndContext : public base::RefCounted<CallbackAndContext> {
public:
CallbackAndContext(v8::Isolate* isolate,
v8::Handle<v8::Function> callback,
v8::Handle<v8::Context> context)
: isolate_(isolate) {
callback_.Reset(isolate_, callback);
context_.Reset(isolate_, context);
}
v8::Isolate* isolate() {
return isolate_;
}
v8::Handle<v8::Function> GetCallback() {
return v8::Local<v8::Function>::New(isolate_, callback_);
}
v8::Handle<v8::Context> GetContext() {
return v8::Local<v8::Context>::New(isolate_, context_);
}
private:
friend class base::RefCounted<CallbackAndContext>;
virtual ~CallbackAndContext() {
callback_.Reset();
context_.Reset();
}
v8::Isolate* isolate_;
v8::Persistent<v8::Function> callback_;
v8::Persistent<v8::Context> context_;
DISALLOW_COPY_AND_ASSIGN(CallbackAndContext);
};
class GpuBenchmarkingContext {
public:
GpuBenchmarkingContext()
: web_frame_(NULL),
web_view_(NULL),
render_view_impl_(NULL),
compositor_(NULL) {}
bool Init(bool init_compositor) {
web_frame_ = WebLocalFrame::frameForCurrentContext();
if (!web_frame_)
return false;
web_view_ = web_frame_->view();
if (!web_view_) {
web_frame_ = NULL;
return false;
}
render_view_impl_ = RenderViewImpl::FromWebView(web_view_);
if (!render_view_impl_) {
web_frame_ = NULL;
web_view_ = NULL;
return false;
}
if (!init_compositor)
return true;
compositor_ = render_view_impl_->compositor();
if (!compositor_) {
web_frame_ = NULL;
web_view_ = NULL;
render_view_impl_ = NULL;
return false;
}
return true;
}
WebLocalFrame* web_frame() const {
DCHECK(web_frame_ != NULL);
return web_frame_;
}
WebView* web_view() const {
DCHECK(web_view_ != NULL);
return web_view_;
}
RenderViewImpl* render_view_impl() const {
DCHECK(render_view_impl_ != NULL);
return render_view_impl_;
}
RenderWidgetCompositor* compositor() const {
DCHECK(compositor_ != NULL);
return compositor_;
}
private:
WebLocalFrame* web_frame_;
WebView* web_view_;
RenderViewImpl* render_view_impl_;
RenderWidgetCompositor* compositor_;
DISALLOW_COPY_AND_ASSIGN(GpuBenchmarkingContext);
};
void OnMicroBenchmarkCompleted(
CallbackAndContext* callback_and_context,
scoped_ptr<base::Value> result) {
v8::Isolate* isolate = callback_and_context->isolate();
v8::HandleScope scope(isolate);
v8::Handle<v8::Context> context = callback_and_context->GetContext();
v8::Context::Scope context_scope(context);
WebLocalFrame* frame = WebLocalFrame::frameForContext(context);
if (frame) {
scoped_ptr<V8ValueConverter> converter =
make_scoped_ptr(V8ValueConverter::create());
v8::Handle<v8::Value> value = converter->ToV8Value(result.get(), context);
v8::Handle<v8::Value> argv[] = { value };
frame->callFunctionEvenIfScriptDisabled(
callback_and_context->GetCallback(),
v8::Object::New(isolate),
1,
argv);
}
}
void OnSnapshotCompleted(CallbackAndContext* callback_and_context,
const gfx::Size& size,
const std::vector<unsigned char>& png) {
v8::Isolate* isolate = callback_and_context->isolate();
v8::HandleScope scope(isolate);
v8::Handle<v8::Context> context = callback_and_context->GetContext();
v8::Context::Scope context_scope(context);
WebLocalFrame* frame = WebLocalFrame::frameForContext(context);
if (frame) {
v8::Handle<v8::Value> result;
if (!size.IsEmpty()) {
v8::Handle<v8::Object> result_object;
result_object = v8::Object::New(isolate);
result_object->Set(v8::String::NewFromUtf8(isolate, "width"),
v8::Number::New(isolate, size.width()));
result_object->Set(v8::String::NewFromUtf8(isolate, "height"),
v8::Number::New(isolate, size.height()));
std::string base64_png;
base::Base64Encode(
base::StringPiece(reinterpret_cast<const char*>(&*png.begin()),
png.size()),
&base64_png);
result_object->Set(v8::String::NewFromUtf8(isolate, "data"),
v8::String::NewFromUtf8(isolate,
base64_png.c_str(),
v8::String::kNormalString,
base64_png.size()));
result = result_object;
} else {
result = v8::Null(isolate);
}
v8::Handle<v8::Value> argv[] = {result};
frame->callFunctionEvenIfScriptDisabled(
callback_and_context->GetCallback(), v8::Object::New(isolate), 1, argv);
}
}
void OnSyntheticGestureCompleted(CallbackAndContext* callback_and_context) {
v8::Isolate* isolate = callback_and_context->isolate();
v8::HandleScope scope(isolate);
v8::Handle<v8::Context> context = callback_and_context->GetContext();
v8::Context::Scope context_scope(context);
WebLocalFrame* frame = WebLocalFrame::frameForContext(context);
if (frame) {
frame->callFunctionEvenIfScriptDisabled(
callback_and_context->GetCallback(), v8::Object::New(isolate), 0, NULL);
}
}
bool BeginSmoothScroll(v8::Isolate* isolate,
int pixels_to_scroll,
v8::Handle<v8::Function> callback,
int gesture_source_type,
const std::string& direction,
int speed_in_pixels_s,
bool prevent_fling,
int start_x,
int start_y) {
GpuBenchmarkingContext context;
if (!context.Init(false))
return false;
scoped_refptr<CallbackAndContext> callback_and_context =
new CallbackAndContext(
isolate, callback, context.web_frame()->mainWorldScriptContext());
scoped_ptr<SyntheticSmoothScrollGestureParams> gesture_params(
new SyntheticSmoothScrollGestureParams);
// Convert coordinates from CSS pixels to density independent pixels (DIPs).
float page_scale_factor = context.web_view()->pageScaleFactor();
if (gesture_source_type < 0 ||
gesture_source_type > SyntheticGestureParams::GESTURE_SOURCE_TYPE_MAX) {
return false;
}
gesture_params->gesture_source_type =
static_cast<SyntheticGestureParams::GestureSourceType>(
gesture_source_type);
gesture_params->speed_in_pixels_s = speed_in_pixels_s;
gesture_params->prevent_fling = prevent_fling;
gesture_params->anchor.SetPoint(start_x * page_scale_factor,
start_y * page_scale_factor);
int distance_length = pixels_to_scroll * page_scale_factor;
gfx::Vector2d distance;
if (direction == "down")
distance.set_y(-distance_length);
else if (direction == "up")
distance.set_y(distance_length);
else if (direction == "right")
distance.set_x(-distance_length);
else if (direction == "left")
distance.set_x(distance_length);
else {
return false;
}
gesture_params->distances.push_back(distance);
// TODO(nduca): If the render_view_impl is destroyed while the gesture is in
// progress, we will leak the callback and context. This needs to be fixed,
// somehow.
context.render_view_impl()->QueueSyntheticGesture(
gesture_params.Pass(),
base::Bind(&OnSyntheticGestureCompleted, callback_and_context));
return true;
}
} // namespace
gin::WrapperInfo GpuBenchmarking::kWrapperInfo = {gin::kEmbedderNativeGin};
// static
void GpuBenchmarking::Install(blink::WebFrame* frame) {
v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
gin::Handle<GpuBenchmarking> controller =
gin::CreateHandle(isolate, new GpuBenchmarking());
if (controller.IsEmpty())
return;
v8::Handle<v8::Object> chrome = GetOrCreateChromeObject(isolate,
context->Global());
chrome->Set(gin::StringToV8(isolate, "gpuBenchmarking"), controller.ToV8());
}
GpuBenchmarking::GpuBenchmarking() {
}
GpuBenchmarking::~GpuBenchmarking() {
}
gin::ObjectTemplateBuilder GpuBenchmarking::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin::Wrappable<GpuBenchmarking>::GetObjectTemplateBuilder(isolate)
.SetMethod("setNeedsDisplayOnAllLayers",
&GpuBenchmarking::SetNeedsDisplayOnAllLayers)
.SetMethod("setRasterizeOnlyVisibleContent",
&GpuBenchmarking::SetRasterizeOnlyVisibleContent)
.SetMethod("printToSkPicture", &GpuBenchmarking::PrintToSkPicture)
.SetValue("DEFAULT_INPUT", 0)
.SetValue("TOUCH_INPUT", 1)
.SetValue("MOUSE_INPUT", 2)
.SetMethod("gestureSourceTypeSupported",
&GpuBenchmarking::GestureSourceTypeSupported)
.SetMethod("smoothScrollBy", &GpuBenchmarking::SmoothScrollBy)
.SetMethod("swipe", &GpuBenchmarking::Swipe)
.SetMethod("scrollBounce", &GpuBenchmarking::ScrollBounce)
// TODO(dominikg): Remove once JS interface changes have rolled into
// stable.
.SetValue("newPinchInterface", true)
.SetMethod("pinchBy", &GpuBenchmarking::PinchBy)
.SetMethod("tap", &GpuBenchmarking::Tap)
.SetMethod("beginWindowSnapshotPNG",
&GpuBenchmarking::BeginWindowSnapshotPNG)
.SetMethod("clearImageCache", &GpuBenchmarking::ClearImageCache)
.SetMethod("runMicroBenchmark", &GpuBenchmarking::RunMicroBenchmark)
.SetMethod("sendMessageToMicroBenchmark",
&GpuBenchmarking::SendMessageToMicroBenchmark)
.SetMethod("hasGpuProcess", &GpuBenchmarking::HasGpuProcess);
}
void GpuBenchmarking::SetNeedsDisplayOnAllLayers() {
GpuBenchmarkingContext context;
if (!context.Init(true))
return;
context.compositor()->SetNeedsDisplayOnAllLayers();
}
void GpuBenchmarking::SetRasterizeOnlyVisibleContent() {
GpuBenchmarkingContext context;
if (!context.Init(true))
return;
context.compositor()->SetRasterizeOnlyVisibleContent();
}
void GpuBenchmarking::PrintToSkPicture(v8::Isolate* isolate,
const std::string& dirname) {
GpuBenchmarkingContext context;
if (!context.Init(true))
return;
const cc::Layer* root_layer = context.compositor()->GetRootLayer();
if (!root_layer)
return;
base::FilePath dirpath = base::FilePath::FromUTF8Unsafe(dirname);
if (!base::CreateDirectory(dirpath) ||
!base::PathIsWritable(dirpath)) {
std::string msg("Path is not writable: ");
msg.append(dirpath.MaybeAsASCII());
isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(
isolate, msg.c_str(), v8::String::kNormalString, msg.length())));
return;
}
SkPictureSerializer serializer(dirpath);
serializer.Serialize(root_layer);
}
bool GpuBenchmarking::GestureSourceTypeSupported(int gesture_source_type) {
if (gesture_source_type < 0 ||
gesture_source_type > SyntheticGestureParams::GESTURE_SOURCE_TYPE_MAX) {
return false;
}
return SyntheticGestureParams::IsGestureSourceTypeSupported(
static_cast<SyntheticGestureParams::GestureSourceType>(
gesture_source_type));
}
bool GpuBenchmarking::SmoothScrollBy(gin::Arguments* args) {
GpuBenchmarkingContext context;
if (!context.Init(true))
return false;
float page_scale_factor = context.web_view()->pageScaleFactor();
blink::WebRect rect = context.render_view_impl()->windowRect();
int pixels_to_scroll = 0;
v8::Handle<v8::Function> callback;
int start_x = rect.width / (page_scale_factor * 2);
int start_y = rect.height / (page_scale_factor * 2);
int gesture_source_type = 0; // DEFAULT_INPUT
std::string direction = "down";
int speed_in_pixels_s = 800;
if (!GetOptionalArg(args, &pixels_to_scroll) ||
!GetOptionalArg(args, &callback) ||
!GetOptionalArg(args, &start_x) ||
!GetOptionalArg(args, &start_y) ||
!GetOptionalArg(args, &gesture_source_type) ||
!GetOptionalArg(args, &direction) ||
!GetOptionalArg(args, &speed_in_pixels_s)) {
return false;
}
return BeginSmoothScroll(args->isolate(),
pixels_to_scroll,
callback,
gesture_source_type,
direction,
speed_in_pixels_s,
true,
start_x,
start_y);
}
bool GpuBenchmarking::Swipe(gin::Arguments* args) {
GpuBenchmarkingContext context;
if (!context.Init(true))
return false;
float page_scale_factor = context.web_view()->pageScaleFactor();
blink::WebRect rect = context.render_view_impl()->windowRect();
std::string direction = "up";
int pixels_to_scroll = 0;
v8::Handle<v8::Function> callback;
int start_x = rect.width / (page_scale_factor * 2);
int start_y = rect.height / (page_scale_factor * 2);
int speed_in_pixels_s = 800;
if (!GetOptionalArg(args, &direction) ||
!GetOptionalArg(args, &pixels_to_scroll) ||
!GetOptionalArg(args, &callback) ||
!GetOptionalArg(args, &start_x) ||
!GetOptionalArg(args, &start_y) ||
!GetOptionalArg(args, &speed_in_pixels_s)) {
return false;
}
return BeginSmoothScroll(args->isolate(),
-pixels_to_scroll,
callback,
1, // TOUCH_INPUT
direction,
speed_in_pixels_s,
false,
start_x,
start_y);
}
bool GpuBenchmarking::ScrollBounce(gin::Arguments* args) {
GpuBenchmarkingContext context;
if (!context.Init(false))
return false;
float page_scale_factor = context.web_view()->pageScaleFactor();
blink::WebRect rect = context.render_view_impl()->windowRect();
std::string direction = "down";
int distance_length = 0;
int overscroll_length = 0;
int repeat_count = 1;
v8::Handle<v8::Function> callback;
int start_x = rect.width / (page_scale_factor * 2);
int start_y = rect.height / (page_scale_factor * 2);
int speed_in_pixels_s = 800;
if (!GetOptionalArg(args, &direction) ||
!GetOptionalArg(args, &distance_length) ||
!GetOptionalArg(args, &overscroll_length) ||
!GetOptionalArg(args, &repeat_count) ||
!GetOptionalArg(args, &callback) ||
!GetOptionalArg(args, &start_x) ||
!GetOptionalArg(args, &start_y) ||
!GetOptionalArg(args, &speed_in_pixels_s)) {
return false;
}
scoped_refptr<CallbackAndContext> callback_and_context =
new CallbackAndContext(args->isolate(),
callback,
context.web_frame()->mainWorldScriptContext());
scoped_ptr<SyntheticSmoothScrollGestureParams> gesture_params(
new SyntheticSmoothScrollGestureParams);
gesture_params->speed_in_pixels_s = speed_in_pixels_s;
gesture_params->anchor.SetPoint(start_x * page_scale_factor,
start_y * page_scale_factor);
distance_length *= page_scale_factor;
overscroll_length *= page_scale_factor;
gfx::Vector2d distance;
gfx::Vector2d overscroll;
if (direction == "down") {
distance.set_y(-distance_length);
overscroll.set_y(overscroll_length);
} else if (direction == "up") {
distance.set_y(distance_length);
overscroll.set_y(-overscroll_length);
} else if (direction == "right") {
distance.set_x(-distance_length);
overscroll.set_x(overscroll_length);
} else if (direction == "left") {
distance.set_x(distance_length);
overscroll.set_x(-overscroll_length);
} else {
return false;
}
for (int i = 0; i < repeat_count; i++) {
gesture_params->distances.push_back(distance);
gesture_params->distances.push_back(-distance + overscroll);
}
// TODO(nduca): If the render_view_impl is destroyed while the gesture is in
// progress, we will leak the callback and context. This needs to be fixed,
// somehow.
context.render_view_impl()->QueueSyntheticGesture(
gesture_params.Pass(),
base::Bind(&OnSyntheticGestureCompleted, callback_and_context));
return true;
}
bool GpuBenchmarking::PinchBy(gin::Arguments* args) {
GpuBenchmarkingContext context;
if (!context.Init(false))
return false;
float scale_factor;
int anchor_x;
int anchor_y;
v8::Handle<v8::Function> callback;
int relative_pointer_speed_in_pixels_s = 800;
if (!GetArg(args, &scale_factor) ||
!GetArg(args, &anchor_x) ||
!GetArg(args, &anchor_y) ||
!GetOptionalArg(args, &callback) ||
!GetOptionalArg(args, &relative_pointer_speed_in_pixels_s)) {
return false;
}
scoped_ptr<SyntheticPinchGestureParams> gesture_params(
new SyntheticPinchGestureParams);
// Convert coordinates from CSS pixels to density independent pixels (DIPs).
float page_scale_factor = context.web_view()->pageScaleFactor();
gesture_params->scale_factor = scale_factor;
gesture_params->anchor.SetPoint(anchor_x * page_scale_factor,
anchor_y * page_scale_factor);
gesture_params->relative_pointer_speed_in_pixels_s =
relative_pointer_speed_in_pixels_s;
scoped_refptr<CallbackAndContext> callback_and_context =
new CallbackAndContext(args->isolate(),
callback,
context.web_frame()->mainWorldScriptContext());
// TODO(nduca): If the render_view_impl is destroyed while the gesture is in
// progress, we will leak the callback and context. This needs to be fixed,
// somehow.
context.render_view_impl()->QueueSyntheticGesture(
gesture_params.Pass(),
base::Bind(&OnSyntheticGestureCompleted, callback_and_context));
return true;
}
bool GpuBenchmarking::Tap(gin::Arguments* args) {
GpuBenchmarkingContext context;
if (!context.Init(false))
return false;
int position_x;
int position_y;
v8::Handle<v8::Function> callback;
int duration_ms = 50;
int gesture_source_type = 0; // DEFAULT_INPUT
if (!GetArg(args, &position_x) ||
!GetArg(args, &position_y) ||
!GetOptionalArg(args, &callback) ||
!GetOptionalArg(args, &duration_ms) ||
!GetOptionalArg(args, &gesture_source_type)) {
return false;
}
scoped_ptr<SyntheticTapGestureParams> gesture_params(
new SyntheticTapGestureParams);
// Convert coordinates from CSS pixels to density independent pixels (DIPs).
float page_scale_factor = context.web_view()->pageScaleFactor();
gesture_params->position.SetPoint(position_x * page_scale_factor,
position_y * page_scale_factor);
gesture_params->duration_ms = duration_ms;
if (gesture_source_type < 0 ||
gesture_source_type > SyntheticGestureParams::GESTURE_SOURCE_TYPE_MAX) {
return false;
}
gesture_params->gesture_source_type =
static_cast<SyntheticGestureParams::GestureSourceType>(
gesture_source_type);
scoped_refptr<CallbackAndContext> callback_and_context =
new CallbackAndContext(args->isolate(),
callback,
context.web_frame()->mainWorldScriptContext());
// TODO(nduca): If the render_view_impl is destroyed while the gesture is in
// progress, we will leak the callback and context. This needs to be fixed,
// somehow.
context.render_view_impl()->QueueSyntheticGesture(
gesture_params.Pass(),
base::Bind(&OnSyntheticGestureCompleted, callback_and_context));
return true;
}
void GpuBenchmarking::BeginWindowSnapshotPNG(
v8::Isolate* isolate,
v8::Handle<v8::Function> callback) {
GpuBenchmarkingContext context;
if (!context.Init(false))
return;
scoped_refptr<CallbackAndContext> callback_and_context =
new CallbackAndContext(isolate,
callback,
context.web_frame()->mainWorldScriptContext());
context.render_view_impl()->GetWindowSnapshot(
base::Bind(&OnSnapshotCompleted, callback_and_context));
}
void GpuBenchmarking::ClearImageCache() {
WebImageCache::clear();
}
int GpuBenchmarking::RunMicroBenchmark(gin::Arguments* args) {
GpuBenchmarkingContext context;
if (!context.Init(true))
return 0;
std::string name;
v8::Handle<v8::Function> callback;
v8::Handle<v8::Object> arguments;
if (!GetArg(args, &name) || !GetArg(args, &callback) ||
!GetOptionalArg(args, &arguments)) {
return 0;
}
scoped_refptr<CallbackAndContext> callback_and_context =
new CallbackAndContext(args->isolate(),
callback,
context.web_frame()->mainWorldScriptContext());
scoped_ptr<V8ValueConverter> converter =
make_scoped_ptr(V8ValueConverter::create());
v8::Handle<v8::Context> v8_context = callback_and_context->GetContext();
scoped_ptr<base::Value> value =
make_scoped_ptr(converter->FromV8Value(arguments, v8_context));
return context.compositor()->ScheduleMicroBenchmark(
name,
value.Pass(),
base::Bind(&OnMicroBenchmarkCompleted, callback_and_context));
}
bool GpuBenchmarking::SendMessageToMicroBenchmark(
int id,
v8::Handle<v8::Object> message) {
GpuBenchmarkingContext context;
if (!context.Init(true))
return false;
scoped_ptr<V8ValueConverter> converter =
make_scoped_ptr(V8ValueConverter::create());
v8::Handle<v8::Context> v8_context =
context.web_frame()->mainWorldScriptContext();
scoped_ptr<base::Value> value =
make_scoped_ptr(converter->FromV8Value(message, v8_context));
return context.compositor()->SendMessageToMicroBenchmark(id, value.Pass());
}
bool GpuBenchmarking::HasGpuProcess() {
GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
return !!gpu_channel;
}
} // namespace content
|