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