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

Side by Side Diff: pdf/out_of_process_instance.cc

Issue 2400743002: Improved Pinch-Zoom for PDF. (Closed)
Patch Set: Small 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
« no previous file with comments | « pdf/out_of_process_instance.h ('k') | pdf/paint_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_bitmap_smaller_(false),
286 last_zoom_when_smaller_(1.0),
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_ratio = 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 starting_scroll_offset_ = scroll_offset;
422 initial_zoom_ratio_ = zoom_ratio;
423 last_bitmap_smaller_ = false;
424 needs_reraster_ = false;
425 return;
426 }
427
428 if (pinch_phase == PINCH_UPDATE_ZOOM_IN) {
429 if (!(dict.Get(pp::Var(kJSPinchX)).is_number() &&
430 dict.Get(pp::Var(kJSPinchY)).is_number() &&
431 dict.Get(pp::Var(kJSPinchVectorX)).is_number() &&
432 dict.Get(pp::Var(kJSPinchVectorY)).is_number())) {
433 NOTREACHED();
434 return;
435 }
436
437 pp::Point pinch_center(dict.Get(pp::Var(kJSPinchX)).AsDouble(),
438 dict.Get(pp::Var(kJSPinchY)).AsDouble());
439 // Pinch vector is the panning caused due to change in pinch
440 // center between start and end of the gesture.
441 pp::Point pinch_vector =
442 pp::Point(dict.Get(kJSPinchVectorX).AsDouble() * zoom_ratio,
443 dict.Get(kJSPinchVectorY).AsDouble() * zoom_ratio);
444 pp::Point scroll_delta;
445 // If the rendered document doesn't fill the display area we will
446 // use |paint_offset| to anchor the paint vertically into the same place.
447 // We use the scroll bars instead of the pinch vector to get the actual
448 // position on screen of the paint.
449 pp::Point paint_offset;
450
451 if (plugin_size_.width() > GetDocumentPixelWidth() * zoom_ratio) {
452 // We want to keep the paint in the middle but it must stay in the same
453 // position relative to the scroll bars.
454 paint_offset = pp::Point(0, (1 - zoom_ratio) * pinch_center.y());
455 scroll_delta = pp::Point(0,
456 (scroll_offset.y() -
457 starting_scroll_offset_.y() * zoom_ratio / initial_zoom_ratio_));
458
459 pinch_vector = pp::Point();
460 last_zoom_when_smaller_ = zoom;
461 last_bitmap_smaller_ = true;
462 } else if (last_bitmap_smaller_) {
463 pinch_center = pp::Point((plugin_size_.width() / device_scale_) / 2,
464 (plugin_size_.height() / device_scale_) / 2);
465 paint_offset = pp::Point(
466 (1 - zoom / last_zoom_when_smaller_) * pinch_center.x(),
467 (1 - zoom_ratio) * pinch_center.y());
468 pinch_vector = pp::Point();
469 scroll_delta = pp::Point(
470 (scroll_offset.x() -
471 starting_scroll_offset_.x() * zoom_ratio / initial_zoom_ratio_),
472 (scroll_offset.y() -
473 starting_scroll_offset_.y() * zoom_ratio / initial_zoom_ratio_));
474 }
475
476 paint_manager_.SetTransform(zoom_ratio, pinch_center,
477 pinch_vector + paint_offset + scroll_delta,
478 true);
479 needs_reraster_ = false;
480 return;
481 }
482
483 if (pinch_phase == PINCH_UPDATE_ZOOM_OUT || pinch_phase == PINCH_END) {
484 // We reraster on pinch zoom out in order to solve the invalid regions
485 // that appear after zooming out.
486 // On pinch end the scale is again 1.f and we request a reraster
487 // in the new position.
488 paint_manager_.ClearTransform();
489 last_bitmap_smaller_ = false;
490 needs_reraster_ = true;
491 }
492
405 // Bound the input parameters. 493 // Bound the input parameters.
406 zoom = std::max(kMinZoom, zoom); 494 zoom = std::max(kMinZoom, zoom);
407 SetZoom(zoom); 495 SetZoom(zoom);
408 scroll_offset = BoundScrollOffsetToDocument(scroll_offset); 496 scroll_offset = BoundScrollOffsetToDocument(scroll_offset);
409 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_); 497 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_);
410 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_); 498 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_);
411 } else if (type == kJSGetPasswordCompleteType && 499 } else if (type == kJSGetPasswordCompleteType &&
412 dict.Get(pp::Var(kJSPassword)).is_string()) { 500 dict.Get(pp::Var(kJSPassword)).is_string()) {
413 if (password_callback_) { 501 if (password_callback_) {
414 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_; 502 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_;
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 DCHECK(plugin_size_.IsEmpty()); 869 DCHECK(plugin_size_.IsEmpty());
782 return; 870 return;
783 } 871 }
784 if (first_paint_) { 872 if (first_paint_) {
785 first_paint_ = false; 873 first_paint_ = false;
786 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size()); 874 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size());
787 FillRect(rect, background_color_); 875 FillRect(rect, background_color_);
788 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true)); 876 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true));
789 } 877 }
790 878
791 if (!received_viewport_message_) 879 if (!received_viewport_message_ || !needs_reraster_)
792 return; 880 return;
793 881
794 engine_->PrePaint(); 882 engine_->PrePaint();
795 883
796 for (const auto& paint_rect : paint_rects) { 884 for (const auto& paint_rect : paint_rects) {
797 // Intersect with plugin area since there could be pending invalidates from 885 // Intersect with plugin area since there could be pending invalidates from
798 // when the plugin area was larger. 886 // when the plugin area was larger.
799 pp::Rect rect = 887 pp::Rect rect =
800 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_)); 888 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_));
801 if (rect.IsEmpty()) 889 if (rect.IsEmpty())
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after
1534 const pp::FloatPoint& scroll_offset) { 1622 const pp::FloatPoint& scroll_offset) {
1535 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); 1623 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); 1624 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f);
1537 float min_y = -top_toolbar_height_; 1625 float min_y = -top_toolbar_height_;
1538 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); 1626 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); 1627 float y = std::max(std::min(scroll_offset.y(), max_y), min_y);
1540 return pp::FloatPoint(x, y); 1628 return pp::FloatPoint(x, y);
1541 } 1629 }
1542 1630
1543 } // namespace chrome_pdf 1631 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/out_of_process_instance.h ('k') | pdf/paint_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698