blob: 081d18b6b8549cb59e5efa06adfb94b99fb53ba1 [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>
20#include "TransactionUtils.h"
21
22namespace android {
23
24namespace {
25
26// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
27// individual pixel values for testing purposes.
28class ScreenCapture : public RefBase {
29public:
30 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
31 captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken());
32 }
33
chaviw4b9d5e12020-08-04 18:30:35 -070034 static void captureDisplay(std::unique_ptr<ScreenCapture>* sc,
35 const DisplayCaptureArgs& captureArgs) {
Valerie Haue9137b72019-08-27 13:22:18 -070036 const auto sf = ComposerService::getComposerService();
37 SurfaceComposerClient::Transaction().apply(true);
38
chaviw4b9d5e12020-08-04 18:30:35 -070039 ScreenCaptureResults captureResults;
40 ASSERT_EQ(NO_ERROR, sf->captureDisplay(captureArgs, captureResults));
41 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
42 }
43
44 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
chaviwd2432892020-07-24 17:42:39 -070045 DisplayCaptureArgs args;
46 args.displayToken = displayToken;
chaviw4b9d5e12020-08-04 18:30:35 -070047 captureDisplay(sc, args);
Valerie Haue9137b72019-08-27 13:22:18 -070048 }
49
chaviw70cb6a42020-07-30 13:57:36 -070050 static void captureLayers(std::unique_ptr<ScreenCapture>* sc,
51 const LayerCaptureArgs& captureArgs) {
Valerie Haue9137b72019-08-27 13:22:18 -070052 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
53 SurfaceComposerClient::Transaction().apply(true);
54
chaviw3efadb12020-07-27 10:07:15 -070055 ScreenCaptureResults captureResults;
chaviw70cb6a42020-07-30 13:57:36 -070056 ASSERT_EQ(NO_ERROR, sf->captureLayers(captureArgs, captureResults));
chaviw3efadb12020-07-27 10:07:15 -070057 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
Valerie Haue9137b72019-08-27 13:22:18 -070058 }
59
60 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
61 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Valerie Haue271df92019-09-06 09:23:22 -070062 TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Valerie Haue9137b72019-08-27 13:22:18 -070063 }
64
65 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
66 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
67 const bool leftBorder = rect.left > 0;
68 const bool topBorder = rect.top > 0;
69 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
70 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
71
72 if (topBorder) {
73 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
74 if (leftBorder) {
75 top.left -= 1;
76 }
77 if (rightBorder) {
78 top.right += 1;
79 }
80 expectColor(top, color, tolerance);
81 }
82 if (leftBorder) {
83 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
84 expectColor(left, color, tolerance);
85 }
86 if (rightBorder) {
87 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
88 expectColor(right, color, tolerance);
89 }
90 if (bottomBorder) {
91 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
92 if (leftBorder) {
93 bottom.left -= 1;
94 }
95 if (rightBorder) {
96 bottom.right += 1;
97 }
98 expectColor(bottom, color, tolerance);
99 }
100 }
101
102 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
103 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
104 uint8_t tolerance = 0) {
105 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
106
107 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
108 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
109 // avoid checking borders due to unspecified filtering behavior
110 const int32_t offsetX = filtered ? 2 : 0;
111 const int32_t offsetY = filtered ? 2 : 0;
112 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
113 tolerance);
114 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
115 tolerance);
116 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
117 tolerance);
118 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
119 bottomRight, tolerance);
120 }
121
122 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
123 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
124 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
125 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
126 String8 err(String8::format("pixel @ (%3d, %3d): "
127 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
128 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
129 EXPECT_EQ(String8(), err) << err.string();
130 }
131 }
132
133 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
134
135 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
136
137 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
138
139 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
140 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
141 }
142
143 ~ScreenCapture() { mOutBuffer->unlock(); }
144
145private:
146 sp<GraphicBuffer> mOutBuffer;
147 uint8_t* mPixels = nullptr;
148};
149} // namespace
150} // namespace android