blob: 167d7776cd97af6d77806f3b4e8926a0aaa7abc0 [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
chaviwdcafa8d2020-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
chaviw8ffc7b82020-08-18 11:25:37 -070037 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
38 status_t status = sf->captureDisplay(captureArgs, captureListener);
39 if (status != NO_ERROR) {
40 return status;
41 }
42 captureResults = captureListener->waitForResults();
43 return captureResults.result;
44 }
45
46 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
47 captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken());
chaviw4b9d5e12020-08-04 18:30:35 -070048 }
49
50 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
chaviwd2432892020-07-24 17:42:39 -070051 DisplayCaptureArgs args;
52 args.displayToken = displayToken;
chaviw4b9d5e12020-08-04 18:30:35 -070053 captureDisplay(sc, args);
Valerie Haue9137b72019-08-27 13:22:18 -070054 }
55
chaviw8ffc7b82020-08-18 11:25:37 -070056 static void captureDisplay(std::unique_ptr<ScreenCapture>* sc,
57 DisplayCaptureArgs& captureArgs) {
58 ScreenCaptureResults captureResults;
59 ASSERT_EQ(NO_ERROR, captureDisplay(captureArgs, captureResults));
60 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
61 }
62
63 static status_t captureLayers(LayerCaptureArgs& captureArgs,
64 ScreenCaptureResults& captureResults) {
65 const auto sf = ComposerService::getComposerService();
Valerie Haue9137b72019-08-27 13:22:18 -070066 SurfaceComposerClient::Transaction().apply(true);
67
chaviw8ffc7b82020-08-18 11:25:37 -070068 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
69 status_t status = sf->captureLayers(captureArgs, captureListener);
70 if (status != NO_ERROR) {
71 return status;
72 }
73 captureResults = captureListener->waitForResults();
74 return captureResults.result;
75 }
76
77 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, LayerCaptureArgs& captureArgs) {
chaviw3efadb12020-07-27 10:07:15 -070078 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -070079 ASSERT_EQ(NO_ERROR, captureLayers(captureArgs, captureResults));
chaviw3efadb12020-07-27 10:07:15 -070080 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
Valerie Haue9137b72019-08-27 13:22:18 -070081 }
82
83 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
84 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Valerie Haue271df92019-09-06 09:23:22 -070085 TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Valerie Haue9137b72019-08-27 13:22:18 -070086 }
87
88 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
89 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
90 const bool leftBorder = rect.left > 0;
91 const bool topBorder = rect.top > 0;
92 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
93 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
94
95 if (topBorder) {
96 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
97 if (leftBorder) {
98 top.left -= 1;
99 }
100 if (rightBorder) {
101 top.right += 1;
102 }
103 expectColor(top, color, tolerance);
104 }
105 if (leftBorder) {
106 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
107 expectColor(left, color, tolerance);
108 }
109 if (rightBorder) {
110 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
111 expectColor(right, color, tolerance);
112 }
113 if (bottomBorder) {
114 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
115 if (leftBorder) {
116 bottom.left -= 1;
117 }
118 if (rightBorder) {
119 bottom.right += 1;
120 }
121 expectColor(bottom, color, tolerance);
122 }
123 }
124
125 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
126 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
127 uint8_t tolerance = 0) {
128 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
129
130 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
131 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
132 // avoid checking borders due to unspecified filtering behavior
133 const int32_t offsetX = filtered ? 2 : 0;
134 const int32_t offsetY = filtered ? 2 : 0;
135 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
136 tolerance);
137 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
138 tolerance);
139 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
140 tolerance);
141 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
142 bottomRight, tolerance);
143 }
144
145 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
146 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
147 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
148 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
149 String8 err(String8::format("pixel @ (%3d, %3d): "
150 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
151 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
152 EXPECT_EQ(String8(), err) << err.string();
153 }
154 }
155
156 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
157
158 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
159
160 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
161
162 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
163 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
164 }
165
166 ~ScreenCapture() { mOutBuffer->unlock(); }
167
168private:
169 sp<GraphicBuffer> mOutBuffer;
170 uint8_t* mPixels = nullptr;
171};
172} // namespace
173} // namespace android