blob: 56628f88bc3f9e3b14c289fdf690748c9ec3f198 [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
chaviw70cb6a42020-07-30 13:57:36 -070046 static void captureLayers(std::unique_ptr<ScreenCapture>* sc,
47 const LayerCaptureArgs& captureArgs) {
Valerie Haue9137b72019-08-27 13:22:18 -070048 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
49 SurfaceComposerClient::Transaction().apply(true);
50
chaviw3efadb12020-07-27 10:07:15 -070051 ScreenCaptureResults captureResults;
chaviw70cb6a42020-07-30 13:57:36 -070052 ASSERT_EQ(NO_ERROR, sf->captureLayers(captureArgs, captureResults));
chaviw3efadb12020-07-27 10:07:15 -070053 *sc = std::make_unique<ScreenCapture>(captureResults.buffer);
Valerie Haue9137b72019-08-27 13:22:18 -070054 }
55
56 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
57 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Valerie Haue271df92019-09-06 09:23:22 -070058 TransactionUtils::expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Valerie Haue9137b72019-08-27 13:22:18 -070059 }
60
61 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
62 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
63 const bool leftBorder = rect.left > 0;
64 const bool topBorder = rect.top > 0;
65 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
66 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
67
68 if (topBorder) {
69 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
70 if (leftBorder) {
71 top.left -= 1;
72 }
73 if (rightBorder) {
74 top.right += 1;
75 }
76 expectColor(top, color, tolerance);
77 }
78 if (leftBorder) {
79 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
80 expectColor(left, color, tolerance);
81 }
82 if (rightBorder) {
83 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
84 expectColor(right, color, tolerance);
85 }
86 if (bottomBorder) {
87 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
88 if (leftBorder) {
89 bottom.left -= 1;
90 }
91 if (rightBorder) {
92 bottom.right += 1;
93 }
94 expectColor(bottom, color, tolerance);
95 }
96 }
97
98 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
99 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
100 uint8_t tolerance = 0) {
101 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
102
103 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
104 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
105 // avoid checking borders due to unspecified filtering behavior
106 const int32_t offsetX = filtered ? 2 : 0;
107 const int32_t offsetY = filtered ? 2 : 0;
108 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
109 tolerance);
110 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
111 tolerance);
112 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
113 tolerance);
114 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
115 bottomRight, tolerance);
116 }
117
118 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
119 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
120 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
121 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
122 String8 err(String8::format("pixel @ (%3d, %3d): "
123 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
124 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
125 EXPECT_EQ(String8(), err) << err.string();
126 }
127 }
128
129 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
130
131 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
132
133 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
134
135 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
136 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
137 }
138
139 ~ScreenCapture() { mOutBuffer->unlock(); }
140
141private:
142 sp<GraphicBuffer> mOutBuffer;
143 uint8_t* mPixels = nullptr;
144};
145} // namespace
146} // namespace android