1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 #include "LayerTransactionTest.h"
22
23 namespace android {
24
25 class EffectLayerTest : public LayerTransactionTest {
26 protected:
SetUp()27 virtual void SetUp() {
28 LayerTransactionTest::SetUp();
29 ASSERT_EQ(NO_ERROR, mClient->initCheck());
30
31 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
32 ASSERT_FALSE(ids.empty());
33 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
34 ASSERT_FALSE(display == nullptr);
35
36 mParentLayer = createColorLayer("Parent layer", Color::RED);
37 asTransaction([&](Transaction& t) {
38 t.setDisplayLayerStack(display, ui::DEFAULT_LAYER_STACK);
39 t.setLayer(mParentLayer, INT32_MAX - 2).show(mParentLayer);
40 t.setFlags(mParentLayer, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque);
41 });
42 }
43
TearDown()44 virtual void TearDown() {
45 LayerTransactionTest::TearDown();
46 mParentLayer = 0;
47 }
48
49 sp<SurfaceControl> mParentLayer;
50 };
51
TEST_F(EffectLayerTest,DefaultEffectLayerHasSolidBlackFill)52 TEST_F(EffectLayerTest, DefaultEffectLayerHasSolidBlackFill) {
53 sp<SurfaceControl> effectLayer =
54 mClient->createSurface(String8("Effect Layer"), 0 /* width */, 0 /* height */,
55 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect,
56 mParentLayer->getHandle());
57
58 EXPECT_NE(nullptr, effectLayer.get()) << "failed to create SurfaceControl";
59 asTransaction([&](Transaction& t) {
60 t.setCrop(effectLayer, Rect(0, 0, 400, 400));
61 t.show(effectLayer);
62 });
63
64 {
65 SCOPED_TRACE("Default effect Layer has solid black fill");
66 auto shot = screenshot();
67 shot->expectColor(Rect(0, 0, 400, 400), Color::BLACK);
68 }
69 }
70
TEST_F(EffectLayerTest,EffectLayerWithNoFill)71 TEST_F(EffectLayerTest, EffectLayerWithNoFill) {
72 sp<SurfaceControl> effectLayer =
73 mClient->createSurface(String8("Effect Layer"), 0 /* width */, 0 /* height */,
74 PIXEL_FORMAT_RGBA_8888,
75 ISurfaceComposerClient::eFXSurfaceEffect |
76 ISurfaceComposerClient::eNoColorFill,
77 mParentLayer->getHandle());
78
79 EXPECT_NE(nullptr, effectLayer.get()) << "failed to create SurfaceControl";
80 asTransaction([&](Transaction& t) {
81 t.setCrop(effectLayer, Rect(0, 0, 400, 400));
82 t.show(effectLayer);
83 });
84
85 {
86 SCOPED_TRACE("Effect layer with nofill option is transparent");
87 auto shot = screenshot();
88 shot->expectColor(Rect(0, 0, 400, 400), Color::RED);
89 }
90 }
91
TEST_F(EffectLayerTest,EffectLayerCanSetColor)92 TEST_F(EffectLayerTest, EffectLayerCanSetColor) {
93 sp<SurfaceControl> effectLayer =
94 mClient->createSurface(String8("Effect Layer"), 0 /* width */, 0 /* height */,
95 PIXEL_FORMAT_RGBA_8888,
96 ISurfaceComposerClient::eFXSurfaceEffect |
97 ISurfaceComposerClient::eNoColorFill,
98 mParentLayer->getHandle());
99
100 EXPECT_NE(nullptr, effectLayer.get()) << "failed to create SurfaceControl";
101 asTransaction([&](Transaction& t) {
102 t.setCrop(effectLayer, Rect(0, 0, 400, 400));
103 t.setColor(effectLayer,
104 half3{Color::GREEN.r / 255.0f, Color::GREEN.g / 255.0f,
105 Color::GREEN.b / 255.0f});
106 t.show(effectLayer);
107 });
108
109 {
110 SCOPED_TRACE("Effect Layer can set color");
111 auto shot = screenshot();
112 shot->expectColor(Rect(0, 0, 400, 400), Color::GREEN);
113 }
114 }
115
TEST_F(EffectLayerTest,BlurEffectLayerIsVisible)116 TEST_F(EffectLayerTest, BlurEffectLayerIsVisible) {
117 if (!deviceSupportsBlurs()) GTEST_SKIP();
118 if (!deviceUsesSkiaRenderEngine()) GTEST_SKIP();
119
120 const auto canvasSize = 256;
121
122 sp<SurfaceControl> leftLayer = createColorLayer("Left", Color::BLUE);
123 sp<SurfaceControl> rightLayer = createColorLayer("Right", Color::RED);
124 sp<SurfaceControl> blurLayer;
125 const auto leftRect = Rect(0, 0, canvasSize / 2, canvasSize);
126 const auto rightRect = Rect(canvasSize / 2, 0, canvasSize, canvasSize);
127 const auto blurRect = Rect(0, 0, canvasSize, canvasSize);
128
129 asTransaction([&](Transaction& t) {
130 t.setLayer(leftLayer, mLayerZBase + 1);
131 t.reparent(leftLayer, mParentLayer);
132 t.setCrop(leftLayer, leftRect);
133 t.setLayer(rightLayer, mLayerZBase + 2);
134 t.reparent(rightLayer, mParentLayer);
135 t.setCrop(rightLayer, rightRect);
136 t.show(leftLayer);
137 t.show(rightLayer);
138 });
139
140 {
141 auto shot = screenshot();
142 shot->expectColor(leftRect, Color::BLUE);
143 shot->expectColor(rightRect, Color::RED);
144 }
145
146 ASSERT_NO_FATAL_FAILURE(blurLayer = createColorLayer("BackgroundBlur", Color::TRANSPARENT));
147
148 const auto blurRadius = canvasSize / 4;
149 asTransaction([&](Transaction& t) {
150 t.setLayer(blurLayer, mLayerZBase + 3);
151 t.reparent(blurLayer, mParentLayer);
152 t.setBackgroundBlurRadius(blurLayer, blurRadius);
153 t.setCrop(blurLayer, blurRect);
154 t.setAlpha(blurLayer, 0.0f);
155 t.show(blurLayer);
156 });
157
158 {
159 auto shot = screenshot();
160
161 const auto stepSize = 1;
162 const auto expectedBlurAreaSize = blurRadius * 1.5f;
163 const auto blurAreaStartX = canvasSize / 2 - expectedBlurAreaSize / 2;
164 const auto blurAreaEndX = canvasSize / 2 + expectedBlurAreaSize / 2;
165 // testAreaEndY is needed because the setBackgroundBlurRadius API blurs everything behind
166 // the surface, which means it samples pixels from outside the canvasSize and we get some
167 // unexpected colors in the screenshot.
168 const auto testAreaEndY = canvasSize - blurRadius * 2;
169
170 Color previousColor;
171 Color currentColor;
172 for (int y = 0; y < testAreaEndY; y++) {
173 shot->checkPixel(0, y, /* r = */ 0, /* g = */ 0, /* b = */ 255);
174 previousColor = shot->getPixelColor(0, y);
175 for (int x = blurAreaStartX; x < blurAreaEndX; x += stepSize) {
176 currentColor = shot->getPixelColor(x, y);
177 ASSERT_GT(currentColor.r, previousColor.r);
178 ASSERT_LT(currentColor.b, previousColor.b);
179 ASSERT_EQ(0, currentColor.g);
180 }
181 shot->checkPixel(canvasSize - 1, y, 255, 0, 0);
182 }
183 }
184 }
185
TEST_F(EffectLayerTest,EffectLayerWithColorNoCrop)186 TEST_F(EffectLayerTest, EffectLayerWithColorNoCrop) {
187 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
188 ASSERT_FALSE(ids.empty());
189 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
190 ui::DisplayMode mode;
191 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(display, &mode));
192 const ui::Size& resolution = mode.resolution;
193 auto shot = screenshot();
194 shot->expectColor(Rect(0, 0, resolution.getWidth(), resolution.getHeight()), Color::RED);
195 }
196
197 } // namespace android
198
199 // TODO(b/129481165): remove the #pragma below and fix conversion issues
200 #pragma clang diagnostic pop // ignored "-Wconversion"
201