[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 7 | #include "cc/nine_patch_layer_impl.h" |
| 8 | |
| 9 | #include "cc/append_quads_data.h" |
| 10 | #include "cc/single_thread_proxy.h" |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 11 | #include "cc/test/fake_impl_proxy.h" |
| 12 | #include "cc/test/fake_layer_tree_host_impl.h" |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 13 | #include "cc/test/geometry_test_utils.h" |
| 14 | #include "cc/test/layer_test_common.h" |
| 15 | #include "cc/test/mock_quad_culler.h" |
| 16 | #include "cc/texture_draw_quad.h" |
| 17 | #include "ui/gfx/rect_conversions.h" |
| 18 | #include "ui/gfx/safe_integer_conversions.h" |
| 19 | #include "testing/gmock/include/gmock/gmock.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 21 | #include "ui/gfx/transform.h" |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 22 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 23 | namespace cc { |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | gfx::Rect ToRoundedIntRect(gfx::RectF rect_f) { |
| 27 | return gfx::Rect(gfx::ToRoundedInt(rect_f.x()), gfx::ToRoundedInt(rect_f.y()), gfx::ToRoundedInt(rect_f.width()), gfx::ToRoundedInt(rect_f.height())); |
| 28 | } |
| 29 | |
| 30 | TEST(NinePatchLayerImplTest, verifyDrawQuads) |
| 31 | { |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 32 | // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30. |
| 33 | // The bounds of the layer are set to 400x400, so the draw quads |
| 34 | // generated should leave the border width (40) intact. |
| 35 | MockQuadCuller quadCuller; |
| 36 | gfx::Size bitmapSize(100, 100); |
| 37 | gfx::Size layerSize(400, 400); |
| 38 | gfx::Rect visibleContentRect(gfx::Point(), layerSize); |
| 39 | gfx::Rect apertureRect(20, 30, 40, 50); |
| 40 | gfx::Rect scaledApertureNonUniform(20, 30, 340, 350); |
| 41 | |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 42 | FakeImplProxy proxy; |
| 43 | FakeLayerTreeHostImpl hostImpl(&proxy); |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 44 | scoped_ptr<NinePatchLayerImpl> layer = NinePatchLayerImpl::create(hostImpl.activeTree(), 1); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame] | 45 | layer->drawProperties().visible_content_rect = visibleContentRect; |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 46 | layer->setBounds(layerSize); |
| 47 | layer->setContentBounds(layerSize); |
| 48 | layer->createRenderSurface(); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame] | 49 | layer->drawProperties().render_target = layer.get(); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 50 | layer->setLayout(bitmapSize, apertureRect); |
| 51 | layer->setResourceId(1); |
| 52 | |
| 53 | // This scale should not affect the generated quad geometry, but only |
| 54 | // the shared draw transform. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 55 | gfx::Transform transform; |
| 56 | transform.Scale(10, 10); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame] | 57 | layer->drawProperties().target_space_transform = transform; |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 58 | |
| 59 | AppendQuadsData data; |
| 60 | layer->appendQuads(quadCuller, data); |
| 61 | |
| 62 | // Verify quad rects |
| 63 | const QuadList& quads = quadCuller.quadList(); |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 64 | EXPECT_EQ(8, quads.size()); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 65 | Region remaining(visibleContentRect); |
| 66 | for (size_t i = 0; i < quads.size(); ++i) { |
| 67 | DrawQuad* quad = quads[i]; |
[email protected] | 1bc93f6 | 2012-11-17 19:29:50 | [diff] [blame] | 68 | gfx::Rect quadRect = quad->rect; |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 69 | |
| 70 | EXPECT_TRUE(visibleContentRect.Contains(quadRect)) << i; |
| 71 | EXPECT_TRUE(remaining.Contains(quadRect)) << i; |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 72 | EXPECT_EQ(transform, quad->quadTransform()); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 73 | remaining.Subtract(Region(quadRect)); |
| 74 | } |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 75 | EXPECT_RECT_EQ(scaledApertureNonUniform, remaining.bounds()); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 76 | Region scaledApertureRegion(scaledApertureNonUniform); |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 77 | EXPECT_EQ(scaledApertureRegion, remaining); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 78 | |
| 79 | // Verify UV rects |
| 80 | gfx::Rect bitmapRect(gfx::Point(), bitmapSize); |
| 81 | Region texRemaining(bitmapRect); |
| 82 | for (size_t i = 0; i < quads.size(); ++i) { |
| 83 | DrawQuad* quad = quads[i]; |
[email protected] | c22418b | 2012-11-20 23:06:26 | [diff] [blame] | 84 | const TextureDrawQuad* texQuad = TextureDrawQuad::MaterialCast(quad); |
[email protected] | 1fd555b | 2013-01-18 00:13:21 | [diff] [blame] | 85 | gfx::RectF texRect = gfx::BoundingRect(texQuad->uv_top_left, texQuad->uv_bottom_right); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 86 | texRect.Scale(bitmapSize.width(), bitmapSize.height()); |
| 87 | texRemaining.Subtract(Region(ToRoundedIntRect(texRect))); |
| 88 | } |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 89 | EXPECT_RECT_EQ(apertureRect, texRemaining.bounds()); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 90 | Region apertureRegion(apertureRect); |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 91 | EXPECT_EQ(apertureRegion, texRemaining); |
| 92 | } |
| 93 | |
| 94 | TEST(NinePatchLayerImplTest, verifyDrawQuadsForSqueezedLayer) |
| 95 | { |
| 96 | // Test with a layer much smaller than the bitmap. |
| 97 | MockQuadCuller quadCuller; |
| 98 | gfx::Size bitmapSize(101, 101); |
| 99 | gfx::Size layerSize(51, 51); |
| 100 | gfx::Rect visibleContentRect(gfx::Point(), layerSize); |
| 101 | gfx::Rect apertureRect(20, 30, 40, 45); // rightWidth: 40, botHeight: 25 |
| 102 | |
[email protected] | 586d51ed | 2012-12-07 20:31:45 | [diff] [blame] | 103 | FakeImplProxy proxy; |
| 104 | FakeLayerTreeHostImpl hostImpl(&proxy); |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 105 | scoped_ptr<NinePatchLayerImpl> layer = NinePatchLayerImpl::create(hostImpl.activeTree(), 1); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame] | 106 | layer->drawProperties().visible_content_rect = visibleContentRect; |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 107 | layer->setBounds(layerSize); |
| 108 | layer->setContentBounds(layerSize); |
| 109 | layer->createRenderSurface(); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame] | 110 | layer->drawProperties().render_target = layer.get(); |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 111 | layer->setLayout(bitmapSize, apertureRect); |
| 112 | layer->setResourceId(1); |
| 113 | |
| 114 | AppendQuadsData data; |
| 115 | layer->appendQuads(quadCuller, data); |
| 116 | |
| 117 | // Verify corner rects fill the layer and don't overlap |
| 118 | const QuadList& quads = quadCuller.quadList(); |
| 119 | EXPECT_EQ(4, quads.size()); |
| 120 | Region filled; |
| 121 | for (size_t i = 0; i < quads.size(); ++i) { |
| 122 | DrawQuad* quad = quads[i]; |
| 123 | gfx::Rect quadRect = quad->rect; |
| 124 | |
| 125 | EXPECT_FALSE(filled.Intersects(quadRect)); |
| 126 | filled.Union(quadRect); |
| 127 | } |
| 128 | Region expectedFull(visibleContentRect); |
| 129 | EXPECT_EQ(expectedFull, filled); |
| 130 | |
| 131 | // Verify UV rects cover the corners of the bitmap and the crop is weighted |
| 132 | // proportionately to the relative corner sizes (for uneven apertures). |
| 133 | gfx::Rect bitmapRect(gfx::Point(), bitmapSize); |
| 134 | Region texRemaining(bitmapRect); |
| 135 | for (size_t i = 0; i < quads.size(); ++i) { |
| 136 | DrawQuad* quad = quads[i]; |
| 137 | const TextureDrawQuad* texQuad = TextureDrawQuad::MaterialCast(quad); |
[email protected] | 1fd555b | 2013-01-18 00:13:21 | [diff] [blame] | 138 | gfx::RectF texRect = gfx::BoundingRect(texQuad->uv_top_left, texQuad->uv_bottom_right); |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 139 | texRect.Scale(bitmapSize.width(), bitmapSize.height()); |
| 140 | texRemaining.Subtract(Region(ToRoundedIntRect(texRect))); |
| 141 | } |
| 142 | Region expectedRemainingRegion = Region(gfx::Rect(bitmapSize)); |
| 143 | expectedRemainingRegion.Subtract(gfx::Rect(0, 0, 17, 28)); |
| 144 | expectedRemainingRegion.Subtract(gfx::Rect(67, 0, 34, 28)); |
| 145 | expectedRemainingRegion.Subtract(gfx::Rect(0, 78, 17, 23)); |
| 146 | expectedRemainingRegion.Subtract(gfx::Rect(67, 78, 34, 23)); |
| 147 | EXPECT_EQ(expectedRemainingRegion, texRemaining); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 150 | } // namespace |
| 151 | } // namespace cc |