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.
5 #include "base/containers/hash_tables.h"
6 #include "cc/layers/append_quads_data.h"
7 #include "cc/layers/nine_patch_layer_impl.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"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_ui_resource_layer_tree_host_impl.h"
13 #include "cc/test/geometry_test_utils.h"
14 #include "cc/test/layer_test_common.h"
15 #include "cc/trees/single_thread_proxy.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/rect_conversions.h"
19 #include "ui/gfx/safe_integer_conversions.h"
20 #include "ui/gfx/transform.h"
25 gfx::Rect ToRoundedIntRect(const gfx::RectF& rect_f) {
26 return gfx::Rect(gfx::ToRoundedInt(rect_f.x()),
27 gfx::ToRoundedInt(rect_f.y()),
28 gfx::ToRoundedInt(rect_f.width()),
29 gfx::ToRoundedInt(rect_f.height()));
32 void NinePatchLayerLayoutTest(const gfx::Size& bitmap_size,
33 const gfx::Rect& aperture_rect,
34 const gfx::Size& layer_size,
35 const gfx::Rect& border,
37 size_t expected_quad_size) {
38 MockOcclusionTracker<LayerImpl> occlusion_tracker;
39 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
40 gfx::Rect visible_content_rect(layer_size);
41 gfx::Rect expected_remaining(border.x(),
43 layer_size.width() - border.width(),
44 layer_size.height() - border.height());
47 TestSharedBitmapManager shared_bitmap_manager;
48 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
49 scoped_ptr<NinePatchLayerImpl> layer =
50 NinePatchLayerImpl::Create(host_impl.active_tree(), 1);
51 layer->draw_properties().visible_content_rect = visible_content_rect;
52 layer->SetBounds(layer_size);
53 layer->SetContentBounds(layer_size);
54 layer->CreateRenderSurface();
55 layer->draw_properties().render_target = layer.get();
58 bool is_opaque = false;
59 UIResourceBitmap bitmap(bitmap_size, is_opaque);
61 host_impl.CreateUIResource(uid, bitmap);
62 layer->SetUIResourceId(uid);
63 layer->SetImageBounds(bitmap_size);
64 layer->SetLayout(aperture_rect, border, fill_center);
66 layer->AppendQuads(render_pass.get(), occlusion_tracker, &data);
69 const QuadList& quads = render_pass->quad_list;
70 EXPECT_EQ(expected_quad_size, quads.size());
72 Region remaining(visible_content_rect);
74 for (QuadList::ConstIterator iter = quads.begin(); iter != quads.end();
76 gfx::Rect quad_rect = iter->rect;
78 EXPECT_TRUE(visible_content_rect.Contains(quad_rect)) << i;
79 EXPECT_TRUE(remaining.Contains(quad_rect)) << i;
80 remaining.Subtract(Region(quad_rect));
84 // Check if the left-over quad is the same size as the mapped aperture quad in
87 EXPECT_RECT_EQ(expected_remaining, gfx::ToEnclosedRect(remaining.bounds()));
89 EXPECT_TRUE(remaining.bounds().IsEmpty());
93 gfx::Rect bitmap_rect(bitmap_size);
94 Region tex_remaining(bitmap_rect);
95 for (QuadList::ConstIterator iter = quads.begin(); iter != quads.end();
97 const TextureDrawQuad* tex_quad = TextureDrawQuad::MaterialCast(&*iter);
99 gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right);
100 tex_rect.Scale(bitmap_size.width(), bitmap_size.height());
101 tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect)));
105 EXPECT_RECT_EQ(aperture_rect, tex_remaining.bounds());
106 Region aperture_region(aperture_rect);
107 EXPECT_EQ(aperture_region, tex_remaining);
109 EXPECT_TRUE(remaining.bounds().IsEmpty());
113 TEST(NinePatchLayerImplTest, VerifyDrawQuads) {
114 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30.
115 // The bounds of the layer are set to 400x400.
116 gfx::Size bitmap_size(100, 100);
117 gfx::Size layer_size(400, 500);
118 gfx::Rect aperture_rect(20, 30, 40, 50);
119 gfx::Rect border(40, 40, 80, 80);
120 bool fill_center = false;
121 size_t expected_quad_size = 8;
122 NinePatchLayerLayoutTest(bitmap_size,
129 // The bounds of the layer are set to less than the bitmap size.
130 bitmap_size = gfx::Size(100, 100);
131 layer_size = gfx::Size(40, 50);
132 aperture_rect = gfx::Rect(20, 30, 40, 50);
133 border = gfx::Rect(10, 10, 25, 15);
135 expected_quad_size = 9;
136 NinePatchLayerLayoutTest(bitmap_size,
143 // Layer and image sizes are equal.
144 bitmap_size = gfx::Size(100, 100);
145 layer_size = gfx::Size(100, 100);
146 aperture_rect = gfx::Rect(20, 30, 40, 50);
147 border = gfx::Rect(20, 30, 40, 50);
149 expected_quad_size = 9;
150 NinePatchLayerLayoutTest(bitmap_size,
158 TEST(NinePatchLayerImplTest, VerifyDrawQuadsWithEmptyPatches) {
159 // The top component of the 9-patch is empty, so there should be no quads for
160 // the top three components.
161 gfx::Size bitmap_size(100, 100);
162 gfx::Size layer_size(100, 100);
163 gfx::Rect aperture_rect(10, 0, 80, 90);
164 gfx::Rect border(10, 0, 20, 10);
165 bool fill_center = false;
166 size_t expected_quad_size = 5;
167 NinePatchLayerLayoutTest(bitmap_size,
174 // The top and left components of the 9-patch are empty, so there should be no
175 // quads for the left and top components.
176 bitmap_size = gfx::Size(100, 100);
177 layer_size = gfx::Size(100, 100);
178 aperture_rect = gfx::Rect(0, 0, 90, 90);
179 border = gfx::Rect(0, 0, 10, 10);
181 expected_quad_size = 3;
182 NinePatchLayerLayoutTest(bitmap_size,
189 // The aperture is the size of the bitmap and the center doesn't draw.
190 bitmap_size = gfx::Size(100, 100);
191 layer_size = gfx::Size(100, 100);
192 aperture_rect = gfx::Rect(0, 0, 100, 100);
193 border = gfx::Rect(0, 0, 0, 0);
195 expected_quad_size = 0;
196 NinePatchLayerLayoutTest(bitmap_size,
203 // The aperture is the size of the bitmap and the center does draw.
204 bitmap_size = gfx::Size(100, 100);
205 layer_size = gfx::Size(100, 100);
206 aperture_rect = gfx::Rect(0, 0, 100, 100);
207 border = gfx::Rect(0, 0, 0, 0);
209 expected_quad_size = 1;
210 NinePatchLayerLayoutTest(bitmap_size,
218 TEST(NinePatchLayerImplTest, Occlusion) {
219 gfx::Size layer_size(1000, 1000);
220 gfx::Size viewport_size(1000, 1000);
222 LayerTestCommon::LayerImplTest impl;
225 sk_bitmap.allocN32Pixels(10, 10);
226 sk_bitmap.setImmutable();
227 UIResourceId uid = 5;
228 UIResourceBitmap bitmap(sk_bitmap);
229 impl.host_impl()->CreateUIResource(uid, bitmap);
231 NinePatchLayerImpl* nine_patch_layer_impl =
232 impl.AddChildToRoot<NinePatchLayerImpl>();
233 nine_patch_layer_impl->SetBounds(layer_size);
234 nine_patch_layer_impl->SetContentBounds(layer_size);
235 nine_patch_layer_impl->SetDrawsContent(true);
236 nine_patch_layer_impl->SetUIResourceId(uid);
237 nine_patch_layer_impl->SetImageBounds(gfx::Size(10, 10));
239 gfx::Rect aperture = gfx::Rect(3, 3, 4, 4);
240 gfx::Rect border = gfx::Rect(300, 300, 400, 400);
241 nine_patch_layer_impl->SetLayout(aperture, border, true);
243 impl.CalcDrawProps(viewport_size);
246 SCOPED_TRACE("No occlusion");
248 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded);
250 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
251 gfx::Rect(layer_size));
252 EXPECT_EQ(9u, impl.quad_list().size());
256 SCOPED_TRACE("Full occlusion");
257 gfx::Rect occluded(nine_patch_layer_impl->visible_content_rect());
258 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded);
260 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
261 EXPECT_EQ(impl.quad_list().size(), 0u);
265 SCOPED_TRACE("Partial occlusion");
266 gfx::Rect occluded(0, 0, 500, 1000);
267 impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded);
269 size_t partially_occluded_count = 0;
270 LayerTestCommon::VerifyQuadsAreOccluded(
271 impl.quad_list(), occluded, &partially_occluded_count);
272 // The layer outputs nine quads, three of which are partially occluded, and
273 // three fully occluded.
274 EXPECT_EQ(6u, impl.quad_list().size());
275 EXPECT_EQ(3u, partially_occluded_count);