Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <functional> |
| 19 | #include <limits> |
| 20 | #include <ostream> |
| 21 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 24 | #include <android/native_window.h> |
| 25 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 26 | #include <gui/ISurfaceComposer.h> |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 27 | #include <gui/LayerState.h> |
| 28 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 29 | #include <gui/Surface.h> |
| 30 | #include <gui/SurfaceComposerClient.h> |
| 31 | #include <private/gui/ComposerService.h> |
| 32 | |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 33 | #include <ui/DisplayInfo.h> |
Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 34 | #include <ui/Rect.h> |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 35 | #include <utils/String8.h> |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 36 | |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 37 | #include <math.h> |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 38 | #include <math/vec3.h> |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 39 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | struct Color { |
| 45 | uint8_t r; |
| 46 | uint8_t g; |
| 47 | uint8_t b; |
| 48 | uint8_t a; |
| 49 | |
| 50 | static const Color RED; |
| 51 | static const Color BLACK; |
| 52 | }; |
| 53 | |
| 54 | const Color Color::RED{255, 0, 0, 255}; |
| 55 | const Color Color::BLACK{0, 0, 0, 255}; |
| 56 | |
| 57 | std::ostream& operator<<(std::ostream& os, const Color& color) { |
| 58 | os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a); |
| 59 | return os; |
| 60 | } |
| 61 | |
| 62 | // Fill a region with the specified color. |
| 63 | void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) { |
| 64 | int32_t x = rect.left; |
| 65 | int32_t y = rect.top; |
| 66 | int32_t width = rect.right - rect.left; |
| 67 | int32_t height = rect.bottom - rect.top; |
| 68 | |
| 69 | if (x < 0) { |
| 70 | width += x; |
| 71 | x = 0; |
| 72 | } |
| 73 | if (y < 0) { |
| 74 | height += y; |
| 75 | y = 0; |
| 76 | } |
| 77 | if (x + width > buffer.width) { |
| 78 | x = std::min(x, buffer.width); |
| 79 | width = buffer.width - x; |
| 80 | } |
| 81 | if (y + height > buffer.height) { |
| 82 | y = std::min(y, buffer.height); |
| 83 | height = buffer.height - y; |
| 84 | } |
| 85 | |
| 86 | for (int32_t j = 0; j < height; j++) { |
| 87 | uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4; |
| 88 | for (int32_t i = 0; i < width; i++) { |
| 89 | dst[0] = color.r; |
| 90 | dst[1] = color.g; |
| 91 | dst[2] = color.b; |
| 92 | dst[3] = color.a; |
| 93 | dst += 4; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Check if a region has the specified color. |
| 99 | void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect, |
| 100 | const Color& color, uint8_t tolerance) { |
| 101 | int32_t x = rect.left; |
| 102 | int32_t y = rect.top; |
| 103 | int32_t width = rect.right - rect.left; |
| 104 | int32_t height = rect.bottom - rect.top; |
| 105 | |
| 106 | if (x + width > int32_t(buffer.width)) { |
| 107 | x = std::min(x, int32_t(buffer.width)); |
| 108 | width = buffer.width - x; |
| 109 | } |
| 110 | if (y + height > int32_t(buffer.height)) { |
| 111 | y = std::min(y, int32_t(buffer.height)); |
| 112 | height = buffer.height - y; |
| 113 | } |
| 114 | |
| 115 | auto colorCompare = [tolerance](uint8_t a, uint8_t b) { |
| 116 | uint8_t tmp = a >= b ? a - b : b - a; |
| 117 | return tmp <= tolerance; |
| 118 | }; |
| 119 | for (int32_t j = 0; j < height; j++) { |
| 120 | const uint8_t* src = |
| 121 | static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4; |
| 122 | for (int32_t i = 0; i < width; i++) { |
| 123 | const uint8_t expected[4] = {color.r, color.g, color.b, color.a}; |
| 124 | EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare)) |
| 125 | << "pixel @ (" << x + i << ", " << y + j << "): " |
| 126 | << "expected (" << color << "), " |
| 127 | << "got (" << Color{src[0], src[1], src[2], src[3]} << ")"; |
| 128 | src += 4; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } // anonymous namespace |
| 134 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 135 | using Transaction = SurfaceComposerClient::Transaction; |
| 136 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 137 | // Fill an RGBA_8888 formatted surface with a single color. |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 138 | static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b, |
| 139 | bool unlock = true) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 140 | ANativeWindow_Buffer outBuffer; |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 141 | sp<Surface> s = sc->getSurface(); |
| 142 | ASSERT_TRUE(s != NULL); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 143 | ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL)); |
| 144 | uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 145 | for (int y = 0; y < outBuffer.height; y++) { |
| 146 | for (int x = 0; x < outBuffer.width; x++) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 147 | uint8_t* pixel = img + (4 * (y * outBuffer.stride + x)); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 148 | pixel[0] = r; |
| 149 | pixel[1] = g; |
| 150 | pixel[2] = b; |
| 151 | pixel[3] = 255; |
| 152 | } |
| 153 | } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 154 | if (unlock) { |
| 155 | ASSERT_EQ(NO_ERROR, s->unlockAndPost()); |
| 156 | } |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check |
| 160 | // individual pixel values for testing purposes. |
| 161 | class ScreenCapture : public RefBase { |
| 162 | public: |
Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 163 | static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0, |
| 164 | int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) { |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 165 | sp<IGraphicBufferProducer> producer; |
| 166 | sp<IGraphicBufferConsumer> consumer; |
| 167 | BufferQueue::createBufferQueue(&producer, &consumer); |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 168 | sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 169 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 170 | sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 171 | SurfaceComposerClient::Transaction().apply(true); |
| 172 | |
Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 173 | ASSERT_EQ(NO_ERROR, |
| 174 | sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false)); |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 175 | *sc = new ScreenCapture(cpuConsumer); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 178 | void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) { |
| 179 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format); |
| 180 | expectBufferColor(mBuf, rect, color, tolerance); |
| 181 | } |
| 182 | |
| 183 | void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) { |
| 184 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format); |
| 185 | const bool leftBorder = rect.left > 0; |
| 186 | const bool topBorder = rect.top > 0; |
| 187 | const bool rightBorder = rect.right < int32_t(mBuf.width); |
| 188 | const bool bottomBorder = rect.bottom < int32_t(mBuf.height); |
| 189 | |
| 190 | if (topBorder) { |
| 191 | Rect top(rect.left, rect.top - 1, rect.right, rect.top); |
| 192 | if (leftBorder) { |
| 193 | top.left -= 1; |
| 194 | } |
| 195 | if (rightBorder) { |
| 196 | top.right += 1; |
| 197 | } |
| 198 | expectColor(top, color, tolerance); |
| 199 | } |
| 200 | if (leftBorder) { |
| 201 | Rect left(rect.left - 1, rect.top, rect.left, rect.bottom); |
| 202 | expectColor(left, color, tolerance); |
| 203 | } |
| 204 | if (rightBorder) { |
| 205 | Rect right(rect.right, rect.top, rect.right + 1, rect.bottom); |
| 206 | expectColor(right, color, tolerance); |
| 207 | } |
| 208 | if (bottomBorder) { |
| 209 | Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1); |
| 210 | if (leftBorder) { |
| 211 | bottom.left -= 1; |
| 212 | } |
| 213 | if (rightBorder) { |
| 214 | bottom.right += 1; |
| 215 | } |
| 216 | expectColor(bottom, color, tolerance); |
| 217 | } |
| 218 | } |
| 219 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 220 | void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 221 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format); |
| 222 | const uint8_t* img = static_cast<const uint8_t*>(mBuf.data); |
| 223 | const uint8_t* pixel = img + (4 * (y * mBuf.stride + x)); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 224 | if (r != pixel[0] || g != pixel[1] || b != pixel[2]) { |
| 225 | String8 err(String8::format("pixel @ (%3d, %3d): " |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 226 | "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]", |
| 227 | x, y, r, g, b, pixel[0], pixel[1], pixel[2])); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 228 | EXPECT_EQ(String8(), err) << err.string(); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 232 | void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 233 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 234 | void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 235 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 236 | void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 237 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 238 | private: |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 239 | ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) { |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 240 | EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf)); |
| 241 | } |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 242 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 243 | ~ScreenCapture() { mCC->unlockBuffer(mBuf); } |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 244 | |
| 245 | sp<CpuConsumer> mCC; |
| 246 | CpuConsumer::LockedBuffer mBuf; |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 249 | class CaptureLayer { |
| 250 | public: |
| 251 | static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) { |
| 252 | sp<IGraphicBufferProducer> producer; |
| 253 | sp<IGraphicBufferConsumer> consumer; |
| 254 | BufferQueue::createBufferQueue(&producer, &consumer); |
| 255 | sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1); |
| 256 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 257 | sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 258 | SurfaceComposerClient::Transaction().apply(true); |
| 259 | ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer)); |
| 260 | *sc = std::make_unique<CaptureLayer>(cpuConsumer); |
| 261 | } |
| 262 | |
| 263 | void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { |
| 264 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format); |
| 265 | const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data); |
| 266 | const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x)); |
| 267 | if (r != pixel[0] || g != pixel[1] || b != pixel[2]) { |
| 268 | String8 err(String8::format("pixel @ (%3d, %3d): " |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 269 | "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]", |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 270 | x, y, r, g, b, pixel[0], pixel[1], pixel[2])); |
| 271 | EXPECT_EQ(String8(), err) << err.string(); |
| 272 | } |
| 273 | } |
| 274 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 275 | void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 276 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 277 | void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 278 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 279 | void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 280 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 281 | CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 282 | EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer)); |
| 283 | } |
| 284 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 285 | ~CaptureLayer() { mCC->unlockBuffer(mBuffer); } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 286 | |
| 287 | private: |
| 288 | sp<CpuConsumer> mCC; |
| 289 | CpuConsumer::LockedBuffer mBuffer; |
| 290 | }; |
| 291 | |
Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 292 | class LayerTransactionTest : public ::testing::Test { |
| 293 | protected: |
| 294 | void SetUp() override { |
| 295 | mClient = new SurfaceComposerClient; |
| 296 | ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient"; |
| 297 | |
| 298 | ASSERT_NO_FATAL_FAILURE(SetUpDisplay()); |
| 299 | } |
| 300 | |
| 301 | sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height, |
| 302 | uint32_t flags = 0) { |
| 303 | auto layer = |
| 304 | mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags); |
| 305 | EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl"; |
| 306 | |
| 307 | status_t error = Transaction() |
| 308 | .setLayerStack(layer, mDisplayLayerStack) |
| 309 | .setLayer(layer, mLayerZBase) |
| 310 | .apply(); |
| 311 | if (error != NO_ERROR) { |
| 312 | ADD_FAILURE() << "failed to initialize SurfaceControl"; |
| 313 | layer.clear(); |
| 314 | } |
| 315 | |
| 316 | return layer; |
| 317 | } |
| 318 | |
| 319 | ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) { |
| 320 | // wait for previous transactions (such as setSize) to complete |
| 321 | Transaction().apply(true); |
| 322 | |
| 323 | ANativeWindow_Buffer buffer = {}; |
| 324 | EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr)); |
| 325 | |
| 326 | return buffer; |
| 327 | } |
| 328 | |
| 329 | void postLayerBuffer(const sp<SurfaceControl>& layer) { |
| 330 | ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost()); |
| 331 | |
| 332 | // wait for the newly posted buffer to be latched |
| 333 | waitForLayerBuffers(); |
| 334 | } |
| 335 | |
| 336 | void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) { |
| 337 | ANativeWindow_Buffer buffer; |
| 338 | ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer)); |
| 339 | fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color); |
| 340 | postLayerBuffer(layer); |
| 341 | } |
| 342 | |
| 343 | sp<ScreenCapture> screenshot() { |
| 344 | sp<ScreenCapture> screenshot; |
| 345 | ScreenCapture::captureScreen(&screenshot, mLayerZBase); |
| 346 | return screenshot; |
| 347 | } |
| 348 | |
| 349 | sp<SurfaceComposerClient> mClient; |
| 350 | |
| 351 | sp<IBinder> mDisplay; |
| 352 | uint32_t mDisplayWidth; |
| 353 | uint32_t mDisplayHeight; |
| 354 | uint32_t mDisplayLayerStack; |
| 355 | |
| 356 | // leave room for ~256 layers |
| 357 | const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256; |
| 358 | |
| 359 | private: |
| 360 | void SetUpDisplay() { |
| 361 | mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain); |
| 362 | ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display"; |
| 363 | |
| 364 | // get display width/height |
| 365 | DisplayInfo info; |
| 366 | SurfaceComposerClient::getDisplayInfo(mDisplay, &info); |
| 367 | mDisplayWidth = info.w; |
| 368 | mDisplayHeight = info.h; |
| 369 | |
| 370 | // After a new buffer is queued, SurfaceFlinger is notified and will |
| 371 | // latch the new buffer on next vsync. Let's heuristically wait for 3 |
| 372 | // vsyncs. |
| 373 | mBufferPostDelay = int32_t(1e6 / info.fps) * 3; |
| 374 | |
| 375 | mDisplayLayerStack = 0; |
| 376 | // set layer stack (b/68888219) |
| 377 | Transaction t; |
| 378 | t.setDisplayLayerStack(mDisplay, mDisplayLayerStack); |
| 379 | t.apply(); |
| 380 | } |
| 381 | |
| 382 | void waitForLayerBuffers() { usleep(mBufferPostDelay); } |
| 383 | |
| 384 | int32_t mBufferPostDelay; |
| 385 | }; |
| 386 | |
| 387 | TEST_F(LayerTransactionTest, SetPositionBasic) { |
| 388 | sp<SurfaceControl> layer; |
| 389 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 390 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 391 | |
| 392 | { |
| 393 | SCOPED_TRACE("default position"); |
| 394 | auto shot = screenshot(); |
| 395 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 396 | shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); |
| 397 | } |
| 398 | |
| 399 | Transaction().setPosition(layer, 5, 10).apply(); |
| 400 | { |
| 401 | SCOPED_TRACE("new position"); |
| 402 | auto shot = screenshot(); |
| 403 | shot->expectColor(Rect(5, 10, 37, 42), Color::RED); |
| 404 | shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | TEST_F(LayerTransactionTest, SetPositionRounding) { |
| 409 | sp<SurfaceControl> layer; |
| 410 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 411 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 412 | |
| 413 | // GLES requires only 4 bits of subpixel precision during rasterization |
| 414 | // XXX GLES composition does not match HWC composition due to precision |
| 415 | // loss (b/69315223) |
| 416 | const float epsilon = 1.0f / 16.0f; |
| 417 | Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply(); |
| 418 | { |
| 419 | SCOPED_TRACE("rounding down"); |
| 420 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 421 | } |
| 422 | |
| 423 | Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply(); |
| 424 | { |
| 425 | SCOPED_TRACE("rounding up"); |
| 426 | screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | TEST_F(LayerTransactionTest, SetPositionOutOfBounds) { |
| 431 | sp<SurfaceControl> layer; |
| 432 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 433 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 434 | |
| 435 | Transaction().setPosition(layer, -32, -32).apply(); |
| 436 | { |
| 437 | SCOPED_TRACE("negative coordinates"); |
| 438 | screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK); |
| 439 | } |
| 440 | |
| 441 | Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply(); |
| 442 | { |
| 443 | SCOPED_TRACE("positive coordinates"); |
| 444 | screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) { |
| 449 | sp<SurfaceControl> layer; |
| 450 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 451 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 452 | |
| 453 | // partially out of bounds |
| 454 | Transaction().setPosition(layer, -30, -30).apply(); |
| 455 | { |
| 456 | SCOPED_TRACE("negative coordinates"); |
| 457 | screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED); |
| 458 | } |
| 459 | |
| 460 | Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply(); |
| 461 | { |
| 462 | SCOPED_TRACE("positive coordinates"); |
| 463 | screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth, |
| 464 | mDisplayHeight), |
| 465 | Color::RED); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | TEST_F(LayerTransactionTest, SetPositionWithResize) { |
| 470 | sp<SurfaceControl> layer; |
| 471 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 472 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 473 | |
| 474 | // setPosition is applied immediately by default, with or without resize |
| 475 | // pending |
| 476 | Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply(); |
| 477 | { |
| 478 | SCOPED_TRACE("resize pending"); |
| 479 | auto shot = screenshot(); |
| 480 | shot->expectColor(Rect(5, 10, 37, 42), Color::RED); |
| 481 | shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK); |
| 482 | } |
| 483 | |
| 484 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 485 | { |
| 486 | SCOPED_TRACE("resize applied"); |
| 487 | screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | TEST_F(LayerTransactionTest, SetPositionWithNextResize) { |
| 492 | sp<SurfaceControl> layer; |
| 493 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 494 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 495 | |
| 496 | // request setPosition to be applied with the next resize |
| 497 | Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply(); |
| 498 | { |
| 499 | SCOPED_TRACE("new position pending"); |
| 500 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 501 | } |
| 502 | |
| 503 | Transaction().setPosition(layer, 15, 20).apply(); |
| 504 | { |
| 505 | SCOPED_TRACE("pending new position modified"); |
| 506 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 507 | } |
| 508 | |
| 509 | Transaction().setSize(layer, 64, 64).apply(); |
| 510 | { |
| 511 | SCOPED_TRACE("resize pending"); |
| 512 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 513 | } |
| 514 | |
| 515 | // finally resize and latch the buffer |
| 516 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 517 | { |
| 518 | SCOPED_TRACE("new position applied"); |
| 519 | screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) { |
| 524 | sp<SurfaceControl> layer; |
| 525 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 526 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 527 | |
| 528 | // setPosition is not immediate even with SCALE_TO_WINDOW override |
| 529 | Transaction() |
| 530 | .setPosition(layer, 5, 10) |
| 531 | .setSize(layer, 64, 64) |
| 532 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) |
| 533 | .setGeometryAppliesWithResize(layer) |
| 534 | .apply(); |
| 535 | { |
| 536 | SCOPED_TRACE("new position pending"); |
| 537 | screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED); |
| 538 | } |
| 539 | |
| 540 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 541 | { |
| 542 | SCOPED_TRACE("new position applied"); |
| 543 | screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED); |
| 544 | } |
| 545 | } |
| 546 | |
Chia-I Wu | 0eaea31 | 2017-10-31 10:14:40 -0700 | [diff] [blame^] | 547 | TEST_F(LayerTransactionTest, SetSizeBasic) { |
| 548 | sp<SurfaceControl> layer; |
| 549 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 550 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 551 | |
| 552 | Transaction().setSize(layer, 64, 64).apply(); |
| 553 | { |
| 554 | SCOPED_TRACE("resize pending"); |
| 555 | auto shot = screenshot(); |
| 556 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 557 | shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); |
| 558 | } |
| 559 | |
| 560 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 561 | { |
| 562 | SCOPED_TRACE("resize applied"); |
| 563 | auto shot = screenshot(); |
| 564 | shot->expectColor(Rect(0, 0, 64, 64), Color::RED); |
| 565 | shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | TEST_F(LayerTransactionTest, SetSizeInvalid) { |
| 570 | // cannot test robustness against invalid sizes (zero or really huge) |
| 571 | } |
| 572 | |
| 573 | TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) { |
| 574 | sp<SurfaceControl> layer; |
| 575 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 576 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); |
| 577 | |
| 578 | // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition |
| 579 | Transaction() |
| 580 | .setSize(layer, 64, 64) |
| 581 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) |
| 582 | .apply(); |
| 583 | screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED); |
| 584 | } |
| 585 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 586 | class LayerUpdateTest : public ::testing::Test { |
| 587 | protected: |
| 588 | virtual void SetUp() { |
| 589 | mComposerClient = new SurfaceComposerClient; |
| 590 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
| 591 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 592 | sp<IBinder> display( |
| 593 | SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 594 | DisplayInfo info; |
Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 595 | SurfaceComposerClient::getDisplayInfo(display, &info); |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 596 | |
| 597 | ssize_t displayWidth = info.w; |
| 598 | ssize_t displayHeight = info.h; |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 599 | |
| 600 | // Background surface |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 601 | mBGSurfaceControl = |
| 602 | mComposerClient->createSurface(String8("BG Test Surface"), displayWidth, |
| 603 | displayHeight, PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 604 | ASSERT_TRUE(mBGSurfaceControl != NULL); |
| 605 | ASSERT_TRUE(mBGSurfaceControl->isValid()); |
| 606 | fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195); |
| 607 | |
| 608 | // Foreground surface |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 609 | mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64, |
| 610 | PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 611 | ASSERT_TRUE(mFGSurfaceControl != NULL); |
| 612 | ASSERT_TRUE(mFGSurfaceControl->isValid()); |
| 613 | |
| 614 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); |
| 615 | |
| 616 | // Synchronization surface |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 617 | mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1, |
| 618 | PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 619 | ASSERT_TRUE(mSyncSurfaceControl != NULL); |
| 620 | ASSERT_TRUE(mSyncSurfaceControl->isValid()); |
| 621 | |
| 622 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 623 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 624 | asTransaction([&](Transaction& t) { |
| 625 | t.setDisplayLayerStack(display, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 626 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 627 | t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 628 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 629 | t.setLayer(mFGSurfaceControl, INT32_MAX - 1) |
| 630 | .setPosition(mFGSurfaceControl, 64, 64) |
| 631 | .show(mFGSurfaceControl); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 632 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 633 | t.setLayer(mSyncSurfaceControl, INT32_MAX - 1) |
| 634 | .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2) |
| 635 | .show(mSyncSurfaceControl); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 636 | }); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | virtual void TearDown() { |
| 640 | mComposerClient->dispose(); |
| 641 | mBGSurfaceControl = 0; |
| 642 | mFGSurfaceControl = 0; |
| 643 | mSyncSurfaceControl = 0; |
| 644 | mComposerClient = 0; |
| 645 | } |
| 646 | |
| 647 | void waitForPostedBuffers() { |
| 648 | // Since the sync surface is in synchronous mode (i.e. double buffered) |
| 649 | // posting three buffers to it should ensure that at least two |
| 650 | // SurfaceFlinger::handlePageFlip calls have been made, which should |
| 651 | // guaranteed that a buffer posted to another Surface has been retired. |
| 652 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 653 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 654 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 655 | } |
| 656 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 657 | void asTransaction(const std::function<void(Transaction&)>& exec) { |
| 658 | Transaction t; |
| 659 | exec(t); |
| 660 | t.apply(true); |
| 661 | } |
| 662 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 663 | sp<SurfaceComposerClient> mComposerClient; |
| 664 | sp<SurfaceControl> mBGSurfaceControl; |
| 665 | sp<SurfaceControl> mFGSurfaceControl; |
| 666 | |
| 667 | // This surface is used to ensure that the buffers posted to |
| 668 | // mFGSurfaceControl have been picked up by SurfaceFlinger. |
| 669 | sp<SurfaceControl> mSyncSurfaceControl; |
| 670 | }; |
| 671 | |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 672 | TEST_F(LayerUpdateTest, RelativesAreNotDetached) { |
| 673 | sp<ScreenCapture> sc; |
| 674 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 675 | sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10, |
| 676 | 10, PIXEL_FORMAT_RGBA_8888, 0); |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 677 | fillSurfaceRGBA8(relative, 10, 10, 10); |
| 678 | waitForPostedBuffers(); |
| 679 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 680 | Transaction{} |
| 681 | .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1) |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 682 | .setPosition(relative, 64, 64) |
| 683 | .apply(); |
| 684 | |
| 685 | { |
| 686 | // The relative should be on top of the FG control. |
| 687 | ScreenCapture::captureScreen(&sc); |
| 688 | sc->checkPixel(64, 64, 10, 10, 10); |
| 689 | } |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 690 | Transaction{}.detachChildren(mFGSurfaceControl).apply(); |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 691 | |
| 692 | { |
| 693 | // Nothing should change at this point. |
| 694 | ScreenCapture::captureScreen(&sc); |
| 695 | sc->checkPixel(64, 64, 10, 10, 10); |
| 696 | } |
| 697 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 698 | Transaction{}.hide(relative).apply(); |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 699 | |
| 700 | { |
| 701 | // Ensure that the relative was actually hidden, rather than |
| 702 | // being left in the detached but visible state. |
| 703 | ScreenCapture::captureScreen(&sc); |
| 704 | sc->expectFGColor(64, 64); |
| 705 | } |
| 706 | } |
| 707 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 708 | TEST_F(LayerUpdateTest, LayerMoveWorks) { |
| 709 | sp<ScreenCapture> sc; |
| 710 | { |
| 711 | SCOPED_TRACE("before move"); |
| 712 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 713 | sc->expectBGColor(0, 12); |
| 714 | sc->expectFGColor(75, 75); |
| 715 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 718 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 719 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 720 | { |
| 721 | // This should reflect the new position, but not the new color. |
| 722 | SCOPED_TRACE("after move, before redraw"); |
| 723 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 724 | sc->expectBGColor(24, 24); |
| 725 | sc->expectBGColor(75, 75); |
| 726 | sc->expectFGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63); |
| 730 | waitForPostedBuffers(); |
| 731 | { |
| 732 | // This should reflect the new position and the new color. |
| 733 | SCOPED_TRACE("after redraw"); |
| 734 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 735 | sc->expectBGColor(24, 24); |
| 736 | sc->expectBGColor(75, 75); |
| 737 | sc->checkPixel(145, 145, 63, 195, 63); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | TEST_F(LayerUpdateTest, LayerResizeWorks) { |
| 742 | sp<ScreenCapture> sc; |
| 743 | { |
| 744 | SCOPED_TRACE("before resize"); |
| 745 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 746 | sc->expectBGColor(0, 12); |
| 747 | sc->expectFGColor(75, 75); |
| 748 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 751 | ALOGD("resizing"); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 752 | asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); }); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 753 | ALOGD("resized"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 754 | { |
| 755 | // This should not reflect the new size or color because SurfaceFlinger |
| 756 | // has not yet received a buffer of the correct size. |
| 757 | SCOPED_TRACE("after resize, before redraw"); |
| 758 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 759 | sc->expectBGColor(0, 12); |
| 760 | sc->expectFGColor(75, 75); |
| 761 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 764 | ALOGD("drawing"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 765 | fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63); |
| 766 | waitForPostedBuffers(); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 767 | ALOGD("drawn"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 768 | { |
| 769 | // This should reflect the new size and the new color. |
| 770 | SCOPED_TRACE("after redraw"); |
| 771 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 772 | sc->expectBGColor(24, 24); |
| 773 | sc->checkPixel(75, 75, 63, 195, 63); |
| 774 | sc->checkPixel(145, 145, 63, 195, 63); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 778 | TEST_F(LayerUpdateTest, LayerCropWorks) { |
| 779 | sp<ScreenCapture> sc; |
| 780 | { |
| 781 | SCOPED_TRACE("before crop"); |
| 782 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 783 | sc->expectBGColor(24, 24); |
| 784 | sc->expectFGColor(75, 75); |
| 785 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 788 | asTransaction([&](Transaction& t) { |
| 789 | Rect cropRect(16, 16, 32, 32); |
| 790 | t.setCrop(mFGSurfaceControl, cropRect); |
| 791 | }); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 792 | { |
| 793 | // This should crop the foreground surface. |
| 794 | SCOPED_TRACE("after crop"); |
| 795 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 796 | sc->expectBGColor(24, 24); |
| 797 | sc->expectBGColor(75, 75); |
| 798 | sc->expectFGColor(95, 80); |
| 799 | sc->expectFGColor(80, 95); |
| 800 | sc->expectBGColor(96, 96); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 804 | TEST_F(LayerUpdateTest, LayerFinalCropWorks) { |
| 805 | sp<ScreenCapture> sc; |
| 806 | { |
| 807 | SCOPED_TRACE("before crop"); |
| 808 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 809 | sc->expectBGColor(24, 24); |
| 810 | sc->expectFGColor(75, 75); |
| 811 | sc->expectBGColor(145, 145); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 812 | } |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 813 | asTransaction([&](Transaction& t) { |
| 814 | Rect cropRect(16, 16, 32, 32); |
| 815 | t.setFinalCrop(mFGSurfaceControl, cropRect); |
| 816 | }); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 817 | { |
| 818 | // This should crop the foreground surface. |
| 819 | SCOPED_TRACE("after crop"); |
| 820 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 821 | sc->expectBGColor(24, 24); |
| 822 | sc->expectBGColor(75, 75); |
| 823 | sc->expectBGColor(95, 80); |
| 824 | sc->expectBGColor(80, 95); |
| 825 | sc->expectBGColor(96, 96); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 829 | TEST_F(LayerUpdateTest, LayerSetLayerWorks) { |
| 830 | sp<ScreenCapture> sc; |
| 831 | { |
| 832 | SCOPED_TRACE("before setLayer"); |
| 833 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 834 | sc->expectBGColor(24, 24); |
| 835 | sc->expectFGColor(75, 75); |
| 836 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 839 | asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 840 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 841 | { |
| 842 | // This should hide the foreground surface beneath the background. |
| 843 | SCOPED_TRACE("after setLayer"); |
| 844 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 845 | sc->expectBGColor(24, 24); |
| 846 | sc->expectBGColor(75, 75); |
| 847 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
| 851 | TEST_F(LayerUpdateTest, LayerShowHideWorks) { |
| 852 | sp<ScreenCapture> sc; |
| 853 | { |
| 854 | SCOPED_TRACE("before hide"); |
| 855 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 856 | sc->expectBGColor(24, 24); |
| 857 | sc->expectFGColor(75, 75); |
| 858 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 861 | asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 862 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 863 | { |
| 864 | // This should hide the foreground surface. |
| 865 | SCOPED_TRACE("after hide, before show"); |
| 866 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 867 | sc->expectBGColor(24, 24); |
| 868 | sc->expectBGColor(75, 75); |
| 869 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 872 | asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 873 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 874 | { |
| 875 | // This should show the foreground surface. |
| 876 | SCOPED_TRACE("after show"); |
| 877 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 878 | sc->expectBGColor(24, 24); |
| 879 | sc->expectFGColor(75, 75); |
| 880 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
| 884 | TEST_F(LayerUpdateTest, LayerSetAlphaWorks) { |
| 885 | sp<ScreenCapture> sc; |
| 886 | { |
| 887 | SCOPED_TRACE("before setAlpha"); |
| 888 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 889 | sc->expectBGColor(24, 24); |
| 890 | sc->expectFGColor(75, 75); |
| 891 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 894 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 895 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 896 | { |
| 897 | // This should set foreground to be 75% opaque. |
| 898 | SCOPED_TRACE("after setAlpha"); |
| 899 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 900 | sc->expectBGColor(24, 24); |
| 901 | sc->checkPixel(75, 75, 162, 63, 96); |
| 902 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 906 | TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) { |
| 907 | sp<ScreenCapture> sc; |
| 908 | { |
| 909 | SCOPED_TRACE("before setLayerStack"); |
| 910 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 911 | sc->expectBGColor(24, 24); |
| 912 | sc->expectFGColor(75, 75); |
| 913 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 916 | asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); }); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 917 | { |
| 918 | // This should hide the foreground surface since it goes to a different |
| 919 | // layer stack. |
| 920 | SCOPED_TRACE("after setLayerStack"); |
| 921 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 922 | sc->expectBGColor(24, 24); |
| 923 | sc->expectBGColor(75, 75); |
| 924 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | TEST_F(LayerUpdateTest, LayerSetFlagsWorks) { |
| 929 | sp<ScreenCapture> sc; |
| 930 | { |
| 931 | SCOPED_TRACE("before setFlags"); |
| 932 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 933 | sc->expectBGColor(24, 24); |
| 934 | sc->expectFGColor(75, 75); |
| 935 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 938 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 939 | t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 940 | }); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 941 | { |
| 942 | // This should hide the foreground surface |
| 943 | SCOPED_TRACE("after setFlags"); |
| 944 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 945 | sc->expectBGColor(24, 24); |
| 946 | sc->expectBGColor(75, 75); |
| 947 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
| 951 | TEST_F(LayerUpdateTest, LayerSetMatrixWorks) { |
| 952 | sp<ScreenCapture> sc; |
| 953 | { |
| 954 | SCOPED_TRACE("before setMatrix"); |
| 955 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 956 | sc->expectBGColor(24, 24); |
| 957 | sc->expectFGColor(91, 96); |
| 958 | sc->expectFGColor(96, 101); |
| 959 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 962 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 963 | t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 964 | }); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 965 | { |
| 966 | SCOPED_TRACE("after setMatrix"); |
| 967 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 968 | sc->expectBGColor(24, 24); |
| 969 | sc->expectFGColor(91, 96); |
| 970 | sc->expectBGColor(96, 91); |
| 971 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 975 | class GeometryLatchingTest : public LayerUpdateTest { |
| 976 | protected: |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 977 | void EXPECT_INITIAL_STATE(const char* trace) { |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 978 | SCOPED_TRACE(trace); |
| 979 | ScreenCapture::captureScreen(&sc); |
| 980 | // We find the leading edge of the FG surface. |
| 981 | sc->expectFGColor(127, 127); |
| 982 | sc->expectBGColor(128, 128); |
| 983 | } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 984 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 985 | void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 986 | |
| 987 | void unlockFGBuffer() { |
| 988 | sp<Surface> s = mFGSurfaceControl->getSurface(); |
| 989 | ASSERT_EQ(NO_ERROR, s->unlockAndPost()); |
| 990 | waitForPostedBuffers(); |
| 991 | } |
| 992 | |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 993 | void completeFGResize() { |
| 994 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); |
| 995 | waitForPostedBuffers(); |
| 996 | } |
| 997 | void restoreInitialState() { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 998 | asTransaction([&](Transaction& t) { |
| 999 | t.setSize(mFGSurfaceControl, 64, 64); |
| 1000 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1001 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64)); |
| 1002 | t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); |
| 1003 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1004 | |
| 1005 | EXPECT_INITIAL_STATE("After restoring initial state"); |
| 1006 | } |
| 1007 | sp<ScreenCapture> sc; |
| 1008 | }; |
| 1009 | |
| 1010 | TEST_F(GeometryLatchingTest, SurfacePositionLatching) { |
| 1011 | EXPECT_INITIAL_STATE("before anything"); |
| 1012 | |
| 1013 | // By default position can be updated even while |
| 1014 | // a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1015 | asTransaction([&](Transaction& t) { |
| 1016 | t.setSize(mFGSurfaceControl, 32, 32); |
| 1017 | t.setPosition(mFGSurfaceControl, 100, 100); |
| 1018 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1019 | |
| 1020 | { |
| 1021 | SCOPED_TRACE("After moving surface"); |
| 1022 | ScreenCapture::captureScreen(&sc); |
| 1023 | // If we moved, the FG Surface should cover up what was previously BG |
| 1024 | // however if we didn't move the FG wouldn't be large enough now. |
| 1025 | sc->expectFGColor(163, 163); |
| 1026 | } |
| 1027 | |
| 1028 | restoreInitialState(); |
| 1029 | |
| 1030 | // Now we repeat with setGeometryAppliesWithResize |
| 1031 | // and verify the position DOESN'T latch. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1032 | asTransaction([&](Transaction& t) { |
| 1033 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1034 | t.setSize(mFGSurfaceControl, 32, 32); |
| 1035 | t.setPosition(mFGSurfaceControl, 100, 100); |
| 1036 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1037 | |
| 1038 | { |
| 1039 | SCOPED_TRACE("While resize is pending"); |
| 1040 | ScreenCapture::captureScreen(&sc); |
| 1041 | // This time we shouldn't have moved, so the BG color |
| 1042 | // should still be visible. |
| 1043 | sc->expectBGColor(128, 128); |
| 1044 | } |
| 1045 | |
| 1046 | completeFGResize(); |
| 1047 | |
| 1048 | { |
| 1049 | SCOPED_TRACE("After the resize"); |
| 1050 | ScreenCapture::captureScreen(&sc); |
| 1051 | // But after the resize completes, we should move |
| 1052 | // and the FG should be visible here. |
| 1053 | sc->expectFGColor(128, 128); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | class CropLatchingTest : public GeometryLatchingTest { |
| 1058 | protected: |
| 1059 | void EXPECT_CROPPED_STATE(const char* trace) { |
| 1060 | SCOPED_TRACE(trace); |
| 1061 | ScreenCapture::captureScreen(&sc); |
| 1062 | // The edge should be moved back one pixel by our crop. |
| 1063 | sc->expectFGColor(126, 126); |
| 1064 | sc->expectBGColor(127, 127); |
| 1065 | sc->expectBGColor(128, 128); |
| 1066 | } |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1067 | |
| 1068 | void EXPECT_RESIZE_STATE(const char* trace) { |
| 1069 | SCOPED_TRACE(trace); |
| 1070 | ScreenCapture::captureScreen(&sc); |
| 1071 | // The FG is now resized too 128,128 at 64,64 |
| 1072 | sc->expectFGColor(64, 64); |
| 1073 | sc->expectFGColor(191, 191); |
| 1074 | sc->expectBGColor(192, 192); |
| 1075 | } |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1076 | }; |
| 1077 | |
| 1078 | TEST_F(CropLatchingTest, CropLatching) { |
| 1079 | EXPECT_INITIAL_STATE("before anything"); |
| 1080 | // Normally the crop applies immediately even while a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1081 | asTransaction([&](Transaction& t) { |
| 1082 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1083 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63)); |
| 1084 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1085 | |
| 1086 | EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); |
| 1087 | |
| 1088 | restoreInitialState(); |
| 1089 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1090 | asTransaction([&](Transaction& t) { |
| 1091 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1092 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1093 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63)); |
| 1094 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1095 | |
| 1096 | EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); |
| 1097 | |
| 1098 | completeFGResize(); |
| 1099 | |
| 1100 | EXPECT_CROPPED_STATE("after the resize finishes"); |
| 1101 | } |
| 1102 | |
| 1103 | TEST_F(CropLatchingTest, FinalCropLatching) { |
| 1104 | EXPECT_INITIAL_STATE("before anything"); |
| 1105 | // Normally the crop applies immediately even while a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1106 | asTransaction([&](Transaction& t) { |
| 1107 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1108 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1109 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1110 | |
| 1111 | EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); |
| 1112 | |
| 1113 | restoreInitialState(); |
| 1114 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1115 | asTransaction([&](Transaction& t) { |
| 1116 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1117 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1118 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1119 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1120 | |
| 1121 | EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); |
| 1122 | |
| 1123 | completeFGResize(); |
| 1124 | |
| 1125 | EXPECT_CROPPED_STATE("after the resize finishes"); |
| 1126 | } |
| 1127 | |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1128 | // In this test we ensure that setGeometryAppliesWithResize actually demands |
| 1129 | // a buffer of the new size, and not just any size. |
| 1130 | TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) { |
| 1131 | EXPECT_INITIAL_STATE("before anything"); |
| 1132 | // Normally the crop applies immediately even while a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1133 | asTransaction([&](Transaction& t) { |
| 1134 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1135 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1136 | }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1137 | |
| 1138 | EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); |
| 1139 | |
| 1140 | restoreInitialState(); |
| 1141 | |
| 1142 | // In order to prepare to submit a buffer at the wrong size, we acquire it prior to |
| 1143 | // initiating the resize. |
| 1144 | lockAndFillFGBuffer(); |
| 1145 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1146 | asTransaction([&](Transaction& t) { |
| 1147 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1148 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1149 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1150 | }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1151 | |
| 1152 | EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); |
| 1153 | |
| 1154 | // We now submit our old buffer, at the old size, and ensure it doesn't |
| 1155 | // trigger geometry latching. |
| 1156 | unlockFGBuffer(); |
| 1157 | |
| 1158 | EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)"); |
| 1159 | |
| 1160 | completeFGResize(); |
| 1161 | |
| 1162 | EXPECT_CROPPED_STATE("after the resize finishes"); |
| 1163 | } |
| 1164 | |
| 1165 | TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) { |
| 1166 | EXPECT_INITIAL_STATE("before anything"); |
| 1167 | // In this scenario, we attempt to set the final crop a second time while the resize |
| 1168 | // is still pending, and ensure we are successful. Success meaning the second crop |
| 1169 | // is the one which eventually latches and not the first. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1170 | asTransaction([&](Transaction& t) { |
| 1171 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1172 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1173 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1174 | }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1175 | |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1176 | EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize"); |
| 1177 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1178 | asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1179 | |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1180 | EXPECT_INITIAL_STATE("after setting another crop"); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1181 | |
| 1182 | completeFGResize(); |
| 1183 | |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1184 | EXPECT_RESIZE_STATE("after the resize finishes"); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1187 | TEST_F(LayerUpdateTest, DeferredTransactionTest) { |
| 1188 | sp<ScreenCapture> sc; |
| 1189 | { |
| 1190 | SCOPED_TRACE("before anything"); |
| 1191 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1192 | sc->expectBGColor(32, 32); |
| 1193 | sc->expectFGColor(96, 96); |
| 1194 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | // set up two deferred transactions on different frames |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1198 | asTransaction([&](Transaction& t) { |
| 1199 | t.setAlpha(mFGSurfaceControl, 0.75); |
| 1200 | t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(), |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1201 | mSyncSurfaceControl->getSurface()->getNextFrameNumber()); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1202 | }); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1203 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1204 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1205 | t.setPosition(mFGSurfaceControl, 128, 128); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1206 | t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(), |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1207 | mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1208 | }); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1209 | |
| 1210 | { |
| 1211 | SCOPED_TRACE("before any trigger"); |
| 1212 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1213 | sc->expectBGColor(32, 32); |
| 1214 | sc->expectFGColor(96, 96); |
| 1215 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | // should trigger the first deferred transaction, but not the second one |
| 1219 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 1220 | { |
| 1221 | SCOPED_TRACE("after first trigger"); |
| 1222 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1223 | sc->expectBGColor(32, 32); |
| 1224 | sc->checkPixel(96, 96, 162, 63, 96); |
| 1225 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | // should show up immediately since it's not deferred |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1229 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); }); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1230 | |
| 1231 | // trigger the second deferred transaction |
| 1232 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 1233 | { |
| 1234 | SCOPED_TRACE("after second trigger"); |
| 1235 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1236 | sc->expectBGColor(32, 32); |
| 1237 | sc->expectBGColor(96, 96); |
| 1238 | sc->expectFGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1242 | TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) { |
| 1243 | sp<ScreenCapture> sc; |
| 1244 | { |
| 1245 | SCOPED_TRACE("before adding relative surface"); |
| 1246 | ScreenCapture::captureScreen(&sc); |
| 1247 | sc->expectBGColor(24, 24); |
| 1248 | sc->expectFGColor(75, 75); |
| 1249 | sc->expectBGColor(145, 145); |
| 1250 | } |
| 1251 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1252 | auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64, |
| 1253 | PIXEL_FORMAT_RGBA_8888, 0); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1254 | fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177); |
| 1255 | waitForPostedBuffers(); |
| 1256 | |
| 1257 | // Now we stack the surface above the foreground surface and make sure it is visible. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1258 | asTransaction([&](Transaction& t) { |
| 1259 | t.setPosition(relativeSurfaceControl, 64, 64); |
| 1260 | t.show(relativeSurfaceControl); |
| 1261 | t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1); |
| 1262 | }); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1263 | |
| 1264 | { |
| 1265 | SCOPED_TRACE("after adding relative surface"); |
| 1266 | ScreenCapture::captureScreen(&sc); |
| 1267 | // our relative surface should be visible now. |
| 1268 | sc->checkPixel(75, 75, 255, 177, 177); |
| 1269 | } |
| 1270 | |
| 1271 | // A call to setLayer will override a call to setRelativeLayer |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1272 | asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); }); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1273 | |
| 1274 | { |
| 1275 | SCOPED_TRACE("after set layer"); |
| 1276 | ScreenCapture::captureScreen(&sc); |
| 1277 | // now the FG surface should be visible again. |
| 1278 | sc->expectFGColor(75, 75); |
| 1279 | } |
| 1280 | } |
| 1281 | |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1282 | TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) { |
| 1283 | sp<ScreenCapture> sc; |
| 1284 | |
| 1285 | sp<SurfaceControl> childNoBuffer = |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1286 | mComposerClient->createSurface(String8("Bufferless child"), 10, 10, |
| 1287 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 1288 | sp<SurfaceControl> childBuffer = |
| 1289 | mComposerClient->createSurface(String8("Buffered child"), 20, 20, |
| 1290 | PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get()); |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1291 | fillSurfaceRGBA8(childBuffer, 200, 200, 200); |
| 1292 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1293 | SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true); |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1294 | |
| 1295 | { |
| 1296 | ScreenCapture::captureScreen(&sc); |
| 1297 | sc->expectChildColor(73, 73); |
| 1298 | sc->expectFGColor(74, 74); |
| 1299 | } |
| 1300 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1301 | SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true); |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1302 | |
| 1303 | { |
| 1304 | ScreenCapture::captureScreen(&sc); |
| 1305 | sc->expectChildColor(73, 73); |
| 1306 | sc->expectChildColor(74, 74); |
| 1307 | } |
| 1308 | } |
| 1309 | |
Robert Carr | 2c5f6d2 | 2017-09-26 12:30:35 -0700 | [diff] [blame] | 1310 | TEST_F(LayerUpdateTest, MergingTransactions) { |
| 1311 | sp<ScreenCapture> sc; |
| 1312 | { |
| 1313 | SCOPED_TRACE("before move"); |
| 1314 | ScreenCapture::captureScreen(&sc); |
| 1315 | sc->expectBGColor(0, 12); |
| 1316 | sc->expectFGColor(75, 75); |
| 1317 | sc->expectBGColor(145, 145); |
| 1318 | } |
| 1319 | |
| 1320 | Transaction t1, t2; |
| 1321 | t1.setPosition(mFGSurfaceControl, 128, 128); |
| 1322 | t2.setPosition(mFGSurfaceControl, 0, 0); |
| 1323 | // We expect that the position update from t2 now |
| 1324 | // overwrites the position update from t1. |
| 1325 | t1.merge(std::move(t2)); |
| 1326 | t1.apply(); |
| 1327 | |
| 1328 | { |
| 1329 | ScreenCapture::captureScreen(&sc); |
| 1330 | sc->expectFGColor(1, 1); |
| 1331 | } |
| 1332 | } |
| 1333 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1334 | class ChildLayerTest : public LayerUpdateTest { |
| 1335 | protected: |
| 1336 | void SetUp() override { |
| 1337 | LayerUpdateTest::SetUp(); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1338 | mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10, |
| 1339 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1340 | fillSurfaceRGBA8(mChild, 200, 200, 200); |
| 1341 | |
| 1342 | { |
| 1343 | SCOPED_TRACE("before anything"); |
| 1344 | ScreenCapture::captureScreen(&mCapture); |
| 1345 | mCapture->expectChildColor(64, 64); |
| 1346 | } |
| 1347 | } |
| 1348 | void TearDown() override { |
| 1349 | LayerUpdateTest::TearDown(); |
| 1350 | mChild = 0; |
| 1351 | } |
| 1352 | |
| 1353 | sp<SurfaceControl> mChild; |
| 1354 | sp<ScreenCapture> mCapture; |
| 1355 | }; |
| 1356 | |
| 1357 | TEST_F(ChildLayerTest, ChildLayerPositioning) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1358 | asTransaction([&](Transaction& t) { |
| 1359 | t.show(mChild); |
| 1360 | t.setPosition(mChild, 10, 10); |
| 1361 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1362 | }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1363 | |
| 1364 | { |
| 1365 | ScreenCapture::captureScreen(&mCapture); |
| 1366 | // Top left of foreground must now be visible |
| 1367 | mCapture->expectFGColor(64, 64); |
| 1368 | // But 10 pixels in we should see the child surface |
| 1369 | mCapture->expectChildColor(74, 74); |
| 1370 | // And 10 more pixels we should be back to the foreground surface |
| 1371 | mCapture->expectFGColor(84, 84); |
| 1372 | } |
| 1373 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1374 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1375 | |
| 1376 | { |
| 1377 | ScreenCapture::captureScreen(&mCapture); |
| 1378 | // Top left of foreground should now be at 0, 0 |
| 1379 | mCapture->expectFGColor(0, 0); |
| 1380 | // But 10 pixels in we should see the child surface |
| 1381 | mCapture->expectChildColor(10, 10); |
| 1382 | // And 10 more pixels we should be back to the foreground surface |
| 1383 | mCapture->expectFGColor(20, 20); |
| 1384 | } |
| 1385 | } |
| 1386 | |
Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1387 | TEST_F(ChildLayerTest, ChildLayerCropping) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1388 | asTransaction([&](Transaction& t) { |
| 1389 | t.show(mChild); |
| 1390 | t.setPosition(mChild, 0, 0); |
| 1391 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1392 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5)); |
| 1393 | }); |
Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1394 | |
| 1395 | { |
| 1396 | ScreenCapture::captureScreen(&mCapture); |
| 1397 | mCapture->expectChildColor(0, 0); |
| 1398 | mCapture->expectChildColor(4, 4); |
| 1399 | mCapture->expectBGColor(5, 5); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | TEST_F(ChildLayerTest, ChildLayerFinalCropping) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1404 | asTransaction([&](Transaction& t) { |
| 1405 | t.show(mChild); |
| 1406 | t.setPosition(mChild, 0, 0); |
| 1407 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1408 | t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5)); |
| 1409 | }); |
Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1410 | |
| 1411 | { |
| 1412 | ScreenCapture::captureScreen(&mCapture); |
| 1413 | mCapture->expectChildColor(0, 0); |
| 1414 | mCapture->expectChildColor(4, 4); |
| 1415 | mCapture->expectBGColor(5, 5); |
| 1416 | } |
| 1417 | } |
| 1418 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1419 | TEST_F(ChildLayerTest, ChildLayerConstraints) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1420 | asTransaction([&](Transaction& t) { |
| 1421 | t.show(mChild); |
| 1422 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1423 | t.setPosition(mChild, 63, 63); |
| 1424 | }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1425 | |
| 1426 | { |
| 1427 | ScreenCapture::captureScreen(&mCapture); |
| 1428 | mCapture->expectFGColor(0, 0); |
| 1429 | // Last pixel in foreground should now be the child. |
| 1430 | mCapture->expectChildColor(63, 63); |
| 1431 | // But the child should be constrained and the next pixel |
| 1432 | // must be the background |
| 1433 | mCapture->expectBGColor(64, 64); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | TEST_F(ChildLayerTest, ChildLayerScaling) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1438 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1439 | |
| 1440 | // Find the boundary between the parent and child |
| 1441 | { |
| 1442 | ScreenCapture::captureScreen(&mCapture); |
| 1443 | mCapture->expectChildColor(9, 9); |
| 1444 | mCapture->expectFGColor(10, 10); |
| 1445 | } |
| 1446 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1447 | asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1448 | |
| 1449 | // The boundary should be twice as far from the origin now. |
| 1450 | // The pixels from the last test should all be child now |
| 1451 | { |
| 1452 | ScreenCapture::captureScreen(&mCapture); |
| 1453 | mCapture->expectChildColor(9, 9); |
| 1454 | mCapture->expectChildColor(10, 10); |
| 1455 | mCapture->expectChildColor(19, 19); |
| 1456 | mCapture->expectFGColor(20, 20); |
| 1457 | } |
| 1458 | } |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1459 | |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1460 | TEST_F(ChildLayerTest, ChildLayerAlpha) { |
| 1461 | fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254); |
| 1462 | fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0); |
| 1463 | fillSurfaceRGBA8(mChild, 0, 254, 0); |
| 1464 | waitForPostedBuffers(); |
| 1465 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1466 | asTransaction([&](Transaction& t) { |
| 1467 | t.show(mChild); |
| 1468 | t.setPosition(mChild, 0, 0); |
| 1469 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1470 | }); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1471 | |
| 1472 | { |
| 1473 | ScreenCapture::captureScreen(&mCapture); |
| 1474 | // Unblended child color |
| 1475 | mCapture->checkPixel(0, 0, 0, 254, 0); |
| 1476 | } |
| 1477 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1478 | asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); }); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1479 | |
| 1480 | { |
| 1481 | ScreenCapture::captureScreen(&mCapture); |
| 1482 | // Child and BG blended. |
| 1483 | mCapture->checkPixel(0, 0, 127, 127, 0); |
| 1484 | } |
| 1485 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1486 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); }); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1487 | |
| 1488 | { |
| 1489 | ScreenCapture::captureScreen(&mCapture); |
| 1490 | // Child and BG blended. |
| 1491 | mCapture->checkPixel(0, 0, 95, 64, 95); |
| 1492 | } |
| 1493 | } |
| 1494 | |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1495 | TEST_F(ChildLayerTest, ReparentChildren) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1496 | asTransaction([&](Transaction& t) { |
| 1497 | t.show(mChild); |
| 1498 | t.setPosition(mChild, 10, 10); |
| 1499 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1500 | }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1501 | |
| 1502 | { |
| 1503 | ScreenCapture::captureScreen(&mCapture); |
| 1504 | // Top left of foreground must now be visible |
| 1505 | mCapture->expectFGColor(64, 64); |
| 1506 | // But 10 pixels in we should see the child surface |
| 1507 | mCapture->expectChildColor(74, 74); |
| 1508 | // And 10 more pixels we should be back to the foreground surface |
| 1509 | mCapture->expectFGColor(84, 84); |
| 1510 | } |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1511 | |
| 1512 | asTransaction([&](Transaction& t) { |
| 1513 | t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle()); |
| 1514 | }); |
| 1515 | |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1516 | { |
| 1517 | ScreenCapture::captureScreen(&mCapture); |
| 1518 | mCapture->expectFGColor(64, 64); |
| 1519 | // In reparenting we should have exposed the entire foreground surface. |
| 1520 | mCapture->expectFGColor(74, 74); |
| 1521 | // And the child layer should now begin at 10, 10 (since the BG |
| 1522 | // layer is at (0, 0)). |
| 1523 | mCapture->expectBGColor(9, 9); |
| 1524 | mCapture->expectChildColor(10, 10); |
| 1525 | } |
| 1526 | } |
| 1527 | |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1528 | TEST_F(ChildLayerTest, DetachChildrenSameClient) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1529 | asTransaction([&](Transaction& t) { |
| 1530 | t.show(mChild); |
| 1531 | t.setPosition(mChild, 10, 10); |
| 1532 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1533 | }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1534 | |
| 1535 | { |
| 1536 | ScreenCapture::captureScreen(&mCapture); |
| 1537 | // Top left of foreground must now be visible |
| 1538 | mCapture->expectFGColor(64, 64); |
| 1539 | // But 10 pixels in we should see the child surface |
| 1540 | mCapture->expectChildColor(74, 74); |
| 1541 | // And 10 more pixels we should be back to the foreground surface |
| 1542 | mCapture->expectFGColor(84, 84); |
| 1543 | } |
| 1544 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1545 | asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1546 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1547 | asTransaction([&](Transaction& t) { t.hide(mChild); }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1548 | |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1549 | // Since the child has the same client as the parent, it will not get |
| 1550 | // detached and will be hidden. |
| 1551 | { |
| 1552 | ScreenCapture::captureScreen(&mCapture); |
| 1553 | mCapture->expectFGColor(64, 64); |
| 1554 | mCapture->expectFGColor(74, 74); |
| 1555 | mCapture->expectFGColor(84, 84); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | TEST_F(ChildLayerTest, DetachChildrenDifferentClient) { |
| 1560 | sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient; |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1561 | sp<SurfaceControl> mChildNewClient = |
| 1562 | mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10, |
| 1563 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1564 | |
| 1565 | ASSERT_TRUE(mChildNewClient != NULL); |
| 1566 | ASSERT_TRUE(mChildNewClient->isValid()); |
| 1567 | |
| 1568 | fillSurfaceRGBA8(mChildNewClient, 200, 200, 200); |
| 1569 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1570 | asTransaction([&](Transaction& t) { |
| 1571 | t.hide(mChild); |
| 1572 | t.show(mChildNewClient); |
| 1573 | t.setPosition(mChildNewClient, 10, 10); |
| 1574 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1575 | }); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1576 | |
| 1577 | { |
| 1578 | ScreenCapture::captureScreen(&mCapture); |
| 1579 | // Top left of foreground must now be visible |
| 1580 | mCapture->expectFGColor(64, 64); |
| 1581 | // But 10 pixels in we should see the child surface |
| 1582 | mCapture->expectChildColor(74, 74); |
| 1583 | // And 10 more pixels we should be back to the foreground surface |
| 1584 | mCapture->expectFGColor(84, 84); |
| 1585 | } |
| 1586 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1587 | asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); }); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1588 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1589 | asTransaction([&](Transaction& t) { t.hide(mChildNewClient); }); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1590 | |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1591 | // Nothing should have changed. |
| 1592 | { |
| 1593 | ScreenCapture::captureScreen(&mCapture); |
| 1594 | mCapture->expectFGColor(64, 64); |
| 1595 | mCapture->expectChildColor(74, 74); |
| 1596 | mCapture->expectFGColor(84, 84); |
| 1597 | } |
| 1598 | } |
| 1599 | |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 1600 | TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1601 | asTransaction([&](Transaction& t) { |
| 1602 | t.show(mChild); |
| 1603 | t.setPosition(mChild, 0, 0); |
| 1604 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1605 | }); |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 1606 | |
| 1607 | { |
| 1608 | ScreenCapture::captureScreen(&mCapture); |
| 1609 | // We've positioned the child in the top left. |
| 1610 | mCapture->expectChildColor(0, 0); |
| 1611 | // But it's only 10x10. |
| 1612 | mCapture->expectFGColor(10, 10); |
| 1613 | } |
| 1614 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1615 | asTransaction([&](Transaction& t) { |
| 1616 | t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 1617 | // We cause scaling by 2. |
| 1618 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1619 | }); |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 1620 | |
| 1621 | { |
| 1622 | ScreenCapture::captureScreen(&mCapture); |
| 1623 | // We've positioned the child in the top left. |
| 1624 | mCapture->expectChildColor(0, 0); |
| 1625 | mCapture->expectChildColor(10, 10); |
| 1626 | mCapture->expectChildColor(19, 19); |
| 1627 | // And now it should be scaled all the way to 20x20 |
| 1628 | mCapture->expectFGColor(20, 20); |
| 1629 | } |
| 1630 | } |
| 1631 | |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1632 | // Regression test for b/37673612 |
| 1633 | TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1634 | asTransaction([&](Transaction& t) { |
| 1635 | t.show(mChild); |
| 1636 | t.setPosition(mChild, 0, 0); |
| 1637 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1638 | }); |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1639 | |
| 1640 | { |
| 1641 | ScreenCapture::captureScreen(&mCapture); |
| 1642 | // We've positioned the child in the top left. |
| 1643 | mCapture->expectChildColor(0, 0); |
| 1644 | // But it's only 10x10. |
| 1645 | mCapture->expectFGColor(10, 10); |
| 1646 | } |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1647 | // We set things up as in b/37673612 so that there is a mismatch between the buffer size and |
| 1648 | // the WM specified state size. |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1649 | asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); }); |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1650 | sp<Surface> s = mFGSurfaceControl->getSurface(); |
| 1651 | auto anw = static_cast<ANativeWindow*>(s.get()); |
| 1652 | native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90); |
| 1653 | native_window_set_buffers_dimensions(anw, 64, 128); |
| 1654 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); |
| 1655 | waitForPostedBuffers(); |
| 1656 | |
| 1657 | { |
| 1658 | // The child should still be in the same place and not have any strange scaling as in |
| 1659 | // b/37673612. |
| 1660 | ScreenCapture::captureScreen(&mCapture); |
| 1661 | mCapture->expectChildColor(0, 0); |
| 1662 | mCapture->expectFGColor(10, 10); |
| 1663 | } |
| 1664 | } |
| 1665 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 1666 | TEST_F(ChildLayerTest, Bug36858924) { |
| 1667 | // Destroy the child layer |
| 1668 | mChild.clear(); |
| 1669 | |
| 1670 | // Now recreate it as hidden |
| 1671 | mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10, |
| 1672 | PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden, |
| 1673 | mFGSurfaceControl.get()); |
| 1674 | |
| 1675 | // Show the child layer in a deferred transaction |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1676 | asTransaction([&](Transaction& t) { |
| 1677 | t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(), |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1678 | mFGSurfaceControl->getSurface()->getNextFrameNumber()); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1679 | t.show(mChild); |
| 1680 | }); |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 1681 | |
| 1682 | // Render the foreground surface a few times |
| 1683 | // |
| 1684 | // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third |
| 1685 | // frame because SurfaceFlinger would never process the deferred transaction and would therefore |
| 1686 | // never acquire/release the first buffer |
| 1687 | ALOGI("Filling 1"); |
| 1688 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); |
| 1689 | ALOGI("Filling 2"); |
| 1690 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255); |
| 1691 | ALOGI("Filling 3"); |
| 1692 | fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0); |
| 1693 | ALOGI("Filling 4"); |
| 1694 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); |
| 1695 | } |
| 1696 | |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1697 | TEST_F(ChildLayerTest, Reparent) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1698 | asTransaction([&](Transaction& t) { |
| 1699 | t.show(mChild); |
| 1700 | t.setPosition(mChild, 10, 10); |
| 1701 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1702 | }); |
chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 1703 | |
| 1704 | { |
| 1705 | ScreenCapture::captureScreen(&mCapture); |
| 1706 | // Top left of foreground must now be visible |
| 1707 | mCapture->expectFGColor(64, 64); |
| 1708 | // But 10 pixels in we should see the child surface |
| 1709 | mCapture->expectChildColor(74, 74); |
| 1710 | // And 10 more pixels we should be back to the foreground surface |
| 1711 | mCapture->expectFGColor(84, 84); |
| 1712 | } |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1713 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1714 | asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1715 | |
chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 1716 | { |
| 1717 | ScreenCapture::captureScreen(&mCapture); |
| 1718 | mCapture->expectFGColor(64, 64); |
| 1719 | // In reparenting we should have exposed the entire foreground surface. |
| 1720 | mCapture->expectFGColor(74, 74); |
| 1721 | // And the child layer should now begin at 10, 10 (since the BG |
| 1722 | // layer is at (0, 0)). |
| 1723 | mCapture->expectBGColor(9, 9); |
| 1724 | mCapture->expectChildColor(10, 10); |
| 1725 | } |
| 1726 | } |
| 1727 | |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1728 | TEST_F(ChildLayerTest, ReparentToNoParent) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1729 | asTransaction([&](Transaction& t) { |
| 1730 | t.show(mChild); |
| 1731 | t.setPosition(mChild, 10, 10); |
| 1732 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1733 | }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1734 | |
| 1735 | { |
| 1736 | ScreenCapture::captureScreen(&mCapture); |
| 1737 | // Top left of foreground must now be visible |
| 1738 | mCapture->expectFGColor(64, 64); |
| 1739 | // But 10 pixels in we should see the child surface |
| 1740 | mCapture->expectChildColor(74, 74); |
| 1741 | // And 10 more pixels we should be back to the foreground surface |
| 1742 | mCapture->expectFGColor(84, 84); |
| 1743 | } |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1744 | asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1745 | { |
| 1746 | ScreenCapture::captureScreen(&mCapture); |
| 1747 | // Nothing should have changed. |
| 1748 | mCapture->expectFGColor(64, 64); |
| 1749 | mCapture->expectChildColor(74, 74); |
| 1750 | mCapture->expectFGColor(84, 84); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | TEST_F(ChildLayerTest, ReparentFromNoParent) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1755 | sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10, |
| 1756 | PIXEL_FORMAT_RGBA_8888, 0); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1757 | ASSERT_TRUE(newSurface != NULL); |
| 1758 | ASSERT_TRUE(newSurface->isValid()); |
| 1759 | |
| 1760 | fillSurfaceRGBA8(newSurface, 63, 195, 63); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1761 | asTransaction([&](Transaction& t) { |
| 1762 | t.hide(mChild); |
| 1763 | t.show(newSurface); |
| 1764 | t.setPosition(newSurface, 10, 10); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1765 | t.setLayer(newSurface, INT32_MAX - 2); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1766 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1767 | }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1768 | |
| 1769 | { |
| 1770 | ScreenCapture::captureScreen(&mCapture); |
| 1771 | // Top left of foreground must now be visible |
| 1772 | mCapture->expectFGColor(64, 64); |
| 1773 | // At 10, 10 we should see the new surface |
| 1774 | mCapture->checkPixel(10, 10, 63, 195, 63); |
| 1775 | } |
| 1776 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1777 | asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1778 | |
| 1779 | { |
| 1780 | ScreenCapture::captureScreen(&mCapture); |
| 1781 | // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from |
| 1782 | // mFGSurface, putting it at 74, 74. |
| 1783 | mCapture->expectFGColor(64, 64); |
| 1784 | mCapture->checkPixel(74, 74, 63, 195, 63); |
| 1785 | mCapture->expectFGColor(84, 84); |
| 1786 | } |
| 1787 | } |
| 1788 | |
chaviw | c967433 | 2017-08-28 12:32:18 -0700 | [diff] [blame] | 1789 | TEST_F(ChildLayerTest, NestedChildren) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1790 | sp<SurfaceControl> grandchild = |
| 1791 | mComposerClient->createSurface(String8("Grandchild surface"), 10, 10, |
| 1792 | PIXEL_FORMAT_RGBA_8888, 0, mChild.get()); |
chaviw | c967433 | 2017-08-28 12:32:18 -0700 | [diff] [blame] | 1793 | fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 1794 | |
| 1795 | { |
| 1796 | ScreenCapture::captureScreen(&mCapture); |
| 1797 | // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer |
| 1798 | // which begins at 64, 64 |
| 1799 | mCapture->checkPixel(64, 64, 50, 50, 50); |
| 1800 | } |
| 1801 | } |
| 1802 | |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 1803 | TEST_F(ChildLayerTest, ChildLayerRelativeLayer) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1804 | sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128, |
| 1805 | 128, PIXEL_FORMAT_RGBA_8888, 0); |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 1806 | fillSurfaceRGBA8(relative, 255, 255, 255); |
| 1807 | |
| 1808 | Transaction t; |
| 1809 | t.setLayer(relative, INT32_MAX) |
| 1810 | .setRelativeLayer(mChild, relative->getHandle(), 1) |
| 1811 | .setPosition(mFGSurfaceControl, 0, 0) |
| 1812 | .apply(true); |
| 1813 | |
| 1814 | // We expect that the child should have been elevated above our |
| 1815 | // INT_MAX layer even though it's not a child of it. |
| 1816 | { |
| 1817 | ScreenCapture::captureScreen(&mCapture); |
| 1818 | mCapture->expectChildColor(0, 0); |
| 1819 | mCapture->expectChildColor(9, 9); |
| 1820 | mCapture->checkPixel(10, 10, 255, 255, 255); |
| 1821 | } |
| 1822 | } |
| 1823 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1824 | class LayerColorTest : public LayerUpdateTest { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1825 | protected: |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1826 | void SetUp() override { |
| 1827 | LayerUpdateTest::SetUp(); |
| 1828 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1829 | mLayerColorControl = |
| 1830 | mComposerClient->createSurface(String8("Layer color surface"), 128, 128, |
| 1831 | PIXEL_FORMAT_RGBA_8888, |
| 1832 | ISurfaceComposerClient::eFXSurfaceColor); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1833 | |
| 1834 | ASSERT_TRUE(mLayerColorControl != NULL); |
| 1835 | ASSERT_TRUE(mLayerColorControl->isValid()); |
| 1836 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1837 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1838 | t.setLayer(mLayerColorControl, INT32_MAX - 1); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1839 | t.setPosition(mLayerColorControl, 140, 140); |
| 1840 | t.hide(mLayerColorControl); |
| 1841 | t.hide(mFGSurfaceControl); |
| 1842 | }); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | void TearDown() override { |
| 1846 | LayerUpdateTest::TearDown(); |
| 1847 | mLayerColorControl = 0; |
| 1848 | } |
| 1849 | |
| 1850 | sp<SurfaceControl> mLayerColorControl; |
| 1851 | }; |
| 1852 | |
| 1853 | TEST_F(LayerColorTest, ColorLayerNoAlpha) { |
| 1854 | sp<ScreenCapture> sc; |
| 1855 | |
| 1856 | { |
| 1857 | SCOPED_TRACE("before setColor"); |
| 1858 | ScreenCapture::captureScreen(&sc); |
| 1859 | sc->expectBGColor(145, 145); |
| 1860 | } |
| 1861 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1862 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1863 | half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1864 | t.setColor(mLayerColorControl, color); |
| 1865 | t.show(mLayerColorControl); |
| 1866 | }); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1867 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1868 | { |
| 1869 | // There should now be a color |
| 1870 | SCOPED_TRACE("after setColor"); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1871 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1872 | ScreenCapture::captureScreen(&sc); |
| 1873 | sc->checkPixel(145, 145, 43, 207, 131); |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | TEST_F(LayerColorTest, ColorLayerWithAlpha) { |
| 1878 | sp<ScreenCapture> sc; |
| 1879 | { |
| 1880 | SCOPED_TRACE("before setColor"); |
| 1881 | ScreenCapture::captureScreen(&sc); |
| 1882 | sc->expectBGColor(145, 145); |
| 1883 | } |
| 1884 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1885 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1886 | half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1887 | t.setColor(mLayerColorControl, color); |
| 1888 | t.setAlpha(mLayerColorControl, .75f); |
| 1889 | t.show(mLayerColorControl); |
| 1890 | }); |
| 1891 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1892 | { |
| 1893 | // There should now be a color with .75 alpha |
| 1894 | SCOPED_TRACE("after setColor"); |
| 1895 | ScreenCapture::captureScreen(&sc); |
| 1896 | sc->checkPixel(145, 145, 48, 171, 147); |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | TEST_F(LayerColorTest, ColorLayerWithNoColor) { |
| 1901 | sp<ScreenCapture> sc; |
| 1902 | { |
| 1903 | SCOPED_TRACE("before setColor"); |
| 1904 | ScreenCapture::captureScreen(&sc); |
| 1905 | sc->expectBGColor(145, 145); |
| 1906 | } |
| 1907 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1908 | asTransaction([&](Transaction& t) { t.show(mLayerColorControl); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1909 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1910 | { |
| 1911 | // There should now be set to 0,0,0 (black) as default. |
| 1912 | SCOPED_TRACE("after setColor"); |
| 1913 | ScreenCapture::captureScreen(&sc); |
| 1914 | sc->checkPixel(145, 145, 0, 0, 0); |
| 1915 | } |
| 1916 | } |
| 1917 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1918 | class ScreenCaptureTest : public LayerUpdateTest { |
| 1919 | protected: |
| 1920 | std::unique_ptr<CaptureLayer> mCapture; |
| 1921 | }; |
| 1922 | |
| 1923 | TEST_F(ScreenCaptureTest, CaptureSingleLayer) { |
| 1924 | auto bgHandle = mBGSurfaceControl->getHandle(); |
| 1925 | CaptureLayer::captureScreen(&mCapture, bgHandle); |
| 1926 | mCapture->expectBGColor(0, 0); |
| 1927 | // Doesn't capture FG layer which is at 64, 64 |
| 1928 | mCapture->expectBGColor(64, 64); |
| 1929 | } |
| 1930 | |
| 1931 | TEST_F(ScreenCaptureTest, CaptureLayerWithChild) { |
| 1932 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 1933 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1934 | sp<SurfaceControl> child = |
| 1935 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 1936 | 0, mFGSurfaceControl.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1937 | fillSurfaceRGBA8(child, 200, 200, 200); |
| 1938 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1939 | SurfaceComposerClient::Transaction().show(child).apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1940 | |
| 1941 | // Captures mFGSurfaceControl layer and its child. |
| 1942 | CaptureLayer::captureScreen(&mCapture, fgHandle); |
| 1943 | mCapture->expectFGColor(10, 10); |
| 1944 | mCapture->expectChildColor(0, 0); |
| 1945 | } |
| 1946 | |
| 1947 | TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) { |
| 1948 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 1949 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1950 | sp<SurfaceControl> child = |
| 1951 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 1952 | 0, mFGSurfaceControl.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1953 | fillSurfaceRGBA8(child, 200, 200, 200); |
| 1954 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1955 | sp<SurfaceControl> grandchild = |
| 1956 | mComposerClient->createSurface(String8("Grandchild surface"), 5, 5, |
| 1957 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1958 | |
| 1959 | fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 1960 | SurfaceComposerClient::Transaction() |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1961 | .show(child) |
| 1962 | .setPosition(grandchild, 5, 5) |
| 1963 | .show(grandchild) |
| 1964 | .apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1965 | |
| 1966 | // Captures mFGSurfaceControl, its child, and the grandchild. |
| 1967 | CaptureLayer::captureScreen(&mCapture, fgHandle); |
| 1968 | mCapture->expectFGColor(10, 10); |
| 1969 | mCapture->expectChildColor(0, 0); |
| 1970 | mCapture->checkPixel(5, 5, 50, 50, 50); |
| 1971 | } |
| 1972 | |
| 1973 | TEST_F(ScreenCaptureTest, CaptureChildOnly) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1974 | sp<SurfaceControl> child = |
| 1975 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 1976 | 0, mFGSurfaceControl.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1977 | fillSurfaceRGBA8(child, 200, 200, 200); |
| 1978 | auto childHandle = child->getHandle(); |
| 1979 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1980 | SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1981 | |
| 1982 | // Captures only the child layer, and not the parent. |
| 1983 | CaptureLayer::captureScreen(&mCapture, childHandle); |
| 1984 | mCapture->expectChildColor(0, 0); |
| 1985 | mCapture->expectChildColor(9, 9); |
| 1986 | } |
| 1987 | |
| 1988 | TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1989 | sp<SurfaceControl> child = |
| 1990 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 1991 | 0, mFGSurfaceControl.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1992 | fillSurfaceRGBA8(child, 200, 200, 200); |
| 1993 | auto childHandle = child->getHandle(); |
| 1994 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1995 | sp<SurfaceControl> grandchild = |
| 1996 | mComposerClient->createSurface(String8("Grandchild surface"), 5, 5, |
| 1997 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1998 | fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 1999 | |
| 2000 | SurfaceComposerClient::Transaction() |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2001 | .show(child) |
| 2002 | .setPosition(grandchild, 5, 5) |
| 2003 | .show(grandchild) |
| 2004 | .apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2005 | |
| 2006 | auto grandchildHandle = grandchild->getHandle(); |
| 2007 | |
| 2008 | // Captures only the grandchild. |
| 2009 | CaptureLayer::captureScreen(&mCapture, grandchildHandle); |
| 2010 | mCapture->checkPixel(0, 0, 50, 50, 50); |
| 2011 | mCapture->checkPixel(4, 4, 50, 50, 50); |
| 2012 | } |
| 2013 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2014 | } // namespace android |