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