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