1 /*
2  * Copyright (C) 2019 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 <gui/BufferItemConsumer.h>
22 #include "TransactionTestHarnesses.h"
23 
24 namespace android {
25 
26 using android::hardware::graphics::common::V1_1::BufferUsage;
27 
28 class LayerTypeAndRenderTypeTransactionTest
29       : public LayerTypeTransactionHarness,
30         public ::testing::WithParamInterface<std::tuple<uint32_t, RenderPath>> {
31 public:
LayerTypeAndRenderTypeTransactionTest()32     LayerTypeAndRenderTypeTransactionTest()
33           : LayerTypeTransactionHarness(std::get<0>(GetParam())),
34             mRenderPathHarness(LayerRenderPathTestHarness(this, std::get<1>(GetParam()))) {}
35 
getScreenCapture()36     std::unique_ptr<ScreenCapture> getScreenCapture() {
37         return mRenderPathHarness.getScreenCapture();
38     }
39 
40 protected:
41     LayerRenderPathTestHarness mRenderPathHarness;
42 
43     static constexpr int64_t kUsageFlags = BufferUsage::CPU_READ_OFTEN |
44             BufferUsage::CPU_WRITE_OFTEN | BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
45 };
46 
47 ::testing::Environment* const binderEnv =
48         ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
49 
50 INSTANTIATE_TEST_CASE_P(
51         LayerTypeAndRenderTypeTransactionTests, LayerTypeAndRenderTypeTransactionTest,
52         ::testing::Combine(
53                 ::testing::Values(
54                         static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)),
55                 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT)));
56 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetZBasic)57 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
58     sp<SurfaceControl> layerR;
59     sp<SurfaceControl> layerG;
60     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
61     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
62     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
63     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
64 
65     Transaction().setLayer(layerR, mLayerZBase + 1).apply();
66     {
67         SCOPED_TRACE("layerR");
68         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
69     }
70 
71     Transaction().setLayer(layerG, mLayerZBase + 2).apply();
72     {
73         SCOPED_TRACE("layerG");
74         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
75     }
76 }
77 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetRelativeZBug64572777)78 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
79     sp<SurfaceControl> layerR;
80     sp<SurfaceControl> layerG;
81 
82     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
83     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
84     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
85     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
86 
87     Transaction().setPosition(layerG, 16, 16).setRelativeLayer(layerG, layerR, 1).apply();
88 
89     Transaction().reparent(layerG, nullptr).apply();
90 
91     // layerG should have been removed
92     getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
93 }
94 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetFlagsHidden)95 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
96     sp<SurfaceControl> layer;
97     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
98     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
99 
100     Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
101     {
102         SCOPED_TRACE("layer hidden");
103         getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
104     }
105 
106     Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
107     {
108         SCOPED_TRACE("layer shown");
109         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
110     }
111 }
112 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetFlagsOpaque)113 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
114     const Color translucentRed = {100, 0, 0, 100};
115     sp<SurfaceControl> layerR;
116     sp<SurfaceControl> layerG;
117     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
118     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
119     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
120     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
121 
122     Transaction()
123             .setLayer(layerR, mLayerZBase + 1)
124             .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
125             .apply();
126     {
127         SCOPED_TRACE("layerR opaque");
128         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
129     }
130 
131     Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
132     {
133         SCOPED_TRACE("layerR translucent");
134         const uint8_t g = uint8_t(255 - translucentRed.a);
135         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
136     }
137 }
138 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetZNegative)139 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
140     sp<SurfaceControl> parent =
141             LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
142                                               ISurfaceComposerClient::eFXSurfaceContainer);
143     Transaction().setCrop(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
144     sp<SurfaceControl> layerR;
145     sp<SurfaceControl> layerG;
146     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
147     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
148     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
149     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
150 
151     Transaction().reparent(layerR, parent).reparent(layerG, parent).apply();
152     Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
153     {
154         SCOPED_TRACE("layerR");
155         auto shot = getScreenCapture();
156         shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
157     }
158 
159     Transaction().setLayer(layerR, -3).apply();
160     {
161         SCOPED_TRACE("layerG");
162         auto shot = getScreenCapture();
163         shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
164     }
165 }
166 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetAlphaClamped)167 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
168     const Color color = {64, 0, 0, 255};
169     sp<SurfaceControl> layer;
170     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
171     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
172 
173     Transaction().setAlpha(layer, 2.0f).apply();
174     {
175         SCOPED_TRACE("clamped to 1.0f");
176         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
177     }
178 
179     Transaction().setAlpha(layer, -1.0f).apply();
180     {
181         SCOPED_TRACE("clamped to 0.0f");
182         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
183     }
184 }
185 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadius)186 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
187     sp<SurfaceControl> layer;
188     const uint8_t size = 64;
189     const uint8_t testArea = 4;
190     const float cornerRadius = 20.0f;
191     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
192     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
193 
194     Transaction().setCornerRadius(layer, cornerRadius).apply();
195     {
196         const uint8_t bottom = size - 1;
197         const uint8_t right = size - 1;
198         auto shot = getScreenCapture();
199         // Transparent corners
200         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
201         shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
202         shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
203         shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
204         // Solid center
205         shot->expectColor(Rect(size / 2 - testArea / 2, size / 2 - testArea / 2,
206                                size / 2 + testArea / 2, size / 2 + testArea / 2),
207                           Color::RED);
208     }
209 }
210 
211 // b/200781179 - don't round a layer without a valid crop
212 // This behaviour should be fixed since we treat buffer layers differently than
213 // effect or container layers.
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusInvalidCrop)214 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusInvalidCrop) {
215     sp<SurfaceControl> parent;
216     sp<SurfaceControl> child;
217     const uint8_t size = 64;
218     const uint8_t testArea = 4;
219     const float cornerRadius = 20.0f;
220     ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
221     ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::GREEN, size, size));
222     ASSERT_NO_FATAL_FAILURE(child = createColorLayer("child", Color::RED));
223 
224     Transaction().setCornerRadius(child, cornerRadius).reparent(child, parent).show(child).apply();
225     {
226         const uint8_t bottom = size - 1;
227         const uint8_t right = size - 1;
228         auto shot = getScreenCapture();
229         std::this_thread::sleep_for(std::chrono::seconds(5));
230         // Solid corners since we don't round a layer without a valid crop
231         shot->expectColor(Rect(0, 0, testArea, testArea), Color::RED);
232         shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::RED);
233         shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::RED);
234         shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::RED);
235         // Solid center
236         shot->expectColor(Rect(size / 2 - testArea / 2, size / 2 - testArea / 2,
237                                size / 2 + testArea / 2, size / 2 + testArea / 2),
238                           Color::RED);
239     }
240 }
241 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusRotated)242 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusRotated) {
243     sp<SurfaceControl> parent;
244     sp<SurfaceControl> child;
245     const uint8_t size = 64;
246     const uint8_t testArea = 4;
247     const float cornerRadius = 20.0f;
248     ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
249     ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
250     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size));
251     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size));
252 
253     Transaction()
254             .setCornerRadius(parent, cornerRadius)
255             .reparent(child, parent)
256             .setPosition(child, 0, size)
257             // Rotate by half PI
258             .setMatrix(child, 0.0f, -1.0f, 1.0f, 0.0f)
259             .apply();
260 
261     {
262         const uint8_t bottom = size - 1;
263         const uint8_t right = size - 1;
264         auto shot = getScreenCapture();
265         // Edges are transparent
266         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
267         shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
268         shot->expectColor(Rect(0, bottom - testArea, testArea, bottom - testArea), Color::BLACK);
269         shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
270         // Solid center
271         shot->expectColor(Rect(size / 2 - testArea / 2, size / 2 - testArea / 2,
272                                size / 2 + testArea / 2, size / 2 + testArea / 2),
273                           Color::GREEN);
274     }
275 }
276 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusChildCrop)277 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
278     sp<SurfaceControl> parent;
279     sp<SurfaceControl> child;
280     const uint8_t size = 64;
281     const uint8_t testArea = 4;
282     const float cornerRadius = 20.0f;
283     ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
284     ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
285     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
286     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
287 
288     Transaction()
289             .setCornerRadius(parent, cornerRadius)
290             .reparent(child, parent)
291             .setPosition(child, 0, size / 2)
292             .apply();
293 
294     {
295         const uint8_t bottom = size - 1;
296         const uint8_t right = size - 1;
297         auto shot = getScreenCapture();
298         // Top edge of child should not have rounded corners because it's translated in the parent
299         shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
300                           Color::GREEN);
301         // But bottom edges should have been clipped according to parent bounds
302         shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
303         shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
304     }
305 }
306 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusBufferRotationTransform)307 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusBufferRotationTransform) {
308     sp<SurfaceControl> layer;
309     sp<SurfaceControl> parent;
310     ASSERT_NO_FATAL_FAILURE(
311             parent = LayerTransactionTest::createLayer("parent", 0, 0,
312                                                        ISurfaceComposerClient::eFXSurfaceEffect));
313 
314     const uint32_t bufferWidth = 1500;
315     const uint32_t bufferHeight = 300;
316 
317     const uint32_t layerWidth = 300;
318     const uint32_t layerHeight = 1500;
319 
320     const uint32_t testArea = 4;
321     const float cornerRadius = 120.0f;
322     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", bufferWidth, bufferHeight));
323     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, bufferWidth, bufferHeight));
324 
325     Transaction()
326             .reparent(layer, parent)
327             .setColor(parent, half3(0, 1, 0))
328             .setCrop(parent, Rect(0, 0, layerWidth, layerHeight))
329             .setCornerRadius(parent, cornerRadius)
330 
331             .setTransform(layer, ui::Transform::ROT_90)
332             .setDestinationFrame(layer, Rect(0, 0, layerWidth, layerHeight))
333             .apply();
334     {
335         auto shot = getScreenCapture();
336         // Corners are transparent
337         // top-left
338         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
339         // top-right
340         shot->expectColor(Rect(layerWidth - testArea, 0, layerWidth, testArea), Color::BLACK);
341         // bottom-left
342         shot->expectColor(Rect(0, layerHeight - testArea, testArea, layerHeight), Color::BLACK);
343         // bottom-right
344         shot->expectColor(Rect(layerWidth - testArea, layerHeight - testArea, layerWidth,
345                                layerHeight),
346                           Color::BLACK);
347 
348         // Area after corner radius is solid
349         // top-left to top-right under the corner
350         shot->expectColor(Rect(0, cornerRadius, layerWidth, cornerRadius + testArea), Color::RED);
351         // bottom-left to bottom-right above the corner
352         shot->expectColor(Rect(0, layerHeight - cornerRadius - testArea, layerWidth,
353                                layerHeight - cornerRadius),
354                           Color::RED);
355         // left side after the corner
356         shot->expectColor(Rect(cornerRadius, 0, cornerRadius + testArea, layerHeight), Color::RED);
357         // right side before the corner
358         shot->expectColor(Rect(layerWidth - cornerRadius - testArea, 0, layerWidth - cornerRadius,
359                                layerHeight),
360                           Color::RED);
361     }
362 }
363 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusBufferCropTransform)364 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusBufferCropTransform) {
365     sp<SurfaceControl> layer;
366     sp<SurfaceControl> parent;
367     ASSERT_NO_FATAL_FAILURE(
368             parent = LayerTransactionTest::createLayer("parent", 0, 0,
369                                                        ISurfaceComposerClient::eFXSurfaceEffect));
370 
371     const uint32_t bufferWidth = 150 * 2;
372     const uint32_t bufferHeight = 750 * 2;
373 
374     const Rect bufferCrop(0, 0, 150, 750);
375 
376     const uint32_t layerWidth = 300;
377     const uint32_t layerHeight = 1500;
378 
379     const uint32_t testArea = 4;
380     const float cornerRadius = 120.0f;
381     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", bufferWidth, bufferHeight));
382     ASSERT_NO_FATAL_FAILURE(fillLayerQuadrant(layer, bufferWidth, bufferHeight, Color::RED,
383                                               Color::BLACK, Color::GREEN, Color::BLUE));
384 
385     Transaction()
386             .reparent(layer, parent)
387             .setColor(parent, half3(0, 1, 0))
388             .setCrop(parent, Rect(0, 0, layerWidth, layerHeight))
389             .setCornerRadius(parent, cornerRadius)
390 
391             .setBufferCrop(layer, bufferCrop)
392             .setDestinationFrame(layer, Rect(0, 0, layerWidth, layerHeight))
393             .apply();
394     {
395         auto shot = getScreenCapture();
396         // Corners are transparent
397         // top-left
398         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
399         // top-right
400         shot->expectColor(Rect(layerWidth - testArea, 0, layerWidth, testArea), Color::BLACK);
401         // bottom-left
402         shot->expectColor(Rect(0, layerHeight - testArea, testArea, layerHeight), Color::BLACK);
403         // bottom-right
404         shot->expectColor(Rect(layerWidth - testArea, layerHeight - testArea, layerWidth,
405                                layerHeight),
406                           Color::BLACK);
407 
408         // Area after corner radius is solid
409         // since the buffer is scaled, there will blending so adjust some of the bounds when
410         // checking.
411         float adjustedCornerRadius = cornerRadius + 15;
412         float adjustedLayerHeight = layerHeight - 15;
413         float adjustedLayerWidth = layerWidth - 15;
414 
415         // top-left to top-right under the corner
416         shot->expectColor(Rect(15, adjustedCornerRadius, adjustedLayerWidth,
417                                adjustedCornerRadius + testArea),
418                           Color::RED);
419         // bottom-left to bottom-right above the corner
420         shot->expectColor(Rect(15, adjustedLayerHeight - adjustedCornerRadius - testArea,
421                                adjustedLayerWidth, adjustedLayerHeight - adjustedCornerRadius),
422                           Color::RED);
423         // left side after the corner
424         shot->expectColor(Rect(adjustedCornerRadius, 15, adjustedCornerRadius + testArea,
425                                adjustedLayerHeight),
426                           Color::RED);
427         // right side before the corner
428         shot->expectColor(Rect(adjustedLayerWidth - adjustedCornerRadius - testArea, 15,
429                                adjustedLayerWidth - adjustedCornerRadius, adjustedLayerHeight),
430                           Color::RED);
431     }
432 }
433 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusChildBufferRotationTransform)434 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildBufferRotationTransform) {
435     sp<SurfaceControl> layer;
436     sp<SurfaceControl> parent;
437     ASSERT_NO_FATAL_FAILURE(
438             parent = LayerTransactionTest::createLayer("parent", 0, 0,
439                                                        ISurfaceComposerClient::eFXSurfaceEffect));
440 
441     const uint32_t bufferWidth = 1500;
442     const uint32_t bufferHeight = 300;
443 
444     const uint32_t layerWidth = 300;
445     const uint32_t layerHeight = 1500;
446 
447     const uint32_t testArea = 4;
448     const float cornerRadius = 120.0f;
449     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", bufferWidth, bufferHeight));
450     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::BLUE, bufferWidth, bufferHeight));
451 
452     sp<SurfaceControl> child;
453     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", bufferWidth, bufferHeight));
454     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::RED, bufferWidth, bufferHeight));
455 
456     Transaction()
457             .reparent(layer, parent)
458             .reparent(child, layer)
459             .setColor(parent, half3(0, 1, 0))
460             .setCrop(parent, Rect(0, 0, layerWidth, layerHeight))
461             .setCornerRadius(parent, cornerRadius) /* */
462 
463             .setTransform(layer, ui::Transform::ROT_90)
464             .setDestinationFrame(layer, Rect(0, 0, layerWidth, layerHeight))
465 
466             .setTransform(child, ui::Transform::ROT_90)
467             .setDestinationFrame(child, Rect(0, 0, layerWidth, layerHeight))
468             .apply();
469     {
470         auto shot = getScreenCapture();
471         // Corners are transparent
472         // top-left
473         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
474         // top-right
475         shot->expectColor(Rect(layerWidth - testArea, 0, layerWidth, testArea), Color::BLACK);
476         // bottom-left
477         shot->expectColor(Rect(0, layerHeight - testArea, testArea, layerHeight), Color::BLACK);
478         // bottom-right
479         shot->expectColor(Rect(layerWidth - testArea, layerHeight - testArea, layerWidth,
480                                layerHeight),
481                           Color::BLACK);
482 
483         // Area after corner radius is solid
484         // top-left to top-right under the corner
485         shot->expectColor(Rect(0, cornerRadius, layerWidth, cornerRadius + testArea), Color::RED);
486         // bottom-left to bottom-right above the corner
487         shot->expectColor(Rect(0, layerHeight - cornerRadius - testArea, layerWidth,
488                                layerHeight - cornerRadius),
489                           Color::RED);
490         // left side after the corner
491         shot->expectColor(Rect(cornerRadius, 0, cornerRadius + testArea, layerHeight), Color::RED);
492         // right side before the corner
493         shot->expectColor(Rect(layerWidth - cornerRadius - testArea, 0, layerWidth - cornerRadius,
494                                layerHeight),
495                           Color::RED);
496     }
497 }
498 
TEST_P(LayerTypeAndRenderTypeTransactionTest,ChildCornerRadiusTakesPrecedence)499 TEST_P(LayerTypeAndRenderTypeTransactionTest, ChildCornerRadiusTakesPrecedence) {
500     sp<SurfaceControl> parent;
501     sp<SurfaceControl> child;
502     const uint32_t size = 64;
503     const uint32_t parentSize = size * 3;
504     const uint32_t testLength = 4;
505     const float cornerRadius = 20.0f;
506     ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", parentSize, parentSize));
507     ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, parentSize, parentSize));
508     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size));
509     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size));
510 
511     Transaction()
512             .setCornerRadius(parent, cornerRadius)
513             .setCornerRadius(child, cornerRadius)
514             .reparent(child, parent)
515             .setPosition(child, size, size)
516             .apply();
517 
518     {
519         const uint32_t top = size - 1;
520         const uint32_t left = size - 1;
521         const uint32_t bottom = size * 2 - 1;
522         const uint32_t right = size * 2 - 1;
523         auto shot = getScreenCapture();
524         // Edges are transparent
525         // TL
526         shot->expectColor(Rect(left, top, testLength, testLength), Color::RED);
527         // TR
528         shot->expectColor(Rect(right - testLength, top, right, testLength), Color::RED);
529         // BL
530         shot->expectColor(Rect(left, bottom - testLength, testLength, bottom - testLength),
531                           Color::RED);
532         // BR
533         shot->expectColor(Rect(right - testLength, bottom - testLength, right, bottom), Color::RED);
534         // Solid center
535         shot->expectColor(Rect(parentSize / 2 - testLength / 2, parentSize / 2 - testLength / 2,
536                                parentSize / 2 + testLength / 2, parentSize / 2 + testLength / 2),
537                           Color::GREEN);
538     }
539 }
540 
541 // Test if ParentCornerRadiusTakesPrecedence if the parent corner radius crop is fully contained by
542 // the child corner radius crop.
TEST_P(LayerTypeAndRenderTypeTransactionTest,ParentCornerRadiusTakesPrecedence)543 TEST_P(LayerTypeAndRenderTypeTransactionTest, ParentCornerRadiusTakesPrecedence) {
544     sp<SurfaceControl> parent;
545     sp<SurfaceControl> child;
546     const uint32_t size = 64;
547     const uint32_t parentSize = size * 3;
548     const Rect parentCrop(size + 1, size + 1, size * 2 - 1, size * 2 - 1);
549     const uint32_t testLength = 4;
550     const float cornerRadius = 20.0f;
551     ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", parentSize, parentSize));
552     ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, parentSize, parentSize));
553     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size));
554     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size));
555 
556     Transaction()
557             .setCornerRadius(parent, cornerRadius)
558             .setCrop(parent, parentCrop)
559             .setCornerRadius(child, cornerRadius)
560             .reparent(child, parent)
561             .setPosition(child, size, size)
562             .apply();
563 
564     {
565         const uint32_t top = size - 1;
566         const uint32_t left = size - 1;
567         const uint32_t bottom = size * 2 - 1;
568         const uint32_t right = size * 2 - 1;
569         auto shot = getScreenCapture();
570         // Edges are transparent
571         // TL
572         shot->expectColor(Rect(left, top, testLength, testLength), Color::BLACK);
573         // TR
574         shot->expectColor(Rect(right - testLength, top, right, testLength), Color::BLACK);
575         // BL
576         shot->expectColor(Rect(left, bottom - testLength, testLength, bottom - testLength),
577                           Color::BLACK);
578         // BR
579         shot->expectColor(Rect(right - testLength, bottom - testLength, right, bottom),
580                           Color::BLACK);
581         // Solid center
582         shot->expectColor(Rect(parentSize / 2 - testLength / 2, parentSize / 2 - testLength / 2,
583                                parentSize / 2 + testLength / 2, parentSize / 2 + testLength / 2),
584                           Color::GREEN);
585     }
586 }
587 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBackgroundBlurRadiusSimple)588 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBackgroundBlurRadiusSimple) {
589     if (!deviceSupportsBlurs()) GTEST_SKIP();
590     if (!deviceUsesSkiaRenderEngine()) GTEST_SKIP();
591 
592     const auto canvasSize = 256;
593 
594     sp<SurfaceControl> leftLayer;
595     sp<SurfaceControl> rightLayer;
596     sp<SurfaceControl> greenLayer;
597     sp<SurfaceControl> blurLayer;
598     const auto leftRect = Rect(0, 0, canvasSize / 2, canvasSize);
599     const auto rightRect = Rect(canvasSize / 2, 0, canvasSize, canvasSize);
600     const auto blurRect = Rect(0, 0, canvasSize, canvasSize);
601 
602     ASSERT_NO_FATAL_FAILURE(leftLayer =
603                                     createLayer("Left", leftRect.getWidth(), leftRect.getHeight()));
604     ASSERT_NO_FATAL_FAILURE(
605             fillLayerColor(leftLayer, Color::BLUE, leftRect.getWidth(), leftRect.getHeight()));
606     ASSERT_NO_FATAL_FAILURE(greenLayer = createLayer("Green", canvasSize * 2, canvasSize * 2));
607     ASSERT_NO_FATAL_FAILURE(
608             fillLayerColor(greenLayer, Color::GREEN, canvasSize * 2, canvasSize * 2));
609     ASSERT_NO_FATAL_FAILURE(
610             rightLayer = createLayer("Right", rightRect.getWidth(), rightRect.getHeight()));
611     ASSERT_NO_FATAL_FAILURE(
612             fillLayerColor(rightLayer, Color::RED, rightRect.getWidth(), rightRect.getHeight()));
613 
614     Transaction()
615             .setLayer(greenLayer, mLayerZBase)
616             .setLayer(leftLayer, mLayerZBase + 1)
617             .setLayer(rightLayer, mLayerZBase + 2)
618             .setPosition(rightLayer, rightRect.left, rightRect.top)
619             .apply();
620 
621     {
622         auto shot = getScreenCapture();
623         shot->expectColor(leftRect, Color::BLUE);
624         shot->expectColor(rightRect, Color::RED);
625     }
626 
627     ASSERT_NO_FATAL_FAILURE(blurLayer = createColorLayer("BackgroundBlur", Color::TRANSPARENT));
628 
629     const auto blurRadius = canvasSize / 2;
630     Transaction()
631             .setLayer(blurLayer, mLayerZBase + 3)
632             .setBackgroundBlurRadius(blurLayer, blurRadius)
633             .setCrop(blurLayer, blurRect)
634             .setAlpha(blurLayer, 0.0f)
635             .apply();
636 
637     {
638         auto shot = getScreenCapture();
639 
640         const auto stepSize = 1;
641         const auto blurAreaOffset = blurRadius * 0.7f;
642         const auto blurAreaStartX = canvasSize / 2 - blurRadius + blurAreaOffset;
643         const auto blurAreaEndX = canvasSize / 2 + blurRadius - blurAreaOffset;
644         Color previousColor;
645         Color currentColor;
646         for (int y = 0; y < canvasSize; y++) {
647             shot->checkPixel(0, y, /* r = */ 0, /* g = */ 0, /* b = */ 255);
648             previousColor = shot->getPixelColor(0, y);
649             for (int x = blurAreaStartX; x < blurAreaEndX; x += stepSize) {
650                 currentColor = shot->getPixelColor(x, y);
651                 ASSERT_GT(currentColor.r, previousColor.r);
652                 ASSERT_LT(currentColor.b, previousColor.b);
653                 ASSERT_EQ(0, currentColor.g);
654             }
655             shot->checkPixel(canvasSize - 1, y, 255, 0, 0);
656         }
657     }
658 }
659 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBackgroundBlurRadiusOnMultipleLayers)660 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBackgroundBlurRadiusOnMultipleLayers) {
661     if (!deviceSupportsBlurs()) GTEST_SKIP();
662     if (!deviceUsesSkiaRenderEngine()) GTEST_SKIP();
663 
664     auto size = 256;
665     auto center = size / 2;
666     auto blurRadius = 50;
667 
668     sp<SurfaceControl> backgroundLayer;
669     ASSERT_NO_FATAL_FAILURE(backgroundLayer = createLayer("background", size, size));
670     ASSERT_NO_FATAL_FAILURE(fillLayerColor(backgroundLayer, Color::GREEN, size, size));
671 
672     sp<SurfaceControl> leftLayer;
673     ASSERT_NO_FATAL_FAILURE(leftLayer = createLayer("left", size / 2, size));
674     ASSERT_NO_FATAL_FAILURE(fillLayerColor(leftLayer, Color::RED, size / 2, size));
675 
676     sp<SurfaceControl> blurLayer1;
677     auto centralSquareSize = size / 2;
678     ASSERT_NO_FATAL_FAILURE(blurLayer1 =
679                                     createLayer("blur1", centralSquareSize, centralSquareSize));
680     ASSERT_NO_FATAL_FAILURE(
681             fillLayerColor(blurLayer1, Color::BLUE, centralSquareSize, centralSquareSize));
682 
683     sp<SurfaceControl> blurLayer2;
684     ASSERT_NO_FATAL_FAILURE(blurLayer2 = createLayer("blur2", size, size));
685     ASSERT_NO_FATAL_FAILURE(
686             fillLayerColor(blurLayer2, Color::TRANSPARENT, centralSquareSize, centralSquareSize));
687 
688     Transaction()
689             .setBackgroundBlurRadius(blurLayer1, blurRadius)
690             .setBackgroundBlurRadius(blurLayer2, blurRadius)
691             .apply();
692 
693     auto shot = getScreenCapture();
694     shot->expectColor(Rect(center - 5, center - 5, center, center), Color{100, 100, 100, 255},
695                       40 /* tolerance */);
696 }
697 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBackgroundBlurAffectedByParentAlpha)698 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBackgroundBlurAffectedByParentAlpha) {
699     if (!deviceSupportsBlurs()) GTEST_SKIP();
700     if (!deviceUsesSkiaRenderEngine()) GTEST_SKIP();
701 
702     sp<SurfaceControl> left;
703     sp<SurfaceControl> right;
704     sp<SurfaceControl> blur;
705     sp<SurfaceControl> blurParent;
706 
707     const auto size = 256;
708     ASSERT_NO_FATAL_FAILURE(left = createLayer("Left", size, size));
709     ASSERT_NO_FATAL_FAILURE(fillLayerColor(left, Color::BLUE, size, size));
710     ASSERT_NO_FATAL_FAILURE(right = createLayer("Right", size, size));
711     ASSERT_NO_FATAL_FAILURE(fillLayerColor(right, Color::RED, size, size));
712 
713     Transaction()
714             .setLayer(left, mLayerZBase + 1)
715             .setLayer(right, mLayerZBase + 2)
716             .setPosition(right, size, 0)
717             .apply();
718 
719     {
720         auto shot = getScreenCapture();
721         shot->expectColor(Rect(0, 0, size, size), Color::BLUE);
722         shot->expectColor(Rect(size, 0, size * 2, size), Color::RED);
723     }
724 
725     ASSERT_NO_FATAL_FAILURE(blur = createLayer("BackgroundBlur", size * 2, size));
726     ASSERT_NO_FATAL_FAILURE(fillLayerColor(blur, Color::TRANSPARENT, size * 2, size));
727     ASSERT_NO_FATAL_FAILURE(blurParent = createLayer("BackgroundBlurParent", size * 2, size));
728     ASSERT_NO_FATAL_FAILURE(fillLayerColor(blurParent, Color::TRANSPARENT, size * 2, size));
729 
730     Transaction()
731             .setLayer(blurParent, mLayerZBase + 3)
732             .setAlpha(blurParent, 0.5)
733             .setLayer(blur, mLayerZBase + 4)
734             .setBackgroundBlurRadius(blur, size) // set the blur radius to the size of one rect
735             .reparent(blur, blurParent)
736             .apply();
737 
738     {
739         auto shot = getScreenCapture();
740         // assert that outer sides of the red and blue rects are not blended with the other color;
741         // if the blur didn't take into account parent alpha, the outer sides would have traces of
742         // the other color
743         shot->expectColor(Rect(0, 0, size / 2, size), Color::BLUE);
744         shot->expectColor(Rect(size + size / 2, 0, size * 2, size), Color::RED);
745         // assert that middle line has blended red and blur color; adding a tolerance of 10 to
746         // account for future blur algorithm changes
747         shot->expectColor(Rect(size, 0, size + 1, size), {136, 0, 119, 255}, 10);
748     }
749 }
750 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetColorWithBuffer)751 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
752     sp<SurfaceControl> bufferLayer;
753     ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
754     ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
755 
756     // color is ignored
757     Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
758     getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
759 }
760 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetLayerStackBasic)761 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
762     sp<SurfaceControl> layer;
763     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
764     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
765 
766     Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
767     {
768         SCOPED_TRACE("non-existing layer stack");
769         getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
770     }
771 
772     Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
773     {
774         SCOPED_TRACE("original layer stack");
775         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
776     }
777 }
778 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBufferFormat)779 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBufferFormat) {
780     int32_t width = 100;
781     int32_t height = 100;
782     Rect crop = Rect(0, 0, width, height);
783 
784     sp<SurfaceControl> behindLayer = createColorLayer("Behind layer", Color::RED);
785     sp<SurfaceControl> layer;
786     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, 0, nullptr, nullptr,
787                                                 PIXEL_FORMAT_RGBX_8888));
788 
789     Transaction()
790             .setLayer(layer, INT32_MAX - 1)
791             .show(layer)
792             .setLayerStack(behindLayer, mDisplayLayerStack)
793             .setCrop(behindLayer, crop)
794             .setLayer(behindLayer, INT32_MAX - 2)
795             .show(behindLayer)
796             .apply();
797 
798     sp<Surface> surface = layer->getSurface();
799 
800     sp<GraphicBuffer> buffer =
801             new GraphicBuffer(width, height, PIXEL_FORMAT_RGBX_8888, 1, kUsageFlags, "test");
802     ASSERT_NO_FATAL_FAILURE(
803             TransactionUtils::fillGraphicBufferColor(buffer, crop, Color::TRANSPARENT));
804 
805     if (mLayerType == ISurfaceComposerClient::eFXSurfaceBufferQueue) {
806         Surface::attachAndQueueBufferWithDataspace(surface.get(), buffer, ui::Dataspace::V0_SRGB);
807     } else {
808         Transaction().setBuffer(layer, buffer).apply();
809     }
810 
811     {
812         SCOPED_TRACE("Buffer Opaque Format");
813         auto shot = screenshot();
814         shot->expectColor(crop, Color::BLACK);
815     }
816 
817     buffer = new GraphicBuffer(width, height, PIXEL_FORMAT_RGBA_8888, 1, kUsageFlags, "test");
818     ASSERT_NO_FATAL_FAILURE(
819             TransactionUtils::fillGraphicBufferColor(buffer, crop, Color::TRANSPARENT));
820 
821     if (mLayerType == ISurfaceComposerClient::eFXSurfaceBufferQueue) {
822         Surface::attachAndQueueBufferWithDataspace(surface.get(), buffer, ui::Dataspace::V0_SRGB);
823     } else {
824         Transaction().setBuffer(layer, buffer).apply();
825     }
826 
827     {
828         SCOPED_TRACE("Buffer Transparent Format");
829         auto shot = screenshot();
830         shot->expectColor(crop, Color::RED);
831     }
832 }
833 } // namespace android
834 
835 // TODO(b/129481165): remove the #pragma below and fix conversion issues
836 #pragma clang diagnostic pop // ignored "-Wconversion"
837