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