blob: 962a0cf7afe3ef6d7bd85b0c1d70e108aaf3155d [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.
chaviw70cb6a42020-07-30 13:57:36 -0700242 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 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)
267 .setRelativeLayer(relative, child->getHandle(), 1)
268 .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
575TEST_F(ScreenCaptureTest, CaptureLayerWithUid) {
576 uid_t fakeUid = 12345;
577
578 sp<SurfaceControl> layer;
579 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32,
580 ISurfaceComposerClient::eFXSurfaceBufferQueue,
581 mBGSurfaceControl.get()));
582 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
583
584 Transaction().show(layer).setLayer(layer, INT32_MAX).apply();
585
586 LayerCaptureArgs captureArgs;
587 captureArgs.layerHandle = mBGSurfaceControl->getHandle();
588 captureArgs.childrenOnly = false;
589
590 // Make sure red layer with the background layer is screenshot.
591 ScreenCapture::captureLayers(&mCapture, captureArgs);
592 mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED);
593 mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255});
594
595 // From non system uid, can't request screenshot without a specified uid.
596 std::unique_ptr<UIDFaker> uidFaker = std::make_unique<UIDFaker>(fakeUid);
597
chaviw8ffc7b82020-08-18 11:25:37 -0700598 ASSERT_EQ(PERMISSION_DENIED, ScreenCapture::captureLayers(captureArgs, mCaptureResults));
chaviw4b9d5e12020-08-04 18:30:35 -0700599
600 // Make screenshot request with current uid set. No layers were created with the current
601 // uid so screenshot will be black.
602 captureArgs.uid = fakeUid;
603 ScreenCapture::captureLayers(&mCapture, captureArgs);
604 mCapture->expectColor(Rect(0, 0, 32, 32), Color::TRANSPARENT);
605 mCapture->expectBorder(Rect(0, 0, 32, 32), Color::TRANSPARENT);
606
607 sp<SurfaceControl> layerWithFakeUid;
608 // Create a new layer with the current uid
609 ASSERT_NO_FATAL_FAILURE(layerWithFakeUid =
610 createLayer("new test layer", 32, 32,
611 ISurfaceComposerClient::eFXSurfaceBufferQueue,
612 mBGSurfaceControl.get()));
613 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32));
614 Transaction()
615 .show(layerWithFakeUid)
616 .setLayer(layerWithFakeUid, INT32_MAX)
617 .setPosition(layerWithFakeUid, 128, 128)
618 // reparent a layer that was created with a different uid to the new layer.
619 .reparent(layer, layerWithFakeUid->getHandle())
620 .apply();
621
622 // Screenshot from the fakeUid caller with the uid requested allows the layer
623 // with that uid to be screenshotted. The child layer is skipped since it was created
624 // from a different uid.
625 ScreenCapture::captureLayers(&mCapture, captureArgs);
626 mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
627 mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
628
629 // Clear fake calling uid so it's back to system.
630 uidFaker = nullptr;
631 // Screenshot from the test caller with the uid requested allows the layer
632 // with that uid to be screenshotted. The child layer is skipped since it was created
633 // from a different uid.
634 ScreenCapture::captureLayers(&mCapture, captureArgs);
635 mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN);
636 mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT);
637
638 // Screenshot from the fakeUid caller with no uid requested allows everything to be screenshot.
639 captureArgs.uid = -1;
640 ScreenCapture::captureLayers(&mCapture, captureArgs);
641 mCapture->expectColor(Rect(128, 128, 160, 160), Color::RED);
642 mCapture->expectBorder(Rect(128, 128, 160, 160), {63, 63, 195, 255});
643}
644
chaviw3efadb12020-07-27 10:07:15 -0700645// In the following tests we verify successful skipping of a parent layer,
646// so we use the same verification logic and only change how we mutate
647// the parent layer to verify that various properties are ignored.
648class ScreenCaptureChildOnlyTest : public ScreenCaptureTest {
649public:
650 void SetUp() override {
651 ScreenCaptureTest::SetUp();
652
653 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
654 mFGSurfaceControl.get());
655 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
656
657 SurfaceComposerClient::Transaction().show(mChild).apply(true);
658 }
659
660 void verify(std::function<void()> verifyStartingState) {
661 // Verify starting state before a screenshot is taken.
662 verifyStartingState();
663
664 // Verify child layer does not inherit any of the properties of its
665 // parent when its screenshot is captured.
chaviw70cb6a42020-07-30 13:57:36 -0700666 LayerCaptureArgs captureArgs;
667 captureArgs.layerHandle = mFGSurfaceControl->getHandle();
668 captureArgs.childrenOnly = true;
669 ScreenCapture::captureLayers(&mCapture, captureArgs);
chaviw3efadb12020-07-27 10:07:15 -0700670 mCapture->checkPixel(10, 10, 0, 0, 0);
671 mCapture->expectChildColor(0, 0);
672
673 // Verify all assumptions are still true after the screenshot is taken.
674 verifyStartingState();
675 }
676
677 std::unique_ptr<ScreenCapture> mCapture;
678 sp<SurfaceControl> mChild;
679};
680
681// Regression test b/76099859
682TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
683 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
684
685 // Even though the parent is hidden we should still capture the child.
686
687 // Before and after reparenting, verify child is properly hidden
688 // when rendering full-screen.
689 verify([&] { screenshot()->expectBGColor(64, 64); });
690}
691
692TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
693 SurfaceComposerClient::Transaction()
694 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
695 .apply(true);
696
697 // Even though the parent is cropped out we should still capture the child.
698
699 // Before and after reparenting, verify child is cropped by parent.
700 verify([&] { screenshot()->expectBGColor(65, 65); });
701}
702
703// Regression test b/124372894
704TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
705 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
706
707 // We should not inherit the parent scaling.
708
709 // Before and after reparenting, verify child is properly scaled.
710 verify([&] { screenshot()->expectChildColor(80, 80); });
711}
712
713} // namespace android