blob: cb7040a68b04e23a4ec0787c75e2f9ea7ac2067f [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#pragma once
17
Huihong Luo3bdef862022-03-03 11:57:19 -080018#include <gui/AidlStatusUtil.h>
chaviwe7b9f272020-08-18 16:08:59 -070019#include <gui/SyncScreenCaptureListener.h>
Huihong Luof5029222021-12-16 14:33:46 -080020#include <private/gui/ComposerServiceAIDL.h>
Valerie Haue9137b72019-08-27 13:22:18 -070021#include <ui/Rect.h>
22#include <utils/String8.h>
chaviw8ffc7b82020-08-18 11:25:37 -070023#include <functional>
Valerie Haue9137b72019-08-27 13:22:18 -070024#include "TransactionUtils.h"
25
26namespace android {
27
Huihong Luo3bdef862022-03-03 11:57:19 -080028using gui::aidl_utils::statusTFromBinderStatus;
29
Valerie Haue9137b72019-08-27 13:22:18 -070030namespace {
31
32// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
33// individual pixel values for testing purposes.
34class ScreenCapture : public RefBase {
35public:
chaviw8ffc7b82020-08-18 11:25:37 -070036 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
37 ScreenCaptureResults& captureResults) {
Huihong Luof5029222021-12-16 14:33:46 -080038 const auto sf = ComposerServiceAIDL::getComposerService();
Valerie Haue9137b72019-08-27 13:22:18 -070039 SurfaceComposerClient::Transaction().apply(true);
40
Peiyong Lincd261472020-10-06 18:05:25 -070041 captureArgs.dataspace = ui::Dataspace::V0_SRGB;
Ady Abrahamd11bade2022-08-01 16:18:03 -070042 const sp<SyncScreenCaptureListener> captureListener = sp<SyncScreenCaptureListener>::make();
Huihong Luof5029222021-12-16 14:33:46 -080043 binder::Status status = sf->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -080044 status_t err = statusTFromBinderStatus(status);
45 if (err != NO_ERROR) {
46 return err;
chaviw8ffc7b82020-08-18 11:25:37 -070047 }
48 captureResults = captureListener->waitForResults();
49 return captureResults.result;
50 }
51
52 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
53 captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken());
chaviw4b9d5e12020-08-04 18:30:35 -070054 }
55
56 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
chaviwd2432892020-07-24 17:42:39 -070057 DisplayCaptureArgs args;
58 args.displayToken = displayToken;
chaviw4b9d5e12020-08-04 18:30:35 -070059 captureDisplay(sc, args);
Valerie Haue9137b72019-08-27 13:22:18 -070060 }
61
chaviw8ffc7b82020-08-18 11:25:37 -070062 static void captureDisplay(std::unique_ptr<ScreenCapture>* sc,
63 DisplayCaptureArgs& captureArgs) {
64 ScreenCaptureResults captureResults;
65 ASSERT_EQ(NO_ERROR, captureDisplay(captureArgs, captureResults));
Alec Mouri14d5b862022-04-27 21:20:04 +000066 *sc = std::make_unique<ScreenCapture>(captureResults.buffer,
67 captureResults.capturedHdrLayers);
chaviw8ffc7b82020-08-18 11:25:37 -070068 }
69
70 static status_t captureLayers(LayerCaptureArgs& captureArgs,
71 ScreenCaptureResults& captureResults) {
Huihong Luof5029222021-12-16 14:33:46 -080072 const auto sf = ComposerServiceAIDL::getComposerService();
Valerie Haue9137b72019-08-27 13:22:18 -070073 SurfaceComposerClient::Transaction().apply(true);
74
Peiyong Lincd261472020-10-06 18:05:25 -070075 captureArgs.dataspace = ui::Dataspace::V0_SRGB;
Ady Abrahamd11bade2022-08-01 16:18:03 -070076 const sp<SyncScreenCaptureListener> captureListener = sp<SyncScreenCaptureListener>::make();
Huihong Luof5029222021-12-16 14:33:46 -080077 binder::Status status = sf->captureLayers(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -080078 status_t err = statusTFromBinderStatus(status);
79 if (err != NO_ERROR) {
80 return err;
chaviw8ffc7b82020-08-18 11:25:37 -070081 }
82 captureResults = captureListener->waitForResults();
83 return captureResults.result;
84 }
85
86 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, LayerCaptureArgs& captureArgs) {
chaviw3efadb12020-07-27 10:07:15 -070087 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -070088 ASSERT_EQ(NO_ERROR, captureLayers(captureArgs, captureResults));
Alec Mouri14d5b862022-04-27 21:20:04 +000089 *sc = std::make_unique<ScreenCapture>(captureResults.buffer,
90 captureResults.capturedHdrLayers);
Valerie Haue9137b72019-08-27 13:22:18 -070091 }
92
Alec Mouri14d5b862022-04-27 21:20:04 +000093 bool capturedHdrLayers() const { return mContainsHdr; }
94
Valerie Haue9137b72019-08-27 13:22:18 -070095 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Arthur Hung58144272021-01-16 03:43:53 +000096 ASSERT_NE(nullptr, mOutBuffer);
arthurhungf1b7c7b2021-03-03 17:42:23 +080097 ASSERT_NE(nullptr, mPixels);
Valerie Haue9137b72019-08-27 13:22:18 -070098 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Valerie Haue271df92019-09-06 09:23:22 -070099 TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Valerie Haue9137b72019-08-27 13:22:18 -0700100 }
101
102 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Arthur Hung58144272021-01-16 03:43:53 +0000103 ASSERT_NE(nullptr, mOutBuffer);
Valerie Haue9137b72019-08-27 13:22:18 -0700104 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
105 const bool leftBorder = rect.left > 0;
106 const bool topBorder = rect.top > 0;
107 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
108 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
109
110 if (topBorder) {
111 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
112 if (leftBorder) {
113 top.left -= 1;
114 }
115 if (rightBorder) {
116 top.right += 1;
117 }
118 expectColor(top, color, tolerance);
119 }
120 if (leftBorder) {
121 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
122 expectColor(left, color, tolerance);
123 }
124 if (rightBorder) {
125 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
126 expectColor(right, color, tolerance);
127 }
128 if (bottomBorder) {
129 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
130 if (leftBorder) {
131 bottom.left -= 1;
132 }
133 if (rightBorder) {
134 bottom.right += 1;
135 }
136 expectColor(bottom, color, tolerance);
137 }
138 }
139
140 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
141 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
142 uint8_t tolerance = 0) {
143 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
144
145 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
146 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
147 // avoid checking borders due to unspecified filtering behavior
148 const int32_t offsetX = filtered ? 2 : 0;
149 const int32_t offsetY = filtered ? 2 : 0;
150 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
151 tolerance);
152 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
153 tolerance);
154 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
155 tolerance);
156 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
157 bottomRight, tolerance);
158 }
159
160 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Arthur Hung58144272021-01-16 03:43:53 +0000161 ASSERT_NE(nullptr, mOutBuffer);
Valerie Haue9137b72019-08-27 13:22:18 -0700162 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
163 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
164 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
165 String8 err(String8::format("pixel @ (%3d, %3d): "
166 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
167 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
168 EXPECT_EQ(String8(), err) << err.string();
169 }
170 }
171
Galia Peycheva561d05c2021-03-08 15:14:05 +0100172 Color getPixelColor(uint32_t x, uint32_t y) {
173 if (!mOutBuffer || mOutBuffer->getPixelFormat() != HAL_PIXEL_FORMAT_RGBA_8888) {
174 return {0, 0, 0, 0};
175 }
176
177 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
178 return {pixel[0], pixel[1], pixel[2], pixel[3]};
179 }
180
Valerie Haue9137b72019-08-27 13:22:18 -0700181 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
182
183 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
184
185 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
186
chaviw79468ab2021-10-27 11:11:24 -0500187 void expectSize(uint32_t width, uint32_t height) {
188 EXPECT_EQ(width, mOutBuffer->getWidth());
189 EXPECT_EQ(height, mOutBuffer->getHeight());
190 }
191
Alec Mouri14d5b862022-04-27 21:20:04 +0000192 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer, bool containsHdr)
193 : mOutBuffer(outBuffer), mContainsHdr(containsHdr) {
Arthur Hung58144272021-01-16 03:43:53 +0000194 if (mOutBuffer) {
195 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
196 }
Valerie Haue9137b72019-08-27 13:22:18 -0700197 }
198
Arthur Hung58144272021-01-16 03:43:53 +0000199 ~ScreenCapture() {
200 if (mOutBuffer) mOutBuffer->unlock();
201 }
Valerie Haue9137b72019-08-27 13:22:18 -0700202
203private:
204 sp<GraphicBuffer> mOutBuffer;
Alec Mouri14d5b862022-04-27 21:20:04 +0000205 bool mContainsHdr = mContainsHdr;
Valerie Haue9137b72019-08-27 13:22:18 -0700206 uint8_t* mPixels = nullptr;
207};
208} // namespace
209} // namespace android