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

Side by Side Diff: pdf/out_of_process_instance.cc

Issue 1901903002: Improved Pinch-Zoom for PDF (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pepper_add_set_layer_transform
Patch Set: Another patch of fixes Created 4 years, 8 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 const char kAccessibleLoaded[] = "loaded"; 56 const char kAccessibleLoaded[] = "loaded";
57 const char kAccessibleCopyable[] = "copyable"; 57 const char kAccessibleCopyable[] = "copyable";
58 58
59 // Constants used in handling postMessage() messages. 59 // Constants used in handling postMessage() messages.
60 const char kType[] = "type"; 60 const char kType[] = "type";
61 // Viewport message arguments. (Page -> Plugin). 61 // Viewport message arguments. (Page -> Plugin).
62 const char kJSViewportType[] = "viewport"; 62 const char kJSViewportType[] = "viewport";
63 const char kJSXOffset[] = "xOffset"; 63 const char kJSXOffset[] = "xOffset";
64 const char kJSYOffset[] = "yOffset"; 64 const char kJSYOffset[] = "yOffset";
65 const char kJSZoom[] = "zoom"; 65 const char kJSZoom[] = "zoom";
66 const char kJSRender[] = "render";
67 // kJSPx and kJSPy represent the center of the pinch gesture.
68 const char kJSPx[] = "px";
69 const char kJSPy[] = "py";
70 // kJSPinchVector represents the amount of panning caused by the pinch gesture.
71 const char kJSPinchVectorX[] = "pinchVectorX";
72 const char kJSPinchVectorY[] = "pinchVectorY";
66 // Stop scrolling message (Page -> Plugin) 73 // Stop scrolling message (Page -> Plugin)
67 const char kJSStopScrollingType[] = "stopScrolling"; 74 const char kJSStopScrollingType[] = "stopScrolling";
68 // Document dimension arguments (Plugin -> Page). 75 // Document dimension arguments (Plugin -> Page).
69 const char kJSDocumentDimensionsType[] = "documentDimensions"; 76 const char kJSDocumentDimensionsType[] = "documentDimensions";
70 const char kJSDocumentWidth[] = "width"; 77 const char kJSDocumentWidth[] = "width";
71 const char kJSDocumentHeight[] = "height"; 78 const char kJSDocumentHeight[] = "height";
72 const char kJSPageDimensions[] = "pageDimensions"; 79 const char kJSPageDimensions[] = "pageDimensions";
73 const char kJSPageX[] = "x"; 80 const char kJSPageX[] = "x";
74 const char kJSPageY[] = "y"; 81 const char kJSPageY[] = "y";
75 const char kJSPageWidth[] = "width"; 82 const char kJSPageWidth[] = "width";
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 } 273 }
267 274
268 } // namespace 275 } // namespace
269 276
270 OutOfProcessInstance::OutOfProcessInstance(PP_Instance instance) 277 OutOfProcessInstance::OutOfProcessInstance(PP_Instance instance)
271 : pp::Instance(instance), 278 : pp::Instance(instance),
272 pp::Find_Private(this), 279 pp::Find_Private(this),
273 pp::Printing_Dev(this), 280 pp::Printing_Dev(this),
274 cursor_(PP_CURSORTYPE_POINTER), 281 cursor_(PP_CURSORTYPE_POINTER),
275 zoom_(1.0), 282 zoom_(1.0),
283 do_render_(true),
284 last_current_scroll_(0, 0),
285 was_smaller_(false),
276 device_scale_(1.0), 286 device_scale_(1.0),
277 full_(false), 287 full_(false),
278 paint_manager_(this, this, true), 288 paint_manager_(this, this, true),
279 first_paint_(true), 289 first_paint_(true),
280 document_load_state_(LOAD_STATE_LOADING), 290 document_load_state_(LOAD_STATE_LOADING),
281 preview_document_load_state_(LOAD_STATE_COMPLETE), 291 preview_document_load_state_(LOAD_STATE_COMPLETE),
282 uma_(this), 292 uma_(this),
283 told_browser_about_unsupported_feature_(false), 293 told_browser_about_unsupported_feature_(false),
284 print_preview_page_count_(0), 294 print_preview_page_count_(0),
285 last_progress_sent_(0), 295 last_progress_sent_(0),
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 } 389 }
380 390
381 void OutOfProcessInstance::HandleMessage(const pp::Var& message) { 391 void OutOfProcessInstance::HandleMessage(const pp::Var& message) {
382 pp::VarDictionary dict(message); 392 pp::VarDictionary dict(message);
383 if (!dict.Get(kType).is_string()) { 393 if (!dict.Get(kType).is_string()) {
384 NOTREACHED(); 394 NOTREACHED();
385 return; 395 return;
386 } 396 }
387 397
388 std::string type = dict.Get(kType).AsString(); 398 std::string type = dict.Get(kType).AsString();
389
390 if (type == kJSViewportType && 399 if (type == kJSViewportType &&
391 dict.Get(pp::Var(kJSXOffset)).is_number() && 400 dict.Get(pp::Var(kJSXOffset)).is_number() &&
392 dict.Get(pp::Var(kJSYOffset)).is_number() && 401 dict.Get(pp::Var(kJSYOffset)).is_number() &&
393 dict.Get(pp::Var(kJSZoom)).is_number()) { 402 dict.Get(pp::Var(kJSZoom)).is_number() &&
403 dict.Get(pp::Var(kJSRender)).is_bool() &&
404 dict.Get(pp::Var(kJSPx)).is_number() &&
405 dict.Get(pp::Var(kJSPy)).is_number()) {
394 received_viewport_message_ = true; 406 received_viewport_message_ = true;
395 stop_scrolling_ = false; 407 stop_scrolling_ = false;
408 bool do_render = dict.Get(pp::Var(kJSRender)).AsBool();
396 double zoom = dict.Get(pp::Var(kJSZoom)).AsDouble(); 409 double zoom = dict.Get(pp::Var(kJSZoom)).AsDouble();
410 double zoom_delta = zoom / zoom_;
411
412 // Pinch vector is the panning caused due to change in pinch
413 // center between start and end of the gesture.
414 pp::Point pinch_vector =
415 pp::Point(dict.Get(kJSPinchVectorX).AsDouble() * zoom_delta,
416 dict.Get(kJSPinchVectorY).AsDouble() * zoom_delta);
397 pp::FloatPoint scroll_offset(dict.Get(pp::Var(kJSXOffset)).AsDouble(), 417 pp::FloatPoint scroll_offset(dict.Get(pp::Var(kJSXOffset)).AsDouble(),
398 dict.Get(pp::Var(kJSYOffset)).AsDouble()); 418 dict.Get(pp::Var(kJSYOffset)).AsDouble());
419 pp::Point pinch_center(dict.Get(pp::Var(kJSPx)).AsDouble(),
420 dict.Get(pp::Var(kJSPy)).AsDouble());
421 pp::Point scroll_delta(0,0);
422
423 // If the rendered document doesn't fill the display area we will
424 // use paint_offset to anchor the paint vertically into the same place. We
425 // use the scroll bars instead of the pinch vector to get the actual
426 // position on screen of the paint.
427 pp::Point paint_offset(0,0);
428
429 // Pinch start.
bokan 2016/04/22 15:45:35 Still need to change these to an enum as James sug
430 if (!do_render && do_render_) {
431 last_current_scroll_ = scroll_offset;
432 old_zoom_ = zoom;
433 initial_zoom_delta_ = zoom / zoom_;
434 was_smaller_ = false;
435 do_render_ = false;
436 return;
437 }
438
439 if (!do_render &&
440 plugin_size_.width() > GetDocumentPixelWidth() * zoom_delta) {
441 // We want to keep the paint in the middle but it must stay in the same
442 // position relative to the scroll bars.
443 paint_offset = pp::Point(0, (1 - zoom_delta) * pinch_center.y());
444 scroll_delta = pp::Point(
445 0, (scroll_offset.y() -
446 last_current_scroll_.y() * zoom_delta / initial_zoom_delta_));
447
448 pinch_vector = pp::Point();
449 old_zoom_ = zoom;
450 was_smaller_ = true;
451 } else {
452 if (was_smaller_) {
453 pinch_center = pp::Point((plugin_size_.width() / device_scale_) / 2,
454 (plugin_size_.height() / device_scale_) / 2);
455 paint_offset = pp::Point((1 - zoom / old_zoom_) * pinch_center.x(),
456 (1 - zoom_delta) * pinch_center.y());
457 pinch_vector = pp::Point(0, 0);
458 scroll_delta = pp::Point(
459 (scroll_offset.x() -
460 last_current_scroll_.x() * zoom_delta / initial_zoom_delta_),
461 (scroll_offset.y() -
462 last_current_scroll_.y() * zoom_delta / initial_zoom_delta_));
463 }
464 }
465
466 // While pinching.
467 if (!do_render) {
468 paint_manager_.SetTransform(zoom_delta, pinch_center,
469 pinch_vector + paint_offset + scroll_delta);
470 do_render_ = do_render;
471 return;
472 }
473
474 // On pinch end the scale is again 1.f and we request a render in the new
475 // position.
476 if (do_render && !do_render_) {
477 paint_manager_.SetTransform(1.f);
478 was_smaller_ = false;
479 }
399 480
400 // Bound the input parameters. 481 // Bound the input parameters.
401 zoom = std::max(kMinZoom, zoom); 482 zoom = std::max(kMinZoom, zoom);
402 SetZoom(zoom); 483 SetZoom(zoom);
484
485 do_render_ = do_render;
403 scroll_offset = BoundScrollOffsetToDocument(scroll_offset); 486 scroll_offset = BoundScrollOffsetToDocument(scroll_offset);
404 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_); 487 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_);
405 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_); 488 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_);
406 } else if (type == kJSGetPasswordCompleteType && 489 } else if (type == kJSGetPasswordCompleteType &&
407 dict.Get(pp::Var(kJSPassword)).is_string()) { 490 dict.Get(pp::Var(kJSPassword)).is_string()) {
408 if (password_callback_) { 491 if (password_callback_) {
409 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_; 492 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_;
410 password_callback_.reset(); 493 password_callback_.reset();
411 *callback.output() = dict.Get(pp::Var(kJSPassword)).pp_var(); 494 *callback.output() = dict.Get(pp::Var(kJSPassword)).pp_var();
412 callback.Run(PP_OK); 495 callback.Run(PP_OK);
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 float device_scale = view.GetDeviceScale(); 644 float device_scale = view.GetDeviceScale();
562 pp::Size view_device_size(view_rect.width() * device_scale, 645 pp::Size view_device_size(view_rect.width() * device_scale,
563 view_rect.height() * device_scale); 646 view_rect.height() * device_scale);
564 647
565 if (view_device_size != plugin_size_ || device_scale != device_scale_) { 648 if (view_device_size != plugin_size_ || device_scale != device_scale_) {
566 device_scale_ = device_scale; 649 device_scale_ = device_scale;
567 plugin_dip_size_ = view_rect.size(); 650 plugin_dip_size_ = view_rect.size();
568 plugin_size_ = view_device_size; 651 plugin_size_ = view_device_size;
569 652
570 paint_manager_.SetSize(view_device_size, device_scale_); 653 paint_manager_.SetSize(view_device_size, device_scale_);
571
572 pp::Size new_image_data_size = PaintManager::GetNewContextSize( 654 pp::Size new_image_data_size = PaintManager::GetNewContextSize(
573 image_data_.size(), 655 image_data_.size(),
574 plugin_size_); 656 plugin_size_);
575 if (new_image_data_size != image_data_.size()) { 657 if (new_image_data_size != image_data_.size()) {
576 image_data_ = pp::ImageData(this, 658 image_data_ = pp::ImageData(this,
577 PP_IMAGEDATAFORMAT_BGRA_PREMUL, 659 PP_IMAGEDATAFORMAT_BGRA_PREMUL,
578 new_image_data_size, 660 new_image_data_size,
579 false); 661 false);
580 first_paint_ = true; 662 first_paint_ = true;
581 } 663 }
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 DCHECK(plugin_size_.IsEmpty()); 772 DCHECK(plugin_size_.IsEmpty());
691 return; 773 return;
692 } 774 }
693 if (first_paint_) { 775 if (first_paint_) {
694 first_paint_ = false; 776 first_paint_ = false;
695 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size()); 777 pp::Rect rect = pp::Rect(pp::Point(), image_data_.size());
696 FillRect(rect, background_color_); 778 FillRect(rect, background_color_);
697 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true)); 779 ready->push_back(PaintManager::ReadyRect(rect, image_data_, true));
698 } 780 }
699 781
700 if (!received_viewport_message_) 782 if (!received_viewport_message_ || !do_render_)
701 return; 783 return;
702 784
703 engine_->PrePaint(); 785 engine_->PrePaint();
704 786
705 for (const auto& paint_rect : paint_rects) { 787 for (const auto& paint_rect : paint_rects) {
706 // Intersect with plugin area since there could be pending invalidates from 788 // Intersect with plugin area since there could be pending invalidates from
707 // when the plugin area was larger. 789 // when the plugin area was larger.
708 pp::Rect rect = 790 pp::Rect rect =
709 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_)); 791 paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_));
710 if (rect.IsEmpty()) 792 if (rect.IsEmpty())
(...skipping 725 matching lines...) Expand 10 before | Expand all | Expand 10 after
1436 const pp::FloatPoint& scroll_offset) { 1518 const pp::FloatPoint& scroll_offset) {
1437 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); 1519 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width();
1438 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f); 1520 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f);
1439 float min_y = -top_toolbar_height_; 1521 float min_y = -top_toolbar_height_;
1440 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); 1522 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height();
1441 float y = std::max(std::min(scroll_offset.y(), max_y), min_y); 1523 float y = std::max(std::min(scroll_offset.y(), max_y), min_y);
1442 return pp::FloatPoint(x, y); 1524 return pp::FloatPoint(x, y);
1443 } 1525 }
1444 1526
1445 } // namespace chrome_pdf 1527 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698