chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 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 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 21 | #include <private/android_filesystem_config.h> |
| 22 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 23 | #include "LayerTransactionTest.h" |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | class ScreenCaptureTest : public LayerTransactionTest { |
| 28 | protected: |
| 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 | |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 77 | TEST_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 | |
| 87 | sp<ISurfaceComposer> composer = ComposerService::getComposerService(); |
| 88 | ASSERT_EQ(PERMISSION_DENIED, composer->captureDisplay(mCaptureArgs, mCaptureResults)); |
| 89 | |
| 90 | UIDFaker f(AID_SYSTEM); |
| 91 | |
| 92 | // By default the system can capture screenshots with secure layers but they |
| 93 | // will be blacked out |
| 94 | ASSERT_EQ(NO_ERROR, composer->captureDisplay(mCaptureArgs, mCaptureResults)); |
| 95 | |
| 96 | { |
| 97 | SCOPED_TRACE("as system"); |
| 98 | auto shot = screenshot(); |
| 99 | shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK); |
| 100 | } |
| 101 | |
| 102 | // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able |
| 103 | // to receive them...we are expected to take care with the results. |
| 104 | DisplayCaptureArgs args; |
| 105 | args.displayToken = mDisplay; |
| 106 | args.captureSecureLayers = true; |
| 107 | ASSERT_EQ(NO_ERROR, composer->captureDisplay(args, mCaptureResults)); |
| 108 | ASSERT_TRUE(mCaptureResults.capturedSecureLayers); |
| 109 | ScreenCapture sc(mCaptureResults.buffer); |
| 110 | sc.expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 111 | } |
| 112 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 113 | TEST_F(ScreenCaptureTest, CaptureSingleLayer) { |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 114 | LayerCaptureArgs captureArgs; |
| 115 | captureArgs.layerHandle = mBGSurfaceControl->getHandle(); |
| 116 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 117 | mCapture->expectBGColor(0, 0); |
| 118 | // Doesn't capture FG layer which is at 64, 64 |
| 119 | mCapture->expectBGColor(64, 64); |
| 120 | } |
| 121 | |
| 122 | TEST_F(ScreenCaptureTest, CaptureLayerWithChild) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 123 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 124 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 125 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 126 | |
| 127 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 128 | |
| 129 | // Captures mFGSurfaceControl layer and its child. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 130 | LayerCaptureArgs captureArgs; |
| 131 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 132 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 133 | mCapture->expectFGColor(10, 10); |
| 134 | mCapture->expectChildColor(0, 0); |
| 135 | } |
| 136 | |
| 137 | TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) { |
| 138 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 139 | |
| 140 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 141 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 142 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 143 | |
| 144 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 145 | |
| 146 | // Captures mFGSurfaceControl's child |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 147 | LayerCaptureArgs captureArgs; |
| 148 | captureArgs.layerHandle = fgHandle; |
| 149 | captureArgs.childrenOnly = true; |
| 150 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 151 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 152 | mCapture->expectChildColor(0, 0); |
| 153 | } |
| 154 | |
| 155 | TEST_F(ScreenCaptureTest, CaptureLayerExclude) { |
| 156 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 157 | |
| 158 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 159 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 160 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 161 | sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10, |
| 162 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 163 | TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200); |
| 164 | |
| 165 | SurfaceComposerClient::Transaction() |
| 166 | .show(child) |
| 167 | .show(child2) |
| 168 | .setLayer(child, 1) |
| 169 | .setLayer(child2, 2) |
| 170 | .apply(true); |
| 171 | |
| 172 | // Child2 would be visible but its excluded, so we should see child1 color instead. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 173 | LayerCaptureArgs captureArgs; |
| 174 | captureArgs.layerHandle = fgHandle; |
| 175 | captureArgs.childrenOnly = true; |
| 176 | captureArgs.excludeHandles = {child2->getHandle()}; |
| 177 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 178 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 179 | mCapture->checkPixel(0, 0, 200, 200, 200); |
| 180 | } |
| 181 | |
| 182 | // Like the last test but verifies that children are also exclude. |
| 183 | TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) { |
| 184 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 185 | |
| 186 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 187 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 188 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 189 | sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10, |
| 190 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 191 | TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200); |
| 192 | sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10, |
| 193 | PIXEL_FORMAT_RGBA_8888, 0, child2.get()); |
| 194 | TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200); |
| 195 | |
| 196 | SurfaceComposerClient::Transaction() |
| 197 | .show(child) |
| 198 | .show(child2) |
| 199 | .show(child3) |
| 200 | .setLayer(child, 1) |
| 201 | .setLayer(child2, 2) |
| 202 | .apply(true); |
| 203 | |
| 204 | // Child2 would be visible but its excluded, so we should see child1 color instead. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 205 | LayerCaptureArgs captureArgs; |
| 206 | captureArgs.layerHandle = fgHandle; |
| 207 | captureArgs.childrenOnly = true; |
| 208 | captureArgs.excludeHandles = {child2->getHandle()}; |
| 209 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 210 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 211 | mCapture->checkPixel(0, 0, 200, 200, 200); |
| 212 | } |
| 213 | |
| 214 | TEST_F(ScreenCaptureTest, CaptureTransparent) { |
| 215 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 216 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 217 | |
| 218 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 219 | |
| 220 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 221 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 222 | // Captures child |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 223 | LayerCaptureArgs captureArgs; |
| 224 | captureArgs.layerHandle = child->getHandle(); |
| 225 | captureArgs.sourceCrop = {0, 0, 10, 20}; |
| 226 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 227 | mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255}); |
| 228 | // Area outside of child's bounds is transparent. |
| 229 | mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0}); |
| 230 | } |
| 231 | |
| 232 | TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 233 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 234 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 235 | ASSERT_NE(nullptr, child.get()) << "failed to create surface"; |
| 236 | sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0); |
| 237 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 238 | TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100); |
| 239 | |
| 240 | SurfaceComposerClient::Transaction() |
| 241 | .show(child) |
| 242 | // Set relative layer above fg layer so should be shown above when computing all layers. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 243 | .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1) |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 244 | .show(relative) |
| 245 | .apply(true); |
| 246 | |
| 247 | // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 248 | LayerCaptureArgs captureArgs; |
| 249 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 250 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 251 | mCapture->expectFGColor(10, 10); |
| 252 | mCapture->expectChildColor(0, 0); |
| 253 | } |
| 254 | |
| 255 | TEST_F(ScreenCaptureTest, CaptureRelativeInTree) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 256 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 257 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 258 | sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10, |
| 259 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 260 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 261 | TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100); |
| 262 | |
| 263 | SurfaceComposerClient::Transaction() |
| 264 | .show(child) |
| 265 | // Set relative layer below fg layer but relative to child layer so it should be shown |
| 266 | // above child layer. |
| 267 | .setLayer(relative, -1) |
| 268 | .setRelativeLayer(relative, child->getHandle(), 1) |
| 269 | .show(relative) |
| 270 | .apply(true); |
| 271 | |
| 272 | // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its |
| 273 | // relative value should be taken into account, placing it above child layer. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 274 | LayerCaptureArgs captureArgs; |
| 275 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 276 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 277 | mCapture->expectFGColor(10, 10); |
| 278 | // Relative layer is showing on top of child layer |
| 279 | mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255}); |
| 280 | } |
| 281 | |
| 282 | TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithSourceCrop) { |
| 283 | sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get()); |
| 284 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 285 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 286 | LayerCaptureArgs captureArgs; |
| 287 | captureArgs.layerHandle = child->getHandle(); |
| 288 | captureArgs.sourceCrop = {0, 0, 10, 10}; |
| 289 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 290 | |
| 291 | mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED); |
| 292 | } |
| 293 | |
| 294 | TEST_F(ScreenCaptureTest, CaptureBoundedLayerWithoutSourceCrop) { |
| 295 | sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get()); |
| 296 | Rect layerCrop(0, 0, 10, 10); |
| 297 | SurfaceComposerClient::Transaction().setCrop_legacy(child, layerCrop).show(child).apply(true); |
| 298 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 299 | LayerCaptureArgs captureArgs; |
| 300 | captureArgs.layerHandle = child->getHandle(); |
| 301 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 302 | |
| 303 | mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED); |
| 304 | } |
| 305 | |
| 306 | TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithoutSourceCropFails) { |
| 307 | sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get()); |
| 308 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 309 | |
| 310 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 311 | |
| 312 | LayerCaptureArgs args; |
| 313 | args.layerHandle = child->getHandle(); |
| 314 | |
| 315 | ScreenCaptureResults captureResults; |
| 316 | ASSERT_EQ(BAD_VALUE, sf->captureLayers(args, captureResults)); |
| 317 | } |
| 318 | |
| 319 | TEST_F(ScreenCaptureTest, CaptureBufferLayerWithoutBufferFails) { |
| 320 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 321 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 322 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 323 | |
| 324 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 325 | sp<GraphicBuffer> outBuffer; |
| 326 | |
| 327 | LayerCaptureArgs args; |
| 328 | args.layerHandle = child->getHandle(); |
| 329 | args.childrenOnly = false; |
| 330 | |
| 331 | ScreenCaptureResults captureResults; |
| 332 | ASSERT_EQ(BAD_VALUE, sf->captureLayers(args, captureResults)); |
| 333 | |
| 334 | TransactionUtils::fillSurfaceRGBA8(child, Color::RED); |
| 335 | SurfaceComposerClient::Transaction().apply(true); |
| 336 | ASSERT_EQ(NO_ERROR, sf->captureLayers(args, captureResults)); |
| 337 | ScreenCapture sc(captureResults.buffer); |
| 338 | sc.expectColor(Rect(0, 0, 9, 9), Color::RED); |
| 339 | } |
| 340 | |
| 341 | TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 342 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 343 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 344 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 345 | |
| 346 | sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5, |
| 347 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
| 348 | |
| 349 | TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 350 | SurfaceComposerClient::Transaction() |
| 351 | .show(child) |
| 352 | .setPosition(grandchild, 5, 5) |
| 353 | .show(grandchild) |
| 354 | .apply(true); |
| 355 | |
| 356 | // Captures mFGSurfaceControl, its child, and the grandchild. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 357 | LayerCaptureArgs captureArgs; |
| 358 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 359 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 360 | mCapture->expectFGColor(10, 10); |
| 361 | mCapture->expectChildColor(0, 0); |
| 362 | mCapture->checkPixel(5, 5, 50, 50, 50); |
| 363 | } |
| 364 | |
| 365 | TEST_F(ScreenCaptureTest, CaptureChildOnly) { |
| 366 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 367 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 368 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 369 | |
| 370 | SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true); |
| 371 | |
| 372 | // Captures only the child layer, and not the parent. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 373 | LayerCaptureArgs captureArgs; |
| 374 | captureArgs.layerHandle = child->getHandle(); |
| 375 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 376 | mCapture->expectChildColor(0, 0); |
| 377 | mCapture->expectChildColor(9, 9); |
| 378 | } |
| 379 | |
| 380 | TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) { |
| 381 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 382 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 383 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 384 | auto childHandle = child->getHandle(); |
| 385 | |
| 386 | sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5, |
| 387 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
| 388 | TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 389 | |
| 390 | SurfaceComposerClient::Transaction() |
| 391 | .show(child) |
| 392 | .setPosition(grandchild, 5, 5) |
| 393 | .show(grandchild) |
| 394 | .apply(true); |
| 395 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 396 | // Captures only the grandchild. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 397 | LayerCaptureArgs captureArgs; |
| 398 | captureArgs.layerHandle = grandchild->getHandle(); |
| 399 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 400 | mCapture->checkPixel(0, 0, 50, 50, 50); |
| 401 | mCapture->checkPixel(4, 4, 50, 50, 50); |
| 402 | } |
| 403 | |
| 404 | TEST_F(ScreenCaptureTest, CaptureCrop) { |
| 405 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 406 | sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30, |
| 407 | PIXEL_FORMAT_RGBA_8888, 0, redLayer.get()); |
| 408 | |
| 409 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 410 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30)); |
| 411 | |
| 412 | SurfaceComposerClient::Transaction() |
| 413 | .setLayer(redLayer, INT32_MAX - 1) |
| 414 | .show(redLayer) |
| 415 | .show(blueLayer) |
| 416 | .apply(true); |
| 417 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 418 | // Capturing full screen should have both red and blue are visible. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 419 | LayerCaptureArgs captureArgs; |
| 420 | captureArgs.layerHandle = redLayer->getHandle(); |
| 421 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 422 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); |
| 423 | // red area below the blue area |
| 424 | mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED); |
| 425 | // red area to the right of the blue area |
| 426 | mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED); |
| 427 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 428 | captureArgs.sourceCrop = {0, 0, 30, 30}; |
| 429 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 430 | // Capturing the cropped screen, cropping out the shown red area, should leave only the blue |
| 431 | // area visible. |
| 432 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); |
| 433 | mCapture->checkPixel(30, 30, 0, 0, 0); |
| 434 | } |
| 435 | |
| 436 | TEST_F(ScreenCaptureTest, CaptureSize) { |
| 437 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 438 | sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30, |
| 439 | PIXEL_FORMAT_RGBA_8888, 0, redLayer.get()); |
| 440 | |
| 441 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 442 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30)); |
| 443 | |
| 444 | SurfaceComposerClient::Transaction() |
| 445 | .setLayer(redLayer, INT32_MAX - 1) |
| 446 | .show(redLayer) |
| 447 | .show(blueLayer) |
| 448 | .apply(true); |
| 449 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 450 | // Capturing full screen should have both red and blue are visible. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 451 | LayerCaptureArgs captureArgs; |
| 452 | captureArgs.layerHandle = redLayer->getHandle(); |
| 453 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 454 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); |
| 455 | // red area below the blue area |
| 456 | mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED); |
| 457 | // red area to the right of the blue area |
| 458 | mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED); |
| 459 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 460 | captureArgs.frameScale = 0.5f; |
| 461 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 462 | // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area. |
| 463 | mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE); |
| 464 | // red area below the blue area |
| 465 | mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED); |
| 466 | // red area to the right of the blue area |
| 467 | mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED); |
| 468 | mCapture->checkPixel(30, 30, 0, 0, 0); |
| 469 | } |
| 470 | |
| 471 | TEST_F(ScreenCaptureTest, CaptureInvalidLayer) { |
| 472 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 473 | |
| 474 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 475 | |
| 476 | auto redLayerHandle = redLayer->getHandle(); |
| 477 | Transaction().reparent(redLayer, nullptr).apply(); |
| 478 | redLayer.clear(); |
| 479 | SurfaceComposerClient::Transaction().apply(true); |
| 480 | |
| 481 | LayerCaptureArgs args; |
| 482 | args.layerHandle = redLayerHandle; |
| 483 | |
| 484 | ScreenCaptureResults captureResults; |
| 485 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 486 | // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND |
| 487 | ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(args, captureResults)); |
| 488 | } |
| 489 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 490 | TEST_F(ScreenCaptureTest, CaputureSecureLayer) { |
| 491 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 492 | sp<SurfaceControl> secureLayer = |
| 493 | createLayer(String8("Secure surface"), 30, 30, |
| 494 | ISurfaceComposerClient::eSecure | |
| 495 | ISurfaceComposerClient::eFXSurfaceBufferQueue, |
| 496 | redLayer.get()); |
| 497 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 498 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(secureLayer, Color::BLUE, 30, 30)); |
| 499 | |
| 500 | auto redLayerHandle = redLayer->getHandle(); |
| 501 | Transaction() |
| 502 | .show(redLayer) |
| 503 | .show(secureLayer) |
| 504 | .setLayerStack(redLayer, 0) |
| 505 | .setLayer(redLayer, INT32_MAX) |
| 506 | .apply(); |
| 507 | |
| 508 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 509 | |
| 510 | LayerCaptureArgs args; |
| 511 | args.layerHandle = redLayerHandle; |
| 512 | args.childrenOnly = false; |
| 513 | ScreenCaptureResults captureResults; |
| 514 | |
| 515 | // Call from outside system with secure layers will result in permission denied |
| 516 | ASSERT_EQ(PERMISSION_DENIED, sf->captureLayers(args, captureResults)); |
| 517 | |
| 518 | UIDFaker f(AID_SYSTEM); |
| 519 | |
| 520 | // From system request, only red layer will be screenshot since the blue layer is secure. |
| 521 | // Black will be present where the secure layer is. |
| 522 | ScreenCapture::captureLayers(&mCapture, args); |
| 523 | mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLACK); |
| 524 | mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED); |
| 525 | |
| 526 | // Passing flag secure so the blue layer should be screenshot too. |
| 527 | args.captureSecureLayers = true; |
| 528 | ScreenCapture::captureLayers(&mCapture, args); |
| 529 | mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLUE); |
| 530 | mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED); |
| 531 | } |
| 532 | |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 533 | TEST_F(ScreenCaptureTest, CaptureDisplayWithUid) { |
| 534 | uid_t fakeUid = 12345; |
| 535 | |
| 536 | DisplayCaptureArgs captureArgs; |
| 537 | captureArgs.displayToken = mDisplay; |
| 538 | |
| 539 | sp<SurfaceControl> layer; |
| 540 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32, |
| 541 | ISurfaceComposerClient::eFXSurfaceBufferQueue, |
| 542 | mBGSurfaceControl.get())); |
| 543 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32)); |
| 544 | |
| 545 | Transaction().show(layer).setLayer(layer, INT32_MAX).apply(); |
| 546 | |
| 547 | // Make sure red layer with the background layer is screenshot. |
| 548 | ScreenCapture::captureDisplay(&mCapture, captureArgs); |
| 549 | mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 550 | mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255}); |
| 551 | |
| 552 | // From non system uid, can't request screenshot without a specified uid. |
| 553 | UIDFaker f(fakeUid); |
| 554 | sp<ISurfaceComposer> composer = ComposerService::getComposerService(); |
| 555 | ASSERT_EQ(PERMISSION_DENIED, composer->captureDisplay(captureArgs, mCaptureResults)); |
| 556 | |
| 557 | // Make screenshot request with current uid set. No layers were created with the current |
| 558 | // uid so screenshot will be black. |
| 559 | captureArgs.uid = fakeUid; |
| 560 | ScreenCapture::captureDisplay(&mCapture, captureArgs); |
| 561 | mCapture->expectColor(Rect(0, 0, 32, 32), Color::BLACK); |
| 562 | mCapture->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); |
| 563 | |
| 564 | sp<SurfaceControl> layerWithFakeUid; |
| 565 | // Create a new layer with the current uid |
| 566 | ASSERT_NO_FATAL_FAILURE(layerWithFakeUid = |
| 567 | createLayer("new test layer", 32, 32, |
| 568 | ISurfaceComposerClient::eFXSurfaceBufferQueue, |
| 569 | mBGSurfaceControl.get())); |
| 570 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32)); |
| 571 | Transaction() |
| 572 | .show(layerWithFakeUid) |
| 573 | .setLayer(layerWithFakeUid, INT32_MAX) |
| 574 | .setPosition(layerWithFakeUid, 128, 128) |
| 575 | .apply(); |
| 576 | |
| 577 | // Screenshot from the fakeUid caller with the uid requested allows the layer |
| 578 | // with that uid to be screenshotted. Everything else is black |
| 579 | ScreenCapture::captureDisplay(&mCapture, captureArgs); |
| 580 | mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN); |
| 581 | mCapture->expectBorder(Rect(128, 128, 160, 160), Color::BLACK); |
| 582 | } |
| 583 | |
| 584 | TEST_F(ScreenCaptureTest, CaptureLayerWithUid) { |
| 585 | uid_t fakeUid = 12345; |
| 586 | |
| 587 | sp<SurfaceControl> layer; |
| 588 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test layer", 32, 32, |
| 589 | ISurfaceComposerClient::eFXSurfaceBufferQueue, |
| 590 | mBGSurfaceControl.get())); |
| 591 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32)); |
| 592 | |
| 593 | Transaction().show(layer).setLayer(layer, INT32_MAX).apply(); |
| 594 | |
| 595 | LayerCaptureArgs captureArgs; |
| 596 | captureArgs.layerHandle = mBGSurfaceControl->getHandle(); |
| 597 | captureArgs.childrenOnly = false; |
| 598 | |
| 599 | // Make sure red layer with the background layer is screenshot. |
| 600 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
| 601 | mCapture->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 602 | mCapture->expectBorder(Rect(0, 0, 32, 32), {63, 63, 195, 255}); |
| 603 | |
| 604 | // From non system uid, can't request screenshot without a specified uid. |
| 605 | std::unique_ptr<UIDFaker> uidFaker = std::make_unique<UIDFaker>(fakeUid); |
| 606 | |
| 607 | sp<ISurfaceComposer> composer = ComposerService::getComposerService(); |
| 608 | ASSERT_EQ(PERMISSION_DENIED, composer->captureLayers(captureArgs, mCaptureResults)); |
| 609 | |
| 610 | // Make screenshot request with current uid set. No layers were created with the current |
| 611 | // uid so screenshot will be black. |
| 612 | captureArgs.uid = fakeUid; |
| 613 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
| 614 | mCapture->expectColor(Rect(0, 0, 32, 32), Color::TRANSPARENT); |
| 615 | mCapture->expectBorder(Rect(0, 0, 32, 32), Color::TRANSPARENT); |
| 616 | |
| 617 | sp<SurfaceControl> layerWithFakeUid; |
| 618 | // Create a new layer with the current uid |
| 619 | ASSERT_NO_FATAL_FAILURE(layerWithFakeUid = |
| 620 | createLayer("new test layer", 32, 32, |
| 621 | ISurfaceComposerClient::eFXSurfaceBufferQueue, |
| 622 | mBGSurfaceControl.get())); |
| 623 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerWithFakeUid, Color::GREEN, 32, 32)); |
| 624 | Transaction() |
| 625 | .show(layerWithFakeUid) |
| 626 | .setLayer(layerWithFakeUid, INT32_MAX) |
| 627 | .setPosition(layerWithFakeUid, 128, 128) |
| 628 | // reparent a layer that was created with a different uid to the new layer. |
| 629 | .reparent(layer, layerWithFakeUid->getHandle()) |
| 630 | .apply(); |
| 631 | |
| 632 | // Screenshot from the fakeUid caller with the uid requested allows the layer |
| 633 | // with that uid to be screenshotted. The child layer is skipped since it was created |
| 634 | // from a different uid. |
| 635 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
| 636 | mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN); |
| 637 | mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT); |
| 638 | |
| 639 | // Clear fake calling uid so it's back to system. |
| 640 | uidFaker = nullptr; |
| 641 | // Screenshot from the test caller with the uid requested allows the layer |
| 642 | // with that uid to be screenshotted. The child layer is skipped since it was created |
| 643 | // from a different uid. |
| 644 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
| 645 | mCapture->expectColor(Rect(128, 128, 160, 160), Color::GREEN); |
| 646 | mCapture->expectBorder(Rect(128, 128, 160, 160), Color::TRANSPARENT); |
| 647 | |
| 648 | // Screenshot from the fakeUid caller with no uid requested allows everything to be screenshot. |
| 649 | captureArgs.uid = -1; |
| 650 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
| 651 | mCapture->expectColor(Rect(128, 128, 160, 160), Color::RED); |
| 652 | mCapture->expectBorder(Rect(128, 128, 160, 160), {63, 63, 195, 255}); |
| 653 | } |
| 654 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 655 | // In the following tests we verify successful skipping of a parent layer, |
| 656 | // so we use the same verification logic and only change how we mutate |
| 657 | // the parent layer to verify that various properties are ignored. |
| 658 | class ScreenCaptureChildOnlyTest : public ScreenCaptureTest { |
| 659 | public: |
| 660 | void SetUp() override { |
| 661 | ScreenCaptureTest::SetUp(); |
| 662 | |
| 663 | mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, |
| 664 | mFGSurfaceControl.get()); |
| 665 | TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200); |
| 666 | |
| 667 | SurfaceComposerClient::Transaction().show(mChild).apply(true); |
| 668 | } |
| 669 | |
| 670 | void verify(std::function<void()> verifyStartingState) { |
| 671 | // Verify starting state before a screenshot is taken. |
| 672 | verifyStartingState(); |
| 673 | |
| 674 | // Verify child layer does not inherit any of the properties of its |
| 675 | // parent when its screenshot is captured. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 676 | LayerCaptureArgs captureArgs; |
| 677 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 678 | captureArgs.childrenOnly = true; |
| 679 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 680 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 681 | mCapture->expectChildColor(0, 0); |
| 682 | |
| 683 | // Verify all assumptions are still true after the screenshot is taken. |
| 684 | verifyStartingState(); |
| 685 | } |
| 686 | |
| 687 | std::unique_ptr<ScreenCapture> mCapture; |
| 688 | sp<SurfaceControl> mChild; |
| 689 | }; |
| 690 | |
| 691 | // Regression test b/76099859 |
| 692 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) { |
| 693 | SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true); |
| 694 | |
| 695 | // Even though the parent is hidden we should still capture the child. |
| 696 | |
| 697 | // Before and after reparenting, verify child is properly hidden |
| 698 | // when rendering full-screen. |
| 699 | verify([&] { screenshot()->expectBGColor(64, 64); }); |
| 700 | } |
| 701 | |
| 702 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) { |
| 703 | SurfaceComposerClient::Transaction() |
| 704 | .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1)) |
| 705 | .apply(true); |
| 706 | |
| 707 | // Even though the parent is cropped out we should still capture the child. |
| 708 | |
| 709 | // Before and after reparenting, verify child is cropped by parent. |
| 710 | verify([&] { screenshot()->expectBGColor(65, 65); }); |
| 711 | } |
| 712 | |
| 713 | // Regression test b/124372894 |
| 714 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) { |
| 715 | SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true); |
| 716 | |
| 717 | // We should not inherit the parent scaling. |
| 718 | |
| 719 | // Before and after reparenting, verify child is properly scaled. |
| 720 | verify([&] { screenshot()->expectChildColor(80, 80); }); |
| 721 | } |
| 722 | |
| 723 | } // namespace android |