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

Side by Side Diff: cc/trees/layer_tree_host_impl.cc

Issue 986443003: Move viewport scrolling logic into separate class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 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 "cc/trees/layer_tree_host_impl.h" 5 #include "cc/trees/layer_tree_host_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 20 matching lines...) Expand all
31 #include "cc/input/page_scale_animation.h" 31 #include "cc/input/page_scale_animation.h"
32 #include "cc/input/scroll_elasticity_helper.h" 32 #include "cc/input/scroll_elasticity_helper.h"
33 #include "cc/input/top_controls_manager.h" 33 #include "cc/input/top_controls_manager.h"
34 #include "cc/layers/append_quads_data.h" 34 #include "cc/layers/append_quads_data.h"
35 #include "cc/layers/heads_up_display_layer_impl.h" 35 #include "cc/layers/heads_up_display_layer_impl.h"
36 #include "cc/layers/layer_impl.h" 36 #include "cc/layers/layer_impl.h"
37 #include "cc/layers/layer_iterator.h" 37 #include "cc/layers/layer_iterator.h"
38 #include "cc/layers/painted_scrollbar_layer_impl.h" 38 #include "cc/layers/painted_scrollbar_layer_impl.h"
39 #include "cc/layers/render_surface_impl.h" 39 #include "cc/layers/render_surface_impl.h"
40 #include "cc/layers/scrollbar_layer_impl_base.h" 40 #include "cc/layers/scrollbar_layer_impl_base.h"
41 #include "cc/layers/viewport.h"
41 #include "cc/output/compositor_frame_metadata.h" 42 #include "cc/output/compositor_frame_metadata.h"
42 #include "cc/output/copy_output_request.h" 43 #include "cc/output/copy_output_request.h"
43 #include "cc/output/delegating_renderer.h" 44 #include "cc/output/delegating_renderer.h"
44 #include "cc/output/gl_renderer.h" 45 #include "cc/output/gl_renderer.h"
45 #include "cc/output/software_renderer.h" 46 #include "cc/output/software_renderer.h"
46 #include "cc/quads/render_pass_draw_quad.h" 47 #include "cc/quads/render_pass_draw_quad.h"
47 #include "cc/quads/shared_quad_state.h" 48 #include "cc/quads/shared_quad_state.h"
48 #include "cc/quads/solid_color_draw_quad.h" 49 #include "cc/quads/solid_color_draw_quad.h"
49 #include "cc/quads/texture_draw_quad.h" 50 #include "cc/quads/texture_draw_quad.h"
50 #include "cc/resources/bitmap_tile_task_worker_pool.h" 51 #include "cc/resources/bitmap_tile_task_worker_pool.h"
(...skipping 2418 matching lines...) Expand 10 before | Expand all | Expand 10 after
2469 SetNeedsAnimate(); 2470 SetNeedsAnimate();
2470 return SCROLL_STARTED; 2471 return SCROLL_STARTED;
2471 } 2472 }
2472 } 2473 }
2473 ScrollEnd(); 2474 ScrollEnd();
2474 return scroll_status; 2475 return scroll_status;
2475 } 2476 }
2476 2477
2477 gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta( 2478 gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2478 LayerImpl* layer_impl, 2479 LayerImpl* layer_impl,
2479 float scale_from_viewport_to_screen_space,
2480 const gfx::PointF& viewport_point, 2480 const gfx::PointF& viewport_point,
2481 const gfx::Vector2dF& viewport_delta) { 2481 const gfx::Vector2dF& viewport_delta) {
2482 // Layers with non-invertible screen space transforms should not have passed 2482 // Layers with non-invertible screen space transforms should not have passed
2483 // the scroll hit test in the first place. 2483 // the scroll hit test in the first place.
2484 DCHECK(layer_impl->screen_space_transform().IsInvertible()); 2484 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2485 gfx::Transform inverse_screen_space_transform( 2485 gfx::Transform inverse_screen_space_transform(
2486 gfx::Transform::kSkipInitialization); 2486 gfx::Transform::kSkipInitialization);
2487 bool did_invert = layer_impl->screen_space_transform().GetInverse( 2487 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2488 &inverse_screen_space_transform); 2488 &inverse_screen_space_transform);
2489 // TODO(shawnsingh): With the advent of impl-side crolling for non-root 2489 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2490 // layers, we may need to explicitly handle uninvertible transforms here. 2490 // layers, we may need to explicitly handle uninvertible transforms here.
2491 DCHECK(did_invert); 2491 DCHECK(did_invert);
2492 2492
2493 float scale_from_viewport_to_screen_space = device_scale_factor_;
2493 gfx::PointF screen_space_point = 2494 gfx::PointF screen_space_point =
2494 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space); 2495 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
2495 2496
2496 gfx::Vector2dF screen_space_delta = viewport_delta; 2497 gfx::Vector2dF screen_space_delta = viewport_delta;
2497 screen_space_delta.Scale(scale_from_viewport_to_screen_space); 2498 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2498 2499
2499 // First project the scroll start and end points to local layer space to find 2500 // First project the scroll start and end points to local layer space to find
2500 // the scroll delta in layer coordinates. 2501 // the scroll delta in layer coordinates.
2501 bool start_clipped, end_clipped; 2502 bool start_clipped, end_clipped;
2502 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta; 2503 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
2544 &end_clipped); 2545 &end_clipped);
2545 DCHECK(!end_clipped); 2546 DCHECK(!end_clipped);
2546 if (end_clipped) 2547 if (end_clipped)
2547 return gfx::Vector2dF(); 2548 return gfx::Vector2dF();
2548 gfx::PointF actual_viewport_end_point = 2549 gfx::PointF actual_viewport_end_point =
2549 gfx::ScalePoint(actual_screen_space_end_point, 2550 gfx::ScalePoint(actual_screen_space_end_point,
2550 1.f / scale_from_viewport_to_screen_space); 2551 1.f / scale_from_viewport_to_screen_space);
2551 return actual_viewport_end_point - viewport_point; 2552 return actual_viewport_end_point - viewport_point;
2552 } 2553 }
2553 2554
2554 static gfx::Vector2dF ScrollLayerWithLocalDelta( 2555 gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithLocalDelta(
2555 LayerImpl* layer_impl, 2556 LayerImpl* layer_impl,
2556 const gfx::Vector2dF& local_delta, 2557 const gfx::Vector2dF& local_delta,
2557 float page_scale_factor) { 2558 float page_scale_factor) const {
2558 gfx::ScrollOffset previous_offset = layer_impl->CurrentScrollOffset(); 2559 gfx::ScrollOffset previous_offset = layer_impl->CurrentScrollOffset();
2559 gfx::Vector2dF delta = local_delta; 2560 gfx::Vector2dF delta = local_delta;
2560 delta.Scale(1.f / page_scale_factor); 2561 delta.Scale(1.f / page_scale_factor);
2561 layer_impl->ScrollBy(delta); 2562 layer_impl->ScrollBy(delta);
2562 gfx::ScrollOffset scrolled = 2563 gfx::ScrollOffset scrolled =
2563 layer_impl->CurrentScrollOffset() - previous_offset; 2564 layer_impl->CurrentScrollOffset() - previous_offset;
2564 return gfx::Vector2dF(scrolled.x(), scrolled.y()); 2565 return gfx::Vector2dF(scrolled.x(), scrolled.y());
2565 } 2566 }
2566 2567
2567 bool LayerTreeHostImpl::ShouldTopControlsConsumeScroll(
2568 const gfx::Vector2dF& scroll_delta) const {
2569 DCHECK(CurrentlyScrollingLayer());
2570
2571 if (!top_controls_manager_)
2572 return false;
2573
2574 // Always consume if it's in the direction to show the top controls.
2575 if (scroll_delta.y() < 0)
2576 return true;
2577
2578 if (active_tree()->TotalScrollOffset().y() <
2579 active_tree()->TotalMaxScrollOffset().y())
2580 return true;
2581
2582 return false;
2583 }
2584
2585 InputHandlerScrollResult LayerTreeHostImpl::ScrollBy( 2568 InputHandlerScrollResult LayerTreeHostImpl::ScrollBy(
2586 const gfx::Point& viewport_point, 2569 const gfx::Point& viewport_point,
2587 const gfx::Vector2dF& scroll_delta) { 2570 const gfx::Vector2dF& scroll_delta) {
2588 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy"); 2571 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2589 if (!CurrentlyScrollingLayer()) 2572 if (!CurrentlyScrollingLayer())
2590 return InputHandlerScrollResult(); 2573 return InputHandlerScrollResult();
2591 2574
2592 gfx::Vector2dF pending_delta = scroll_delta; 2575 gfx::Vector2dF pending_delta = scroll_delta;
2593 gfx::Vector2dF unused_root_delta; 2576 gfx::Vector2dF unused_root_delta;
2594 bool did_scroll_x = false; 2577 bool did_scroll_x = false;
2595 bool did_scroll_y = false; 2578 bool did_scroll_y = false;
2596 bool did_scroll_top_controls = false;
2597 2579
2598 bool consume_by_top_controls = ShouldTopControlsConsumeScroll(scroll_delta); 2580 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2599
2600 // There's an edge case where the outer viewport isn't scrollable when the
2601 // scroll starts, however, as the top controls show the outer viewport becomes
2602 // scrollable. Therefore, always try scrolling the outer viewport before the
2603 // inner.
2604 // TODO(bokan): Move the top controls logic out of the loop since the scroll
2605 // that causes the outer viewport to become scrollable will still be applied
2606 // to the inner viewport.
2607 LayerImpl* start_layer = CurrentlyScrollingLayer();
2608 if (start_layer == InnerViewportScrollLayer() && OuterViewportScrollLayer())
2609 start_layer = OuterViewportScrollLayer();
2610
2611 for (LayerImpl* layer_impl = start_layer;
2612 layer_impl; 2581 layer_impl;
2613 layer_impl = layer_impl->parent()) { 2582 layer_impl = layer_impl->parent()) {
2614 if (!layer_impl->scrollable()) 2583 if (!layer_impl->scrollable())
2615 continue; 2584 continue;
2616 2585
2617 if (layer_impl == InnerViewportScrollLayer() ||
2618 layer_impl == OuterViewportScrollLayer()) {
2619 if (consume_by_top_controls) {
2620 gfx::Vector2dF excess_delta =
2621 top_controls_manager_->ScrollBy(pending_delta);
2622 gfx::Vector2dF applied_delta = pending_delta - excess_delta;
2623 pending_delta = excess_delta;
2624 // Force updating of vertical adjust values if needed.
2625 if (applied_delta.y() != 0)
2626 did_scroll_top_controls = true;
2627 }
2628 // Track root layer deltas for reporting overscroll.
2629 if (layer_impl == InnerViewportScrollLayer())
2630 unused_root_delta = pending_delta;
2631 }
2632
2633 gfx::Vector2dF applied_delta; 2586 gfx::Vector2dF applied_delta;
2634 // Gesture events need to be transformed from viewport coordinates to local 2587 if (Viewport()->IsViewportScrollingLayer(layer_impl)) {
2635 // layer coordinates so that the scrolling contents exactly follow the 2588 Viewport::ScrollResult result = Viewport()->ScrollBy(pending_delta,
2636 // user's finger. In contrast, wheel events represent a fixed amount of 2589 viewport_point,
2637 // scrolling so we can just apply them directly, but the page scale factor 2590 wheel_scrolling_);
2638 // is applied to the scroll delta. 2591 applied_delta = result.applied_delta;
2639 if (!wheel_scrolling_) { 2592 unused_root_delta = result.unused_scroll_delta;
2640 float scale_from_viewport_to_screen_space = device_scale_factor_; 2593 } else if (!Viewport()->IsViewportLayer(layer_impl)) {
2641 applied_delta = 2594 // Gesture events need to be transformed from viewport coordinates to
2642 ScrollLayerWithViewportSpaceDelta(layer_impl, 2595 // local layer coordinates so that the scrolling contents exactly follow
2643 scale_from_viewport_to_screen_space, 2596 // the user's finger. In contrast, wheel events represent a fixed amount
2644 viewport_point, pending_delta); 2597 // of scrolling so we can just apply them directly, but the page scale
2645 } else { 2598 // factor is applied to the scroll delta.
2646 applied_delta = ScrollLayerWithLocalDelta( 2599 if (!wheel_scrolling_) {
2647 layer_impl, pending_delta, active_tree_->current_page_scale_factor()); 2600 applied_delta =
2648 } 2601 ScrollLayerWithViewportSpaceDelta(layer_impl,
2649 2602 viewport_point,
2650 const float kEpsilon = 0.1f; 2603 pending_delta);
2651 if (layer_impl == InnerViewportScrollLayer()) { 2604 } else {
2652 unused_root_delta.Subtract(applied_delta); 2605 applied_delta = ScrollLayerWithLocalDelta(
2653 if (std::abs(unused_root_delta.x()) < kEpsilon) 2606 layer_impl,
2654 unused_root_delta.set_x(0.0f); 2607 pending_delta,
2655 if (std::abs(unused_root_delta.y()) < kEpsilon) 2608 active_tree_->current_page_scale_factor());
2656 unused_root_delta.set_y(0.0f);
2657 // Disable overscroll on axes which is impossible to scroll.
2658 if (settings_.report_overscroll_only_for_scrollable_axes) {
2659 if (std::abs(active_tree_->TotalMaxScrollOffset().x()) <= kEpsilon ||
2660 !layer_impl->user_scrollable_horizontal())
2661 unused_root_delta.set_x(0.0f);
2662 if (std::abs(active_tree_->TotalMaxScrollOffset().y()) <= kEpsilon ||
2663 !layer_impl->user_scrollable_vertical())
2664 unused_root_delta.set_y(0.0f);
2665 } 2609 }
2666 } 2610 }
2667 2611
2668 // Scrolls should bubble perfectly between the outer and inner viewports.
2669 bool allow_unrestricted_bubbling_for_current_layer =
2670 layer_impl == OuterViewportScrollLayer();
2671 bool allow_bubbling_for_current_layer =
2672 allow_unrestricted_bubbling_for_current_layer || should_bubble_scrolls_;
2673
2674 // If the layer wasn't able to move, try the next one in the hierarchy. 2612 // If the layer wasn't able to move, try the next one in the hierarchy.
2613 const float kEpsilon = 0.1f;
2675 bool did_move_layer_x = std::abs(applied_delta.x()) > kEpsilon; 2614 bool did_move_layer_x = std::abs(applied_delta.x()) > kEpsilon;
2676 bool did_move_layer_y = std::abs(applied_delta.y()) > kEpsilon; 2615 bool did_move_layer_y = std::abs(applied_delta.y()) > kEpsilon;
2677 did_scroll_x |= did_move_layer_x; 2616 did_scroll_x |= did_move_layer_x;
2678 did_scroll_y |= did_move_layer_y; 2617 did_scroll_y |= did_move_layer_y;
2679 if (!did_move_layer_x && !did_move_layer_y) { 2618
2680 if (allow_bubbling_for_current_layer || !did_lock_scrolling_layer_) 2619 if (did_move_layer_x || did_move_layer_y) {
2681 continue; 2620 did_lock_scrolling_layer_ = true;
2682 else 2621
2622 // When scrolls are allowed to bubble, it's important that the original
2623 // scrolling layer be preserved. This ensures that, after a scroll
2624 // bubbles, the user can reverse scroll directions and immediately resume
2625 // scrolling the original layer that scrolled.
2626 if (!should_bubble_scrolls_) {
2627 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2683 break; 2628 break;
2684 } 2629 }
2685 2630
2686 did_lock_scrolling_layer_ = true;
2687
2688 // When scrolls are allowed to bubble, it's important that the original
2689 // scrolling layer be preserved. This ensures that, after a scroll bubbles,
2690 // the user can reverse scroll directions and immediately resume scrolling
2691 // the original layer that scrolled.
2692 if (!should_bubble_scrolls_)
2693 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2694
2695 if (!allow_bubbling_for_current_layer)
2696 break;
2697
2698 if (allow_unrestricted_bubbling_for_current_layer) {
2699 pending_delta -= applied_delta;
2700 } else {
2701 // If the applied delta is within 45 degrees of the input delta, bail out 2631 // If the applied delta is within 45 degrees of the input delta, bail out
2702 // to make it easier to scroll just one layer in one direction without 2632 // to make it easier to scroll just one layer in one direction without
2703 // affecting any of its parents. 2633 // affecting any of its parents.
2704 float angle_threshold = 45; 2634 float angle_threshold = 45;
2705 if (MathUtil::SmallestAngleBetweenVectors(applied_delta, pending_delta) < 2635 if (MathUtil::SmallestAngleBetweenVectors(applied_delta, pending_delta) <
2706 angle_threshold) { 2636 angle_threshold)
2707 pending_delta = gfx::Vector2dF();
2708 break; 2637 break;
2709 }
2710 2638
2711 // Allow further movement only on an axis perpendicular to the direction 2639 // Allow further movement only on an axis perpendicular to the direction
2712 // in which the layer moved. 2640 // in which the layer moved.
2713 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x()); 2641 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
2714 pending_delta = 2642 pending_delta =
2715 MathUtil::ProjectVector(pending_delta, perpendicular_axis); 2643 MathUtil::ProjectVector(pending_delta, perpendicular_axis);
2644
2645 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
2646 break;
2716 } 2647 }
2717
2718 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
2719 break;
2720 } 2648 }
2721 2649
2722 bool did_scroll_content = did_scroll_x || did_scroll_y; 2650 bool did_scroll_content = did_scroll_x || did_scroll_y;
2723 if (did_scroll_content) { 2651 if (did_scroll_content) {
2724 // If we are scrolling with an active scroll handler, forward latency 2652 // If we are scrolling with an active scroll handler, forward latency
2725 // tracking information to the main thread so the delay introduced by the 2653 // tracking information to the main thread so the delay introduced by the
2726 // handler is accounted for. 2654 // handler is accounted for.
2727 if (scroll_affects_scroll_handler()) 2655 if (scroll_affects_scroll_handler())
2728 NotifySwapPromiseMonitorsOfForwardingToMainThread(); 2656 NotifySwapPromiseMonitorsOfForwardingToMainThread();
2729 client_->SetNeedsCommitOnImplThread(); 2657 client_->SetNeedsCommitOnImplThread();
2730 SetNeedsRedraw(); 2658 SetNeedsRedraw();
2731 client_->RenewTreePriority(); 2659 client_->RenewTreePriority();
2732 } 2660 }
2733 2661
2734 // Scrolling along an axis resets accumulated root overscroll for that axis. 2662 // Scrolling along an axis resets accumulated root overscroll for that axis.
2735 if (did_scroll_x) 2663 if (did_scroll_x)
2736 accumulated_root_overscroll_.set_x(0); 2664 accumulated_root_overscroll_.set_x(0);
2737 if (did_scroll_y) 2665 if (did_scroll_y)
2738 accumulated_root_overscroll_.set_y(0); 2666 accumulated_root_overscroll_.set_y(0);
2739 accumulated_root_overscroll_ += unused_root_delta; 2667 accumulated_root_overscroll_ += unused_root_delta;
2740 2668
2741 InputHandlerScrollResult scroll_result; 2669 InputHandlerScrollResult scroll_result;
2742 scroll_result.did_scroll = did_scroll_content || did_scroll_top_controls; 2670 scroll_result.did_scroll = did_scroll_content;
2743 scroll_result.did_overscroll_root = !unused_root_delta.IsZero(); 2671 scroll_result.did_overscroll_root = !unused_root_delta.IsZero();
2744 scroll_result.accumulated_root_overscroll = accumulated_root_overscroll_; 2672 scroll_result.accumulated_root_overscroll = accumulated_root_overscroll_;
2745 scroll_result.unused_scroll_delta = unused_root_delta; 2673 scroll_result.unused_scroll_delta = unused_root_delta;
2746 return scroll_result; 2674 return scroll_result;
2747 } 2675 }
2748 2676
2749 // This implements scrolling by page as described here: 2677 // This implements scrolling by page as described here:
2750 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).asp x#_win32_The_Mouse_Wheel 2678 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).asp x#_win32_The_Mouse_Wheel
2751 // for events with WHEEL_PAGESCROLL set. 2679 // for events with WHEEL_PAGESCROLL set.
2752 bool LayerTreeHostImpl::ScrollVerticallyByPage(const gfx::Point& viewport_point, 2680 bool LayerTreeHostImpl::ScrollVerticallyByPage(const gfx::Point& viewport_point,
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after
3461 for (; it != swap_promise_monitor_.end(); it++) 3389 for (; it != swap_promise_monitor_.end(); it++)
3462 (*it)->OnSetNeedsRedrawOnImpl(); 3390 (*it)->OnSetNeedsRedrawOnImpl();
3463 } 3391 }
3464 3392
3465 void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfForwardingToMainThread() { 3393 void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfForwardingToMainThread() {
3466 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin(); 3394 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3467 for (; it != swap_promise_monitor_.end(); it++) 3395 for (; it != swap_promise_monitor_.end(); it++)
3468 (*it)->OnForwardScrollUpdateToMainThreadOnImpl(); 3396 (*it)->OnForwardScrollUpdateToMainThreadOnImpl();
3469 } 3397 }
3470 3398
3399 Viewport* LayerTreeHostImpl::Viewport() {
3400 return active_tree_->viewport();
3401 }
3402
3471 } // namespace cc 3403 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698