| 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 |  | 
| chaviw | e7b9f27 | 2020-08-18 16:08:59 -0700 | [diff] [blame] | 18 | #include <gui/SyncScreenCaptureListener.h> | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 19 | #include <ui/Rect.h> | 
 | 20 | #include <utils/String8.h> | 
| chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 21 | #include <functional> | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 22 | #include "TransactionUtils.h" | 
 | 23 |  | 
 | 24 | namespace android { | 
 | 25 |  | 
 | 26 | namespace { | 
 | 27 |  | 
 | 28 | // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check | 
 | 29 | // individual pixel values for testing purposes. | 
 | 30 | class ScreenCapture : public RefBase { | 
 | 31 | public: | 
| chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 32 |     static status_t captureDisplay(DisplayCaptureArgs& captureArgs, | 
 | 33 |                                    ScreenCaptureResults& captureResults) { | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 34 |         const auto sf = ComposerService::getComposerService(); | 
 | 35 |         SurfaceComposerClient::Transaction().apply(true); | 
 | 36 |  | 
| arthurhung | 79e81aa | 2020-08-28 00:09:19 +0800 | [diff] [blame] | 37 |         captureArgs.useRGBColorSpace = true; | 
| chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 38 |         const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener(); | 
 | 39 |         status_t status = sf->captureDisplay(captureArgs, captureListener); | 
| arthurhung | 79e81aa | 2020-08-28 00:09:19 +0800 | [diff] [blame] | 40 |  | 
| chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 41 |         if (status != NO_ERROR) { | 
 | 42 |             return status; | 
 | 43 |         } | 
 | 44 |         captureResults = captureListener->waitForResults(); | 
 | 45 |         return captureResults.result; | 
 | 46 |     } | 
 | 47 |  | 
 | 48 |     static void captureScreen(std::unique_ptr<ScreenCapture>* sc) { | 
 | 49 |         captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken()); | 
| chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 50 |     } | 
 | 51 |  | 
 | 52 |     static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) { | 
| chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 53 |         DisplayCaptureArgs args; | 
 | 54 |         args.displayToken = displayToken; | 
| chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 55 |         captureDisplay(sc, args); | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 56 |     } | 
 | 57 |  | 
| chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 58 |     static void captureDisplay(std::unique_ptr<ScreenCapture>* sc, | 
 | 59 |                                DisplayCaptureArgs& captureArgs) { | 
 | 60 |         ScreenCaptureResults captureResults; | 
 | 61 |         ASSERT_EQ(NO_ERROR, captureDisplay(captureArgs, captureResults)); | 
 | 62 |         *sc = std::make_unique<ScreenCapture>(captureResults.buffer); | 
 | 63 |     } | 
 | 64 |  | 
 | 65 |     static status_t captureLayers(LayerCaptureArgs& captureArgs, | 
 | 66 |                                   ScreenCaptureResults& captureResults) { | 
 | 67 |         const auto sf = ComposerService::getComposerService(); | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 68 |         SurfaceComposerClient::Transaction().apply(true); | 
 | 69 |  | 
| arthurhung | 79e81aa | 2020-08-28 00:09:19 +0800 | [diff] [blame] | 70 |         captureArgs.useRGBColorSpace = true; | 
| chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 71 |         const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener(); | 
 | 72 |         status_t status = sf->captureLayers(captureArgs, captureListener); | 
 | 73 |         if (status != NO_ERROR) { | 
 | 74 |             return status; | 
 | 75 |         } | 
 | 76 |         captureResults = captureListener->waitForResults(); | 
 | 77 |         return captureResults.result; | 
 | 78 |     } | 
 | 79 |  | 
 | 80 |     static void captureLayers(std::unique_ptr<ScreenCapture>* sc, LayerCaptureArgs& captureArgs) { | 
| chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 81 |         ScreenCaptureResults captureResults; | 
| chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 82 |         ASSERT_EQ(NO_ERROR, captureLayers(captureArgs, captureResults)); | 
| chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 83 |         *sc = std::make_unique<ScreenCapture>(captureResults.buffer); | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 84 |     } | 
 | 85 |  | 
 | 86 |     void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) { | 
 | 87 |         ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 88 |         TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance); | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 89 |     } | 
 | 90 |  | 
 | 91 |     void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) { | 
 | 92 |         ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); | 
 | 93 |         const bool leftBorder = rect.left > 0; | 
 | 94 |         const bool topBorder = rect.top > 0; | 
 | 95 |         const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth()); | 
 | 96 |         const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight()); | 
 | 97 |  | 
 | 98 |         if (topBorder) { | 
 | 99 |             Rect top(rect.left, rect.top - 1, rect.right, rect.top); | 
 | 100 |             if (leftBorder) { | 
 | 101 |                 top.left -= 1; | 
 | 102 |             } | 
 | 103 |             if (rightBorder) { | 
 | 104 |                 top.right += 1; | 
 | 105 |             } | 
 | 106 |             expectColor(top, color, tolerance); | 
 | 107 |         } | 
 | 108 |         if (leftBorder) { | 
 | 109 |             Rect left(rect.left - 1, rect.top, rect.left, rect.bottom); | 
 | 110 |             expectColor(left, color, tolerance); | 
 | 111 |         } | 
 | 112 |         if (rightBorder) { | 
 | 113 |             Rect right(rect.right, rect.top, rect.right + 1, rect.bottom); | 
 | 114 |             expectColor(right, color, tolerance); | 
 | 115 |         } | 
 | 116 |         if (bottomBorder) { | 
 | 117 |             Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1); | 
 | 118 |             if (leftBorder) { | 
 | 119 |                 bottom.left -= 1; | 
 | 120 |             } | 
 | 121 |             if (rightBorder) { | 
 | 122 |                 bottom.right += 1; | 
 | 123 |             } | 
 | 124 |             expectColor(bottom, color, tolerance); | 
 | 125 |         } | 
 | 126 |     } | 
 | 127 |  | 
 | 128 |     void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight, | 
 | 129 |                         const Color& bottomLeft, const Color& bottomRight, bool filtered = false, | 
 | 130 |                         uint8_t tolerance = 0) { | 
 | 131 |         ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0); | 
 | 132 |  | 
 | 133 |         const int32_t centerX = rect.left + (rect.right - rect.left) / 2; | 
 | 134 |         const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2; | 
 | 135 |         // avoid checking borders due to unspecified filtering behavior | 
 | 136 |         const int32_t offsetX = filtered ? 2 : 0; | 
 | 137 |         const int32_t offsetY = filtered ? 2 : 0; | 
 | 138 |         expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft, | 
 | 139 |                     tolerance); | 
 | 140 |         expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight, | 
 | 141 |                     tolerance); | 
 | 142 |         expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft, | 
 | 143 |                     tolerance); | 
 | 144 |         expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom), | 
 | 145 |                     bottomRight, tolerance); | 
 | 146 |     } | 
 | 147 |  | 
 | 148 |     void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { | 
 | 149 |         ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); | 
 | 150 |         const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x)); | 
 | 151 |         if (r != pixel[0] || g != pixel[1] || b != pixel[2]) { | 
 | 152 |             String8 err(String8::format("pixel @ (%3d, %3d): " | 
 | 153 |                                         "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]", | 
 | 154 |                                         x, y, r, g, b, pixel[0], pixel[1], pixel[2])); | 
 | 155 |             EXPECT_EQ(String8(), err) << err.string(); | 
 | 156 |         } | 
 | 157 |     } | 
 | 158 |  | 
 | 159 |     void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); } | 
 | 160 |  | 
 | 161 |     void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); } | 
 | 162 |  | 
 | 163 |     void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); } | 
 | 164 |  | 
 | 165 |     explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) { | 
 | 166 |         mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels)); | 
 | 167 |     } | 
 | 168 |  | 
 | 169 |     ~ScreenCapture() { mOutBuffer->unlock(); } | 
 | 170 |  | 
 | 171 | private: | 
 | 172 |     sp<GraphicBuffer> mOutBuffer; | 
 | 173 |     uint8_t* mPixels = nullptr; | 
 | 174 | }; | 
 | 175 | } // namespace | 
 | 176 | } // namespace android |