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