Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #pragma once |
| 17 | |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 18 | #include <gui/AidlStatusUtil.h> |
chaviw | e7b9f27 | 2020-08-18 16:08:59 -0700 | [diff] [blame] | 19 | #include <gui/SyncScreenCaptureListener.h> |
Huihong Luo | 9e84f33 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 20 | #include <private/gui/ComposerServiceAIDL.h> |
Patrick Williams | fdb57bb | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 21 | #include <ui/FenceResult.h> |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 22 | #include <ui/Rect.h> |
| 23 | #include <utils/String8.h> |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 24 | #include <functional> |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 25 | #include "TransactionUtils.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 29 | using gui::aidl_utils::statusTFromBinderStatus; |
| 30 | |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check |
| 34 | // individual pixel values for testing purposes. |
| 35 | class ScreenCapture : public RefBase { |
| 36 | public: |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 37 | static status_t captureDisplay(DisplayCaptureArgs& captureArgs, |
| 38 | ScreenCaptureResults& captureResults) { |
Huihong Luo | 9e84f33 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 39 | const auto sf = ComposerServiceAIDL::getComposerService(); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 40 | SurfaceComposerClient::Transaction().apply(true); |
| 41 | |
Peiyong Lin | cd26147 | 2020-10-06 18:05:25 -0700 | [diff] [blame] | 42 | captureArgs.dataspace = ui::Dataspace::V0_SRGB; |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 43 | const sp<SyncScreenCaptureListener> captureListener = sp<SyncScreenCaptureListener>::make(); |
Huihong Luo | 9e84f33 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 44 | binder::Status status = sf->captureDisplay(captureArgs, captureListener); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 45 | status_t err = statusTFromBinderStatus(status); |
| 46 | if (err != NO_ERROR) { |
| 47 | return err; |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 48 | } |
| 49 | captureResults = captureListener->waitForResults(); |
Patrick Williams | fdb57bb | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 50 | return fenceStatus(captureResults.fenceResult); |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void captureScreen(std::unique_ptr<ScreenCapture>* sc) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 54 | const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 55 | // TODO(b/248317436): extend to cover all displays for multi-display devices |
| 56 | const auto display = |
| 57 | ids.empty() ? nullptr : SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); |
| 58 | captureScreen(sc, display); |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) { |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 62 | DisplayCaptureArgs args; |
| 63 | args.displayToken = displayToken; |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 64 | captureDisplay(sc, args); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 65 | } |
| 66 | |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 67 | static void captureDisplay(std::unique_ptr<ScreenCapture>* sc, |
| 68 | DisplayCaptureArgs& captureArgs) { |
| 69 | ScreenCaptureResults captureResults; |
| 70 | ASSERT_EQ(NO_ERROR, captureDisplay(captureArgs, captureResults)); |
Alec Mouri | 14d5b86 | 2022-04-27 21:20:04 +0000 | [diff] [blame] | 71 | *sc = std::make_unique<ScreenCapture>(captureResults.buffer, |
| 72 | captureResults.capturedHdrLayers); |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static status_t captureLayers(LayerCaptureArgs& captureArgs, |
| 76 | ScreenCaptureResults& captureResults) { |
Huihong Luo | 9e84f33 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 77 | const auto sf = ComposerServiceAIDL::getComposerService(); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 78 | SurfaceComposerClient::Transaction().apply(true); |
| 79 | |
Peiyong Lin | cd26147 | 2020-10-06 18:05:25 -0700 | [diff] [blame] | 80 | captureArgs.dataspace = ui::Dataspace::V0_SRGB; |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 81 | const sp<SyncScreenCaptureListener> captureListener = sp<SyncScreenCaptureListener>::make(); |
Huihong Luo | 9e84f33 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 82 | binder::Status status = sf->captureLayers(captureArgs, captureListener); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 83 | status_t err = statusTFromBinderStatus(status); |
| 84 | if (err != NO_ERROR) { |
| 85 | return err; |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 86 | } |
| 87 | captureResults = captureListener->waitForResults(); |
Patrick Williams | fdb57bb | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 88 | return fenceStatus(captureResults.fenceResult); |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void captureLayers(std::unique_ptr<ScreenCapture>* sc, LayerCaptureArgs& captureArgs) { |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 92 | ScreenCaptureResults captureResults; |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 93 | ASSERT_EQ(NO_ERROR, captureLayers(captureArgs, captureResults)); |
Alec Mouri | 14d5b86 | 2022-04-27 21:20:04 +0000 | [diff] [blame] | 94 | *sc = std::make_unique<ScreenCapture>(captureResults.buffer, |
| 95 | captureResults.capturedHdrLayers); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Alec Mouri | 14d5b86 | 2022-04-27 21:20:04 +0000 | [diff] [blame] | 98 | bool capturedHdrLayers() const { return mContainsHdr; } |
| 99 | |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 100 | void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) { |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 101 | ASSERT_NE(nullptr, mOutBuffer); |
arthurhung | f1b7c7b | 2021-03-03 17:42:23 +0800 | [diff] [blame] | 102 | ASSERT_NE(nullptr, mPixels); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 103 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); |
Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 104 | TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) { |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 108 | ASSERT_NE(nullptr, mOutBuffer); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 109 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); |
| 110 | const bool leftBorder = rect.left > 0; |
| 111 | const bool topBorder = rect.top > 0; |
| 112 | const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth()); |
| 113 | const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight()); |
| 114 | |
| 115 | if (topBorder) { |
| 116 | Rect top(rect.left, rect.top - 1, rect.right, rect.top); |
| 117 | if (leftBorder) { |
| 118 | top.left -= 1; |
| 119 | } |
| 120 | if (rightBorder) { |
| 121 | top.right += 1; |
| 122 | } |
| 123 | expectColor(top, color, tolerance); |
| 124 | } |
| 125 | if (leftBorder) { |
| 126 | Rect left(rect.left - 1, rect.top, rect.left, rect.bottom); |
| 127 | expectColor(left, color, tolerance); |
| 128 | } |
| 129 | if (rightBorder) { |
| 130 | Rect right(rect.right, rect.top, rect.right + 1, rect.bottom); |
| 131 | expectColor(right, color, tolerance); |
| 132 | } |
| 133 | if (bottomBorder) { |
| 134 | Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1); |
| 135 | if (leftBorder) { |
| 136 | bottom.left -= 1; |
| 137 | } |
| 138 | if (rightBorder) { |
| 139 | bottom.right += 1; |
| 140 | } |
| 141 | expectColor(bottom, color, tolerance); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight, |
| 146 | const Color& bottomLeft, const Color& bottomRight, bool filtered = false, |
| 147 | uint8_t tolerance = 0) { |
| 148 | ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0); |
| 149 | |
| 150 | const int32_t centerX = rect.left + (rect.right - rect.left) / 2; |
| 151 | const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2; |
| 152 | // avoid checking borders due to unspecified filtering behavior |
| 153 | const int32_t offsetX = filtered ? 2 : 0; |
| 154 | const int32_t offsetY = filtered ? 2 : 0; |
| 155 | expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft, |
| 156 | tolerance); |
| 157 | expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight, |
| 158 | tolerance); |
| 159 | expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft, |
| 160 | tolerance); |
| 161 | expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom), |
| 162 | bottomRight, tolerance); |
| 163 | } |
| 164 | |
| 165 | void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 166 | ASSERT_NE(nullptr, mOutBuffer); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 167 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); |
| 168 | const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x)); |
| 169 | if (r != pixel[0] || g != pixel[1] || b != pixel[2]) { |
| 170 | String8 err(String8::format("pixel @ (%3d, %3d): " |
| 171 | "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]", |
| 172 | x, y, r, g, b, pixel[0], pixel[1], pixel[2])); |
Tomasz Wasilczyk | 0e1adf4 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 173 | EXPECT_EQ(String8(), err) << err.c_str(); |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Galia Peycheva | 561d05c | 2021-03-08 15:14:05 +0100 | [diff] [blame] | 177 | Color getPixelColor(uint32_t x, uint32_t y) { |
| 178 | if (!mOutBuffer || mOutBuffer->getPixelFormat() != HAL_PIXEL_FORMAT_RGBA_8888) { |
| 179 | return {0, 0, 0, 0}; |
| 180 | } |
| 181 | |
| 182 | const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x)); |
| 183 | return {pixel[0], pixel[1], pixel[2], pixel[3]}; |
| 184 | } |
| 185 | |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 186 | void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); } |
| 187 | |
| 188 | void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); } |
| 189 | |
| 190 | void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); } |
| 191 | |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 192 | void expectSize(uint32_t width, uint32_t height) { |
| 193 | EXPECT_EQ(width, mOutBuffer->getWidth()); |
| 194 | EXPECT_EQ(height, mOutBuffer->getHeight()); |
| 195 | } |
| 196 | |
Alec Mouri | 14d5b86 | 2022-04-27 21:20:04 +0000 | [diff] [blame] | 197 | explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer, bool containsHdr) |
| 198 | : mOutBuffer(outBuffer), mContainsHdr(containsHdr) { |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 199 | if (mOutBuffer) { |
| 200 | mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels)); |
| 201 | } |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 204 | ~ScreenCapture() { |
| 205 | if (mOutBuffer) mOutBuffer->unlock(); |
| 206 | } |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 207 | |
| 208 | private: |
| 209 | sp<GraphicBuffer> mOutBuffer; |
Alec Mouri | 14d5b86 | 2022-04-27 21:20:04 +0000 | [diff] [blame] | 210 | bool mContainsHdr = mContainsHdr; |
Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 211 | uint8_t* mPixels = nullptr; |
| 212 | }; |
| 213 | } // namespace |
| 214 | } // namespace android |