blob: ffdf55b6875e4f0597c71ee0a332e273b8f07fdd [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
34 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
35 const auto sf = ComposerService::getComposerService();
36 SurfaceComposerClient::Transaction().apply(true);
37
chaviwd2432892020-07-24 17:42:39 -070038 DisplayCaptureArgs args;
39 args.displayToken = displayToken;
40
41 ScreenCaptureResults captureResults;
42 ASSERT_EQ(NO_ERROR, sf->captureDisplay(args, captureResults));
43 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
Valerie Haue9137b72019-08-27 13:22:18 -070044 }
45
46 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
47 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
48 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
49 SurfaceComposerClient::Transaction().apply(true);
50
51 sp<GraphicBuffer> outBuffer;
52 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
53 *sc = std::make_unique<ScreenCapture>(outBuffer);
54 }
55
56 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
57 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
58 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
59 SurfaceComposerClient::Transaction().apply(true);
60
61 sp<GraphicBuffer> outBuffer;
62 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
63 *sc = std::make_unique<ScreenCapture>(outBuffer);
64 }
65
66 static void captureChildLayersExcluding(
67 std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
68 std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>> excludeLayers) {
69 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
70 SurfaceComposerClient::Transaction().apply(true);
71
72 sp<GraphicBuffer> outBuffer;
73 ASSERT_EQ(NO_ERROR,
74 sf->captureLayers(parentHandle, &outBuffer, ui::Dataspace::V0_SRGB,
75 ui::PixelFormat::RGBA_8888, Rect::EMPTY_RECT, excludeLayers,
76 1.0f, true));
77 *sc = std::make_unique<ScreenCapture>(outBuffer);
78 }
79
80 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
81 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Valerie Haue271df92019-09-06 09:23:22 -070082 TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Valerie Haue9137b72019-08-27 13:22:18 -070083 }
84
85 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
86 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
87 const bool leftBorder = rect.left > 0;
88 const bool topBorder = rect.top > 0;
89 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
90 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
91
92 if (topBorder) {
93 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
94 if (leftBorder) {
95 top.left -= 1;
96 }
97 if (rightBorder) {
98 top.right += 1;
99 }
100 expectColor(top, color, tolerance);
101 }
102 if (leftBorder) {
103 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
104 expectColor(left, color, tolerance);
105 }
106 if (rightBorder) {
107 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
108 expectColor(right, color, tolerance);
109 }
110 if (bottomBorder) {
111 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
112 if (leftBorder) {
113 bottom.left -= 1;
114 }
115 if (rightBorder) {
116 bottom.right += 1;
117 }
118 expectColor(bottom, color, tolerance);
119 }
120 }
121
122 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
123 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
124 uint8_t tolerance = 0) {
125 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
126
127 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
128 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
129 // avoid checking borders due to unspecified filtering behavior
130 const int32_t offsetX = filtered ? 2 : 0;
131 const int32_t offsetY = filtered ? 2 : 0;
132 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
133 tolerance);
134 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
135 tolerance);
136 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
137 tolerance);
138 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
139 bottomRight, tolerance);
140 }
141
142 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
143 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
144 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
145 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
146 String8 err(String8::format("pixel @ (%3d, %3d): "
147 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
148 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
149 EXPECT_EQ(String8(), err) << err.string();
150 }
151 }
152
153 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
154
155 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
156
157 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
158
159 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
160 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
161 }
162
163 ~ScreenCapture() { mOutBuffer->unlock(); }
164
165private:
166 sp<GraphicBuffer> mOutBuffer;
167 uint8_t* mPixels = nullptr;
168};
169} // namespace
170} // namespace android