[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 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 7 | #include "cc/layers/append_quads_data.h" |
| 8 | #include "cc/layers/nine_patch_layer_impl.h" |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 9 | #include "cc/resources/ui_resource_bitmap.h" |
| 10 | #include "cc/resources/ui_resource_client.h" |
khushalsagar | b64b360d | 2015-10-21 19:25:16 | [diff] [blame] | 11 | #include "cc/test/fake_impl_task_runner_provider.h" |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 12 | #include "cc/test/fake_layer_tree_frame_sink.h" |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 13 | #include "cc/test/fake_ui_resource_layer_tree_host_impl.h" |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 14 | #include "cc/test/geometry_test_utils.h" |
| 15 | #include "cc/test/layer_test_common.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 16 | #include "cc/trees/single_thread_proxy.h" |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 17 | #include "components/viz/common/quads/texture_draw_quad.h" |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 18 | #include "testing/gmock/include/gmock/gmock.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
heejin.r.chung | d28506ba | 2014-10-23 16:36:20 | [diff] [blame] | 20 | #include "ui/gfx/geometry/rect_conversions.h" |
| 21 | #include "ui/gfx/geometry/safe_integer_conversions.h" |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 22 | #include "ui/gfx/transform.h" |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 23 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 24 | namespace cc { |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 25 | namespace { |
| 26 | |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 27 | gfx::Rect ToRoundedIntRect(const gfx::RectF& rect_f) { |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 28 | return gfx::Rect(gfx::ToRoundedInt(rect_f.x()), |
| 29 | gfx::ToRoundedInt(rect_f.y()), |
| 30 | gfx::ToRoundedInt(rect_f.width()), |
| 31 | gfx::ToRoundedInt(rect_f.height())); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 34 | void NinePatchLayerLayoutTest(const gfx::Size& bitmap_size, |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 35 | const gfx::Rect& aperture_rect, |
[email protected] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 36 | const gfx::Size& layer_size, |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 37 | const gfx::Rect& border, |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 38 | bool fill_center, |
| 39 | size_t expected_quad_size) { |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 40 | std::unique_ptr<viz::RenderPass> render_pass = viz::RenderPass::Create(); |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 41 | gfx::Rect visible_layer_rect(layer_size); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 42 | gfx::Rect expected_remaining(border.x(), border.y(), |
lionel.g.landwerlin | eec4f7b | 2016-05-16 18:46:46 | [diff] [blame] | 43 | layer_size.width() - border.width(), |
| 44 | layer_size.height() - border.height()); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 45 | |
khushalsagar | b64b360d | 2015-10-21 19:25:16 | [diff] [blame] | 46 | FakeImplTaskRunnerProvider task_runner_provider; |
danakj | cf61058 | 2015-06-16 22:48:56 | [diff] [blame] | 47 | TestTaskGraphRunner task_graph_runner; |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 48 | std::unique_ptr<LayerTreeFrameSink> layer_tree_frame_sink = |
| 49 | FakeLayerTreeFrameSink::Create3d(); |
piman | c44437a2 | 2016-10-29 00:09:22 | [diff] [blame] | 50 | FakeUIResourceLayerTreeHostImpl host_impl(&task_runner_provider, |
| 51 | &task_graph_runner); |
sievers | 71c62dd5 | 2015-10-07 01:44:39 | [diff] [blame] | 52 | host_impl.SetVisible(true); |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 53 | host_impl.InitializeRenderer(layer_tree_frame_sink.get()); |
danakj | 26bcd45 | 2015-04-07 20:14:39 | [diff] [blame] | 54 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 55 | std::unique_ptr<NinePatchLayerImpl> layer = |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 56 | NinePatchLayerImpl::Create(host_impl.active_tree(), 1); |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 57 | layer->draw_properties().visible_layer_rect = visible_layer_rect; |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 58 | layer->SetBounds(layer_size); |
jaydasika | 6b5a32bf | 2016-04-22 21:56:36 | [diff] [blame] | 59 | layer->test_properties()->force_render_surface = true; |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 60 | |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 61 | UIResourceId uid = 1; |
[email protected] | 0046982c | 2014-03-25 22:00:51 | [diff] [blame] | 62 | bool is_opaque = false; |
| 63 | UIResourceBitmap bitmap(bitmap_size, is_opaque); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 64 | |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 65 | host_impl.CreateUIResource(uid, bitmap); |
| 66 | layer->SetUIResourceId(uid); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 67 | layer->SetImageBounds(bitmap_size); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 68 | layer->SetLayout(aperture_rect, border, gfx::Rect(), fill_center, false); |
wkorman | f09921d | 2017-01-07 01:01:20 | [diff] [blame] | 69 | host_impl.active_tree()->SetRootLayerForTesting(std::move(layer)); |
| 70 | host_impl.active_tree()->BuildPropertyTreesForTesting(); |
| 71 | |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 72 | AppendQuadsData data; |
wkorman | f09921d | 2017-01-07 01:01:20 | [diff] [blame] | 73 | host_impl.active_tree()->root_layer_for_testing()->AppendQuads( |
| 74 | render_pass.get(), &data); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 75 | |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 76 | // Verify quad rects |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 77 | const auto& quads = render_pass->quad_list; |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 78 | EXPECT_EQ(expected_quad_size, quads.size()); |
| 79 | |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 80 | Region layer_remaining(visible_layer_rect); |
weiliangc | 7eb7ee6 | 2014-10-07 00:04:40 | [diff] [blame] | 81 | for (auto iter = quads.cbegin(); iter != quads.cend(); ++iter) { |
weiliangc | 032e929d | 2014-09-26 01:55:01 | [diff] [blame] | 82 | gfx::Rect quad_rect = iter->rect; |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 83 | |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 84 | EXPECT_TRUE(visible_layer_rect.Contains(quad_rect)) << iter.index(); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 85 | EXPECT_TRUE(layer_remaining.Contains(quad_rect)) << iter.index(); |
yiyix | 10141eca | 2017-09-05 20:36:52 | [diff] [blame] | 86 | EXPECT_EQ(iter->needs_blending, |
| 87 | !iter->shared_quad_state->are_contents_opaque); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 88 | layer_remaining.Subtract(Region(quad_rect)); |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 89 | } |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 90 | |
| 91 | // Check if the left-over quad is the same size as the mapped aperture quad in |
| 92 | // layer space. |
| 93 | if (!fill_center) { |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 94 | EXPECT_EQ(expected_remaining, layer_remaining.bounds()); |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 95 | } else { |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 96 | EXPECT_TRUE(layer_remaining.bounds().IsEmpty()); |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 97 | } |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 98 | |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 99 | // Verify UV rects |
[email protected] | 2c7c670 | 2013-03-26 03:14:05 | [diff] [blame] | 100 | gfx::Rect bitmap_rect(bitmap_size); |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 101 | Region tex_remaining(bitmap_rect); |
vmpstr | a2e8943 | 2016-07-20 02:05:34 | [diff] [blame] | 102 | for (auto* quad : quads) { |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 103 | const viz::TextureDrawQuad* tex_quad = |
| 104 | viz::TextureDrawQuad::MaterialCast(quad); |
[email protected] | 57115a4 | 2013-03-24 10:00:49 | [diff] [blame] | 105 | gfx::RectF tex_rect = |
| 106 | gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right); |
| 107 | tex_rect.Scale(bitmap_size.width(), bitmap_size.height()); |
| 108 | tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect))); |
| 109 | } |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 110 | |
| 111 | if (!fill_center) { |
lionel.g.landwerlin | eec4f7b | 2016-05-16 18:46:46 | [diff] [blame] | 112 | EXPECT_EQ(aperture_rect, tex_remaining.bounds()); |
| 113 | Region aperture_region(aperture_rect); |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 114 | EXPECT_EQ(aperture_region, tex_remaining); |
| 115 | } else { |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 116 | EXPECT_TRUE(layer_remaining.bounds().IsEmpty()); |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 117 | } |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 118 | } |
| 119 | |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 120 | void NinePatchLayerLayoutTestWithOcclusion(const gfx::Size& bitmap_size, |
| 121 | const gfx::Rect& aperture_rect, |
| 122 | const gfx::Size& layer_size, |
| 123 | const gfx::Rect& border, |
| 124 | const gfx::Rect& occlusion, |
| 125 | bool fill_center, |
| 126 | size_t expected_quad_size) { |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 127 | std::unique_ptr<viz::RenderPass> render_pass = viz::RenderPass::Create(); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 128 | gfx::Rect visible_layer_rect(layer_size); |
| 129 | int border_left = std::min(border.x(), occlusion.x()), |
| 130 | border_top = std::min(border.y(), occlusion.y()), |
| 131 | border_right = std::min(border.width() - border.x(), |
| 132 | layer_size.width() - occlusion.right()), |
| 133 | border_bottom = std::min(border.height() - border.y(), |
| 134 | layer_size.height() - occlusion.bottom()); |
| 135 | gfx::Rect expected_layer_remaining( |
| 136 | border_left, border_top, layer_size.width() - border_left - border_right, |
| 137 | layer_size.height() - border_top - border_bottom); |
| 138 | float ratio_left = border_left == 0 ? 0 : (aperture_rect.x() / border.x()), |
| 139 | ratio_top = border_top == 0 ? 0 : (aperture_rect.y() / border.y()), |
| 140 | ratio_right = border_right == 0 |
| 141 | ? 0 |
| 142 | : ((bitmap_size.width() - aperture_rect.right()) / |
| 143 | (border.width() - border.x())), |
| 144 | ratio_bottom = border_bottom == 0 |
| 145 | ? 0 |
| 146 | : ((bitmap_size.height() - aperture_rect.bottom()) / |
| 147 | (border.height() - border.y())); |
| 148 | int image_remaining_left = border_left * ratio_left, |
| 149 | image_remaining_top = border_top * ratio_top, |
| 150 | image_remaining_right = border_right * ratio_right, |
| 151 | image_remaining_bottom = border_bottom * ratio_bottom; |
| 152 | gfx::Rect expected_tex_remaining( |
| 153 | image_remaining_left, image_remaining_top, |
| 154 | bitmap_size.width() - image_remaining_right - image_remaining_left, |
| 155 | bitmap_size.height() - image_remaining_bottom - image_remaining_top); |
| 156 | |
| 157 | FakeImplTaskRunnerProvider task_runner_provider; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 158 | TestTaskGraphRunner task_graph_runner; |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 159 | std::unique_ptr<LayerTreeFrameSink> layer_tree_frame_sink = |
| 160 | FakeLayerTreeFrameSink::Create3d(); |
piman | c44437a2 | 2016-10-29 00:09:22 | [diff] [blame] | 161 | FakeUIResourceLayerTreeHostImpl host_impl(&task_runner_provider, |
| 162 | &task_graph_runner); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 163 | host_impl.SetVisible(true); |
danakj | c7afae5 | 2017-06-20 21:12:41 | [diff] [blame] | 164 | host_impl.InitializeRenderer(layer_tree_frame_sink.get()); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 165 | |
| 166 | std::unique_ptr<NinePatchLayerImpl> layer = |
| 167 | NinePatchLayerImpl::Create(host_impl.active_tree(), 1); |
| 168 | layer->draw_properties().visible_layer_rect = visible_layer_rect; |
| 169 | layer->SetBounds(layer_size); |
| 170 | layer->test_properties()->force_render_surface = true; |
| 171 | |
| 172 | UIResourceId uid = 1; |
| 173 | bool is_opaque = false; |
| 174 | UIResourceBitmap bitmap(bitmap_size, is_opaque); |
| 175 | |
| 176 | host_impl.CreateUIResource(uid, bitmap); |
| 177 | layer->SetUIResourceId(uid); |
| 178 | layer->SetImageBounds(bitmap_size); |
| 179 | layer->SetLayout(aperture_rect, border, occlusion, false, false); |
wkorman | f09921d | 2017-01-07 01:01:20 | [diff] [blame] | 180 | host_impl.active_tree()->SetRootLayerForTesting(std::move(layer)); |
| 181 | host_impl.active_tree()->BuildPropertyTreesForTesting(); |
| 182 | |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 183 | AppendQuadsData data; |
wkorman | f09921d | 2017-01-07 01:01:20 | [diff] [blame] | 184 | host_impl.active_tree()->root_layer_for_testing()->AppendQuads( |
| 185 | render_pass.get(), &data); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 186 | |
| 187 | // Verify quad rects |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 188 | const auto& quads = render_pass->quad_list; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 189 | EXPECT_EQ(expected_quad_size, quads.size()); |
| 190 | |
| 191 | Region layer_remaining(visible_layer_rect); |
| 192 | for (auto iter = quads.cbegin(); iter != quads.cend(); ++iter) { |
| 193 | gfx::Rect quad_rect = iter->rect; |
| 194 | |
| 195 | EXPECT_TRUE(visible_layer_rect.Contains(quad_rect)) << iter.index(); |
| 196 | EXPECT_TRUE(layer_remaining.Contains(quad_rect)) << iter.index(); |
yiyix | 10141eca | 2017-09-05 20:36:52 | [diff] [blame] | 197 | EXPECT_EQ(iter->needs_blending, |
| 198 | !iter->shared_quad_state->are_contents_opaque); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 199 | layer_remaining.Subtract(Region(quad_rect)); |
| 200 | } |
| 201 | |
| 202 | // Check if the left-over quad is the same size as the mapped aperture quad in |
| 203 | // layer space. |
| 204 | EXPECT_EQ(expected_layer_remaining, layer_remaining.bounds()); |
| 205 | |
| 206 | // Verify UV rects |
| 207 | gfx::Rect bitmap_rect(bitmap_size); |
| 208 | Region tex_remaining(bitmap_rect); |
vmpstr | 691539d | 2017-02-13 19:49:18 | [diff] [blame] | 209 | for (auto* quad : quads) { |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 210 | const viz::TextureDrawQuad* tex_quad = |
| 211 | viz::TextureDrawQuad::MaterialCast(quad); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 212 | gfx::RectF tex_rect = |
| 213 | gfx::BoundingRect(tex_quad->uv_top_left, tex_quad->uv_bottom_right); |
| 214 | tex_rect.Scale(bitmap_size.width(), bitmap_size.height()); |
| 215 | tex_remaining.Subtract(Region(ToRoundedIntRect(tex_rect))); |
| 216 | } |
| 217 | |
| 218 | EXPECT_EQ(expected_tex_remaining, tex_remaining.bounds()); |
| 219 | Region aperture_region(expected_tex_remaining); |
| 220 | EXPECT_EQ(aperture_region, tex_remaining); |
| 221 | } |
| 222 | |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 223 | TEST(NinePatchLayerImplTest, VerifyDrawQuads) { |
| 224 | // Input is a 100x100 bitmap with a 40x50 aperture at x=20, y=30. |
| 225 | // The bounds of the layer are set to 400x400. |
| 226 | gfx::Size bitmap_size(100, 100); |
| 227 | gfx::Size layer_size(400, 500); |
| 228 | gfx::Rect aperture_rect(20, 30, 40, 50); |
| 229 | gfx::Rect border(40, 40, 80, 80); |
| 230 | bool fill_center = false; |
| 231 | size_t expected_quad_size = 8; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 232 | NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border, |
| 233 | fill_center, expected_quad_size); |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 234 | |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 235 | // The bounds of the layer are set to less than the bitmap size. |
| 236 | bitmap_size = gfx::Size(100, 100); |
| 237 | layer_size = gfx::Size(40, 50); |
| 238 | aperture_rect = gfx::Rect(20, 30, 40, 50); |
| 239 | border = gfx::Rect(10, 10, 25, 15); |
| 240 | fill_center = true; |
| 241 | expected_quad_size = 9; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 242 | NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border, |
| 243 | fill_center, expected_quad_size); |
[email protected] | 411ccfc | 2012-11-21 22:14:12 | [diff] [blame] | 244 | |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame] | 245 | // Layer and image sizes are equal. |
| 246 | bitmap_size = gfx::Size(100, 100); |
| 247 | layer_size = gfx::Size(100, 100); |
| 248 | aperture_rect = gfx::Rect(20, 30, 40, 50); |
| 249 | border = gfx::Rect(20, 30, 40, 50); |
| 250 | fill_center = true; |
| 251 | expected_quad_size = 9; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 252 | NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border, |
| 253 | fill_center, expected_quad_size); |
| 254 | } |
| 255 | |
| 256 | TEST(NinePatchLayerImplTest, VerifyDrawQuadsWithOcclusion) { |
| 257 | // Occlusion removed part of the border and leaves us with 12 patches. |
| 258 | gfx::Size bitmap_size(100, 100); |
| 259 | gfx::Rect aperture_rect(30, 30, 40, 40); |
| 260 | gfx::Size layer_size(400, 400); |
| 261 | gfx::Rect occlusion(20, 20, 360, 360); |
| 262 | gfx::Rect border(30, 30, 60, 60); |
| 263 | size_t expected_quad_size = 12; |
| 264 | NinePatchLayerLayoutTestWithOcclusion(bitmap_size, aperture_rect, layer_size, |
| 265 | border, occlusion, false, |
| 266 | expected_quad_size); |
| 267 | |
| 268 | bitmap_size = gfx::Size(100, 100); |
| 269 | aperture_rect = gfx::Rect(20, 30, 60, 40); |
| 270 | layer_size = gfx::Size(400, 400); |
| 271 | occlusion = gfx::Rect(10, 10, 380, 380); |
| 272 | border = gfx::Rect(20, 30, 40, 60); |
| 273 | expected_quad_size = 12; |
| 274 | NinePatchLayerLayoutTestWithOcclusion(bitmap_size, aperture_rect, layer_size, |
| 275 | border, occlusion, false, |
| 276 | expected_quad_size); |
| 277 | |
| 278 | // All borders are empty, so nothing should be drawn. |
| 279 | bitmap_size = gfx::Size(100, 100); |
| 280 | aperture_rect = gfx::Rect(0, 0, 100, 100); |
| 281 | layer_size = gfx::Size(400, 400); |
| 282 | occlusion = gfx::Rect(0, 0, 400, 400); |
| 283 | border = gfx::Rect(0, 0, 0, 0); |
| 284 | expected_quad_size = 0; |
| 285 | NinePatchLayerLayoutTestWithOcclusion(bitmap_size, aperture_rect, layer_size, |
| 286 | border, occlusion, false, |
| 287 | expected_quad_size); |
| 288 | |
| 289 | // Right border is empty, we should have no quads on the right side. |
| 290 | bitmap_size = gfx::Size(100, 100); |
| 291 | aperture_rect = gfx::Rect(20, 30, 80, 40); |
| 292 | layer_size = gfx::Size(400, 400); |
| 293 | occlusion = gfx::Rect(10, 10, 390, 380); |
| 294 | border = gfx::Rect(20, 30, 20, 60); |
| 295 | expected_quad_size = 7; |
| 296 | NinePatchLayerLayoutTestWithOcclusion(bitmap_size, aperture_rect, layer_size, |
| 297 | border, occlusion, false, |
| 298 | expected_quad_size); |
[email protected] | 35680c0 | 2012-11-06 05:53:00 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | e681b3d | 2014-05-09 01:43:00 | [diff] [blame] | 301 | TEST(NinePatchLayerImplTest, VerifyDrawQuadsWithEmptyPatches) { |
| 302 | // The top component of the 9-patch is empty, so there should be no quads for |
| 303 | // the top three components. |
| 304 | gfx::Size bitmap_size(100, 100); |
| 305 | gfx::Size layer_size(100, 100); |
| 306 | gfx::Rect aperture_rect(10, 0, 80, 90); |
| 307 | gfx::Rect border(10, 0, 20, 10); |
| 308 | bool fill_center = false; |
| 309 | size_t expected_quad_size = 5; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 310 | NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border, |
| 311 | fill_center, expected_quad_size); |
[email protected] | e681b3d | 2014-05-09 01:43:00 | [diff] [blame] | 312 | |
| 313 | // The top and left components of the 9-patch are empty, so there should be no |
| 314 | // quads for the left and top components. |
| 315 | bitmap_size = gfx::Size(100, 100); |
| 316 | layer_size = gfx::Size(100, 100); |
| 317 | aperture_rect = gfx::Rect(0, 0, 90, 90); |
| 318 | border = gfx::Rect(0, 0, 10, 10); |
| 319 | fill_center = false; |
| 320 | expected_quad_size = 3; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 321 | NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border, |
| 322 | fill_center, expected_quad_size); |
[email protected] | e681b3d | 2014-05-09 01:43:00 | [diff] [blame] | 323 | |
| 324 | // The aperture is the size of the bitmap and the center doesn't draw. |
| 325 | bitmap_size = gfx::Size(100, 100); |
| 326 | layer_size = gfx::Size(100, 100); |
| 327 | aperture_rect = gfx::Rect(0, 0, 100, 100); |
| 328 | border = gfx::Rect(0, 0, 0, 0); |
| 329 | fill_center = false; |
| 330 | expected_quad_size = 0; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 331 | NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border, |
| 332 | fill_center, expected_quad_size); |
[email protected] | e681b3d | 2014-05-09 01:43:00 | [diff] [blame] | 333 | |
| 334 | // The aperture is the size of the bitmap and the center does draw. |
| 335 | bitmap_size = gfx::Size(100, 100); |
| 336 | layer_size = gfx::Size(100, 100); |
| 337 | aperture_rect = gfx::Rect(0, 0, 100, 100); |
| 338 | border = gfx::Rect(0, 0, 0, 0); |
| 339 | fill_center = true; |
| 340 | expected_quad_size = 1; |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 341 | NinePatchLayerLayoutTest(bitmap_size, aperture_rect, layer_size, border, |
| 342 | fill_center, expected_quad_size); |
[email protected] | e681b3d | 2014-05-09 01:43:00 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | ffe2734 | 2014-04-18 01:25:06 | [diff] [blame] | 345 | TEST(NinePatchLayerImplTest, Occlusion) { |
| 346 | gfx::Size layer_size(1000, 1000); |
| 347 | gfx::Size viewport_size(1000, 1000); |
| 348 | |
| 349 | LayerTestCommon::LayerImplTest impl; |
| 350 | |
| 351 | SkBitmap sk_bitmap; |
| 352 | sk_bitmap.allocN32Pixels(10, 10); |
| 353 | sk_bitmap.setImmutable(); |
| 354 | UIResourceId uid = 5; |
| 355 | UIResourceBitmap bitmap(sk_bitmap); |
| 356 | impl.host_impl()->CreateUIResource(uid, bitmap); |
| 357 | |
| 358 | NinePatchLayerImpl* nine_patch_layer_impl = |
| 359 | impl.AddChildToRoot<NinePatchLayerImpl>(); |
[email protected] | ffe2734 | 2014-04-18 01:25:06 | [diff] [blame] | 360 | nine_patch_layer_impl->SetBounds(layer_size); |
[email protected] | ffe2734 | 2014-04-18 01:25:06 | [diff] [blame] | 361 | nine_patch_layer_impl->SetDrawsContent(true); |
| 362 | nine_patch_layer_impl->SetUIResourceId(uid); |
| 363 | nine_patch_layer_impl->SetImageBounds(gfx::Size(10, 10)); |
| 364 | |
| 365 | gfx::Rect aperture = gfx::Rect(3, 3, 4, 4); |
| 366 | gfx::Rect border = gfx::Rect(300, 300, 400, 400); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 367 | nine_patch_layer_impl->SetLayout(aperture, border, gfx::Rect(), true, false); |
[email protected] | ffe2734 | 2014-04-18 01:25:06 | [diff] [blame] | 368 | |
| 369 | impl.CalcDrawProps(viewport_size); |
| 370 | |
| 371 | { |
| 372 | SCOPED_TRACE("No occlusion"); |
| 373 | gfx::Rect occluded; |
| 374 | impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded); |
| 375 | |
| 376 | LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), |
| 377 | gfx::Rect(layer_size)); |
| 378 | EXPECT_EQ(9u, impl.quad_list().size()); |
| 379 | } |
| 380 | |
| 381 | { |
| 382 | SCOPED_TRACE("Full occlusion"); |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 383 | gfx::Rect occluded(nine_patch_layer_impl->visible_layer_rect()); |
[email protected] | ffe2734 | 2014-04-18 01:25:06 | [diff] [blame] | 384 | impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded); |
| 385 | |
| 386 | LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect()); |
| 387 | EXPECT_EQ(impl.quad_list().size(), 0u); |
| 388 | } |
| 389 | |
| 390 | { |
| 391 | SCOPED_TRACE("Partial occlusion"); |
| 392 | gfx::Rect occluded(0, 0, 500, 1000); |
| 393 | impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, occluded); |
| 394 | |
| 395 | size_t partially_occluded_count = 0; |
hartmanng | 53af0fe | 2014-09-22 20:26:11 | [diff] [blame] | 396 | LayerTestCommon::VerifyQuadsAreOccluded( |
| 397 | impl.quad_list(), occluded, &partially_occluded_count); |
[email protected] | ffe2734 | 2014-04-18 01:25:06 | [diff] [blame] | 398 | // The layer outputs nine quads, three of which are partially occluded, and |
| 399 | // three fully occluded. |
| 400 | EXPECT_EQ(6u, impl.quad_list().size()); |
| 401 | EXPECT_EQ(3u, partially_occluded_count); |
| 402 | } |
| 403 | } |
| 404 | |
jungjik.lee | c9ca782 | 2014-11-14 01:10:27 | [diff] [blame] | 405 | TEST(NinePatchLayerImplTest, OpaqueRect) { |
| 406 | gfx::Size layer_size(1000, 1000); |
| 407 | gfx::Size viewport_size(1000, 1000); |
| 408 | |
| 409 | LayerTestCommon::LayerImplTest impl; |
| 410 | |
| 411 | SkBitmap sk_bitmap_opaque; |
| 412 | sk_bitmap_opaque.allocN32Pixels(10, 10); |
| 413 | sk_bitmap_opaque.setImmutable(); |
| 414 | sk_bitmap_opaque.setAlphaType(kOpaque_SkAlphaType); |
| 415 | |
| 416 | UIResourceId uid_opaque = 6; |
| 417 | UIResourceBitmap bitmap_opaque(sk_bitmap_opaque); |
| 418 | impl.host_impl()->CreateUIResource(uid_opaque, bitmap_opaque); |
| 419 | |
| 420 | SkBitmap sk_bitmap_alpha; |
| 421 | sk_bitmap_alpha.allocN32Pixels(10, 10); |
| 422 | sk_bitmap_alpha.setImmutable(); |
| 423 | sk_bitmap_alpha.setAlphaType(kUnpremul_SkAlphaType); |
| 424 | |
| 425 | UIResourceId uid_alpha = 7; |
| 426 | UIResourceBitmap bitmap_alpha(sk_bitmap_alpha); |
| 427 | |
| 428 | impl.host_impl()->CreateUIResource(uid_alpha, bitmap_alpha); |
| 429 | |
| 430 | NinePatchLayerImpl *nine_patch_layer_impl = |
| 431 | impl.AddChildToRoot<NinePatchLayerImpl>(); |
| 432 | nine_patch_layer_impl->SetBounds(layer_size); |
jungjik.lee | c9ca782 | 2014-11-14 01:10:27 | [diff] [blame] | 433 | nine_patch_layer_impl->SetDrawsContent(true); |
| 434 | |
| 435 | impl.CalcDrawProps(viewport_size); |
| 436 | |
| 437 | { |
| 438 | SCOPED_TRACE("Use opaque image"); |
| 439 | |
| 440 | nine_patch_layer_impl->SetUIResourceId(uid_opaque); |
| 441 | nine_patch_layer_impl->SetImageBounds(gfx::Size(10, 10)); |
| 442 | |
| 443 | gfx::Rect aperture = gfx::Rect(3, 3, 4, 4); |
| 444 | gfx::Rect border = gfx::Rect(300, 300, 400, 400); |
lionel.g.landwerlin | 258c7d3 | 2016-07-27 09:55:24 | [diff] [blame] | 445 | nine_patch_layer_impl->SetLayout(aperture, border, gfx::Rect(), true, |
| 446 | false); |
jungjik.lee | c9ca782 | 2014-11-14 01:10:27 | [diff] [blame] | 447 | |
| 448 | impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, gfx::Rect()); |
| 449 | |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 450 | const auto& quad_list = impl.quad_list(); |
| 451 | for (auto it = quad_list.BackToFrontBegin(); |
jungjik.lee | c9ca782 | 2014-11-14 01:10:27 | [diff] [blame] | 452 | it != quad_list.BackToFrontEnd(); ++it) |
| 453 | EXPECT_FALSE(it->ShouldDrawWithBlending()); |
| 454 | } |
| 455 | |
| 456 | { |
| 457 | SCOPED_TRACE("Use tranparent image"); |
| 458 | |
| 459 | nine_patch_layer_impl->SetUIResourceId(uid_alpha); |
| 460 | |
| 461 | impl.AppendQuadsWithOcclusion(nine_patch_layer_impl, gfx::Rect()); |
| 462 | |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 463 | const auto& quad_list = impl.quad_list(); |
| 464 | for (auto it = quad_list.BackToFrontBegin(); |
jungjik.lee | c9ca782 | 2014-11-14 01:10:27 | [diff] [blame] | 465 | it != quad_list.BackToFrontEnd(); ++it) |
| 466 | EXPECT_TRUE(it->ShouldDrawWithBlending()); |
| 467 | } |
| 468 | } |
| 469 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame] | 470 | } // namespace |
| 471 | } // namespace cc |