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/geometry/rect_conversions.h"
19 #include "ui/gfx/geometry/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 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
39 gfx::Rect
visible_content_rect(layer_size
);
40 gfx::Rect
expected_remaining(border
.x(),
42 layer_size
.width() - border
.width(),
43 layer_size
.height() - border
.height());
46 TestSharedBitmapManager shared_bitmap_manager
;
47 FakeUIResourceLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
48 scoped_ptr
<NinePatchLayerImpl
> layer
=
49 NinePatchLayerImpl::Create(host_impl
.active_tree(), 1);
50 layer
->draw_properties().visible_content_rect
= visible_content_rect
;
51 layer
->SetBounds(layer_size
);
52 layer
->SetContentBounds(layer_size
);
53 layer
->SetHasRenderSurface(true);
54 layer
->draw_properties().render_target
= layer
.get();
57 bool is_opaque
= false;
58 UIResourceBitmap
bitmap(bitmap_size
, is_opaque
);
60 host_impl
.CreateUIResource(uid
, bitmap
);
61 layer
->SetUIResourceId(uid
);
62 layer
->SetImageBounds(bitmap_size
);
63 layer
->SetLayout(aperture_rect
, border
, fill_center
);
65 layer
->AppendQuads(render_pass
.get(), &data
);
68 const QuadList
& quads
= render_pass
->quad_list
;
69 EXPECT_EQ(expected_quad_size
, quads
.size());
71 Region
remaining(visible_content_rect
);
72 for (auto iter
= quads
.cbegin(); iter
!= quads
.cend(); ++iter
) {
73 gfx::Rect quad_rect
= iter
->rect
;
75 EXPECT_TRUE(visible_content_rect
.Contains(quad_rect
)) << iter
.index();
76 EXPECT_TRUE(remaining
.Contains(quad_rect
)) << iter
.index();
77 remaining
.Subtract(Region(quad_rect
));
80 // Check if the left-over quad is the same size as the mapped aperture quad in
83 EXPECT_EQ(expected_remaining
, gfx::ToEnclosedRect(remaining
.bounds()));
85 EXPECT_TRUE(remaining
.bounds().IsEmpty());
89 gfx::Rect
bitmap_rect(bitmap_size
);
90 Region
tex_remaining(bitmap_rect
);
91 for (const auto& quad
: quads
) {
92 const TextureDrawQuad
* tex_quad
= TextureDrawQuad::MaterialCast(quad
);
94 gfx::BoundingRect(tex_quad
->uv_top_left
, tex_quad
->uv_bottom_right
);
95 tex_rect
.Scale(bitmap_size
.width(), bitmap_size
.height());
96 tex_remaining
.Subtract(Region(ToRoundedIntRect(tex_rect
)));
100 EXPECT_EQ(aperture_rect
, tex_remaining
.bounds());
101 Region
aperture_region(aperture_rect
);
102 EXPECT_EQ(aperture_region
, tex_remaining
);
104 EXPECT_TRUE(remaining
.bounds().IsEmpty());
108 TEST(NinePatchLayerImplTest
, VerifyDrawQuads
) {
109 // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30.
110 // The bounds of the layer are set to 400x400.
111 gfx::Size
bitmap_size(100, 100);
112 gfx::Size
layer_size(400, 500);
113 gfx::Rect
aperture_rect(20, 30, 40, 50);
114 gfx::Rect
border(40, 40, 80, 80);
115 bool fill_center
= false;
116 size_t expected_quad_size
= 8;
117 NinePatchLayerLayoutTest(bitmap_size
,
124 // The bounds of the layer are set to less than the bitmap size.
125 bitmap_size
= gfx::Size(100, 100);
126 layer_size
= gfx::Size(40, 50);
127 aperture_rect
= gfx::Rect(20, 30, 40, 50);
128 border
= gfx::Rect(10, 10, 25, 15);
130 expected_quad_size
= 9;
131 NinePatchLayerLayoutTest(bitmap_size
,
138 // Layer and image sizes are equal.
139 bitmap_size
= gfx::Size(100, 100);
140 layer_size
= gfx::Size(100, 100);
141 aperture_rect
= gfx::Rect(20, 30, 40, 50);
142 border
= gfx::Rect(20, 30, 40, 50);
144 expected_quad_size
= 9;
145 NinePatchLayerLayoutTest(bitmap_size
,
153 TEST(NinePatchLayerImplTest
, VerifyDrawQuadsWithEmptyPatches
) {
154 // The top component of the 9-patch is empty, so there should be no quads for
155 // the top three components.
156 gfx::Size
bitmap_size(100, 100);
157 gfx::Size
layer_size(100, 100);
158 gfx::Rect
aperture_rect(10, 0, 80, 90);
159 gfx::Rect
border(10, 0, 20, 10);
160 bool fill_center
= false;
161 size_t expected_quad_size
= 5;
162 NinePatchLayerLayoutTest(bitmap_size
,
169 // The top and left components of the 9-patch are empty, so there should be no
170 // quads for the left and top components.
171 bitmap_size
= gfx::Size(100, 100);
172 layer_size
= gfx::Size(100, 100);
173 aperture_rect
= gfx::Rect(0, 0, 90, 90);
174 border
= gfx::Rect(0, 0, 10, 10);
176 expected_quad_size
= 3;
177 NinePatchLayerLayoutTest(bitmap_size
,
184 // The aperture is the size of the bitmap and the center doesn't draw.
185 bitmap_size
= gfx::Size(100, 100);
186 layer_size
= gfx::Size(100, 100);
187 aperture_rect
= gfx::Rect(0, 0, 100, 100);
188 border
= gfx::Rect(0, 0, 0, 0);
190 expected_quad_size
= 0;
191 NinePatchLayerLayoutTest(bitmap_size
,
198 // The aperture is the size of the bitmap and the center does draw.
199 bitmap_size
= gfx::Size(100, 100);
200 layer_size
= gfx::Size(100, 100);
201 aperture_rect
= gfx::Rect(0, 0, 100, 100);
202 border
= gfx::Rect(0, 0, 0, 0);
204 expected_quad_size
= 1;
205 NinePatchLayerLayoutTest(bitmap_size
,
213 TEST(NinePatchLayerImplTest
, Occlusion
) {
214 gfx::Size
layer_size(1000, 1000);
215 gfx::Size
viewport_size(1000, 1000);
217 LayerTestCommon::LayerImplTest impl
;
220 sk_bitmap
.allocN32Pixels(10, 10);
221 sk_bitmap
.setImmutable();
222 UIResourceId uid
= 5;
223 UIResourceBitmap
bitmap(sk_bitmap
);
224 impl
.host_impl()->CreateUIResource(uid
, bitmap
);
226 NinePatchLayerImpl
* nine_patch_layer_impl
=
227 impl
.AddChildToRoot
<NinePatchLayerImpl
>();
228 nine_patch_layer_impl
->SetBounds(layer_size
);
229 nine_patch_layer_impl
->SetContentBounds(layer_size
);
230 nine_patch_layer_impl
->SetDrawsContent(true);
231 nine_patch_layer_impl
->SetUIResourceId(uid
);
232 nine_patch_layer_impl
->SetImageBounds(gfx::Size(10, 10));
234 gfx::Rect aperture
= gfx::Rect(3, 3, 4, 4);
235 gfx::Rect border
= gfx::Rect(300, 300, 400, 400);
236 nine_patch_layer_impl
->SetLayout(aperture
, border
, true);
238 impl
.CalcDrawProps(viewport_size
);
241 SCOPED_TRACE("No occlusion");
243 impl
.AppendQuadsWithOcclusion(nine_patch_layer_impl
, occluded
);
245 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(),
246 gfx::Rect(layer_size
));
247 EXPECT_EQ(9u, impl
.quad_list().size());
251 SCOPED_TRACE("Full occlusion");
252 gfx::Rect
occluded(nine_patch_layer_impl
->visible_content_rect());
253 impl
.AppendQuadsWithOcclusion(nine_patch_layer_impl
, occluded
);
255 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(), gfx::Rect());
256 EXPECT_EQ(impl
.quad_list().size(), 0u);
260 SCOPED_TRACE("Partial occlusion");
261 gfx::Rect
occluded(0, 0, 500, 1000);
262 impl
.AppendQuadsWithOcclusion(nine_patch_layer_impl
, occluded
);
264 size_t partially_occluded_count
= 0;
265 LayerTestCommon::VerifyQuadsAreOccluded(
266 impl
.quad_list(), occluded
, &partially_occluded_count
);
267 // The layer outputs nine quads, three of which are partially occluded, and
268 // three fully occluded.
269 EXPECT_EQ(6u, impl
.quad_list().size());
270 EXPECT_EQ(3u, partially_occluded_count
);
274 TEST(NinePatchLayerImplTest
, OpaqueRect
) {
275 gfx::Size
layer_size(1000, 1000);
276 gfx::Size
viewport_size(1000, 1000);
278 LayerTestCommon::LayerImplTest impl
;
280 SkBitmap sk_bitmap_opaque
;
281 sk_bitmap_opaque
.allocN32Pixels(10, 10);
282 sk_bitmap_opaque
.setImmutable();
283 sk_bitmap_opaque
.setAlphaType(kOpaque_SkAlphaType
);
285 UIResourceId uid_opaque
= 6;
286 UIResourceBitmap
bitmap_opaque(sk_bitmap_opaque
);
287 impl
.host_impl()->CreateUIResource(uid_opaque
, bitmap_opaque
);
289 SkBitmap sk_bitmap_alpha
;
290 sk_bitmap_alpha
.allocN32Pixels(10, 10);
291 sk_bitmap_alpha
.setImmutable();
292 sk_bitmap_alpha
.setAlphaType(kUnpremul_SkAlphaType
);
294 UIResourceId uid_alpha
= 7;
295 UIResourceBitmap
bitmap_alpha(sk_bitmap_alpha
);
297 impl
.host_impl()->CreateUIResource(uid_alpha
, bitmap_alpha
);
299 NinePatchLayerImpl
*nine_patch_layer_impl
=
300 impl
.AddChildToRoot
<NinePatchLayerImpl
>();
301 nine_patch_layer_impl
->SetBounds(layer_size
);
302 nine_patch_layer_impl
->SetContentBounds(layer_size
);
303 nine_patch_layer_impl
->SetDrawsContent(true);
305 impl
.CalcDrawProps(viewport_size
);
308 SCOPED_TRACE("Use opaque image");
310 nine_patch_layer_impl
->SetUIResourceId(uid_opaque
);
311 nine_patch_layer_impl
->SetImageBounds(gfx::Size(10, 10));
313 gfx::Rect aperture
= gfx::Rect(3, 3, 4, 4);
314 gfx::Rect border
= gfx::Rect(300, 300, 400, 400);
315 nine_patch_layer_impl
->SetLayout(aperture
, border
, true);
317 impl
.AppendQuadsWithOcclusion(nine_patch_layer_impl
, gfx::Rect());
319 const QuadList
&quad_list
= impl
.quad_list();
320 for (QuadList::ConstBackToFrontIterator it
= quad_list
.BackToFrontBegin();
321 it
!= quad_list
.BackToFrontEnd(); ++it
)
322 EXPECT_FALSE(it
->ShouldDrawWithBlending());
326 SCOPED_TRACE("Use tranparent image");
328 nine_patch_layer_impl
->SetUIResourceId(uid_alpha
);
330 impl
.AppendQuadsWithOcclusion(nine_patch_layer_impl
, gfx::Rect());
332 const QuadList
&quad_list
= impl
.quad_list();
333 for (QuadList::ConstBackToFrontIterator it
= quad_list
.BackToFrontBegin();
334 it
!= quad_list
.BackToFrontEnd(); ++it
)
335 EXPECT_TRUE(it
->ShouldDrawWithBlending());