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

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