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

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

Issue 1902893002: cc: Move calculation of content rect to RenderSurfaceImpl class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@targetid
Patch Set: 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 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_common.h" 5 #include "cc/trees/layer_tree_host_common.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <set> 10 #include <set>
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 layer_transforms_should_scale_layer_contents = true; 96 layer_transforms_should_scale_layer_contents = true;
97 } 97 }
98 }; 98 };
99 99
100 class LayerTreeHostCommonScalingTest : public LayerTreeHostCommonTest { 100 class LayerTreeHostCommonScalingTest : public LayerTreeHostCommonTest {
101 public: 101 public:
102 LayerTreeHostCommonScalingTest() 102 LayerTreeHostCommonScalingTest()
103 : LayerTreeHostCommonTest(LayerTreeSettingsScaleContent()) {} 103 : LayerTreeHostCommonTest(LayerTreeSettingsScaleContent()) {}
104 }; 104 };
105 105
106 class LayerTreeHostCommonDrawRectsTest : public LayerTreeHostCommonTest {
107 public:
108 LayerTreeHostCommonDrawRectsTest() : LayerTreeHostCommonTest() {}
109
110 LayerImpl* TestVisibleRectAndDrawableContentRect(
111 const gfx::Rect& target_rect,
112 const gfx::Transform& layer_transform,
113 const gfx::Rect& layer_rect) {
114 LayerImpl* root = root_layer();
115 LayerImpl* target = AddChild<LayerImpl>(root);
116 LayerImpl* drawing_layer = AddChild<LayerImpl>(target);
117
118 root->SetDrawsContent(true);
119 target->SetDrawsContent(true);
120 target->SetMasksToBounds(true);
121 drawing_layer->SetDrawsContent(true);
122
123 gfx::Transform identity;
124
125 SetLayerPropertiesForTesting(root, identity, gfx::Point3F(), gfx::PointF(),
126 gfx::Size(500, 500), true, false, true);
127 SetLayerPropertiesForTesting(target, identity, gfx::Point3F(),
128 gfx::PointF(target_rect.origin()),
129 target_rect.size(), true, false, true);
130 SetLayerPropertiesForTesting(drawing_layer, layer_transform, gfx::Point3F(),
131 gfx::PointF(layer_rect.origin()),
132 layer_rect.size(), true, false, false);
133
134 host_impl()->active_tree()->property_trees()->needs_rebuild = true;
135 ExecuteCalculateDrawProperties(root);
136
137 return drawing_layer;
138 }
139 };
140
106 TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) { 141 TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
107 // Sanity check: For layers positioned at zero, with zero size, 142 // Sanity check: For layers positioned at zero, with zero size,
108 // and with identity transforms, then the draw transform, 143 // and with identity transforms, then the draw transform,
109 // screen space transform, and the hierarchy passed on to children 144 // screen space transform, and the hierarchy passed on to children
110 // layers should also be identity transforms. 145 // layers should also be identity transforms.
111 146
112 LayerImpl* parent = root_layer(); 147 LayerImpl* parent = root_layer();
113 LayerImpl* child = AddChild<LayerImpl>(parent); 148 LayerImpl* child = AddChild<LayerImpl>(parent);
114 LayerImpl* grand_child = AddChild<LayerImpl>(child); 149 LayerImpl* grand_child = AddChild<LayerImpl>(child);
115 150
(...skipping 2570 matching lines...) Expand 10 before | Expand all | Expand 10 after
2686 2721
2687 EXPECT_FALSE(parent->screen_space_transform_is_animating()); 2722 EXPECT_FALSE(parent->screen_space_transform_is_animating());
2688 EXPECT_FALSE(child->screen_space_transform_is_animating()); 2723 EXPECT_FALSE(child->screen_space_transform_is_animating());
2689 2724
2690 EXPECT_FALSE(grand_child->TransformIsAnimating()); 2725 EXPECT_FALSE(grand_child->TransformIsAnimating());
2691 EXPECT_TRUE(grand_child->HasPotentiallyRunningTransformAnimation()); 2726 EXPECT_TRUE(grand_child->HasPotentiallyRunningTransformAnimation());
2692 EXPECT_TRUE(grand_child->screen_space_transform_is_animating()); 2727 EXPECT_TRUE(grand_child->screen_space_transform_is_animating());
2693 EXPECT_TRUE(great_grand_child->screen_space_transform_is_animating()); 2728 EXPECT_TRUE(great_grand_child->screen_space_transform_is_animating());
2694 } 2729 }
2695 2730
2696 TEST_F(LayerTreeHostCommonTest, VisibleRectForIdentityTransform) { 2731 TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsForIdentityTransform) {
2697 // Test the calculateVisibleRect() function works correctly for identity 2732 // Test visible layer rect and drawable content rect are calculated correctly
2698 // transforms. 2733 // correctly for identity transforms.
2699 2734
2700 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100); 2735 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2701 gfx::Transform layer_to_surface_transform; 2736 gfx::Transform layer_to_surface_transform;
2702 2737
2703 // Case 1: Layer is contained within the surface. 2738 // Case 1: Layer is contained within the surface.
2704 gfx::Rect layer_content_rect = gfx::Rect(10, 10, 30, 30); 2739 gfx::Rect layer_content_rect = gfx::Rect(10, 10, 30, 30);
2705 gfx::Rect expected = gfx::Rect(10, 10, 30, 30); 2740 gfx::Rect expected_visible_layer_rect = gfx::Rect(30, 30);
2706 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect( 2741 gfx::Rect expected_drawable_content_rect = gfx::Rect(10, 10, 30, 30);
2707 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2742 LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
2708 EXPECT_EQ(expected, actual); 2743 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2744 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2745 EXPECT_EQ(expected_drawable_content_rect,
2746 drawing_layer->drawable_content_rect());
2709 2747
2710 // Case 2: Layer is outside the surface rect. 2748 // Case 2: Layer is outside the surface rect.
2711 layer_content_rect = gfx::Rect(120, 120, 30, 30); 2749 layer_content_rect = gfx::Rect(120, 120, 30, 30);
2712 actual = LayerTreeHostCommon::CalculateVisibleRect( 2750 expected_visible_layer_rect = gfx::Rect();
2713 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2751 expected_drawable_content_rect = gfx::Rect();
2714 EXPECT_TRUE(actual.IsEmpty()); 2752 drawing_layer = TestVisibleRectAndDrawableContentRect(
2753 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2754 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2755 EXPECT_EQ(expected_drawable_content_rect,
2756 drawing_layer->drawable_content_rect());
2715 2757
2716 // Case 3: Layer is partially overlapping the surface rect. 2758 // Case 3: Layer is partially overlapping the surface rect.
2717 layer_content_rect = gfx::Rect(80, 80, 30, 30); 2759 layer_content_rect = gfx::Rect(80, 80, 30, 30);
2718 expected = gfx::Rect(80, 80, 20, 20); 2760 expected_visible_layer_rect = gfx::Rect(20, 20);
2719 actual = LayerTreeHostCommon::CalculateVisibleRect( 2761 expected_drawable_content_rect = gfx::Rect(80, 80, 20, 20);
2720 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2762 drawing_layer = TestVisibleRectAndDrawableContentRect(
2721 EXPECT_EQ(expected, actual); 2763 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2764 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2765 EXPECT_EQ(expected_drawable_content_rect,
2766 drawing_layer->drawable_content_rect());
2722 } 2767 }
2723 2768
2724 TEST_F(LayerTreeHostCommonTest, VisibleRectForTranslations) { 2769 TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor2DRotations) {
2725 // Test the calculateVisibleRect() function works correctly for scaling 2770 // Test visible layer rect and drawable content rect are calculated correctly
2726 // transforms. 2771 // for rotations about z-axis (i.e. 2D rotations).
2727 2772
2728 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100); 2773 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2729 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30); 2774 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2730 gfx::Transform layer_to_surface_transform;
2731
2732 // Case 1: Layer is contained within the surface.
2733 layer_to_surface_transform.MakeIdentity();
2734 layer_to_surface_transform.Translate(10.0, 10.0);
2735 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2736 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2737 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2738 EXPECT_EQ(expected, actual);
2739
2740 // Case 2: Layer is outside the surface rect.
2741 layer_to_surface_transform.MakeIdentity();
2742 layer_to_surface_transform.Translate(120.0, 120.0);
2743 actual = LayerTreeHostCommon::CalculateVisibleRect(
2744 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2745 EXPECT_TRUE(actual.IsEmpty());
2746
2747 // Case 3: Layer is partially overlapping the surface rect.
2748 layer_to_surface_transform.MakeIdentity();
2749 layer_to_surface_transform.Translate(80.0, 80.0);
2750 expected = gfx::Rect(0, 0, 20, 20);
2751 actual = LayerTreeHostCommon::CalculateVisibleRect(
2752 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2753 EXPECT_EQ(expected, actual);
2754 }
2755
2756 TEST_F(LayerTreeHostCommonTest, VisibleRectFor2DRotations) {
2757 // Test the calculateVisibleRect() function works correctly for rotations
2758 // about z-axis (i.e. 2D rotations). Remember that calculateVisibleRect()
2759 // should return the g in the layer's space.
2760
2761 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2762 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2763 gfx::Transform layer_to_surface_transform; 2775 gfx::Transform layer_to_surface_transform;
2764 2776
2765 // Case 1: Layer is contained within the surface. 2777 // Case 1: Layer is contained within the surface.
2766 layer_to_surface_transform.MakeIdentity(); 2778 layer_to_surface_transform.MakeIdentity();
2767 layer_to_surface_transform.Translate(50.0, 50.0); 2779 layer_to_surface_transform.Translate(50.0, 50.0);
2768 layer_to_surface_transform.Rotate(45.0); 2780 layer_to_surface_transform.Rotate(45.0);
2769 gfx::Rect expected = gfx::Rect(0, 0, 30, 30); 2781 gfx::Rect expected_visible_layer_rect = gfx::Rect(30, 30);
2770 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect( 2782 gfx::Rect expected_drawable_content_rect = gfx::Rect(28, 50, 44, 43);
2771 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2783 LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
2772 EXPECT_EQ(expected, actual); 2784 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2785 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2786 EXPECT_EQ(expected_drawable_content_rect,
2787 drawing_layer->drawable_content_rect());
2773 2788
2774 // Case 2: Layer is outside the surface rect. 2789 // Case 2: Layer is outside the surface rect.
2775 layer_to_surface_transform.MakeIdentity(); 2790 layer_to_surface_transform.MakeIdentity();
2776 layer_to_surface_transform.Translate(-50.0, 0.0); 2791 layer_to_surface_transform.Translate(-50.0, 0.0);
2777 layer_to_surface_transform.Rotate(45.0); 2792 layer_to_surface_transform.Rotate(45.0);
2778 actual = LayerTreeHostCommon::CalculateVisibleRect( 2793 expected_visible_layer_rect = gfx::Rect();
2779 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2794 expected_drawable_content_rect = gfx::Rect();
2780 EXPECT_TRUE(actual.IsEmpty()); 2795 drawing_layer = TestVisibleRectAndDrawableContentRect(
2796 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2797 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2798 EXPECT_EQ(expected_drawable_content_rect,
2799 drawing_layer->drawable_content_rect());
2781 2800
2782 // Case 3: The layer is rotated about its top-left corner. In surface space, 2801 // Case 3: The layer is rotated about its top-left corner. In surface space,
2783 // the layer is oriented diagonally, with the left half outside of the render 2802 // the layer is oriented diagonally, with the left half outside of the render
2784 // surface. In this case, the g should still be the entire layer 2803 // surface. In this case, the g should still be the entire layer
2785 // (remember the g is computed in layer space); both the top-left 2804 // (remember the g is computed in layer space); both the top-left
2786 // and bottom-right corners of the layer are still visible. 2805 // and bottom-right corners of the layer are still visible.
2787 layer_to_surface_transform.MakeIdentity(); 2806 layer_to_surface_transform.MakeIdentity();
2788 layer_to_surface_transform.Rotate(45.0); 2807 layer_to_surface_transform.Rotate(45.0);
2789 expected = gfx::Rect(0, 0, 30, 30); 2808 expected_visible_layer_rect = gfx::Rect(30, 30);
2790 actual = LayerTreeHostCommon::CalculateVisibleRect( 2809 expected_drawable_content_rect = gfx::Rect(22, 43);
2791 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2810 drawing_layer = TestVisibleRectAndDrawableContentRect(
2792 EXPECT_EQ(expected, actual); 2811 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2812 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2813 EXPECT_EQ(expected_drawable_content_rect,
2814 drawing_layer->drawable_content_rect());
2793 2815
2794 // Case 4: The layer is rotated about its top-left corner, and translated 2816 // Case 4: The layer is rotated about its top-left corner, and translated
2795 // upwards. In surface space, the layer is oriented diagonally, with only the 2817 // upwards. In surface space, the layer is oriented diagonally, with only the
2796 // top corner of the surface overlapping the layer. In layer space, the render 2818 // top corner of the surface overlapping the layer. In layer space, the render
2797 // surface overlaps the right side of the layer. The g should be 2819 // surface overlaps the right side of the layer. The g should be
2798 // the layer's right half. 2820 // the layer's right half.
2799 layer_to_surface_transform.MakeIdentity(); 2821 layer_to_surface_transform.MakeIdentity();
2800 layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0); 2822 layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0);
2801 layer_to_surface_transform.Rotate(45.0); 2823 layer_to_surface_transform.Rotate(45.0);
2802 expected = gfx::Rect(15, 0, 15, 30); // Right half of layer bounds. 2824 // Right half of layer bounds.
2803 actual = LayerTreeHostCommon::CalculateVisibleRect( 2825 expected_visible_layer_rect = gfx::Rect(15, 0, 15, 30);
2804 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2826 expected_drawable_content_rect = gfx::Rect(22, 22);
2805 EXPECT_EQ(expected, actual); 2827 drawing_layer = TestVisibleRectAndDrawableContentRect(
2828 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2829 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2830 EXPECT_EQ(expected_drawable_content_rect,
2831 drawing_layer->drawable_content_rect());
2806 } 2832 }
2807 2833
2808 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dOrthographicTransform) { 2834 TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor3dOrthographicTransform) {
2809 // Test that the calculateVisibleRect() function works correctly for 3d 2835 // Test visible layer rect and drawable content rect are calculated correctly
2810 // transforms. 2836 // for 3d transforms.
2811 2837
2812 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100); 2838 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2813 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100); 2839 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2814 gfx::Transform layer_to_surface_transform; 2840 gfx::Transform layer_to_surface_transform;
2815 2841
2816 // Case 1: Orthographic projection of a layer rotated about y-axis by 45 2842 // Case 1: Orthographic projection of a layer rotated about y-axis by 45
2817 // degrees, should be fully contained in the render surface. 2843 // degrees, should be fully contained in the render surface.
2844 // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
2818 layer_to_surface_transform.MakeIdentity(); 2845 layer_to_surface_transform.MakeIdentity();
2819 layer_to_surface_transform.RotateAboutYAxis(45.0); 2846 layer_to_surface_transform.RotateAboutYAxis(45.0);
2820 gfx::Rect expected = gfx::Rect(0, 0, 100, 100); 2847 gfx::Rect expected_visible_layer_rect = gfx::Rect(100, 100);
2821 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect( 2848 gfx::Rect expected_drawable_content_rect = gfx::Rect(71, 100);
2822 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2849 LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
2823 EXPECT_EQ(expected, actual); 2850 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2851 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2852 EXPECT_EQ(expected_drawable_content_rect,
2853 drawing_layer->drawable_content_rect());
2824 2854
2825 // Case 2: Orthographic projection of a layer rotated about y-axis by 45 2855 // Case 2: Orthographic projection of a layer rotated about y-axis by 45
2826 // degrees, but shifted to the side so only the right-half the layer would be 2856 // degrees, but shifted to the side so only the right-half the layer would be
2827 // visible on the surface. 2857 // visible on the surface.
2828 // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width. 2858 // 50 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
2829 SkMScalar half_width_of_rotated_layer = 2859 SkMScalar half_width_of_rotated_layer =
2830 SkDoubleToMScalar((100.0 / sqrt(2.0)) * 0.5); 2860 SkDoubleToMScalar((100.0 / sqrt(2.0)) * 0.5);
2831 layer_to_surface_transform.MakeIdentity(); 2861 layer_to_surface_transform.MakeIdentity();
2832 layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0); 2862 layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0);
2833 layer_to_surface_transform.RotateAboutYAxis(45.0); // Rotates about the left 2863 layer_to_surface_transform.RotateAboutYAxis(45.0); // Rotates about the left
2834 // edge of the layer. 2864 // edge of the layer.
2835 expected = gfx::Rect(50, 0, 50, 100); // Tight half of the layer. 2865 // Tight half of the layer.
2836 actual = LayerTreeHostCommon::CalculateVisibleRect( 2866 expected_visible_layer_rect = gfx::Rect(50, 0, 50, 100);
2837 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2867 expected_drawable_content_rect = gfx::Rect(36, 100);
2838 EXPECT_EQ(expected, actual); 2868 drawing_layer = TestVisibleRectAndDrawableContentRect(
2869 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2870 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2871 EXPECT_EQ(expected_drawable_content_rect,
2872 drawing_layer->drawable_content_rect());
2839 } 2873 }
2840 2874
2841 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveTransform) { 2875 TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor3dPerspectiveTransform) {
2842 // Test the calculateVisibleRect() function works correctly when the layer has 2876 // Test visible layer rect and drawable content rect are calculated correctly
2843 // a perspective projection onto the target surface. 2877 // when the layer has a perspective projection onto the target surface.
2844 2878
2845 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100); 2879 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2846 gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200); 2880 gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200);
2847 gfx::Transform layer_to_surface_transform; 2881 gfx::Transform layer_to_surface_transform;
2848 2882
2849 // Case 1: Even though the layer is twice as large as the surface, due to 2883 // Case 1: Even though the layer is twice as large as the surface, due to
2850 // perspective foreshortening, the layer will fit fully in the surface when 2884 // perspective foreshortening, the layer will fit fully in the surface when
2851 // its translated more than the perspective amount. 2885 // its translated more than the perspective amount.
2852 layer_to_surface_transform.MakeIdentity(); 2886 layer_to_surface_transform.MakeIdentity();
2853 2887
2854 // The following sequence of transforms applies the perspective about the 2888 // The following sequence of transforms applies the perspective about the
2855 // center of the surface. 2889 // center of the surface.
2856 layer_to_surface_transform.Translate(50.0, 50.0); 2890 layer_to_surface_transform.Translate(50.0, 50.0);
2857 layer_to_surface_transform.ApplyPerspectiveDepth(9.0); 2891 layer_to_surface_transform.ApplyPerspectiveDepth(9.0);
2858 layer_to_surface_transform.Translate(-50.0, -50.0); 2892 layer_to_surface_transform.Translate(-50.0, -50.0);
2859 2893
2860 // This translate places the layer in front of the surface's projection plane. 2894 // This translate places the layer in front of the surface's projection plane.
2861 layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0); 2895 layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0);
2862 2896
2863 gfx::Rect expected = gfx::Rect(-50, -50, 200, 200); 2897 // Layer position is (-50, -50), visible rect in layer space is layer bounds
2864 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect( 2898 // offset by layer position.
2865 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2899 gfx::Rect expected_visible_layer_rect = gfx::Rect(50, 50, 150, 150);
2866 EXPECT_EQ(expected, actual); 2900 gfx::Rect expected_drawable_content_rect = gfx::Rect(38, 38);
2901 LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
2902 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2903 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2904 EXPECT_EQ(expected_drawable_content_rect,
2905 drawing_layer->drawable_content_rect());
2867 2906
2868 // Case 2: same projection as before, except that the layer is also translated 2907 // Case 2: same projection as before, except that the layer is also translated
2869 // to the side, so that only the right half of the layer should be visible. 2908 // to the side, so that only the right half of the layer should be visible.
2870 // 2909 //
2871 // Explanation of expected result: The perspective ratio is (z distance 2910 // Explanation of expected result: The perspective ratio is (z distance
2872 // between layer and camera origin) / (z distance between projection plane and 2911 // between layer and camera origin) / (z distance between projection plane and
2873 // camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to 2912 // camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to
2874 // move a layer by translating -50 units in projected surface units (so that 2913 // move a layer by translating -50 units in projected surface units (so that
ajuma 2016/04/19 19:20:37 s/-50/-25
2875 // only half of it is visible), then we would need to translate by (-36 / 9) * 2914 // only half of it is visible), then we would need to translate by (-36 / 9) *
2876 // -50 == -200 in the layer's units. 2915 // -25 == -100 in the layer's units.
2877 layer_to_surface_transform.Translate3d(-200.0, 0.0, 0.0); 2916 layer_to_surface_transform.Translate3d(-100.0, 0.0, 0.0);
2878 expected = gfx::Rect(gfx::Point(50, -50), 2917 // Visible layer rect is moved by 100, and drawable content rect is in target
2879 gfx::Size(100, 200)); // The right half of the layer's 2918 // space and is moved by 25.
2880 // bounding rect. 2919 expected_visible_layer_rect = gfx::Rect(150, 50, 50, 150);
2881 actual = LayerTreeHostCommon::CalculateVisibleRect( 2920 expected_drawable_content_rect = gfx::Rect(13, 38);
2882 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2921 drawing_layer = TestVisibleRectAndDrawableContentRect(
2883 EXPECT_EQ(expected, actual); 2922 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2923 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2924 EXPECT_EQ(expected_drawable_content_rect,
2925 drawing_layer->drawable_content_rect());
2884 } 2926 }
2885 2927
2886 TEST_F(LayerTreeHostCommonTest, 2928 TEST_F(LayerTreeHostCommonDrawRectsTest,
2887 VisibleRectFor3dOrthographicIsNotClippedBehindSurface) { 2929 DrawRectsFor3dOrthographicIsNotClippedBehindSurface) {
2888 // There is currently no explicit concept of an orthographic projection plane 2930 // There is currently no explicit concept of an orthographic projection plane
2889 // in our code (nor in the CSS spec to my knowledge). Therefore, layers that 2931 // in our code (nor in the CSS spec to my knowledge). Therefore, layers that
2890 // are technically behind the surface in an orthographic world should not be 2932 // are technically behind the surface in an orthographic world should not be
2891 // clipped when they are flattened to the surface. 2933 // clipped when they are flattened to the surface.
2892 2934
2893 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100); 2935 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2894 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100); 2936 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2895 gfx::Transform layer_to_surface_transform; 2937 gfx::Transform layer_to_surface_transform;
2896 2938
2897 // This sequence of transforms effectively rotates the layer about the y-axis 2939 // This sequence of transforms effectively rotates the layer about the y-axis
2898 // at the center of the layer. 2940 // at the center of the layer.
2899 layer_to_surface_transform.MakeIdentity(); 2941 layer_to_surface_transform.MakeIdentity();
2900 layer_to_surface_transform.Translate(50.0, 0.0); 2942 layer_to_surface_transform.Translate(50.0, 0.0);
2901 layer_to_surface_transform.RotateAboutYAxis(45.0); 2943 layer_to_surface_transform.RotateAboutYAxis(45.0);
2902 layer_to_surface_transform.Translate(-50.0, 0.0); 2944 layer_to_surface_transform.Translate(-50.0, 0.0);
2903 2945
2904 gfx::Rect expected = gfx::Rect(0, 0, 100, 100); 2946 // Layer is rotated about Y Axis, and its width is 100/sqrt(2) in surface
2905 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect( 2947 // space.
2906 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2948 gfx::Rect expected_visible_layer_rect = gfx::Rect(100, 100);
2907 EXPECT_EQ(expected, actual); 2949 gfx::Rect expected_drawable_content_rect = gfx::Rect(14, 0, 72, 100);
2950 LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
2951 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2952 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2953 EXPECT_EQ(expected_drawable_content_rect,
2954 drawing_layer->drawable_content_rect());
2908 } 2955 }
2909 2956
2910 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveWhenClippedByW) { 2957 TEST_F(LayerTreeHostCommonDrawRectsTest,
2911 // Test the calculateVisibleRect() function works correctly when projecting a 2958 DrawRectsFor3dPerspectiveWhenClippedByW) {
2912 // surface onto a layer, but the layer is partially behind the camera (not 2959 // Test visible layer rect and drawable content rect are calculated correctly
2913 // just behind the projection plane). In this case, the cartesian coordinates 2960 // when projecting a surface onto a layer, but the layer is partially behind
2914 // may seem to be valid, but actually they are not. The visible rect needs to 2961 // the camera (not just behind the projection plane). In this case, the
2915 // be properly clipped by the w = 0 plane in homogeneous coordinates before 2962 // cartesian coordinates may seem to be valid, but actually they are not. The
2916 // converting to cartesian coordinates. 2963 // visible rect needs to be properly clipped by the w = 0 plane in homogeneous
2964 // coordinates before converting to cartesian coordinates. The drawable
2965 // content rect would be entire surface rect because layer is rotated at the
2966 // camera position.
2917 2967
2918 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100); 2968 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 200, 200);
2919 gfx::Rect layer_content_rect = gfx::Rect(-10, -1, 20, 2); 2969 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 20, 2);
2920 gfx::Transform layer_to_surface_transform; 2970 gfx::Transform layer_to_surface_transform;
2921 2971
2922 // The layer is positioned so that the right half of the layer should be in 2972 // The layer is positioned so that the right half of the layer should be in
2923 // front of the camera, while the other half is behind the surface's 2973 // front of the camera, while the other half is behind the surface's
2924 // projection plane. The following sequence of transforms applies the 2974 // projection plane. The following sequence of transforms applies the
2925 // perspective and rotation about the center of the layer. 2975 // perspective and rotation about the center of the layer.
2926 layer_to_surface_transform.MakeIdentity(); 2976 layer_to_surface_transform.MakeIdentity();
2927 layer_to_surface_transform.ApplyPerspectiveDepth(1.0); 2977 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2928 layer_to_surface_transform.Translate3d(-2.0, 0.0, 1.0); 2978 layer_to_surface_transform.Translate3d(10.0, 0.0, 1.0);
2929 layer_to_surface_transform.RotateAboutYAxis(45.0); 2979 layer_to_surface_transform.RotateAboutYAxis(-45.0);
2980 layer_to_surface_transform.Translate(-10, -1);
2930 2981
2931 // Sanity check that this transform does indeed cause w < 0 when applying the 2982 // Sanity check that this transform does indeed cause w < 0 when applying the
2932 // transform, otherwise this code is not testing the intended scenario. 2983 // transform, otherwise this code is not testing the intended scenario.
2933 bool clipped; 2984 bool clipped;
2934 MathUtil::MapQuad(layer_to_surface_transform, 2985 MathUtil::MapQuad(layer_to_surface_transform,
2935 gfx::QuadF(gfx::RectF(layer_content_rect)), 2986 gfx::QuadF(gfx::RectF(layer_content_rect)),
2936 &clipped); 2987 &clipped);
2937 ASSERT_TRUE(clipped); 2988 ASSERT_TRUE(clipped);
2938 2989
2939 int expected_x_position = 0; 2990 gfx::Rect expected_visible_layer_rect = gfx::Rect(0, 1, 10, 1);
2940 int expected_width = 10; 2991 gfx::Rect expected_drawable_content_rect = target_surface_rect;
2941 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect( 2992 LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
2942 target_surface_rect, layer_content_rect, layer_to_surface_transform); 2993 target_surface_rect, layer_to_surface_transform, layer_content_rect);
2943 EXPECT_EQ(expected_x_position, actual.x()); 2994 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
2944 EXPECT_EQ(expected_width, actual.width()); 2995 EXPECT_EQ(expected_drawable_content_rect,
2996 drawing_layer->drawable_content_rect());
2945 } 2997 }
2946 2998
2947 TEST_F(LayerTreeHostCommonTest, VisibleRectForPerspectiveUnprojection) { 2999 TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsForPerspectiveUnprojection) {
2948 // To determine visible rect in layer space, there needs to be an 3000 // To determine visible rect in layer space, there needs to be an
2949 // un-projection from surface space to layer space. When the original 3001 // un-projection from surface space to layer space. When the original
2950 // transform was a perspective projection that was clipped, it returns a rect 3002 // transform was a perspective projection that was clipped, it returns a rect
2951 // that encloses the clipped bounds. Un-projecting this new rect may require 3003 // that encloses the clipped bounds. Un-projecting this new rect may require
2952 // clipping again. 3004 // clipping again.
2953 3005
2954 // This sequence of transforms causes one corner of the layer to protrude 3006 // This sequence of transforms causes one corner of the layer to protrude
2955 // across the w = 0 plane, and should be clipped. 3007 // across the w = 0 plane, and should be clipped.
2956 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100); 3008 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 150, 150);
2957 gfx::Rect layer_content_rect = gfx::Rect(-10, -10, 20, 20); 3009 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 20, 20);
2958 gfx::Transform layer_to_surface_transform; 3010 gfx::Transform layer_to_surface_transform;
2959 layer_to_surface_transform.MakeIdentity(); 3011 layer_to_surface_transform.MakeIdentity();
3012 layer_to_surface_transform.Translate(10, 10);
2960 layer_to_surface_transform.ApplyPerspectiveDepth(1.0); 3013 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2961 layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0); 3014 layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0);
2962 layer_to_surface_transform.RotateAboutYAxis(45.0); 3015 layer_to_surface_transform.RotateAboutYAxis(45.0);
2963 layer_to_surface_transform.RotateAboutXAxis(80.0); 3016 layer_to_surface_transform.RotateAboutXAxis(80.0);
3017 layer_to_surface_transform.Translate(-10, -10);
2964 3018
2965 // Sanity check that un-projection does indeed cause w < 0, otherwise this 3019 // Sanity check that un-projection does indeed cause w < 0, otherwise this
2966 // code is not testing the intended scenario. 3020 // code is not testing the intended scenario.
2967 bool clipped; 3021 bool clipped;
2968 gfx::RectF clipped_rect = MathUtil::MapClippedRect( 3022 gfx::RectF clipped_rect = MathUtil::MapClippedRect(
2969 layer_to_surface_transform, gfx::RectF(layer_content_rect)); 3023 layer_to_surface_transform, gfx::RectF(layer_content_rect));
2970 MathUtil::ProjectQuad( 3024 MathUtil::ProjectQuad(
2971 Inverse(layer_to_surface_transform), gfx::QuadF(clipped_rect), &clipped); 3025 Inverse(layer_to_surface_transform), gfx::QuadF(clipped_rect), &clipped);
2972 ASSERT_TRUE(clipped); 3026 ASSERT_TRUE(clipped);
2973 3027
2974 // Only the corner of the layer is not visible on the surface because of being 3028 // Only the corner of the layer is not visible on the surface because of being
2975 // clipped. But, the net result of rounding visible region to an axis-aligned 3029 // clipped. But, the net result of rounding visible region to an axis-aligned
2976 // rect is that the entire layer should still be considered visible. 3030 // rect is that the entire layer should still be considered visible.
2977 gfx::Rect expected = gfx::Rect(-10, -10, 20, 20); 3031 gfx::Rect expected_visible_layer_rect = layer_content_rect;
2978 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect( 3032 gfx::Rect expected_drawable_content_rect = target_surface_rect;
2979 target_surface_rect, layer_content_rect, layer_to_surface_transform); 3033 LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
2980 EXPECT_EQ(expected, actual); 3034 target_surface_rect, layer_to_surface_transform, layer_content_rect);
3035 EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
3036 EXPECT_EQ(expected_drawable_content_rect,
3037 drawing_layer->drawable_content_rect());
2981 } 3038 }
2982 3039
2983 TEST_F(LayerTreeHostCommonTest, 3040 TEST_F(LayerTreeHostCommonTest,
2984 VisibleRectsForPositionedRootLayerClippedByViewport) { 3041 VisibleRectsForPositionedRootLayerClippedByViewport) {
2985 LayerImpl* root = root_layer(); 3042 LayerImpl* root = root_layer();
2986 root->SetDrawsContent(true); 3043 root->SetDrawsContent(true);
2987 3044
2988 gfx::Transform identity_matrix; 3045 gfx::Transform identity_matrix;
2989 // Root layer is positioned at (60, 70). The default device viewport size 3046 // Root layer is positioned at (60, 70). The default device viewport size
2990 // is (0, 0, 100x100) in target space. So the root layer's visible rect 3047 // is (0, 0, 100x100) in target space. So the root layer's visible rect
(...skipping 6954 matching lines...) Expand 10 before | Expand all | Expand 10 after
9945 EXPECT_EQ(scroll_child6.id, grand_child10->scroll_tree_index()); 10002 EXPECT_EQ(scroll_child6.id, grand_child10->scroll_tree_index());
9946 EXPECT_EQ(scroll_root1.id, parent3->scroll_tree_index()); 10003 EXPECT_EQ(scroll_root1.id, parent3->scroll_tree_index());
9947 EXPECT_EQ(scroll_child7.id, child8->scroll_tree_index()); 10004 EXPECT_EQ(scroll_child7.id, child8->scroll_tree_index());
9948 EXPECT_EQ(scroll_root1.id, parent4->scroll_tree_index()); 10005 EXPECT_EQ(scroll_root1.id, parent4->scroll_tree_index());
9949 EXPECT_EQ(scroll_root1.id, child9->scroll_tree_index()); 10006 EXPECT_EQ(scroll_root1.id, child9->scroll_tree_index());
9950 EXPECT_EQ(scroll_root1.id, grand_child12->scroll_tree_index()); 10007 EXPECT_EQ(scroll_root1.id, grand_child12->scroll_tree_index());
9951 } 10008 }
9952 10009
9953 } // namespace 10010 } // namespace
9954 } // namespace cc 10011 } // namespace cc
OLDNEW
« cc/layers/render_surface_impl.cc ('K') | « cc/trees/layer_tree_host_common.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698