blob: 7edddb6ea3651a899873d0346b6f6d4cad3c89ca [file] [log] [blame]
Valerie Hau9cfc6d82019-09-23 13:54:07 -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#ifndef ANDROID_LAYER_TRANSACTION_TEST_H
17#define ANDROID_LAYER_TRANSACTION_TEST_H
18
19#include <gtest/gtest.h>
20
21#include <gui/ISurfaceComposer.h>
22#include <gui/SurfaceComposerClient.h>
23#include <hardware/hwcomposer_defs.h>
24#include <private/gui/ComposerService.h>
25
26#include <ui/DisplayInfo.h>
27
28#include "BufferGenerator.h"
29#include "utils/ScreenshotUtils.h"
30#include "utils/TransactionUtils.h"
31
32namespace android {
33
34using android::hardware::graphics::common::V1_1::BufferUsage;
35
36class LayerTransactionTest : public ::testing::Test {
37protected:
38 void SetUp() override {
39 mClient = new SurfaceComposerClient;
40 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
41
42 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
43
44 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
45 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
46 }
47
48 virtual void TearDown() {
49 mBlackBgSurface = 0;
50 mClient->dispose();
51 mClient = 0;
52 }
53
54 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
55 const char* name, uint32_t width, uint32_t height,
56 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
57 auto layer =
58 createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
59
60 Transaction t;
61 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
62
63 status_t error = t.apply();
64 if (error != NO_ERROR) {
65 ADD_FAILURE() << "failed to initialize SurfaceControl";
66 layer.clear();
67 }
68
69 return layer;
70 }
71
72 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
73 const char* name, uint32_t width, uint32_t height,
74 PixelFormat format, uint32_t flags,
75 SurfaceControl* parent = nullptr) {
76 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
77 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
78 return layer;
79 }
80
81 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
82 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
83 return createLayer(mClient, name, width, height, flags, parent);
84 }
85
86 sp<SurfaceControl> createColorLayer(const char* name, const Color& color,
87 SurfaceControl* parent = nullptr) {
88 auto colorLayer = createSurface(mClient, name, 0 /* buffer width */, 0 /* buffer height */,
89 PIXEL_FORMAT_RGBA_8888,
90 ISurfaceComposerClient::eFXSurfaceColor, parent);
91 asTransaction([&](Transaction& t) {
92 t.setColor(colorLayer, half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f});
93 t.setAlpha(colorLayer, color.a / 255.0f);
94 });
95 return colorLayer;
96 }
97
98 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
99 // wait for previous transactions (such as setSize) to complete
100 Transaction().apply(true);
101
102 ANativeWindow_Buffer buffer = {};
103 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
104
105 return buffer;
106 }
107
108 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
109 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
110
111 // wait for the newly posted buffer to be latched
112 waitForLayerBuffers();
113 }
114
115 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
116 int32_t bufferWidth, int32_t bufferHeight) {
117 ANativeWindow_Buffer buffer;
118 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
119 TransactionUtils::fillANativeWindowBufferColor(buffer,
120 Rect(0, 0, bufferWidth, bufferHeight),
121 color);
122 postBufferQueueLayerBuffer(layer);
123 }
124
125 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
126 int32_t bufferWidth, int32_t bufferHeight) {
127 sp<GraphicBuffer> buffer =
128 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
129 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
130 BufferUsage::COMPOSER_OVERLAY,
131 "test");
132 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight),
133 color);
134 Transaction().setBuffer(layer, buffer).apply();
135 }
136
137 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
138 int32_t bufferWidth, int32_t bufferHeight) {
139 switch (mLayerType) {
140 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
141 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
142 break;
143 case ISurfaceComposerClient::eFXSurfaceBufferState:
144 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
145 break;
146 default:
147 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
148 }
149 }
150
151 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
152 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
153 const Color& topRight, const Color& bottomLeft,
154 const Color& bottomRight) {
155 switch (mLayerType) {
156 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
157 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
158 bottomLeft, bottomRight);
159 break;
160 case ISurfaceComposerClient::eFXSurfaceBufferState:
161 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
162 bottomLeft, bottomRight);
163 break;
164 default:
165 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
166 }
167 }
168
169 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
170 int32_t bufferHeight, const Color& topLeft,
171 const Color& topRight, const Color& bottomLeft,
172 const Color& bottomRight) {
173 ANativeWindow_Buffer buffer;
174 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
175 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
176
177 const int32_t halfW = bufferWidth / 2;
178 const int32_t halfH = bufferHeight / 2;
179 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
180 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
181 topRight);
182 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
183 bottomLeft);
184 TransactionUtils::fillANativeWindowBufferColor(buffer,
185 Rect(halfW, halfH, bufferWidth,
186 bufferHeight),
187 bottomRight);
188
189 postBufferQueueLayerBuffer(layer);
190 }
191
192 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
193 int32_t bufferHeight, const Color& topLeft,
194 const Color& topRight, const Color& bottomLeft,
195 const Color& bottomRight) {
196 sp<GraphicBuffer> buffer =
197 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
198 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
199 BufferUsage::COMPOSER_OVERLAY,
200 "test");
201
202 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
203
204 const int32_t halfW = bufferWidth / 2;
205 const int32_t halfH = bufferHeight / 2;
206 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
207 TransactionUtils::fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
208 topRight);
209 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
210 bottomLeft);
211 TransactionUtils::fillGraphicBufferColor(buffer,
212 Rect(halfW, halfH, bufferWidth, bufferHeight),
213 bottomRight);
214
215 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
216 }
217
218 std::unique_ptr<ScreenCapture> screenshot() {
219 std::unique_ptr<ScreenCapture> screenshot;
220 ScreenCapture::captureScreen(&screenshot);
221 return screenshot;
222 }
223
224 void asTransaction(const std::function<void(Transaction&)>& exec) {
225 Transaction t;
226 exec(t);
227 t.apply(true);
228 }
229
230 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
231 static BufferGenerator bufferGenerator;
232 return bufferGenerator.get(outBuffer, outFence);
233 }
234
235 sp<SurfaceComposerClient> mClient;
236
237 sp<IBinder> mDisplay;
238 uint32_t mDisplayWidth;
239 uint32_t mDisplayHeight;
240 uint32_t mDisplayLayerStack;
241 Rect mDisplayRect = Rect::INVALID_RECT;
242
243 // leave room for ~256 layers
244 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
245
246 sp<SurfaceControl> mBlackBgSurface;
247 bool mColorManagementUsed;
248
249private:
250 void SetUpDisplay() {
251 mDisplay = mClient->getInternalDisplayToken();
252 ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
253
254 // get display width/height
255 DisplayInfo info;
256 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info));
257 mDisplayWidth = info.w;
258 mDisplayHeight = info.h;
259 mDisplayRect =
260 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
261
262 // After a new buffer is queued, SurfaceFlinger is notified and will
263 // latch the new buffer on next vsync. Let's heuristically wait for 3
264 // vsyncs.
265 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
266
267 mDisplayLayerStack = 0;
268
269 mBlackBgSurface =
270 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
271 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
272
273 // set layer stack (b/68888219)
274 Transaction t;
275 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
276 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
277 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
278 t.setColor(mBlackBgSurface, half3{0, 0, 0});
279 t.setLayer(mBlackBgSurface, mLayerZBase);
280 t.apply();
281 }
282
283 void waitForLayerBuffers() {
284 // Request an empty transaction to get applied synchronously to ensure the buffer is
285 // latched.
286 Transaction().apply(true);
287 usleep(mBufferPostDelay);
288 }
289
290 int32_t mBufferPostDelay;
291
292 friend class LayerRenderPathTestHarness;
293};
294} // namespace android
295
296#endif