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 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 547 | class LayerUpdateTest : public ::testing::Test { |
| 548 | protected: |
| 549 | virtual void SetUp() { |
| 550 | mComposerClient = new SurfaceComposerClient; |
| 551 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
| 552 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 553 | sp<IBinder> display( |
| 554 | SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 555 | DisplayInfo info; |
Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 556 | SurfaceComposerClient::getDisplayInfo(display, &info); |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 557 | |
| 558 | ssize_t displayWidth = info.w; |
| 559 | ssize_t displayHeight = info.h; |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 560 | |
| 561 | // Background surface |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 562 | mBGSurfaceControl = |
| 563 | mComposerClient->createSurface(String8("BG Test Surface"), displayWidth, |
| 564 | displayHeight, PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 565 | ASSERT_TRUE(mBGSurfaceControl != NULL); |
| 566 | ASSERT_TRUE(mBGSurfaceControl->isValid()); |
| 567 | fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195); |
| 568 | |
| 569 | // Foreground surface |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 570 | mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64, |
| 571 | PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 572 | ASSERT_TRUE(mFGSurfaceControl != NULL); |
| 573 | ASSERT_TRUE(mFGSurfaceControl->isValid()); |
| 574 | |
| 575 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); |
| 576 | |
| 577 | // Synchronization surface |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 578 | mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1, |
| 579 | PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 580 | ASSERT_TRUE(mSyncSurfaceControl != NULL); |
| 581 | ASSERT_TRUE(mSyncSurfaceControl->isValid()); |
| 582 | |
| 583 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 584 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 585 | asTransaction([&](Transaction& t) { |
| 586 | t.setDisplayLayerStack(display, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 587 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 588 | t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 589 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 590 | t.setLayer(mFGSurfaceControl, INT32_MAX - 1) |
| 591 | .setPosition(mFGSurfaceControl, 64, 64) |
| 592 | .show(mFGSurfaceControl); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 593 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 594 | t.setLayer(mSyncSurfaceControl, INT32_MAX - 1) |
| 595 | .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2) |
| 596 | .show(mSyncSurfaceControl); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 597 | }); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | virtual void TearDown() { |
| 601 | mComposerClient->dispose(); |
| 602 | mBGSurfaceControl = 0; |
| 603 | mFGSurfaceControl = 0; |
| 604 | mSyncSurfaceControl = 0; |
| 605 | mComposerClient = 0; |
| 606 | } |
| 607 | |
| 608 | void waitForPostedBuffers() { |
| 609 | // Since the sync surface is in synchronous mode (i.e. double buffered) |
| 610 | // posting three buffers to it should ensure that at least two |
| 611 | // SurfaceFlinger::handlePageFlip calls have been made, which should |
| 612 | // guaranteed that a buffer posted to another Surface has been retired. |
| 613 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 614 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 615 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 616 | } |
| 617 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 618 | void asTransaction(const std::function<void(Transaction&)>& exec) { |
| 619 | Transaction t; |
| 620 | exec(t); |
| 621 | t.apply(true); |
| 622 | } |
| 623 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 624 | sp<SurfaceComposerClient> mComposerClient; |
| 625 | sp<SurfaceControl> mBGSurfaceControl; |
| 626 | sp<SurfaceControl> mFGSurfaceControl; |
| 627 | |
| 628 | // This surface is used to ensure that the buffers posted to |
| 629 | // mFGSurfaceControl have been picked up by SurfaceFlinger. |
| 630 | sp<SurfaceControl> mSyncSurfaceControl; |
| 631 | }; |
| 632 | |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 633 | TEST_F(LayerUpdateTest, RelativesAreNotDetached) { |
| 634 | sp<ScreenCapture> sc; |
| 635 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 636 | sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10, |
| 637 | 10, PIXEL_FORMAT_RGBA_8888, 0); |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 638 | fillSurfaceRGBA8(relative, 10, 10, 10); |
| 639 | waitForPostedBuffers(); |
| 640 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 641 | Transaction{} |
| 642 | .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1) |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 643 | .setPosition(relative, 64, 64) |
| 644 | .apply(); |
| 645 | |
| 646 | { |
| 647 | // The relative should be on top of the FG control. |
| 648 | ScreenCapture::captureScreen(&sc); |
| 649 | sc->checkPixel(64, 64, 10, 10, 10); |
| 650 | } |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 651 | Transaction{}.detachChildren(mFGSurfaceControl).apply(); |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 652 | |
| 653 | { |
| 654 | // Nothing should change at this point. |
| 655 | ScreenCapture::captureScreen(&sc); |
| 656 | sc->checkPixel(64, 64, 10, 10, 10); |
| 657 | } |
| 658 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 659 | Transaction{}.hide(relative).apply(); |
Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 660 | |
| 661 | { |
| 662 | // Ensure that the relative was actually hidden, rather than |
| 663 | // being left in the detached but visible state. |
| 664 | ScreenCapture::captureScreen(&sc); |
| 665 | sc->expectFGColor(64, 64); |
| 666 | } |
| 667 | } |
| 668 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 669 | TEST_F(LayerUpdateTest, LayerMoveWorks) { |
| 670 | sp<ScreenCapture> sc; |
| 671 | { |
| 672 | SCOPED_TRACE("before move"); |
| 673 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 674 | sc->expectBGColor(0, 12); |
| 675 | sc->expectFGColor(75, 75); |
| 676 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 679 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 680 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 681 | { |
| 682 | // This should reflect the new position, but not the new color. |
| 683 | SCOPED_TRACE("after move, before redraw"); |
| 684 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 685 | sc->expectBGColor(24, 24); |
| 686 | sc->expectBGColor(75, 75); |
| 687 | sc->expectFGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63); |
| 691 | waitForPostedBuffers(); |
| 692 | { |
| 693 | // This should reflect the new position and the new color. |
| 694 | SCOPED_TRACE("after redraw"); |
| 695 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 696 | sc->expectBGColor(24, 24); |
| 697 | sc->expectBGColor(75, 75); |
| 698 | sc->checkPixel(145, 145, 63, 195, 63); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | TEST_F(LayerUpdateTest, LayerResizeWorks) { |
| 703 | sp<ScreenCapture> sc; |
| 704 | { |
| 705 | SCOPED_TRACE("before resize"); |
| 706 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 707 | sc->expectBGColor(0, 12); |
| 708 | sc->expectFGColor(75, 75); |
| 709 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 712 | ALOGD("resizing"); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 713 | asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); }); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 714 | ALOGD("resized"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 715 | { |
| 716 | // This should not reflect the new size or color because SurfaceFlinger |
| 717 | // has not yet received a buffer of the correct size. |
| 718 | SCOPED_TRACE("after resize, before redraw"); |
| 719 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 720 | sc->expectBGColor(0, 12); |
| 721 | sc->expectFGColor(75, 75); |
| 722 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 725 | ALOGD("drawing"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 726 | fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63); |
| 727 | waitForPostedBuffers(); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 728 | ALOGD("drawn"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 729 | { |
| 730 | // This should reflect the new size and the new color. |
| 731 | SCOPED_TRACE("after redraw"); |
| 732 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 733 | sc->expectBGColor(24, 24); |
| 734 | sc->checkPixel(75, 75, 63, 195, 63); |
| 735 | sc->checkPixel(145, 145, 63, 195, 63); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 739 | TEST_F(LayerUpdateTest, LayerCropWorks) { |
| 740 | sp<ScreenCapture> sc; |
| 741 | { |
| 742 | SCOPED_TRACE("before crop"); |
| 743 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 744 | sc->expectBGColor(24, 24); |
| 745 | sc->expectFGColor(75, 75); |
| 746 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 749 | asTransaction([&](Transaction& t) { |
| 750 | Rect cropRect(16, 16, 32, 32); |
| 751 | t.setCrop(mFGSurfaceControl, cropRect); |
| 752 | }); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 753 | { |
| 754 | // This should crop the foreground surface. |
| 755 | SCOPED_TRACE("after crop"); |
| 756 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 757 | sc->expectBGColor(24, 24); |
| 758 | sc->expectBGColor(75, 75); |
| 759 | sc->expectFGColor(95, 80); |
| 760 | sc->expectFGColor(80, 95); |
| 761 | sc->expectBGColor(96, 96); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 765 | TEST_F(LayerUpdateTest, LayerFinalCropWorks) { |
| 766 | sp<ScreenCapture> sc; |
| 767 | { |
| 768 | SCOPED_TRACE("before crop"); |
| 769 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 770 | sc->expectBGColor(24, 24); |
| 771 | sc->expectFGColor(75, 75); |
| 772 | sc->expectBGColor(145, 145); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 773 | } |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 774 | asTransaction([&](Transaction& t) { |
| 775 | Rect cropRect(16, 16, 32, 32); |
| 776 | t.setFinalCrop(mFGSurfaceControl, cropRect); |
| 777 | }); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 778 | { |
| 779 | // This should crop the foreground surface. |
| 780 | SCOPED_TRACE("after crop"); |
| 781 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 782 | sc->expectBGColor(24, 24); |
| 783 | sc->expectBGColor(75, 75); |
| 784 | sc->expectBGColor(95, 80); |
| 785 | sc->expectBGColor(80, 95); |
| 786 | sc->expectBGColor(96, 96); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 790 | TEST_F(LayerUpdateTest, LayerSetLayerWorks) { |
| 791 | sp<ScreenCapture> sc; |
| 792 | { |
| 793 | SCOPED_TRACE("before setLayer"); |
| 794 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 795 | sc->expectBGColor(24, 24); |
| 796 | sc->expectFGColor(75, 75); |
| 797 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 800 | asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 801 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 802 | { |
| 803 | // This should hide the foreground surface beneath the background. |
| 804 | SCOPED_TRACE("after setLayer"); |
| 805 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 806 | sc->expectBGColor(24, 24); |
| 807 | sc->expectBGColor(75, 75); |
| 808 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | TEST_F(LayerUpdateTest, LayerShowHideWorks) { |
| 813 | sp<ScreenCapture> sc; |
| 814 | { |
| 815 | SCOPED_TRACE("before hide"); |
| 816 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 817 | sc->expectBGColor(24, 24); |
| 818 | sc->expectFGColor(75, 75); |
| 819 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 822 | asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 823 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 824 | { |
| 825 | // This should hide the foreground surface. |
| 826 | SCOPED_TRACE("after hide, before show"); |
| 827 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 828 | sc->expectBGColor(24, 24); |
| 829 | sc->expectBGColor(75, 75); |
| 830 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 833 | asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 834 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 835 | { |
| 836 | // This should show the foreground surface. |
| 837 | SCOPED_TRACE("after show"); |
| 838 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 839 | sc->expectBGColor(24, 24); |
| 840 | sc->expectFGColor(75, 75); |
| 841 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | TEST_F(LayerUpdateTest, LayerSetAlphaWorks) { |
| 846 | sp<ScreenCapture> sc; |
| 847 | { |
| 848 | SCOPED_TRACE("before setAlpha"); |
| 849 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 850 | sc->expectBGColor(24, 24); |
| 851 | sc->expectFGColor(75, 75); |
| 852 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 855 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 856 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 857 | { |
| 858 | // This should set foreground to be 75% opaque. |
| 859 | SCOPED_TRACE("after setAlpha"); |
| 860 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 861 | sc->expectBGColor(24, 24); |
| 862 | sc->checkPixel(75, 75, 162, 63, 96); |
| 863 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 867 | TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) { |
| 868 | sp<ScreenCapture> sc; |
| 869 | { |
| 870 | SCOPED_TRACE("before setLayerStack"); |
| 871 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 872 | sc->expectBGColor(24, 24); |
| 873 | sc->expectFGColor(75, 75); |
| 874 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 877 | asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); }); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 878 | { |
| 879 | // This should hide the foreground surface since it goes to a different |
| 880 | // layer stack. |
| 881 | SCOPED_TRACE("after setLayerStack"); |
| 882 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 883 | sc->expectBGColor(24, 24); |
| 884 | sc->expectBGColor(75, 75); |
| 885 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
| 889 | TEST_F(LayerUpdateTest, LayerSetFlagsWorks) { |
| 890 | sp<ScreenCapture> sc; |
| 891 | { |
| 892 | SCOPED_TRACE("before setFlags"); |
| 893 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 894 | sc->expectBGColor(24, 24); |
| 895 | sc->expectFGColor(75, 75); |
| 896 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 899 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 900 | t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 901 | }); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 902 | { |
| 903 | // This should hide the foreground surface |
| 904 | SCOPED_TRACE("after setFlags"); |
| 905 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 906 | sc->expectBGColor(24, 24); |
| 907 | sc->expectBGColor(75, 75); |
| 908 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
| 912 | TEST_F(LayerUpdateTest, LayerSetMatrixWorks) { |
| 913 | sp<ScreenCapture> sc; |
| 914 | { |
| 915 | SCOPED_TRACE("before setMatrix"); |
| 916 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 917 | sc->expectBGColor(24, 24); |
| 918 | sc->expectFGColor(91, 96); |
| 919 | sc->expectFGColor(96, 101); |
| 920 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 923 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 924 | 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] | 925 | }); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 926 | { |
| 927 | SCOPED_TRACE("after setMatrix"); |
| 928 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 929 | sc->expectBGColor(24, 24); |
| 930 | sc->expectFGColor(91, 96); |
| 931 | sc->expectBGColor(96, 91); |
| 932 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 936 | class GeometryLatchingTest : public LayerUpdateTest { |
| 937 | protected: |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 938 | void EXPECT_INITIAL_STATE(const char* trace) { |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 939 | SCOPED_TRACE(trace); |
| 940 | ScreenCapture::captureScreen(&sc); |
| 941 | // We find the leading edge of the FG surface. |
| 942 | sc->expectFGColor(127, 127); |
| 943 | sc->expectBGColor(128, 128); |
| 944 | } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 945 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 946 | void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 947 | |
| 948 | void unlockFGBuffer() { |
| 949 | sp<Surface> s = mFGSurfaceControl->getSurface(); |
| 950 | ASSERT_EQ(NO_ERROR, s->unlockAndPost()); |
| 951 | waitForPostedBuffers(); |
| 952 | } |
| 953 | |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 954 | void completeFGResize() { |
| 955 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); |
| 956 | waitForPostedBuffers(); |
| 957 | } |
| 958 | void restoreInitialState() { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 959 | asTransaction([&](Transaction& t) { |
| 960 | t.setSize(mFGSurfaceControl, 64, 64); |
| 961 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 962 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64)); |
| 963 | t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); |
| 964 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 965 | |
| 966 | EXPECT_INITIAL_STATE("After restoring initial state"); |
| 967 | } |
| 968 | sp<ScreenCapture> sc; |
| 969 | }; |
| 970 | |
| 971 | TEST_F(GeometryLatchingTest, SurfacePositionLatching) { |
| 972 | EXPECT_INITIAL_STATE("before anything"); |
| 973 | |
| 974 | // By default position can be updated even while |
| 975 | // a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 976 | asTransaction([&](Transaction& t) { |
| 977 | t.setSize(mFGSurfaceControl, 32, 32); |
| 978 | t.setPosition(mFGSurfaceControl, 100, 100); |
| 979 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 980 | |
| 981 | { |
| 982 | SCOPED_TRACE("After moving surface"); |
| 983 | ScreenCapture::captureScreen(&sc); |
| 984 | // If we moved, the FG Surface should cover up what was previously BG |
| 985 | // however if we didn't move the FG wouldn't be large enough now. |
| 986 | sc->expectFGColor(163, 163); |
| 987 | } |
| 988 | |
| 989 | restoreInitialState(); |
| 990 | |
| 991 | // Now we repeat with setGeometryAppliesWithResize |
| 992 | // and verify the position DOESN'T latch. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 993 | asTransaction([&](Transaction& t) { |
| 994 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 995 | t.setSize(mFGSurfaceControl, 32, 32); |
| 996 | t.setPosition(mFGSurfaceControl, 100, 100); |
| 997 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 998 | |
| 999 | { |
| 1000 | SCOPED_TRACE("While resize is pending"); |
| 1001 | ScreenCapture::captureScreen(&sc); |
| 1002 | // This time we shouldn't have moved, so the BG color |
| 1003 | // should still be visible. |
| 1004 | sc->expectBGColor(128, 128); |
| 1005 | } |
| 1006 | |
| 1007 | completeFGResize(); |
| 1008 | |
| 1009 | { |
| 1010 | SCOPED_TRACE("After the resize"); |
| 1011 | ScreenCapture::captureScreen(&sc); |
| 1012 | // But after the resize completes, we should move |
| 1013 | // and the FG should be visible here. |
| 1014 | sc->expectFGColor(128, 128); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | class CropLatchingTest : public GeometryLatchingTest { |
| 1019 | protected: |
| 1020 | void EXPECT_CROPPED_STATE(const char* trace) { |
| 1021 | SCOPED_TRACE(trace); |
| 1022 | ScreenCapture::captureScreen(&sc); |
| 1023 | // The edge should be moved back one pixel by our crop. |
| 1024 | sc->expectFGColor(126, 126); |
| 1025 | sc->expectBGColor(127, 127); |
| 1026 | sc->expectBGColor(128, 128); |
| 1027 | } |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1028 | |
| 1029 | void EXPECT_RESIZE_STATE(const char* trace) { |
| 1030 | SCOPED_TRACE(trace); |
| 1031 | ScreenCapture::captureScreen(&sc); |
| 1032 | // The FG is now resized too 128,128 at 64,64 |
| 1033 | sc->expectFGColor(64, 64); |
| 1034 | sc->expectFGColor(191, 191); |
| 1035 | sc->expectBGColor(192, 192); |
| 1036 | } |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1037 | }; |
| 1038 | |
| 1039 | TEST_F(CropLatchingTest, CropLatching) { |
| 1040 | EXPECT_INITIAL_STATE("before anything"); |
| 1041 | // Normally the crop applies immediately even while a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1042 | asTransaction([&](Transaction& t) { |
| 1043 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1044 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63)); |
| 1045 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1046 | |
| 1047 | EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); |
| 1048 | |
| 1049 | restoreInitialState(); |
| 1050 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1051 | asTransaction([&](Transaction& t) { |
| 1052 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1053 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1054 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63)); |
| 1055 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1056 | |
| 1057 | EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); |
| 1058 | |
| 1059 | completeFGResize(); |
| 1060 | |
| 1061 | EXPECT_CROPPED_STATE("after the resize finishes"); |
| 1062 | } |
| 1063 | |
| 1064 | TEST_F(CropLatchingTest, FinalCropLatching) { |
| 1065 | EXPECT_INITIAL_STATE("before anything"); |
| 1066 | // Normally the crop applies immediately even while a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1067 | asTransaction([&](Transaction& t) { |
| 1068 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1069 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1070 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1071 | |
| 1072 | EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); |
| 1073 | |
| 1074 | restoreInitialState(); |
| 1075 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1076 | asTransaction([&](Transaction& t) { |
| 1077 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1078 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1079 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1080 | }); |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1081 | |
| 1082 | EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); |
| 1083 | |
| 1084 | completeFGResize(); |
| 1085 | |
| 1086 | EXPECT_CROPPED_STATE("after the resize finishes"); |
| 1087 | } |
| 1088 | |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1089 | // In this test we ensure that setGeometryAppliesWithResize actually demands |
| 1090 | // a buffer of the new size, and not just any size. |
| 1091 | TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) { |
| 1092 | EXPECT_INITIAL_STATE("before anything"); |
| 1093 | // Normally the crop applies immediately even while a resize is pending. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1094 | asTransaction([&](Transaction& t) { |
| 1095 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1096 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1097 | }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1098 | |
| 1099 | EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); |
| 1100 | |
| 1101 | restoreInitialState(); |
| 1102 | |
| 1103 | // In order to prepare to submit a buffer at the wrong size, we acquire it prior to |
| 1104 | // initiating the resize. |
| 1105 | lockAndFillFGBuffer(); |
| 1106 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1107 | asTransaction([&](Transaction& t) { |
| 1108 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1109 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1110 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1111 | }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1112 | |
| 1113 | EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); |
| 1114 | |
| 1115 | // We now submit our old buffer, at the old size, and ensure it doesn't |
| 1116 | // trigger geometry latching. |
| 1117 | unlockFGBuffer(); |
| 1118 | |
| 1119 | EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)"); |
| 1120 | |
| 1121 | completeFGResize(); |
| 1122 | |
| 1123 | EXPECT_CROPPED_STATE("after the resize finishes"); |
| 1124 | } |
| 1125 | |
| 1126 | TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) { |
| 1127 | EXPECT_INITIAL_STATE("before anything"); |
| 1128 | // In this scenario, we attempt to set the final crop a second time while the resize |
| 1129 | // is still pending, and ensure we are successful. Success meaning the second crop |
| 1130 | // is the one which eventually latches and not the first. |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1131 | asTransaction([&](Transaction& t) { |
| 1132 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1133 | t.setGeometryAppliesWithResize(mFGSurfaceControl); |
| 1134 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); |
| 1135 | }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1136 | |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1137 | EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize"); |
| 1138 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1139 | asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); }); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1140 | |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1141 | EXPECT_INITIAL_STATE("after setting another crop"); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1142 | |
| 1143 | completeFGResize(); |
| 1144 | |
chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1145 | EXPECT_RESIZE_STATE("after the resize finishes"); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1148 | TEST_F(LayerUpdateTest, DeferredTransactionTest) { |
| 1149 | sp<ScreenCapture> sc; |
| 1150 | { |
| 1151 | SCOPED_TRACE("before anything"); |
| 1152 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1153 | sc->expectBGColor(32, 32); |
| 1154 | sc->expectFGColor(96, 96); |
| 1155 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | // set up two deferred transactions on different frames |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1159 | asTransaction([&](Transaction& t) { |
| 1160 | t.setAlpha(mFGSurfaceControl, 0.75); |
| 1161 | t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(), |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1162 | mSyncSurfaceControl->getSurface()->getNextFrameNumber()); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1163 | }); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1164 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1165 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1166 | t.setPosition(mFGSurfaceControl, 128, 128); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1167 | t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(), |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1168 | mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1169 | }); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1170 | |
| 1171 | { |
| 1172 | SCOPED_TRACE("before any trigger"); |
| 1173 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1174 | sc->expectBGColor(32, 32); |
| 1175 | sc->expectFGColor(96, 96); |
| 1176 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | // should trigger the first deferred transaction, but not the second one |
| 1180 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 1181 | { |
| 1182 | SCOPED_TRACE("after first trigger"); |
| 1183 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1184 | sc->expectBGColor(32, 32); |
| 1185 | sc->checkPixel(96, 96, 162, 63, 96); |
| 1186 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | // should show up immediately since it's not deferred |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1190 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); }); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1191 | |
| 1192 | // trigger the second deferred transaction |
| 1193 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 1194 | { |
| 1195 | SCOPED_TRACE("after second trigger"); |
| 1196 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1197 | sc->expectBGColor(32, 32); |
| 1198 | sc->expectBGColor(96, 96); |
| 1199 | sc->expectFGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1203 | TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) { |
| 1204 | sp<ScreenCapture> sc; |
| 1205 | { |
| 1206 | SCOPED_TRACE("before adding relative surface"); |
| 1207 | ScreenCapture::captureScreen(&sc); |
| 1208 | sc->expectBGColor(24, 24); |
| 1209 | sc->expectFGColor(75, 75); |
| 1210 | sc->expectBGColor(145, 145); |
| 1211 | } |
| 1212 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1213 | auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64, |
| 1214 | PIXEL_FORMAT_RGBA_8888, 0); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1215 | fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177); |
| 1216 | waitForPostedBuffers(); |
| 1217 | |
| 1218 | // 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] | 1219 | asTransaction([&](Transaction& t) { |
| 1220 | t.setPosition(relativeSurfaceControl, 64, 64); |
| 1221 | t.show(relativeSurfaceControl); |
| 1222 | t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1); |
| 1223 | }); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1224 | |
| 1225 | { |
| 1226 | SCOPED_TRACE("after adding relative surface"); |
| 1227 | ScreenCapture::captureScreen(&sc); |
| 1228 | // our relative surface should be visible now. |
| 1229 | sc->checkPixel(75, 75, 255, 177, 177); |
| 1230 | } |
| 1231 | |
| 1232 | // A call to setLayer will override a call to setRelativeLayer |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1233 | asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); }); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1234 | |
| 1235 | { |
| 1236 | SCOPED_TRACE("after set layer"); |
| 1237 | ScreenCapture::captureScreen(&sc); |
| 1238 | // now the FG surface should be visible again. |
| 1239 | sc->expectFGColor(75, 75); |
| 1240 | } |
| 1241 | } |
| 1242 | |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1243 | TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) { |
| 1244 | sp<ScreenCapture> sc; |
| 1245 | |
| 1246 | sp<SurfaceControl> childNoBuffer = |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1247 | mComposerClient->createSurface(String8("Bufferless child"), 10, 10, |
| 1248 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
| 1249 | sp<SurfaceControl> childBuffer = |
| 1250 | mComposerClient->createSurface(String8("Buffered child"), 20, 20, |
| 1251 | PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get()); |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1252 | fillSurfaceRGBA8(childBuffer, 200, 200, 200); |
| 1253 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1254 | SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true); |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1255 | |
| 1256 | { |
| 1257 | ScreenCapture::captureScreen(&sc); |
| 1258 | sc->expectChildColor(73, 73); |
| 1259 | sc->expectFGColor(74, 74); |
| 1260 | } |
| 1261 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1262 | SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true); |
Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1263 | |
| 1264 | { |
| 1265 | ScreenCapture::captureScreen(&sc); |
| 1266 | sc->expectChildColor(73, 73); |
| 1267 | sc->expectChildColor(74, 74); |
| 1268 | } |
| 1269 | } |
| 1270 | |
Robert Carr | 2c5f6d2 | 2017-09-26 12:30:35 -0700 | [diff] [blame] | 1271 | TEST_F(LayerUpdateTest, MergingTransactions) { |
| 1272 | sp<ScreenCapture> sc; |
| 1273 | { |
| 1274 | SCOPED_TRACE("before move"); |
| 1275 | ScreenCapture::captureScreen(&sc); |
| 1276 | sc->expectBGColor(0, 12); |
| 1277 | sc->expectFGColor(75, 75); |
| 1278 | sc->expectBGColor(145, 145); |
| 1279 | } |
| 1280 | |
| 1281 | Transaction t1, t2; |
| 1282 | t1.setPosition(mFGSurfaceControl, 128, 128); |
| 1283 | t2.setPosition(mFGSurfaceControl, 0, 0); |
| 1284 | // We expect that the position update from t2 now |
| 1285 | // overwrites the position update from t1. |
| 1286 | t1.merge(std::move(t2)); |
| 1287 | t1.apply(); |
| 1288 | |
| 1289 | { |
| 1290 | ScreenCapture::captureScreen(&sc); |
| 1291 | sc->expectFGColor(1, 1); |
| 1292 | } |
| 1293 | } |
| 1294 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1295 | class ChildLayerTest : public LayerUpdateTest { |
| 1296 | protected: |
| 1297 | void SetUp() override { |
| 1298 | LayerUpdateTest::SetUp(); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1299 | mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10, |
| 1300 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1301 | fillSurfaceRGBA8(mChild, 200, 200, 200); |
| 1302 | |
| 1303 | { |
| 1304 | SCOPED_TRACE("before anything"); |
| 1305 | ScreenCapture::captureScreen(&mCapture); |
| 1306 | mCapture->expectChildColor(64, 64); |
| 1307 | } |
| 1308 | } |
| 1309 | void TearDown() override { |
| 1310 | LayerUpdateTest::TearDown(); |
| 1311 | mChild = 0; |
| 1312 | } |
| 1313 | |
| 1314 | sp<SurfaceControl> mChild; |
| 1315 | sp<ScreenCapture> mCapture; |
| 1316 | }; |
| 1317 | |
| 1318 | TEST_F(ChildLayerTest, ChildLayerPositioning) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1319 | asTransaction([&](Transaction& t) { |
| 1320 | t.show(mChild); |
| 1321 | t.setPosition(mChild, 10, 10); |
| 1322 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1323 | }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1324 | |
| 1325 | { |
| 1326 | ScreenCapture::captureScreen(&mCapture); |
| 1327 | // Top left of foreground must now be visible |
| 1328 | mCapture->expectFGColor(64, 64); |
| 1329 | // But 10 pixels in we should see the child surface |
| 1330 | mCapture->expectChildColor(74, 74); |
| 1331 | // And 10 more pixels we should be back to the foreground surface |
| 1332 | mCapture->expectFGColor(84, 84); |
| 1333 | } |
| 1334 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1335 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1336 | |
| 1337 | { |
| 1338 | ScreenCapture::captureScreen(&mCapture); |
| 1339 | // Top left of foreground should now be at 0, 0 |
| 1340 | mCapture->expectFGColor(0, 0); |
| 1341 | // But 10 pixels in we should see the child surface |
| 1342 | mCapture->expectChildColor(10, 10); |
| 1343 | // And 10 more pixels we should be back to the foreground surface |
| 1344 | mCapture->expectFGColor(20, 20); |
| 1345 | } |
| 1346 | } |
| 1347 | |
Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1348 | TEST_F(ChildLayerTest, ChildLayerCropping) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1349 | asTransaction([&](Transaction& t) { |
| 1350 | t.show(mChild); |
| 1351 | t.setPosition(mChild, 0, 0); |
| 1352 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1353 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5)); |
| 1354 | }); |
Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1355 | |
| 1356 | { |
| 1357 | ScreenCapture::captureScreen(&mCapture); |
| 1358 | mCapture->expectChildColor(0, 0); |
| 1359 | mCapture->expectChildColor(4, 4); |
| 1360 | mCapture->expectBGColor(5, 5); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | TEST_F(ChildLayerTest, ChildLayerFinalCropping) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1365 | asTransaction([&](Transaction& t) { |
| 1366 | t.show(mChild); |
| 1367 | t.setPosition(mChild, 0, 0); |
| 1368 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1369 | t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5)); |
| 1370 | }); |
Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1371 | |
| 1372 | { |
| 1373 | ScreenCapture::captureScreen(&mCapture); |
| 1374 | mCapture->expectChildColor(0, 0); |
| 1375 | mCapture->expectChildColor(4, 4); |
| 1376 | mCapture->expectBGColor(5, 5); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1380 | TEST_F(ChildLayerTest, ChildLayerConstraints) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1381 | asTransaction([&](Transaction& t) { |
| 1382 | t.show(mChild); |
| 1383 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1384 | t.setPosition(mChild, 63, 63); |
| 1385 | }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1386 | |
| 1387 | { |
| 1388 | ScreenCapture::captureScreen(&mCapture); |
| 1389 | mCapture->expectFGColor(0, 0); |
| 1390 | // Last pixel in foreground should now be the child. |
| 1391 | mCapture->expectChildColor(63, 63); |
| 1392 | // But the child should be constrained and the next pixel |
| 1393 | // must be the background |
| 1394 | mCapture->expectBGColor(64, 64); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | TEST_F(ChildLayerTest, ChildLayerScaling) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1399 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1400 | |
| 1401 | // Find the boundary between the parent and child |
| 1402 | { |
| 1403 | ScreenCapture::captureScreen(&mCapture); |
| 1404 | mCapture->expectChildColor(9, 9); |
| 1405 | mCapture->expectFGColor(10, 10); |
| 1406 | } |
| 1407 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1408 | asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); }); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1409 | |
| 1410 | // The boundary should be twice as far from the origin now. |
| 1411 | // The pixels from the last test should all be child now |
| 1412 | { |
| 1413 | ScreenCapture::captureScreen(&mCapture); |
| 1414 | mCapture->expectChildColor(9, 9); |
| 1415 | mCapture->expectChildColor(10, 10); |
| 1416 | mCapture->expectChildColor(19, 19); |
| 1417 | mCapture->expectFGColor(20, 20); |
| 1418 | } |
| 1419 | } |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1420 | |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1421 | TEST_F(ChildLayerTest, ChildLayerAlpha) { |
| 1422 | fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254); |
| 1423 | fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0); |
| 1424 | fillSurfaceRGBA8(mChild, 0, 254, 0); |
| 1425 | waitForPostedBuffers(); |
| 1426 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1427 | asTransaction([&](Transaction& t) { |
| 1428 | t.show(mChild); |
| 1429 | t.setPosition(mChild, 0, 0); |
| 1430 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1431 | }); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1432 | |
| 1433 | { |
| 1434 | ScreenCapture::captureScreen(&mCapture); |
| 1435 | // Unblended child color |
| 1436 | mCapture->checkPixel(0, 0, 0, 254, 0); |
| 1437 | } |
| 1438 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1439 | asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); }); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1440 | |
| 1441 | { |
| 1442 | ScreenCapture::captureScreen(&mCapture); |
| 1443 | // Child and BG blended. |
| 1444 | mCapture->checkPixel(0, 0, 127, 127, 0); |
| 1445 | } |
| 1446 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1447 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); }); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1448 | |
| 1449 | { |
| 1450 | ScreenCapture::captureScreen(&mCapture); |
| 1451 | // Child and BG blended. |
| 1452 | mCapture->checkPixel(0, 0, 95, 64, 95); |
| 1453 | } |
| 1454 | } |
| 1455 | |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1456 | TEST_F(ChildLayerTest, ReparentChildren) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1457 | asTransaction([&](Transaction& t) { |
| 1458 | t.show(mChild); |
| 1459 | t.setPosition(mChild, 10, 10); |
| 1460 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1461 | }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1462 | |
| 1463 | { |
| 1464 | ScreenCapture::captureScreen(&mCapture); |
| 1465 | // Top left of foreground must now be visible |
| 1466 | mCapture->expectFGColor(64, 64); |
| 1467 | // But 10 pixels in we should see the child surface |
| 1468 | mCapture->expectChildColor(74, 74); |
| 1469 | // And 10 more pixels we should be back to the foreground surface |
| 1470 | mCapture->expectFGColor(84, 84); |
| 1471 | } |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1472 | |
| 1473 | asTransaction([&](Transaction& t) { |
| 1474 | t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle()); |
| 1475 | }); |
| 1476 | |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1477 | { |
| 1478 | ScreenCapture::captureScreen(&mCapture); |
| 1479 | mCapture->expectFGColor(64, 64); |
| 1480 | // In reparenting we should have exposed the entire foreground surface. |
| 1481 | mCapture->expectFGColor(74, 74); |
| 1482 | // And the child layer should now begin at 10, 10 (since the BG |
| 1483 | // layer is at (0, 0)). |
| 1484 | mCapture->expectBGColor(9, 9); |
| 1485 | mCapture->expectChildColor(10, 10); |
| 1486 | } |
| 1487 | } |
| 1488 | |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1489 | TEST_F(ChildLayerTest, DetachChildrenSameClient) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1490 | asTransaction([&](Transaction& t) { |
| 1491 | t.show(mChild); |
| 1492 | t.setPosition(mChild, 10, 10); |
| 1493 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1494 | }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1495 | |
| 1496 | { |
| 1497 | ScreenCapture::captureScreen(&mCapture); |
| 1498 | // Top left of foreground must now be visible |
| 1499 | mCapture->expectFGColor(64, 64); |
| 1500 | // But 10 pixels in we should see the child surface |
| 1501 | mCapture->expectChildColor(74, 74); |
| 1502 | // And 10 more pixels we should be back to the foreground surface |
| 1503 | mCapture->expectFGColor(84, 84); |
| 1504 | } |
| 1505 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1506 | asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1507 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1508 | asTransaction([&](Transaction& t) { t.hide(mChild); }); |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1509 | |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1510 | // Since the child has the same client as the parent, it will not get |
| 1511 | // detached and will be hidden. |
| 1512 | { |
| 1513 | ScreenCapture::captureScreen(&mCapture); |
| 1514 | mCapture->expectFGColor(64, 64); |
| 1515 | mCapture->expectFGColor(74, 74); |
| 1516 | mCapture->expectFGColor(84, 84); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | TEST_F(ChildLayerTest, DetachChildrenDifferentClient) { |
| 1521 | sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient; |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1522 | sp<SurfaceControl> mChildNewClient = |
| 1523 | mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10, |
| 1524 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1525 | |
| 1526 | ASSERT_TRUE(mChildNewClient != NULL); |
| 1527 | ASSERT_TRUE(mChildNewClient->isValid()); |
| 1528 | |
| 1529 | fillSurfaceRGBA8(mChildNewClient, 200, 200, 200); |
| 1530 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1531 | asTransaction([&](Transaction& t) { |
| 1532 | t.hide(mChild); |
| 1533 | t.show(mChildNewClient); |
| 1534 | t.setPosition(mChildNewClient, 10, 10); |
| 1535 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1536 | }); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1537 | |
| 1538 | { |
| 1539 | ScreenCapture::captureScreen(&mCapture); |
| 1540 | // Top left of foreground must now be visible |
| 1541 | mCapture->expectFGColor(64, 64); |
| 1542 | // But 10 pixels in we should see the child surface |
| 1543 | mCapture->expectChildColor(74, 74); |
| 1544 | // And 10 more pixels we should be back to the foreground surface |
| 1545 | mCapture->expectFGColor(84, 84); |
| 1546 | } |
| 1547 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1548 | asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); }); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1549 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1550 | asTransaction([&](Transaction& t) { t.hide(mChildNewClient); }); |
chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 1551 | |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1552 | // Nothing should have changed. |
| 1553 | { |
| 1554 | ScreenCapture::captureScreen(&mCapture); |
| 1555 | mCapture->expectFGColor(64, 64); |
| 1556 | mCapture->expectChildColor(74, 74); |
| 1557 | mCapture->expectFGColor(84, 84); |
| 1558 | } |
| 1559 | } |
| 1560 | |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 1561 | TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1562 | asTransaction([&](Transaction& t) { |
| 1563 | t.show(mChild); |
| 1564 | t.setPosition(mChild, 0, 0); |
| 1565 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1566 | }); |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 1567 | |
| 1568 | { |
| 1569 | ScreenCapture::captureScreen(&mCapture); |
| 1570 | // We've positioned the child in the top left. |
| 1571 | mCapture->expectChildColor(0, 0); |
| 1572 | // But it's only 10x10. |
| 1573 | mCapture->expectFGColor(10, 10); |
| 1574 | } |
| 1575 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1576 | asTransaction([&](Transaction& t) { |
| 1577 | t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 1578 | // We cause scaling by 2. |
| 1579 | t.setSize(mFGSurfaceControl, 128, 128); |
| 1580 | }); |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 1581 | |
| 1582 | { |
| 1583 | ScreenCapture::captureScreen(&mCapture); |
| 1584 | // We've positioned the child in the top left. |
| 1585 | mCapture->expectChildColor(0, 0); |
| 1586 | mCapture->expectChildColor(10, 10); |
| 1587 | mCapture->expectChildColor(19, 19); |
| 1588 | // And now it should be scaled all the way to 20x20 |
| 1589 | mCapture->expectFGColor(20, 20); |
| 1590 | } |
| 1591 | } |
| 1592 | |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1593 | // Regression test for b/37673612 |
| 1594 | TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1595 | asTransaction([&](Transaction& t) { |
| 1596 | t.show(mChild); |
| 1597 | t.setPosition(mChild, 0, 0); |
| 1598 | t.setPosition(mFGSurfaceControl, 0, 0); |
| 1599 | }); |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1600 | |
| 1601 | { |
| 1602 | ScreenCapture::captureScreen(&mCapture); |
| 1603 | // We've positioned the child in the top left. |
| 1604 | mCapture->expectChildColor(0, 0); |
| 1605 | // But it's only 10x10. |
| 1606 | mCapture->expectFGColor(10, 10); |
| 1607 | } |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1608 | // We set things up as in b/37673612 so that there is a mismatch between the buffer size and |
| 1609 | // the WM specified state size. |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1610 | asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); }); |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 1611 | sp<Surface> s = mFGSurfaceControl->getSurface(); |
| 1612 | auto anw = static_cast<ANativeWindow*>(s.get()); |
| 1613 | native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90); |
| 1614 | native_window_set_buffers_dimensions(anw, 64, 128); |
| 1615 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); |
| 1616 | waitForPostedBuffers(); |
| 1617 | |
| 1618 | { |
| 1619 | // The child should still be in the same place and not have any strange scaling as in |
| 1620 | // b/37673612. |
| 1621 | ScreenCapture::captureScreen(&mCapture); |
| 1622 | mCapture->expectChildColor(0, 0); |
| 1623 | mCapture->expectFGColor(10, 10); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 1627 | TEST_F(ChildLayerTest, Bug36858924) { |
| 1628 | // Destroy the child layer |
| 1629 | mChild.clear(); |
| 1630 | |
| 1631 | // Now recreate it as hidden |
| 1632 | mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10, |
| 1633 | PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden, |
| 1634 | mFGSurfaceControl.get()); |
| 1635 | |
| 1636 | // Show the child layer in a deferred transaction |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1637 | asTransaction([&](Transaction& t) { |
| 1638 | t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(), |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1639 | mFGSurfaceControl->getSurface()->getNextFrameNumber()); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1640 | t.show(mChild); |
| 1641 | }); |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 1642 | |
| 1643 | // Render the foreground surface a few times |
| 1644 | // |
| 1645 | // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third |
| 1646 | // frame because SurfaceFlinger would never process the deferred transaction and would therefore |
| 1647 | // never acquire/release the first buffer |
| 1648 | ALOGI("Filling 1"); |
| 1649 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); |
| 1650 | ALOGI("Filling 2"); |
| 1651 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255); |
| 1652 | ALOGI("Filling 3"); |
| 1653 | fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0); |
| 1654 | ALOGI("Filling 4"); |
| 1655 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); |
| 1656 | } |
| 1657 | |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1658 | TEST_F(ChildLayerTest, Reparent) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1659 | asTransaction([&](Transaction& t) { |
| 1660 | t.show(mChild); |
| 1661 | t.setPosition(mChild, 10, 10); |
| 1662 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1663 | }); |
chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 1664 | |
| 1665 | { |
| 1666 | ScreenCapture::captureScreen(&mCapture); |
| 1667 | // Top left of foreground must now be visible |
| 1668 | mCapture->expectFGColor(64, 64); |
| 1669 | // But 10 pixels in we should see the child surface |
| 1670 | mCapture->expectChildColor(74, 74); |
| 1671 | // And 10 more pixels we should be back to the foreground surface |
| 1672 | mCapture->expectFGColor(84, 84); |
| 1673 | } |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1674 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1675 | asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1676 | |
chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 1677 | { |
| 1678 | ScreenCapture::captureScreen(&mCapture); |
| 1679 | mCapture->expectFGColor(64, 64); |
| 1680 | // In reparenting we should have exposed the entire foreground surface. |
| 1681 | mCapture->expectFGColor(74, 74); |
| 1682 | // And the child layer should now begin at 10, 10 (since the BG |
| 1683 | // layer is at (0, 0)). |
| 1684 | mCapture->expectBGColor(9, 9); |
| 1685 | mCapture->expectChildColor(10, 10); |
| 1686 | } |
| 1687 | } |
| 1688 | |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1689 | TEST_F(ChildLayerTest, ReparentToNoParent) { |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1690 | asTransaction([&](Transaction& t) { |
| 1691 | t.show(mChild); |
| 1692 | t.setPosition(mChild, 10, 10); |
| 1693 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1694 | }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1695 | |
| 1696 | { |
| 1697 | ScreenCapture::captureScreen(&mCapture); |
| 1698 | // Top left of foreground must now be visible |
| 1699 | mCapture->expectFGColor(64, 64); |
| 1700 | // But 10 pixels in we should see the child surface |
| 1701 | mCapture->expectChildColor(74, 74); |
| 1702 | // And 10 more pixels we should be back to the foreground surface |
| 1703 | mCapture->expectFGColor(84, 84); |
| 1704 | } |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1705 | asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1706 | { |
| 1707 | ScreenCapture::captureScreen(&mCapture); |
| 1708 | // Nothing should have changed. |
| 1709 | mCapture->expectFGColor(64, 64); |
| 1710 | mCapture->expectChildColor(74, 74); |
| 1711 | mCapture->expectFGColor(84, 84); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | TEST_F(ChildLayerTest, ReparentFromNoParent) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1716 | sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10, |
| 1717 | PIXEL_FORMAT_RGBA_8888, 0); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1718 | ASSERT_TRUE(newSurface != NULL); |
| 1719 | ASSERT_TRUE(newSurface->isValid()); |
| 1720 | |
| 1721 | fillSurfaceRGBA8(newSurface, 63, 195, 63); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1722 | asTransaction([&](Transaction& t) { |
| 1723 | t.hide(mChild); |
| 1724 | t.show(newSurface); |
| 1725 | t.setPosition(newSurface, 10, 10); |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1726 | t.setLayer(newSurface, INT32_MAX - 2); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1727 | t.setPosition(mFGSurfaceControl, 64, 64); |
| 1728 | }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1729 | |
| 1730 | { |
| 1731 | ScreenCapture::captureScreen(&mCapture); |
| 1732 | // Top left of foreground must now be visible |
| 1733 | mCapture->expectFGColor(64, 64); |
| 1734 | // At 10, 10 we should see the new surface |
| 1735 | mCapture->checkPixel(10, 10, 63, 195, 63); |
| 1736 | } |
| 1737 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1738 | asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); }); |
chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 1739 | |
| 1740 | { |
| 1741 | ScreenCapture::captureScreen(&mCapture); |
| 1742 | // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from |
| 1743 | // mFGSurface, putting it at 74, 74. |
| 1744 | mCapture->expectFGColor(64, 64); |
| 1745 | mCapture->checkPixel(74, 74, 63, 195, 63); |
| 1746 | mCapture->expectFGColor(84, 84); |
| 1747 | } |
| 1748 | } |
| 1749 | |
chaviw | c967433 | 2017-08-28 12:32:18 -0700 | [diff] [blame] | 1750 | TEST_F(ChildLayerTest, NestedChildren) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1751 | sp<SurfaceControl> grandchild = |
| 1752 | mComposerClient->createSurface(String8("Grandchild surface"), 10, 10, |
| 1753 | PIXEL_FORMAT_RGBA_8888, 0, mChild.get()); |
chaviw | c967433 | 2017-08-28 12:32:18 -0700 | [diff] [blame] | 1754 | fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 1755 | |
| 1756 | { |
| 1757 | ScreenCapture::captureScreen(&mCapture); |
| 1758 | // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer |
| 1759 | // which begins at 64, 64 |
| 1760 | mCapture->checkPixel(64, 64, 50, 50, 50); |
| 1761 | } |
| 1762 | } |
| 1763 | |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 1764 | TEST_F(ChildLayerTest, ChildLayerRelativeLayer) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1765 | sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128, |
| 1766 | 128, PIXEL_FORMAT_RGBA_8888, 0); |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 1767 | fillSurfaceRGBA8(relative, 255, 255, 255); |
| 1768 | |
| 1769 | Transaction t; |
| 1770 | t.setLayer(relative, INT32_MAX) |
| 1771 | .setRelativeLayer(mChild, relative->getHandle(), 1) |
| 1772 | .setPosition(mFGSurfaceControl, 0, 0) |
| 1773 | .apply(true); |
| 1774 | |
| 1775 | // We expect that the child should have been elevated above our |
| 1776 | // INT_MAX layer even though it's not a child of it. |
| 1777 | { |
| 1778 | ScreenCapture::captureScreen(&mCapture); |
| 1779 | mCapture->expectChildColor(0, 0); |
| 1780 | mCapture->expectChildColor(9, 9); |
| 1781 | mCapture->checkPixel(10, 10, 255, 255, 255); |
| 1782 | } |
| 1783 | } |
| 1784 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1785 | class LayerColorTest : public LayerUpdateTest { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1786 | protected: |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1787 | void SetUp() override { |
| 1788 | LayerUpdateTest::SetUp(); |
| 1789 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1790 | mLayerColorControl = |
| 1791 | mComposerClient->createSurface(String8("Layer color surface"), 128, 128, |
| 1792 | PIXEL_FORMAT_RGBA_8888, |
| 1793 | ISurfaceComposerClient::eFXSurfaceColor); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1794 | |
| 1795 | ASSERT_TRUE(mLayerColorControl != NULL); |
| 1796 | ASSERT_TRUE(mLayerColorControl->isValid()); |
| 1797 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1798 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1799 | t.setLayer(mLayerColorControl, INT32_MAX - 1); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1800 | t.setPosition(mLayerColorControl, 140, 140); |
| 1801 | t.hide(mLayerColorControl); |
| 1802 | t.hide(mFGSurfaceControl); |
| 1803 | }); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | void TearDown() override { |
| 1807 | LayerUpdateTest::TearDown(); |
| 1808 | mLayerColorControl = 0; |
| 1809 | } |
| 1810 | |
| 1811 | sp<SurfaceControl> mLayerColorControl; |
| 1812 | }; |
| 1813 | |
| 1814 | TEST_F(LayerColorTest, ColorLayerNoAlpha) { |
| 1815 | sp<ScreenCapture> sc; |
| 1816 | |
| 1817 | { |
| 1818 | SCOPED_TRACE("before setColor"); |
| 1819 | ScreenCapture::captureScreen(&sc); |
| 1820 | sc->expectBGColor(145, 145); |
| 1821 | } |
| 1822 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1823 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1824 | 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] | 1825 | t.setColor(mLayerColorControl, color); |
| 1826 | t.show(mLayerColorControl); |
| 1827 | }); |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1828 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1829 | { |
| 1830 | // There should now be a color |
| 1831 | SCOPED_TRACE("after setColor"); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1832 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1833 | ScreenCapture::captureScreen(&sc); |
| 1834 | sc->checkPixel(145, 145, 43, 207, 131); |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | TEST_F(LayerColorTest, ColorLayerWithAlpha) { |
| 1839 | sp<ScreenCapture> sc; |
| 1840 | { |
| 1841 | SCOPED_TRACE("before setColor"); |
| 1842 | ScreenCapture::captureScreen(&sc); |
| 1843 | sc->expectBGColor(145, 145); |
| 1844 | } |
| 1845 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1846 | asTransaction([&](Transaction& t) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1847 | 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] | 1848 | t.setColor(mLayerColorControl, color); |
| 1849 | t.setAlpha(mLayerColorControl, .75f); |
| 1850 | t.show(mLayerColorControl); |
| 1851 | }); |
| 1852 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1853 | { |
| 1854 | // There should now be a color with .75 alpha |
| 1855 | SCOPED_TRACE("after setColor"); |
| 1856 | ScreenCapture::captureScreen(&sc); |
| 1857 | sc->checkPixel(145, 145, 48, 171, 147); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | TEST_F(LayerColorTest, ColorLayerWithNoColor) { |
| 1862 | sp<ScreenCapture> sc; |
| 1863 | { |
| 1864 | SCOPED_TRACE("before setColor"); |
| 1865 | ScreenCapture::captureScreen(&sc); |
| 1866 | sc->expectBGColor(145, 145); |
| 1867 | } |
| 1868 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1869 | asTransaction([&](Transaction& t) { t.show(mLayerColorControl); }); |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1870 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1871 | { |
| 1872 | // There should now be set to 0,0,0 (black) as default. |
| 1873 | SCOPED_TRACE("after setColor"); |
| 1874 | ScreenCapture::captureScreen(&sc); |
| 1875 | sc->checkPixel(145, 145, 0, 0, 0); |
| 1876 | } |
| 1877 | } |
| 1878 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1879 | class ScreenCaptureTest : public LayerUpdateTest { |
| 1880 | protected: |
| 1881 | std::unique_ptr<CaptureLayer> mCapture; |
| 1882 | }; |
| 1883 | |
| 1884 | TEST_F(ScreenCaptureTest, CaptureSingleLayer) { |
| 1885 | auto bgHandle = mBGSurfaceControl->getHandle(); |
| 1886 | CaptureLayer::captureScreen(&mCapture, bgHandle); |
| 1887 | mCapture->expectBGColor(0, 0); |
| 1888 | // Doesn't capture FG layer which is at 64, 64 |
| 1889 | mCapture->expectBGColor(64, 64); |
| 1890 | } |
| 1891 | |
| 1892 | TEST_F(ScreenCaptureTest, CaptureLayerWithChild) { |
| 1893 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 1894 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1895 | sp<SurfaceControl> child = |
| 1896 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 1897 | 0, mFGSurfaceControl.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1898 | fillSurfaceRGBA8(child, 200, 200, 200); |
| 1899 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1900 | SurfaceComposerClient::Transaction().show(child).apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1901 | |
| 1902 | // Captures mFGSurfaceControl layer and its child. |
| 1903 | CaptureLayer::captureScreen(&mCapture, fgHandle); |
| 1904 | mCapture->expectFGColor(10, 10); |
| 1905 | mCapture->expectChildColor(0, 0); |
| 1906 | } |
| 1907 | |
| 1908 | TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) { |
| 1909 | auto fgHandle = mFGSurfaceControl->getHandle(); |
| 1910 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1911 | sp<SurfaceControl> child = |
| 1912 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 1913 | 0, mFGSurfaceControl.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1914 | fillSurfaceRGBA8(child, 200, 200, 200); |
| 1915 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1916 | sp<SurfaceControl> grandchild = |
| 1917 | mComposerClient->createSurface(String8("Grandchild surface"), 5, 5, |
| 1918 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1919 | |
| 1920 | fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 1921 | SurfaceComposerClient::Transaction() |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1922 | .show(child) |
| 1923 | .setPosition(grandchild, 5, 5) |
| 1924 | .show(grandchild) |
| 1925 | .apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1926 | |
| 1927 | // Captures mFGSurfaceControl, its child, and the grandchild. |
| 1928 | CaptureLayer::captureScreen(&mCapture, fgHandle); |
| 1929 | mCapture->expectFGColor(10, 10); |
| 1930 | mCapture->expectChildColor(0, 0); |
| 1931 | mCapture->checkPixel(5, 5, 50, 50, 50); |
| 1932 | } |
| 1933 | |
| 1934 | TEST_F(ScreenCaptureTest, CaptureChildOnly) { |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1935 | sp<SurfaceControl> child = |
| 1936 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 1937 | 0, mFGSurfaceControl.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1938 | fillSurfaceRGBA8(child, 200, 200, 200); |
| 1939 | auto childHandle = child->getHandle(); |
| 1940 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1941 | SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1942 | |
| 1943 | // Captures only the child layer, and not the parent. |
| 1944 | CaptureLayer::captureScreen(&mCapture, childHandle); |
| 1945 | mCapture->expectChildColor(0, 0); |
| 1946 | mCapture->expectChildColor(9, 9); |
| 1947 | } |
| 1948 | |
| 1949 | TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) { |
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 | auto childHandle = child->getHandle(); |
| 1955 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1956 | sp<SurfaceControl> grandchild = |
| 1957 | mComposerClient->createSurface(String8("Grandchild surface"), 5, 5, |
| 1958 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1959 | fillSurfaceRGBA8(grandchild, 50, 50, 50); |
| 1960 | |
| 1961 | SurfaceComposerClient::Transaction() |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1962 | .show(child) |
| 1963 | .setPosition(grandchild, 5, 5) |
| 1964 | .show(grandchild) |
| 1965 | .apply(true); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 1966 | |
| 1967 | auto grandchildHandle = grandchild->getHandle(); |
| 1968 | |
| 1969 | // Captures only the grandchild. |
| 1970 | CaptureLayer::captureScreen(&mCapture, grandchildHandle); |
| 1971 | mCapture->checkPixel(0, 0, 50, 50, 50); |
| 1972 | mCapture->checkPixel(4, 4, 50, 50, 50); |
| 1973 | } |
| 1974 | |
Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1975 | } // namespace android |