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 | |
| 77 | TEST_F(ScreenCaptureTest, CaptureSingleLayer) { |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 78 | LayerCaptureArgs captureArgs; |
| 79 | captureArgs.layerHandle = mBGSurfaceControl->getHandle(); |
| 80 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 81 | mCapture->expectBGColor(0, 0); |
| 82 | // Doesn't capture FG layer which is at 64, 64 |
| 83 | mCapture->expectBGColor(64, 64); |
| 84 | } |
| 85 | |
| 86 | TEST_F(ScreenCaptureTest, CaptureLayerWithChild) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 87 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 88 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 89 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 90 | |
| 91 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 92 | |
| 93 | // Captures mFGSurfaceControl layer and its child. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 94 | LayerCaptureArgs captureArgs; |
| 95 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 96 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 97 | mCapture->expectFGColor(10, 10); |
| 98 | mCapture->expectChildColor(0, 0); |
| 99 | } |
| 100 | |
| 101 | TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) { |
| 102 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 103 | |
| 104 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 105 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 106 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 107 | |
| 108 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 109 | |
| 110 | // Captures mFGSurfaceControl's child |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 111 | LayerCaptureArgs captureArgs; |
| 112 | captureArgs.layerHandle = fgHandle; |
| 113 | captureArgs.childrenOnly = true; |
| 114 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 115 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 116 | mCapture->expectChildColor(0, 0); |
| 117 | } |
| 118 | |
| 119 | TEST_F(ScreenCaptureTest, CaptureLayerExclude) { |
| 120 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 121 | |
| 122 | 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 | sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10, |
| 126 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 127 | TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200); |
| 128 | |
| 129 | SurfaceComposerClient::Transaction() |
| 130 | .show(child) |
| 131 | .show(child2) |
| 132 | .setLayer(child, 1) |
| 133 | .setLayer(child2, 2) |
| 134 | .apply(true); |
| 135 | |
| 136 | // 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] | 137 | LayerCaptureArgs captureArgs; |
| 138 | captureArgs.layerHandle = fgHandle; |
| 139 | captureArgs.childrenOnly = true; |
| 140 | captureArgs.excludeHandles = {child2->getHandle()}; |
| 141 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 142 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 143 | mCapture->checkPixel(0, 0, 200, 200, 200); |
| 144 | } |
| 145 | |
| 146 | // Like the last test but verifies that children are also exclude. |
| 147 | TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) { |
| 148 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 149 | |
| 150 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 151 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 152 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 153 | sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10, |
| 154 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 155 | TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200); |
| 156 | sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10, |
| 157 | PIXEL_FORMAT_RGBA_8888, 0, child2.get()); |
| 158 | TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200); |
| 159 | |
| 160 | SurfaceComposerClient::Transaction() |
| 161 | .show(child) |
| 162 | .show(child2) |
| 163 | .show(child3) |
| 164 | .setLayer(child, 1) |
| 165 | .setLayer(child2, 2) |
| 166 | .apply(true); |
| 167 | |
| 168 | // 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] | 169 | LayerCaptureArgs captureArgs; |
| 170 | captureArgs.layerHandle = fgHandle; |
| 171 | captureArgs.childrenOnly = true; |
| 172 | captureArgs.excludeHandles = {child2->getHandle()}; |
| 173 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 174 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 175 | mCapture->checkPixel(0, 0, 200, 200, 200); |
| 176 | } |
| 177 | |
| 178 | TEST_F(ScreenCaptureTest, CaptureTransparent) { |
| 179 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 180 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 181 | |
| 182 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 183 | |
| 184 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 185 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 186 | // Captures child |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 187 | LayerCaptureArgs captureArgs; |
| 188 | captureArgs.layerHandle = child->getHandle(); |
| 189 | captureArgs.sourceCrop = {0, 0, 10, 20}; |
| 190 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 191 | mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255}); |
| 192 | // Area outside of child's bounds is transparent. |
| 193 | mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0}); |
| 194 | } |
| 195 | |
| 196 | TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 197 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 198 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 199 | ASSERT_NE(nullptr, child.get()) << "failed to create surface"; |
| 200 | sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0); |
| 201 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 202 | TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100); |
| 203 | |
| 204 | SurfaceComposerClient::Transaction() |
| 205 | .show(child) |
| 206 | // 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] | 207 | .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1) |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 208 | .show(relative) |
| 209 | .apply(true); |
| 210 | |
| 211 | // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 212 | LayerCaptureArgs captureArgs; |
| 213 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 214 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 215 | mCapture->expectFGColor(10, 10); |
| 216 | mCapture->expectChildColor(0, 0); |
| 217 | } |
| 218 | |
| 219 | TEST_F(ScreenCaptureTest, CaptureRelativeInTree) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 220 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 221 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 222 | sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10, |
| 223 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 224 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 225 | TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100); |
| 226 | |
| 227 | SurfaceComposerClient::Transaction() |
| 228 | .show(child) |
| 229 | // Set relative layer below fg layer but relative to child layer so it should be shown |
| 230 | // above child layer. |
| 231 | .setLayer(relative, -1) |
| 232 | .setRelativeLayer(relative, child->getHandle(), 1) |
| 233 | .show(relative) |
| 234 | .apply(true); |
| 235 | |
| 236 | // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its |
| 237 | // relative value should be taken into account, placing it above child layer. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 238 | LayerCaptureArgs captureArgs; |
| 239 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 240 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 241 | mCapture->expectFGColor(10, 10); |
| 242 | // Relative layer is showing on top of child layer |
| 243 | mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255}); |
| 244 | } |
| 245 | |
| 246 | TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithSourceCrop) { |
| 247 | sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get()); |
| 248 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 249 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 250 | LayerCaptureArgs captureArgs; |
| 251 | captureArgs.layerHandle = child->getHandle(); |
| 252 | captureArgs.sourceCrop = {0, 0, 10, 10}; |
| 253 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 254 | |
| 255 | mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED); |
| 256 | } |
| 257 | |
| 258 | TEST_F(ScreenCaptureTest, CaptureBoundedLayerWithoutSourceCrop) { |
| 259 | sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get()); |
| 260 | Rect layerCrop(0, 0, 10, 10); |
| 261 | SurfaceComposerClient::Transaction().setCrop_legacy(child, layerCrop).show(child).apply(true); |
| 262 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 263 | LayerCaptureArgs captureArgs; |
| 264 | captureArgs.layerHandle = child->getHandle(); |
| 265 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 266 | |
| 267 | mCapture->expectColor(Rect(0, 0, 9, 9), Color::RED); |
| 268 | } |
| 269 | |
| 270 | TEST_F(ScreenCaptureTest, CaptureBoundlessLayerWithoutSourceCropFails) { |
| 271 | sp<SurfaceControl> child = createColorLayer("Child layer", Color::RED, mFGSurfaceControl.get()); |
| 272 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 273 | |
| 274 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 275 | |
| 276 | LayerCaptureArgs args; |
| 277 | args.layerHandle = child->getHandle(); |
| 278 | |
| 279 | ScreenCaptureResults captureResults; |
| 280 | ASSERT_EQ(BAD_VALUE, sf->captureLayers(args, captureResults)); |
| 281 | } |
| 282 | |
| 283 | TEST_F(ScreenCaptureTest, CaptureBufferLayerWithoutBufferFails) { |
| 284 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 285 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 286 | SurfaceComposerClient::Transaction().show(child).apply(true); |
| 287 | |
| 288 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 289 | sp<GraphicBuffer> outBuffer; |
| 290 | |
| 291 | LayerCaptureArgs args; |
| 292 | args.layerHandle = child->getHandle(); |
| 293 | args.childrenOnly = false; |
| 294 | |
| 295 | ScreenCaptureResults captureResults; |
| 296 | ASSERT_EQ(BAD_VALUE, sf->captureLayers(args, captureResults)); |
| 297 | |
| 298 | TransactionUtils::fillSurfaceRGBA8(child, Color::RED); |
| 299 | SurfaceComposerClient::Transaction().apply(true); |
| 300 | ASSERT_EQ(NO_ERROR, sf->captureLayers(args, captureResults)); |
| 301 | ScreenCapture sc(captureResults.buffer); |
| 302 | sc.expectColor(Rect(0, 0, 9, 9), Color::RED); |
| 303 | } |
| 304 | |
| 305 | TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 306 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 307 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 308 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 309 | |
| 310 | sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5, |
| 311 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
| 312 | |
| 313 | TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 314 | SurfaceComposerClient::Transaction() |
| 315 | .show(child) |
| 316 | .setPosition(grandchild, 5, 5) |
| 317 | .show(grandchild) |
| 318 | .apply(true); |
| 319 | |
| 320 | // Captures mFGSurfaceControl, its child, and the grandchild. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 321 | LayerCaptureArgs captureArgs; |
| 322 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 323 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 324 | mCapture->expectFGColor(10, 10); |
| 325 | mCapture->expectChildColor(0, 0); |
| 326 | mCapture->checkPixel(5, 5, 50, 50, 50); |
| 327 | } |
| 328 | |
| 329 | TEST_F(ScreenCaptureTest, CaptureChildOnly) { |
| 330 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 331 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 332 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 333 | |
| 334 | SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true); |
| 335 | |
| 336 | // Captures only the child layer, and not the parent. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 337 | LayerCaptureArgs captureArgs; |
| 338 | captureArgs.layerHandle = child->getHandle(); |
| 339 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 340 | mCapture->expectChildColor(0, 0); |
| 341 | mCapture->expectChildColor(9, 9); |
| 342 | } |
| 343 | |
| 344 | TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) { |
| 345 | sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10, |
| 346 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 347 | TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200); |
| 348 | auto childHandle = child->getHandle(); |
| 349 | |
| 350 | sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5, |
| 351 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
| 352 | TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 353 | |
| 354 | SurfaceComposerClient::Transaction() |
| 355 | .show(child) |
| 356 | .setPosition(grandchild, 5, 5) |
| 357 | .show(grandchild) |
| 358 | .apply(true); |
| 359 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 360 | // Captures only the grandchild. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 361 | LayerCaptureArgs captureArgs; |
| 362 | captureArgs.layerHandle = grandchild->getHandle(); |
| 363 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 364 | mCapture->checkPixel(0, 0, 50, 50, 50); |
| 365 | mCapture->checkPixel(4, 4, 50, 50, 50); |
| 366 | } |
| 367 | |
| 368 | TEST_F(ScreenCaptureTest, CaptureCrop) { |
| 369 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 370 | sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30, |
| 371 | PIXEL_FORMAT_RGBA_8888, 0, redLayer.get()); |
| 372 | |
| 373 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 374 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30)); |
| 375 | |
| 376 | SurfaceComposerClient::Transaction() |
| 377 | .setLayer(redLayer, INT32_MAX - 1) |
| 378 | .show(redLayer) |
| 379 | .show(blueLayer) |
| 380 | .apply(true); |
| 381 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 382 | // Capturing full screen should have both red and blue are visible. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 383 | LayerCaptureArgs captureArgs; |
| 384 | captureArgs.layerHandle = redLayer->getHandle(); |
| 385 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 386 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); |
| 387 | // red area below the blue area |
| 388 | mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED); |
| 389 | // red area to the right of the blue area |
| 390 | mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED); |
| 391 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 392 | captureArgs.sourceCrop = {0, 0, 30, 30}; |
| 393 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 394 | // Capturing the cropped screen, cropping out the shown red area, should leave only the blue |
| 395 | // area visible. |
| 396 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); |
| 397 | mCapture->checkPixel(30, 30, 0, 0, 0); |
| 398 | } |
| 399 | |
| 400 | TEST_F(ScreenCaptureTest, CaptureSize) { |
| 401 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 402 | sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30, |
| 403 | PIXEL_FORMAT_RGBA_8888, 0, redLayer.get()); |
| 404 | |
| 405 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 406 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30)); |
| 407 | |
| 408 | SurfaceComposerClient::Transaction() |
| 409 | .setLayer(redLayer, INT32_MAX - 1) |
| 410 | .show(redLayer) |
| 411 | .show(blueLayer) |
| 412 | .apply(true); |
| 413 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 414 | // Capturing full screen should have both red and blue are visible. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 415 | LayerCaptureArgs captureArgs; |
| 416 | captureArgs.layerHandle = redLayer->getHandle(); |
| 417 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 418 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); |
| 419 | // red area below the blue area |
| 420 | mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED); |
| 421 | // red area to the right of the blue area |
| 422 | mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED); |
| 423 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 424 | captureArgs.frameScale = 0.5f; |
| 425 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 426 | // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area. |
| 427 | mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE); |
| 428 | // red area below the blue area |
| 429 | mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED); |
| 430 | // red area to the right of the blue area |
| 431 | mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED); |
| 432 | mCapture->checkPixel(30, 30, 0, 0, 0); |
| 433 | } |
| 434 | |
| 435 | TEST_F(ScreenCaptureTest, CaptureInvalidLayer) { |
| 436 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 437 | |
| 438 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 439 | |
| 440 | auto redLayerHandle = redLayer->getHandle(); |
| 441 | Transaction().reparent(redLayer, nullptr).apply(); |
| 442 | redLayer.clear(); |
| 443 | SurfaceComposerClient::Transaction().apply(true); |
| 444 | |
| 445 | LayerCaptureArgs args; |
| 446 | args.layerHandle = redLayerHandle; |
| 447 | |
| 448 | ScreenCaptureResults captureResults; |
| 449 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 450 | // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND |
| 451 | ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(args, captureResults)); |
| 452 | } |
| 453 | |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 454 | TEST_F(ScreenCaptureTest, CaputureSecureLayer) { |
| 455 | sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0); |
| 456 | sp<SurfaceControl> secureLayer = |
| 457 | createLayer(String8("Secure surface"), 30, 30, |
| 458 | ISurfaceComposerClient::eSecure | |
| 459 | ISurfaceComposerClient::eFXSurfaceBufferQueue, |
| 460 | redLayer.get()); |
| 461 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60)); |
| 462 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(secureLayer, Color::BLUE, 30, 30)); |
| 463 | |
| 464 | auto redLayerHandle = redLayer->getHandle(); |
| 465 | Transaction() |
| 466 | .show(redLayer) |
| 467 | .show(secureLayer) |
| 468 | .setLayerStack(redLayer, 0) |
| 469 | .setLayer(redLayer, INT32_MAX) |
| 470 | .apply(); |
| 471 | |
| 472 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 473 | |
| 474 | LayerCaptureArgs args; |
| 475 | args.layerHandle = redLayerHandle; |
| 476 | args.childrenOnly = false; |
| 477 | ScreenCaptureResults captureResults; |
| 478 | |
| 479 | // Call from outside system with secure layers will result in permission denied |
| 480 | ASSERT_EQ(PERMISSION_DENIED, sf->captureLayers(args, captureResults)); |
| 481 | |
| 482 | UIDFaker f(AID_SYSTEM); |
| 483 | |
| 484 | // From system request, only red layer will be screenshot since the blue layer is secure. |
| 485 | // Black will be present where the secure layer is. |
| 486 | ScreenCapture::captureLayers(&mCapture, args); |
| 487 | mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLACK); |
| 488 | mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED); |
| 489 | |
| 490 | // Passing flag secure so the blue layer should be screenshot too. |
| 491 | args.captureSecureLayers = true; |
| 492 | ScreenCapture::captureLayers(&mCapture, args); |
| 493 | mCapture->expectColor(Rect(0, 0, 30, 30), Color::BLUE); |
| 494 | mCapture->expectColor(Rect(30, 30, 60, 60), Color::RED); |
| 495 | } |
| 496 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 497 | // In the following tests we verify successful skipping of a parent layer, |
| 498 | // so we use the same verification logic and only change how we mutate |
| 499 | // the parent layer to verify that various properties are ignored. |
| 500 | class ScreenCaptureChildOnlyTest : public ScreenCaptureTest { |
| 501 | public: |
| 502 | void SetUp() override { |
| 503 | ScreenCaptureTest::SetUp(); |
| 504 | |
| 505 | mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, |
| 506 | mFGSurfaceControl.get()); |
| 507 | TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200); |
| 508 | |
| 509 | SurfaceComposerClient::Transaction().show(mChild).apply(true); |
| 510 | } |
| 511 | |
| 512 | void verify(std::function<void()> verifyStartingState) { |
| 513 | // Verify starting state before a screenshot is taken. |
| 514 | verifyStartingState(); |
| 515 | |
| 516 | // Verify child layer does not inherit any of the properties of its |
| 517 | // parent when its screenshot is captured. |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 518 | LayerCaptureArgs captureArgs; |
| 519 | captureArgs.layerHandle = mFGSurfaceControl->getHandle(); |
| 520 | captureArgs.childrenOnly = true; |
| 521 | ScreenCapture::captureLayers(&mCapture, captureArgs); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 522 | mCapture->checkPixel(10, 10, 0, 0, 0); |
| 523 | mCapture->expectChildColor(0, 0); |
| 524 | |
| 525 | // Verify all assumptions are still true after the screenshot is taken. |
| 526 | verifyStartingState(); |
| 527 | } |
| 528 | |
| 529 | std::unique_ptr<ScreenCapture> mCapture; |
| 530 | sp<SurfaceControl> mChild; |
| 531 | }; |
| 532 | |
| 533 | // Regression test b/76099859 |
| 534 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) { |
| 535 | SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true); |
| 536 | |
| 537 | // Even though the parent is hidden we should still capture the child. |
| 538 | |
| 539 | // Before and after reparenting, verify child is properly hidden |
| 540 | // when rendering full-screen. |
| 541 | verify([&] { screenshot()->expectBGColor(64, 64); }); |
| 542 | } |
| 543 | |
| 544 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) { |
| 545 | SurfaceComposerClient::Transaction() |
| 546 | .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1)) |
| 547 | .apply(true); |
| 548 | |
| 549 | // Even though the parent is cropped out we should still capture the child. |
| 550 | |
| 551 | // Before and after reparenting, verify child is cropped by parent. |
| 552 | verify([&] { screenshot()->expectBGColor(65, 65); }); |
| 553 | } |
| 554 | |
| 555 | // Regression test b/124372894 |
| 556 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) { |
| 557 | SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true); |
| 558 | |
| 559 | // We should not inherit the parent scaling. |
| 560 | |
| 561 | // Before and after reparenting, verify child is properly scaled. |
| 562 | verify([&] { screenshot()->expectChildColor(80, 80); }); |
| 563 | } |
| 564 | |
| 565 | } // namespace android |