blob: d4e952aa2161eac6a3aa9a1c8ab6f2237c996241 [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) {
78 auto layer = client->createSurface(String8(name), width, height, format, flags, parent,
79 LayerMetadata(), outTransformHint);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070080 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
81 return layer;
82 }
83
84 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Valerie Hau1acd6962019-10-28 16:35:48 -070085 uint32_t flags = 0, SurfaceControl* parent = nullptr,
chaviwdebadb82020-03-26 14:57:24 -070086 uint32_t* outTransformHint = nullptr,
87 PixelFormat format = PIXEL_FORMAT_RGBA_8888) {
88 return createLayer(mClient, name, width, height, flags, parent, outTransformHint, format);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070089 }
90
91 sp<SurfaceControl> createColorLayer(const char* name, const Color& color,
92 SurfaceControl* parent = nullptr) {
93 auto colorLayer = createSurface(mClient, name, 0 /* buffer width */, 0 /* buffer height */,
94 PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -080095 ISurfaceComposerClient::eFXSurfaceEffect, parent);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070096 asTransaction([&](Transaction& t) {
97 t.setColor(colorLayer, half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f});
98 t.setAlpha(colorLayer, color.a / 255.0f);
99 });
100 return colorLayer;
101 }
102
103 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
104 // wait for previous transactions (such as setSize) to complete
105 Transaction().apply(true);
106
107 ANativeWindow_Buffer buffer = {};
108 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
109
110 return buffer;
111 }
112
113 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
114 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
115
116 // wait for the newly posted buffer to be latched
117 waitForLayerBuffers();
118 }
119
120 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
121 int32_t bufferWidth, int32_t bufferHeight) {
122 ANativeWindow_Buffer buffer;
123 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
124 TransactionUtils::fillANativeWindowBufferColor(buffer,
125 Rect(0, 0, bufferWidth, bufferHeight),
126 color);
127 postBufferQueueLayerBuffer(layer);
128 }
129
130 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
131 int32_t bufferWidth, int32_t bufferHeight) {
132 sp<GraphicBuffer> buffer =
133 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
134 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
135 BufferUsage::COMPOSER_OVERLAY,
136 "test");
137 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight),
138 color);
139 Transaction().setBuffer(layer, buffer).apply();
140 }
141
142 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
143 int32_t bufferWidth, int32_t bufferHeight) {
144 switch (mLayerType) {
145 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
146 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
147 break;
148 case ISurfaceComposerClient::eFXSurfaceBufferState:
149 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
150 break;
151 default:
152 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
153 }
154 }
155
156 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
157 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
158 const Color& topRight, const Color& bottomLeft,
159 const Color& bottomRight) {
160 switch (mLayerType) {
161 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
162 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
163 bottomLeft, bottomRight);
164 break;
165 case ISurfaceComposerClient::eFXSurfaceBufferState:
166 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
167 bottomLeft, bottomRight);
168 break;
169 default:
170 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
171 }
172 }
173
174 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
175 int32_t bufferHeight, const Color& topLeft,
176 const Color& topRight, const Color& bottomLeft,
177 const Color& bottomRight) {
178 ANativeWindow_Buffer buffer;
179 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
180 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
181
182 const int32_t halfW = bufferWidth / 2;
183 const int32_t halfH = bufferHeight / 2;
184 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
185 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
186 topRight);
187 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
188 bottomLeft);
189 TransactionUtils::fillANativeWindowBufferColor(buffer,
190 Rect(halfW, halfH, bufferWidth,
191 bufferHeight),
192 bottomRight);
193
194 postBufferQueueLayerBuffer(layer);
195 }
196
197 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
198 int32_t bufferHeight, const Color& topLeft,
199 const Color& topRight, const Color& bottomLeft,
200 const Color& bottomRight) {
201 sp<GraphicBuffer> buffer =
202 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
203 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
204 BufferUsage::COMPOSER_OVERLAY,
205 "test");
206
207 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
208
209 const int32_t halfW = bufferWidth / 2;
210 const int32_t halfH = bufferHeight / 2;
211 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
212 TransactionUtils::fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
213 topRight);
214 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
215 bottomLeft);
216 TransactionUtils::fillGraphicBufferColor(buffer,
217 Rect(halfW, halfH, bufferWidth, bufferHeight),
218 bottomRight);
219
220 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
221 }
222
223 std::unique_ptr<ScreenCapture> screenshot() {
224 std::unique_ptr<ScreenCapture> screenshot;
225 ScreenCapture::captureScreen(&screenshot);
226 return screenshot;
227 }
228
229 void asTransaction(const std::function<void(Transaction&)>& exec) {
230 Transaction t;
231 exec(t);
232 t.apply(true);
233 }
234
235 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
236 static BufferGenerator bufferGenerator;
237 return bufferGenerator.get(outBuffer, outFence);
238 }
239
240 sp<SurfaceComposerClient> mClient;
241
242 sp<IBinder> mDisplay;
243 uint32_t mDisplayWidth;
244 uint32_t mDisplayHeight;
245 uint32_t mDisplayLayerStack;
246 Rect mDisplayRect = Rect::INVALID_RECT;
247
248 // leave room for ~256 layers
249 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
250
251 sp<SurfaceControl> mBlackBgSurface;
252 bool mColorManagementUsed;
253
chaviwd2432892020-07-24 17:42:39 -0700254 DisplayCaptureArgs mCaptureArgs;
255 ScreenCaptureResults mCaptureResults;
256
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700257private:
258 void SetUpDisplay() {
259 mDisplay = mClient->getInternalDisplayToken();
260 ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
261
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800262 DisplayConfig config;
263 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(mDisplay, &config));
264 mDisplayRect = Rect(config.resolution);
265 mDisplayWidth = mDisplayRect.getWidth();
266 mDisplayHeight = mDisplayRect.getHeight();
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700267
268 // After a new buffer is queued, SurfaceFlinger is notified and will
269 // latch the new buffer on next vsync. Let's heuristically wait for 3
270 // vsyncs.
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800271 mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3;
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700272
273 mDisplayLayerStack = 0;
274
275 mBlackBgSurface =
276 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800277 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700278
279 // set layer stack (b/68888219)
280 Transaction t;
281 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
282 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
283 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
284 t.setColor(mBlackBgSurface, half3{0, 0, 0});
285 t.setLayer(mBlackBgSurface, mLayerZBase);
286 t.apply();
287 }
288
289 void waitForLayerBuffers() {
290 // Request an empty transaction to get applied synchronously to ensure the buffer is
291 // latched.
292 Transaction().apply(true);
293 usleep(mBufferPostDelay);
294 }
295
296 int32_t mBufferPostDelay;
297
298 friend class LayerRenderPathTestHarness;
299};
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700300
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800301} // namespace android