cc: For debugging, show checkerboards with the tile border color
[chromium-blink-merge.git] / cc / layers / ui_resource_layer_impl_unittest.cc
blobe31d68ae6e59e740fcdd9706de39e262f9ea7d64
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/test/test_task_graph_runner.h"
17 #include "cc/trees/single_thread_proxy.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gfx/transform.h"
22 namespace cc {
23 namespace {
25 scoped_ptr<UIResourceLayerImpl> GenerateUIResourceLayer(
26 FakeUIResourceLayerTreeHostImpl* host_impl,
27 const gfx::Size& bitmap_size,
28 const gfx::Size& layer_size,
29 bool opaque,
30 UIResourceId uid) {
31 gfx::Rect visible_layer_rect(layer_size);
32 scoped_ptr<UIResourceLayerImpl> layer =
33 UIResourceLayerImpl::Create(host_impl->active_tree(), 1);
34 layer->draw_properties().visible_layer_rect = visible_layer_rect;
35 layer->SetBounds(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);
44 return layer.Pass();
47 void QuadSizeTest(scoped_ptr<UIResourceLayerImpl> layer,
48 size_t expected_quad_size) {
49 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
51 AppendQuadsData data;
52 layer->AppendQuads(render_pass.get(), &data);
54 // Verify quad rects
55 const QuadList& quads = render_pass->quad_list;
56 EXPECT_EQ(expected_quad_size, quads.size());
59 TEST(UIResourceLayerImplTest, VerifyDrawQuads) {
60 FakeImplProxy proxy;
61 TestSharedBitmapManager shared_bitmap_manager;
62 TestTaskGraphRunner task_graph_runner;
63 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
64 &task_graph_runner);
65 host_impl.InitializeRenderer(FakeOutputSurface::Create3d());
67 // Make sure we're appending quads when there are valid values.
68 gfx::Size bitmap_size(100, 100);
69 gfx::Size layer_size(100, 100);;
70 size_t expected_quad_size = 1;
71 bool opaque = true;
72 UIResourceId uid = 1;
73 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
74 bitmap_size,
75 layer_size,
76 opaque,
77 uid);
78 QuadSizeTest(layer.Pass(), expected_quad_size);
80 // Make sure we're not appending quads when there are invalid values.
81 expected_quad_size = 0;
82 uid = 0;
83 layer = GenerateUIResourceLayer(&host_impl,
84 bitmap_size,
85 layer_size,
86 opaque,
87 uid);
88 QuadSizeTest(layer.Pass(), expected_quad_size);
91 void OpaqueBoundsTest(scoped_ptr<UIResourceLayerImpl> layer,
92 const gfx::Rect& expected_opaque_bounds) {
93 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
95 AppendQuadsData data;
96 layer->AppendQuads(render_pass.get(), &data);
98 // Verify quad rects
99 const QuadList& quads = render_pass->quad_list;
100 EXPECT_GE(quads.size(), (size_t)0);
101 gfx::Rect opaque_rect = quads.front()->opaque_rect;
102 EXPECT_EQ(expected_opaque_bounds, opaque_rect);
105 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnSkBitmap) {
106 FakeImplProxy proxy;
107 TestSharedBitmapManager shared_bitmap_manager;
108 TestTaskGraphRunner task_graph_runner;
109 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
110 &task_graph_runner);
111 host_impl.InitializeRenderer(FakeOutputSurface::Create3d());
113 gfx::Size bitmap_size(100, 100);
114 gfx::Size layer_size(100, 100);;
115 bool opaque = false;
116 UIResourceId uid = 1;
117 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
118 bitmap_size,
119 layer_size,
120 opaque,
121 uid);
122 gfx::Rect expected_opaque_bounds;
123 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
125 opaque = true;
126 layer = GenerateUIResourceLayer(&host_impl,
127 bitmap_size,
128 layer_size,
129 opaque,
130 uid);
131 expected_opaque_bounds = gfx::Rect(layer->bounds());
132 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
135 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnLayer) {
136 FakeImplProxy proxy;
137 TestSharedBitmapManager shared_bitmap_manager;
138 TestTaskGraphRunner task_graph_runner;
139 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
140 &task_graph_runner);
141 host_impl.InitializeRenderer(FakeOutputSurface::Create3d());
143 gfx::Size bitmap_size(100, 100);
144 gfx::Size layer_size(100, 100);
145 bool skbitmap_opaque = false;
146 UIResourceId uid = 1;
147 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(
148 &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
149 layer->SetContentsOpaque(false);
150 gfx::Rect expected_opaque_bounds;
151 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
153 layer = GenerateUIResourceLayer(
154 &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
155 layer->SetContentsOpaque(true);
156 expected_opaque_bounds = gfx::Rect(layer->bounds());
157 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
160 TEST(UIResourceLayerImplTest, Occlusion) {
161 gfx::Size layer_size(1000, 1000);
162 gfx::Size viewport_size(1000, 1000);
164 LayerTestCommon::LayerImplTest impl;
166 SkBitmap sk_bitmap;
167 sk_bitmap.allocN32Pixels(10, 10);
168 sk_bitmap.setImmutable();
169 UIResourceId uid = 5;
170 UIResourceBitmap bitmap(sk_bitmap);
171 impl.host_impl()->CreateUIResource(uid, bitmap);
173 UIResourceLayerImpl* ui_resource_layer_impl =
174 impl.AddChildToRoot<UIResourceLayerImpl>();
175 ui_resource_layer_impl->SetBounds(layer_size);
176 ui_resource_layer_impl->SetDrawsContent(true);
177 ui_resource_layer_impl->SetUIResourceId(uid);
179 impl.CalcDrawProps(viewport_size);
182 SCOPED_TRACE("No occlusion");
183 gfx::Rect occluded;
184 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
186 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
187 gfx::Rect(layer_size));
188 EXPECT_EQ(1u, impl.quad_list().size());
192 SCOPED_TRACE("Full occlusion");
193 gfx::Rect occluded(ui_resource_layer_impl->visible_layer_rect());
194 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
196 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
197 EXPECT_EQ(impl.quad_list().size(), 0u);
201 SCOPED_TRACE("Partial occlusion");
202 gfx::Rect occluded(200, 0, 800, 1000);
203 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
205 size_t partially_occluded_count = 0;
206 LayerTestCommon::VerifyQuadsAreOccluded(
207 impl.quad_list(), occluded, &partially_occluded_count);
208 // The layer outputs one quad, which is partially occluded.
209 EXPECT_EQ(1u, impl.quad_list().size());
210 EXPECT_EQ(1u, partially_occluded_count);
214 } // namespace
215 } // namespace cc