OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/gpu/gpu_benchmarking_extension.h" | 5 #include "content/renderer/gpu/gpu_benchmarking_extension.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "cc/layers/layer.h" | 14 #include "cc/layers/layer.h" |
15 #include "content/common/browser_rendering_stats.h" | 15 #include "content/common/browser_rendering_stats.h" |
16 #include "content/common/gpu/gpu_rendering_stats.h" | 16 #include "content/common/gpu/gpu_rendering_stats.h" |
| 17 #include "content/common/input/synthetic_gesture_params.h" |
| 18 #include "content/common/input/synthetic_pinch_gesture_params.h" |
| 19 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" |
17 #include "content/public/renderer/render_thread.h" | 20 #include "content/public/renderer/render_thread.h" |
18 #include "content/public/renderer/v8_value_converter.h" | 21 #include "content/public/renderer/v8_value_converter.h" |
19 #include "content/renderer/gpu/render_widget_compositor.h" | 22 #include "content/renderer/gpu/render_widget_compositor.h" |
20 #include "content/renderer/render_thread_impl.h" | 23 #include "content/renderer/render_thread_impl.h" |
21 #include "content/renderer/render_view_impl.h" | 24 #include "content/renderer/render_view_impl.h" |
22 #include "content/renderer/skia_benchmarking_extension.h" | 25 #include "content/renderer/skia_benchmarking_extension.h" |
23 #include "third_party/WebKit/public/web/WebFrame.h" | 26 #include "third_party/WebKit/public/web/WebFrame.h" |
24 #include "third_party/WebKit/public/web/WebImageCache.h" | 27 #include "third_party/WebKit/public/web/WebImageCache.h" |
25 #include "third_party/WebKit/public/web/WebView.h" | 28 #include "third_party/WebKit/public/web/WebView.h" |
26 #include "third_party/skia/include/core/SkData.h" | 29 #include "third_party/skia/include/core/SkData.h" |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 " return GetRenderingStats();" | 263 " return GetRenderingStats();" |
261 "};" | 264 "};" |
262 "chrome.gpuBenchmarking.gpuRenderingStats = function() {" | 265 "chrome.gpuBenchmarking.gpuRenderingStats = function() {" |
263 " native function GetGpuRenderingStats();" | 266 " native function GetGpuRenderingStats();" |
264 " return GetGpuRenderingStats();" | 267 " return GetGpuRenderingStats();" |
265 "};" | 268 "};" |
266 "chrome.gpuBenchmarking.printToSkPicture = function(dirname) {" | 269 "chrome.gpuBenchmarking.printToSkPicture = function(dirname) {" |
267 " native function PrintToSkPicture();" | 270 " native function PrintToSkPicture();" |
268 " return PrintToSkPicture(dirname);" | 271 " return PrintToSkPicture(dirname);" |
269 "};" | 272 "};" |
| 273 "chrome.gpuBenchmarking.DEFAULT_INPUT = 0;" |
| 274 "chrome.gpuBenchmarking.TOUCH_INPUT = 1;" |
| 275 "chrome.gpuBenchmarking.MOUSE_INPUT = 2;" |
270 "chrome.gpuBenchmarking.smoothScrollBy = " | 276 "chrome.gpuBenchmarking.smoothScrollBy = " |
271 " function(pixels_to_scroll, opt_callback, opt_mouse_event_x," | 277 " function(pixels_to_scroll, opt_callback, opt_mouse_event_x," |
272 " opt_mouse_event_y) {" | 278 " opt_mouse_event_y, opt_gesture_source_type," |
| 279 " opt_speed_in_pixels_s) {" |
273 " pixels_to_scroll = pixels_to_scroll || 0;" | 280 " pixels_to_scroll = pixels_to_scroll || 0;" |
274 " callback = opt_callback || function() { };" | 281 " callback = opt_callback || function() { };" |
| 282 " gesture_source_type = opt_gesture_source_type ||" |
| 283 " chrome.gpuBenchmarking.DEFAULT_INPUT;" |
| 284 " speed_in_pixels_s = opt_speed_in_pixels_s || 800;" |
275 " native function BeginSmoothScroll();" | 285 " native function BeginSmoothScroll();" |
276 " if (typeof opt_mouse_event_x !== 'undefined' &&" | 286 " if (typeof opt_mouse_event_x !== 'undefined' &&" |
277 " typeof opt_mouse_event_y !== 'undefined') {" | 287 " typeof opt_mouse_event_y !== 'undefined') {" |
278 " return BeginSmoothScroll(pixels_to_scroll >= 0, callback," | 288 " return BeginSmoothScroll(pixels_to_scroll, callback," |
279 " Math.abs(pixels_to_scroll)," | 289 " gesture_source_type, speed_in_pixels_s," |
280 " opt_mouse_event_x, opt_mouse_event_y);" | 290 " opt_mouse_event_x, opt_mouse_event_y);" |
281 " } else {" | 291 " } else {" |
282 " return BeginSmoothScroll(pixels_to_scroll >= 0, callback," | 292 " return BeginSmoothScroll(pixels_to_scroll, callback," |
283 " Math.abs(pixels_to_scroll));" | 293 " gesture_source_type," |
| 294 " speed_in_pixels_s);" |
284 " }" | 295 " }" |
285 "};" | 296 "};" |
286 "chrome.gpuBenchmarking.smoothScrollBySendsTouch = function() {" | 297 "chrome.gpuBenchmarking.smoothScrollBySendsTouch = function() {" |
287 " native function SmoothScrollSendsTouch();" | 298 " native function SmoothScrollSendsTouch();" |
288 " return SmoothScrollSendsTouch();" | 299 " return SmoothScrollSendsTouch();" |
289 "};" | 300 "};" |
290 "chrome.gpuBenchmarking.pinchBy = " | 301 "chrome.gpuBenchmarking.pinchBy = " |
291 " function(zoom_in, pixels_to_move, anchor_x, anchor_y," | 302 " function(zoom_in, pixels_to_cover, anchor_x, anchor_y," |
292 " opt_callback) {" | 303 " opt_callback, opt_relative_pointer_speed_in_pixels_s) {" |
293 " callback = opt_callback || function() { };" | 304 " callback = opt_callback || function() { };" |
| 305 " relative_pointer_speed_in_pixels_s =" |
| 306 " opt_relative_pointer_speed_in_pixels_s || 800;" |
294 " native function BeginPinch();" | 307 " native function BeginPinch();" |
295 " return BeginPinch(zoom_in, pixels_to_move," | 308 " return BeginPinch(zoom_in, pixels_to_cover," |
296 " anchor_x, anchor_y, callback);" | 309 " anchor_x, anchor_y, callback," |
| 310 " relative_pointer_speed_in_pixels_s);" |
297 "};" | 311 "};" |
298 "chrome.gpuBenchmarking.beginWindowSnapshotPNG = function(callback) {" | 312 "chrome.gpuBenchmarking.beginWindowSnapshotPNG = function(callback) {" |
299 " native function BeginWindowSnapshotPNG();" | 313 " native function BeginWindowSnapshotPNG();" |
300 " BeginWindowSnapshotPNG(callback);" | 314 " BeginWindowSnapshotPNG(callback);" |
301 "};" | 315 "};" |
302 "chrome.gpuBenchmarking.clearImageCache = function() {" | 316 "chrome.gpuBenchmarking.clearImageCache = function() {" |
303 " native function ClearImageCache();" | 317 " native function ClearImageCache();" |
304 " ClearImageCache();" | 318 " ClearImageCache();" |
305 "};" | 319 "};" |
306 "chrome.gpuBenchmarking.runMicroBenchmark =" | 320 "chrome.gpuBenchmarking.runMicroBenchmark =" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 msg.append(dirpath.MaybeAsASCII()); | 442 msg.append(dirpath.MaybeAsASCII()); |
429 v8::ThrowException(v8::Exception::Error( | 443 v8::ThrowException(v8::Exception::Error( |
430 v8::String::New(msg.c_str(), msg.length()))); | 444 v8::String::New(msg.c_str(), msg.length()))); |
431 return; | 445 return; |
432 } | 446 } |
433 | 447 |
434 SkPictureSerializer serializer(dirpath); | 448 SkPictureSerializer serializer(dirpath); |
435 serializer.Serialize(root_layer); | 449 serializer.Serialize(root_layer); |
436 } | 450 } |
437 | 451 |
438 static void OnSmoothScrollCompleted( | 452 static void OnSyntheticGestureCompleted( |
439 CallbackAndContext* callback_and_context) { | 453 CallbackAndContext* callback_and_context) { |
440 v8::HandleScope scope(callback_and_context->isolate()); | 454 v8::HandleScope scope(callback_and_context->isolate()); |
441 v8::Handle<v8::Context> context = callback_and_context->GetContext(); | 455 v8::Handle<v8::Context> context = callback_and_context->GetContext(); |
442 v8::Context::Scope context_scope(context); | 456 v8::Context::Scope context_scope(context); |
443 WebFrame* frame = WebFrame::frameForContext(context); | 457 WebFrame* frame = WebFrame::frameForContext(context); |
444 if (frame) { | 458 if (frame) { |
445 frame->callFunctionEvenIfScriptDisabled( | 459 frame->callFunctionEvenIfScriptDisabled( |
446 callback_and_context->GetCallback(), v8::Object::New(), 0, NULL); | 460 callback_and_context->GetCallback(), v8::Object::New(), 0, NULL); |
447 } | 461 } |
448 } | 462 } |
449 | 463 |
450 static void SmoothScrollSendsTouch( | 464 static void SmoothScrollSendsTouch( |
451 const v8::FunctionCallbackInfo<v8::Value>& args) { | 465 const v8::FunctionCallbackInfo<v8::Value>& args) { |
452 // TODO(epenner): Should other platforms emulate touch events? | 466 // TODO(epenner): Should other platforms emulate touch events? |
453 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 467 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
454 args.GetReturnValue().Set(true); | 468 args.GetReturnValue().Set(true); |
455 #else | 469 #else |
456 args.GetReturnValue().Set(false); | 470 args.GetReturnValue().Set(false); |
457 #endif | 471 #endif |
458 } | 472 } |
459 | 473 |
460 static void BeginSmoothScroll( | 474 static void BeginSmoothScroll( |
461 const v8::FunctionCallbackInfo<v8::Value>& args) { | 475 const v8::FunctionCallbackInfo<v8::Value>& args) { |
462 GpuBenchmarkingContext context; | 476 GpuBenchmarkingContext context; |
463 if (!context.Init(false)) | 477 if (!context.Init(false)) |
464 return; | 478 return; |
465 | 479 |
466 // Account for the 2 optional arguments, mouse_event_x and mouse_event_y. | 480 // Account for the 2 optional arguments, mouse_event_x and mouse_event_y. |
467 int arglen = args.Length(); | 481 int arglen = args.Length(); |
468 if (arglen < 3 || | 482 if (arglen < 4 || |
469 !args[0]->IsBoolean() || | 483 !args[0]->IsNumber() || |
470 !args[1]->IsFunction() || | 484 !args[1]->IsFunction() || |
471 !args[2]->IsNumber()) { | 485 !args[2]->IsNumber() || |
| 486 !args[3]->IsNumber()) { |
472 args.GetReturnValue().Set(false); | 487 args.GetReturnValue().Set(false); |
473 return; | 488 return; |
474 } | 489 } |
475 | 490 |
476 bool scroll_down = args[0]->BooleanValue(); | |
477 v8::Local<v8::Function> callback_local = | 491 v8::Local<v8::Function> callback_local = |
478 v8::Local<v8::Function>::Cast(args[1]); | 492 v8::Local<v8::Function>::Cast(args[1]); |
479 | 493 |
480 scoped_refptr<CallbackAndContext> callback_and_context = | 494 scoped_refptr<CallbackAndContext> callback_and_context = |
481 new CallbackAndContext(args.GetIsolate(), | 495 new CallbackAndContext(args.GetIsolate(), |
482 callback_local, | 496 callback_local, |
483 context.web_frame()->mainWorldScriptContext()); | 497 context.web_frame()->mainWorldScriptContext()); |
484 | 498 |
| 499 scoped_ptr<SyntheticSmoothScrollGestureParams> gesture_params( |
| 500 new SyntheticSmoothScrollGestureParams); |
| 501 |
485 // Convert coordinates from CSS pixels to density independent pixels (DIPs). | 502 // Convert coordinates from CSS pixels to density independent pixels (DIPs). |
486 float page_scale_factor = context.web_view()->pageScaleFactor(); | 503 float page_scale_factor = context.web_view()->pageScaleFactor(); |
487 | 504 |
488 int pixels_to_scroll = args[2]->IntegerValue() * page_scale_factor; | 505 gesture_params->distance = args[0]->IntegerValue() * page_scale_factor; |
| 506 int gesture_source_type = args[2]->IntegerValue(); |
| 507 if (gesture_source_type < 0 || |
| 508 gesture_source_type > SyntheticGestureParams::GESTURE_SOURCE_TYPE_MAX) { |
| 509 args.GetReturnValue().Set(false); |
| 510 return; |
| 511 } |
| 512 gesture_params->gesture_source_type = |
| 513 static_cast<SyntheticGestureParams::GestureSourceType>( |
| 514 gesture_source_type); |
| 515 gesture_params->speed_in_pixels_s = args[3]->IntegerValue(); |
489 | 516 |
490 int mouse_event_x = 0; | 517 if (arglen == 4) { |
491 int mouse_event_y = 0; | |
492 | |
493 if (arglen == 3) { | |
494 blink::WebRect rect = context.render_view_impl()->windowRect(); | 518 blink::WebRect rect = context.render_view_impl()->windowRect(); |
495 mouse_event_x = rect.x + rect.width / 2; | 519 gesture_params->anchor.SetPoint(rect.x + rect.width / 2, |
496 mouse_event_y = rect.y + rect.height / 2; | 520 rect.y + rect.height / 2); |
497 } else { | 521 } else { |
498 if (arglen != 5 || | 522 if (arglen != 6 || |
499 !args[3]->IsNumber() || | 523 !args[4]->IsNumber() || |
500 !args[4]->IsNumber()) { | 524 !args[5]->IsNumber()) { |
501 args.GetReturnValue().Set(false); | 525 args.GetReturnValue().Set(false); |
502 return; | 526 return; |
503 } | 527 } |
504 | 528 |
505 mouse_event_x = args[3]->IntegerValue() * page_scale_factor; | 529 gesture_params->anchor.SetPoint( |
506 mouse_event_y = args[4]->IntegerValue() * page_scale_factor; | 530 args[4]->IntegerValue() * page_scale_factor, |
| 531 args[5]->IntegerValue() * page_scale_factor); |
507 } | 532 } |
508 | 533 |
509 // TODO(nduca): If the render_view_impl is destroyed while the gesture is in | 534 // TODO(nduca): If the render_view_impl is destroyed while the gesture is in |
510 // progress, we will leak the callback and context. This needs to be fixed, | 535 // progress, we will leak the callback and context. This needs to be fixed, |
511 // somehow. | 536 // somehow. |
512 context.render_view_impl()->BeginSmoothScroll( | 537 context.render_view_impl()->QueueSyntheticGesture( |
513 scroll_down, | 538 gesture_params.PassAs<SyntheticGestureParams>(), |
514 base::Bind(&OnSmoothScrollCompleted, | 539 base::Bind(&OnSyntheticGestureCompleted, |
515 callback_and_context), | 540 callback_and_context)); |
516 pixels_to_scroll, | |
517 mouse_event_x, | |
518 mouse_event_y); | |
519 | 541 |
520 args.GetReturnValue().Set(true); | 542 args.GetReturnValue().Set(true); |
521 } | 543 } |
522 | 544 |
523 static void BeginPinch( | 545 static void BeginPinch( |
524 const v8::FunctionCallbackInfo<v8::Value>& args) { | 546 const v8::FunctionCallbackInfo<v8::Value>& args) { |
525 GpuBenchmarkingContext context; | 547 GpuBenchmarkingContext context; |
526 if (!context.Init(false)) | 548 if (!context.Init(false)) |
527 return; | 549 return; |
528 | 550 |
529 int arglen = args.Length(); | 551 int arglen = args.Length(); |
530 if (arglen < 5 || | 552 if (arglen < 6 || |
531 !args[0]->IsBoolean() || | 553 !args[0]->IsBoolean() || |
532 !args[1]->IsNumber() || | 554 !args[1]->IsNumber() || |
533 !args[2]->IsNumber() || | 555 !args[2]->IsNumber() || |
534 !args[3]->IsNumber() || | 556 !args[3]->IsNumber() || |
535 !args[4]->IsFunction()) { | 557 !args[4]->IsFunction() || |
| 558 !args[5]->IsNumber()) { |
536 args.GetReturnValue().Set(false); | 559 args.GetReturnValue().Set(false); |
537 return; | 560 return; |
538 } | 561 } |
539 | 562 |
| 563 scoped_ptr<SyntheticPinchGestureParams> gesture_params( |
| 564 new SyntheticPinchGestureParams); |
| 565 |
540 // Convert coordinates from CSS pixels to density independent pixels (DIPs). | 566 // Convert coordinates from CSS pixels to density independent pixels (DIPs). |
541 float page_scale_factor = context.web_view()->pageScaleFactor(); | 567 float page_scale_factor = context.web_view()->pageScaleFactor(); |
542 | 568 |
543 bool zoom_in = args[0]->BooleanValue(); | 569 gesture_params->zoom_in = args[0]->BooleanValue(); |
544 int pixels_to_move = args[1]->IntegerValue() * page_scale_factor; | 570 gesture_params->total_num_pixels_covered = |
545 int anchor_x = args[2]->IntegerValue() * page_scale_factor; | 571 args[1]->IntegerValue() * page_scale_factor; |
546 int anchor_y = args[3]->IntegerValue() * page_scale_factor; | 572 gesture_params->anchor.SetPoint( |
| 573 args[2]->IntegerValue() * page_scale_factor, |
| 574 args[3]->IntegerValue() * page_scale_factor); |
| 575 gesture_params->relative_pointer_speed_in_pixels_s = |
| 576 args[5]->IntegerValue(); |
547 | 577 |
548 v8::Local<v8::Function> callback_local = | 578 v8::Local<v8::Function> callback_local = |
549 v8::Local<v8::Function>::Cast(args[4]); | 579 v8::Local<v8::Function>::Cast(args[4]); |
550 | 580 |
551 scoped_refptr<CallbackAndContext> callback_and_context = | 581 scoped_refptr<CallbackAndContext> callback_and_context = |
552 new CallbackAndContext(args.GetIsolate(), | 582 new CallbackAndContext(args.GetIsolate(), |
553 callback_local, | 583 callback_local, |
554 context.web_frame()->mainWorldScriptContext()); | 584 context.web_frame()->mainWorldScriptContext()); |
555 | 585 |
556 | 586 |
557 // TODO(nduca): If the render_view_impl is destroyed while the gesture is in | 587 // TODO(nduca): If the render_view_impl is destroyed while the gesture is in |
558 // progress, we will leak the callback and context. This needs to be fixed, | 588 // progress, we will leak the callback and context. This needs to be fixed, |
559 // somehow. | 589 // somehow. |
560 context.render_view_impl()->BeginPinch( | 590 context.render_view_impl()->QueueSyntheticGesture( |
561 zoom_in, | 591 gesture_params.PassAs<SyntheticGestureParams>(), |
562 pixels_to_move, | 592 base::Bind(&OnSyntheticGestureCompleted, |
563 anchor_x, | |
564 anchor_y, | |
565 base::Bind(&OnSmoothScrollCompleted, | |
566 callback_and_context)); | 593 callback_and_context)); |
567 | 594 |
568 args.GetReturnValue().Set(true); | 595 args.GetReturnValue().Set(true); |
569 } | 596 } |
570 | 597 |
571 static void OnSnapshotCompleted(CallbackAndContext* callback_and_context, | 598 static void OnSnapshotCompleted(CallbackAndContext* callback_and_context, |
572 const gfx::Size& size, | 599 const gfx::Size& size, |
573 const std::vector<unsigned char>& png) { | 600 const std::vector<unsigned char>& png) { |
574 v8::HandleScope scope(callback_and_context->isolate()); | 601 v8::HandleScope scope(callback_and_context->isolate()); |
575 v8::Handle<v8::Context> context = callback_and_context->GetContext(); | 602 v8::Handle<v8::Context> context = callback_and_context->GetContext(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel(); | 721 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel(); |
695 args.GetReturnValue().Set(!!gpu_channel); | 722 args.GetReturnValue().Set(!!gpu_channel); |
696 } | 723 } |
697 }; | 724 }; |
698 | 725 |
699 v8::Extension* GpuBenchmarkingExtension::Get() { | 726 v8::Extension* GpuBenchmarkingExtension::Get() { |
700 return new GpuBenchmarkingWrapper(); | 727 return new GpuBenchmarkingWrapper(); |
701 } | 728 } |
702 | 729 |
703 } // namespace content | 730 } // namespace content |
OLD | NEW |