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