[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 1 | // Copyright 2014 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/render_surface_impl.h" |
| 6 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 9 | #include "cc/layers/append_quads_data.h" |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 10 | #include "cc/test/fake_mask_layer_impl.h" |
sunxd | bb0b3ae9 | 2017-04-05 21:13:20 | [diff] [blame] | 11 | #include "cc/test/fake_raster_source.h" |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 12 | #include "cc/test/layer_test_common.h" |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 13 | #include "components/viz/common/quads/render_pass_draw_quad.h" |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace cc { |
| 17 | namespace { |
| 18 | |
| 19 | TEST(RenderSurfaceLayerImplTest, Occlusion) { |
| 20 | gfx::Size layer_size(1000, 1000); |
| 21 | gfx::Size viewport_size(1000, 1000); |
| 22 | |
| 23 | LayerTestCommon::LayerImplTest impl; |
| 24 | |
| 25 | LayerImpl* owning_layer_impl = impl.AddChildToRoot<LayerImpl>(); |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 26 | owning_layer_impl->SetBounds(layer_size); |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 27 | owning_layer_impl->SetDrawsContent(true); |
jaydasika | 6b5a32bf | 2016-04-22 21:56:36 | [diff] [blame] | 28 | owning_layer_impl->test_properties()->force_render_surface = true; |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 29 | |
| 30 | impl.CalcDrawProps(viewport_size); |
| 31 | |
chrishtr | 7e3aaf2 | 2017-05-04 15:04:01 | [diff] [blame] | 32 | RenderSurfaceImpl* render_surface_impl = GetRenderSurface(owning_layer_impl); |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 33 | ASSERT_TRUE(render_surface_impl); |
| 34 | |
| 35 | { |
| 36 | SCOPED_TRACE("No occlusion"); |
| 37 | gfx::Rect occluded; |
| 38 | impl.AppendSurfaceQuadsWithOcclusion(render_surface_impl, occluded); |
| 39 | |
| 40 | LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), |
| 41 | gfx::Rect(layer_size)); |
| 42 | EXPECT_EQ(1u, impl.quad_list().size()); |
| 43 | } |
| 44 | |
| 45 | { |
| 46 | SCOPED_TRACE("Full occlusion"); |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 47 | gfx::Rect occluded(owning_layer_impl->visible_layer_rect()); |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 48 | impl.AppendSurfaceQuadsWithOcclusion(render_surface_impl, occluded); |
| 49 | |
| 50 | LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect()); |
| 51 | EXPECT_EQ(impl.quad_list().size(), 0u); |
| 52 | } |
| 53 | |
| 54 | { |
| 55 | SCOPED_TRACE("Partial occlusion"); |
| 56 | gfx::Rect occluded(200, 0, 800, 1000); |
| 57 | impl.AppendSurfaceQuadsWithOcclusion(render_surface_impl, occluded); |
| 58 | |
| 59 | size_t partially_occluded_count = 0; |
hartmanng | 53af0fe | 2014-09-22 20:26:11 | [diff] [blame] | 60 | LayerTestCommon::VerifyQuadsAreOccluded( |
| 61 | impl.quad_list(), occluded, &partially_occluded_count); |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 62 | // The layer outputs one quad, which is partially occluded. |
| 63 | EXPECT_EQ(1u, impl.quad_list().size()); |
| 64 | EXPECT_EQ(1u, partially_occluded_count); |
| 65 | } |
| 66 | } |
| 67 | |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 68 | static std::unique_ptr<viz::RenderPass> DoAppendQuadsWithScaledMask( |
sunxd | 48493dc | 2017-10-12 20:25:14 | [diff] [blame] | 69 | DrawMode draw_mode, |
| 70 | float device_scale_factor, |
| 71 | Layer::LayerMaskType mask_type) { |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 72 | gfx::Size layer_size(1000, 1000); |
| 73 | gfx::Size viewport_size(1000, 1000); |
ericrk | 664405a | 2017-05-04 20:58:37 | [diff] [blame] | 74 | float scale_factor = 2; |
sunxd | bb0b3ae9 | 2017-04-05 21:13:20 | [diff] [blame] | 75 | scoped_refptr<FakeRasterSource> raster_source = |
| 76 | FakeRasterSource::CreateFilledSolidColor(layer_size); |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 77 | |
reveman | b2b99a3b | 2017-04-06 23:39:08 | [diff] [blame] | 78 | LayerTreeSettings settings; |
| 79 | settings.layer_transforms_should_scale_layer_contents = true; |
| 80 | LayerTestCommon::LayerImplTest impl(settings); |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 81 | std::unique_ptr<LayerImpl> root = |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 82 | LayerImpl::Create(impl.host_impl()->active_tree(), 2); |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 83 | std::unique_ptr<LayerImpl> surface = |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 84 | LayerImpl::Create(impl.host_impl()->active_tree(), 3); |
| 85 | surface->SetBounds(layer_size); |
ajuma | 35b24f6 | 2017-01-27 21:23:09 | [diff] [blame] | 86 | surface->test_properties()->force_render_surface = true; |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 87 | |
| 88 | gfx::Transform scale; |
ericrk | 664405a | 2017-05-04 20:58:37 | [diff] [blame] | 89 | scale.Scale(scale_factor, scale_factor); |
jaydasika | 10d43fc | 2016-08-18 04:06:04 | [diff] [blame] | 90 | surface->test_properties()->transform = scale; |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 91 | |
ericrk | 664405a | 2017-05-04 20:58:37 | [diff] [blame] | 92 | std::unique_ptr<FakeMaskLayerImpl> mask_layer = FakeMaskLayerImpl::Create( |
sunxd | 48493dc | 2017-10-12 20:25:14 | [diff] [blame] | 93 | impl.host_impl()->active_tree(), 4, raster_source, mask_type); |
ericrk | 664405a | 2017-05-04 20:58:37 | [diff] [blame] | 94 | mask_layer->set_resource_size( |
| 95 | gfx::ScaleToCeiledSize(layer_size, scale_factor)); |
| 96 | mask_layer->SetDrawsContent(true); |
| 97 | mask_layer->SetBounds(layer_size); |
| 98 | surface->test_properties()->SetMaskLayer(std::move(mask_layer)); |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 99 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 100 | std::unique_ptr<LayerImpl> child = |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 101 | LayerImpl::Create(impl.host_impl()->active_tree(), 5); |
| 102 | child->SetDrawsContent(true); |
| 103 | child->SetBounds(layer_size); |
| 104 | |
jaydasika | 89f7b5a | 2016-06-22 02:08:39 | [diff] [blame] | 105 | surface->test_properties()->AddChild(std::move(child)); |
| 106 | root->test_properties()->AddChild(std::move(surface)); |
jaydasika | bf1875a | 2016-06-28 03:39:59 | [diff] [blame] | 107 | impl.host_impl()->active_tree()->SetRootLayerForTesting(std::move(root)); |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 108 | |
sunxd | 48493dc | 2017-10-12 20:25:14 | [diff] [blame] | 109 | impl.host_impl()->active_tree()->SetDeviceScaleFactor(device_scale_factor); |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 110 | impl.host_impl()->SetViewportSize(viewport_size); |
jaydasika | 4340ea0 | 2016-06-06 19:44:26 | [diff] [blame] | 111 | impl.host_impl()->active_tree()->BuildLayerListAndPropertyTreesForTesting(); |
Vladimir Levin | 55c6640 | 2017-07-13 02:21:06 | [diff] [blame] | 112 | impl.host_impl()->active_tree()->UpdateDrawProperties(); |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 113 | |
jaydasika | fc66cfb | 2016-06-10 04:34:22 | [diff] [blame] | 114 | LayerImpl* surface_raw = impl.host_impl() |
| 115 | ->active_tree() |
jaydasika | bf1875a | 2016-06-28 03:39:59 | [diff] [blame] | 116 | ->root_layer_for_testing() |
jaydasika | fc66cfb | 2016-06-10 04:34:22 | [diff] [blame] | 117 | ->test_properties() |
| 118 | ->children[0]; |
chrishtr | 7e3aaf2 | 2017-05-04 15:04:01 | [diff] [blame] | 119 | RenderSurfaceImpl* render_surface_impl = GetRenderSurface(surface_raw); |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 120 | std::unique_ptr<viz::RenderPass> render_pass = viz::RenderPass::Create(); |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 121 | AppendQuadsData append_quads_data; |
Bo Liu | 9636833 | 2017-06-15 19:52:43 | [diff] [blame] | 122 | render_surface_impl->AppendQuads(draw_mode, render_pass.get(), |
| 123 | &append_quads_data); |
| 124 | return render_pass; |
| 125 | } |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 126 | |
Bo Liu | 9636833 | 2017-06-15 19:52:43 | [diff] [blame] | 127 | TEST(RenderSurfaceLayerImplTest, AppendQuadsWithScaledMask) { |
sunxd | 48493dc | 2017-10-12 20:25:14 | [diff] [blame] | 128 | std::unique_ptr<viz::RenderPass> render_pass = DoAppendQuadsWithScaledMask( |
| 129 | DRAW_MODE_HARDWARE, 1.f, Layer::LayerMaskType::SINGLE_TEXTURE_MASK); |
sunxd | bb0b3ae9 | 2017-04-05 21:13:20 | [diff] [blame] | 130 | DCHECK(render_pass->quad_list.front()); |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 131 | const viz::RenderPassDrawQuad* quad = |
| 132 | viz::RenderPassDrawQuad::MaterialCast(render_pass->quad_list.front()); |
sunxd | bb0b3ae9 | 2017-04-05 21:13:20 | [diff] [blame] | 133 | EXPECT_EQ(gfx::RectF(0, 0, 1, 1), quad->mask_uv_rect); |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 134 | EXPECT_EQ(gfx::Vector2dF(2.f, 2.f), quad->filters_scale); |
| 135 | } |
| 136 | |
Bo Liu | 9636833 | 2017-06-15 19:52:43 | [diff] [blame] | 137 | TEST(RenderSurfaceLayerImplTest, ResourcelessAppendQuadsSkipMask) { |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 138 | std::unique_ptr<viz::RenderPass> render_pass = |
sunxd | 48493dc | 2017-10-12 20:25:14 | [diff] [blame] | 139 | DoAppendQuadsWithScaledMask(DRAW_MODE_RESOURCELESS_SOFTWARE, 1.f, |
| 140 | Layer::LayerMaskType::SINGLE_TEXTURE_MASK); |
Bo Liu | 9636833 | 2017-06-15 19:52:43 | [diff] [blame] | 141 | DCHECK(render_pass->quad_list.front()); |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 142 | const viz::RenderPassDrawQuad* quad = |
| 143 | viz::RenderPassDrawQuad::MaterialCast(render_pass->quad_list.front()); |
Bo Liu | 9636833 | 2017-06-15 19:52:43 | [diff] [blame] | 144 | EXPECT_EQ(0u, quad->mask_resource_id()); |
| 145 | } |
| 146 | |
sunxd | 48493dc | 2017-10-12 20:25:14 | [diff] [blame] | 147 | TEST(RenderSurfaceLayerImplTest, |
| 148 | AppendQuadsWithSolidColorMaskAndDeviceScaleFactor) { |
| 149 | std::unique_ptr<viz::RenderPass> render_pass = DoAppendQuadsWithScaledMask( |
| 150 | DRAW_MODE_HARDWARE, 2.f, Layer::LayerMaskType::MULTI_TEXTURE_MASK); |
| 151 | DCHECK(render_pass->quad_list.front()); |
| 152 | const viz::RenderPassDrawQuad* quad = |
| 153 | viz::RenderPassDrawQuad::MaterialCast(render_pass->quad_list.front()); |
| 154 | EXPECT_EQ(gfx::Transform(), |
| 155 | quad->shared_quad_state->quad_to_target_transform); |
| 156 | LayerTestCommon::VerifyQuadsExactlyCoverRect( |
| 157 | render_pass->quad_list, quad->shared_quad_state->quad_layer_rect); |
| 158 | } |
| 159 | |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 160 | } // namespace |
| 161 | } // namespace cc |