| 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 |  | 
 | 17 | #pragma once | 
 | 18 |  | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 19 | #include <chrono> | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> | 
 | 21 |  | 
 | 22 | #include <android/native_window.h> | 
 | 23 | #include <hardware/hwcomposer_defs.h> | 
 | 24 |  | 
 | 25 | #include <binder/IPCThreadState.h> | 
 | 26 |  | 
 | 27 | #include <gui/Surface.h> | 
 | 28 | #include <gui/SurfaceComposerClient.h> | 
 | 29 |  | 
 | 30 | #include <private/gui/ComposerService.h> | 
 | 31 |  | 
 | 32 | #include <ui/GraphicBuffer.h> | 
 | 33 | #include <ui/Rect.h> | 
 | 34 |  | 
 | 35 | #include "ColorUtils.h" | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 36 |  | 
 | 37 | namespace android { | 
 | 38 |  | 
 | 39 | namespace { | 
 | 40 |  | 
 | 41 | using namespace std::chrono_literals; | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 42 | using Transaction = SurfaceComposerClient::Transaction; | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 43 |  | 
 | 44 | std::ostream& operator<<(std::ostream& os, const Color& color) { | 
 | 45 |     os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a); | 
 | 46 |     return os; | 
 | 47 | } | 
 | 48 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 49 | class TransactionUtils { | 
 | 50 | public: | 
 | 51 |     // Fill a region with the specified color. | 
 | 52 |     static void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, | 
 | 53 |                                              const Color& color) { | 
 | 54 |         Rect r(0, 0, buffer.width, buffer.height); | 
 | 55 |         if (!r.intersect(rect, &r)) { | 
 | 56 |             return; | 
 | 57 |         } | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 58 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 59 |         int32_t width = r.right - r.left; | 
 | 60 |         int32_t height = r.bottom - r.top; | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 61 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 62 |         for (int32_t row = 0; row < height; row++) { | 
 | 63 |             uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + | 
 | 64 |                     (buffer.stride * (r.top + row) + r.left) * 4; | 
 | 65 |             for (int32_t column = 0; column < width; column++) { | 
 | 66 |                 dst[0] = color.r; | 
 | 67 |                 dst[1] = color.g; | 
 | 68 |                 dst[2] = color.b; | 
 | 69 |                 dst[3] = color.a; | 
 | 70 |                 dst += 4; | 
 | 71 |             } | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 72 |         } | 
 | 73 |     } | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 74 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 75 |     // Fill a region with the specified color. | 
 | 76 |     static void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, | 
 | 77 |                                        const Color& color) { | 
 | 78 |         Rect r(0, 0, buffer->width, buffer->height); | 
 | 79 |         if (!r.intersect(rect, &r)) { | 
 | 80 |             return; | 
 | 81 |         } | 
 | 82 |  | 
 | 83 |         int32_t width = r.right - r.left; | 
 | 84 |         int32_t height = r.bottom - r.top; | 
 | 85 |  | 
 | 86 |         uint8_t* pixels; | 
 | 87 |         buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, | 
 | 88 |                      reinterpret_cast<void**>(&pixels)); | 
 | 89 |  | 
 | 90 |         for (int32_t row = 0; row < height; row++) { | 
 | 91 |             uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4; | 
 | 92 |             for (int32_t column = 0; column < width; column++) { | 
 | 93 |                 dst[0] = color.r; | 
 | 94 |                 dst[1] = color.g; | 
 | 95 |                 dst[2] = color.b; | 
 | 96 |                 dst[3] = color.a; | 
 | 97 |                 dst += 4; | 
 | 98 |             } | 
 | 99 |         } | 
 | 100 |         buffer->unlock(); | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 101 |     } | 
 | 102 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 103 |     // Check if a region has the specified color. | 
 | 104 |     static void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, | 
 | 105 |                                   const Rect& rect, const Color& color, uint8_t tolerance) { | 
 | 106 |         int32_t x = rect.left; | 
 | 107 |         int32_t y = rect.top; | 
 | 108 |         int32_t width = rect.right - rect.left; | 
 | 109 |         int32_t height = rect.bottom - rect.top; | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 110 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 111 |         int32_t bufferWidth = int32_t(outBuffer->getWidth()); | 
 | 112 |         int32_t bufferHeight = int32_t(outBuffer->getHeight()); | 
 | 113 |         if (x + width > bufferWidth) { | 
 | 114 |             x = std::min(x, bufferWidth); | 
 | 115 |             width = bufferWidth - x; | 
 | 116 |         } | 
 | 117 |         if (y + height > bufferHeight) { | 
 | 118 |             y = std::min(y, bufferHeight); | 
 | 119 |             height = bufferHeight - y; | 
 | 120 |         } | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 121 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 122 |         auto colorCompare = [tolerance](uint8_t a, uint8_t b) { | 
 | 123 |             uint8_t tmp = a >= b ? a - b : b - a; | 
 | 124 |             return tmp <= tolerance; | 
 | 125 |         }; | 
 | 126 |         for (int32_t j = 0; j < height; j++) { | 
 | 127 |             const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4; | 
 | 128 |             for (int32_t i = 0; i < width; i++) { | 
 | 129 |                 const uint8_t expected[4] = {color.r, color.g, color.b, color.a}; | 
 | 130 |                 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare)) | 
 | 131 |                         << "pixel @ (" << x + i << ", " << y + j << "): " | 
 | 132 |                         << "expected (" << color << "), " | 
 | 133 |                         << "got (" << Color{src[0], src[1], src[2], src[3]} << ")"; | 
 | 134 |                 src += 4; | 
 | 135 |             } | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 136 |         } | 
 | 137 |     } | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 138 |  | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 139 |     // Fill an RGBA_8888 formatted surface with a single color. | 
 | 140 |     static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b, | 
 | 141 |                                  bool unlock = true) { | 
 | 142 |         ANativeWindow_Buffer outBuffer; | 
 | 143 |         sp<Surface> s = sc->getSurface(); | 
 | 144 |         ASSERT_TRUE(s != nullptr); | 
 | 145 |         ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr)); | 
 | 146 |         uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits); | 
 | 147 |         for (int y = 0; y < outBuffer.height; y++) { | 
 | 148 |             for (int x = 0; x < outBuffer.width; x++) { | 
 | 149 |                 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x)); | 
 | 150 |                 pixel[0] = r; | 
 | 151 |                 pixel[1] = g; | 
 | 152 |                 pixel[2] = b; | 
 | 153 |                 pixel[3] = 255; | 
 | 154 |             } | 
 | 155 |         } | 
 | 156 |         if (unlock) { | 
 | 157 |             ASSERT_EQ(NO_ERROR, s->unlockAndPost()); | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 158 |         } | 
 | 159 |     } | 
| Valerie Hau | e271df9 | 2019-09-06 09:23:22 -0700 | [diff] [blame] | 160 | }; | 
| Valerie Hau | e9137b7 | 2019-08-27 13:22:18 -0700 | [diff] [blame] | 161 |  | 
 | 162 | enum class RenderPath { SCREENSHOT, VIRTUAL_DISPLAY }; | 
 | 163 |  | 
 | 164 | // Environment for starting up binder threads. This is required for testing | 
 | 165 | // virtual displays, as BufferQueue parameters may be queried over binder. | 
 | 166 | class BinderEnvironment : public ::testing::Environment { | 
 | 167 | public: | 
 | 168 |     void SetUp() override { ProcessState::self()->startThreadPool(); } | 
 | 169 | }; | 
 | 170 |  | 
 | 171 | /** RAII Wrapper around get/seteuid */ | 
 | 172 | class UIDFaker { | 
 | 173 |     uid_t oldId; | 
 | 174 |  | 
 | 175 | public: | 
 | 176 |     UIDFaker(uid_t uid) { | 
 | 177 |         oldId = geteuid(); | 
 | 178 |         seteuid(uid); | 
 | 179 |     } | 
 | 180 |     ~UIDFaker() { seteuid(oldId); } | 
 | 181 | }; | 
 | 182 | } // namespace | 
 | 183 | } // namespace android |