1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <vector> 6 #include "cc/layers/picture_image_layer.h" 7 #include "cc/test/fake_layer_tree_host.h" 8 #include "cc/test/geometry_test_utils.h" 9 #include "cc/trees/layer_tree_host_common.h" 10 #include "content/renderer/compositor_bindings/web_layer_impl_fixed_bounds.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "third_party/WebKit/public/platform/WebFloatPoint.h" 13 #include "third_party/WebKit/public/platform/WebSize.h" 14 #include "third_party/skia/include/utils/SkMatrix44.h" 15 #include "ui/gfx/point3_f.h" 16 17 using blink::WebFloatPoint; 18 using blink::WebSize; 19 20 namespace content { 21 namespace { 22 23 TEST(WebLayerImplFixedBoundsTest, IdentityBounds) { 24 scoped_ptr<WebLayerImplFixedBounds> layer(new WebLayerImplFixedBounds()); 25 layer->setAnchorPoint(WebFloatPoint(0, 0)); 26 layer->SetFixedBounds(gfx::Size(100, 100)); 27 layer->setBounds(WebSize(100, 100)); 28 EXPECT_EQ(WebSize(100, 100), layer->bounds()); 29 EXPECT_EQ(gfx::Size(100, 100), layer->layer()->bounds()); 30 EXPECT_EQ(gfx::Transform(), layer->layer()->transform()); 31 } 32 33 gfx::Point3F TransformPoint(const gfx::Transform& transform, 34 const gfx::Point3F& point) { 35 gfx::Point3F result = point; 36 transform.TransformPoint(&result); 37 return result; 38 } 39 40 void CheckBoundsScaleSimple(WebLayerImplFixedBounds* layer, 41 const WebSize& bounds, 42 const gfx::Size& fixed_bounds) { 43 layer->setBounds(bounds); 44 layer->SetFixedBounds(fixed_bounds); 45 46 EXPECT_EQ(bounds, layer->bounds()); 47 EXPECT_EQ(fixed_bounds, layer->layer()->bounds()); 48 EXPECT_TRUE(layer->transform().isIdentity()); 49 50 // An arbitrary point to check the scale and transforms. 51 gfx::Point3F original_point(10, 20, 1); 52 gfx::Point3F scaled_point( 53 original_point.x() * bounds.width / fixed_bounds.width(), 54 original_point.y() * bounds.height / fixed_bounds.height(), 55 original_point.z()); 56 // Test if the bounds scale is correctly applied in transform. 57 EXPECT_POINT3F_EQ( 58 scaled_point, 59 TransformPoint(layer->layer()->transform(), original_point)); 60 } 61 62 TEST(WebLayerImplFixedBoundsTest, BoundsScaleSimple) { 63 scoped_ptr<WebLayerImplFixedBounds> layer(new WebLayerImplFixedBounds()); 64 layer->setAnchorPoint(WebFloatPoint(0, 0)); 65 CheckBoundsScaleSimple(layer.get(), WebSize(100, 200), gfx::Size(150, 250)); 66 // Change fixed_bounds. 67 CheckBoundsScaleSimple(layer.get(), WebSize(100, 200), gfx::Size(75, 100)); 68 // Change bounds. 69 CheckBoundsScaleSimple(layer.get(), WebSize(300, 100), gfx::Size(75, 100)); 70 } 71 72 void ExpectEqualLayerRectsInTarget(cc::Layer* layer1, cc::Layer* layer2) { 73 gfx::RectF layer1_rect_in_target(layer1->content_bounds()); 74 layer1->draw_transform().TransformRect(&layer1_rect_in_target); 75 76 gfx::RectF layer2_rect_in_target(layer2->content_bounds()); 77 layer2->draw_transform().TransformRect(&layer2_rect_in_target); 78 79 EXPECT_FLOAT_RECT_EQ(layer1_rect_in_target, layer2_rect_in_target); 80 } 81 82 void CompareFixedBoundsLayerAndNormalLayer(const WebFloatPoint& anchor_point, 83 const gfx::Transform& transform) { 84 const gfx::Size kDeviceViewportSize(800, 600); 85 const float kDeviceScaleFactor = 2.f; 86 const float kPageScaleFactor = 1.5f; 87 88 WebSize bounds(150, 200); 89 WebFloatPoint position(20, 30); 90 gfx::Size fixed_bounds(160, 70); 91 92 scoped_ptr<WebLayerImplFixedBounds> root_layer(new WebLayerImplFixedBounds()); 93 94 WebLayerImplFixedBounds* fixed_bounds_layer = 95 new WebLayerImplFixedBounds(cc::PictureImageLayer::Create()); 96 fixed_bounds_layer->setBounds(bounds); 97 fixed_bounds_layer->SetFixedBounds(fixed_bounds); 98 fixed_bounds_layer->setAnchorPoint(anchor_point); 99 fixed_bounds_layer->setTransform(transform.matrix()); 100 fixed_bounds_layer->setPosition(position); 101 root_layer->addChild(fixed_bounds_layer); 102 103 WebLayerImpl* normal_layer(new WebLayerImpl(cc::PictureImageLayer::Create())); 104 105 normal_layer->setBounds(bounds); 106 normal_layer->setAnchorPoint(anchor_point); 107 normal_layer->setTransform(transform.matrix()); 108 normal_layer->setPosition(position); 109 root_layer->addChild(normal_layer); 110 111 scoped_ptr<cc::FakeLayerTreeHost> host = cc::FakeLayerTreeHost::Create(); 112 host->SetRootLayer(root_layer->layer()); 113 114 { 115 cc::RenderSurfaceLayerList render_surface_layer_list; 116 cc::LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs( 117 root_layer->layer(), kDeviceViewportSize, &render_surface_layer_list); 118 inputs.device_scale_factor = kDeviceScaleFactor; 119 inputs.page_scale_factor = kPageScaleFactor; 120 inputs.page_scale_application_layer = root_layer->layer(), 121 cc::LayerTreeHostCommon::CalculateDrawProperties(&inputs); 122 123 ExpectEqualLayerRectsInTarget(normal_layer->layer(), 124 fixed_bounds_layer->layer()); 125 } 126 127 // Change of fixed bounds should not affect the target geometries. 128 fixed_bounds_layer->SetFixedBounds( 129 gfx::Size(fixed_bounds.width() / 2, fixed_bounds.height() * 2)); 130 131 { 132 cc::RenderSurfaceLayerList render_surface_layer_list; 133 cc::LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs( 134 root_layer->layer(), kDeviceViewportSize, &render_surface_layer_list); 135 inputs.device_scale_factor = kDeviceScaleFactor; 136 inputs.page_scale_factor = kPageScaleFactor; 137 inputs.page_scale_application_layer = root_layer->layer(), 138 cc::LayerTreeHostCommon::CalculateDrawProperties(&inputs); 139 140 ExpectEqualLayerRectsInTarget(normal_layer->layer(), 141 fixed_bounds_layer->layer()); 142 } 143 } 144 145 // TODO(perkj): CompareToWebLayerImplSimple disabled on LSAN due to crbug/386080 146 #if defined(LEAK_SANITIZER) 147 #define MAYBE_CompareToWebLayerImplSimple DISABLED_CompareToWebLayerImplSimple 148 #else 149 #define MAYBE_CompareToWebLayerImplSimple CompareToWebLayerImplSimple 150 #endif 151 // A black box test that ensures WebLayerImplFixedBounds won't change target 152 // geometries. Simple case: identity transforms and zero anchor point. 153 TEST(WebLayerImplFixedBoundsTest, MAYBE_CompareToWebLayerImplSimple) { 154 CompareFixedBoundsLayerAndNormalLayer(WebFloatPoint(0, 0), gfx::Transform()); 155 } 156 157 // TODO(perkj): CompareToWebLayerImplComplex disabled on LSAN due to 158 // crbug/386080 159 #if defined(LEAK_SANITIZER) 160 #define MAYBE_CompareToWebLayerImplComplex DISABLED_CompareToWebLayerImplComplex 161 #else 162 #define MAYBE_CompareToWebLayerImplComplex CompareToWebLayerImplComplex 163 #endif 164 // A black box test that ensures WebLayerImplFixedBounds won't change target 165 // geometries. Complex case: complex transforms and non-zero anchor point. 166 TEST(WebLayerImplFixedBoundsTest, MAYBE_CompareToWebLayerImplComplex) { 167 gfx::Transform transform; 168 // These are arbitrary values that should not affect the results. 169 transform.Translate3d(50, 60, 70); 170 transform.Scale3d(2, 3, 4); 171 transform.RotateAbout(gfx::Vector3dF(33, 44, 55), 99); 172 173 CompareFixedBoundsLayerAndNormalLayer(WebFloatPoint(0, 0), transform); 174 175 // With non-zero anchor point, WebLayerImplFixedBounds will fall back to 176 // WebLayerImpl. 177 CompareFixedBoundsLayerAndNormalLayer(WebFloatPoint(0.4f, 0.6f), transform); 178 } 179 180 } // namespace 181 } // namespace content 182