1 // Copyright 2019 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/mirror_layer.h"
7 #include "cc/layers/solid_color_layer.h"
8 #include "cc/test/layer_tree_pixel_test.h"
9 #include "cc/test/pixel_comparator.h"
10 #include "ui/gfx/geometry/size.h"
11 #include "ui/gfx/transform_util.h"
12 
13 #if !defined(OS_ANDROID)
14 
15 namespace cc {
16 namespace {
17 
18 class LayerTreeHostMirrorPixelTest
19     : public LayerTreePixelTest,
20       public ::testing::WithParamInterface<viz::RendererType> {
21  protected:
LayerTreeHostMirrorPixelTest()22   LayerTreeHostMirrorPixelTest() : LayerTreePixelTest(renderer_type()) {}
23 
renderer_type() const24   viz::RendererType renderer_type() const { return GetParam(); }
25 };
26 
27 INSTANTIATE_TEST_SUITE_P(All,
28                          LayerTreeHostMirrorPixelTest,
29                          ::testing::ValuesIn(viz::GetRendererTypes()),
30                          ::testing::PrintToStringParamName());
31 
32 // Verifies that a mirror layer with a scale mirrors another layer correctly.
TEST_P(LayerTreeHostMirrorPixelTest,MirrorLayer)33 TEST_P(LayerTreeHostMirrorPixelTest, MirrorLayer) {
34   const float scale = 2.f;
35   gfx::Rect background_bounds(120, 180);
36   gfx::Rect mirrored_bounds(10, 10, 50, 50);
37   gfx::Rect mirror_bounds(10, 70, 100, 100);
38 
39   auto background = CreateSolidColorLayer(background_bounds, SK_ColorWHITE);
40 
41   auto mirrored_layer = CreateSolidColorLayerWithBorder(
42       mirrored_bounds, SK_ColorGREEN, 5, SK_ColorBLUE);
43 
44   auto mirror_layer = MirrorLayer::Create(mirrored_layer);
45   mirror_layer->SetIsDrawable(true);
46   mirror_layer->SetBounds(mirror_bounds.size());
47   mirror_layer->SetPosition(gfx::PointF(mirror_bounds.origin()));
48   mirror_layer->SetTransform(gfx::GetScaleTransform(gfx::Point(), scale));
49   background->AddChild(mirrored_layer);
50   background->AddChild(mirror_layer);
51 
52   if (use_software_renderer()) {
53     const bool discard_alpha = true;
54     const float error_pixels_percentage_limit = 3.f;
55     const float small_error_pixels_percentage_limit = 0.f;
56     const float avg_abs_error_limit = 65.f;
57     const int max_abs_error_limit = 120;
58     const int small_error_threshold = 0;
59     pixel_comparator_ = std::make_unique<FuzzyPixelComparator>(
60         discard_alpha, error_pixels_percentage_limit,
61         small_error_pixels_percentage_limit, avg_abs_error_limit,
62         max_abs_error_limit, small_error_threshold);
63   }
64 
65   if (use_skia_vulkan())
66     pixel_comparator_ = std::make_unique<FuzzyPixelOffByOneComparator>(true);
67 
68   RunPixelTest(background,
69                base::FilePath(FILE_PATH_LITERAL("mirror_layer.png")));
70 }
71 
72 }  // namespace
73 }  // namespace cc
74 
75 #endif  // !defined(OS_ANDROID)
76