• 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 "LayerTransactionTest.h"
22 
23 namespace android {
24 
25 using android::hardware::graphics::common::V1_1::BufferUsage;
26 
27 ::testing::Environment* const binderEnv =
28         ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
29 
30 class LayerUpdateTest : public LayerTransactionTest {
31 protected:
SetUp()32     virtual void SetUp() {
33         LayerTransactionTest::SetUp();
34         ASSERT_EQ(NO_ERROR, mClient->initCheck());
35 
36         const auto display = SurfaceComposerClient::getInternalDisplayToken();
37         ASSERT_FALSE(display == nullptr);
38 
39         ui::DisplayMode mode;
40         ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(display, &mode));
41         const ui::Size& resolution = mode.resolution;
42 
43         // Background surface
44         mBGSurfaceControl = createLayer(String8("BG Test Surface"), resolution.getWidth(),
45                                         resolution.getHeight(), 0);
46         ASSERT_TRUE(mBGSurfaceControl != nullptr);
47         ASSERT_TRUE(mBGSurfaceControl->isValid());
48         TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
49 
50         // Foreground surface
51         mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
52 
53         ASSERT_TRUE(mFGSurfaceControl != nullptr);
54         ASSERT_TRUE(mFGSurfaceControl->isValid());
55 
56         TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
57 
58         // Synchronization surface
59         mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
60         ASSERT_TRUE(mSyncSurfaceControl != nullptr);
61         ASSERT_TRUE(mSyncSurfaceControl->isValid());
62 
63         TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
64 
65         asTransaction([&](Transaction& t) {
66             t.setDisplayLayerStack(display, ui::DEFAULT_LAYER_STACK);
67 
68             t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
69 
70             t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
71                     .setPosition(mFGSurfaceControl, 64, 64)
72                     .show(mFGSurfaceControl);
73 
74             t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
75                     .setPosition(mSyncSurfaceControl, resolution.getWidth() - 2,
76                                  resolution.getHeight() - 2)
77                     .show(mSyncSurfaceControl);
78         });
79     }
80 
TearDown()81     virtual void TearDown() {
82         LayerTransactionTest::TearDown();
83         mBGSurfaceControl = 0;
84         mFGSurfaceControl = 0;
85         mSyncSurfaceControl = 0;
86     }
87 
waitForPostedBuffers()88     void waitForPostedBuffers() {
89         // Since the sync surface is in synchronous mode (i.e. double buffered)
90         // posting three buffers to it should ensure that at least two
91         // SurfaceFlinger::handlePageFlip calls have been made, which should
92         // guaranteed that a buffer posted to another Surface has been retired.
93         TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
94         TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
95         TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
96     }
97 
98     sp<SurfaceControl> mBGSurfaceControl;
99     sp<SurfaceControl> mFGSurfaceControl;
100 
101     // This surface is used to ensure that the buffers posted to
102     // mFGSurfaceControl have been picked up by SurfaceFlinger.
103     sp<SurfaceControl> mSyncSurfaceControl;
104 };
105 
106 class GeometryLatchingTest : public LayerUpdateTest {
107 protected:
EXPECT_INITIAL_STATE(const char * trace)108     void EXPECT_INITIAL_STATE(const char* trace) {
109         SCOPED_TRACE(trace);
110         ScreenCapture::captureScreen(&sc);
111         // We find the leading edge of the FG surface.
112         sc->expectFGColor(127, 127);
113         sc->expectBGColor(128, 128);
114     }
115 
lockAndFillFGBuffer()116     void lockAndFillFGBuffer() {
117         TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
118     }
119 
unlockFGBuffer()120     void unlockFGBuffer() {
121         sp<Surface> s = mFGSurfaceControl->getSurface();
122         ASSERT_EQ(NO_ERROR, s->unlockAndPost());
123         waitForPostedBuffers();
124     }
125 
completeFGResize()126     void completeFGResize() {
127         TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
128         waitForPostedBuffers();
129     }
130 
131     std::unique_ptr<ScreenCapture> sc;
132 };
133 
134 class CropLatchingTest : public GeometryLatchingTest {
135 protected:
EXPECT_CROPPED_STATE(const char * trace)136     void EXPECT_CROPPED_STATE(const char* trace) {
137         SCOPED_TRACE(trace);
138         ScreenCapture::captureScreen(&sc);
139         // The edge should be moved back one pixel by our crop.
140         sc->expectFGColor(126, 126);
141         sc->expectBGColor(127, 127);
142         sc->expectBGColor(128, 128);
143     }
144 
EXPECT_RESIZE_STATE(const char * trace)145     void EXPECT_RESIZE_STATE(const char* trace) {
146         SCOPED_TRACE(trace);
147         ScreenCapture::captureScreen(&sc);
148         // The FG is now resized too 128,128 at 64,64
149         sc->expectFGColor(64, 64);
150         sc->expectFGColor(191, 191);
151         sc->expectBGColor(192, 192);
152     }
153 };
154 
TEST_F(LayerUpdateTest,LayerWithNoBuffersResizesImmediately)155 TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
156     std::unique_ptr<ScreenCapture> sc;
157 
158     sp<SurfaceControl> childNoBuffer =
159             createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
160                           PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
161     sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
162                                                    PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
163     TransactionUtils::fillSurfaceRGBA8(childBuffer, 200, 200, 200);
164     SurfaceComposerClient::Transaction{}
165             .setCrop(childNoBuffer, Rect(0, 0, 10, 10))
166             .show(childNoBuffer)
167             .show(childBuffer)
168             .apply(true);
169     {
170         ScreenCapture::captureScreen(&sc);
171         sc->expectChildColor(73, 73);
172         sc->expectFGColor(74, 74);
173     }
174     SurfaceComposerClient::Transaction{}.setCrop(childNoBuffer, Rect(0, 0, 20, 20)).apply(true);
175     {
176         ScreenCapture::captureScreen(&sc);
177         sc->expectChildColor(73, 73);
178         sc->expectChildColor(74, 74);
179     }
180 }
181 
TEST_F(LayerUpdateTest,MergingTransactions)182 TEST_F(LayerUpdateTest, MergingTransactions) {
183     std::unique_ptr<ScreenCapture> sc;
184     {
185         SCOPED_TRACE("before move");
186         ScreenCapture::captureScreen(&sc);
187         sc->expectBGColor(0, 12);
188         sc->expectFGColor(75, 75);
189         sc->expectBGColor(145, 145);
190     }
191 
192     Transaction t1, t2;
193     t1.setPosition(mFGSurfaceControl, 128, 128);
194     t2.setPosition(mFGSurfaceControl, 0, 0);
195     // We expect that the position update from t2 now
196     // overwrites the position update from t1.
197     t1.merge(std::move(t2));
198     t1.apply();
199 
200     {
201         ScreenCapture::captureScreen(&sc);
202         sc->expectFGColor(1, 1);
203     }
204 }
205 
TEST_F(LayerUpdateTest,MergingTransactionFlags)206 TEST_F(LayerUpdateTest, MergingTransactionFlags) {
207     Transaction().hide(mFGSurfaceControl).apply();
208     std::unique_ptr<ScreenCapture> sc;
209     {
210         SCOPED_TRACE("before merge");
211         ScreenCapture::captureScreen(&sc);
212         sc->expectBGColor(0, 12);
213         sc->expectBGColor(75, 75);
214         sc->expectBGColor(145, 145);
215     }
216 
217     Transaction t1, t2;
218     t1.show(mFGSurfaceControl);
219     t2.setFlags(mFGSurfaceControl, 0 /* flags */, layer_state_t::eLayerSecure /* mask */);
220     t1.merge(std::move(t2));
221     t1.apply();
222 
223     {
224         SCOPED_TRACE("after merge");
225         ScreenCapture::captureScreen(&sc);
226         sc->expectFGColor(75, 75);
227     }
228 }
229 
230 class ChildLayerTest : public LayerUpdateTest {
231 protected:
SetUp()232     void SetUp() override {
233         LayerUpdateTest::SetUp();
234         mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
235                                mFGSurfaceControl.get());
236         TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
237         waitForPostedBuffers();
238 
239         {
240             SCOPED_TRACE("before anything");
241             mCapture = screenshot();
242             mCapture->expectChildColor(64, 64);
243         }
244     }
TearDown()245     void TearDown() override {
246         LayerUpdateTest::TearDown();
247         mChild = 0;
248     }
249 
250     sp<SurfaceControl> mChild;
251     std::unique_ptr<ScreenCapture> mCapture;
252 };
253 
TEST_F(ChildLayerTest,ChildLayerPositioning)254 TEST_F(ChildLayerTest, ChildLayerPositioning) {
255     asTransaction([&](Transaction& t) {
256         t.show(mChild);
257         t.setPosition(mChild, 10, 10);
258         t.setPosition(mFGSurfaceControl, 64, 64);
259     });
260 
261     {
262         mCapture = screenshot();
263         // Top left of foreground must now be visible
264         mCapture->expectFGColor(64, 64);
265         // But 10 pixels in we should see the child surface
266         mCapture->expectChildColor(74, 74);
267         // And 10 more pixels we should be back to the foreground surface
268         mCapture->expectFGColor(84, 84);
269     }
270 
271     asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
272 
273     {
274         mCapture = screenshot();
275         // Top left of foreground should now be at 0, 0
276         mCapture->expectFGColor(0, 0);
277         // But 10 pixels in we should see the child surface
278         mCapture->expectChildColor(10, 10);
279         // And 10 more pixels we should be back to the foreground surface
280         mCapture->expectFGColor(20, 20);
281     }
282 }
283 
TEST_F(ChildLayerTest,ChildLayerCropping)284 TEST_F(ChildLayerTest, ChildLayerCropping) {
285     asTransaction([&](Transaction& t) {
286         t.show(mChild);
287         t.setPosition(mChild, 0, 0);
288         t.setPosition(mFGSurfaceControl, 0, 0);
289         t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
290     });
291 
292     {
293         mCapture = screenshot();
294         mCapture->expectChildColor(0, 0);
295         mCapture->expectChildColor(4, 4);
296         mCapture->expectBGColor(5, 5);
297     }
298 }
299 
TEST_F(ChildLayerTest,ChildLayerConstraints)300 TEST_F(ChildLayerTest, ChildLayerConstraints) {
301     asTransaction([&](Transaction& t) {
302         t.show(mChild);
303         t.setPosition(mFGSurfaceControl, 0, 0);
304         t.setPosition(mChild, 63, 63);
305     });
306 
307     {
308         mCapture = screenshot();
309         mCapture->expectFGColor(0, 0);
310         // Last pixel in foreground should now be the child.
311         mCapture->expectChildColor(63, 63);
312         // But the child should be constrained and the next pixel
313         // must be the background
314         mCapture->expectBGColor(64, 64);
315     }
316 }
317 
TEST_F(ChildLayerTest,ChildLayerScaling)318 TEST_F(ChildLayerTest, ChildLayerScaling) {
319     asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
320 
321     // Find the boundary between the parent and child
322     {
323         mCapture = screenshot();
324         mCapture->expectChildColor(9, 9);
325         mCapture->expectFGColor(10, 10);
326     }
327 
328     asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
329 
330     // The boundary should be twice as far from the origin now.
331     // The pixels from the last test should all be child now
332     {
333         mCapture = screenshot();
334         mCapture->expectChildColor(9, 9);
335         mCapture->expectChildColor(10, 10);
336         mCapture->expectChildColor(19, 19);
337         mCapture->expectFGColor(20, 20);
338     }
339 }
340 
341 // A child with a scale transform should be cropped by its parent bounds.
TEST_F(ChildLayerTest,ChildLayerScalingCroppedByParent)342 TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
343     asTransaction([&](Transaction& t) {
344         t.setPosition(mFGSurfaceControl, 0, 0);
345         t.setPosition(mChild, 0, 0);
346     });
347 
348     // Find the boundary between the parent and child.
349     {
350         mCapture = screenshot();
351         mCapture->expectChildColor(0, 0);
352         mCapture->expectChildColor(9, 9);
353         mCapture->expectFGColor(10, 10);
354     }
355 
356     asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
357 
358     // The child should fill its parent bounds and be cropped by it.
359     {
360         mCapture = screenshot();
361         mCapture->expectChildColor(0, 0);
362         mCapture->expectChildColor(63, 63);
363         mCapture->expectBGColor(64, 64);
364     }
365 }
366 
TEST_F(ChildLayerTest,ChildLayerAlpha)367 TEST_F(ChildLayerTest, ChildLayerAlpha) {
368     TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
369     TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
370     TransactionUtils::fillSurfaceRGBA8(mChild, 0, 254, 0);
371     waitForPostedBuffers();
372 
373     asTransaction([&](Transaction& t) {
374         t.show(mChild);
375         t.setPosition(mChild, 0, 0);
376         t.setPosition(mFGSurfaceControl, 0, 0);
377     });
378 
379     {
380         mCapture = screenshot();
381         // Unblended child color
382         mCapture->checkPixel(0, 0, 0, 254, 0);
383     }
384 
385     asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
386 
387     {
388         mCapture = screenshot();
389         // Child and BG blended. See b/175352694 for tolerance.
390         mCapture->expectColor(Rect(0, 0, 1, 1), Color{127, 127, 0, 255}, 1);
391     }
392 
393     asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
394 
395     {
396         mCapture = screenshot();
397         // Child and BG blended. See b/175352694 for tolerance.
398         mCapture->expectColor(Rect(0, 0, 1, 1), Color{95, 64, 95, 255}, 1);
399     }
400 }
401 
TEST_F(ChildLayerTest,ChildrenSurviveParentDestruction)402 TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
403     sp<SurfaceControl> mGrandChild =
404             createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
405     TransactionUtils::fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
406 
407     {
408         SCOPED_TRACE("Grandchild visible");
409         ScreenCapture::captureScreen(&mCapture);
410         mCapture->checkPixel(64, 64, 111, 111, 111);
411     }
412 
413     Transaction().reparent(mChild, nullptr).apply();
414     mChild.clear();
415 
416     {
417         SCOPED_TRACE("After destroying child");
418         ScreenCapture::captureScreen(&mCapture);
419         mCapture->expectFGColor(64, 64);
420     }
421 
422     asTransaction([&](Transaction& t) { t.reparent(mGrandChild, mFGSurfaceControl); });
423 
424     {
425         SCOPED_TRACE("After reparenting grandchild");
426         ScreenCapture::captureScreen(&mCapture);
427         mCapture->checkPixel(64, 64, 111, 111, 111);
428     }
429 }
430 
TEST_F(ChildLayerTest,ChildrenRelativeZSurvivesParentDestruction)431 TEST_F(ChildLayerTest, ChildrenRelativeZSurvivesParentDestruction) {
432     sp<SurfaceControl> mGrandChild =
433             createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
434     TransactionUtils::fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
435 
436     // draw grand child behind the foreground surface
437     asTransaction([&](Transaction& t) { t.setRelativeLayer(mGrandChild, mFGSurfaceControl, -1); });
438 
439     {
440         SCOPED_TRACE("Child visible");
441         ScreenCapture::captureScreen(&mCapture);
442         mCapture->checkPixel(64, 64, 200, 200, 200);
443     }
444 
445     asTransaction([&](Transaction& t) {
446         t.reparent(mChild, nullptr);
447         t.reparent(mGrandChild, mFGSurfaceControl);
448     });
449 
450     {
451         SCOPED_TRACE("foreground visible reparenting grandchild");
452         ScreenCapture::captureScreen(&mCapture);
453         mCapture->checkPixel(64, 64, 195, 63, 63);
454     }
455 }
456 
TEST_F(ChildLayerTest,Reparent)457 TEST_F(ChildLayerTest, Reparent) {
458     asTransaction([&](Transaction& t) {
459         t.show(mChild);
460         t.setPosition(mChild, 10, 10);
461         t.setPosition(mFGSurfaceControl, 64, 64);
462     });
463 
464     {
465         mCapture = screenshot();
466         // Top left of foreground must now be visible
467         mCapture->expectFGColor(64, 64);
468         // But 10 pixels in we should see the child surface
469         mCapture->expectChildColor(74, 74);
470         // And 10 more pixels we should be back to the foreground surface
471         mCapture->expectFGColor(84, 84);
472     }
473 
474     asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl); });
475 
476     {
477         mCapture = screenshot();
478         mCapture->expectFGColor(64, 64);
479         // In reparenting we should have exposed the entire foreground surface.
480         mCapture->expectFGColor(74, 74);
481         // And the child layer should now begin at 10, 10 (since the BG
482         // layer is at (0, 0)).
483         mCapture->expectBGColor(9, 9);
484         mCapture->expectChildColor(10, 10);
485     }
486 }
487 
TEST_F(ChildLayerTest,ReparentToNoParent)488 TEST_F(ChildLayerTest, ReparentToNoParent) {
489     asTransaction([&](Transaction& t) {
490         t.show(mChild);
491         t.setPosition(mChild, 10, 10);
492         t.setPosition(mFGSurfaceControl, 64, 64);
493     });
494 
495     {
496         mCapture = screenshot();
497         // Top left of foreground must now be visible
498         mCapture->expectFGColor(64, 64);
499         // But 10 pixels in we should see the child surface
500         mCapture->expectChildColor(74, 74);
501         // And 10 more pixels we should be back to the foreground surface
502         mCapture->expectFGColor(84, 84);
503     }
504     asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
505     {
506         mCapture = screenshot();
507         // The surface should now be offscreen.
508         mCapture->expectFGColor(64, 64);
509         mCapture->expectFGColor(74, 74);
510         mCapture->expectFGColor(84, 84);
511     }
512 }
513 
TEST_F(ChildLayerTest,ReparentFromNoParent)514 TEST_F(ChildLayerTest, ReparentFromNoParent) {
515     sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
516     ASSERT_TRUE(newSurface != nullptr);
517     ASSERT_TRUE(newSurface->isValid());
518 
519     TransactionUtils::fillSurfaceRGBA8(newSurface, 63, 195, 63);
520     asTransaction([&](Transaction& t) {
521         t.hide(mChild);
522         t.show(newSurface);
523         t.setPosition(newSurface, 10, 10);
524         t.setLayer(newSurface, INT32_MAX - 2);
525         t.setPosition(mFGSurfaceControl, 64, 64);
526     });
527 
528     {
529         mCapture = screenshot();
530         // Top left of foreground must now be visible
531         mCapture->expectFGColor(64, 64);
532         // At 10, 10 we should see the new surface
533         mCapture->checkPixel(10, 10, 63, 195, 63);
534     }
535 
536     asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl); });
537 
538     {
539         mCapture = screenshot();
540         // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
541         // mFGSurface, putting it at 74, 74.
542         mCapture->expectFGColor(64, 64);
543         mCapture->checkPixel(74, 74, 63, 195, 63);
544         mCapture->expectFGColor(84, 84);
545     }
546 }
547 
TEST_F(ChildLayerTest,NestedChildren)548 TEST_F(ChildLayerTest, NestedChildren) {
549     sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
550                                                   PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
551     TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
552 
553     {
554         mCapture = screenshot();
555         // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
556         // which begins at 64, 64
557         mCapture->checkPixel(64, 64, 50, 50, 50);
558     }
559 }
560 
TEST_F(ChildLayerTest,ChildLayerRelativeLayer)561 TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
562     sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
563     TransactionUtils::fillSurfaceRGBA8(relative, 255, 255, 255);
564 
565     Transaction t;
566     t.setLayer(relative, INT32_MAX)
567             .setRelativeLayer(mChild, relative, 1)
568             .setPosition(mFGSurfaceControl, 0, 0)
569             .apply(true);
570 
571     // We expect that the child should have been elevated above our
572     // INT_MAX layer even though it's not a child of it.
573     {
574         mCapture = screenshot();
575         mCapture->expectChildColor(0, 0);
576         mCapture->expectChildColor(9, 9);
577         mCapture->checkPixel(10, 10, 255, 255, 255);
578     }
579 }
580 
581 class BoundlessLayerTest : public LayerUpdateTest {
582 protected:
583     std::unique_ptr<ScreenCapture> mCapture;
584 };
585 
586 // Verify setting a size on a buffer layer has no effect.
TEST_F(BoundlessLayerTest,BufferLayerIgnoresSize)587 TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
588     sp<SurfaceControl> bufferLayer =
589             createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
590                           mFGSurfaceControl.get());
591     ASSERT_TRUE(bufferLayer->isValid());
592     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
593     asTransaction([&](Transaction& t) { t.show(bufferLayer); });
594     {
595         mCapture = screenshot();
596         // Top left of background must now be visible
597         mCapture->expectBGColor(0, 0);
598         // Foreground Surface bounds must be color layer
599         mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
600         // Buffer layer should not extend past buffer bounds
601         mCapture->expectFGColor(95, 95);
602     }
603 }
604 
605 // Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
606 // which will crop the color layer.
TEST_F(BoundlessLayerTest,BoundlessColorLayerFillsParentBufferBounds)607 TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
608     sp<SurfaceControl> colorLayer =
609             createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
610                           ISurfaceComposerClient::eFXSurfaceEffect, mFGSurfaceControl.get());
611     ASSERT_TRUE(colorLayer->isValid());
612     asTransaction([&](Transaction& t) {
613         t.setColor(colorLayer, half3{0, 0, 0});
614         t.show(colorLayer);
615     });
616     {
617         mCapture = screenshot();
618         // Top left of background must now be visible
619         mCapture->expectBGColor(0, 0);
620         // Foreground Surface bounds must be color layer
621         mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
622         // Color layer should not extend past foreground bounds
623         mCapture->expectBGColor(129, 129);
624     }
625 }
626 
627 // Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
628 // a crop which will be used to crop the color layer.
TEST_F(BoundlessLayerTest,BoundlessColorLayerFillsParentCropBounds)629 TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
630     sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
631                                                  0 /* flags */, mFGSurfaceControl.get());
632     ASSERT_TRUE(cropLayer->isValid());
633     sp<SurfaceControl> colorLayer =
634             createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
635                           ISurfaceComposerClient::eFXSurfaceEffect, cropLayer.get());
636     ASSERT_TRUE(colorLayer->isValid());
637     asTransaction([&](Transaction& t) {
638         t.setCrop(cropLayer, Rect(5, 5, 10, 10));
639         t.setColor(colorLayer, half3{0, 0, 0});
640         t.show(cropLayer);
641         t.show(colorLayer);
642     });
643     {
644         mCapture = screenshot();
645         // Top left of background must now be visible
646         mCapture->expectBGColor(0, 0);
647         // Top left of foreground must now be visible
648         mCapture->expectFGColor(64, 64);
649         // 5 pixels from the foreground we should see the child surface
650         mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
651         // 10 pixels from the foreground we should be back to the foreground surface
652         mCapture->expectFGColor(74, 74);
653     }
654 }
655 
656 // Verify for boundless layer with no children, their transforms have no effect.
TEST_F(BoundlessLayerTest,BoundlessColorLayerTransformHasNoEffect)657 TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
658     sp<SurfaceControl> colorLayer =
659             createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
660                           ISurfaceComposerClient::eFXSurfaceEffect, mFGSurfaceControl.get());
661     ASSERT_TRUE(colorLayer->isValid());
662     asTransaction([&](Transaction& t) {
663         t.setPosition(colorLayer, 320, 320);
664         t.setMatrix(colorLayer, 2, 0, 0, 2);
665         t.setColor(colorLayer, half3{0, 0, 0});
666         t.show(colorLayer);
667     });
668     {
669         mCapture = screenshot();
670         // Top left of background must now be visible
671         mCapture->expectBGColor(0, 0);
672         // Foreground Surface bounds must be color layer
673         mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
674         // Color layer should not extend past foreground bounds
675         mCapture->expectBGColor(129, 129);
676     }
677 }
678 
679 // Verify for boundless layer with children, their transforms have an effect.
TEST_F(BoundlessLayerTest,IntermediateBoundlessLayerCanSetTransform)680 TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
681     sp<SurfaceControl> boundlessLayerRightShift =
682             createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
683                           0 /* flags */, mFGSurfaceControl.get());
684     ASSERT_TRUE(boundlessLayerRightShift->isValid());
685     sp<SurfaceControl> boundlessLayerDownShift =
686             createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
687                           0 /* flags */, boundlessLayerRightShift.get());
688     ASSERT_TRUE(boundlessLayerDownShift->isValid());
689     sp<SurfaceControl> colorLayer =
690             createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
691                           ISurfaceComposerClient::eFXSurfaceEffect, boundlessLayerDownShift.get());
692     ASSERT_TRUE(colorLayer->isValid());
693     asTransaction([&](Transaction& t) {
694         t.setPosition(boundlessLayerRightShift, 32, 0);
695         t.show(boundlessLayerRightShift);
696         t.setPosition(boundlessLayerDownShift, 0, 32);
697         t.show(boundlessLayerDownShift);
698         t.setCrop(colorLayer, Rect(0, 0, 64, 64));
699         t.setColor(colorLayer, half3{0, 0, 0});
700         t.show(colorLayer);
701     });
702     {
703         mCapture = screenshot();
704         // Top left of background must now be visible
705         mCapture->expectBGColor(0, 0);
706         // Top left of foreground must now be visible
707         mCapture->expectFGColor(64, 64);
708         // Foreground Surface bounds must be color layer
709         mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
710         // Color layer should not extend past foreground bounds
711         mCapture->expectBGColor(129, 129);
712     }
713 }
714 
715 // Verify child layers do not get clipped if they temporarily move into the negative
716 // coordinate space as the result of an intermediate transformation.
TEST_F(BoundlessLayerTest,IntermediateBoundlessLayerDoNotCrop)717 TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
718     sp<SurfaceControl> boundlessLayer =
719             mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
720                                    0 /* flags */, mFGSurfaceControl->getHandle());
721     ASSERT_TRUE(boundlessLayer != nullptr);
722     ASSERT_TRUE(boundlessLayer->isValid());
723     sp<SurfaceControl> colorLayer =
724             mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
725                                    ISurfaceComposerClient::eFXSurfaceEffect,
726                                    boundlessLayer->getHandle());
727     ASSERT_TRUE(colorLayer != nullptr);
728     ASSERT_TRUE(colorLayer->isValid());
729     asTransaction([&](Transaction& t) {
730         // shift child layer off bounds. If this layer was not boundless, we will
731         // expect the child layer to be cropped.
732         t.setPosition(boundlessLayer, 32, 32);
733         t.show(boundlessLayer);
734         t.setCrop(colorLayer, Rect(0, 0, 64, 64));
735         // undo shift by parent
736         t.setPosition(colorLayer, -32, -32);
737         t.setColor(colorLayer, half3{0, 0, 0});
738         t.show(colorLayer);
739     });
740     {
741         mCapture = screenshot();
742         // Top left of background must now be visible
743         mCapture->expectBGColor(0, 0);
744         // Foreground Surface bounds must be color layer
745         mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
746         // Color layer should not extend past foreground bounds
747         mCapture->expectBGColor(129, 129);
748     }
749 }
750 
751 // Verify for boundless root layers with children, their transforms have an effect.
TEST_F(BoundlessLayerTest,RootBoundlessLayerCanSetTransform)752 TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
753     sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
754                                                           PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
755     ASSERT_TRUE(rootBoundlessLayer->isValid());
756     sp<SurfaceControl> colorLayer =
757             createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
758                           ISurfaceComposerClient::eFXSurfaceEffect, rootBoundlessLayer.get());
759 
760     ASSERT_TRUE(colorLayer->isValid());
761     asTransaction([&](Transaction& t) {
762         t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
763         t.setPosition(rootBoundlessLayer, 32, 32);
764         t.show(rootBoundlessLayer);
765         t.setCrop(colorLayer, Rect(0, 0, 64, 64));
766         t.setColor(colorLayer, half3{0, 0, 0});
767         t.show(colorLayer);
768         t.hide(mFGSurfaceControl);
769     });
770     {
771         mCapture = screenshot();
772         // Top left of background must now be visible
773         mCapture->expectBGColor(0, 0);
774         // Top left of foreground must now be visible
775         mCapture->expectBGColor(31, 31);
776         // Foreground Surface bounds must be color layer
777         mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
778         // Color layer should not extend past foreground bounds
779         mCapture->expectBGColor(97, 97);
780     }
781 }
782 
783 } // namespace android
784 
785 // TODO(b/129481165): remove the #pragma below and fix conversion issues
786 #pragma clang diagnostic pop // ignored "-Wconversion"
787