solid_color_layer_impl_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 5#include "cc/layers/solid_color_layer_impl.h" 6 7#include <vector> 8 9#include "cc/layers/append_quads_data.h" 10#include "cc/layers/solid_color_layer.h" 11#include "cc/quads/solid_color_draw_quad.h" 12#include "cc/test/fake_impl_proxy.h" 13#include "cc/test/fake_layer_tree_host.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 20namespace cc { 21namespace { 22 23TEST(SolidColorLayerImplTest, VerifyTilingCompleteAndNoOverlap) { 24 MockOcclusionTracker<LayerImpl> occlusion_tracker; 25 scoped_ptr<RenderPass> render_pass = RenderPass::Create(); 26 27 gfx::Size layer_size = gfx::Size(800, 600); 28 gfx::Rect visible_content_rect = gfx::Rect(layer_size); 29 30 FakeImplProxy proxy; 31 TestSharedBitmapManager shared_bitmap_manager; 32 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); 33 scoped_ptr<SolidColorLayerImpl> layer = 34 SolidColorLayerImpl::Create(host_impl.active_tree(), 1); 35 layer->draw_properties().visible_content_rect = visible_content_rect; 36 layer->SetBounds(layer_size); 37 layer->SetContentBounds(layer_size); 38 layer->CreateRenderSurface(); 39 layer->draw_properties().render_target = layer.get(); 40 41 AppendQuadsData data; 42 layer->AppendQuads(render_pass.get(), occlusion_tracker, &data); 43 44 LayerTestCommon::VerifyQuadsExactlyCoverRect(render_pass->quad_list, 45 visible_content_rect); 46} 47 48TEST(SolidColorLayerImplTest, VerifyCorrectBackgroundColorInQuad) { 49 SkColor test_color = 0xFFA55AFF; 50 51 MockOcclusionTracker<LayerImpl> occlusion_tracker; 52 scoped_ptr<RenderPass> render_pass = RenderPass::Create(); 53 54 gfx::Size layer_size = gfx::Size(100, 100); 55 gfx::Rect visible_content_rect = gfx::Rect(layer_size); 56 57 FakeImplProxy proxy; 58 TestSharedBitmapManager shared_bitmap_manager; 59 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); 60 scoped_ptr<SolidColorLayerImpl> layer = 61 SolidColorLayerImpl::Create(host_impl.active_tree(), 1); 62 layer->draw_properties().visible_content_rect = visible_content_rect; 63 layer->SetBounds(layer_size); 64 layer->SetContentBounds(layer_size); 65 layer->SetBackgroundColor(test_color); 66 layer->CreateRenderSurface(); 67 layer->draw_properties().render_target = layer.get(); 68 69 AppendQuadsData data; 70 layer->AppendQuads(render_pass.get(), occlusion_tracker, &data); 71 72 ASSERT_EQ(render_pass->quad_list.size(), 1U); 73 EXPECT_EQ( 74 SolidColorDrawQuad::MaterialCast(render_pass->quad_list.front())->color, 75 test_color); 76} 77 78TEST(SolidColorLayerImplTest, VerifyCorrectOpacityInQuad) { 79 const float opacity = 0.5f; 80 81 MockOcclusionTracker<LayerImpl> occlusion_tracker; 82 scoped_ptr<RenderPass> render_pass = RenderPass::Create(); 83 84 gfx::Size layer_size = gfx::Size(100, 100); 85 gfx::Rect visible_content_rect = gfx::Rect(layer_size); 86 87 FakeImplProxy proxy; 88 TestSharedBitmapManager shared_bitmap_manager; 89 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); 90 scoped_ptr<SolidColorLayerImpl> layer = 91 SolidColorLayerImpl::Create(host_impl.active_tree(), 1); 92 layer->draw_properties().visible_content_rect = visible_content_rect; 93 layer->SetBounds(layer_size); 94 layer->SetContentBounds(layer_size); 95 layer->draw_properties().opacity = opacity; 96 layer->CreateRenderSurface(); 97 layer->draw_properties().render_target = layer.get(); 98 99 AppendQuadsData data; 100 layer->AppendQuads(render_pass.get(), occlusion_tracker, &data); 101 102 ASSERT_EQ(render_pass->quad_list.size(), 1U); 103 EXPECT_EQ(opacity, 104 SolidColorDrawQuad::MaterialCast(render_pass->quad_list.front()) 105 ->opacity()); 106} 107 108TEST(SolidColorLayerImplTest, VerifyOpaqueRect) { 109 gfx::Size layer_size = gfx::Size(100, 100); 110 gfx::Rect visible_content_rect = gfx::Rect(layer_size); 111 112 scoped_refptr<SolidColorLayer> layer = SolidColorLayer::Create(); 113 layer->SetBounds(layer_size); 114 layer->SetForceRenderSurface(true); 115 116 scoped_refptr<Layer> root = Layer::Create(); 117 root->AddChild(layer); 118 119 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D); 120 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client); 121 host->SetRootLayer(root); 122 123 RenderSurfaceLayerList render_surface_layer_list; 124 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs( 125 root.get(), gfx::Size(500, 500), &render_surface_layer_list); 126 LayerTreeHostCommon::CalculateDrawProperties(&inputs); 127 128 EXPECT_FALSE(layer->contents_opaque()); 129 layer->SetBackgroundColor(SkColorSetARGBInline(255, 10, 20, 30)); 130 EXPECT_TRUE(layer->contents_opaque()); 131 { 132 scoped_ptr<SolidColorLayerImpl> layer_impl = 133 SolidColorLayerImpl::Create(host->host_impl()->active_tree(), 134 layer->id()); 135 layer->PushPropertiesTo(layer_impl.get()); 136 137 // The impl layer should call itself opaque as well. 138 EXPECT_TRUE(layer_impl->contents_opaque()); 139 140 // Impl layer has 1 opacity, and the color is opaque, so the opaque_rect 141 // should be the full tile. 142 layer_impl->draw_properties().opacity = 1; 143 144 MockOcclusionTracker<LayerImpl> occlusion_tracker; 145 scoped_ptr<RenderPass> render_pass = RenderPass::Create(); 146 147 AppendQuadsData data; 148 layer_impl->AppendQuads(render_pass.get(), occlusion_tracker, &data); 149 150 ASSERT_EQ(render_pass->quad_list.size(), 1U); 151 EXPECT_EQ(visible_content_rect.ToString(), 152 render_pass->quad_list.front()->opaque_rect.ToString()); 153 } 154 155 EXPECT_TRUE(layer->contents_opaque()); 156 layer->SetBackgroundColor(SkColorSetARGBInline(254, 10, 20, 30)); 157 EXPECT_FALSE(layer->contents_opaque()); 158 { 159 scoped_ptr<SolidColorLayerImpl> layer_impl = 160 SolidColorLayerImpl::Create(host->host_impl()->active_tree(), 161 layer->id()); 162 layer->PushPropertiesTo(layer_impl.get()); 163 164 // The impl layer should callnot itself opaque anymore. 165 EXPECT_FALSE(layer_impl->contents_opaque()); 166 167 // Impl layer has 1 opacity, but the color is not opaque, so the opaque_rect 168 // should be empty. 169 layer_impl->draw_properties().opacity = 1; 170 171 MockOcclusionTracker<LayerImpl> occlusion_tracker; 172 scoped_ptr<RenderPass> render_pass = RenderPass::Create(); 173 174 AppendQuadsData data; 175 layer_impl->AppendQuads(render_pass.get(), occlusion_tracker, &data); 176 177 ASSERT_EQ(render_pass->quad_list.size(), 1U); 178 EXPECT_EQ(gfx::Rect().ToString(), 179 render_pass->quad_list.front()->opaque_rect.ToString()); 180 } 181} 182 183TEST(SolidColorLayerImplTest, Occlusion) { 184 gfx::Size layer_size(1000, 1000); 185 gfx::Size viewport_size(1000, 1000); 186 187 LayerTestCommon::LayerImplTest impl; 188 189 SolidColorLayerImpl* solid_color_layer_impl = 190 impl.AddChildToRoot<SolidColorLayerImpl>(); 191 solid_color_layer_impl->SetBackgroundColor(SkColorSetARGB(255, 10, 20, 30)); 192 solid_color_layer_impl->SetBounds(layer_size); 193 solid_color_layer_impl->SetContentBounds(layer_size); 194 solid_color_layer_impl->SetDrawsContent(true); 195 196 impl.CalcDrawProps(viewport_size); 197 198 { 199 SCOPED_TRACE("No occlusion"); 200 gfx::Rect occluded; 201 impl.AppendQuadsWithOcclusion(solid_color_layer_impl, occluded); 202 203 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), 204 gfx::Rect(layer_size)); 205 EXPECT_EQ(16u, impl.quad_list().size()); 206 } 207 208 { 209 SCOPED_TRACE("Full occlusion"); 210 gfx::Rect occluded(solid_color_layer_impl->visible_content_rect()); 211 impl.AppendQuadsWithOcclusion(solid_color_layer_impl, occluded); 212 213 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect()); 214 EXPECT_EQ(impl.quad_list().size(), 0u); 215 } 216 217 { 218 SCOPED_TRACE("Partial occlusion"); 219 gfx::Rect occluded(200, 200, 256 * 3, 256 * 3); 220 impl.AppendQuadsWithOcclusion(solid_color_layer_impl, occluded); 221 222 size_t partially_occluded_count = 0; 223 LayerTestCommon::VerifyQuadsAreOccluded( 224 impl.quad_list(), occluded, &partially_occluded_count); 225 // 4 quads are completely occluded, 8 are partially occluded. 226 EXPECT_EQ(16u - 4u, impl.quad_list().size()); 227 EXPECT_EQ(8u, partially_occluded_count); 228 } 229} 230 231} // namespace 232} // namespace cc 233