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

Side by Side Diff: pdf/out_of_process_instance.cc

Issue 2400743002: Improved Pinch-Zoom for PDF. (Closed)
Patch Set: More code review changes. Created 4 years, 1 month 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 kJSPinchPhase[] = "pinchPhase";
60 // kJSPinchX and kJSPinchY represent the center of the pinch gesture.
61 const char kJSPinchX[] = "pinchX";
62 const char kJSPinchY[] = "pinchY";
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 needs_reraster_(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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 404
395 if (type == kJSViewportType && 405 if (type == kJSViewportType &&
396 dict.Get(pp::Var(kJSXOffset)).is_number() && 406 dict.Get(pp::Var(kJSXOffset)).is_number() &&
397 dict.Get(pp::Var(kJSYOffset)).is_number() && 407 dict.Get(pp::Var(kJSYOffset)).is_number() &&
398 dict.Get(pp::Var(kJSZoom)).is_number()) { 408 dict.Get(pp::Var(kJSZoom)).is_number() &&
409 dict.Get(pp::Var(kJSPinchPhase)).is_number()) {
399 received_viewport_message_ = true; 410 received_viewport_message_ = true;
400 stop_scrolling_ = false; 411 stop_scrolling_ = false;
412 PinchPhase pinch_phase =
413 static_cast<PinchPhase>(dict.Get(pp::Var(kJSPinchPhase)).AsInt());
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
402 pp::FloatPoint scroll_offset(dict.Get(pp::Var(kJSXOffset)).AsDouble(), 417 pp::FloatPoint scroll_offset(dict.Get(pp::Var(kJSXOffset)).AsDouble(),
403 dict.Get(pp::Var(kJSYOffset)).AsDouble()); 418 dict.Get(pp::Var(kJSYOffset)).AsDouble());
404 419
420 if (pinch_phase == PINCH_START) {
421 last_current_scroll_ = scroll_offset;
422 old_zoom_ = zoom;
Lei Zhang 2016/10/27 23:04:33 From just reading the header, I thought this was t
Kevin McNee - google account 2016/10/28 19:19:37 If I'm following alessandroa's logic correctly, th
423 initial_zoom_delta_ = zoom_delta;
Lei Zhang 2016/10/27 23:04:33 It's not clear that this is a ratio and not a diff
Kevin McNee - google account 2016/10/28 19:19:37 Done.
424 was_smaller_ = false;
425 needs_reraster_ = false;
426 return;
427 }
428
429 if (pinch_phase == PINCH_UPDATE_ZOOM_IN) {
430 if (!(dict.Get(pp::Var(kJSPinchX)).is_number() &&
431 dict.Get(pp::Var(kJSPinchY)).is_number() &&
432 dict.Get(pp::Var(kJSPinchVectorX)).is_number() &&
433 dict.Get(pp::Var(kJSPinchVectorY)).is_number())) {
434 NOTREACHED();
435 return;
436 }
437
438 pp::Point pinch_center(dict.Get(pp::Var(kJSPinchX)).AsDouble(),
439 dict.Get(pp::Var(kJSPinchY)).AsDouble());
440 // Pinch vector is the panning caused due to change in pinch
441 // center between start and end of the gesture.
442 pp::Point pinch_vector =
443 pp::Point(dict.Get(kJSPinchVectorX).AsDouble() * zoom_delta,
444 dict.Get(kJSPinchVectorY).AsDouble() * zoom_delta);
445 pp::Point scroll_delta(0, 0);
Lei Zhang 2016/10/27 23:04:33 nit: No need to explicitly initialized to (0, 0).
Kevin McNee - google account 2016/10/28 19:19:37 Done.
446 // If the rendered document doesn't fill the display area we will
447 // use paint_offset to anchor the paint vertically into the same place. We
Lei Zhang 2016/10/27 23:04:33 nit: Refer to variables as |varname|.
Kevin McNee - google account 2016/10/28 19:19:37 Done.
448 // use the scroll bars instead of the pinch vector to get the actual
449 // position on screen of the paint.
450 pp::Point paint_offset(0, 0);
451
452 if (plugin_size_.width() > GetDocumentPixelWidth() * zoom_delta) {
453 // We want to keep the paint in the middle but it must stay in the same
454 // position relative to the scroll bars.
455 paint_offset = pp::Point(0, (1 - zoom_delta) * pinch_center.y());
456 scroll_delta = pp::Point(0,
457 (scroll_offset.y() -
458 last_current_scroll_.y() * zoom_delta / initial_zoom_delta_));
459
460 pinch_vector = pp::Point();
461 old_zoom_ = zoom;
462 was_smaller_ = true;
463 } else if (was_smaller_) {
464 pinch_center = pp::Point((plugin_size_.width() / device_scale_) / 2,
465 (plugin_size_.height() / device_scale_) / 2);
466 paint_offset = pp::Point((1 - zoom / old_zoom_) * pinch_center.x(),
467 (1 - zoom_delta) * pinch_center.y());
468 pinch_vector = pp::Point(0, 0);
469 scroll_delta = pp::Point(
470 (scroll_offset.x() -
471 last_current_scroll_.x() * zoom_delta / initial_zoom_delta_),
472 (scroll_offset.y() -
473 last_current_scroll_.y() * zoom_delta / initial_zoom_delta_));
474 }
475
476 paint_manager_.SetTransform(zoom_delta, pinch_center,
477 pinch_vector + paint_offset + scroll_delta);
478 needs_reraster_ = false;
479 return;
480 }
481
482 if (pinch_phase == PINCH_UPDATE_ZOOM_OUT || pinch_phase == PINCH_END) {
483 // We reraster on pinch zoom out in order to solve the invalid regions
484 // that appear after zooming out.
485 // On pinch end the scale is again 1.f and we request a reraster
486 // in the new position.
487 paint_manager_.SetTransform(1.f);
488 was_smaller_ = false;
489 needs_reraster_ = true;
490 }
491
405 // Bound the input parameters. 492 // Bound the input parameters.
406 zoom = std::max(kMinZoom, zoom); 493 zoom = std::max(kMinZoom, zoom);
407 SetZoom(zoom); 494 SetZoom(zoom);
408 scroll_offset = BoundScrollOffsetToDocument(scroll_offset); 495 scroll_offset = BoundScrollOffsetToDocument(scroll_offset);
409 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_); 496 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_);
410 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_); 497 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_);
411 } else if (type == kJSGetPasswordCompleteType && 498 } else if (type == kJSGetPasswordCompleteType &&
412 dict.Get(pp::Var(kJSPassword)).is_string()) { 499 dict.Get(pp::Var(kJSPassword)).is_string()) {
413 if (password_callback_) { 500 if (password_callback_) {
414 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_; 501 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_;
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 DCHECK(plugin_size_.IsEmpty()); 868 DCHECK(plugin_size_.IsEmpty());
782 return; 869 return;
783 } 870 }
784 if (first_paint_) { 871 if (first_paint_) {
785 first_paint_ = false; 872 first_paint_ = false;
786 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size()); 873 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size());
787 FillRect(rect, background_color_); 874 FillRect(rect, background_color_);
788 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true)); 875 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true));
789 } 876 }
790 877
791 if (!received_viewport_message_) 878 if (!received_viewport_message_ || !needs_reraster_)
792 return; 879 return;
793 880
794 engine_->PrePaint(); 881 engine_->PrePaint();
795 882
796 for (const auto& paint_rect : paint_rects) { 883 for (const auto& paint_rect : paint_rects) {
797 // Intersect with plugin area since there could be pending invalidates from 884 // Intersect with plugin area since there could be pending invalidates from
798 // when the plugin area was larger. 885 // when the plugin area was larger.
799 pp::Rect rect = 886 pp::Rect rect =
800 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_)); 887 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_));
801 if (rect.IsEmpty()) 888 if (rect.IsEmpty())
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after
1534 const pp::FloatPoint& scroll_offset) { 1621 const pp::FloatPoint& scroll_offset) {
1535 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); 1622 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); 1623 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f);
1537 float min_y = -top_toolbar_height_; 1624 float min_y = -top_toolbar_height_;
1538 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); 1625 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); 1626 float y = std::max(std::min(scroll_offset.y(), max_y), min_y);
1540 return pp::FloatPoint(x, y); 1627 return pp::FloatPoint(x, y);
1541 } 1628 }
1542 1629
1543 } // namespace chrome_pdf 1630 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698