Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 <stdio.h> | 5 #include "base/containers/hash_tables.h" |
| 6 | |
| 7 #include "cc/layers/append_quads_data.h" | 6 #include "cc/layers/append_quads_data.h" |
| 8 #include "cc/layers/nine_patch_layer_impl.h" | 7 #include "cc/layers/nine_patch_layer_impl.h" |
| 9 #include "cc/quads/texture_draw_quad.h" | 8 #include "cc/quads/texture_draw_quad.h" |
| 9 #include "cc/resources/ui_resource_bitmap.h" | |
| 10 #include "cc/resources/ui_resource_client.h" | |
| 10 #include "cc/test/fake_impl_proxy.h" | 11 #include "cc/test/fake_impl_proxy.h" |
| 11 #include "cc/test/fake_layer_tree_host_impl.h" | 12 #include "cc/test/fake_layer_tree_host_impl.h" |
| 12 #include "cc/test/geometry_test_utils.h" | 13 #include "cc/test/geometry_test_utils.h" |
| 13 #include "cc/test/layer_test_common.h" | 14 #include "cc/test/layer_test_common.h" |
| 14 #include "cc/test/mock_quad_culler.h" | 15 #include "cc/test/mock_quad_culler.h" |
| 15 #include "cc/trees/single_thread_proxy.h" | 16 #include "cc/trees/single_thread_proxy.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/gfx/rect_conversions.h" | 19 #include "ui/gfx/rect_conversions.h" |
| 19 #include "ui/gfx/safe_integer_conversions.h" | 20 #include "ui/gfx/safe_integer_conversions.h" |
| 20 #include "ui/gfx/transform.h" | 21 #include "ui/gfx/transform.h" |
| 21 | 22 |
| 22 namespace cc { | 23 namespace cc { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 26 class FakeUIResourceLayerTreeHostImpl : public FakeLayerTreeHostImpl { | |
| 27 public: | |
| 28 explicit FakeUIResourceLayerTreeHostImpl(Proxy* proxy) | |
| 29 : FakeLayerTreeHostImpl(proxy), fake_next_resource_id_(1) {} | |
| 30 | |
| 31 virtual void CreateUIResource( | |
| 32 UIResourceId uid, | |
| 33 scoped_refptr<UIResourceBitmap> bitmap) OVERRIDE { | |
| 34 if (ResourceIdForUIResource(uid)) | |
| 35 DeleteUIResource(uid); | |
| 36 fake_ui_resource_map_[uid] = fake_next_resource_id_; | |
| 37 } | |
| 38 | |
| 39 virtual void DeleteUIResource(UIResourceId uid) OVERRIDE { | |
| 40 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid); | |
| 41 if (id) | |
| 42 fake_ui_resource_map_.erase(uid); | |
| 43 } | |
| 44 | |
| 45 virtual ResourceProvider::ResourceId ResourceIdForUIResource( | |
| 46 UIResourceId uid) const OVERRIDE { | |
| 47 UIResourceMap::const_iterator iter = fake_ui_resource_map_.find(uid); | |
| 48 if (iter != fake_ui_resource_map_.end()) | |
| 49 return iter->second; | |
| 50 return 0; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 ResourceProvider::ResourceId fake_next_resource_id_; | |
| 55 typedef base::hash_map<UIResourceId, ResourceProvider::ResourceId> | |
| 56 UIResourceMap; | |
| 57 UIResourceMap fake_ui_resource_map_; | |
| 58 }; | |
| 59 | |
| 25 gfx::Rect ToRoundedIntRect(gfx::RectF rect_f) { | 60 gfx::Rect ToRoundedIntRect(gfx::RectF rect_f) { |
| 26 return gfx::Rect(gfx::ToRoundedInt(rect_f.x()), | 61 return gfx::Rect(gfx::ToRoundedInt(rect_f.x()), |
| 27 gfx::ToRoundedInt(rect_f.y()), | 62 gfx::ToRoundedInt(rect_f.y()), |
| 28 gfx::ToRoundedInt(rect_f.width()), | 63 gfx::ToRoundedInt(rect_f.width()), |
| 29 gfx::ToRoundedInt(rect_f.height())); | 64 gfx::ToRoundedInt(rect_f.height())); |
| 30 } | 65 } |
| 31 | 66 |
| 32 TEST(NinePatchLayerImplTest, VerifyDrawQuads) { | 67 void NinePatchLayerLayoutTest(gfx::Size bitmap_size, |
| 33 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30. | 68 gfx::Rect aperture_rect, |
| 34 // The bounds of the layer are set to 400x400, so the draw quads | 69 gfx::Size layer_size, |
| 35 // generated should leave the border width (40) intact. | 70 gfx::Rect border) { |
| 36 MockQuadCuller quad_culler; | 71 MockQuadCuller quad_culler; |
| 37 gfx::Size bitmap_size(100, 100); | |
| 38 gfx::Size layer_size(400, 400); | |
| 39 gfx::Rect visible_content_rect(layer_size); | 72 gfx::Rect visible_content_rect(layer_size); |
| 40 gfx::Rect aperture_rect(20, 30, 40, 50); | 73 gfx::Rect expected_remaining(border.x(), |
| 41 gfx::Rect scaled_aperture_non_uniform(20, 30, 340, 350); | 74 border.y(), |
| 75 layer_size.width() - border.width(), | |
| 76 layer_size.height() - border.height()); | |
| 42 | 77 |
| 43 FakeImplProxy proxy; | 78 FakeImplProxy proxy; |
| 44 FakeLayerTreeHostImpl host_impl(&proxy); | 79 FakeUIResourceLayerTreeHostImpl host_impl(&proxy); |
| 45 scoped_ptr<NinePatchLayerImpl> layer = | 80 scoped_ptr<NinePatchLayerImpl> layer = |
| 46 NinePatchLayerImpl::Create(host_impl.active_tree(), 1); | 81 NinePatchLayerImpl::Create(host_impl.active_tree(), 1); |
| 47 layer->draw_properties().visible_content_rect = visible_content_rect; | 82 layer->draw_properties().visible_content_rect = visible_content_rect; |
| 48 layer->SetBounds(layer_size); | 83 layer->SetBounds(layer_size); |
| 49 layer->SetContentBounds(layer_size); | 84 layer->SetContentBounds(layer_size); |
| 50 layer->CreateRenderSurface(); | 85 layer->CreateRenderSurface(); |
| 51 layer->draw_properties().render_target = layer.get(); | 86 layer->draw_properties().render_target = layer.get(); |
| 52 layer->SetLayout(bitmap_size, aperture_rect); | 87 layer->SetLayout(bitmap_size, aperture_rect, border, false); |
|
danakj
2013/08/27 15:42:48
should fill center be a parameter of NinePatchLaye
powei
2013/08/27 22:39:28
Done.
| |
| 53 layer->SetResourceId(1); | |
| 54 | 88 |
| 55 // This scale should not affect the generated quad geometry, but only | 89 UIResourceId uid = 1; |
| 56 // the shared draw transform. | 90 scoped_refptr<UIResourceBitmap> bitmap = |
| 57 gfx::Transform transform; | 91 UIResourceBitmap::Create(new uint8_t[bitmap_size.GetArea() * 4], |
| 58 transform.Scale(10, 10); | 92 UIResourceBitmap::RGBA8, |
| 59 layer->draw_properties().target_space_transform = transform; | 93 bitmap_size); |
| 94 layer->set_ui_resource_id(uid); | |
| 95 host_impl.CreateUIResource(uid, bitmap); | |
| 60 | 96 |
| 61 AppendQuadsData data; | 97 AppendQuadsData data; |
| 62 layer->AppendQuads(&quad_culler, &data); | 98 layer->AppendQuads(&quad_culler, &data); |
| 63 | 99 |
| 64 // Verify quad rects | 100 // Verify quad rects |
| 65 const QuadList& quads = quad_culler.quad_list(); | 101 const QuadList& quads = quad_culler.quad_list(); |
| 66 EXPECT_EQ(8u, quads.size()); | 102 EXPECT_EQ(8u, quads.size()); |
| 67 Region remaining(visible_content_rect); | 103 Region remaining(visible_content_rect); |
| 68 for (size_t i = 0; i < quads.size(); ++i) { | 104 for (size_t i = 0; i < quads.size(); ++i) { |
| 69 DrawQuad* quad = quads[i]; | 105 DrawQuad* quad = quads[i]; |
| 70 gfx::Rect quad_rect = quad->rect; | 106 gfx::Rect quad_rect = quad->rect; |
| 71 | 107 |
| 72 EXPECT_TRUE(visible_content_rect.Contains(quad_rect)) << i; | 108 EXPECT_TRUE(visible_content_rect.Contains(quad_rect)) << i; |
| 73 EXPECT_TRUE(remaining.Contains(quad_rect)) << i; | 109 EXPECT_TRUE(remaining.Contains(quad_rect)) << i; |
| 74 EXPECT_EQ(transform, quad->quadTransform()); | |
| 75 remaining.Subtract(Region(quad_rect)); | 110 remaining.Subtract(Region(quad_rect)); |
| 76 } | 111 } |
| 77 EXPECT_RECT_EQ(scaled_aperture_non_uniform, remaining.bounds()); | 112 |
| 78 Region scaled_aperture_region(scaled_aperture_non_uniform); | 113 // Check if the left-over quad is the same size as the mapped aperture quad in |
| 79 EXPECT_EQ(scaled_aperture_region, remaining); | 114 // layer space. |
| 115 EXPECT_RECT_EQ(expected_remaining, gfx::ToEnclosedRect(remaining.bounds())); | |
| 80 | 116 |
| 81 // Verify UV rects | 117 // Verify UV rects |
| 82 gfx::Rect bitmap_rect(bitmap_size); | 118 gfx::Rect bitmap_rect(bitmap_size); |
| 83 Region tex_remaining(bitmap_rect); | 119 Region tex_remaining(bitmap_rect); |
| 84 for (size_t i = 0; i < quads.size(); ++i) { | 120 for (size_t i = 0; i < quads.size(); ++i) { |
| 85 DrawQuad* quad = quads[i]; | 121 DrawQuad* quad = quads[i]; |
| 86 const TextureDrawQuad* tex_quad = TextureDrawQuad::MaterialCast(quad); | 122 const TextureDrawQuad* tex_quad = TextureDrawQuad::MaterialCast(quad); |
| 87 gfx::RectF tex_rect = | 123 gfx::RectF tex_rect = |
| 88 gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right); | 124 gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right); |
| 89 tex_rect.Scale(bitmap_size.width(), bitmap_size.height()); | 125 tex_rect.Scale(bitmap_size.width(), bitmap_size.height()); |
| 90 tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect))); | 126 tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect))); |
| 91 } | 127 } |
| 92 EXPECT_RECT_EQ(aperture_rect, tex_remaining.bounds()); | 128 EXPECT_RECT_EQ(aperture_rect, tex_remaining.bounds()); |
| 93 Region aperture_region(aperture_rect); | 129 Region aperture_region(aperture_rect); |
| 94 EXPECT_EQ(aperture_region, tex_remaining); | 130 EXPECT_EQ(aperture_region, tex_remaining); |
| 95 } | 131 } |
| 96 | 132 |
| 97 TEST(NinePatchLayerImplTest, VerifyDrawQuadsForSqueezedLayer) { | 133 TEST(NinePatchLayerImplTest, VerifyDrawQuads) { |
|
danakj
2013/08/27 15:42:48
How about a few more tests that check different im
| |
| 98 // Test with a layer much smaller than the bitmap. | 134 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30. |
| 99 MockQuadCuller quad_culler; | 135 // The bounds of the layer are set to 400x400. |
| 100 gfx::Size bitmap_size(101, 101); | 136 gfx::Size bitmap_size(100, 100); |
| 101 gfx::Size layer_size(51, 51); | 137 gfx::Size layer_size(400, 500); |
| 102 gfx::Rect visible_content_rect(layer_size); | 138 gfx::Rect aperture_rect(20, 30, 40, 50); |
| 103 gfx::Rect aperture_rect(20, 30, 40, 45); // rightWidth: 40, botHeight: 25 | 139 gfx::Rect border(40, 40, 80, 80); |
| 140 NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border); | |
| 104 | 141 |
| 105 FakeImplProxy proxy; | 142 // The bounds of the layer are set to less than the bitmap size. |
| 106 FakeLayerTreeHostImpl host_impl(&proxy); | 143 bitmap_size = gfx::Size(100, 100); |
| 107 scoped_ptr<NinePatchLayerImpl> layer = | 144 layer_size = gfx::Size(40, 50); |
| 108 NinePatchLayerImpl::Create(host_impl.active_tree(), 1); | 145 aperture_rect = gfx::Rect(20, 30, 40, 50); |
| 109 layer->draw_properties().visible_content_rect = visible_content_rect; | 146 border = gfx::Rect(10, 10, 25, 15); |
| 110 layer->SetBounds(layer_size); | 147 NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border); |
| 111 layer->SetContentBounds(layer_size); | |
| 112 layer->CreateRenderSurface(); | |
| 113 layer->draw_properties().render_target = layer.get(); | |
| 114 layer->SetLayout(bitmap_size, aperture_rect); | |
| 115 layer->SetResourceId(1); | |
| 116 | |
| 117 AppendQuadsData data; | |
| 118 layer->AppendQuads(&quad_culler, &data); | |
| 119 | |
| 120 // Verify corner rects fill the layer and don't overlap | |
| 121 const QuadList& quads = quad_culler.quad_list(); | |
| 122 EXPECT_EQ(4u, quads.size()); | |
| 123 Region filled; | |
| 124 for (size_t i = 0; i < quads.size(); ++i) { | |
| 125 DrawQuad* quad = quads[i]; | |
| 126 gfx::Rect quad_rect = quad->rect; | |
| 127 | |
| 128 EXPECT_FALSE(filled.Intersects(quad_rect)); | |
| 129 filled.Union(quad_rect); | |
| 130 } | |
| 131 Region expected_full(visible_content_rect); | |
| 132 EXPECT_EQ(expected_full, filled); | |
| 133 | |
| 134 // Verify UV rects cover the corners of the bitmap and the crop is weighted | |
| 135 // proportionately to the relative corner sizes (for uneven apertures). | |
| 136 gfx::Rect bitmap_rect(bitmap_size); | |
| 137 Region tex_remaining(bitmap_rect); | |
| 138 for (size_t i = 0; i < quads.size(); ++i) { | |
| 139 DrawQuad* quad = quads[i]; | |
| 140 const TextureDrawQuad* tex_quad = TextureDrawQuad::MaterialCast(quad); | |
| 141 gfx::RectF tex_rect = | |
| 142 gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right); | |
| 143 tex_rect.Scale(bitmap_size.width(), bitmap_size.height()); | |
| 144 tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect))); | |
| 145 } | |
| 146 Region expected_remaining_region = Region(gfx::Rect(bitmap_size)); | |
| 147 expected_remaining_region.Subtract(gfx::Rect(0, 0, 17, 28)); | |
| 148 expected_remaining_region.Subtract(gfx::Rect(67, 0, 34, 28)); | |
| 149 expected_remaining_region.Subtract(gfx::Rect(0, 78, 17, 23)); | |
| 150 expected_remaining_region.Subtract(gfx::Rect(67, 78, 34, 23)); | |
| 151 EXPECT_EQ(expected_remaining_region, tex_remaining); | |
| 152 } | 148 } |
| 153 | 149 |
| 154 } // namespace | 150 } // namespace |
| 155 } // namespace cc | 151 } // namespace cc |
| OLD | NEW |