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