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

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

Issue 2767213003: First Implementation of Snapped Points
Patch Set: Rebase and format Created 3 years, 6 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
« no previous file with comments | « cc/trees/layer_tree_host_impl.h ('k') | third_party/WebKit/Source/core/BUILD.gn » ('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 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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 DrawMode LayerTreeHostImpl::GetDrawMode() const { 727 DrawMode LayerTreeHostImpl::GetDrawMode() const {
728 if (resourceless_software_draw_) { 728 if (resourceless_software_draw_) {
729 return DRAW_MODE_RESOURCELESS_SOFTWARE; 729 return DRAW_MODE_RESOURCELESS_SOFTWARE;
730 } else if (compositor_frame_sink_->context_provider()) { 730 } else if (compositor_frame_sink_->context_provider()) {
731 return DRAW_MODE_HARDWARE; 731 return DRAW_MODE_HARDWARE;
732 } else { 732 } else {
733 return DRAW_MODE_SOFTWARE; 733 return DRAW_MODE_SOFTWARE;
734 } 734 }
735 } 735 }
736 736
737 static double FindClosestInSortedArray(double start,
738 double target,
739 const std::vector<SnapPoint>& array) {
740 int size = array.size();
741 if (size <= 0)
742 return target;
743
744 int large_area_cnt = 0;
745
746 if (target >= start) {
747 // First, find the index of the snap point that's closest
748 // to start point.
749 double snap_point = array[0].offset;
750 double delta = target - snap_point;
751 int i = 0;
752 for (; i < size; ++i) {
753 if (array[i].offset >= start)
754 break;
755 if (array[i].is_start)
756 ++large_area_cnt;
757 if (array[i].is_end)
758 --large_area_cnt;
759 snap_point = array[i].offset;
760 delta = target - snap_point;
761 }
762 if (i >= size)
763 return target;
764
765 for (int j = i; j < size; ++j) {
766 if (array[j].offset >= target) {
767 if (large_area_cnt > 0)
768 return target;
769 if (array[j].offset - target < delta)
770 return array[j].offset;
771 return snap_point;
772 }
773 if (array[j].must_snap && start != target)
774 return array[j].offset;
775 if (array[j].is_start)
776 ++large_area_cnt;
777 if (array[j].is_end)
778 --large_area_cnt;
779 snap_point = array[j].offset;
780 delta = target - snap_point;
781 }
782 }
783
784 if (target < start) {
785 double snap_point = array[size - 1].offset;
786 double delta = snap_point - target;
787 int i = size - 1;
788 for (; i >= 0; --i) {
789 if (array[i].offset <= start)
790 break;
791 if (array[i].is_end)
792 ++large_area_cnt;
793 if (array[i].is_start)
794 --large_area_cnt;
795 snap_point = array[i].offset;
796 delta = snap_point - target;
797 }
798 if (i < 0)
799 return target;
800
801 for (int j = i; j >= 0; --j) {
802 if (array[j].offset <= target) {
803 if (large_area_cnt > 0)
804 return target;
805 if (target - array[j].offset < delta)
806 return array[j].offset;
807 return snap_point;
808 }
809 if (array[j].must_snap)
810 return array[j].offset;
811 if (array[j].is_end)
812 ++large_area_cnt;
813 if (array[j].is_start)
814 --large_area_cnt;
815 snap_point = array[j].offset;
816 delta = snap_point - target;
817 }
818 }
819
820 return target;
821 }
822
737 static void AppendQuadsToFillScreen( 823 static void AppendQuadsToFillScreen(
738 const gfx::Rect& root_scroll_layer_rect, 824 const gfx::Rect& root_scroll_layer_rect,
739 RenderPass* target_render_pass, 825 RenderPass* target_render_pass,
740 const RenderSurfaceImpl* root_render_surface, 826 const RenderSurfaceImpl* root_render_surface,
741 SkColor screen_background_color, 827 SkColor screen_background_color,
742 const Region& fill_region) { 828 const Region& fill_region) {
743 if (!root_render_surface || !SkColorGetA(screen_background_color)) 829 if (!root_render_surface || !SkColorGetA(screen_background_color))
744 return; 830 return;
745 if (fill_region.IsEmpty()) 831 if (fill_region.IsEmpty())
746 return; 832 return;
(...skipping 2466 matching lines...) Expand 10 before | Expand all | Expand 10 after
3213 scroll_node, gfx::PointF(viewport_point), delta, scroll_tree); 3299 scroll_node, gfx::PointF(viewport_point), delta, scroll_tree);
3214 } 3300 }
3215 float scale_factor = active_tree()->current_page_scale_factor(); 3301 float scale_factor = active_tree()->current_page_scale_factor();
3216 return ScrollNodeWithLocalDelta(scroll_node, delta, scale_factor, 3302 return ScrollNodeWithLocalDelta(scroll_node, delta, scale_factor,
3217 active_tree()); 3303 active_tree());
3218 } 3304 }
3219 3305
3220 void LayerTreeHostImpl::ApplyScroll(ScrollNode* scroll_node, 3306 void LayerTreeHostImpl::ApplyScroll(ScrollNode* scroll_node,
3221 ScrollState* scroll_state) { 3307 ScrollState* scroll_state) {
3222 DCHECK(scroll_node && scroll_state); 3308 DCHECK(scroll_node && scroll_state);
3309
3223 gfx::Point viewport_point(scroll_state->position_x(), 3310 gfx::Point viewport_point(scroll_state->position_x(),
3224 scroll_state->position_y()); 3311 scroll_state->position_y());
3225 const gfx::Vector2dF delta(scroll_state->delta_x(), scroll_state->delta_y()); 3312 const gfx::Vector2dF delta(scroll_state->delta_x(), scroll_state->delta_y());
3226 gfx::Vector2dF applied_delta; 3313 gfx::Vector2dF applied_delta;
3227 gfx::Vector2dF delta_applied_to_content; 3314 gfx::Vector2dF delta_applied_to_content;
3228 // TODO(tdresser): Use a more rational epsilon. See crbug.com/510550 for 3315 // TODO(tdresser): Use a more rational epsilon. See crbug.com/510550 for
3229 // details. 3316 // details.
3230 const float kEpsilon = 0.1f; 3317 const float kEpsilon = 0.1f;
3231 3318
3232 bool scrolls_main_viewport_scroll_layer = 3319 bool scrolls_main_viewport_scroll_layer =
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
3283 } 3370 }
3284 3371
3285 scroll_state->set_caused_scroll( 3372 scroll_state->set_caused_scroll(
3286 std::abs(delta_applied_to_content.x()) > kEpsilon, 3373 std::abs(delta_applied_to_content.x()) > kEpsilon,
3287 std::abs(delta_applied_to_content.y()) > kEpsilon); 3374 std::abs(delta_applied_to_content.y()) > kEpsilon);
3288 scroll_state->ConsumeDelta(applied_delta.x(), applied_delta.y()); 3375 scroll_state->ConsumeDelta(applied_delta.x(), applied_delta.y());
3289 3376
3290 scroll_state->set_current_native_scrolling_node(scroll_node); 3377 scroll_state->set_current_native_scrolling_node(scroll_node);
3291 } 3378 }
3292 3379
3380 void LayerTreeHostImpl::FindSnappedOffset(
3381 gfx::Vector2dF* snapped_offset,
3382 const gfx::Vector2dF& original_offset) {
3383 ScrollTree& scroll_tree = active_tree_->property_trees()->scroll_tree;
3384 ScrollNode* scroll_node = scroll_tree.CurrentlyScrollingNode();
3385
3386 LayerImpl* layer =
3387 active_tree()->FindActiveTreeLayerById(scroll_node->owning_layer_id);
3388 float scale_factor = active_tree()->current_page_scale_factor();
3389 gfx::Vector2dF scaled = original_offset;
3390 scaled.Scale(1.f / scale_factor);
3391
3392 gfx::ScrollOffset current_offset =
3393 scroll_tree.current_scroll_offset(scroll_node->owning_layer_id);
3394 gfx::ScrollOffset expected_offset =
3395 current_offset - gfx::ScrollOffset(scaled);
3396 std::vector<SnapPoint> horizontal = layer->snap_offsets_horizontal();
3397 std::vector<SnapPoint> vertical = layer->snap_offsets_vertical();
3398 double snapped_x = FindClosestInSortedArray(current_offset.x(),
3399 expected_offset.x(), horizontal);
3400 double snapped_y = FindClosestInSortedArray(current_offset.y(),
3401 expected_offset.y(), vertical);
3402 expected_offset.set_x(snapped_x);
3403 expected_offset.set_y(snapped_y);
3404 expected_offset = current_offset - expected_offset;
3405 snapped_offset->set_x(expected_offset.x());
3406 snapped_offset->set_y(expected_offset.y());
3407 snapped_offset->Scale(scale_factor);
3408 }
3409
3293 void LayerTreeHostImpl::DistributeScrollDelta(ScrollState* scroll_state) { 3410 void LayerTreeHostImpl::DistributeScrollDelta(ScrollState* scroll_state) {
3294 // TODO(majidvp): in Blink we compute scroll chain only at scroll begin which 3411 // TODO(majidvp): in Blink we compute scroll chain only at scroll begin which
3295 // is not the case here. We eventually want to have the same behaviour on both 3412 // is not the case here. We eventually want to have the same behaviour on both
3296 // sides but it may become a non issue if we get rid of scroll chaining (see 3413 // sides but it may become a non issue if we get rid of scroll chaining (see
3297 // crbug.com/526462) 3414 // crbug.com/526462)
3298 std::list<ScrollNode*> current_scroll_chain; 3415 std::list<ScrollNode*> current_scroll_chain;
3299 ScrollTree& scroll_tree = active_tree_->property_trees()->scroll_tree; 3416 ScrollTree& scroll_tree = active_tree_->property_trees()->scroll_tree;
3300 ScrollNode* scroll_node = scroll_tree.CurrentlyScrollingNode(); 3417 ScrollNode* scroll_node = scroll_tree.CurrentlyScrollingNode();
3301 ScrollNode* viewport_scroll_node = OuterViewportScrollNode(); 3418 ScrollNode* viewport_scroll_node = OuterViewportScrollNode();
3302 if (scroll_node) { 3419 if (scroll_node) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
3407 // input handler. 3524 // input handler.
3408 UpdateRootLayerStateForSynchronousInputHandler(); 3525 UpdateRootLayerStateForSynchronousInputHandler();
3409 } 3526 }
3410 3527
3411 // Update compositor worker mutations which may respond to scrolling. 3528 // Update compositor worker mutations which may respond to scrolling.
3412 Mutate(CurrentBeginFrameArgs().frame_time); 3529 Mutate(CurrentBeginFrameArgs().frame_time);
3413 3530
3414 return scroll_result; 3531 return scroll_result;
3415 } 3532 }
3416 3533
3534 void LayerTreeHostImpl::SnapAfterGestureScroll(const gfx::Point& scroll_point) {
3535 gfx::Vector2dF snap_offset, original_offset;
3536 FindSnappedOffset(&snap_offset, original_offset);
3537 ScrollTree& scroll_tree = active_tree_->property_trees()->scroll_tree;
3538 ScrollNode* scroll_node = scroll_tree.CurrentlyScrollingNode();
3539 ScrollAnimationCreate(scroll_node, -snap_offset, base::TimeDelta());
3540 }
3541
3417 void LayerTreeHostImpl::RequestUpdateForSynchronousInputHandler() { 3542 void LayerTreeHostImpl::RequestUpdateForSynchronousInputHandler() {
3418 UpdateRootLayerStateForSynchronousInputHandler(); 3543 UpdateRootLayerStateForSynchronousInputHandler();
3419 } 3544 }
3420 3545
3421 void LayerTreeHostImpl::SetSynchronousInputHandlerRootScrollOffset( 3546 void LayerTreeHostImpl::SetSynchronousInputHandlerRootScrollOffset(
3422 const gfx::ScrollOffset& root_offset) { 3547 const gfx::ScrollOffset& root_offset) {
3423 bool changed = active_tree_->DistributeRootScrollOffset(root_offset); 3548 bool changed = active_tree_->DistributeRootScrollOffset(root_offset);
3424 if (!changed) 3549 if (!changed)
3425 return; 3550 return;
3426 3551
(...skipping 872 matching lines...) Expand 10 before | Expand all | Expand 10 after
4299 4424
4300 void LayerTreeHostImpl::ShowScrollbarsForImplScroll(ElementId element_id) { 4425 void LayerTreeHostImpl::ShowScrollbarsForImplScroll(ElementId element_id) {
4301 if (!element_id) 4426 if (!element_id)
4302 return; 4427 return;
4303 if (ScrollbarAnimationController* animation_controller = 4428 if (ScrollbarAnimationController* animation_controller =
4304 ScrollbarAnimationControllerForElementId(element_id)) 4429 ScrollbarAnimationControllerForElementId(element_id))
4305 animation_controller->DidScrollUpdate(); 4430 animation_controller->DidScrollUpdate();
4306 } 4431 }
4307 4432
4308 } // namespace cc 4433 } // namespace cc
OLDNEW
« no previous file with comments | « cc/trees/layer_tree_host_impl.h ('k') | third_party/WebKit/Source/core/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698