• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusRotated)211 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusRotated) {
212     sp<SurfaceControl> parent;
213     sp<SurfaceControl> child;
214     const uint8_t size = 64;
215     const uint8_t testArea = 4;
216     const float cornerRadius = 20.0f;
217     ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
218     ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
219     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size));
220     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size));
221 
222     Transaction()
223             .setCornerRadius(parent, cornerRadius)
224             .reparent(child, parent)
225             .setPosition(child, 0, size)
226             // Rotate by half PI
227             .setMatrix(child, 0.0f, -1.0f, 1.0f, 0.0f)
228             .apply();
229 
230     {
231         const uint8_t bottom = size - 1;
232         const uint8_t right = size - 1;
233         auto shot = getScreenCapture();
234         // Edges are transparent
235         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
236         shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
237         shot->expectColor(Rect(0, bottom - testArea, testArea, bottom - testArea), Color::BLACK);
238         shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
239         // Solid center
240         shot->expectColor(Rect(size / 2 - testArea / 2, size / 2 - testArea / 2,
241                                size / 2 + testArea / 2, size / 2 + testArea / 2),
242                           Color::GREEN);
243     }
244 }
245 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusChildCrop)246 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
247     sp<SurfaceControl> parent;
248     sp<SurfaceControl> child;
249     const uint8_t size = 64;
250     const uint8_t testArea = 4;
251     const float cornerRadius = 20.0f;
252     ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
253     ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
254     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
255     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
256 
257     Transaction()
258             .setCornerRadius(parent, cornerRadius)
259             .reparent(child, parent)
260             .setPosition(child, 0, size / 2)
261             .apply();
262 
263     {
264         const uint8_t bottom = size - 1;
265         const uint8_t right = size - 1;
266         auto shot = getScreenCapture();
267         // Top edge of child should not have rounded corners because it's translated in the parent
268         shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
269                           Color::GREEN);
270         // But bottom edges should have been clipped according to parent bounds
271         shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
272         shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
273     }
274 }
275 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusBufferRotationTransform)276 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusBufferRotationTransform) {
277     sp<SurfaceControl> layer;
278     sp<SurfaceControl> parent;
279     ASSERT_NO_FATAL_FAILURE(
280             parent = LayerTransactionTest::createLayer("parent", 0, 0,
281                                                        ISurfaceComposerClient::eFXSurfaceEffect));
282 
283     const uint32_t bufferWidth = 1500;
284     const uint32_t bufferHeight = 300;
285 
286     const uint32_t layerWidth = 300;
287     const uint32_t layerHeight = 1500;
288 
289     const uint32_t testArea = 4;
290     const float cornerRadius = 120.0f;
291     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", bufferWidth, bufferHeight));
292     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, bufferWidth, bufferHeight));
293 
294     Transaction()
295             .reparent(layer, parent)
296             .setColor(parent, half3(0, 1, 0))
297             .setCrop(parent, Rect(0, 0, layerWidth, layerHeight))
298             .setCornerRadius(parent, cornerRadius)
299 
300             .setTransform(layer, ui::Transform::ROT_90)
301             .setDestinationFrame(layer, Rect(0, 0, layerWidth, layerHeight))
302             .apply();
303     {
304         auto shot = getScreenCapture();
305         // Corners are transparent
306         // top-left
307         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
308         // top-right
309         shot->expectColor(Rect(layerWidth - testArea, 0, layerWidth, testArea), Color::BLACK);
310         // bottom-left
311         shot->expectColor(Rect(0, layerHeight - testArea, testArea, layerHeight), Color::BLACK);
312         // bottom-right
313         shot->expectColor(Rect(layerWidth - testArea, layerHeight - testArea, layerWidth,
314                                layerHeight),
315                           Color::BLACK);
316 
317         // Area after corner radius is solid
318         // top-left to top-right under the corner
319         shot->expectColor(Rect(0, cornerRadius, layerWidth, cornerRadius + testArea), Color::RED);
320         // bottom-left to bottom-right above the corner
321         shot->expectColor(Rect(0, layerHeight - cornerRadius - testArea, layerWidth,
322                                layerHeight - cornerRadius),
323                           Color::RED);
324         // left side after the corner
325         shot->expectColor(Rect(cornerRadius, 0, cornerRadius + testArea, layerHeight), Color::RED);
326         // right side before the corner
327         shot->expectColor(Rect(layerWidth - cornerRadius - testArea, 0, layerWidth - cornerRadius,
328                                layerHeight),
329                           Color::RED);
330     }
331 }
332 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusBufferCropTransform)333 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusBufferCropTransform) {
334     sp<SurfaceControl> layer;
335     sp<SurfaceControl> parent;
336     ASSERT_NO_FATAL_FAILURE(
337             parent = LayerTransactionTest::createLayer("parent", 0, 0,
338                                                        ISurfaceComposerClient::eFXSurfaceEffect));
339 
340     const uint32_t bufferWidth = 150 * 2;
341     const uint32_t bufferHeight = 750 * 2;
342 
343     const Rect bufferCrop(0, 0, 150, 750);
344 
345     const uint32_t layerWidth = 300;
346     const uint32_t layerHeight = 1500;
347 
348     const uint32_t testArea = 4;
349     const float cornerRadius = 120.0f;
350     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", bufferWidth, bufferHeight));
351     ASSERT_NO_FATAL_FAILURE(fillLayerQuadrant(layer, bufferWidth, bufferHeight, Color::RED,
352                                               Color::BLACK, Color::GREEN, Color::BLUE));
353 
354     Transaction()
355             .reparent(layer, parent)
356             .setColor(parent, half3(0, 1, 0))
357             .setCrop(parent, Rect(0, 0, layerWidth, layerHeight))
358             .setCornerRadius(parent, cornerRadius)
359 
360             .setBufferCrop(layer, bufferCrop)
361             .setDestinationFrame(layer, Rect(0, 0, layerWidth, layerHeight))
362             .apply();
363     {
364         auto shot = getScreenCapture();
365         // Corners are transparent
366         // top-left
367         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
368         // top-right
369         shot->expectColor(Rect(layerWidth - testArea, 0, layerWidth, testArea), Color::BLACK);
370         // bottom-left
371         shot->expectColor(Rect(0, layerHeight - testArea, testArea, layerHeight), Color::BLACK);
372         // bottom-right
373         shot->expectColor(Rect(layerWidth - testArea, layerHeight - testArea, layerWidth,
374                                layerHeight),
375                           Color::BLACK);
376 
377         // Area after corner radius is solid
378         // since the buffer is scaled, there will blending so adjust some of the bounds when
379         // checking.
380         float adjustedCornerRadius = cornerRadius + 15;
381         float adjustedLayerHeight = layerHeight - 15;
382         float adjustedLayerWidth = layerWidth - 15;
383 
384         // top-left to top-right under the corner
385         shot->expectColor(Rect(15, adjustedCornerRadius, adjustedLayerWidth,
386                                adjustedCornerRadius + testArea),
387                           Color::RED);
388         // bottom-left to bottom-right above the corner
389         shot->expectColor(Rect(15, adjustedLayerHeight - adjustedCornerRadius - testArea,
390                                adjustedLayerWidth, adjustedLayerHeight - adjustedCornerRadius),
391                           Color::RED);
392         // left side after the corner
393         shot->expectColor(Rect(adjustedCornerRadius, 15, adjustedCornerRadius + testArea,
394                                adjustedLayerHeight),
395                           Color::RED);
396         // right side before the corner
397         shot->expectColor(Rect(adjustedLayerWidth - adjustedCornerRadius - testArea, 15,
398                                adjustedLayerWidth - adjustedCornerRadius, adjustedLayerHeight),
399                           Color::RED);
400     }
401 }
402 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusChildBufferRotationTransform)403 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildBufferRotationTransform) {
404     sp<SurfaceControl> layer;
405     sp<SurfaceControl> parent;
406     ASSERT_NO_FATAL_FAILURE(
407             parent = LayerTransactionTest::createLayer("parent", 0, 0,
408                                                        ISurfaceComposerClient::eFXSurfaceEffect));
409 
410     const uint32_t bufferWidth = 1500;
411     const uint32_t bufferHeight = 300;
412 
413     const uint32_t layerWidth = 300;
414     const uint32_t layerHeight = 1500;
415 
416     const uint32_t testArea = 4;
417     const float cornerRadius = 120.0f;
418     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", bufferWidth, bufferHeight));
419     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::BLUE, bufferWidth, bufferHeight));
420 
421     sp<SurfaceControl> child;
422     ASSERT_NO_FATAL_FAILURE(child = createLayer("child", bufferWidth, bufferHeight));
423     ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::RED, bufferWidth, bufferHeight));
424 
425     Transaction()
426             .reparent(layer, parent)
427             .reparent(child, layer)
428             .setColor(parent, half3(0, 1, 0))
429             .setCrop(parent, Rect(0, 0, layerWidth, layerHeight))
430             .setCornerRadius(parent, cornerRadius) /* */
431 
432             .setTransform(layer, ui::Transform::ROT_90)
433             .setDestinationFrame(layer, Rect(0, 0, layerWidth, layerHeight))
434 
435             .setTransform(child, ui::Transform::ROT_90)
436             .setDestinationFrame(child, Rect(0, 0, layerWidth, layerHeight))
437             .apply();
438     {
439         auto shot = getScreenCapture();
440         // Corners are transparent
441         // top-left
442         shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
443         // top-right
444         shot->expectColor(Rect(layerWidth - testArea, 0, layerWidth, testArea), Color::BLACK);
445         // bottom-left
446         shot->expectColor(Rect(0, layerHeight - testArea, testArea, layerHeight), Color::BLACK);
447         // bottom-right
448         shot->expectColor(Rect(layerWidth - testArea, layerHeight - testArea, layerWidth,
449                                layerHeight),
450                           Color::BLACK);
451 
452         // Area after corner radius is solid
453         // top-left to top-right under the corner
454         shot->expectColor(Rect(0, cornerRadius, layerWidth, cornerRadius + testArea), Color::RED);
455         // bottom-left to bottom-right above the corner
456         shot->expectColor(Rect(0, layerHeight - cornerRadius - testArea, layerWidth,
457                                layerHeight - cornerRadius),
458                           Color::RED);
459         // left side after the corner
460         shot->expectColor(Rect(cornerRadius, 0, cornerRadius + testArea, layerHeight), Color::RED);
461         // right side before the corner
462         shot->expectColor(Rect(layerWidth - cornerRadius - testArea, 0, layerWidth - cornerRadius,
463                                layerHeight),
464                           Color::RED);
465     }
466 }
467 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBackgroundBlurRadiusSimple)468 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBackgroundBlurRadiusSimple) {
469     if (!deviceSupportsBlurs()) GTEST_SKIP();
470     if (!deviceUsesSkiaRenderEngine()) GTEST_SKIP();
471 
472     const auto canvasSize = 256;
473 
474     sp<SurfaceControl> leftLayer;
475     sp<SurfaceControl> rightLayer;
476     sp<SurfaceControl> greenLayer;
477     sp<SurfaceControl> blurLayer;
478     const auto leftRect = Rect(0, 0, canvasSize / 2, canvasSize);
479     const auto rightRect = Rect(canvasSize / 2, 0, canvasSize, canvasSize);
480     const auto blurRect = Rect(0, 0, canvasSize, canvasSize);
481 
482     ASSERT_NO_FATAL_FAILURE(leftLayer =
483                                     createLayer("Left", leftRect.getWidth(), leftRect.getHeight()));
484     ASSERT_NO_FATAL_FAILURE(
485             fillLayerColor(leftLayer, Color::BLUE, leftRect.getWidth(), leftRect.getHeight()));
486     ASSERT_NO_FATAL_FAILURE(greenLayer = createLayer("Green", canvasSize * 2, canvasSize * 2));
487     ASSERT_NO_FATAL_FAILURE(
488             fillLayerColor(greenLayer, Color::GREEN, canvasSize * 2, canvasSize * 2));
489     ASSERT_NO_FATAL_FAILURE(
490             rightLayer = createLayer("Right", rightRect.getWidth(), rightRect.getHeight()));
491     ASSERT_NO_FATAL_FAILURE(
492             fillLayerColor(rightLayer, Color::RED, rightRect.getWidth(), rightRect.getHeight()));
493 
494     Transaction()
495             .setLayer(greenLayer, mLayerZBase)
496             .setLayer(leftLayer, mLayerZBase + 1)
497             .setLayer(rightLayer, mLayerZBase + 2)
498             .setPosition(rightLayer, rightRect.left, rightRect.top)
499             .apply();
500 
501     {
502         auto shot = getScreenCapture();
503         shot->expectColor(leftRect, Color::BLUE);
504         shot->expectColor(rightRect, Color::RED);
505     }
506 
507     ASSERT_NO_FATAL_FAILURE(blurLayer = createColorLayer("BackgroundBlur", Color::TRANSPARENT));
508 
509     const auto blurRadius = canvasSize / 2;
510     Transaction()
511             .setLayer(blurLayer, mLayerZBase + 3)
512             .setBackgroundBlurRadius(blurLayer, blurRadius)
513             .setCrop(blurLayer, blurRect)
514             .setAlpha(blurLayer, 0.0f)
515             .apply();
516 
517     {
518         auto shot = getScreenCapture();
519 
520         const auto stepSize = 1;
521         const auto blurAreaOffset = blurRadius * 0.7f;
522         const auto blurAreaStartX = canvasSize / 2 - blurRadius + blurAreaOffset;
523         const auto blurAreaEndX = canvasSize / 2 + blurRadius - blurAreaOffset;
524         Color previousColor;
525         Color currentColor;
526         for (int y = 0; y < canvasSize; y++) {
527             shot->checkPixel(0, y, /* r = */ 0, /* g = */ 0, /* b = */ 255);
528             previousColor = shot->getPixelColor(0, y);
529             for (int x = blurAreaStartX; x < blurAreaEndX; x += stepSize) {
530                 currentColor = shot->getPixelColor(x, y);
531                 ASSERT_GT(currentColor.r, previousColor.r);
532                 ASSERT_LT(currentColor.b, previousColor.b);
533                 ASSERT_EQ(0, currentColor.g);
534             }
535             shot->checkPixel(canvasSize - 1, y, 255, 0, 0);
536         }
537     }
538 }
539 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBackgroundBlurRadiusOnMultipleLayers)540 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBackgroundBlurRadiusOnMultipleLayers) {
541     if (!deviceSupportsBlurs()) GTEST_SKIP();
542     if (!deviceUsesSkiaRenderEngine()) GTEST_SKIP();
543 
544     auto size = 256;
545     auto center = size / 2;
546     auto blurRadius = 50;
547 
548     sp<SurfaceControl> backgroundLayer;
549     ASSERT_NO_FATAL_FAILURE(backgroundLayer = createLayer("background", size, size));
550     ASSERT_NO_FATAL_FAILURE(fillLayerColor(backgroundLayer, Color::GREEN, size, size));
551 
552     sp<SurfaceControl> leftLayer;
553     ASSERT_NO_FATAL_FAILURE(leftLayer = createLayer("left", size / 2, size));
554     ASSERT_NO_FATAL_FAILURE(fillLayerColor(leftLayer, Color::RED, size / 2, size));
555 
556     sp<SurfaceControl> blurLayer1;
557     auto centralSquareSize = size / 2;
558     ASSERT_NO_FATAL_FAILURE(blurLayer1 =
559                                     createLayer("blur1", centralSquareSize, centralSquareSize));
560     ASSERT_NO_FATAL_FAILURE(
561             fillLayerColor(blurLayer1, Color::BLUE, centralSquareSize, centralSquareSize));
562 
563     sp<SurfaceControl> blurLayer2;
564     ASSERT_NO_FATAL_FAILURE(blurLayer2 = createLayer("blur2", size, size));
565     ASSERT_NO_FATAL_FAILURE(
566             fillLayerColor(blurLayer2, Color::TRANSPARENT, centralSquareSize, centralSquareSize));
567 
568     Transaction()
569             .setBackgroundBlurRadius(blurLayer1, blurRadius)
570             .setBackgroundBlurRadius(blurLayer2, blurRadius)
571             .apply();
572 
573     auto shot = getScreenCapture();
574     shot->expectColor(Rect(center - 5, center - 5, center, center), Color{100, 100, 100, 255},
575                       40 /* tolerance */);
576 }
577 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBackgroundBlurAffectedByParentAlpha)578 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBackgroundBlurAffectedByParentAlpha) {
579     if (!deviceSupportsBlurs()) GTEST_SKIP();
580     if (!deviceUsesSkiaRenderEngine()) GTEST_SKIP();
581 
582     sp<SurfaceControl> left;
583     sp<SurfaceControl> right;
584     sp<SurfaceControl> blur;
585     sp<SurfaceControl> blurParent;
586 
587     const auto size = 256;
588     ASSERT_NO_FATAL_FAILURE(left = createLayer("Left", size, size));
589     ASSERT_NO_FATAL_FAILURE(fillLayerColor(left, Color::BLUE, size, size));
590     ASSERT_NO_FATAL_FAILURE(right = createLayer("Right", size, size));
591     ASSERT_NO_FATAL_FAILURE(fillLayerColor(right, Color::RED, size, size));
592 
593     Transaction()
594             .setLayer(left, mLayerZBase + 1)
595             .setLayer(right, mLayerZBase + 2)
596             .setPosition(right, size, 0)
597             .apply();
598 
599     {
600         auto shot = getScreenCapture();
601         shot->expectColor(Rect(0, 0, size, size), Color::BLUE);
602         shot->expectColor(Rect(size, 0, size * 2, size), Color::RED);
603     }
604 
605     ASSERT_NO_FATAL_FAILURE(blur = createLayer("BackgroundBlur", size * 2, size));
606     ASSERT_NO_FATAL_FAILURE(fillLayerColor(blur, Color::TRANSPARENT, size * 2, size));
607     ASSERT_NO_FATAL_FAILURE(blurParent = createLayer("BackgroundBlurParent", size * 2, size));
608     ASSERT_NO_FATAL_FAILURE(fillLayerColor(blurParent, Color::TRANSPARENT, size * 2, size));
609 
610     Transaction()
611             .setLayer(blurParent, mLayerZBase + 3)
612             .setAlpha(blurParent, 0.5)
613             .setLayer(blur, mLayerZBase + 4)
614             .setBackgroundBlurRadius(blur, size) // set the blur radius to the size of one rect
615             .reparent(blur, blurParent)
616             .apply();
617 
618     {
619         auto shot = getScreenCapture();
620         // assert that outer sides of the red and blue rects are not blended with the other color;
621         // if the blur didn't take into account parent alpha, the outer sides would have traces of
622         // the other color
623         shot->expectColor(Rect(0, 0, size / 2, size), Color::BLUE);
624         shot->expectColor(Rect(size + size / 2, 0, size * 2, size), Color::RED);
625         // assert that middle line has blended red and blur color; adding a tolerance of 10 to
626         // account for future blur algorithm changes
627         shot->expectColor(Rect(size, 0, size + 1, size), {136, 0, 119, 255}, 10);
628     }
629 }
630 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetColorWithBuffer)631 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
632     sp<SurfaceControl> bufferLayer;
633     ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
634     ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
635 
636     // color is ignored
637     Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
638     getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
639 }
640 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetLayerStackBasic)641 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
642     sp<SurfaceControl> layer;
643     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
644     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
645 
646     Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
647     {
648         SCOPED_TRACE("non-existing layer stack");
649         getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
650     }
651 
652     Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
653     {
654         SCOPED_TRACE("original layer stack");
655         getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
656     }
657 }
658 
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetBufferFormat)659 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetBufferFormat) {
660     int32_t width = 100;
661     int32_t height = 100;
662     Rect crop = Rect(0, 0, width, height);
663 
664     sp<SurfaceControl> behindLayer = createColorLayer("Behind layer", Color::RED);
665     sp<SurfaceControl> layer;
666     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, 0, nullptr, nullptr,
667                                                 PIXEL_FORMAT_RGBX_8888));
668 
669     Transaction()
670             .setLayer(layer, INT32_MAX - 1)
671             .show(layer)
672             .setLayerStack(behindLayer, mDisplayLayerStack)
673             .setCrop(behindLayer, crop)
674             .setLayer(behindLayer, INT32_MAX - 2)
675             .show(behindLayer)
676             .apply();
677 
678     sp<Surface> surface = layer->getSurface();
679 
680     sp<GraphicBuffer> buffer =
681             new GraphicBuffer(width, height, PIXEL_FORMAT_RGBX_8888, 1, kUsageFlags, "test");
682     ASSERT_NO_FATAL_FAILURE(
683             TransactionUtils::fillGraphicBufferColor(buffer, crop, Color::TRANSPARENT));
684 
685     if (mLayerType == ISurfaceComposerClient::eFXSurfaceBufferQueue) {
686         Surface::attachAndQueueBufferWithDataspace(surface.get(), buffer, ui::Dataspace::V0_SRGB);
687     } else {
688         Transaction().setBuffer(layer, buffer).apply();
689     }
690 
691     {
692         SCOPED_TRACE("Buffer Opaque Format");
693         auto shot = screenshot();
694         shot->expectColor(crop, Color::BLACK);
695     }
696 
697     buffer = new GraphicBuffer(width, height, PIXEL_FORMAT_RGBA_8888, 1, kUsageFlags, "test");
698     ASSERT_NO_FATAL_FAILURE(
699             TransactionUtils::fillGraphicBufferColor(buffer, crop, Color::TRANSPARENT));
700 
701     if (mLayerType == ISurfaceComposerClient::eFXSurfaceBufferQueue) {
702         Surface::attachAndQueueBufferWithDataspace(surface.get(), buffer, ui::Dataspace::V0_SRGB);
703     } else {
704         Transaction().setBuffer(layer, buffer).apply();
705     }
706 
707     {
708         SCOPED_TRACE("Buffer Transparent Format");
709         auto shot = screenshot();
710         shot->expectColor(crop, Color::RED);
711     }
712 }
713 } // namespace android
714 
715 // TODO(b/129481165): remove the #pragma below and fix conversion issues
716 #pragma clang diagnostic pop // ignored "-Wconversion"
717