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 #include <sys/types.h>
19 #include <cstdint>
20 #pragma clang diagnostic push
21 #pragma clang diagnostic ignored "-Wconversion"
22
23 #include <private/android_filesystem_config.h>
24 #include <ui/DisplayState.h>
25
26 #include "LayerTransactionTest.h"
27
28 namespace android {
29
30 class ScreenCaptureTest : public LayerTransactionTest {
31 protected:
SetUp()32 virtual void SetUp() {
33 LayerTransactionTest::SetUp();
34 ASSERT_EQ(NO_ERROR, mClient->initCheck());
35
36 // Root surface
37 mRootSurfaceControl =
38 createLayer(String8("RootTestSurface"), mDisplayWidth, mDisplayHeight, 0);
39 ASSERT_TRUE(mRootSurfaceControl != nullptr);
40 ASSERT_TRUE(mRootSurfaceControl->isValid());
41
42 // Background surface
43 mBGSurfaceControl = createLayer(String8("BG Test Surface"), mDisplayWidth, mDisplayHeight,
44 0, mRootSurfaceControl.get());
45 ASSERT_TRUE(mBGSurfaceControl != nullptr);
46 ASSERT_TRUE(mBGSurfaceControl->isValid());
47 TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
48
49 // Foreground surface
50 mFGSurfaceControl =
51 createLayer(String8("FG Test Surface"), 64, 64, 0, mRootSurfaceControl.get());
52
53 ASSERT_TRUE(mFGSurfaceControl != nullptr);
54 ASSERT_TRUE(mFGSurfaceControl->isValid());
55
56 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
57
58 asTransaction([&](Transaction& t) {
59 t.setDisplayLayerStack(mDisplay, ui::DEFAULT_LAYER_STACK);
60
61 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
62
63 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
64 .setPosition(mFGSurfaceControl, 64, 64)
65 .show(mFGSurfaceControl);
66 });
67
68 mCaptureArgs.sourceCrop = mDisplayRect;
69 mCaptureArgs.layerHandle = mRootSurfaceControl->getHandle();
70 }
71
TearDown()72 virtual void TearDown() {
73 LayerTransactionTest::TearDown();
74 mBGSurfaceControl = 0;
75 mFGSurfaceControl = 0;
76 }
77
78 sp<SurfaceControl> mRootSurfaceControl;
79 sp<SurfaceControl> mBGSurfaceControl;
80 sp<SurfaceControl> mFGSurfaceControl;
81 std::unique_ptr<ScreenCapture> mCapture;
82 LayerCaptureArgs mCaptureArgs;
83 };
84
TEST_F(ScreenCaptureTest,SetFlagsSecureEUidSystem)85 TEST_F(ScreenCaptureTest, SetFlagsSecureEUidSystem) {
86 sp<SurfaceControl> layer;
87 ASSERT_NO_FATAL_FAILURE(
88 layer = createLayer("test", 32, 32,
89 ISurfaceComposerClient::eSecure |
90 ISurfaceComposerClient::eFXSurfaceBufferQueue,
91 mRootSurfaceControl.get()));
92 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
93
94 Transaction().show(layer).setLayer(layer, INT32_MAX).apply(true);
95
96 {
97 // Ensure the UID is not root because root has all permissions
98 UIDFaker f(AID_APP_START);
99 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(mCaptureArgs, mCaptureResults));
100 }
101
102 {
103 UIDFaker f(AID_SYSTEM);
104
105 // By default the system can capture screenshots with secure layers but they
106 // will be blacked out
107 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(mCaptureArgs, mCaptureResults));
108
109 {
110 SCOPED_TRACE("as system");
111 auto shot = screenshot();
112 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
113 }
114
115 mCaptureArgs.captureSecureLayers = true;
116 // AID_SYSTEM is allowed to capture secure content.
117 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(mCaptureArgs, mCaptureResults));
118 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
119 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
120 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
121 }
122
123 {
124 // Attempt secure screenshot from shell since it doesn't have CAPTURE_BLACKOUT_CONTENT
125 // permission, but is allowed normal screenshots.
126 UIDFaker faker(AID_SHELL);
127 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(mCaptureArgs, mCaptureResults));
128 }
129
130 // Remove flag secure from the layer.
131 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
132 {
133 // Assert that screenshot fails without CAPTURE_BLACKOUT_CONTENT when requesting
134 // captureSecureLayers even if there are no actual secure layers on screen.
135 UIDFaker faker(AID_SHELL);
136 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(mCaptureArgs, mCaptureResults));
137 }
138 }
139
TEST_F(ScreenCaptureTest,CaptureChildSetParentFlagsSecureEUidSystem)140 TEST_F(ScreenCaptureTest, CaptureChildSetParentFlagsSecureEUidSystem) {
141 sp<SurfaceControl> parentLayer;
142 ASSERT_NO_FATAL_FAILURE(
143 parentLayer = createLayer("parent-test", 32, 32,
144 ISurfaceComposerClient::eSecure |
145 ISurfaceComposerClient::eFXSurfaceBufferQueue,
146 mRootSurfaceControl.get()));
147 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parentLayer, Color::RED, 32, 32));
148
149 sp<SurfaceControl> childLayer;
150 ASSERT_NO_FATAL_FAILURE(childLayer = createLayer("child-test", 10, 10,
151 ISurfaceComposerClient::eFXSurfaceBufferQueue,
152 parentLayer.get()));
153 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(childLayer, Color::BLUE, 10, 10));
154
155 Transaction().show(parentLayer).setLayer(parentLayer, INT32_MAX).show(childLayer).apply(true);
156
157 UIDFaker f(AID_SYSTEM);
158
159 {
160 SCOPED_TRACE("as system");
161 auto shot = screenshot();
162 shot->expectColor(Rect(0, 0, 10, 10), Color::BLACK);
163 }
164
165 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
166 // to receive them...we are expected to take care with the results.
167 mCaptureArgs.captureSecureLayers = true;
168 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(mCaptureArgs, mCaptureResults));
169 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
170 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
171 sc.expectColor(Rect(0, 0, 10, 10), Color::BLUE);
172 }
173
174 /**
175 * If a parent layer sets the secure flag, but the screenshot requests is for the child hierarchy,
176 * we need to ensure the secure flag is respected from the parent even though the parent isn't
177 * in the captured sub-hierarchy
178 */
TEST_F(ScreenCaptureTest,CaptureChildRespectsParentSecureFlag)179 TEST_F(ScreenCaptureTest, CaptureChildRespectsParentSecureFlag) {
180 Rect size(0, 0, 100, 100);
181 Transaction().hide(mBGSurfaceControl).hide(mFGSurfaceControl).apply();
182 sp<SurfaceControl> parentLayer;
183 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent-test", 0, 0,
184 ISurfaceComposerClient::eHidden,
185 mRootSurfaceControl.get()));
186
187 sp<SurfaceControl> childLayer;
188 ASSERT_NO_FATAL_FAILURE(childLayer = createLayer("child-test", 0, 0,
189 ISurfaceComposerClient::eFXSurfaceBufferState,
190 parentLayer.get()));
191 ASSERT_NO_FATAL_FAILURE(
192 fillBufferLayerColor(childLayer, Color::GREEN, size.width(), size.height()));
193
194 // hide the parent layer to ensure secure flag is passed down to child when screenshotting
195 Transaction().setLayer(parentLayer, INT32_MAX).show(childLayer).apply(true);
196 Transaction()
197 .setFlags(parentLayer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
198 .apply();
199 LayerCaptureArgs captureArgs;
200 captureArgs.layerHandle = childLayer->getHandle();
201 captureArgs.sourceCrop = size;
202 captureArgs.captureSecureLayers = false;
203 {
204 SCOPED_TRACE("parent hidden");
205 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
206 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
207 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
208 sc.expectColor(size, Color::BLACK);
209 }
210
211 captureArgs.captureSecureLayers = true;
212 {
213 SCOPED_TRACE("capture secure parent not visible");
214 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
215 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
216 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
217 sc.expectColor(size, Color::GREEN);
218 }
219
220 Transaction().show(parentLayer).apply();
221 captureArgs.captureSecureLayers = false;
222 {
223 SCOPED_TRACE("parent visible");
224 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
225 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
226 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
227 sc.expectColor(size, Color::BLACK);
228 }
229
230 captureArgs.captureSecureLayers = true;
231 {
232 SCOPED_TRACE("capture secure parent visible");
233 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
234 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
235 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
236 sc.expectColor(size, Color::GREEN);
237 }
238 }
239
TEST_F(ScreenCaptureTest,CaptureOffscreenChildRespectsParentSecureFlag)240 TEST_F(ScreenCaptureTest, CaptureOffscreenChildRespectsParentSecureFlag) {
241 Rect size(0, 0, 100, 100);
242 Transaction().hide(mBGSurfaceControl).hide(mFGSurfaceControl).apply();
243 // Parent layer should be offscreen.
244 sp<SurfaceControl> parentLayer;
245 ASSERT_NO_FATAL_FAILURE(
246 parentLayer = createLayer("parent-test", 0, 0, ISurfaceComposerClient::eHidden));
247
248 sp<SurfaceControl> childLayer;
249 ASSERT_NO_FATAL_FAILURE(childLayer = createLayer("child-test", 0, 0,
250 ISurfaceComposerClient::eFXSurfaceBufferState,
251 parentLayer.get()));
252 ASSERT_NO_FATAL_FAILURE(
253 fillBufferLayerColor(childLayer, Color::GREEN, size.width(), size.height()));
254
255 // hide the parent layer to ensure secure flag is passed down to child when screenshotting
256 Transaction().setLayer(parentLayer, INT32_MAX).show(childLayer).apply(true);
257 Transaction()
258 .setFlags(parentLayer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
259 .apply();
260 LayerCaptureArgs captureArgs;
261 captureArgs.layerHandle = childLayer->getHandle();
262 captureArgs.sourceCrop = size;
263 captureArgs.captureSecureLayers = false;
264 {
265 SCOPED_TRACE("parent hidden");
266 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
267 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
268 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
269 sc.expectColor(size, Color::BLACK);
270 }
271
272 captureArgs.captureSecureLayers = true;
273 {
274 SCOPED_TRACE("capture secure parent not visible");
275 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
276 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
277 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
278 sc.expectColor(size, Color::GREEN);
279 }
280
281 Transaction().show(parentLayer).apply();
282 captureArgs.captureSecureLayers = false;
283 {
284 SCOPED_TRACE("parent visible");
285 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
286 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
287 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
288 sc.expectColor(size, Color::BLACK);
289 }
290
291 captureArgs.captureSecureLayers = true;
292 {
293 SCOPED_TRACE("capture secure parent visible");
294 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
295 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
296 ScreenCapture sc(mCaptureResults.buffer, mCaptureResults.capturedHdrLayers);
297 sc.expectColor(size, Color::GREEN);
298 }
299 }
300
TEST_F(ScreenCaptureTest,CaptureSingleLayer)301 TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
302 LayerCaptureArgs captureArgs;
303 captureArgs.layerHandle = mBGSurfaceControl->getHandle();
304 ScreenCapture::captureLayers(&mCapture, captureArgs);
305 mCapture->expectBGColor(0, 0);
306 // Doesn't capture FG layer which is at 64, 64
307 mCapture->expectBGColor(64, 64);
308 }
309
TEST_F(ScreenCaptureTest,CaptureLayerWithChild)310 TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
311 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
312 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
313 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
314
315 SurfaceComposerClient::Transaction().show(child).apply(true);
316
317 // Captures mFGSurfaceControl layer and its child.
318 LayerCaptureArgs captureArgs;
319 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
320 ScreenCapture::captureLayers(&mCapture, captureArgs);
321 mCapture->expectFGColor(10, 10);
322 mCapture->expectChildColor(0, 0);
323 }
324
TEST_F(ScreenCaptureTest,CaptureLayerChildOnly)325 TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
326 auto fgHandle = mFGSurfaceControl->getHandle();
327
328 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
329 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
330 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
331
332 SurfaceComposerClient::Transaction().show(child).apply(true);
333
334 // Captures mFGSurfaceControl's child
335 LayerCaptureArgs captureArgs;
336 captureArgs.layerHandle = fgHandle;
337 captureArgs.childrenOnly = true;
338 ScreenCapture::captureLayers(&mCapture, captureArgs);
339 mCapture->checkPixel(10, 10, 0, 0, 0);
340 mCapture->expectChildColor(0, 0);
341 }
342
TEST_F(ScreenCaptureTest,CaptureLayerExclude)343 TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
344 auto fgHandle = mFGSurfaceControl->getHandle();
345
346 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
347 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
348 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
349 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
350 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
351 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
352
353 SurfaceComposerClient::Transaction()
354 .show(child)
355 .show(child2)
356 .setLayer(child, 1)
357 .setLayer(child2, 2)
358 .apply(true);
359
360 // Child2 would be visible but its excluded, so we should see child1 color instead.
361 LayerCaptureArgs captureArgs;
362 captureArgs.layerHandle = fgHandle;
363 captureArgs.childrenOnly = true;
364 captureArgs.excludeHandles = {child2->getHandle()};
365 ScreenCapture::captureLayers(&mCapture, captureArgs);
366 mCapture->checkPixel(10, 10, 0, 0, 0);
367 mCapture->checkPixel(0, 0, 200, 200, 200);
368 }
369
TEST_F(ScreenCaptureTest,CaptureLayerExcludeThroughDisplayArgs)370 TEST_F(ScreenCaptureTest, CaptureLayerExcludeThroughDisplayArgs) {
371 mCaptureArgs.excludeHandles = {mFGSurfaceControl->getHandle()};
372 ScreenCapture::captureLayers(&mCapture, mCaptureArgs);
373 mCapture->expectBGColor(0, 0);
374 // Doesn't capture FG layer which is at 64, 64
375 mCapture->expectBGColor(64, 64);
376 }
377
378 // Like the last test but verifies that children are also exclude.
TEST_F(ScreenCaptureTest,CaptureLayerExcludeTree)379 TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
380 auto fgHandle = mFGSurfaceControl->getHandle();
381
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 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
386 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
387 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
388 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
389 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
390 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
391
392 SurfaceComposerClient::Transaction()
393 .show(child)
394 .show(child2)
395 .show(child3)
396 .setLayer(child, 1)
397 .setLayer(child2, 2)
398 .apply(true);
399
400 // Child2 would be visible but its excluded, so we should see child1 color instead.
401 LayerCaptureArgs captureArgs;
402 captureArgs.layerHandle = fgHandle;
403 captureArgs.childrenOnly = true;
404 captureArgs.excludeHandles = {child2->getHandle()};
405 ScreenCapture::captureLayers(&mCapture, captureArgs);
406 mCapture->checkPixel(10, 10, 0, 0, 0);
407 mCapture->checkPixel(0, 0, 200, 200, 200);
408 }
409
TEST_F(ScreenCaptureTest,CaptureTransparent)410 TEST_F(ScreenCaptureTest, CaptureTransparent) {
411 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
412 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
413
414 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
415
416 SurfaceComposerClient::Transaction().show(child).apply(true);
417
418 // Captures child
419 LayerCaptureArgs captureArgs;
420 captureArgs.layerHandle = child->getHandle();
421 captureArgs.sourceCrop = {0, 0, 10, 20};
422 ScreenCapture::captureLayers(&mCapture, captureArgs);
423 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
424 // Area outside of child's bounds is transparent.
425 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
426 }
427
TEST_F(ScreenCaptureTest,DontCaptureRelativeOutsideTree)428 TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
429 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
430 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
431 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
432 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
433 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
434 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
435
436 SurfaceComposerClient::Transaction()
437 .show(child)
438 // Set relative layer above fg layer so should be shown above when computing all layers.
439 .setRelativeLayer(relative, mFGSurfaceControl, 1)
440 .show(relative)
441 .apply(true);
442
443 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
444 LayerCaptureArgs captureArgs;
445 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
446 ScreenCapture::captureLayers(&mCapture, captureArgs);
447 mCapture->expectFGColor(10, 10);
448 mCapture->expectChildColor(0, 0);
449 }
450
TEST_F(ScreenCaptureTest,CaptureRelativeInTree)451 TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
452 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
453 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
454 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
455 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
456 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
457 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
458
459 SurfaceComposerClient::Transaction()
460 .show(child)
461 // Set relative layer below fg layer but relative to child layer so it should be shown
462 // above child layer.
463 .setLayer(relative, -1)
464 .setRelativeLayer(relative, child, 1)
465 .show(relative)
466 .apply(true);
467
468 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
469 // relative value should be taken into account, placing it above child layer.
470 LayerCaptureArgs captureArgs;
471 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
472 ScreenCapture::captureLayers(&mCapture, captureArgs);
473 mCapture->expectFGColor(10, 10);
474 // Relative layer is showing on top of child layer
475 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
476 }
477
TEST_F(ScreenCaptureTest,CaptureBoundlessLayerWithSourceCrop)478 TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithSourceCrop) {
479 sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
480 SurfaceComposerClient::Transaction().show(child).apply(true);
481
482 LayerCaptureArgs captureArgs;
483 captureArgs.layerHandle = child->getHandle();
484 captureArgs.sourceCrop = {0, 0, 10, 10};
485 ScreenCapture::captureLayers(&mCapture, captureArgs);
486
487 mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED);
488 }
489
TEST_F(ScreenCaptureTest,CaptureBoundedLayerWithoutSourceCrop)490 TEST_F(ScreenCaptureTest, CaptureBoundedLayerWithoutSourceCrop) {
491 sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
492 Rect layerCrop(0, 0, 10, 10);
493 SurfaceComposerClient::Transaction().setCrop(child, layerCrop).show(child).apply(true);
494
495 LayerCaptureArgs captureArgs;
496 captureArgs.layerHandle = child->getHandle();
497 ScreenCapture::captureLayers(&mCapture, captureArgs);
498
499 mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED);
500 }
501
TEST_F(ScreenCaptureTest,CaptureBoundlessLayerWithoutSourceCropFails)502 TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithoutSourceCropFails) {
503 sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
504 SurfaceComposerClient::Transaction().show(child).apply(true);
505
506 LayerCaptureArgs args;
507 args.layerHandle = child->getHandle();
508
509 ScreenCaptureResults captureResults;
510 ASSERT_EQ(BAD_VALUE, ScreenCapture::captureLayers(args, captureResults));
511 }
512
TEST_F(ScreenCaptureTest,CaptureBufferLayerWithoutBufferFails)513 TEST_F(ScreenCaptureTest, CaptureBufferLayerWithoutBufferFails) {
514 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
515 PIXEL_FORMAT_RGBA_8888,
516 ISurfaceComposerClient::eFXSurfaceBufferState,
517 mFGSurfaceControl.get());
518
519 SurfaceComposerClient::Transaction().show(child).apply(true);
520 sp<GraphicBuffer> outBuffer;
521
522 LayerCaptureArgs args;
523 args.layerHandle = child->getHandle();
524 args.childrenOnly = false;
525
526 ScreenCaptureResults captureResults;
527 ASSERT_EQ(BAD_VALUE, ScreenCapture::captureLayers(args, captureResults));
528
529 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(child, Color::RED, 32, 32));
530 SurfaceComposerClient::Transaction().apply(true);
531 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(args, captureResults));
532 ScreenCapture sc(captureResults.buffer, captureResults.capturedHdrLayers);
533 sc.expectColor(Rect(0, 0, 9, 9), Color::RED);
534 }
535
TEST_F(ScreenCaptureTest,CaptureLayerWithGrandchild)536 TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
537 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
538 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
539 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
540
541 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
542 PIXEL_FORMAT_RGBA_8888, 0, child.get());
543
544 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
545 SurfaceComposerClient::Transaction()
546 .show(child)
547 .setPosition(grandchild, 5, 5)
548 .show(grandchild)
549 .apply(true);
550
551 // Captures mFGSurfaceControl, its child, and the grandchild.
552 LayerCaptureArgs captureArgs;
553 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
554 ScreenCapture::captureLayers(&mCapture, captureArgs);
555 mCapture->expectFGColor(10, 10);
556 mCapture->expectChildColor(0, 0);
557 mCapture->checkPixel(5, 5, 50, 50, 50);
558 }
559
TEST_F(ScreenCaptureTest,CaptureChildOnly)560 TEST_F(ScreenCaptureTest, CaptureChildOnly) {
561 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
562 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
563 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
564
565 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
566
567 // Captures only the child layer, and not the parent.
568 LayerCaptureArgs captureArgs;
569 captureArgs.layerHandle = child->getHandle();
570 ScreenCapture::captureLayers(&mCapture, captureArgs);
571 mCapture->expectChildColor(0, 0);
572 mCapture->expectChildColor(9, 9);
573 }
574
TEST_F(ScreenCaptureTest,CaptureGrandchildOnly)575 TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
576 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
577 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
578 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
579 auto childHandle = child->getHandle();
580
581 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
582 PIXEL_FORMAT_RGBA_8888, 0, child.get());
583 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
584
585 SurfaceComposerClient::Transaction()
586 .show(child)
587 .setPosition(grandchild, 5, 5)
588 .show(grandchild)
589 .apply(true);
590
591 // Captures only the grandchild.
592 LayerCaptureArgs captureArgs;
593 captureArgs.layerHandle = grandchild->getHandle();
594 ScreenCapture::captureLayers(&mCapture, captureArgs);
595 mCapture->checkPixel(0, 0, 50, 50, 50);
596 mCapture->checkPixel(4, 4, 50, 50, 50);
597 }
598
TEST_F(ScreenCaptureTest,CaptureCrop)599 TEST_F(ScreenCaptureTest, CaptureCrop) {
600 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60,
601 ISurfaceComposerClient::eFXSurfaceBufferState);
602 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
603 PIXEL_FORMAT_RGBA_8888,
604 ISurfaceComposerClient::eFXSurfaceBufferState,
605 redLayer.get());
606
607 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(redLayer, Color::RED, 60, 60));
608 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(blueLayer, Color::BLUE, 30, 30));
609
610 SurfaceComposerClient::Transaction()
611 .setLayer(redLayer, INT32_MAX - 1)
612 .show(redLayer)
613 .show(blueLayer)
614 .apply(true);
615
616 // Capturing full screen should have both red and blue are visible.
617 LayerCaptureArgs captureArgs;
618 captureArgs.layerHandle = redLayer->getHandle();
619 ScreenCapture::captureLayers(&mCapture, captureArgs);
620 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
621 // red area below the blue area
622 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
623 // red area to the right of the blue area
624 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
625
626 captureArgs.sourceCrop = {0, 0, 30, 30};
627 ScreenCapture::captureLayers(&mCapture, captureArgs);
628 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
629 // area visible.
630 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
631 mCapture->checkPixel(30, 30, 0, 0, 0);
632 }
633
TEST_F(ScreenCaptureTest,CaptureSize)634 TEST_F(ScreenCaptureTest, CaptureSize) {
635 sp<SurfaceControl> redLayer =
636 createLayer(String8("Red surface"), 60, 60, ISurfaceComposerClient::eFXSurfaceBufferState);
637 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
638 PIXEL_FORMAT_RGBA_8888,
639 ISurfaceComposerClient::eFXSurfaceBufferState,
640 redLayer.get());
641
642 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(redLayer, Color::RED, 60, 60));
643 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(blueLayer, Color::BLUE, 30, 30));
644
645 SurfaceComposerClient::Transaction()
646 .setLayer(redLayer, INT32_MAX - 1)
647 .show(redLayer)
648 .show(blueLayer)
649 .apply(true);
650
651 // Capturing full screen should have both red and blue are visible.
652 LayerCaptureArgs captureArgs;
653 captureArgs.layerHandle = redLayer->getHandle();
654 ScreenCapture::captureLayers(&mCapture, captureArgs);
655 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
656 // red area below the blue area
657 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
658 // red area to the right of the blue area
659 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
660
661 captureArgs.frameScaleX = 0.5f;
662 captureArgs.frameScaleY = 0.5f;
663 sleep(1);
664
665 ScreenCapture::captureLayers(&mCapture, captureArgs);
666 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
667 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
668 // red area below the blue area
669 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
670 // red area to the right of the blue area
671 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
672 mCapture->checkPixel(30, 30, 0, 0, 0);
673 }
674
TEST_F(ScreenCaptureTest,CaptureInvalidLayer)675 TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
676 LayerCaptureArgs args;
677 args.layerHandle = sp<BBinder>::make();
678
679 ScreenCaptureResults captureResults;
680 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
681 ASSERT_EQ(NAME_NOT_FOUND, ScreenCapture::captureLayers(args, captureResults));
682 }
683
TEST_F(ScreenCaptureTest,CaptureTooLargeLayer)684 TEST_F(ScreenCaptureTest, CaptureTooLargeLayer) {
685 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60);
686 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
687
688 Transaction().show(redLayer).setLayer(redLayer, INT32_MAX).apply(true);
689
690 LayerCaptureArgs captureArgs;
691 captureArgs.layerHandle = redLayer->getHandle();
692 captureArgs.frameScaleX = INT32_MAX / 60;
693 captureArgs.frameScaleY = INT32_MAX / 60;
694
695 ScreenCaptureResults captureResults;
696 ASSERT_EQ(BAD_VALUE, ScreenCapture::captureLayers(captureArgs, captureResults));
697 }
698
TEST_F(ScreenCaptureTest,CaptureSecureLayer)699 TEST_F(ScreenCaptureTest, CaptureSecureLayer) {
700 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60,
701 ISurfaceComposerClient::eFXSurfaceBufferState);
702 sp<SurfaceControl> secureLayer =
703 createLayer(String8("Secure surface"), 30, 30,
704 ISurfaceComposerClient::eSecure |
705 ISurfaceComposerClient::eFXSurfaceBufferState,
706 redLayer.get());
707 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(redLayer, Color::RED, 60, 60));
708 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(secureLayer, Color::BLUE, 30, 30));
709
710 auto redLayerHandle = redLayer->getHandle();
711 Transaction()
712 .show(redLayer)
713 .show(secureLayer)
714 .setLayerStack(redLayer, ui::DEFAULT_LAYER_STACK)
715 .setLayer(redLayer, INT32_MAX)
716 .apply();
717
718 LayerCaptureArgs args;
719 args.layerHandle = redLayerHandle;
720 args.childrenOnly = false;
721 ScreenCaptureResults captureResults;
722
723 {
724 // Ensure the UID is not root because root has all permissions
725 UIDFaker f(AID_APP_START);
726 // Call from outside system with secure layers will result in permission denied
727 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(args, captureResults));
728 }
729
730 UIDFaker f(AID_SYSTEM);
731
732 // From system request, only red layer will be screenshot since the blue layer is secure.
733 // Black will be present where the secure layer is.
734 ScreenCapture::captureLayers(&mCapture, args);
735 mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLACK);
736 mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED);
737
738 // Passing flag secure so the blue layer should be screenshot too.
739 args.captureSecureLayers = true;
740 ScreenCapture::captureLayers(&mCapture, args);
741 mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLUE);
742 mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED);
743 }
744
TEST_F(ScreenCaptureTest,ScreenshotProtectedBuffer)745 TEST_F(ScreenCaptureTest, ScreenshotProtectedBuffer) {
746 const uint32_t bufferWidth = 60;
747 const uint32_t bufferHeight = 60;
748
749 sp<SurfaceControl> layer =
750 createLayer(String8("Colored surface"), bufferWidth, bufferHeight,
751 ISurfaceComposerClient::eFXSurfaceBufferState, mRootSurfaceControl.get());
752
753 Transaction().show(layer).setLayer(layer, INT32_MAX).apply(true);
754
755 sp<Surface> surface = layer->getSurface();
756 ASSERT_TRUE(surface != nullptr);
757 sp<ANativeWindow> anw(surface);
758
759 ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU));
760 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), GRALLOC_USAGE_PROTECTED));
761
762 int fenceFd;
763 ANativeWindowBuffer* buf = nullptr;
764
765 // End test if device does not support USAGE_PROTECTED
766 // b/309965549 This check does not exit the test when running on AVDs
767 status_t err = anw->dequeueBuffer(anw.get(), &buf, &fenceFd);
768 if (err) {
769 return;
770 }
771 anw->queueBuffer(anw.get(), buf, fenceFd);
772
773 // USAGE_PROTECTED buffer is read as a black screen
774 ScreenCaptureResults captureResults;
775 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(mCaptureArgs, captureResults));
776
777 ScreenCapture sc(captureResults.buffer, captureResults.capturedHdrLayers);
778 sc.expectColor(Rect(0, 0, bufferWidth, bufferHeight), Color::BLACK);
779
780 // Reading color data will expectedly result in crash, only check usage bit
781 // b/309965549 Checking that the usage bit is protected does not work for
782 // devices that do not support usage protected.
783 mCaptureArgs.allowProtected = true;
784 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(mCaptureArgs, captureResults));
785 // ASSERT_EQ(GRALLOC_USAGE_PROTECTED, GRALLOC_USAGE_PROTECTED &
786 // captureResults.buffer->getUsage());
787 }
788
TEST_F(ScreenCaptureTest,CaptureLayer)789 TEST_F(ScreenCaptureTest, CaptureLayer) {
790 sp<SurfaceControl> layer;
791 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 0, 0,
792 ISurfaceComposerClient::eFXSurfaceEffect,
793 mRootSurfaceControl.get()));
794
795 const Color layerColor = Color::RED;
796 const Rect bounds = Rect(10, 10, 40, 40);
797
798 Transaction()
799 .show(layer)
800 .hide(mFGSurfaceControl)
801 .setLayer(layer, INT32_MAX)
802 .setColor(layer, {layerColor.r / 255, layerColor.g / 255, layerColor.b / 255})
803 .setCrop(layer, bounds)
804 .apply();
805
806 {
807 ScreenCapture::captureLayers(&mCapture, mCaptureArgs);
808 mCapture->expectColor(bounds, layerColor);
809 mCapture->expectBorder(bounds, {63, 63, 195, 255});
810 }
811
812 Transaction()
813 .setFlags(layer, layer_state_t::eLayerSkipScreenshot,
814 layer_state_t::eLayerSkipScreenshot)
815 .apply();
816
817 {
818 // Can't screenshot test layer since it now has flag
819 // eLayerSkipScreenshot
820 ScreenCapture::captureLayers(&mCapture, mCaptureArgs);
821 mCapture->expectColor(bounds, {63, 63, 195, 255});
822 mCapture->expectBorder(bounds, {63, 63, 195, 255});
823 }
824 }
825
TEST_F(ScreenCaptureTest,CaptureLayerChild)826 TEST_F(ScreenCaptureTest, CaptureLayerChild) {
827 sp<SurfaceControl> layer;
828 sp<SurfaceControl> childLayer;
829 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 0, 0,
830 ISurfaceComposerClient::eFXSurfaceEffect,
831 mRootSurfaceControl.get()));
832 ASSERT_NO_FATAL_FAILURE(childLayer = createLayer("test layer", 0, 0,
833 ISurfaceComposerClient::eFXSurfaceEffect,
834 layer.get()));
835
836 const Color layerColor = Color::RED;
837 const Color childColor = Color::BLUE;
838 const Rect bounds = Rect(10, 10, 40, 40);
839 const Rect childBounds = Rect(20, 20, 30, 30);
840
841 Transaction()
842 .show(layer)
843 .show(childLayer)
844 .hide(mFGSurfaceControl)
845 .setLayer(layer, INT32_MAX)
846 .setColor(layer, {layerColor.r / 255, layerColor.g / 255, layerColor.b / 255})
847 .setColor(childLayer, {childColor.r / 255, childColor.g / 255, childColor.b / 255})
848 .setCrop(layer, bounds)
849 .setCrop(childLayer, childBounds)
850 .apply();
851
852 {
853 ScreenCapture::captureLayers(&mCapture, mCaptureArgs);
854 mCapture->expectColor(childBounds, childColor);
855 mCapture->expectBorder(childBounds, layerColor);
856 mCapture->expectBorder(bounds, {63, 63, 195, 255});
857 }
858
859 Transaction()
860 .setFlags(layer, layer_state_t::eLayerSkipScreenshot,
861 layer_state_t::eLayerSkipScreenshot)
862 .apply();
863
864 {
865 // Can't screenshot child layer since the parent has the flag
866 // eLayerSkipScreenshot
867 ScreenCapture::captureLayers(&mCapture, mCaptureArgs);
868 mCapture->expectColor(childBounds, {63, 63, 195, 255});
869 mCapture->expectBorder(childBounds, {63, 63, 195, 255});
870 mCapture->expectBorder(bounds, {63, 63, 195, 255});
871 }
872 }
873
TEST_F(ScreenCaptureTest,CaptureLayerWithUid)874 TEST_F(ScreenCaptureTest, CaptureLayerWithUid) {
875 uid_t fakeUid = 12345;
876
877 sp<SurfaceControl> layer;
878 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
879 ISurfaceComposerClient::eFXSurfaceBufferQueue,
880 mBGSurfaceControl.get()));
881 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
882
883 Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
884
885 LayerCaptureArgs captureArgs;
886 captureArgs.layerHandle = mBGSurfaceControl->getHandle();
887 captureArgs.childrenOnly = false;
888
889 // Make sure red layer with the background layer is screenshot.
890 ScreenCapture::captureLayers(&mCapture, captureArgs);
891 mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
892 mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255});
893
894 // From non system uid, can't request screenshot without a specified uid.
895 std::unique_ptr<UIDFaker> uidFaker = std::make_unique<UIDFaker>(fakeUid);
896
897 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
898
899 // Make screenshot request with current uid set. No layers were created with the current
900 // uid so screenshot will be black.
901 captureArgs.uid = fakeUid;
902 ScreenCapture::captureLayers(&mCapture, captureArgs);
903 mCapture->expectColor(Rect(0, 0, 32, 32), Color::TRANSPARENT);
904 mCapture->expectBorder(Rect(0, 0, 32, 32), Color::TRANSPARENT);
905
906 sp<SurfaceControl> layerWithFakeUid;
907 // Create a new layer with the current uid
908 ASSERT_NO_FATAL_FAILURE(layerWithFakeUid =
909 createLayer("new test layer", 32, 32,
910 ISurfaceComposerClient::eFXSurfaceBufferQueue,
911 mBGSurfaceControl.get()));
912 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32));
913 Transaction()
914 .show(layerWithFakeUid)
915 .setLayer(layerWithFakeUid, INT32_MAX)
916 .setPosition(layerWithFakeUid, 128, 128)
917 // reparent a layer that was created with a different uid to the new layer.
918 .reparent(layer, layerWithFakeUid)
919 .apply();
920
921 // Screenshot from the fakeUid caller with the uid requested allows the layer
922 // with that uid to be screenshotted. The child layer is skipped since it was created
923 // from a different uid.
924 ScreenCapture::captureLayers(&mCapture, captureArgs);
925 mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
926 mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
927
928 // Clear fake calling uid so it's back to system.
929 uidFaker = nullptr;
930 // Screenshot from the test caller with the uid requested allows the layer
931 // with that uid to be screenshotted. The child layer is skipped since it was created
932 // from a different uid.
933 ScreenCapture::captureLayers(&mCapture, captureArgs);
934 mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
935 mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
936
937 // Screenshot from the fakeUid caller with no uid requested allows everything to be screenshot.
938 captureArgs.uid = -1;
939 ScreenCapture::captureLayers(&mCapture, captureArgs);
940 mCapture->expectColor(Rect(128, 128, 160, 160), Color::RED);
941 mCapture->expectBorder(Rect(128, 128, 160, 160), {63, 63, 195, 255});
942 }
943
TEST_F(ScreenCaptureTest,CaptureWithGrayscale)944 TEST_F(ScreenCaptureTest, CaptureWithGrayscale) {
945 sp<SurfaceControl> layer;
946 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
947 ISurfaceComposerClient::eFXSurfaceBufferState,
948 mBGSurfaceControl.get()));
949 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::RED, 32, 32));
950 Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
951
952 LayerCaptureArgs captureArgs;
953 captureArgs.layerHandle = layer->getHandle();
954
955 ScreenCapture::captureLayers(&mCapture, captureArgs);
956 mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
957
958 captureArgs.grayscale = true;
959
960 const uint8_t tolerance = 1;
961
962 // Values based on SurfaceFlinger::calculateColorMatrix
963 float3 luminance{0.213f, 0.715f, 0.072f};
964
965 ScreenCapture::captureLayers(&mCapture, captureArgs);
966
967 uint8_t expectedColor = luminance.r * 255;
968 mCapture->expectColor(Rect(0, 0, 32, 32),
969 Color{expectedColor, expectedColor, expectedColor, 255}, tolerance);
970
971 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::BLUE, 32, 32));
972 ScreenCapture::captureLayers(&mCapture, captureArgs);
973
974 expectedColor = luminance.b * 255;
975 mCapture->expectColor(Rect(0, 0, 32, 32),
976 Color{expectedColor, expectedColor, expectedColor, 255}, tolerance);
977 }
978
TEST_F(ScreenCaptureTest,CaptureOffscreen)979 TEST_F(ScreenCaptureTest, CaptureOffscreen) {
980 sp<SurfaceControl> layer;
981 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
982 ISurfaceComposerClient::eFXSurfaceBufferState,
983 mBGSurfaceControl.get()));
984 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::RED, 32, 32));
985
986 Transaction().show(layer).hide(mFGSurfaceControl).reparent(layer, nullptr).apply();
987
988 {
989 // Validate that the red layer is not on screen
990 ScreenCapture::captureLayers(&mCapture, mCaptureArgs);
991 mCapture->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), {63, 63, 195, 255});
992 }
993
994 LayerCaptureArgs captureArgs;
995 captureArgs.layerHandle = layer->getHandle();
996
997 ScreenCapture::captureLayers(&mCapture, captureArgs);
998 mCapture->expectSize(32, 32);
999 mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
1000 }
1001
TEST_F(ScreenCaptureTest,CaptureNonHdrLayer)1002 TEST_F(ScreenCaptureTest, CaptureNonHdrLayer) {
1003 sp<SurfaceControl> layer;
1004 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
1005 ISurfaceComposerClient::eFXSurfaceBufferState,
1006 mBGSurfaceControl.get()));
1007 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::BLACK, 32, 32));
1008 Transaction()
1009 .show(layer)
1010 .setLayer(layer, INT32_MAX)
1011 .setDataspace(layer, ui::Dataspace::V0_SRGB)
1012 .apply();
1013
1014 LayerCaptureArgs captureArgs;
1015 captureArgs.layerHandle = layer->getHandle();
1016
1017 ScreenCapture::captureLayers(&mCapture, captureArgs);
1018 mCapture->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1019 ASSERT_FALSE(mCapture->capturedHdrLayers());
1020 }
1021
TEST_F(ScreenCaptureTest,CaptureHdrLayer)1022 TEST_F(ScreenCaptureTest, CaptureHdrLayer) {
1023 sp<SurfaceControl> layer;
1024 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
1025 ISurfaceComposerClient::eFXSurfaceBufferState,
1026 mBGSurfaceControl.get()));
1027 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::BLACK, 32, 32));
1028 Transaction()
1029 .show(layer)
1030 .setLayer(layer, INT32_MAX)
1031 .setDataspace(layer, ui::Dataspace::BT2020_ITU_PQ)
1032 .apply();
1033
1034 LayerCaptureArgs captureArgs;
1035 captureArgs.layerHandle = layer->getHandle();
1036
1037 ScreenCapture::captureLayers(&mCapture, captureArgs);
1038 mCapture->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1039 ASSERT_TRUE(mCapture->capturedHdrLayers());
1040 }
1041
TEST_F(ScreenCaptureTest,captureOffscreenNullSnapshot)1042 TEST_F(ScreenCaptureTest, captureOffscreenNullSnapshot) {
1043 sp<SurfaceControl> layer;
1044 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
1045 ISurfaceComposerClient::eFXSurfaceBufferState,
1046 mBGSurfaceControl.get()));
1047
1048 // A mirrored layer will not have a snapshot. Testing an offscreen mirrored layer
1049 // ensures that the screenshot path handles cases where snapshots are null.
1050 sp<SurfaceControl> mirroredLayer;
1051 ASSERT_NO_FATAL_FAILURE(mirroredLayer = mirrorSurface(layer.get()));
1052
1053 LayerCaptureArgs captureArgs;
1054 captureArgs.layerHandle = mirroredLayer->getHandle();
1055 captureArgs.sourceCrop = Rect(0, 0, 1, 1);
1056
1057 // Screenshot path should only use the children of the layer hierarchy so
1058 // that it will not create a new snapshot. A snapshot would otherwise be
1059 // created to pass on the properties of the parent, which is not needed
1060 // for the purposes of this test since we explicitly want a null snapshot.
1061 captureArgs.childrenOnly = true;
1062 ScreenCapture::captureLayers(&mCapture, captureArgs);
1063 }
1064
1065 // In the following tests we verify successful skipping of a parent layer,
1066 // so we use the same verification logic and only change how we mutate
1067 // the parent layer to verify that various properties are ignored.
1068 class ScreenCaptureChildOnlyTest : public ScreenCaptureTest {
1069 public:
SetUp()1070 void SetUp() override {
1071 ScreenCaptureTest::SetUp();
1072
1073 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
1074 mFGSurfaceControl.get());
1075 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
1076
1077 SurfaceComposerClient::Transaction().show(mChild).apply(true);
1078 }
1079
verify(std::function<void ()> verifyStartingState)1080 void verify(std::function<void()> verifyStartingState) {
1081 // Verify starting state before a screenshot is taken.
1082 verifyStartingState();
1083
1084 // Verify child layer does not inherit any of the properties of its
1085 // parent when its screenshot is captured.
1086 LayerCaptureArgs captureArgs;
1087 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
1088 captureArgs.childrenOnly = true;
1089 ScreenCapture::captureLayers(&mCapture, captureArgs);
1090 mCapture->checkPixel(10, 10, 0, 0, 0);
1091 mCapture->expectChildColor(0, 0);
1092
1093 // Verify all assumptions are still true after the screenshot is taken.
1094 verifyStartingState();
1095 }
1096
1097 std::unique_ptr<ScreenCapture> mCapture;
1098 sp<SurfaceControl> mChild;
1099 };
1100
1101 // Regression test b/76099859
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentVisibility)1102 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
1103 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
1104
1105 // Even though the parent is hidden we should still capture the child.
1106
1107 // Before and after reparenting, verify child is properly hidden
1108 // when rendering full-screen.
1109 verify([&] { screenshot()->expectBGColor(64, 64); });
1110 }
1111
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentCrop)1112 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
1113 SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true);
1114
1115 // Even though the parent is cropped out we should still capture the child.
1116
1117 // Before and after reparenting, verify child is cropped by parent.
1118 verify([&] { screenshot()->expectBGColor(65, 65); });
1119 }
1120
1121 // Regression test b/124372894
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresTransform)1122 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
1123 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
1124
1125 // We should not inherit the parent scaling.
1126
1127 // Before and after reparenting, verify child is properly scaled.
1128 verify([&] { screenshot()->expectChildColor(80, 80); });
1129 }
1130
1131 } // namespace android
1132
1133 // TODO(b/129481165): remove the #pragma below and fix conversion issues
1134 #pragma clang diagnostic pop // ignored "-Wconversion"
1135