• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 #include <private/android_filesystem_config.h>
22 
23 #include "LayerTransactionTest.h"
24 
25 namespace android {
26 
27 class ScreenCaptureTest : public LayerTransactionTest {
28 protected:
SetUp()29     virtual void SetUp() {
30         LayerTransactionTest::SetUp();
31         ASSERT_EQ(NO_ERROR, mClient->initCheck());
32 
33         const auto display = SurfaceComposerClient::getInternalDisplayToken();
34         ASSERT_FALSE(display == nullptr);
35 
36         ui::DisplayMode mode;
37         ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(display, &mode));
38         const ui::Size& resolution = mode.resolution;
39 
40         // Background surface
41         mBGSurfaceControl = createLayer(String8("BG Test Surface"), resolution.getWidth(),
42                                         resolution.getHeight(), 0);
43         ASSERT_TRUE(mBGSurfaceControl != nullptr);
44         ASSERT_TRUE(mBGSurfaceControl->isValid());
45         TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
46 
47         // Foreground surface
48         mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
49 
50         ASSERT_TRUE(mFGSurfaceControl != nullptr);
51         ASSERT_TRUE(mFGSurfaceControl->isValid());
52 
53         TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
54 
55         asTransaction([&](Transaction& t) {
56             t.setDisplayLayerStack(display, 0);
57 
58             t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
59 
60             t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
61                     .setPosition(mFGSurfaceControl, 64, 64)
62                     .show(mFGSurfaceControl);
63         });
64     }
65 
TearDown()66     virtual void TearDown() {
67         LayerTransactionTest::TearDown();
68         mBGSurfaceControl = 0;
69         mFGSurfaceControl = 0;
70     }
71 
72     sp<SurfaceControl> mBGSurfaceControl;
73     sp<SurfaceControl> mFGSurfaceControl;
74     std::unique_ptr<ScreenCapture> mCapture;
75 };
76 
TEST_F(ScreenCaptureTest,SetFlagsSecureEUidSystem)77 TEST_F(ScreenCaptureTest, SetFlagsSecureEUidSystem) {
78     sp<SurfaceControl> layer;
79     ASSERT_NO_FATAL_FAILURE(
80             layer = createLayer("test", 32, 32,
81                                 ISurfaceComposerClient::eSecure |
82                                         ISurfaceComposerClient::eFXSurfaceBufferQueue));
83     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
84 
85     Transaction().show(layer).setLayer(layer, INT32_MAX).apply(true);
86 
87     {
88         // Ensure the UID is not root because root has all permissions
89         UIDFaker f(AID_APP_START);
90         ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureDisplay(mCaptureArgs, mCaptureResults));
91     }
92 
93     UIDFaker f(AID_SYSTEM);
94 
95     // By default the system can capture screenshots with secure layers but they
96     // will be blacked out
97     ASSERT_EQ(NO_ERROR, ScreenCapture::captureDisplay(mCaptureArgs, mCaptureResults));
98 
99     {
100         SCOPED_TRACE("as system");
101         auto shot = screenshot();
102         shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
103     }
104 
105     // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
106     // to receive them...we are expected to take care with the results.
107     DisplayCaptureArgs args;
108     args.displayToken = mDisplay;
109     args.captureSecureLayers = true;
110     ASSERT_EQ(NO_ERROR, ScreenCapture::captureDisplay(args, mCaptureResults));
111     ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
112     ScreenCapture sc(mCaptureResults.buffer);
113     sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
114 }
115 
TEST_F(ScreenCaptureTest,CaptureChildSetParentFlagsSecureEUidSystem)116 TEST_F(ScreenCaptureTest, CaptureChildSetParentFlagsSecureEUidSystem) {
117     sp<SurfaceControl> parentLayer;
118     ASSERT_NO_FATAL_FAILURE(
119             parentLayer = createLayer("parent-test", 32, 32,
120                                       ISurfaceComposerClient::eSecure |
121                                               ISurfaceComposerClient::eFXSurfaceBufferQueue));
122     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parentLayer, Color::RED, 32, 32));
123 
124     sp<SurfaceControl> childLayer;
125     ASSERT_NO_FATAL_FAILURE(childLayer = createLayer("child-test", 10, 10,
126                                                      ISurfaceComposerClient::eFXSurfaceBufferQueue,
127                                                      parentLayer.get()));
128     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(childLayer, Color::BLUE, 10, 10));
129 
130     Transaction().show(parentLayer).setLayer(parentLayer, INT32_MAX).show(childLayer).apply(true);
131 
132     UIDFaker f(AID_SYSTEM);
133 
134     {
135         SCOPED_TRACE("as system");
136         auto shot = screenshot();
137         shot->expectColor(Rect(0, 0, 10, 10), Color::BLACK);
138     }
139 
140     // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
141     // to receive them...we are expected to take care with the results.
142     DisplayCaptureArgs args;
143     args.displayToken = mDisplay;
144     args.captureSecureLayers = true;
145     ASSERT_EQ(NO_ERROR, ScreenCapture::captureDisplay(args, mCaptureResults));
146     ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
147     ScreenCapture sc(mCaptureResults.buffer);
148     sc.expectColor(Rect(0, 0, 10, 10), Color::BLUE);
149 }
150 
TEST_F(ScreenCaptureTest,CaptureSingleLayer)151 TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
152     LayerCaptureArgs captureArgs;
153     captureArgs.layerHandle = mBGSurfaceControl->getHandle();
154     ScreenCapture::captureLayers(&mCapture, captureArgs);
155     mCapture->expectBGColor(0, 0);
156     // Doesn't capture FG layer which is at 64, 64
157     mCapture->expectBGColor(64, 64);
158 }
159 
TEST_F(ScreenCaptureTest,CaptureLayerWithChild)160 TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
161     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
162                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
163     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
164 
165     SurfaceComposerClient::Transaction().show(child).apply(true);
166 
167     // Captures mFGSurfaceControl layer and its child.
168     LayerCaptureArgs captureArgs;
169     captureArgs.layerHandle = mFGSurfaceControl->getHandle();
170     ScreenCapture::captureLayers(&mCapture, captureArgs);
171     mCapture->expectFGColor(10, 10);
172     mCapture->expectChildColor(0, 0);
173 }
174 
TEST_F(ScreenCaptureTest,CaptureLayerChildOnly)175 TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
176     auto fgHandle = mFGSurfaceControl->getHandle();
177 
178     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
179                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
180     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
181 
182     SurfaceComposerClient::Transaction().show(child).apply(true);
183 
184     // Captures mFGSurfaceControl's child
185     LayerCaptureArgs captureArgs;
186     captureArgs.layerHandle = fgHandle;
187     captureArgs.childrenOnly = true;
188     ScreenCapture::captureLayers(&mCapture, captureArgs);
189     mCapture->checkPixel(10, 10, 0, 0, 0);
190     mCapture->expectChildColor(0, 0);
191 }
192 
TEST_F(ScreenCaptureTest,CaptureLayerExclude)193 TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
194     auto fgHandle = mFGSurfaceControl->getHandle();
195 
196     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
197                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
198     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
199     sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
200                                               PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
201     TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
202 
203     SurfaceComposerClient::Transaction()
204             .show(child)
205             .show(child2)
206             .setLayer(child, 1)
207             .setLayer(child2, 2)
208             .apply(true);
209 
210     // Child2 would be visible but its excluded, so we should see child1 color instead.
211     LayerCaptureArgs captureArgs;
212     captureArgs.layerHandle = fgHandle;
213     captureArgs.childrenOnly = true;
214     captureArgs.excludeHandles = {child2->getHandle()};
215     ScreenCapture::captureLayers(&mCapture, captureArgs);
216     mCapture->checkPixel(10, 10, 0, 0, 0);
217     mCapture->checkPixel(0, 0, 200, 200, 200);
218 }
219 
220 // Like the last test but verifies that children are also exclude.
TEST_F(ScreenCaptureTest,CaptureLayerExcludeTree)221 TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
222     auto fgHandle = mFGSurfaceControl->getHandle();
223 
224     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
225                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
226     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
227     sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
228                                               PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
229     TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
230     sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
231                                               PIXEL_FORMAT_RGBA_8888, 0, child2.get());
232     TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
233 
234     SurfaceComposerClient::Transaction()
235             .show(child)
236             .show(child2)
237             .show(child3)
238             .setLayer(child, 1)
239             .setLayer(child2, 2)
240             .apply(true);
241 
242     // Child2 would be visible but its excluded, so we should see child1 color instead.
243     LayerCaptureArgs captureArgs;
244     captureArgs.layerHandle = fgHandle;
245     captureArgs.childrenOnly = true;
246     captureArgs.excludeHandles = {child2->getHandle()};
247     ScreenCapture::captureLayers(&mCapture, captureArgs);
248     mCapture->checkPixel(10, 10, 0, 0, 0);
249     mCapture->checkPixel(0, 0, 200, 200, 200);
250 }
251 
TEST_F(ScreenCaptureTest,CaptureTransparent)252 TEST_F(ScreenCaptureTest, CaptureTransparent) {
253     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
254                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
255 
256     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
257 
258     SurfaceComposerClient::Transaction().show(child).apply(true);
259 
260     // Captures child
261     LayerCaptureArgs captureArgs;
262     captureArgs.layerHandle = child->getHandle();
263     captureArgs.sourceCrop = {0, 0, 10, 20};
264     ScreenCapture::captureLayers(&mCapture, captureArgs);
265     mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
266     // Area outside of child's bounds is transparent.
267     mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
268 }
269 
TEST_F(ScreenCaptureTest,DontCaptureRelativeOutsideTree)270 TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
271     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
272                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
273     ASSERT_NE(nullptr, child.get()) << "failed to create surface";
274     sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
275     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
276     TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
277 
278     SurfaceComposerClient::Transaction()
279             .show(child)
280             // Set relative layer above fg layer so should be shown above when computing all layers.
281             .setRelativeLayer(relative, mFGSurfaceControl, 1)
282             .show(relative)
283             .apply(true);
284 
285     // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
286     LayerCaptureArgs captureArgs;
287     captureArgs.layerHandle = mFGSurfaceControl->getHandle();
288     ScreenCapture::captureLayers(&mCapture, captureArgs);
289     mCapture->expectFGColor(10, 10);
290     mCapture->expectChildColor(0, 0);
291 }
292 
TEST_F(ScreenCaptureTest,CaptureRelativeInTree)293 TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
294     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
295                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
296     sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
297                                                 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
298     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
299     TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
300 
301     SurfaceComposerClient::Transaction()
302             .show(child)
303             // Set relative layer below fg layer but relative to child layer so it should be shown
304             // above child layer.
305             .setLayer(relative, -1)
306             .setRelativeLayer(relative, child, 1)
307             .show(relative)
308             .apply(true);
309 
310     // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
311     // relative value should be taken into account, placing it above child layer.
312     LayerCaptureArgs captureArgs;
313     captureArgs.layerHandle = mFGSurfaceControl->getHandle();
314     ScreenCapture::captureLayers(&mCapture, captureArgs);
315     mCapture->expectFGColor(10, 10);
316     // Relative layer is showing on top of child layer
317     mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
318 }
319 
TEST_F(ScreenCaptureTest,CaptureBoundlessLayerWithSourceCrop)320 TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithSourceCrop) {
321     sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
322     SurfaceComposerClient::Transaction().show(child).apply(true);
323 
324     LayerCaptureArgs captureArgs;
325     captureArgs.layerHandle = child->getHandle();
326     captureArgs.sourceCrop = {0, 0, 10, 10};
327     ScreenCapture::captureLayers(&mCapture, captureArgs);
328 
329     mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED);
330 }
331 
TEST_F(ScreenCaptureTest,CaptureBoundedLayerWithoutSourceCrop)332 TEST_F(ScreenCaptureTest, CaptureBoundedLayerWithoutSourceCrop) {
333     sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
334     Rect layerCrop(0, 0, 10, 10);
335     SurfaceComposerClient::Transaction().setCrop(child, layerCrop).show(child).apply(true);
336 
337     LayerCaptureArgs captureArgs;
338     captureArgs.layerHandle = child->getHandle();
339     ScreenCapture::captureLayers(&mCapture, captureArgs);
340 
341     mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED);
342 }
343 
TEST_F(ScreenCaptureTest,CaptureBoundlessLayerWithoutSourceCropFails)344 TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithoutSourceCropFails) {
345     sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
346     SurfaceComposerClient::Transaction().show(child).apply(true);
347 
348     LayerCaptureArgs args;
349     args.layerHandle = child->getHandle();
350 
351     ScreenCaptureResults captureResults;
352     ASSERT_EQ(BAD_VALUE, ScreenCapture::captureLayers(args, captureResults));
353 }
354 
TEST_F(ScreenCaptureTest,CaptureBufferLayerWithoutBufferFails)355 TEST_F(ScreenCaptureTest, CaptureBufferLayerWithoutBufferFails) {
356     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
357                                              PIXEL_FORMAT_RGBA_8888,
358                                              ISurfaceComposerClient::eFXSurfaceBufferState,
359                                              mFGSurfaceControl.get());
360 
361     SurfaceComposerClient::Transaction().show(child).apply(true);
362     sp<GraphicBuffer> outBuffer;
363 
364     LayerCaptureArgs args;
365     args.layerHandle = child->getHandle();
366     args.childrenOnly = false;
367 
368     ScreenCaptureResults captureResults;
369     ASSERT_EQ(BAD_VALUE, ScreenCapture::captureLayers(args, captureResults));
370 
371     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::RED, 32, 32));
372     SurfaceComposerClient::Transaction().apply(true);
373     ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(args, captureResults));
374     ScreenCapture sc(captureResults.buffer);
375     sc.expectColor(Rect(0, 0, 9, 9), Color::RED);
376 }
377 
TEST_F(ScreenCaptureTest,CaptureLayerWithGrandchild)378 TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
379     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
380                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
381     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
382 
383     sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
384                                                   PIXEL_FORMAT_RGBA_8888, 0, child.get());
385 
386     TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
387     SurfaceComposerClient::Transaction()
388             .show(child)
389             .setPosition(grandchild, 5, 5)
390             .show(grandchild)
391             .apply(true);
392 
393     // Captures mFGSurfaceControl, its child, and the grandchild.
394     LayerCaptureArgs captureArgs;
395     captureArgs.layerHandle = mFGSurfaceControl->getHandle();
396     ScreenCapture::captureLayers(&mCapture, captureArgs);
397     mCapture->expectFGColor(10, 10);
398     mCapture->expectChildColor(0, 0);
399     mCapture->checkPixel(5, 5, 50, 50, 50);
400 }
401 
TEST_F(ScreenCaptureTest,CaptureChildOnly)402 TEST_F(ScreenCaptureTest, CaptureChildOnly) {
403     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
404                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
405     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
406 
407     SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
408 
409     // Captures only the child layer, and not the parent.
410     LayerCaptureArgs captureArgs;
411     captureArgs.layerHandle = child->getHandle();
412     ScreenCapture::captureLayers(&mCapture, captureArgs);
413     mCapture->expectChildColor(0, 0);
414     mCapture->expectChildColor(9, 9);
415 }
416 
TEST_F(ScreenCaptureTest,CaptureGrandchildOnly)417 TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
418     sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
419                                              PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
420     TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
421     auto childHandle = child->getHandle();
422 
423     sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
424                                                   PIXEL_FORMAT_RGBA_8888, 0, child.get());
425     TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
426 
427     SurfaceComposerClient::Transaction()
428             .show(child)
429             .setPosition(grandchild, 5, 5)
430             .show(grandchild)
431             .apply(true);
432 
433     // Captures only the grandchild.
434     LayerCaptureArgs captureArgs;
435     captureArgs.layerHandle = grandchild->getHandle();
436     ScreenCapture::captureLayers(&mCapture, captureArgs);
437     mCapture->checkPixel(0, 0, 50, 50, 50);
438     mCapture->checkPixel(4, 4, 50, 50, 50);
439 }
440 
TEST_F(ScreenCaptureTest,CaptureCrop)441 TEST_F(ScreenCaptureTest, CaptureCrop) {
442     sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60,
443                                               ISurfaceComposerClient::eFXSurfaceBufferState);
444     sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
445                                                  PIXEL_FORMAT_RGBA_8888,
446                                                  ISurfaceComposerClient::eFXSurfaceBufferState,
447                                                  redLayer.get());
448 
449     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(redLayer, Color::RED, 60, 60));
450     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(blueLayer, Color::BLUE, 30, 30));
451 
452     SurfaceComposerClient::Transaction()
453             .setLayer(redLayer, INT32_MAX - 1)
454             .show(redLayer)
455             .show(blueLayer)
456             .apply(true);
457 
458     // Capturing full screen should have both red and blue are visible.
459     LayerCaptureArgs captureArgs;
460     captureArgs.layerHandle = redLayer->getHandle();
461     ScreenCapture::captureLayers(&mCapture, captureArgs);
462     mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
463     // red area below the blue area
464     mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
465     // red area to the right of the blue area
466     mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
467 
468     captureArgs.sourceCrop = {0, 0, 30, 30};
469     ScreenCapture::captureLayers(&mCapture, captureArgs);
470     // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
471     // area visible.
472     mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
473     mCapture->checkPixel(30, 30, 0, 0, 0);
474 }
475 
TEST_F(ScreenCaptureTest,CaptureSize)476 TEST_F(ScreenCaptureTest, CaptureSize) {
477   sp<SurfaceControl> redLayer =
478       createLayer(String8("Red surface"), 60, 60, ISurfaceComposerClient::eFXSurfaceBufferState);
479     sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
480                                                  PIXEL_FORMAT_RGBA_8888,
481                                                  ISurfaceComposerClient::eFXSurfaceBufferState,
482                                                  redLayer.get());
483 
484     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(redLayer, Color::RED, 60, 60));
485     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(blueLayer, Color::BLUE, 30, 30));
486 
487     SurfaceComposerClient::Transaction()
488             .setLayer(redLayer, INT32_MAX - 1)
489             .show(redLayer)
490             .show(blueLayer)
491             .apply(true);
492 
493     // Capturing full screen should have both red and blue are visible.
494     LayerCaptureArgs captureArgs;
495     captureArgs.layerHandle = redLayer->getHandle();
496     ScreenCapture::captureLayers(&mCapture, captureArgs);
497     mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
498     // red area below the blue area
499     mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
500     // red area to the right of the blue area
501     mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
502 
503     captureArgs.frameScaleX = 0.5f;
504     captureArgs.frameScaleY = 0.5f;
505     sleep(1);
506 
507     ScreenCapture::captureLayers(&mCapture, captureArgs);
508     // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
509     mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
510     // red area below the blue area
511     mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
512     // red area to the right of the blue area
513     mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
514     mCapture->checkPixel(30, 30, 0, 0, 0);
515 }
516 
TEST_F(ScreenCaptureTest,CaptureInvalidLayer)517 TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
518     sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
519 
520     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
521 
522     auto redLayerHandle = redLayer->getHandle();
523     Transaction().reparent(redLayer, nullptr).apply();
524     redLayer.clear();
525     SurfaceComposerClient::Transaction().apply(true);
526 
527     LayerCaptureArgs args;
528     args.layerHandle = redLayerHandle;
529 
530     ScreenCaptureResults captureResults;
531     // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
532     ASSERT_EQ(NAME_NOT_FOUND, ScreenCapture::captureLayers(args, captureResults));
533 }
534 
TEST_F(ScreenCaptureTest,CaptureSecureLayer)535 TEST_F(ScreenCaptureTest, CaptureSecureLayer) {
536     sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60,
537                                               ISurfaceComposerClient::eFXSurfaceBufferState);
538     sp<SurfaceControl> secureLayer =
539             createLayer(String8("Secure surface"), 30, 30,
540                         ISurfaceComposerClient::eSecure |
541                                 ISurfaceComposerClient::eFXSurfaceBufferState,
542                         redLayer.get());
543     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(redLayer, Color::RED, 60, 60));
544     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(secureLayer, Color::BLUE, 30, 30));
545 
546     auto redLayerHandle = redLayer->getHandle();
547     Transaction()
548             .show(redLayer)
549             .show(secureLayer)
550             .setLayerStack(redLayer, 0)
551             .setLayer(redLayer, INT32_MAX)
552             .apply();
553 
554     LayerCaptureArgs args;
555     args.layerHandle = redLayerHandle;
556     args.childrenOnly = false;
557     ScreenCaptureResults captureResults;
558 
559     {
560         // Ensure the UID is not root because root has all permissions
561         UIDFaker f(AID_APP_START);
562         // Call from outside system with secure layers will result in permission denied
563         ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(args, captureResults));
564     }
565 
566     UIDFaker f(AID_SYSTEM);
567 
568     // From system request, only red layer will be screenshot since the blue layer is secure.
569     // Black will be present where the secure layer is.
570     ScreenCapture::captureLayers(&mCapture, args);
571     mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLACK);
572     mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED);
573 
574     // Passing flag secure so the blue layer should be screenshot too.
575     args.captureSecureLayers = true;
576     ScreenCapture::captureLayers(&mCapture, args);
577     mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLUE);
578     mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED);
579 }
580 
TEST_F(ScreenCaptureTest,CaptureDisplayWithUid)581 TEST_F(ScreenCaptureTest, CaptureDisplayWithUid) {
582     uid_t fakeUid = 12345;
583 
584     DisplayCaptureArgs captureArgs;
585     captureArgs.displayToken = mDisplay;
586 
587     sp<SurfaceControl> layer;
588     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
589                                                 ISurfaceComposerClient::eFXSurfaceBufferQueue,
590                                                 mBGSurfaceControl.get()));
591     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
592 
593     Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
594 
595     // Make sure red layer with the background layer is screenshot.
596     ScreenCapture::captureDisplay(&mCapture, captureArgs);
597     mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
598     mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255});
599 
600     // From non system uid, can't request screenshot without a specified uid.
601     UIDFaker f(fakeUid);
602     ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureDisplay(captureArgs, mCaptureResults));
603 
604     // Make screenshot request with current uid set. No layers were created with the current
605     // uid so screenshot will be black.
606     captureArgs.uid = fakeUid;
607     ScreenCapture::captureDisplay(&mCapture, captureArgs);
608     mCapture->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
609     mCapture->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
610 
611     sp<SurfaceControl> layerWithFakeUid;
612     // Create a new layer with the current uid
613     ASSERT_NO_FATAL_FAILURE(layerWithFakeUid =
614                                     createLayer("new test layer", 32, 32,
615                                                 ISurfaceComposerClient::eFXSurfaceBufferQueue,
616                                                 mBGSurfaceControl.get()));
617     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32));
618     Transaction()
619             .show(layerWithFakeUid)
620             .setLayer(layerWithFakeUid, INT32_MAX)
621             .setPosition(layerWithFakeUid, 128, 128)
622             .apply();
623 
624     // Screenshot from the fakeUid caller with the uid requested allows the layer
625     // with that uid to be screenshotted. Everything else is black
626     ScreenCapture::captureDisplay(&mCapture, captureArgs);
627     mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
628     mCapture->expectBorder(Rect(128, 128, 160, 160), Color::BLACK);
629 }
630 
TEST_F(ScreenCaptureTest,CaptureDisplayPrimaryDisplayOnly)631 TEST_F(ScreenCaptureTest, CaptureDisplayPrimaryDisplayOnly) {
632     sp<SurfaceControl> layer;
633     ASSERT_NO_FATAL_FAILURE(
634             layer = createLayer("test layer", 0, 0, ISurfaceComposerClient::eFXSurfaceEffect));
635 
636     const Color layerColor = Color::RED;
637     const Rect bounds = Rect(10, 10, 40, 40);
638 
639     Transaction()
640             .show(layer)
641             .hide(mFGSurfaceControl)
642             .setLayerStack(layer, 0)
643             .setLayer(layer, INT32_MAX)
644             .setColor(layer, {layerColor.r / 255, layerColor.g / 255, layerColor.b / 255})
645             .setCrop(layer, bounds)
646             .apply();
647 
648     DisplayCaptureArgs captureArgs;
649     captureArgs.displayToken = mDisplay;
650 
651     {
652         ScreenCapture::captureDisplay(&mCapture, captureArgs);
653         mCapture->expectColor(bounds, layerColor);
654         mCapture->expectBorder(bounds, {63, 63, 195, 255});
655     }
656 
657     Transaction()
658             .setFlags(layer, layer_state_t::eLayerSkipScreenshot,
659                       layer_state_t::eLayerSkipScreenshot)
660             .apply();
661 
662     {
663         // Can't screenshot test layer since it now has flag
664         // eLayerSkipScreenshot
665         ScreenCapture::captureDisplay(&mCapture, captureArgs);
666         mCapture->expectColor(bounds, {63, 63, 195, 255});
667         mCapture->expectBorder(bounds, {63, 63, 195, 255});
668     }
669 }
670 
TEST_F(ScreenCaptureTest,CaptureDisplayChildPrimaryDisplayOnly)671 TEST_F(ScreenCaptureTest, CaptureDisplayChildPrimaryDisplayOnly) {
672     sp<SurfaceControl> layer;
673     sp<SurfaceControl> childLayer;
674     ASSERT_NO_FATAL_FAILURE(
675             layer = createLayer("test layer", 0, 0, ISurfaceComposerClient::eFXSurfaceEffect));
676     ASSERT_NO_FATAL_FAILURE(childLayer = createLayer("test layer", 0, 0,
677                                                      ISurfaceComposerClient::eFXSurfaceEffect,
678                                                      layer.get()));
679 
680     const Color layerColor = Color::RED;
681     const Color childColor = Color::BLUE;
682     const Rect bounds = Rect(10, 10, 40, 40);
683     const Rect childBounds = Rect(20, 20, 30, 30);
684 
685     Transaction()
686             .show(layer)
687             .show(childLayer)
688             .hide(mFGSurfaceControl)
689             .setLayerStack(layer, 0)
690             .setLayer(layer, INT32_MAX)
691             .setColor(layer, {layerColor.r / 255, layerColor.g / 255, layerColor.b / 255})
692             .setColor(childLayer, {childColor.r / 255, childColor.g / 255, childColor.b / 255})
693             .setCrop(layer, bounds)
694             .setCrop(childLayer, childBounds)
695             .apply();
696 
697     DisplayCaptureArgs captureArgs;
698     captureArgs.displayToken = mDisplay;
699 
700     {
701         ScreenCapture::captureDisplay(&mCapture, captureArgs);
702         mCapture->expectColor(childBounds, childColor);
703         mCapture->expectBorder(childBounds, layerColor);
704         mCapture->expectBorder(bounds, {63, 63, 195, 255});
705     }
706 
707     Transaction()
708             .setFlags(layer, layer_state_t::eLayerSkipScreenshot,
709                       layer_state_t::eLayerSkipScreenshot)
710             .apply();
711 
712     {
713         // Can't screenshot child layer since the parent has the flag
714         // eLayerSkipScreenshot
715         ScreenCapture::captureDisplay(&mCapture, captureArgs);
716         mCapture->expectColor(childBounds, {63, 63, 195, 255});
717         mCapture->expectBorder(childBounds, {63, 63, 195, 255});
718         mCapture->expectBorder(bounds, {63, 63, 195, 255});
719     }
720 }
721 
TEST_F(ScreenCaptureTest,CaptureLayerWithUid)722 TEST_F(ScreenCaptureTest, CaptureLayerWithUid) {
723     uid_t fakeUid = 12345;
724 
725     sp<SurfaceControl> layer;
726     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
727                                                 ISurfaceComposerClient::eFXSurfaceBufferQueue,
728                                                 mBGSurfaceControl.get()));
729     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
730 
731     Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
732 
733     LayerCaptureArgs captureArgs;
734     captureArgs.layerHandle = mBGSurfaceControl->getHandle();
735     captureArgs.childrenOnly = false;
736 
737     // Make sure red layer with the background layer is screenshot.
738     ScreenCapture::captureLayers(&mCapture, captureArgs);
739     mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
740     mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255});
741 
742     // From non system uid, can't request screenshot without a specified uid.
743     std::unique_ptr<UIDFaker> uidFaker = std::make_unique<UIDFaker>(fakeUid);
744 
745     ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
746 
747     // Make screenshot request with current uid set. No layers were created with the current
748     // uid so screenshot will be black.
749     captureArgs.uid = fakeUid;
750     ScreenCapture::captureLayers(&mCapture, captureArgs);
751     mCapture->expectColor(Rect(0, 0, 32, 32), Color::TRANSPARENT);
752     mCapture->expectBorder(Rect(0, 0, 32, 32), Color::TRANSPARENT);
753 
754     sp<SurfaceControl> layerWithFakeUid;
755     // Create a new layer with the current uid
756     ASSERT_NO_FATAL_FAILURE(layerWithFakeUid =
757                                     createLayer("new test layer", 32, 32,
758                                                 ISurfaceComposerClient::eFXSurfaceBufferQueue,
759                                                 mBGSurfaceControl.get()));
760     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32));
761     Transaction()
762             .show(layerWithFakeUid)
763             .setLayer(layerWithFakeUid, INT32_MAX)
764             .setPosition(layerWithFakeUid, 128, 128)
765             // reparent a layer that was created with a different uid to the new layer.
766             .reparent(layer, layerWithFakeUid)
767             .apply();
768 
769     // Screenshot from the fakeUid caller with the uid requested allows the layer
770     // with that uid to be screenshotted. The child layer is skipped since it was created
771     // from a different uid.
772     ScreenCapture::captureLayers(&mCapture, captureArgs);
773     mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
774     mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
775 
776     // Clear fake calling uid so it's back to system.
777     uidFaker = nullptr;
778     // Screenshot from the test caller with the uid requested allows the layer
779     // with that uid to be screenshotted. The child layer is skipped since it was created
780     // from a different uid.
781     ScreenCapture::captureLayers(&mCapture, captureArgs);
782     mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
783     mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
784 
785     // Screenshot from the fakeUid caller with no uid requested allows everything to be screenshot.
786     captureArgs.uid = -1;
787     ScreenCapture::captureLayers(&mCapture, captureArgs);
788     mCapture->expectColor(Rect(128, 128, 160, 160), Color::RED);
789     mCapture->expectBorder(Rect(128, 128, 160, 160), {63, 63, 195, 255});
790 }
791 
TEST_F(ScreenCaptureTest,CaptureWithGrayscale)792 TEST_F(ScreenCaptureTest, CaptureWithGrayscale) {
793     sp<SurfaceControl> layer;
794     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
795                                                 ISurfaceComposerClient::eFXSurfaceBufferState,
796                                                 mBGSurfaceControl.get()));
797     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
798     Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
799 
800     LayerCaptureArgs captureArgs;
801     captureArgs.layerHandle = layer->getHandle();
802 
803     ScreenCapture::captureLayers(&mCapture, captureArgs);
804     mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
805 
806     captureArgs.grayscale = true;
807 
808     const uint8_t tolerance = 1;
809 
810     // Values based on SurfaceFlinger::calculateColorMatrix
811     float3 luminance{0.213f, 0.715f, 0.072f};
812 
813     ScreenCapture::captureLayers(&mCapture, captureArgs);
814 
815     uint8_t expectedColor = luminance.r * 255;
816     mCapture->expectColor(Rect(0, 0, 32, 32),
817                           Color{expectedColor, expectedColor, expectedColor, 255}, tolerance);
818 
819     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
820     ScreenCapture::captureLayers(&mCapture, captureArgs);
821 
822     expectedColor = luminance.b * 255;
823     mCapture->expectColor(Rect(0, 0, 32, 32),
824                           Color{expectedColor, expectedColor, expectedColor, 255}, tolerance);
825 }
826 
827 // In the following tests we verify successful skipping of a parent layer,
828 // so we use the same verification logic and only change how we mutate
829 // the parent layer to verify that various properties are ignored.
830 class ScreenCaptureChildOnlyTest : public ScreenCaptureTest {
831 public:
SetUp()832     void SetUp() override {
833         ScreenCaptureTest::SetUp();
834 
835         mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
836                                mFGSurfaceControl.get());
837         TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
838 
839         SurfaceComposerClient::Transaction().show(mChild).apply(true);
840     }
841 
verify(std::function<void ()> verifyStartingState)842     void verify(std::function<void()> verifyStartingState) {
843         // Verify starting state before a screenshot is taken.
844         verifyStartingState();
845 
846         // Verify child layer does not inherit any of the properties of its
847         // parent when its screenshot is captured.
848         LayerCaptureArgs captureArgs;
849         captureArgs.layerHandle = mFGSurfaceControl->getHandle();
850         captureArgs.childrenOnly = true;
851         ScreenCapture::captureLayers(&mCapture, captureArgs);
852         mCapture->checkPixel(10, 10, 0, 0, 0);
853         mCapture->expectChildColor(0, 0);
854 
855         // Verify all assumptions are still true after the screenshot is taken.
856         verifyStartingState();
857     }
858 
859     std::unique_ptr<ScreenCapture> mCapture;
860     sp<SurfaceControl> mChild;
861 };
862 
863 // Regression test b/76099859
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentVisibility)864 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
865     SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
866 
867     // Even though the parent is hidden we should still capture the child.
868 
869     // Before and after reparenting, verify child is properly hidden
870     // when rendering full-screen.
871     verify([&] { screenshot()->expectBGColor(64, 64); });
872 }
873 
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentCrop)874 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
875     SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true);
876 
877     // Even though the parent is cropped out we should still capture the child.
878 
879     // Before and after reparenting, verify child is cropped by parent.
880     verify([&] { screenshot()->expectBGColor(65, 65); });
881 }
882 
883 // Regression test b/124372894
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresTransform)884 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
885     SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
886 
887     // We should not inherit the parent scaling.
888 
889     // Before and after reparenting, verify child is properly scaled.
890     verify([&] { screenshot()->expectChildColor(80, 80); });
891 }
892 
893 } // namespace android
894 
895 // TODO(b/129481165): remove the #pragma below and fix conversion issues
896 #pragma clang diagnostic pop // ignored "-Wconversion"
897