blob: 5c5b18ec1c4d6265d500700eb67d1ec4b13162ec [file] [log] [blame]
Valerie Haue9137b72019-08-27 13:22:18 -07001/*
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 Shalamanov30b0b3c2020-10-13 19:15:06 +020019#pragma clang diagnostic push
20#pragma clang diagnostic ignored "-Wconversion"
21
Valerie Haue9137b72019-08-27 13:22:18 -070022#include <chrono>
Valerie Haue9137b72019-08-27 13:22:18 -070023
24#include <android/native_window.h>
Valerie Haue9137b72019-08-27 13:22:18 -070025#include <binder/IPCThreadState.h>
Peiyong Line9d809e2020-04-14 13:10:48 -070026#include <gtest/gtest.h>
Valerie Haue9137b72019-08-27 13:22:18 -070027#include <gui/Surface.h>
28#include <gui/SurfaceComposerClient.h>
Valerie Haue9137b72019-08-27 13:22:18 -070029#include <private/gui/ComposerService.h>
Valerie Haue9137b72019-08-27 13:22:18 -070030#include <ui/GraphicBuffer.h>
31#include <ui/Rect.h>
32
33#include "ColorUtils.h"
Valerie Haue9137b72019-08-27 13:22:18 -070034
35namespace android {
36
37namespace {
38
39using namespace std::chrono_literals;
Valerie Haue271df92019-09-06 09:23:22 -070040using Transaction = SurfaceComposerClient::Transaction;
Valerie Haue9137b72019-08-27 13:22:18 -070041
42std::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 Haue271df92019-09-06 09:23:22 -070047class TransactionUtils {
48public:
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 Haue9137b72019-08-27 13:22:18 -070056
Valerie Haue271df92019-09-06 09:23:22 -070057 int32_t width = r.right - r.left;
58 int32_t height = r.bottom - r.top;
Valerie Haue9137b72019-08-27 13:22:18 -070059
Valerie Haue271df92019-09-06 09:23:22 -070060 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 Haue9137b72019-08-27 13:22:18 -070070 }
71 }
Valerie Haue9137b72019-08-27 13:22:18 -070072
Valerie Haue271df92019-09-06 09:23:22 -070073 // 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 Haue9137b72019-08-27 13:22:18 -070099 }
100
Valerie Haue271df92019-09-06 09:23:22 -0700101 // 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 Haue9137b72019-08-27 13:22:18 -0700108
Valerie Haue271df92019-09-06 09:23:22 -0700109 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 Haue9137b72019-08-27 13:22:18 -0700119
Valerie Haue271df92019-09-06 09:23:22 -0700120 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 Haue9137b72019-08-27 13:22:18 -0700134 }
135 }
Valerie Haue9137b72019-08-27 13:22:18 -0700136
Vishnu Nairefc42e22019-12-03 17:36:12 -0800137 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 Haue271df92019-09-06 09:23:22 -0700142 // 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 Haue9137b72019-08-27 13:22:18 -0700161 }
162 }
Valerie Haue271df92019-09-06 09:23:22 -0700163};
Valerie Haue9137b72019-08-27 13:22:18 -0700164
165enum 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.
169class BinderEnvironment : public ::testing::Environment {
170public:
171 void SetUp() override { ProcessState::self()->startThreadPool(); }
172};
173
174/** RAII Wrapper around get/seteuid */
175class UIDFaker {
176 uid_t oldId;
177
178public:
179 UIDFaker(uid_t uid) {
180 oldId = geteuid();
181 seteuid(uid);
182 }
183 ~UIDFaker() { seteuid(oldId); }
184};
185} // namespace
186} // namespace android
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200187
188// TODO(b/129481165): remove the #pragma below and fix conversion issues
189#pragma clang diagnostic pop // ignored "-Wconversion"