blob: fbf5ecf388b45afac3ba26e5a1ee63fbbd9a7916 [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
18#include <ui/Rect.h>
19#include <utils/String8.h>
chaviw8ffc7b82020-08-18 11:25:37 -070020#include <functional>
21#include <future>
Valerie Haue9137b72019-08-27 13:22:18 -070022#include "TransactionUtils.h"
23
24namespace android {
25
26namespace {
27
chaviw8ffc7b82020-08-18 11:25:37 -070028class SyncScreenCaptureListener : public BnScreenCaptureListener {
29public:
30 status_t onScreenCaptureComplete(const ScreenCaptureResults& captureResults) override {
31 resultsPromise.set_value(captureResults);
32 return NO_ERROR;
33 }
34
35 ScreenCaptureResults waitForResults() {
36 std::future<ScreenCaptureResults> resultsFuture = resultsPromise.get_future();
37 return resultsFuture.get();
38 }
39
40private:
41 std::promise<ScreenCaptureResults> resultsPromise;
42};
43
Valerie Haue9137b72019-08-27 13:22:18 -070044// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
45// individual pixel values for testing purposes.
46class ScreenCapture : public RefBase {
47public:
chaviw8ffc7b82020-08-18 11:25:37 -070048 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
49 ScreenCaptureResults& captureResults) {
Valerie Haue9137b72019-08-27 13:22:18 -070050 const auto sf = ComposerService::getComposerService();
51 SurfaceComposerClient::Transaction().apply(true);
52
chaviw8ffc7b82020-08-18 11:25:37 -070053 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
54 status_t status = sf->captureDisplay(captureArgs, captureListener);
55 if (status != NO_ERROR) {
56 return status;
57 }
58 captureResults = captureListener->waitForResults();
59 return captureResults.result;
60 }
61
62 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
63 captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken());
chaviw4b9d5e12020-08-04 18:30:35 -070064 }
65
66 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
chaviwd2432892020-07-24 17:42:39 -070067 DisplayCaptureArgs args;
68 args.displayToken = displayToken;
chaviw4b9d5e12020-08-04 18:30:35 -070069 captureDisplay(sc, args);
Valerie Haue9137b72019-08-27 13:22:18 -070070 }
71
chaviw8ffc7b82020-08-18 11:25:37 -070072 static void captureDisplay(std::unique_ptr<ScreenCapture>* sc,
73 DisplayCaptureArgs& captureArgs) {
74 ScreenCaptureResults captureResults;
75 ASSERT_EQ(NO_ERROR, captureDisplay(captureArgs, captureResults));
76 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
77 }
78
79 static status_t captureLayers(LayerCaptureArgs& captureArgs,
80 ScreenCaptureResults& captureResults) {
81 const auto sf = ComposerService::getComposerService();
Valerie Haue9137b72019-08-27 13:22:18 -070082 SurfaceComposerClient::Transaction().apply(true);
83
chaviw8ffc7b82020-08-18 11:25:37 -070084 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
85 status_t status = sf->captureLayers(captureArgs, captureListener);
86 if (status != NO_ERROR) {
87 return status;
88 }
89 captureResults = captureListener->waitForResults();
90 return captureResults.result;
91 }
92
93 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, LayerCaptureArgs& captureArgs) {
chaviw3efadb12020-07-27 10:07:15 -070094 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -070095 ASSERT_EQ(NO_ERROR, captureLayers(captureArgs, captureResults));
chaviw3efadb12020-07-27 10:07:15 -070096 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
Valerie Haue9137b72019-08-27 13:22:18 -070097 }
98
99 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
100 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Valerie Haue271df92019-09-06 09:23:22 -0700101 TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Valerie Haue9137b72019-08-27 13:22:18 -0700102 }
103
104 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
105 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
106 const bool leftBorder = rect.left > 0;
107 const bool topBorder = rect.top > 0;
108 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
109 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
110
111 if (topBorder) {
112 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
113 if (leftBorder) {
114 top.left -= 1;
115 }
116 if (rightBorder) {
117 top.right += 1;
118 }
119 expectColor(top, color, tolerance);
120 }
121 if (leftBorder) {
122 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
123 expectColor(left, color, tolerance);
124 }
125 if (rightBorder) {
126 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
127 expectColor(right, color, tolerance);
128 }
129 if (bottomBorder) {
130 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
131 if (leftBorder) {
132 bottom.left -= 1;
133 }
134 if (rightBorder) {
135 bottom.right += 1;
136 }
137 expectColor(bottom, color, tolerance);
138 }
139 }
140
141 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
142 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
143 uint8_t tolerance = 0) {
144 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
145
146 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
147 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
148 // avoid checking borders due to unspecified filtering behavior
149 const int32_t offsetX = filtered ? 2 : 0;
150 const int32_t offsetY = filtered ? 2 : 0;
151 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
152 tolerance);
153 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
154 tolerance);
155 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
156 tolerance);
157 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
158 bottomRight, tolerance);
159 }
160
161 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
162 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
172 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
173
174 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
175
176 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
177
178 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
179 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
180 }
181
182 ~ScreenCapture() { mOutBuffer->unlock(); }
183
184private:
185 sp<GraphicBuffer> mOutBuffer;
186 uint8_t* mPixels = nullptr;
187};
188} // namespace
189} // namespace android