solid_color_layer_impl_unittest.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/solid_color_layer_impl.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/append_quads_data.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/solid_color_layer.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/quads/solid_color_draw_quad.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/test/fake_impl_proxy.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/test/fake_layer_tree_host_impl.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/test/layer_test_common.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/test/mock_quad_culler.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/trees/single_thread_proxy.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace cc {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(SolidColorLayerImplTest, VerifyTilingCompleteAndNoOverlap) {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockQuadCuller quad_culler;
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Size layer_size = gfx::Size(800, 600);
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeImplProxy proxy;
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeLayerTreeHostImpl host_impl(&proxy);
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<SolidColorLayerImpl> layer =
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SolidColorLayerImpl::Create(host_impl.active_tree(), 1);
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().visible_content_rect = visible_content_rect;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetBounds(layer_size);
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetContentBounds(layer_size);
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->CreateRenderSurface();
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().render_target = layer.get();
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AppendQuadsData data;
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->AppendQuads(&quad_culler, &data);
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerTestCommon::VerifyQuadsExactlyCoverRect(quad_culler.quad_list(),
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                               visible_content_rect);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(SolidColorLayerImplTest, VerifyCorrectBackgroundColorInQuad) {
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SkColor test_color = 0xFFA55AFF;
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockQuadCuller quad_culler;
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Size layer_size = gfx::Size(100, 100);
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeImplProxy proxy;
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeLayerTreeHostImpl host_impl(&proxy);
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<SolidColorLayerImpl> layer =
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SolidColorLayerImpl::Create(host_impl.active_tree(), 1);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().visible_content_rect = visible_content_rect;
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetBounds(layer_size);
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetContentBounds(layer_size);
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetBackgroundColor(test_color);
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->CreateRenderSurface();
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().render_target = layer.get();
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AppendQuadsData data;
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->AppendQuads(&quad_culler, &data);
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_EQ(quad_culler.quad_list().size(), 1U);
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(SolidColorDrawQuad::MaterialCast(quad_culler.quad_list()[0])->color,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            test_color);
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(SolidColorLayerImplTest, VerifyCorrectOpacityInQuad) {
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const float opacity = 0.5f;
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockQuadCuller quad_culler;
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Size layer_size = gfx::Size(100, 100);
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeImplProxy proxy;
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeLayerTreeHostImpl host_impl(&proxy);
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<SolidColorLayerImpl> layer =
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SolidColorLayerImpl::Create(host_impl.active_tree(), 1);
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().visible_content_rect = visible_content_rect;
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetBounds(layer_size);
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetContentBounds(layer_size);
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().opacity = opacity;
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->CreateRenderSurface();
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().render_target = layer.get();
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AppendQuadsData data;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->AppendQuads(&quad_culler, &data);
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_EQ(quad_culler.quad_list().size(), 1U);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(opacity,
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            SolidColorDrawQuad::MaterialCast(quad_culler.quad_list()[0])
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                ->opacity());
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(SolidColorLayerImplTest, VerifyOpaqueRect) {
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeImplProxy proxy;
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FakeLayerTreeHostImpl host_impl(&proxy);
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Size layer_size = gfx::Size(100, 100);
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<SolidColorLayer> layer = SolidColorLayer::Create();
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetBounds(layer_size);
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetForceRenderSurface(true);
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<Layer> root = Layer::Create();
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  root->AddChild(layer);
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
112ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  RenderSurfaceLayerList render_surface_layer_list;
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LayerTreeHostCommon::CalculateDrawProperties(root.get(),
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                               gfx::Size(500, 500),
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                               gfx::Transform(),
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                               1.f,
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                               1.f,
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                               NULL,
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                               1024,
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                               false,
121b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                               false,
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                               &render_surface_layer_list);
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(layer->contents_opaque());
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetBackgroundColor(SkColorSetARGBInline(255, 10, 20, 30));
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(layer->contents_opaque());
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<SolidColorLayerImpl> layer_impl =
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SolidColorLayerImpl::Create(host_impl.active_tree(), layer->id());
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer->PushPropertiesTo(layer_impl.get());
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The impl layer should call itself opaque as well.
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_TRUE(layer_impl->contents_opaque());
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Impl layer has 1 opacity, and the color is opaque, so the opaque_rect
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // should be the full tile.
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_impl->draw_properties().opacity = 1;
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MockQuadCuller quad_culler;
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AppendQuadsData data;
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_impl->AppendQuads(&quad_culler, &data);
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ASSERT_EQ(quad_culler.quad_list().size(), 1U);
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(visible_content_rect.ToString(),
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              quad_culler.quad_list()[0]->opaque_rect.ToString());
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(layer->contents_opaque());
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->SetBackgroundColor(SkColorSetARGBInline(254, 10, 20, 30));
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(layer->contents_opaque());
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<SolidColorLayerImpl> layer_impl =
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SolidColorLayerImpl::Create(host_impl.active_tree(), layer->id());
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer->PushPropertiesTo(layer_impl.get());
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The impl layer should callnot itself opaque anymore.
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_FALSE(layer_impl->contents_opaque());
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Impl layer has 1 opacity, but the color is not opaque, so the opaque_rect
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // should be empty.
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_impl->draw_properties().opacity = 1;
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MockQuadCuller quad_culler;
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AppendQuadsData data;
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_impl->AppendQuads(&quad_culler, &data);
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ASSERT_EQ(quad_culler.quad_list().size(), 1U);
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(gfx::Rect().ToString(),
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              quad_culler.quad_list()[0]->opaque_rect.ToString());
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace cc
175