blob: e1739968c0883639f10d95be3905f2ba8bf0b7d3 [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
36 DisplayConfig config;
37 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
38 const ui::Size& resolution = config.resolution;
39
40 // Background surface
41 mBGSurfaceControl = createLayer(String8("BG Test Surface"), resolution.getWidth(),
42 resolution.getHeight(), 0);
43 ASSERT_TRUE(mBGSurfaceControl != nullptr);
44 ASSERT_TRUE(mBGSurfaceControl->isValid());
45 TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
46
47 // Foreground surface
48 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
49
50 ASSERT_TRUE(mFGSurfaceControl != nullptr);
51 ASSERT_TRUE(mFGSurfaceControl->isValid());
52
53 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
54
55 asTransaction([&](Transaction& t) {
56 t.setDisplayLayerStack(display, 0);
57
58 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
59
60 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
61 .setPosition(mFGSurfaceControl, 64, 64)
62 .show(mFGSurfaceControl);
63 });
64 }
65
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
chaviw8ffc7b82020-08-18 11:25:37 -070087 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureDisplay(mCaptureArgs, mCaptureResults));
chaviw4b9d5e12020-08-04 18:30:35 -070088
89 UIDFaker f(AID_SYSTEM);
90
91 // By default the system can capture screenshots with secure layers but they
92 // will be blacked out
chaviw8ffc7b82020-08-18 11:25:37 -070093 ASSERT_EQ(NO_ERROR, ScreenCapture::captureDisplay(mCaptureArgs, mCaptureResults));
chaviw4b9d5e12020-08-04 18:30:35 -070094
95 {
96 SCOPED_TRACE("as system");
97 auto shot = screenshot();
98 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
99 }
100
101 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
102 // to receive them...we are expected to take care with the results.
103 DisplayCaptureArgs args;
104 args.displayToken = mDisplay;
105 args.captureSecureLayers = true;
chaviw8ffc7b82020-08-18 11:25:37 -0700106 ASSERT_EQ(NO_ERROR, ScreenCapture::captureDisplay(args, mCaptureResults));
chaviw4b9d5e12020-08-04 18:30:35 -0700107 ASSERT_TRUE(mCaptureResults.capturedSecureLayers);
108 ScreenCapture sc(mCaptureResults.buffer);
109 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
110}
111
chaviw3efadb12020-07-27 10:07:15 -0700112TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
chaviw70cb6a42020-07-30 13:57:36 -0700113 LayerCaptureArgs captureArgs;
114 captureArgs.layerHandle = mBGSurfaceControl->getHandle();
115 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700116 mCapture->expectBGColor(0, 0);
117 // Doesn't capture FG layer which is at 64, 64
118 mCapture->expectBGColor(64, 64);
119}
120
121TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
chaviw3efadb12020-07-27 10:07:15 -0700122 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
123 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
124 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
125
126 SurfaceComposerClient::Transaction().show(child).apply(true);
127
128 // Captures mFGSurfaceControl layer and its child.
chaviw70cb6a42020-07-30 13:57:36 -0700129 LayerCaptureArgs captureArgs;
130 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
131 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700132 mCapture->expectFGColor(10, 10);
133 mCapture->expectChildColor(0, 0);
134}
135
136TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
137 auto fgHandle = mFGSurfaceControl->getHandle();
138
139 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
140 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
141 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
142
143 SurfaceComposerClient::Transaction().show(child).apply(true);
144
145 // Captures mFGSurfaceControl's child
chaviw70cb6a42020-07-30 13:57:36 -0700146 LayerCaptureArgs captureArgs;
147 captureArgs.layerHandle = fgHandle;
148 captureArgs.childrenOnly = true;
149 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700150 mCapture->checkPixel(10, 10, 0, 0, 0);
151 mCapture->expectChildColor(0, 0);
152}
153
154TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
155 auto fgHandle = mFGSurfaceControl->getHandle();
156
157 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
158 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
159 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
160 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
161 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
162 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
163
164 SurfaceComposerClient::Transaction()
165 .show(child)
166 .show(child2)
167 .setLayer(child, 1)
168 .setLayer(child2, 2)
169 .apply(true);
170
171 // Child2 would be visible but its excluded, so we should see child1 color instead.
chaviw70cb6a42020-07-30 13:57:36 -0700172 LayerCaptureArgs captureArgs;
173 captureArgs.layerHandle = fgHandle;
174 captureArgs.childrenOnly = true;
175 captureArgs.excludeHandles = {child2->getHandle()};
176 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700177 mCapture->checkPixel(10, 10, 0, 0, 0);
178 mCapture->checkPixel(0, 0, 200, 200, 200);
179}
180
181// Like the last test but verifies that children are also exclude.
182TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
183 auto fgHandle = mFGSurfaceControl->getHandle();
184
185 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
186 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
187 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
188 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
189 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
190 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
191 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
192 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
193 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
194
195 SurfaceComposerClient::Transaction()
196 .show(child)
197 .show(child2)
198 .show(child3)
199 .setLayer(child, 1)
200 .setLayer(child2, 2)
201 .apply(true);
202
203 // Child2 would be visible but its excluded, so we should see child1 color instead.
chaviw70cb6a42020-07-30 13:57:36 -0700204 LayerCaptureArgs captureArgs;
205 captureArgs.layerHandle = fgHandle;
206 captureArgs.childrenOnly = true;
207 captureArgs.excludeHandles = {child2->getHandle()};
208 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700209 mCapture->checkPixel(10, 10, 0, 0, 0);
210 mCapture->checkPixel(0, 0, 200, 200, 200);
211}
212
213TEST_F(ScreenCaptureTest, CaptureTransparent) {
214 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
215 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
216
217 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
218
219 SurfaceComposerClient::Transaction().show(child).apply(true);
220
chaviw3efadb12020-07-27 10:07:15 -0700221 // Captures child
chaviw70cb6a42020-07-30 13:57:36 -0700222 LayerCaptureArgs captureArgs;
223 captureArgs.layerHandle = child->getHandle();
224 captureArgs.sourceCrop = {0, 0, 10, 20};
225 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700226 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
227 // Area outside of child's bounds is transparent.
228 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
229}
230
231TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
chaviw3efadb12020-07-27 10:07:15 -0700232 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
233 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
234 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
235 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
236 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
237 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
238
239 SurfaceComposerClient::Transaction()
240 .show(child)
241 // Set relative layer above fg layer so should be shown above when computing all layers.
Pablo Gamito11dcc222020-09-12 15:49:39 +0000242 .setRelativeLayer(relative, mFGSurfaceControl, 1)
chaviw3efadb12020-07-27 10:07:15 -0700243 .show(relative)
244 .apply(true);
245
246 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
chaviw70cb6a42020-07-30 13:57:36 -0700247 LayerCaptureArgs captureArgs;
248 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
249 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700250 mCapture->expectFGColor(10, 10);
251 mCapture->expectChildColor(0, 0);
252}
253
254TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
chaviw3efadb12020-07-27 10:07:15 -0700255 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
256 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
257 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
258 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
259 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
260 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
261
262 SurfaceComposerClient::Transaction()
263 .show(child)
264 // Set relative layer below fg layer but relative to child layer so it should be shown
265 // above child layer.
266 .setLayer(relative, -1)
Pablo Gamito11dcc222020-09-12 15:49:39 +0000267 .setRelativeLayer(relative, child, 1)
chaviw3efadb12020-07-27 10:07:15 -0700268 .show(relative)
269 .apply(true);
270
271 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
272 // relative value should be taken into account, placing it above child layer.
chaviw70cb6a42020-07-30 13:57:36 -0700273 LayerCaptureArgs captureArgs;
274 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
275 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700276 mCapture->expectFGColor(10, 10);
277 // Relative layer is showing on top of child layer
278 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
279}
280
281TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithSourceCrop) {
282 sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
283 SurfaceComposerClient::Transaction().show(child).apply(true);
284
chaviw70cb6a42020-07-30 13:57:36 -0700285 LayerCaptureArgs captureArgs;
286 captureArgs.layerHandle = child->getHandle();
287 captureArgs.sourceCrop = {0, 0, 10, 10};
288 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700289
290 mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED);
291}
292
293TEST_F(ScreenCaptureTest, CaptureBoundedLayerWithoutSourceCrop) {
294 sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
295 Rect layerCrop(0, 0, 10, 10);
296 SurfaceComposerClient::Transaction().setCrop_legacy(child, layerCrop).show(child).apply(true);
297
chaviw70cb6a42020-07-30 13:57:36 -0700298 LayerCaptureArgs captureArgs;
299 captureArgs.layerHandle = child->getHandle();
300 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700301
302 mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED);
303}
304
305TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithoutSourceCropFails) {
306 sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get());
307 SurfaceComposerClient::Transaction().show(child).apply(true);
308
chaviw3efadb12020-07-27 10:07:15 -0700309 LayerCaptureArgs args;
310 args.layerHandle = child->getHandle();
311
312 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -0700313 ASSERT_EQ(BAD_VALUE, ScreenCapture::captureLayers(args, captureResults));
chaviw3efadb12020-07-27 10:07:15 -0700314}
315
316TEST_F(ScreenCaptureTest, CaptureBufferLayerWithoutBufferFails) {
317 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
318 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
319 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviw3efadb12020-07-27 10:07:15 -0700320 sp<GraphicBuffer> outBuffer;
321
322 LayerCaptureArgs args;
323 args.layerHandle = child->getHandle();
324 args.childrenOnly = false;
325
326 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -0700327 ASSERT_EQ(BAD_VALUE, ScreenCapture::captureLayers(args, captureResults));
chaviw3efadb12020-07-27 10:07:15 -0700328
329 TransactionUtils::fillSurfaceRGBA8(child, Color::RED);
330 SurfaceComposerClient::Transaction().apply(true);
chaviw8ffc7b82020-08-18 11:25:37 -0700331 ASSERT_EQ(NO_ERROR, ScreenCapture::captureLayers(args, captureResults));
chaviw3efadb12020-07-27 10:07:15 -0700332 ScreenCapture sc(captureResults.buffer);
333 sc.expectColor(Rect(0, 0, 9, 9), Color::RED);
334}
335
336TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
chaviw3efadb12020-07-27 10:07:15 -0700337 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
338 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
339 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
340
341 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
342 PIXEL_FORMAT_RGBA_8888, 0, child.get());
343
344 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
345 SurfaceComposerClient::Transaction()
346 .show(child)
347 .setPosition(grandchild, 5, 5)
348 .show(grandchild)
349 .apply(true);
350
351 // Captures mFGSurfaceControl, its child, and the grandchild.
chaviw70cb6a42020-07-30 13:57:36 -0700352 LayerCaptureArgs captureArgs;
353 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
354 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700355 mCapture->expectFGColor(10, 10);
356 mCapture->expectChildColor(0, 0);
357 mCapture->checkPixel(5, 5, 50, 50, 50);
358}
359
360TEST_F(ScreenCaptureTest, CaptureChildOnly) {
361 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
362 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
363 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviw3efadb12020-07-27 10:07:15 -0700364
365 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
366
367 // Captures only the child layer, and not the parent.
chaviw70cb6a42020-07-30 13:57:36 -0700368 LayerCaptureArgs captureArgs;
369 captureArgs.layerHandle = child->getHandle();
370 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700371 mCapture->expectChildColor(0, 0);
372 mCapture->expectChildColor(9, 9);
373}
374
375TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
376 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
377 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
378 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
379 auto childHandle = child->getHandle();
380
381 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
382 PIXEL_FORMAT_RGBA_8888, 0, child.get());
383 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
384
385 SurfaceComposerClient::Transaction()
386 .show(child)
387 .setPosition(grandchild, 5, 5)
388 .show(grandchild)
389 .apply(true);
390
chaviw3efadb12020-07-27 10:07:15 -0700391 // Captures only the grandchild.
chaviw70cb6a42020-07-30 13:57:36 -0700392 LayerCaptureArgs captureArgs;
393 captureArgs.layerHandle = grandchild->getHandle();
394 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700395 mCapture->checkPixel(0, 0, 50, 50, 50);
396 mCapture->checkPixel(4, 4, 50, 50, 50);
397}
398
399TEST_F(ScreenCaptureTest, CaptureCrop) {
400 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
401 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
402 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
403
404 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
405 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
406
407 SurfaceComposerClient::Transaction()
408 .setLayer(redLayer, INT32_MAX - 1)
409 .show(redLayer)
410 .show(blueLayer)
411 .apply(true);
412
chaviw3efadb12020-07-27 10:07:15 -0700413 // Capturing full screen should have both red and blue are visible.
chaviw70cb6a42020-07-30 13:57:36 -0700414 LayerCaptureArgs captureArgs;
415 captureArgs.layerHandle = redLayer->getHandle();
416 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700417 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
418 // red area below the blue area
419 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
420 // red area to the right of the blue area
421 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
422
chaviw70cb6a42020-07-30 13:57:36 -0700423 captureArgs.sourceCrop = {0, 0, 30, 30};
424 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700425 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
426 // area visible.
427 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
428 mCapture->checkPixel(30, 30, 0, 0, 0);
429}
430
431TEST_F(ScreenCaptureTest, CaptureSize) {
432 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
433 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
434 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
435
436 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
437 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
438
439 SurfaceComposerClient::Transaction()
440 .setLayer(redLayer, INT32_MAX - 1)
441 .show(redLayer)
442 .show(blueLayer)
443 .apply(true);
444
chaviw3efadb12020-07-27 10:07:15 -0700445 // Capturing full screen should have both red and blue are visible.
chaviw70cb6a42020-07-30 13:57:36 -0700446 LayerCaptureArgs captureArgs;
447 captureArgs.layerHandle = redLayer->getHandle();
448 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700449 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
450 // red area below the blue area
451 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
452 // red area to the right of the blue area
453 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
454
chaviw70cb6a42020-07-30 13:57:36 -0700455 captureArgs.frameScale = 0.5f;
456 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700457 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
458 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
459 // red area below the blue area
460 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
461 // red area to the right of the blue area
462 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
463 mCapture->checkPixel(30, 30, 0, 0, 0);
464}
465
466TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
467 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
468
469 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
470
471 auto redLayerHandle = redLayer->getHandle();
472 Transaction().reparent(redLayer, nullptr).apply();
473 redLayer.clear();
474 SurfaceComposerClient::Transaction().apply(true);
475
476 LayerCaptureArgs args;
477 args.layerHandle = redLayerHandle;
478
479 ScreenCaptureResults captureResults;
chaviw3efadb12020-07-27 10:07:15 -0700480 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
chaviw8ffc7b82020-08-18 11:25:37 -0700481 ASSERT_EQ(NAME_NOT_FOUND, ScreenCapture::captureLayers(args, captureResults));
chaviw3efadb12020-07-27 10:07:15 -0700482}
483
chaviw70cb6a42020-07-30 13:57:36 -0700484TEST_F(ScreenCaptureTest, CaputureSecureLayer) {
485 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
486 sp<SurfaceControl> secureLayer =
487 createLayer(String8("Secure surface"), 30, 30,
488 ISurfaceComposerClient::eSecure |
489 ISurfaceComposerClient::eFXSurfaceBufferQueue,
490 redLayer.get());
491 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
492 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(secureLayer, Color::BLUE, 30, 30));
493
494 auto redLayerHandle = redLayer->getHandle();
495 Transaction()
496 .show(redLayer)
497 .show(secureLayer)
498 .setLayerStack(redLayer, 0)
499 .setLayer(redLayer, INT32_MAX)
500 .apply();
501
chaviw70cb6a42020-07-30 13:57:36 -0700502 LayerCaptureArgs args;
503 args.layerHandle = redLayerHandle;
504 args.childrenOnly = false;
505 ScreenCaptureResults captureResults;
506
507 // Call from outside system with secure layers will result in permission denied
chaviw8ffc7b82020-08-18 11:25:37 -0700508 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(args, captureResults));
chaviw70cb6a42020-07-30 13:57:36 -0700509
510 UIDFaker f(AID_SYSTEM);
511
512 // From system request, only red layer will be screenshot since the blue layer is secure.
513 // Black will be present where the secure layer is.
514 ScreenCapture::captureLayers(&mCapture, args);
515 mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLACK);
516 mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED);
517
518 // Passing flag secure so the blue layer should be screenshot too.
519 args.captureSecureLayers = true;
520 ScreenCapture::captureLayers(&mCapture, args);
521 mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLUE);
522 mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED);
523}
524
chaviw4b9d5e12020-08-04 18:30:35 -0700525TEST_F(ScreenCaptureTest, CaptureDisplayWithUid) {
526 uid_t fakeUid = 12345;
527
528 DisplayCaptureArgs captureArgs;
529 captureArgs.displayToken = mDisplay;
530
531 sp<SurfaceControl> layer;
532 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
533 ISurfaceComposerClient::eFXSurfaceBufferQueue,
534 mBGSurfaceControl.get()));
535 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
536
537 Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
538
539 // Make sure red layer with the background layer is screenshot.
540 ScreenCapture::captureDisplay(&mCapture, captureArgs);
541 mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
542 mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255});
543
544 // From non system uid, can't request screenshot without a specified uid.
545 UIDFaker f(fakeUid);
chaviw8ffc7b82020-08-18 11:25:37 -0700546 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureDisplay(captureArgs, mCaptureResults));
chaviw4b9d5e12020-08-04 18:30:35 -0700547
548 // Make screenshot request with current uid set. No layers were created with the current
549 // uid so screenshot will be black.
550 captureArgs.uid = fakeUid;
551 ScreenCapture::captureDisplay(&mCapture, captureArgs);
552 mCapture->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
553 mCapture->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
554
555 sp<SurfaceControl> layerWithFakeUid;
556 // Create a new layer with the current uid
557 ASSERT_NO_FATAL_FAILURE(layerWithFakeUid =
558 createLayer("new test layer", 32, 32,
559 ISurfaceComposerClient::eFXSurfaceBufferQueue,
560 mBGSurfaceControl.get()));
561 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32));
562 Transaction()
563 .show(layerWithFakeUid)
564 .setLayer(layerWithFakeUid, INT32_MAX)
565 .setPosition(layerWithFakeUid, 128, 128)
566 .apply();
567
568 // Screenshot from the fakeUid caller with the uid requested allows the layer
569 // with that uid to be screenshotted. Everything else is black
570 ScreenCapture::captureDisplay(&mCapture, captureArgs);
571 mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
572 mCapture->expectBorder(Rect(128, 128, 160, 160), Color::BLACK);
573}
574
chaviwc5676c62020-09-18 15:01:04 -0700575TEST_F(ScreenCaptureTest, CaptureDisplayPrimaryDisplayOnly) {
576 sp<SurfaceControl> layer;
577 ASSERT_NO_FATAL_FAILURE(
578 layer = createLayer("test layer", 0, 0, ISurfaceComposerClient::eFXSurfaceEffect));
579
580 const Color layerColor = Color::RED;
581 const Rect bounds = Rect(10, 10, 40, 40);
582
583 Transaction()
584 .show(layer)
585 .hide(mFGSurfaceControl)
586 .setLayerStack(layer, 0)
587 .setLayer(layer, INT32_MAX)
588 .setColor(layer, {layerColor.r / 255, layerColor.g / 255, layerColor.b / 255})
589 .setCrop_legacy(layer, bounds)
590 .apply();
591
592 DisplayCaptureArgs captureArgs;
593 captureArgs.displayToken = mDisplay;
594
595 {
596 ScreenCapture::captureDisplay(&mCapture, captureArgs);
597 mCapture->expectColor(bounds, layerColor);
598 mCapture->expectBorder(bounds, {63, 63, 195, 255});
599 }
600
601 Transaction()
602 .setFlags(layer, layer_state_t::eLayerSkipScreenshot,
603 layer_state_t::eLayerSkipScreenshot)
604 .apply();
605
606 {
607 // Can't screenshot test layer since it now has flag
608 // eLayerSkipScreenshot
609 ScreenCapture::captureDisplay(&mCapture, captureArgs);
610 mCapture->expectColor(bounds, {63, 63, 195, 255});
611 mCapture->expectBorder(bounds, {63, 63, 195, 255});
612 }
613}
614
615TEST_F(ScreenCaptureTest, CaptureDisplayChildPrimaryDisplayOnly) {
616 sp<SurfaceControl> layer;
617 sp<SurfaceControl> childLayer;
618 ASSERT_NO_FATAL_FAILURE(
619 layer = createLayer("test layer", 0, 0, ISurfaceComposerClient::eFXSurfaceEffect));
620 ASSERT_NO_FATAL_FAILURE(childLayer = createLayer("test layer", 0, 0,
621 ISurfaceComposerClient::eFXSurfaceEffect,
622 layer.get()));
623
624 const Color layerColor = Color::RED;
625 const Color childColor = Color::BLUE;
626 const Rect bounds = Rect(10, 10, 40, 40);
627 const Rect childBounds = Rect(20, 20, 30, 30);
628
629 Transaction()
630 .show(layer)
631 .show(childLayer)
632 .hide(mFGSurfaceControl)
633 .setLayerStack(layer, 0)
634 .setLayer(layer, INT32_MAX)
635 .setColor(layer, {layerColor.r / 255, layerColor.g / 255, layerColor.b / 255})
636 .setColor(childLayer, {childColor.r / 255, childColor.g / 255, childColor.b / 255})
637 .setCrop_legacy(layer, bounds)
638 .setCrop_legacy(childLayer, childBounds)
639 .apply();
640
641 DisplayCaptureArgs captureArgs;
642 captureArgs.displayToken = mDisplay;
643
644 {
645 ScreenCapture::captureDisplay(&mCapture, captureArgs);
646 mCapture->expectColor(childBounds, childColor);
647 mCapture->expectBorder(childBounds, layerColor);
648 mCapture->expectBorder(bounds, {63, 63, 195, 255});
649 }
650
651 Transaction()
652 .setFlags(layer, layer_state_t::eLayerSkipScreenshot,
653 layer_state_t::eLayerSkipScreenshot)
654 .apply();
655
656 {
657 // Can't screenshot child layer since the parent has the flag
658 // eLayerSkipScreenshot
659 ScreenCapture::captureDisplay(&mCapture, captureArgs);
660 mCapture->expectColor(childBounds, {63, 63, 195, 255});
661 mCapture->expectBorder(childBounds, {63, 63, 195, 255});
662 mCapture->expectBorder(bounds, {63, 63, 195, 255});
663 }
664}
665
chaviw4b9d5e12020-08-04 18:30:35 -0700666TEST_F(ScreenCaptureTest, CaptureLayerWithUid) {
667 uid_t fakeUid = 12345;
668
669 sp<SurfaceControl> layer;
670 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
671 ISurfaceComposerClient::eFXSurfaceBufferQueue,
672 mBGSurfaceControl.get()));
673 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
674
675 Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
676
677 LayerCaptureArgs captureArgs;
678 captureArgs.layerHandle = mBGSurfaceControl->getHandle();
679 captureArgs.childrenOnly = false;
680
681 // Make sure red layer with the background layer is screenshot.
682 ScreenCapture::captureLayers(&mCapture, captureArgs);
683 mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
684 mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255});
685
686 // From non system uid, can't request screenshot without a specified uid.
687 std::unique_ptr<UIDFaker> uidFaker = std::make_unique<UIDFaker>(fakeUid);
688
chaviw8ffc7b82020-08-18 11:25:37 -0700689 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
chaviw4b9d5e12020-08-04 18:30:35 -0700690
691 // Make screenshot request with current uid set. No layers were created with the current
692 // uid so screenshot will be black.
693 captureArgs.uid = fakeUid;
694 ScreenCapture::captureLayers(&mCapture, captureArgs);
695 mCapture->expectColor(Rect(0, 0, 32, 32), Color::TRANSPARENT);
696 mCapture->expectBorder(Rect(0, 0, 32, 32), Color::TRANSPARENT);
697
698 sp<SurfaceControl> layerWithFakeUid;
699 // Create a new layer with the current uid
700 ASSERT_NO_FATAL_FAILURE(layerWithFakeUid =
701 createLayer("new test layer", 32, 32,
702 ISurfaceComposerClient::eFXSurfaceBufferQueue,
703 mBGSurfaceControl.get()));
704 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32));
705 Transaction()
706 .show(layerWithFakeUid)
707 .setLayer(layerWithFakeUid, INT32_MAX)
708 .setPosition(layerWithFakeUid, 128, 128)
709 // reparent a layer that was created with a different uid to the new layer.
Pablo Gamito11dcc222020-09-12 15:49:39 +0000710 .reparent(layer, layerWithFakeUid)
chaviw4b9d5e12020-08-04 18:30:35 -0700711 .apply();
712
713 // Screenshot from the fakeUid caller with the uid requested allows the layer
714 // with that uid to be screenshotted. The child layer is skipped since it was created
715 // from a different uid.
716 ScreenCapture::captureLayers(&mCapture, captureArgs);
717 mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
718 mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
719
720 // Clear fake calling uid so it's back to system.
721 uidFaker = nullptr;
722 // Screenshot from the test caller with the uid requested allows the layer
723 // with that uid to be screenshotted. The child layer is skipped since it was created
724 // from a different uid.
725 ScreenCapture::captureLayers(&mCapture, captureArgs);
726 mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
727 mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
728
729 // Screenshot from the fakeUid caller with no uid requested allows everything to be screenshot.
730 captureArgs.uid = -1;
731 ScreenCapture::captureLayers(&mCapture, captureArgs);
732 mCapture->expectColor(Rect(128, 128, 160, 160), Color::RED);
733 mCapture->expectBorder(Rect(128, 128, 160, 160), {63, 63, 195, 255});
734}
735
chaviw3efadb12020-07-27 10:07:15 -0700736// In the following tests we verify successful skipping of a parent layer,
737// so we use the same verification logic and only change how we mutate
738// the parent layer to verify that various properties are ignored.
739class ScreenCaptureChildOnlyTest : public ScreenCaptureTest {
740public:
741 void SetUp() override {
742 ScreenCaptureTest::SetUp();
743
744 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
745 mFGSurfaceControl.get());
746 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
747
748 SurfaceComposerClient::Transaction().show(mChild).apply(true);
749 }
750
751 void verify(std::function<void()> verifyStartingState) {
752 // Verify starting state before a screenshot is taken.
753 verifyStartingState();
754
755 // Verify child layer does not inherit any of the properties of its
756 // parent when its screenshot is captured.
chaviw70cb6a42020-07-30 13:57:36 -0700757 LayerCaptureArgs captureArgs;
758 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
759 captureArgs.childrenOnly = true;
760 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700761 mCapture->checkPixel(10, 10, 0, 0, 0);
762 mCapture->expectChildColor(0, 0);
763
764 // Verify all assumptions are still true after the screenshot is taken.
765 verifyStartingState();
766 }
767
768 std::unique_ptr<ScreenCapture> mCapture;
769 sp<SurfaceControl> mChild;
770};
771
772// Regression test b/76099859
773TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
774 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
775
776 // Even though the parent is hidden we should still capture the child.
777
778 // Before and after reparenting, verify child is properly hidden
779 // when rendering full-screen.
780 verify([&] { screenshot()->expectBGColor(64, 64); });
781}
782
783TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
784 SurfaceComposerClient::Transaction()
785 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
786 .apply(true);
787
788 // Even though the parent is cropped out we should still capture the child.
789
790 // Before and after reparenting, verify child is cropped by parent.
791 verify([&] { screenshot()->expectBGColor(65, 65); });
792}
793
794// Regression test b/124372894
795TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
796 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
797
798 // We should not inherit the parent scaling.
799
800 // Before and after reparenting, verify child is properly scaled.
801 verify([&] { screenshot()->expectChildColor(80, 80); });
802}
803
804} // namespace android