blob: 2fefa45e5526ebf6afbe5da4a30c0b6fbfa35d29 [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
chaviwe7b9f272020-08-18 16:08:59 -070018#include <gui/SyncScreenCaptureListener.h>
Valerie Haue9137b72019-08-27 13:22:18 -070019#include <ui/Rect.h>
20#include <utils/String8.h>
chaviw8ffc7b82020-08-18 11:25:37 -070021#include <functional>
Valerie Haue9137b72019-08-27 13:22:18 -070022#include "TransactionUtils.h"
23
24namespace android {
25
26namespace {
27
28// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
29// individual pixel values for testing purposes.
30class ScreenCapture : public RefBase {
31public:
chaviw8ffc7b82020-08-18 11:25:37 -070032 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
33 ScreenCaptureResults& captureResults) {
Valerie Haue9137b72019-08-27 13:22:18 -070034 const auto sf = ComposerService::getComposerService();
35 SurfaceComposerClient::Transaction().apply(true);
36
Peiyong Lincd261472020-10-06 18:05:25 -070037 captureArgs.dataspace = ui::Dataspace::V0_SRGB;
chaviw8ffc7b82020-08-18 11:25:37 -070038 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
39 status_t status = sf->captureDisplay(captureArgs, captureListener);
arthurhung79e81aa2020-08-28 00:09:19 +080040
chaviw8ffc7b82020-08-18 11:25:37 -070041 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());
chaviw4b9d5e12020-08-04 18:30:35 -070050 }
51
52 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
chaviwd2432892020-07-24 17:42:39 -070053 DisplayCaptureArgs args;
54 args.displayToken = displayToken;
chaviw4b9d5e12020-08-04 18:30:35 -070055 captureDisplay(sc, args);
Valerie Haue9137b72019-08-27 13:22:18 -070056 }
57
chaviw8ffc7b82020-08-18 11:25:37 -070058 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 Haue9137b72019-08-27 13:22:18 -070068 SurfaceComposerClient::Transaction().apply(true);
69
Peiyong Lincd261472020-10-06 18:05:25 -070070 captureArgs.dataspace = ui::Dataspace::V0_SRGB;
chaviw8ffc7b82020-08-18 11:25:37 -070071 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) {
chaviw3efadb12020-07-27 10:07:15 -070081 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -070082 ASSERT_EQ(NO_ERROR, captureLayers(captureArgs, captureResults));
chaviw3efadb12020-07-27 10:07:15 -070083 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
Valerie Haue9137b72019-08-27 13:22:18 -070084 }
85
86 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Arthur Hung58144272021-01-16 03:43:53 +000087 ASSERT_NE(nullptr, mOutBuffer);
Valerie Haue9137b72019-08-27 13:22:18 -070088 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Valerie Haue271df92019-09-06 09:23:22 -070089 TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Valerie Haue9137b72019-08-27 13:22:18 -070090 }
91
92 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Arthur Hung58144272021-01-16 03:43:53 +000093 ASSERT_NE(nullptr, mOutBuffer);
Valerie Haue9137b72019-08-27 13:22:18 -070094 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
95 const bool leftBorder = rect.left > 0;
96 const bool topBorder = rect.top > 0;
97 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
98 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
99
100 if (topBorder) {
101 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
102 if (leftBorder) {
103 top.left -= 1;
104 }
105 if (rightBorder) {
106 top.right += 1;
107 }
108 expectColor(top, color, tolerance);
109 }
110 if (leftBorder) {
111 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
112 expectColor(left, color, tolerance);
113 }
114 if (rightBorder) {
115 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
116 expectColor(right, color, tolerance);
117 }
118 if (bottomBorder) {
119 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
120 if (leftBorder) {
121 bottom.left -= 1;
122 }
123 if (rightBorder) {
124 bottom.right += 1;
125 }
126 expectColor(bottom, color, tolerance);
127 }
128 }
129
130 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
131 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
132 uint8_t tolerance = 0) {
133 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
134
135 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
136 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
137 // avoid checking borders due to unspecified filtering behavior
138 const int32_t offsetX = filtered ? 2 : 0;
139 const int32_t offsetY = filtered ? 2 : 0;
140 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
141 tolerance);
142 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
143 tolerance);
144 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
145 tolerance);
146 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
147 bottomRight, tolerance);
148 }
149
150 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Arthur Hung58144272021-01-16 03:43:53 +0000151 ASSERT_NE(nullptr, mOutBuffer);
Valerie Haue9137b72019-08-27 13:22:18 -0700152 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
153 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
154 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
155 String8 err(String8::format("pixel @ (%3d, %3d): "
156 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
157 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
158 EXPECT_EQ(String8(), err) << err.string();
159 }
160 }
161
162 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
163
164 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
165
166 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
167
168 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
Arthur Hung58144272021-01-16 03:43:53 +0000169 if (mOutBuffer) {
170 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
171 }
Valerie Haue9137b72019-08-27 13:22:18 -0700172 }
173
Arthur Hung58144272021-01-16 03:43:53 +0000174 ~ScreenCapture() {
175 if (mOutBuffer) mOutBuffer->unlock();
176 }
Valerie Haue9137b72019-08-27 13:22:18 -0700177
178private:
179 sp<GraphicBuffer> mOutBuffer;
180 uint8_t* mPixels = nullptr;
181};
182} // namespace
183} // namespace android