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