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