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.
5 #include "cc/layers/append_quads_data.h"
6 #include "cc/layers/ui_resource_layer_impl.h"
7 #include "cc/quads/draw_quad.h"
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_output_surface.h"
13 #include "cc/test/fake_ui_resource_layer_tree_host_impl.h"
14 #include "cc/test/layer_test_common.h"
15 #include "cc/test/test_shared_bitmap_manager.h"
16 #include "cc/trees/single_thread_proxy.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/gfx/transform.h"
24 scoped_ptr
<UIResourceLayerImpl
> GenerateUIResourceLayer(
25 FakeUIResourceLayerTreeHostImpl
* host_impl
,
26 const gfx::Size
& bitmap_size
,
27 const gfx::Size
& layer_size
,
30 gfx::Rect
visible_content_rect(layer_size
);
31 scoped_ptr
<UIResourceLayerImpl
> layer
=
32 UIResourceLayerImpl::Create(host_impl
->active_tree(), 1);
33 layer
->draw_properties().visible_content_rect
= visible_content_rect
;
34 layer
->SetBounds(layer_size
);
35 layer
->SetContentBounds(layer_size
);
36 layer
->SetHasRenderSurface(true);
37 layer
->draw_properties().render_target
= layer
.get();
39 UIResourceBitmap
bitmap(bitmap_size
, opaque
);
41 host_impl
->CreateUIResource(uid
, bitmap
);
42 layer
->SetUIResourceId(uid
);
47 void QuadSizeTest(scoped_ptr
<UIResourceLayerImpl
> layer
,
48 size_t expected_quad_size
) {
49 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
52 layer
->AppendQuads(render_pass
.get(), &data
);
55 const QuadList
& quads
= render_pass
->quad_list
;
56 EXPECT_EQ(expected_quad_size
, quads
.size());
59 TEST(UIResourceLayerImplTest
, VerifyDrawQuads
) {
61 TestSharedBitmapManager shared_bitmap_manager
;
62 FakeUIResourceLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
63 host_impl
.InitializeRenderer(FakeOutputSurface::Create3d());
65 // Make sure we're appending quads when there are valid values.
66 gfx::Size
bitmap_size(100, 100);
67 gfx::Size
layer_size(100, 100);;
68 size_t expected_quad_size
= 1;
71 scoped_ptr
<UIResourceLayerImpl
> layer
= GenerateUIResourceLayer(&host_impl
,
76 QuadSizeTest(layer
.Pass(), expected_quad_size
);
78 // Make sure we're not appending quads when there are invalid values.
79 expected_quad_size
= 0;
81 layer
= GenerateUIResourceLayer(&host_impl
,
86 QuadSizeTest(layer
.Pass(), expected_quad_size
);
89 void OpaqueBoundsTest(scoped_ptr
<UIResourceLayerImpl
> layer
,
90 const gfx::Rect
& expected_opaque_bounds
) {
91 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
94 layer
->AppendQuads(render_pass
.get(), &data
);
97 const QuadList
& quads
= render_pass
->quad_list
;
98 EXPECT_GE(quads
.size(), (size_t)0);
99 gfx::Rect opaque_rect
= quads
.front()->opaque_rect
;
100 EXPECT_EQ(expected_opaque_bounds
, opaque_rect
);
103 TEST(UIResourceLayerImplTest
, VerifySetOpaqueOnSkBitmap
) {
105 TestSharedBitmapManager shared_bitmap_manager
;
106 FakeUIResourceLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
107 host_impl
.InitializeRenderer(FakeOutputSurface::Create3d());
109 gfx::Size
bitmap_size(100, 100);
110 gfx::Size
layer_size(100, 100);;
112 UIResourceId uid
= 1;
113 scoped_ptr
<UIResourceLayerImpl
> layer
= GenerateUIResourceLayer(&host_impl
,
118 gfx::Rect expected_opaque_bounds
;
119 OpaqueBoundsTest(layer
.Pass(), expected_opaque_bounds
);
122 layer
= GenerateUIResourceLayer(&host_impl
,
127 expected_opaque_bounds
= gfx::Rect(layer
->bounds());
128 OpaqueBoundsTest(layer
.Pass(), expected_opaque_bounds
);
131 TEST(UIResourceLayerImplTest
, VerifySetOpaqueOnLayer
) {
133 TestSharedBitmapManager shared_bitmap_manager
;
134 FakeUIResourceLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
135 host_impl
.InitializeRenderer(FakeOutputSurface::Create3d());
137 gfx::Size
bitmap_size(100, 100);
138 gfx::Size
layer_size(100, 100);
139 bool skbitmap_opaque
= false;
140 UIResourceId uid
= 1;
141 scoped_ptr
<UIResourceLayerImpl
> layer
= GenerateUIResourceLayer(
142 &host_impl
, bitmap_size
, layer_size
, skbitmap_opaque
, uid
);
143 layer
->SetContentsOpaque(false);
144 gfx::Rect expected_opaque_bounds
;
145 OpaqueBoundsTest(layer
.Pass(), expected_opaque_bounds
);
147 layer
= GenerateUIResourceLayer(
148 &host_impl
, bitmap_size
, layer_size
, skbitmap_opaque
, uid
);
149 layer
->SetContentsOpaque(true);
150 expected_opaque_bounds
= gfx::Rect(layer
->bounds());
151 OpaqueBoundsTest(layer
.Pass(), expected_opaque_bounds
);
154 TEST(UIResourceLayerImplTest
, Occlusion
) {
155 gfx::Size
layer_size(1000, 1000);
156 gfx::Size
viewport_size(1000, 1000);
158 LayerTestCommon::LayerImplTest impl
;
161 sk_bitmap
.allocN32Pixels(10, 10);
162 sk_bitmap
.setImmutable();
163 UIResourceId uid
= 5;
164 UIResourceBitmap
bitmap(sk_bitmap
);
165 impl
.host_impl()->CreateUIResource(uid
, bitmap
);
167 UIResourceLayerImpl
* ui_resource_layer_impl
=
168 impl
.AddChildToRoot
<UIResourceLayerImpl
>();
169 ui_resource_layer_impl
->SetBounds(layer_size
);
170 ui_resource_layer_impl
->SetContentBounds(layer_size
);
171 ui_resource_layer_impl
->SetDrawsContent(true);
172 ui_resource_layer_impl
->SetUIResourceId(uid
);
174 impl
.CalcDrawProps(viewport_size
);
177 SCOPED_TRACE("No occlusion");
179 impl
.AppendQuadsWithOcclusion(ui_resource_layer_impl
, occluded
);
181 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(),
182 gfx::Rect(layer_size
));
183 EXPECT_EQ(1u, impl
.quad_list().size());
187 SCOPED_TRACE("Full occlusion");
188 gfx::Rect
occluded(ui_resource_layer_impl
->visible_content_rect());
189 impl
.AppendQuadsWithOcclusion(ui_resource_layer_impl
, occluded
);
191 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(), gfx::Rect());
192 EXPECT_EQ(impl
.quad_list().size(), 0u);
196 SCOPED_TRACE("Partial occlusion");
197 gfx::Rect
occluded(200, 0, 800, 1000);
198 impl
.AppendQuadsWithOcclusion(ui_resource_layer_impl
, occluded
);
200 size_t partially_occluded_count
= 0;
201 LayerTestCommon::VerifyQuadsAreOccluded(
202 impl
.quad_list(), occluded
, &partially_occluded_count
);
203 // The layer outputs one quad, which is partially occluded.
204 EXPECT_EQ(1u, impl
.quad_list().size());
205 EXPECT_EQ(1u, partially_occluded_count
);