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. 4 5 #include "build/build_config.h" 6 #include "cc/layers/content_layer.h" 7 #include "cc/layers/content_layer_client.h" 8 #include "cc/layers/image_layer.h" 9 #include "cc/layers/solid_color_layer.h" 10 #include "cc/test/layer_tree_pixel_test.h" 11 #include "cc/test/pixel_comparator.h" 12 13 #if !defined(OS_ANDROID) 14 15 namespace cc { 16 namespace { 17 18 class LayerTreeHostMasksPixelTest : public LayerTreePixelTest {}; 19 20 class MaskContentLayerClient : public cc::ContentLayerClient { 21 public: 22 MaskContentLayerClient() {} 23 virtual ~MaskContentLayerClient() {} 24 25 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {} 26 27 virtual void PaintContents(SkCanvas* canvas, 28 gfx::Rect rect, 29 gfx::RectF* opaque_rect) OVERRIDE { 30 SkPaint paint; 31 paint.setStyle(SkPaint::kStroke_Style); 32 paint.setStrokeWidth(SkIntToScalar(2)); 33 paint.setColor(SK_ColorWHITE); 34 35 canvas->clear(SK_ColorTRANSPARENT); 36 while (!rect.IsEmpty()) { 37 rect.Inset(3, 3, 2, 2); 38 canvas->drawRect( 39 SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()), 40 paint); 41 rect.Inset(3, 3, 2, 2); 42 } 43 } 44 }; 45 46 TEST_F(LayerTreeHostMasksPixelTest, MaskOfLayer) { 47 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer( 48 gfx::Rect(200, 200), SK_ColorWHITE); 49 50 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayerWithBorder( 51 gfx::Rect(50, 50, 100, 100), kCSSGreen, 1, SK_ColorBLACK); 52 background->AddChild(green); 53 54 MaskContentLayerClient client; 55 scoped_refptr<ContentLayer> mask = ContentLayer::Create(&client); 56 mask->SetBounds(gfx::Size(100, 100)); 57 mask->SetIsDrawable(true); 58 mask->SetIsMask(true); 59 green->SetMaskLayer(mask.get()); 60 61 this->impl_side_painting_ = false; 62 RunPixelTest(GL_WITH_BITMAP, 63 background, 64 base::FilePath(FILE_PATH_LITERAL("mask_of_layer.png"))); 65 } 66 67 TEST_F(LayerTreeHostMasksPixelTest, ImageMaskOfLayer) { 68 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer( 69 gfx::Rect(200, 200), SK_ColorWHITE); 70 71 scoped_refptr<ImageLayer> mask = ImageLayer::Create(); 72 mask->SetIsDrawable(true); 73 mask->SetIsMask(true); 74 mask->SetBounds(gfx::Size(100, 100)); 75 76 SkBitmap bitmap; 77 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 400, 400); 78 bitmap.allocPixels(); 79 SkCanvas canvas(bitmap); 80 canvas.scale(SkIntToScalar(4), SkIntToScalar(4)); 81 MaskContentLayerClient client; 82 client.PaintContents(&canvas, 83 gfx::Rect(100, 100), 84 NULL); 85 mask->SetBitmap(bitmap); 86 87 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayerWithBorder( 88 gfx::Rect(50, 50, 100, 100), kCSSGreen, 1, SK_ColorBLACK); 89 green->SetMaskLayer(mask.get()); 90 background->AddChild(green); 91 92 this->impl_side_painting_ = false; 93 RunPixelTest(GL_WITH_BITMAP, 94 background, 95 base::FilePath(FILE_PATH_LITERAL("image_mask_of_layer.png"))); 96 } 97 98 TEST_F(LayerTreeHostMasksPixelTest, MaskOfClippedLayer) { 99 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer( 100 gfx::Rect(200, 200), SK_ColorWHITE); 101 102 // Clip to the top half of the green layer. 103 scoped_refptr<Layer> clip = Layer::Create(); 104 clip->SetAnchorPoint(gfx::PointF(0.f, 0.f)); 105 clip->SetPosition(gfx::Point(0, 0)); 106 clip->SetBounds(gfx::Size(200, 100)); 107 clip->SetMasksToBounds(true); 108 background->AddChild(clip); 109 110 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayerWithBorder( 111 gfx::Rect(50, 50, 100, 100), kCSSGreen, 1, SK_ColorBLACK); 112 clip->AddChild(green); 113 114 MaskContentLayerClient client; 115 scoped_refptr<ContentLayer> mask = ContentLayer::Create(&client); 116 mask->SetBounds(gfx::Size(100, 100)); 117 mask->SetIsDrawable(true); 118 mask->SetIsMask(true); 119 green->SetMaskLayer(mask.get()); 120 121 this->impl_side_painting_ = false; 122 RunPixelTest(GL_WITH_BITMAP, 123 background, 124 base::FilePath(FILE_PATH_LITERAL("mask_of_clipped_layer.png"))); 125 } 126 127 TEST_F(LayerTreeHostMasksPixelTest, MaskWithReplica) { 128 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer( 129 gfx::Rect(200, 200), SK_ColorWHITE); 130 131 MaskContentLayerClient client; 132 scoped_refptr<ContentLayer> mask = ContentLayer::Create(&client); 133 mask->SetBounds(gfx::Size(100, 100)); 134 mask->SetIsDrawable(true); 135 mask->SetIsMask(true); 136 137 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayerWithBorder( 138 gfx::Rect(0, 0, 100, 100), kCSSGreen, 1, SK_ColorBLACK); 139 background->AddChild(green); 140 green->SetMaskLayer(mask.get()); 141 142 gfx::Transform replica_transform; 143 replica_transform.Rotate(-90.0); 144 145 scoped_refptr<Layer> replica = Layer::Create(); 146 replica->SetAnchorPoint(gfx::PointF(0.5f, 0.5f)); 147 replica->SetPosition(gfx::Point(100, 100)); 148 replica->SetTransform(replica_transform); 149 green->SetReplicaLayer(replica.get()); 150 151 this->impl_side_painting_ = false; 152 RunPixelTest(GL_WITH_BITMAP, 153 background, 154 base::FilePath(FILE_PATH_LITERAL("mask_with_replica.png"))); 155 } 156 157 TEST_F(LayerTreeHostMasksPixelTest, MaskWithReplicaOfClippedLayer) { 158 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer( 159 gfx::Rect(200, 200), SK_ColorWHITE); 160 161 MaskContentLayerClient client; 162 scoped_refptr<ContentLayer> mask = ContentLayer::Create(&client); 163 mask->SetBounds(gfx::Size(100, 100)); 164 mask->SetIsDrawable(true); 165 mask->SetIsMask(true); 166 167 // Clip to the bottom half of the green layer, and the left half of the 168 // replica. 169 scoped_refptr<Layer> clip = Layer::Create(); 170 clip->SetAnchorPoint(gfx::PointF(0.f, 0.f)); 171 clip->SetPosition(gfx::Point(0, 50)); 172 clip->SetBounds(gfx::Size(150, 150)); 173 clip->SetMasksToBounds(true); 174 background->AddChild(clip); 175 176 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayerWithBorder( 177 gfx::Rect(0, -50, 100, 100), kCSSGreen, 1, SK_ColorBLACK); 178 clip->AddChild(green); 179 green->SetMaskLayer(mask.get()); 180 181 gfx::Transform replica_transform; 182 replica_transform.Rotate(-90.0); 183 184 scoped_refptr<Layer> replica = Layer::Create(); 185 replica->SetAnchorPoint(gfx::PointF(0.5f, 0.5f)); 186 replica->SetPosition(gfx::Point(100, 100)); 187 replica->SetTransform(replica_transform); 188 green->SetReplicaLayer(replica.get()); 189 190 this->impl_side_painting_ = false; 191 RunPixelTest(GL_WITH_BITMAP, 192 background, 193 base::FilePath(FILE_PATH_LITERAL( 194 "mask_with_replica_of_clipped_layer.png"))); 195 } 196 197 TEST_F(LayerTreeHostMasksPixelTest, MaskOfReplica) { 198 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer( 199 gfx::Rect(200, 200), SK_ColorWHITE); 200 201 MaskContentLayerClient client; 202 scoped_refptr<ContentLayer> mask = ContentLayer::Create(&client); 203 mask->SetBounds(gfx::Size(100, 100)); 204 mask->SetIsDrawable(true); 205 mask->SetIsMask(true); 206 207 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayerWithBorder( 208 gfx::Rect(50, 0, 100, 100), kCSSGreen, 1, SK_ColorBLACK); 209 background->AddChild(green); 210 211 scoped_refptr<SolidColorLayer> orange = CreateSolidColorLayer( 212 gfx::Rect(-50, 50, 50, 50), kCSSOrange); 213 green->AddChild(orange); 214 215 gfx::Transform replica_transform; 216 replica_transform.Rotate(180.0); 217 replica_transform.Translate(100.0, 0.0); 218 219 scoped_refptr<Layer> replica = Layer::Create(); 220 replica->SetAnchorPoint(gfx::PointF(1.f, 1.f)); 221 replica->SetPosition(gfx::Point()); 222 replica->SetTransform(replica_transform); 223 replica->SetMaskLayer(mask.get()); 224 green->SetReplicaLayer(replica.get()); 225 226 this->impl_side_painting_ = false; 227 RunPixelTest(GL_WITH_BITMAP, 228 background, 229 base::FilePath(FILE_PATH_LITERAL("mask_of_replica.png"))); 230 } 231 232 TEST_F(LayerTreeHostMasksPixelTest, MaskOfReplicaOfClippedLayer) { 233 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer( 234 gfx::Rect(200, 200), SK_ColorWHITE); 235 236 MaskContentLayerClient client; 237 scoped_refptr<ContentLayer> mask = ContentLayer::Create(&client); 238 mask->SetBounds(gfx::Size(100, 100)); 239 mask->SetIsDrawable(true); 240 mask->SetIsMask(true); 241 242 // Clip to the bottom 3/4 of the green layer, and the top 3/4 of the replica. 243 scoped_refptr<Layer> clip = Layer::Create(); 244 clip->SetAnchorPoint(gfx::PointF(0.f, 0.f)); 245 clip->SetPosition(gfx::Point(0, 25)); 246 clip->SetBounds(gfx::Size(200, 150)); 247 clip->SetMasksToBounds(true); 248 background->AddChild(clip); 249 250 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayerWithBorder( 251 gfx::Rect(50, -25, 100, 100), kCSSGreen, 1, SK_ColorBLACK); 252 clip->AddChild(green); 253 254 scoped_refptr<SolidColorLayer> orange = CreateSolidColorLayer( 255 gfx::Rect(-50, 50, 50, 50), kCSSOrange); 256 green->AddChild(orange); 257 258 gfx::Transform replica_transform; 259 replica_transform.Rotate(180.0); 260 replica_transform.Translate(100.0, 0.0); 261 262 scoped_refptr<Layer> replica = Layer::Create(); 263 replica->SetAnchorPoint(gfx::PointF(1.f, 1.f)); 264 replica->SetPosition(gfx::Point()); 265 replica->SetTransform(replica_transform); 266 replica->SetMaskLayer(mask.get()); 267 green->SetReplicaLayer(replica.get()); 268 269 this->impl_side_painting_ = false; 270 RunPixelTest(GL_WITH_BITMAP, 271 background, 272 base::FilePath(FILE_PATH_LITERAL( 273 "mask_of_replica_of_clipped_layer.png"))); 274 } 275 276 } // namespace 277 } // namespace cc 278 279 #endif // OS_ANDROID 280