[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 1 | // Copyright 2013 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 | |
| 5 | #include "cc/layers/append_quads_data.h" |
| 6 | #include "cc/layers/ui_resource_layer_impl.h" |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 7 | #include "cc/quads/draw_quad.h" |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 8 | #include "cc/resources/ui_resource_bitmap.h" |
| 9 | #include "cc/resources/ui_resource_client.h" |
| 10 | #include "cc/test/fake_impl_proxy.h" |
| 11 | #include "cc/test/fake_layer_tree_host_impl.h" |
| 12 | #include "cc/test/fake_ui_resource_layer_tree_host_impl.h" |
| 13 | #include "cc/test/layer_test_common.h" |
[email protected] | 4e2eb35 | 2014-03-20 17:25:45 | [diff] [blame] | 14 | #include "cc/test/test_shared_bitmap_manager.h" |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 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/transform.h" |
| 19 | |
| 20 | namespace cc { |
| 21 | namespace { |
| 22 | |
| 23 | scoped_ptr<UIResourceLayerImpl> GenerateUIResourceLayer( |
| 24 | FakeUIResourceLayerTreeHostImpl* host_impl, |
[email protected] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 25 | const gfx::Size& bitmap_size, |
| 26 | const gfx::Size& layer_size, |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 27 | bool opaque, |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 28 | UIResourceId uid) { |
| 29 | gfx::Rect visible_content_rect(layer_size); |
| 30 | scoped_ptr<UIResourceLayerImpl> layer = |
| 31 | UIResourceLayerImpl::Create(host_impl->active_tree(), 1); |
| 32 | layer->draw_properties().visible_content_rect = visible_content_rect; |
| 33 | layer->SetBounds(layer_size); |
| 34 | layer->SetContentBounds(layer_size); |
| 35 | layer->CreateRenderSurface(); |
| 36 | layer->draw_properties().render_target = layer.get(); |
| 37 | |
[email protected] | 0046982c | 2014-03-25 22:00:51 | [diff] [blame] | 38 | UIResourceBitmap bitmap(bitmap_size, opaque); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 39 | |
| 40 | host_impl->CreateUIResource(uid, bitmap); |
| 41 | layer->SetUIResourceId(uid); |
| 42 | |
| 43 | return layer.Pass(); |
| 44 | } |
| 45 | |
| 46 | void QuadSizeTest(scoped_ptr<UIResourceLayerImpl> layer, |
| 47 | size_t expected_quad_size) { |
[email protected] | b74d7f28 | 2014-06-13 22:01:42 | [diff] [blame] | 48 | scoped_ptr<RenderPass> render_pass = RenderPass::Create(); |
[email protected] | b74d7f28 | 2014-06-13 22:01:42 | [diff] [blame] | 49 | |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 50 | AppendQuadsData data; |
vmpstr | 11b77b43 | 2014-10-07 20:11:40 | [diff] [blame] | 51 | layer->AppendQuads(render_pass.get(), Occlusion(), &data); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 52 | |
| 53 | // Verify quad rects |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 54 | const QuadList& quads = render_pass->quad_list; |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 55 | EXPECT_EQ(expected_quad_size, quads.size()); |
| 56 | } |
| 57 | |
| 58 | TEST(UIResourceLayerImplTest, VerifyDrawQuads) { |
| 59 | FakeImplProxy proxy; |
[email protected] | 4e2eb35 | 2014-03-20 17:25:45 | [diff] [blame] | 60 | TestSharedBitmapManager shared_bitmap_manager; |
| 61 | FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 62 | // Make sure we're appending quads when there are valid values. |
| 63 | gfx::Size bitmap_size(100, 100); |
| 64 | gfx::Size layer_size(100, 100);; |
| 65 | size_t expected_quad_size = 1; |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 66 | bool opaque = true; |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 67 | UIResourceId uid = 1; |
| 68 | scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl, |
| 69 | bitmap_size, |
| 70 | layer_size, |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 71 | opaque, |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 72 | uid); |
| 73 | QuadSizeTest(layer.Pass(), expected_quad_size); |
| 74 | |
| 75 | // Make sure we're not appending quads when there are invalid values. |
| 76 | expected_quad_size = 0; |
| 77 | uid = 0; |
| 78 | layer = GenerateUIResourceLayer(&host_impl, |
| 79 | bitmap_size, |
| 80 | layer_size, |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 81 | opaque, |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 82 | uid); |
| 83 | QuadSizeTest(layer.Pass(), expected_quad_size); |
| 84 | } |
| 85 | |
| 86 | void OpaqueBoundsTest(scoped_ptr<UIResourceLayerImpl> layer, |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 87 | const gfx::Rect& expected_opaque_bounds) { |
[email protected] | b74d7f28 | 2014-06-13 22:01:42 | [diff] [blame] | 88 | scoped_ptr<RenderPass> render_pass = RenderPass::Create(); |
[email protected] | b74d7f28 | 2014-06-13 22:01:42 | [diff] [blame] | 89 | |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 90 | AppendQuadsData data; |
vmpstr | 11b77b43 | 2014-10-07 20:11:40 | [diff] [blame] | 91 | layer->AppendQuads(render_pass.get(), Occlusion(), &data); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 92 | |
| 93 | // Verify quad rects |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 94 | const QuadList& quads = render_pass->quad_list; |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 95 | EXPECT_GE(quads.size(), (size_t)0); |
weiliangc | 032e929d | 2014-09-26 01:55:01 | [diff] [blame] | 96 | gfx::Rect opaque_rect = quads.front()->opaque_rect; |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 97 | EXPECT_EQ(expected_opaque_bounds, opaque_rect); |
| 98 | } |
| 99 | |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 100 | TEST(UIResourceLayerImplTest, VerifySetOpaqueOnSkBitmap) { |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 101 | FakeImplProxy proxy; |
[email protected] | 4e2eb35 | 2014-03-20 17:25:45 | [diff] [blame] | 102 | TestSharedBitmapManager shared_bitmap_manager; |
| 103 | FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 104 | |
| 105 | gfx::Size bitmap_size(100, 100); |
| 106 | gfx::Size layer_size(100, 100);; |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 107 | bool opaque = false; |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 108 | UIResourceId uid = 1; |
| 109 | scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl, |
| 110 | bitmap_size, |
| 111 | layer_size, |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 112 | opaque, |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 113 | uid); |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 114 | gfx::Rect expected_opaque_bounds; |
| 115 | OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds); |
| 116 | |
| 117 | opaque = true; |
| 118 | layer = GenerateUIResourceLayer(&host_impl, |
| 119 | bitmap_size, |
| 120 | layer_size, |
| 121 | opaque, |
| 122 | uid); |
bokan | cccfde7 | 2014-10-08 15:15:22 | [diff] [blame^] | 123 | expected_opaque_bounds = gfx::Rect(layer->bounds()); |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 124 | OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds); |
| 125 | } |
| 126 | |
| 127 | TEST(UIResourceLayerImplTest, VerifySetOpaqueOnLayer) { |
| 128 | FakeImplProxy proxy; |
[email protected] | 4e2eb35 | 2014-03-20 17:25:45 | [diff] [blame] | 129 | TestSharedBitmapManager shared_bitmap_manager; |
| 130 | FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 131 | |
| 132 | gfx::Size bitmap_size(100, 100); |
| 133 | gfx::Size layer_size(100, 100); |
| 134 | bool skbitmap_opaque = false; |
| 135 | UIResourceId uid = 1; |
| 136 | scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer( |
| 137 | &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 138 | layer->SetContentsOpaque(false); |
| 139 | gfx::Rect expected_opaque_bounds; |
| 140 | OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds); |
| 141 | |
[email protected] | 709c954 | 2013-10-26 01:43:51 | [diff] [blame] | 142 | layer = GenerateUIResourceLayer( |
| 143 | &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 144 | layer->SetContentsOpaque(true); |
bokan | cccfde7 | 2014-10-08 15:15:22 | [diff] [blame^] | 145 | expected_opaque_bounds = gfx::Rect(layer->bounds()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 146 | OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds); |
| 147 | } |
| 148 | |
[email protected] | 3539089 | 2014-03-20 06:36:13 | [diff] [blame] | 149 | TEST(UIResourceLayerImplTest, Occlusion) { |
| 150 | gfx::Size layer_size(1000, 1000); |
| 151 | gfx::Size viewport_size(1000, 1000); |
| 152 | |
| 153 | LayerTestCommon::LayerImplTest impl; |
| 154 | |
| 155 | SkBitmap sk_bitmap; |
| 156 | sk_bitmap.allocN32Pixels(10, 10); |
| 157 | sk_bitmap.setImmutable(); |
| 158 | UIResourceId uid = 5; |
| 159 | UIResourceBitmap bitmap(sk_bitmap); |
| 160 | impl.host_impl()->CreateUIResource(uid, bitmap); |
| 161 | |
| 162 | UIResourceLayerImpl* ui_resource_layer_impl = |
| 163 | impl.AddChildToRoot<UIResourceLayerImpl>(); |
[email protected] | 3539089 | 2014-03-20 06:36:13 | [diff] [blame] | 164 | ui_resource_layer_impl->SetBounds(layer_size); |
| 165 | ui_resource_layer_impl->SetContentBounds(layer_size); |
| 166 | ui_resource_layer_impl->SetDrawsContent(true); |
| 167 | ui_resource_layer_impl->SetUIResourceId(uid); |
| 168 | |
| 169 | impl.CalcDrawProps(viewport_size); |
| 170 | |
| 171 | { |
| 172 | SCOPED_TRACE("No occlusion"); |
| 173 | gfx::Rect occluded; |
| 174 | impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded); |
| 175 | |
| 176 | LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), |
| 177 | gfx::Rect(layer_size)); |
| 178 | EXPECT_EQ(1u, impl.quad_list().size()); |
| 179 | } |
| 180 | |
| 181 | { |
| 182 | SCOPED_TRACE("Full occlusion"); |
| 183 | gfx::Rect occluded(ui_resource_layer_impl->visible_content_rect()); |
| 184 | impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded); |
| 185 | |
| 186 | LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect()); |
| 187 | EXPECT_EQ(impl.quad_list().size(), 0u); |
| 188 | } |
| 189 | |
| 190 | { |
| 191 | SCOPED_TRACE("Partial occlusion"); |
| 192 | gfx::Rect occluded(200, 0, 800, 1000); |
| 193 | impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded); |
| 194 | |
| 195 | size_t partially_occluded_count = 0; |
hartmanng | 53af0fe | 2014-09-22 20:26:11 | [diff] [blame] | 196 | LayerTestCommon::VerifyQuadsAreOccluded( |
| 197 | impl.quad_list(), occluded, &partially_occluded_count); |
[email protected] | 3539089 | 2014-03-20 06:36:13 | [diff] [blame] | 198 | // The layer outputs one quad, which is partially occluded. |
| 199 | EXPECT_EQ(1u, impl.quad_list().size()); |
| 200 | EXPECT_EQ(1u, partially_occluded_count); |
| 201 | } |
| 202 | } |
| 203 | |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 204 | } // namespace |
| 205 | } // namespace cc |