Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: pdf/out_of_process_instance.cc

Issue 2400743002: Improved Pinch-Zoom for PDF. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "pdf/out_of_process_instance.h" 5 #include "pdf/out_of_process_instance.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> // for min/max() 10 #include <algorithm> // for min/max()
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 const char kChromeExtension[] = 49 const char kChromeExtension[] =
50 "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai"; 50 "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai";
51 51
52 // Constants used in handling postMessage() messages. 52 // Constants used in handling postMessage() messages.
53 const char kType[] = "type"; 53 const char kType[] = "type";
54 // Viewport message arguments. (Page -> Plugin). 54 // Viewport message arguments. (Page -> Plugin).
55 const char kJSViewportType[] = "viewport"; 55 const char kJSViewportType[] = "viewport";
56 const char kJSXOffset[] = "xOffset"; 56 const char kJSXOffset[] = "xOffset";
57 const char kJSYOffset[] = "yOffset"; 57 const char kJSYOffset[] = "yOffset";
58 const char kJSZoom[] = "zoom"; 58 const char kJSZoom[] = "zoom";
59 const char kJSRender[] = "render";
60 // kJSPx and kJSPy represent the center of the pinch gesture.
61 const char kJSPx[] = "px";
62 const char kJSPy[] = "py";
Kevin McNee - google account 2016/10/06 21:53:17 Comment from previous review: bokan: more descript
Kevin McNee - google account 2016/10/13 18:19:24 Done.
63 // kJSPinchVector represents the amount of panning caused by the pinch gesture.
64 const char kJSPinchVectorX[] = "pinchVectorX";
65 const char kJSPinchVectorY[] = "pinchVectorY";
59 // Stop scrolling message (Page -> Plugin) 66 // Stop scrolling message (Page -> Plugin)
60 const char kJSStopScrollingType[] = "stopScrolling"; 67 const char kJSStopScrollingType[] = "stopScrolling";
61 // Document dimension arguments (Plugin -> Page). 68 // Document dimension arguments (Plugin -> Page).
62 const char kJSDocumentDimensionsType[] = "documentDimensions"; 69 const char kJSDocumentDimensionsType[] = "documentDimensions";
63 const char kJSDocumentWidth[] = "width"; 70 const char kJSDocumentWidth[] = "width";
64 const char kJSDocumentHeight[] = "height"; 71 const char kJSDocumentHeight[] = "height";
65 const char kJSPageDimensions[] = "pageDimensions"; 72 const char kJSPageDimensions[] = "pageDimensions";
66 const char kJSPageX[] = "x"; 73 const char kJSPageX[] = "x";
67 const char kJSPageY[] = "y"; 74 const char kJSPageY[] = "y";
68 const char kJSPageWidth[] = "width"; 75 const char kJSPageWidth[] = "width";
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 } 274 }
268 275
269 } // namespace 276 } // namespace
270 277
271 OutOfProcessInstance::OutOfProcessInstance(PP_Instance instance) 278 OutOfProcessInstance::OutOfProcessInstance(PP_Instance instance)
272 : pp::Instance(instance), 279 : pp::Instance(instance),
273 pp::Find_Private(this), 280 pp::Find_Private(this),
274 pp::Printing_Dev(this), 281 pp::Printing_Dev(this),
275 cursor_(PP_CURSORTYPE_POINTER), 282 cursor_(PP_CURSORTYPE_POINTER),
276 zoom_(1.0), 283 zoom_(1.0),
284 do_render_(true),
285 last_current_scroll_(0, 0),
286 was_smaller_(false),
277 device_scale_(1.0), 287 device_scale_(1.0),
278 full_(false), 288 full_(false),
279 paint_manager_(this, this, true), 289 paint_manager_(this, this, true),
280 first_paint_(true), 290 first_paint_(true),
281 document_load_state_(LOAD_STATE_LOADING), 291 document_load_state_(LOAD_STATE_LOADING),
282 preview_document_load_state_(LOAD_STATE_COMPLETE), 292 preview_document_load_state_(LOAD_STATE_COMPLETE),
283 uma_(this), 293 uma_(this),
284 told_browser_about_unsupported_feature_(false), 294 told_browser_about_unsupported_feature_(false),
285 print_preview_page_count_(0), 295 print_preview_page_count_(0),
286 last_progress_sent_(0), 296 last_progress_sent_(0),
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 } 394 }
385 395
386 void OutOfProcessInstance::HandleMessage(const pp::Var& message) { 396 void OutOfProcessInstance::HandleMessage(const pp::Var& message) {
387 pp::VarDictionary dict(message); 397 pp::VarDictionary dict(message);
388 if (!dict.Get(kType).is_string()) { 398 if (!dict.Get(kType).is_string()) {
389 NOTREACHED(); 399 NOTREACHED();
390 return; 400 return;
391 } 401 }
392 402
393 std::string type = dict.Get(kType).AsString(); 403 std::string type = dict.Get(kType).AsString();
394
395 if (type == kJSViewportType && 404 if (type == kJSViewportType &&
Kevin McNee - google account 2016/10/06 21:53:17 Unnecessary whitespace change.
Kevin McNee - google account 2016/10/13 18:19:24 Done.
396 dict.Get(pp::Var(kJSXOffset)).is_number() && 405 dict.Get(pp::Var(kJSXOffset)).is_number() &&
397 dict.Get(pp::Var(kJSYOffset)).is_number() && 406 dict.Get(pp::Var(kJSYOffset)).is_number() &&
398 dict.Get(pp::Var(kJSZoom)).is_number()) { 407 dict.Get(pp::Var(kJSZoom)).is_number() &&
408 dict.Get(pp::Var(kJSRender)).is_bool() &&
409 dict.Get(pp::Var(kJSPx)).is_number() &&
410 dict.Get(pp::Var(kJSPy)).is_number()) {
Kevin McNee - google account 2016/10/13 18:04:02 Missing check for kJSPinchVectorX and kJSPinchVect
Kevin McNee - google account 2016/10/13 18:19:24 Done.
399 received_viewport_message_ = true; 411 received_viewport_message_ = true;
400 stop_scrolling_ = false; 412 stop_scrolling_ = false;
413 bool do_render = dict.Get(pp::Var(kJSRender)).AsBool();
401 double zoom = dict.Get(pp::Var(kJSZoom)).AsDouble(); 414 double zoom = dict.Get(pp::Var(kJSZoom)).AsDouble();
415 double zoom_delta = zoom / zoom_;
416
417 // Pinch vector is the panning caused due to change in pinch
418 // center between start and end of the gesture.
419 pp::Point pinch_vector =
420 pp::Point(dict.Get(kJSPinchVectorX).AsDouble() * zoom_delta,
421 dict.Get(kJSPinchVectorY).AsDouble() * zoom_delta);
402 pp::FloatPoint scroll_offset(dict.Get(pp::Var(kJSXOffset)).AsDouble(), 422 pp::FloatPoint scroll_offset(dict.Get(pp::Var(kJSXOffset)).AsDouble(),
403 dict.Get(pp::Var(kJSYOffset)).AsDouble()); 423 dict.Get(pp::Var(kJSYOffset)).AsDouble());
424 pp::Point pinch_center(dict.Get(pp::Var(kJSPx)).AsDouble(),
425 dict.Get(pp::Var(kJSPy)).AsDouble());
426 pp::Point scroll_delta(0,0);
427
428 // If the rendered document doesn't fill the display area we will
429 // use paint_offset to anchor the paint vertically into the same place. We
430 // use the scroll bars instead of the pinch vector to get the actual
431 // position on screen of the paint.
432 pp::Point paint_offset(0,0);
433
434 // Pinch start.
Kevin McNee - google account 2016/10/06 21:53:18 Comments from previous review: wjmaclean: Is it wo
wjmaclean 2016/10/07 12:30:21 +1 to bokan's comments here. If we split into two
Kevin McNee - google account 2016/10/13 18:19:24 Done.
435 if (!do_render && do_render_) {
Kevin McNee - google account 2016/10/06 21:53:17 Comment from previous review: bokan: We can also g
wjmaclean 2016/10/07 12:30:21 Presumably that's true. I guess the re-raster stat
Kevin McNee - google account 2016/10/13 18:19:24 Done.
436 last_current_scroll_ = scroll_offset;
437 old_zoom_ = zoom;
438 initial_zoom_delta_ = zoom / zoom_;
439 was_smaller_ = false;
440 do_render_ = false;
441 return;
442 }
443
444 if (!do_render &&
445 plugin_size_.width() > GetDocumentPixelWidth() * zoom_delta) {
446 // We want to keep the paint in the middle but it must stay in the same
447 // position relative to the scroll bars.
448 paint_offset = pp::Point(0, (1 - zoom_delta) * pinch_center.y());
449 scroll_delta = pp::Point(
450 0, (scroll_offset.y() -
451 last_current_scroll_.y() * zoom_delta / initial_zoom_delta_));
452
453 pinch_vector = pp::Point();
454 old_zoom_ = zoom;
455 was_smaller_ = true;
456 } else {
457 if (was_smaller_) {
458 pinch_center = pp::Point((plugin_size_.width() / device_scale_) / 2,
459 (plugin_size_.height() / device_scale_) / 2);
460 paint_offset = pp::Point((1 - zoom / old_zoom_) * pinch_center.x(),
461 (1 - zoom_delta) * pinch_center.y());
462 pinch_vector = pp::Point(0, 0);
463 scroll_delta = pp::Point(
464 (scroll_offset.x() -
465 last_current_scroll_.x() * zoom_delta / initial_zoom_delta_),
466 (scroll_offset.y() -
467 last_current_scroll_.y() * zoom_delta / initial_zoom_delta_));
468 }
469 }
470
471 // While pinching.
Kevin McNee - google account 2016/10/06 21:53:18 Comments from previous review: wjmaclean: See com
Kevin McNee - google account 2016/10/13 18:19:24 Done.
472 if (!do_render) {
473 paint_manager_.SetTransform(zoom_delta, pinch_center,
474 pinch_vector + paint_offset + scroll_delta);
475 do_render_ = do_render;
476 return;
477 }
478
479 // On pinch end the scale is again 1.f and we request a render in the new
480 // position.
481 if (do_render && !do_render_) {
Kevin McNee - google account 2016/10/06 21:53:18 Comment from previous review: wjmaclean: PinchEnd
Kevin McNee - google account 2016/10/13 18:19:24 Done.
482 paint_manager_.SetTransform(1.f);
483 was_smaller_ = false;
484 }
404 485
405 // Bound the input parameters. 486 // Bound the input parameters.
406 zoom = std::max(kMinZoom, zoom); 487 zoom = std::max(kMinZoom, zoom);
407 SetZoom(zoom); 488 SetZoom(zoom);
489
490 do_render_ = do_render;
408 scroll_offset = BoundScrollOffsetToDocument(scroll_offset); 491 scroll_offset = BoundScrollOffsetToDocument(scroll_offset);
409 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_); 492 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_);
410 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_); 493 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_);
411 } else if (type == kJSGetPasswordCompleteType && 494 } else if (type == kJSGetPasswordCompleteType &&
412 dict.Get(pp::Var(kJSPassword)).is_string()) { 495 dict.Get(pp::Var(kJSPassword)).is_string()) {
413 if (password_callback_) { 496 if (password_callback_) {
414 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_; 497 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_;
415 password_callback_.reset(); 498 password_callback_.reset();
416 *callback.output() = dict.Get(pp::Var(kJSPassword)).pp_var(); 499 *callback.output() = dict.Get(pp::Var(kJSPassword)).pp_var();
417 callback.Run(PP_OK); 500 callback.Run(PP_OK);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 float old_device_scale = device_scale_; 627 float old_device_scale = device_scale_;
545 float device_scale = view.GetDeviceScale(); 628 float device_scale = view.GetDeviceScale();
546 pp::Size view_device_size(view_rect.width() * device_scale, 629 pp::Size view_device_size(view_rect.width() * device_scale,
547 view_rect.height() * device_scale); 630 view_rect.height() * device_scale);
548 631
549 if (view_device_size != plugin_size_ || device_scale != device_scale_) { 632 if (view_device_size != plugin_size_ || device_scale != device_scale_) {
550 device_scale_ = device_scale; 633 device_scale_ = device_scale;
551 plugin_dip_size_ = view_rect.size(); 634 plugin_dip_size_ = view_rect.size();
552 plugin_size_ = view_device_size; 635 plugin_size_ = view_device_size;
553 636
554 paint_manager_.SetSize(view_device_size, device_scale_); 637 paint_manager_.SetSize(view_device_size, device_scale_);
Kevin McNee - google account 2016/10/06 21:53:17 Unnecessary whitespace change.
Kevin McNee - google account 2016/10/13 18:19:24 Done.
555
556 pp::Size new_image_data_size = PaintManager::GetNewContextSize( 638 pp::Size new_image_data_size = PaintManager::GetNewContextSize(
557 image_data_.size(), 639 image_data_.size(),
558 plugin_size_); 640 plugin_size_);
559 if (new_image_data_size != image_data_.size()) { 641 if (new_image_data_size != image_data_.size()) {
560 image_data_ = pp::ImageData(this, 642 image_data_ = pp::ImageData(this,
561 PP_IMAGEDATAFORMAT_BGRA_PREMUL, 643 PP_IMAGEDATAFORMAT_BGRA_PREMUL,
562 new_image_data_size, 644 new_image_data_size,
563 false); 645 false);
564 first_paint_ = true; 646 first_paint_ = true;
565 } 647 }
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 DCHECK(plugin_size_.IsEmpty()); 863 DCHECK(plugin_size_.IsEmpty());
782 return; 864 return;
783 } 865 }
784 if (first_paint_) { 866 if (first_paint_) {
785 first_paint_ = false; 867 first_paint_ = false;
786 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size()); 868 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size());
787 FillRect(rect, background_color_); 869 FillRect(rect, background_color_);
788 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true)); 870 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true));
789 } 871 }
790 872
791 if (!received_viewport_message_) 873 if (!received_viewport_message_ || !do_render_)
792 return; 874 return;
793 875
794 engine_->PrePaint(); 876 engine_->PrePaint();
795 877
796 for (const auto& paint_rect : paint_rects) { 878 for (const auto& paint_rect : paint_rects) {
797 // Intersect with plugin area since there could be pending invalidates from 879 // Intersect with plugin area since there could be pending invalidates from
798 // when the plugin area was larger. 880 // when the plugin area was larger.
799 pp::Rect rect = 881 pp::Rect rect =
800 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_)); 882 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_));
801 if (rect.IsEmpty()) 883 if (rect.IsEmpty())
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after
1534 const pp::FloatPoint& scroll_offset) { 1616 const pp::FloatPoint& scroll_offset) {
1535 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); 1617 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width();
1536 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f); 1618 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f);
1537 float min_y = -top_toolbar_height_; 1619 float min_y = -top_toolbar_height_;
1538 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); 1620 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height();
1539 float y = std::max(std::min(scroll_offset.y(), max_y), min_y); 1621 float y = std::max(std::min(scroll_offset.y(), max_y), min_y);
1540 return pp::FloatPoint(x, y); 1622 return pp::FloatPoint(x, y);
1541 } 1623 }
1542 1624
1543 } // namespace chrome_pdf 1625 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698