blob: 2bdb8a452d93deae4d86ea4396fd53a9f73be16f [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
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020019// TODO(b/129481165): remove the #pragma below and fix conversion issues
20#pragma clang diagnostic push
21#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010022#pragma clang diagnostic ignored "-Wextra"
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020023
Galia Peycheva561d05c2021-03-08 15:14:05 +010024#include <cutils/properties.h>
Valerie Hau9cfc6d82019-09-23 13:54:07 -070025#include <gtest/gtest.h>
Huihong Luo3bdef862022-03-03 11:57:19 -080026#include <gui/AidlStatusUtil.h>
Valerie Hau9cfc6d82019-09-23 13:54:07 -070027#include <gui/ISurfaceComposer.h>
28#include <gui/SurfaceComposerClient.h>
Valerie Hau9cfc6d82019-09-23 13:54:07 -070029#include <private/gui/ComposerService.h>
Huihong Luo05539a12022-02-23 10:29:40 -080030#include <private/gui/ComposerServiceAIDL.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010031#include <ui/DisplayMode.h>
Valerie Hau9cfc6d82019-09-23 13:54:07 -070032
33#include "BufferGenerator.h"
34#include "utils/ScreenshotUtils.h"
35#include "utils/TransactionUtils.h"
36
37namespace android {
38
39using android::hardware::graphics::common::V1_1::BufferUsage;
40
41class LayerTransactionTest : public ::testing::Test {
42protected:
43 void SetUp() override {
Ady Abrahamd11bade2022-08-01 16:18:03 -070044 mClient = sp<SurfaceComposerClient>::make();
Valerie Hau9cfc6d82019-09-23 13:54:07 -070045 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
46
47 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
48
Huihong Luo05539a12022-02-23 10:29:40 -080049 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
chaviwd2432892020-07-24 17:42:39 -070050 mCaptureArgs.displayToken = mDisplay;
Valerie Hau9cfc6d82019-09-23 13:54:07 -070051 }
52
53 virtual void TearDown() {
54 mBlackBgSurface = 0;
55 mClient->dispose();
56 mClient = 0;
57 }
58
59 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
60 const char* name, uint32_t width, uint32_t height,
Valerie Hau1acd6962019-10-28 16:35:48 -070061 uint32_t flags = 0, SurfaceControl* parent = nullptr,
chaviwdebadb82020-03-26 14:57:24 -070062 uint32_t* outTransformHint = nullptr,
63 PixelFormat format = PIXEL_FORMAT_RGBA_8888) {
64 auto layer =
65 createSurface(client, name, width, height, format, flags, parent, outTransformHint);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070066
67 Transaction t;
68 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
69
70 status_t error = t.apply();
71 if (error != NO_ERROR) {
72 ADD_FAILURE() << "failed to initialize SurfaceControl";
73 layer.clear();
74 }
75
76 return layer;
77 }
78
79 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
80 const char* name, uint32_t width, uint32_t height,
81 PixelFormat format, uint32_t flags,
Valerie Hau1acd6962019-10-28 16:35:48 -070082 SurfaceControl* parent = nullptr,
83 uint32_t* outTransformHint = nullptr) {
Vishnu Nair992496b2020-10-22 17:27:21 -070084 sp<IBinder> parentHandle = (parent) ? parent->getHandle() : nullptr;
85 auto layer = client->createSurface(String8(name), width, height, format, flags,
86 parentHandle, LayerMetadata(), outTransformHint);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070087 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
88 return layer;
89 }
90
91 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Valerie Hau1acd6962019-10-28 16:35:48 -070092 uint32_t flags = 0, SurfaceControl* parent = nullptr,
chaviwdebadb82020-03-26 14:57:24 -070093 uint32_t* outTransformHint = nullptr,
94 PixelFormat format = PIXEL_FORMAT_RGBA_8888) {
95 return createLayer(mClient, name, width, height, flags, parent, outTransformHint, format);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070096 }
97
98 sp<SurfaceControl> createColorLayer(const char* name, const Color& color,
99 SurfaceControl* parent = nullptr) {
100 auto colorLayer = createSurface(mClient, name, 0 /* buffer width */, 0 /* buffer height */,
101 PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800102 ISurfaceComposerClient::eFXSurfaceEffect, parent);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700103 asTransaction([&](Transaction& t) {
104 t.setColor(colorLayer, half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f});
105 t.setAlpha(colorLayer, color.a / 255.0f);
106 });
107 return colorLayer;
108 }
109
110 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
111 // wait for previous transactions (such as setSize) to complete
112 Transaction().apply(true);
113
114 ANativeWindow_Buffer buffer = {};
115 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
116
117 return buffer;
118 }
119
120 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
121 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
122
123 // wait for the newly posted buffer to be latched
124 waitForLayerBuffers();
125 }
126
127 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
Marin Shalamanov46084422020-10-13 12:33:42 +0200128 uint32_t bufferWidth, uint32_t bufferHeight) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700129 ANativeWindow_Buffer buffer;
130 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
131 TransactionUtils::fillANativeWindowBufferColor(buffer,
132 Rect(0, 0, bufferWidth, bufferHeight),
133 color);
134 postBufferQueueLayerBuffer(layer);
135 }
136
Patrick Williams83f36b22022-09-14 17:57:35 +0000137 virtual void fillBufferLayerColor(const sp<SurfaceControl>& layer, const Color& color,
138 int32_t bufferWidth, int32_t bufferHeight) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700139 sp<GraphicBuffer> buffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700140 sp<GraphicBuffer>::make(static_cast<uint32_t>(bufferWidth),
141 static_cast<uint32_t>(bufferHeight), PIXEL_FORMAT_RGBA_8888,
142 1u,
143 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
144 BufferUsage::COMPOSER_OVERLAY |
145 BufferUsage::GPU_TEXTURE,
146 "test");
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700147 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight),
148 color);
149 Transaction().setBuffer(layer, buffer).apply();
150 }
151
152 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
Marin Shalamanov46084422020-10-13 12:33:42 +0200153 uint32_t bufferWidth, uint32_t bufferHeight) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700154 switch (mLayerType) {
155 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
156 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
157 break;
158 case ISurfaceComposerClient::eFXSurfaceBufferState:
Patrick Williams83f36b22022-09-14 17:57:35 +0000159 fillBufferLayerColor(layer, color, bufferWidth, bufferHeight);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700160 break;
161 default:
162 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
163 }
164 }
165
166 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
167 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
168 const Color& topRight, const Color& bottomLeft,
169 const Color& bottomRight) {
170 switch (mLayerType) {
171 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
172 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
173 bottomLeft, bottomRight);
174 break;
175 case ISurfaceComposerClient::eFXSurfaceBufferState:
176 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
177 bottomLeft, bottomRight);
178 break;
179 default:
180 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
181 }
182 }
183
184 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
185 int32_t bufferHeight, const Color& topLeft,
186 const Color& topRight, const Color& bottomLeft,
187 const Color& bottomRight) {
188 ANativeWindow_Buffer buffer;
189 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
190 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
191
192 const int32_t halfW = bufferWidth / 2;
193 const int32_t halfH = bufferHeight / 2;
194 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
195 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
196 topRight);
197 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
198 bottomLeft);
199 TransactionUtils::fillANativeWindowBufferColor(buffer,
200 Rect(halfW, halfH, bufferWidth,
201 bufferHeight),
202 bottomRight);
203
204 postBufferQueueLayerBuffer(layer);
205 }
206
207 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
208 int32_t bufferHeight, const Color& topLeft,
209 const Color& topRight, const Color& bottomLeft,
210 const Color& bottomRight) {
211 sp<GraphicBuffer> buffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700212 sp<GraphicBuffer>::make(static_cast<uint32_t>(bufferWidth),
213 static_cast<uint32_t>(bufferHeight), PIXEL_FORMAT_RGBA_8888,
214 1u,
215 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
216 BufferUsage::COMPOSER_OVERLAY |
217 BufferUsage::GPU_TEXTURE,
218 "test");
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700219
220 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
221
222 const int32_t halfW = bufferWidth / 2;
223 const int32_t halfH = bufferHeight / 2;
224 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
225 TransactionUtils::fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
226 topRight);
227 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
228 bottomLeft);
229 TransactionUtils::fillGraphicBufferColor(buffer,
230 Rect(halfW, halfH, bufferWidth, bufferHeight),
231 bottomRight);
232
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000233 Transaction().setBuffer(layer, buffer).apply();
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700234 }
235
236 std::unique_ptr<ScreenCapture> screenshot() {
237 std::unique_ptr<ScreenCapture> screenshot;
238 ScreenCapture::captureScreen(&screenshot);
239 return screenshot;
240 }
241
242 void asTransaction(const std::function<void(Transaction&)>& exec) {
243 Transaction t;
244 exec(t);
245 t.apply(true);
246 }
247
248 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
249 static BufferGenerator bufferGenerator;
250 return bufferGenerator.get(outBuffer, outFence);
251 }
252
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000253 static ui::Size getBufferSize() {
254 static BufferGenerator bufferGenerator;
255 return bufferGenerator.getSize();
256 }
257
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700258 sp<SurfaceComposerClient> mClient;
259
Galia Peycheva561d05c2021-03-08 15:14:05 +0100260 bool deviceSupportsBlurs() {
261 char value[PROPERTY_VALUE_MAX];
262 property_get("ro.surface_flinger.supports_background_blur", value, "0");
263 return atoi(value);
264 }
265
266 bool deviceUsesSkiaRenderEngine() {
267 char value[PROPERTY_VALUE_MAX];
268 property_get("debug.renderengine.backend", value, "default");
269 return strstr(value, "skia") != nullptr;
270 }
271
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700272 sp<IBinder> mDisplay;
273 uint32_t mDisplayWidth;
274 uint32_t mDisplayHeight;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700275 ui::LayerStack mDisplayLayerStack = ui::DEFAULT_LAYER_STACK;
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700276 Rect mDisplayRect = Rect::INVALID_RECT;
277
278 // leave room for ~256 layers
279 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
280
281 sp<SurfaceControl> mBlackBgSurface;
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700282
chaviwd2432892020-07-24 17:42:39 -0700283 DisplayCaptureArgs mCaptureArgs;
284 ScreenCaptureResults mCaptureResults;
285
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700286private:
287 void SetUpDisplay() {
Huihong Luo31b5ac22022-08-15 20:38:10 -0700288 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
289 ASSERT_FALSE(ids.empty());
290 mDisplay = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700291 ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
292
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100293 ui::DisplayMode mode;
294 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplay, &mode));
295 mDisplayRect = Rect(mode.resolution);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800296 mDisplayWidth = mDisplayRect.getWidth();
297 mDisplayHeight = mDisplayRect.getHeight();
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700298
299 // After a new buffer is queued, SurfaceFlinger is notified and will
300 // latch the new buffer on next vsync. Let's heuristically wait for 3
301 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100302 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700303
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700304 mBlackBgSurface =
305 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800306 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700307
308 // set layer stack (b/68888219)
309 Transaction t;
310 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
chaviw25714502021-02-11 10:01:08 -0800311 t.setCrop(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700312 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
313 t.setColor(mBlackBgSurface, half3{0, 0, 0});
314 t.setLayer(mBlackBgSurface, mLayerZBase);
315 t.apply();
316 }
317
318 void waitForLayerBuffers() {
319 // Request an empty transaction to get applied synchronously to ensure the buffer is
320 // latched.
321 Transaction().apply(true);
322 usleep(mBufferPostDelay);
323 }
324
325 int32_t mBufferPostDelay;
326
327 friend class LayerRenderPathTestHarness;
328};
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700329
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800330} // namespace android
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200331
332// TODO(b/129481165): remove the #pragma below and fix conversion issues
Galia Peycheva561d05c2021-03-08 15:14:05 +0100333#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"