1 /*
2 * Copyright (C) 2022 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 // TODO: Amend all tests when screenshots become fully reworked for borders
19 #pragma clang diagnostic push
20 #pragma clang diagnostic ignored "-Wconversion"
21
22 #include <chrono> // std::chrono::seconds
23 #include <thread> // std::this_thread::sleep_for
24 #include "LayerTransactionTest.h"
25
26 namespace android {
27
28 class LayerBorderTest : public LayerTransactionTest {
29 protected:
SetUp()30 virtual void SetUp() {
31 LayerTransactionTest::SetUp();
32 ASSERT_EQ(NO_ERROR, mClient->initCheck());
33
34 toHalf3 = ColorTransformHelper::toHalf3;
35 toHalf4 = ColorTransformHelper::toHalf4;
36
37 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
38 ASSERT_FALSE(ids.empty());
39 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
40 ASSERT_FALSE(display == nullptr);
41 mColorOrange = toHalf4({255, 140, 0, 255});
42 mParentLayer = createColorLayer("Parent layer", Color::RED);
43
44 mContainerLayer = mClient->createSurface(String8("Container Layer"), 0 /* width */,
45 0 /* height */, PIXEL_FORMAT_RGBA_8888,
46 ISurfaceComposerClient::eFXSurfaceContainer |
47 ISurfaceComposerClient::eNoColorFill,
48 mParentLayer->getHandle());
49 EXPECT_NE(nullptr, mContainerLayer.get()) << "failed to create container layer";
50
51 mEffectLayer1 = mClient->createSurface(String8("Effect Layer"), 0 /* width */,
52 0 /* height */, PIXEL_FORMAT_RGBA_8888,
53 ISurfaceComposerClient::eFXSurfaceEffect |
54 ISurfaceComposerClient::eNoColorFill,
55 mContainerLayer->getHandle());
56 EXPECT_NE(nullptr, mEffectLayer1.get()) << "failed to create effect layer 1";
57
58 mEffectLayer2 = mClient->createSurface(String8("Effect Layer"), 0 /* width */,
59 0 /* height */, PIXEL_FORMAT_RGBA_8888,
60 ISurfaceComposerClient::eFXSurfaceEffect |
61 ISurfaceComposerClient::eNoColorFill,
62 mContainerLayer->getHandle());
63
64 EXPECT_NE(nullptr, mEffectLayer2.get()) << "failed to create effect layer 2";
65
66 asTransaction([&](Transaction& t) {
67 t.setDisplayLayerStack(display, ui::DEFAULT_LAYER_STACK);
68 t.setLayer(mParentLayer, INT32_MAX - 20).show(mParentLayer);
69 t.setFlags(mParentLayer, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque);
70
71 t.setColor(mEffectLayer1, toHalf3(Color::BLUE));
72
73 t.setColor(mEffectLayer2, toHalf3(Color::GREEN));
74 });
75 }
76
TearDown()77 virtual void TearDown() {
78 // Uncomment the line right below when running any of the tests
79 // std::this_thread::sleep_for (std::chrono::seconds(30));
80 LayerTransactionTest::TearDown();
81 mParentLayer = 0;
82 }
83
84 std::function<half3(Color)> toHalf3;
85 std::function<half4(Color)> toHalf4;
86 sp<SurfaceControl> mParentLayer, mContainerLayer, mEffectLayer1, mEffectLayer2;
87 half4 mColorOrange;
88 };
89
TEST_F(LayerBorderTest,OverlappingVisibleRegions)90 TEST_F(LayerBorderTest, OverlappingVisibleRegions) {
91 asTransaction([&](Transaction& t) {
92 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
93 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
94
95 t.enableBorder(mContainerLayer, true, 20, mColorOrange);
96 t.show(mEffectLayer1);
97 t.show(mEffectLayer2);
98 t.show(mContainerLayer);
99 });
100 }
101
TEST_F(LayerBorderTest,PartiallyCoveredVisibleRegion)102 TEST_F(LayerBorderTest, PartiallyCoveredVisibleRegion) {
103 asTransaction([&](Transaction& t) {
104 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
105 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
106
107 t.enableBorder(mEffectLayer1, true, 20, mColorOrange);
108 t.show(mEffectLayer1);
109 t.show(mEffectLayer2);
110 t.show(mContainerLayer);
111 });
112 }
113
TEST_F(LayerBorderTest,NonOverlappingVisibleRegion)114 TEST_F(LayerBorderTest, NonOverlappingVisibleRegion) {
115 asTransaction([&](Transaction& t) {
116 t.setCrop(mEffectLayer1, Rect(0, 0, 200, 200));
117 t.setCrop(mEffectLayer2, Rect(400, 400, 600, 600));
118
119 t.enableBorder(mContainerLayer, true, 20, mColorOrange);
120 t.show(mEffectLayer1);
121 t.show(mEffectLayer2);
122 t.show(mContainerLayer);
123 });
124 }
125
TEST_F(LayerBorderTest,EmptyVisibleRegion)126 TEST_F(LayerBorderTest, EmptyVisibleRegion) {
127 asTransaction([&](Transaction& t) {
128 t.setCrop(mEffectLayer1, Rect(200, 200, 400, 400));
129 t.setCrop(mEffectLayer2, Rect(0, 0, 600, 600));
130
131 t.enableBorder(mEffectLayer1, true, 20, mColorOrange);
132 t.show(mEffectLayer1);
133 t.show(mEffectLayer2);
134 t.show(mContainerLayer);
135 });
136 }
137
TEST_F(LayerBorderTest,ZOrderAdjustment)138 TEST_F(LayerBorderTest, ZOrderAdjustment) {
139 asTransaction([&](Transaction& t) {
140 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
141 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
142 t.setLayer(mParentLayer, 10);
143 t.setLayer(mEffectLayer1, 30);
144 t.setLayer(mEffectLayer2, 20);
145
146 t.enableBorder(mEffectLayer1, true, 20, mColorOrange);
147 t.show(mEffectLayer1);
148 t.show(mEffectLayer2);
149 t.show(mContainerLayer);
150 });
151 }
152
TEST_F(LayerBorderTest,GrandChildHierarchy)153 TEST_F(LayerBorderTest, GrandChildHierarchy) {
154 sp<SurfaceControl> containerLayer2 =
155 mClient->createSurface(String8("Container Layer"), 0 /* width */, 0 /* height */,
156 PIXEL_FORMAT_RGBA_8888,
157 ISurfaceComposerClient::eFXSurfaceContainer |
158 ISurfaceComposerClient::eNoColorFill,
159 mContainerLayer->getHandle());
160 EXPECT_NE(nullptr, containerLayer2.get()) << "failed to create container layer 2";
161
162 sp<SurfaceControl> effectLayer3 =
163 mClient->createSurface(String8("Effect Layer"), 0 /* width */, 0 /* height */,
164 PIXEL_FORMAT_RGBA_8888,
165 ISurfaceComposerClient::eFXSurfaceEffect |
166 ISurfaceComposerClient::eNoColorFill,
167 containerLayer2->getHandle());
168
169 asTransaction([&](Transaction& t) {
170 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
171 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
172 t.setCrop(effectLayer3, Rect(400, 400, 800, 800));
173 t.setColor(effectLayer3, toHalf3(Color::BLUE));
174
175 t.enableBorder(mContainerLayer, true, 20, mColorOrange);
176 t.show(mEffectLayer1);
177 t.show(mEffectLayer2);
178 t.show(effectLayer3);
179 t.show(mContainerLayer);
180 });
181 }
182
TEST_F(LayerBorderTest,TransparentAlpha)183 TEST_F(LayerBorderTest, TransparentAlpha) {
184 asTransaction([&](Transaction& t) {
185 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
186 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
187 t.setAlpha(mEffectLayer1, 0.0f);
188
189 t.enableBorder(mContainerLayer, true, 20, mColorOrange);
190 t.show(mEffectLayer1);
191 t.show(mEffectLayer2);
192 t.show(mContainerLayer);
193 });
194 }
195
TEST_F(LayerBorderTest,SemiTransparentAlpha)196 TEST_F(LayerBorderTest, SemiTransparentAlpha) {
197 asTransaction([&](Transaction& t) {
198 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
199 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
200 t.setAlpha(mEffectLayer2, 0.5f);
201
202 t.enableBorder(mEffectLayer2, true, 20, mColorOrange);
203 t.show(mEffectLayer1);
204 t.show(mEffectLayer2);
205 t.show(mContainerLayer);
206 });
207 }
208
TEST_F(LayerBorderTest,InvisibleLayers)209 TEST_F(LayerBorderTest, InvisibleLayers) {
210 asTransaction([&](Transaction& t) {
211 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
212 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
213
214 t.enableBorder(mContainerLayer, true, 20, mColorOrange);
215 t.hide(mEffectLayer2);
216 t.show(mContainerLayer);
217 });
218 }
219
TEST_F(LayerBorderTest,LayerWithBuffer)220 TEST_F(LayerBorderTest, LayerWithBuffer) {
221 asTransaction([&](Transaction& t) {
222 t.hide(mEffectLayer1);
223 t.hide(mEffectLayer2);
224 t.show(mContainerLayer);
225
226 sp<SurfaceControl> layer =
227 mClient->createSurface(String8("BufferState"), 0 /* width */, 0 /* height */,
228 PIXEL_FORMAT_RGBA_8888,
229 ISurfaceComposerClient::eFXSurfaceBufferState,
230 mContainerLayer->getHandle());
231
232 sp<GraphicBuffer> buffer =
233 sp<GraphicBuffer>::make(400u, 400u, PIXEL_FORMAT_RGBA_8888, 1u,
234 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
235 BufferUsage::COMPOSER_OVERLAY |
236 BufferUsage::GPU_TEXTURE,
237 "test");
238 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 200, 200), Color::GREEN);
239 TransactionUtils::fillGraphicBufferColor(buffer, Rect(200, 200, 400, 400), Color::BLUE);
240
241 t.setBuffer(layer, buffer);
242 t.setPosition(layer, 100, 100);
243 t.show(layer);
244 t.enableBorder(mContainerLayer, true, 20, mColorOrange);
245 });
246 }
247
TEST_F(LayerBorderTest,CustomWidth)248 TEST_F(LayerBorderTest, CustomWidth) {
249 asTransaction([&](Transaction& t) {
250 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
251 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
252
253 t.enableBorder(mContainerLayer, true, 50, mColorOrange);
254 t.show(mEffectLayer1);
255 t.show(mEffectLayer2);
256 t.show(mContainerLayer);
257 });
258 }
259
TEST_F(LayerBorderTest,CustomColor)260 TEST_F(LayerBorderTest, CustomColor) {
261 asTransaction([&](Transaction& t) {
262 t.setCrop(mEffectLayer1, Rect(0, 0, 400, 400));
263 t.setCrop(mEffectLayer2, Rect(200, 200, 600, 600));
264
265 t.enableBorder(mContainerLayer, true, 20, toHalf4({255, 0, 255, 255}));
266 t.show(mEffectLayer1);
267 t.show(mEffectLayer2);
268 t.show(mContainerLayer);
269 });
270 }
271
TEST_F(LayerBorderTest,CustomWidthAndColorAndOpacity)272 TEST_F(LayerBorderTest, CustomWidthAndColorAndOpacity) {
273 asTransaction([&](Transaction& t) {
274 t.setCrop(mEffectLayer1, Rect(0, 0, 200, 200));
275 t.setCrop(mEffectLayer2, Rect(400, 400, 600, 600));
276
277 t.enableBorder(mContainerLayer, true, 40, toHalf4({255, 255, 0, 128}));
278 t.show(mEffectLayer1);
279 t.show(mEffectLayer2);
280 t.show(mContainerLayer);
281 });
282 }
283
284 } // namespace android
285
286 // TODO(b/129481165): remove the #pragma below and fix conversion issues
287 #pragma clang diagnostic pop // ignored "-Wconversion"
288