| 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 | 93853fe | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 53 | static const Color WHITE; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 54 | static const Color BLACK; | 
| Chia-I Wu | 2113bdd | 2017-11-01 15:16:35 -0700 | [diff] [blame] | 55 | static const Color TRANSPARENT; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
|  | 58 | const Color Color::RED{255, 0, 0, 255}; | 
| Chia-I Wu | 0ea0f82 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 59 | const Color Color::GREEN{0, 255, 0, 255}; | 
| Chia-I Wu | 4931330 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 60 | const Color Color::BLUE{0, 0, 255, 255}; | 
| Chia-I Wu | 93853fe | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 61 | const Color Color::WHITE{255, 255, 255, 255}; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 62 | const Color Color::BLACK{0, 0, 0, 255}; | 
| Chia-I Wu | 2113bdd | 2017-11-01 15:16:35 -0700 | [diff] [blame] | 63 | const Color Color::TRANSPARENT{0, 0, 0, 0}; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | std::ostream& operator<<(std::ostream& os, const Color& color) { | 
|  | 66 | os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a); | 
|  | 67 | return os; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | // Fill a region with the specified color. | 
|  | 71 | void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) { | 
|  | 72 | int32_t x = rect.left; | 
|  | 73 | int32_t y = rect.top; | 
|  | 74 | int32_t width = rect.right - rect.left; | 
|  | 75 | int32_t height = rect.bottom - rect.top; | 
|  | 76 |  | 
|  | 77 | if (x < 0) { | 
|  | 78 | width += x; | 
|  | 79 | x = 0; | 
|  | 80 | } | 
|  | 81 | if (y < 0) { | 
|  | 82 | height += y; | 
|  | 83 | y = 0; | 
|  | 84 | } | 
|  | 85 | if (x + width > buffer.width) { | 
|  | 86 | x = std::min(x, buffer.width); | 
|  | 87 | width = buffer.width - x; | 
|  | 88 | } | 
|  | 89 | if (y + height > buffer.height) { | 
|  | 90 | y = std::min(y, buffer.height); | 
|  | 91 | height = buffer.height - y; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | for (int32_t j = 0; j < height; j++) { | 
|  | 95 | uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4; | 
|  | 96 | for (int32_t i = 0; i < width; i++) { | 
|  | 97 | dst[0] = color.r; | 
|  | 98 | dst[1] = color.g; | 
|  | 99 | dst[2] = color.b; | 
|  | 100 | dst[3] = color.a; | 
|  | 101 | dst += 4; | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | // Check if a region has the specified color. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 107 | void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect, | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 108 | const Color& color, uint8_t tolerance) { | 
|  | 109 | int32_t x = rect.left; | 
|  | 110 | int32_t y = rect.top; | 
|  | 111 | int32_t width = rect.right - rect.left; | 
|  | 112 | int32_t height = rect.bottom - rect.top; | 
|  | 113 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 114 | int32_t bufferWidth = int32_t(outBuffer->getWidth()); | 
|  | 115 | int32_t bufferHeight = int32_t(outBuffer->getHeight()); | 
|  | 116 | if (x + width > bufferWidth) { | 
|  | 117 | x = std::min(x, bufferWidth); | 
|  | 118 | width = bufferWidth - x; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 119 | } | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 120 | if (y + height > bufferHeight) { | 
|  | 121 | y = std::min(y, bufferHeight); | 
|  | 122 | height = bufferHeight - y; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
|  | 125 | auto colorCompare = [tolerance](uint8_t a, uint8_t b) { | 
|  | 126 | uint8_t tmp = a >= b ? a - b : b - a; | 
|  | 127 | return tmp <= tolerance; | 
|  | 128 | }; | 
|  | 129 | for (int32_t j = 0; j < height; j++) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 130 | const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 131 | for (int32_t i = 0; i < width; i++) { | 
|  | 132 | const uint8_t expected[4] = {color.r, color.g, color.b, color.a}; | 
|  | 133 | EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare)) | 
|  | 134 | << "pixel @ (" << x + i << ", " << y + j << "): " | 
|  | 135 | << "expected (" << color << "), " | 
|  | 136 | << "got (" << Color{src[0], src[1], src[2], src[3]} << ")"; | 
|  | 137 | src += 4; | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | } // anonymous namespace | 
|  | 143 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 144 | using Transaction = SurfaceComposerClient::Transaction; | 
|  | 145 |  | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 146 | // Fill an RGBA_8888 formatted surface with a single color. | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 147 | static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b, | 
|  | 148 | bool unlock = true) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 149 | ANativeWindow_Buffer outBuffer; | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 150 | sp<Surface> s = sc->getSurface(); | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 151 | ASSERT_TRUE(s != nullptr); | 
|  | 152 | ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 153 | uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits); | 
| Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 154 | for (int y = 0; y < outBuffer.height; y++) { | 
|  | 155 | for (int x = 0; x < outBuffer.width; x++) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 156 | uint8_t* pixel = img + (4 * (y * outBuffer.stride + x)); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 157 | pixel[0] = r; | 
|  | 158 | pixel[1] = g; | 
|  | 159 | pixel[2] = b; | 
|  | 160 | pixel[3] = 255; | 
|  | 161 | } | 
|  | 162 | } | 
| Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 163 | if (unlock) { | 
|  | 164 | ASSERT_EQ(NO_ERROR, s->unlockAndPost()); | 
|  | 165 | } | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 | // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check | 
|  | 169 | // individual pixel values for testing purposes. | 
|  | 170 | class ScreenCapture : public RefBase { | 
|  | 171 | public: | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 172 | static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0, | 
|  | 173 | int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) { | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 174 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 175 | sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 176 | SurfaceComposerClient::Transaction().apply(true); | 
|  | 177 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 178 | sp<GraphicBuffer> outBuffer; | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 179 | ASSERT_EQ(NO_ERROR, | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 180 | sf->captureScreen(display, &outBuffer, Rect(), 0, 0, minLayerZ, maxLayerZ, | 
|  | 181 | false)); | 
|  | 182 | *sc = new ScreenCapture(outBuffer); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle, | 
|  | 186 | Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) { | 
|  | 187 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); | 
|  | 188 | SurfaceComposerClient::Transaction().apply(true); | 
|  | 189 |  | 
|  | 190 | sp<GraphicBuffer> outBuffer; | 
|  | 191 | ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale)); | 
|  | 192 | *sc = std::make_unique<ScreenCapture>(outBuffer); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 195 | static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle, | 
|  | 196 | Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) { | 
|  | 197 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); | 
|  | 198 | SurfaceComposerClient::Transaction().apply(true); | 
|  | 199 |  | 
|  | 200 | sp<GraphicBuffer> outBuffer; | 
|  | 201 | ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true)); | 
|  | 202 | *sc = std::make_unique<ScreenCapture>(outBuffer); | 
|  | 203 | } | 
|  | 204 |  | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 205 | void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 206 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); | 
|  | 207 | expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance); | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
|  | 210 | void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 211 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 212 | const bool leftBorder = rect.left > 0; | 
|  | 213 | const bool topBorder = rect.top > 0; | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 214 | const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth()); | 
|  | 215 | const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight()); | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 216 |  | 
|  | 217 | if (topBorder) { | 
|  | 218 | Rect top(rect.left, rect.top - 1, rect.right, rect.top); | 
|  | 219 | if (leftBorder) { | 
|  | 220 | top.left -= 1; | 
|  | 221 | } | 
|  | 222 | if (rightBorder) { | 
|  | 223 | top.right += 1; | 
|  | 224 | } | 
|  | 225 | expectColor(top, color, tolerance); | 
|  | 226 | } | 
|  | 227 | if (leftBorder) { | 
|  | 228 | Rect left(rect.left - 1, rect.top, rect.left, rect.bottom); | 
|  | 229 | expectColor(left, color, tolerance); | 
|  | 230 | } | 
|  | 231 | if (rightBorder) { | 
|  | 232 | Rect right(rect.right, rect.top, rect.right + 1, rect.bottom); | 
|  | 233 | expectColor(right, color, tolerance); | 
|  | 234 | } | 
|  | 235 | if (bottomBorder) { | 
|  | 236 | Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1); | 
|  | 237 | if (leftBorder) { | 
|  | 238 | bottom.left -= 1; | 
|  | 239 | } | 
|  | 240 | if (rightBorder) { | 
|  | 241 | bottom.right += 1; | 
|  | 242 | } | 
|  | 243 | expectColor(bottom, color, tolerance); | 
|  | 244 | } | 
|  | 245 | } | 
|  | 246 |  | 
| Chia-I Wu | 93853fe | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 247 | void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight, | 
|  | 248 | const Color& bottomLeft, const Color& bottomRight, bool filtered = false, | 
|  | 249 | uint8_t tolerance = 0) { | 
|  | 250 | ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0); | 
|  | 251 |  | 
|  | 252 | const int32_t centerX = rect.left + (rect.right - rect.left) / 2; | 
|  | 253 | const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2; | 
|  | 254 | // avoid checking borders due to unspecified filtering behavior | 
|  | 255 | const int32_t offsetX = filtered ? 2 : 0; | 
|  | 256 | const int32_t offsetY = filtered ? 2 : 0; | 
|  | 257 | expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft, | 
|  | 258 | tolerance); | 
|  | 259 | expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight, | 
|  | 260 | tolerance); | 
|  | 261 | expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft, | 
|  | 262 | tolerance); | 
|  | 263 | expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom), | 
|  | 264 | bottomRight, tolerance); | 
|  | 265 | } | 
|  | 266 |  | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 267 | void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 268 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat()); | 
|  | 269 | const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x)); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 270 | if (r != pixel[0] || g != pixel[1] || b != pixel[2]) { | 
|  | 271 | String8 err(String8::format("pixel @ (%3d, %3d): " | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 272 | "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]", | 
|  | 273 | x, y, r, g, b, pixel[0], pixel[1], pixel[2])); | 
| Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 274 | EXPECT_EQ(String8(), err) << err.string(); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 275 | } | 
|  | 276 | } | 
|  | 277 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 278 | 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] | 279 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 280 | 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] | 281 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 282 | 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] | 283 |  | 
| Chih-Hung Hsieh | 2274904 | 2018-12-20 15:50:39 -0800 | [diff] [blame] | 284 | explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 285 | mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels)); | 
| Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 286 | } | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 287 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 288 | ~ScreenCapture() { mOutBuffer->unlock(); } | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 289 |  | 
|  | 290 | private: | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 291 | sp<GraphicBuffer> mOutBuffer; | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 292 | uint8_t* mPixels = nullptr; | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 293 | }; | 
|  | 294 |  | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 295 | class LayerTransactionTest : public ::testing::Test { | 
|  | 296 | protected: | 
|  | 297 | void SetUp() override { | 
|  | 298 | mClient = new SurfaceComposerClient; | 
|  | 299 | ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient"; | 
|  | 300 |  | 
|  | 301 | ASSERT_NO_FATAL_FAILURE(SetUpDisplay()); | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height, | 
|  | 305 | uint32_t flags = 0) { | 
|  | 306 | auto layer = | 
|  | 307 | mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags); | 
|  | 308 | EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl"; | 
|  | 309 |  | 
|  | 310 | status_t error = Transaction() | 
|  | 311 | .setLayerStack(layer, mDisplayLayerStack) | 
|  | 312 | .setLayer(layer, mLayerZBase) | 
|  | 313 | .apply(); | 
|  | 314 | if (error != NO_ERROR) { | 
|  | 315 | ADD_FAILURE() << "failed to initialize SurfaceControl"; | 
|  | 316 | layer.clear(); | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | return layer; | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) { | 
|  | 323 | // wait for previous transactions (such as setSize) to complete | 
|  | 324 | Transaction().apply(true); | 
|  | 325 |  | 
|  | 326 | ANativeWindow_Buffer buffer = {}; | 
|  | 327 | EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr)); | 
|  | 328 |  | 
|  | 329 | return buffer; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | void postLayerBuffer(const sp<SurfaceControl>& layer) { | 
|  | 333 | ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost()); | 
|  | 334 |  | 
|  | 335 | // wait for the newly posted buffer to be latched | 
|  | 336 | waitForLayerBuffers(); | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) { | 
|  | 340 | ANativeWindow_Buffer buffer; | 
|  | 341 | ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer)); | 
|  | 342 | fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color); | 
|  | 343 | postLayerBuffer(layer); | 
|  | 344 | } | 
|  | 345 |  | 
| Chia-I Wu | 93853fe | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 346 | void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft, | 
|  | 347 | const Color& topRight, const Color& bottomLeft, | 
|  | 348 | const Color& bottomRight) { | 
|  | 349 | ANativeWindow_Buffer buffer; | 
|  | 350 | ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer)); | 
|  | 351 | ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0); | 
|  | 352 |  | 
|  | 353 | const int32_t halfW = buffer.width / 2; | 
|  | 354 | const int32_t halfH = buffer.height / 2; | 
|  | 355 | fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft); | 
|  | 356 | fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight); | 
|  | 357 | fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft); | 
|  | 358 | fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight); | 
|  | 359 |  | 
|  | 360 | postLayerBuffer(layer); | 
|  | 361 | } | 
|  | 362 |  | 
| Chia-I Wu | 718daf8 | 2017-10-20 11:57:17 -0700 | [diff] [blame] | 363 | sp<ScreenCapture> screenshot() { | 
|  | 364 | sp<ScreenCapture> screenshot; | 
|  | 365 | ScreenCapture::captureScreen(&screenshot, mLayerZBase); | 
|  | 366 | return screenshot; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | sp<SurfaceComposerClient> mClient; | 
|  | 370 |  | 
|  | 371 | sp<IBinder> mDisplay; | 
|  | 372 | uint32_t mDisplayWidth; | 
|  | 373 | uint32_t mDisplayHeight; | 
|  | 374 | uint32_t mDisplayLayerStack; | 
|  | 375 |  | 
|  | 376 | // leave room for ~256 layers | 
|  | 377 | const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256; | 
|  | 378 |  | 
|  | 379 | private: | 
|  | 380 | void SetUpDisplay() { | 
|  | 381 | mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain); | 
|  | 382 | ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display"; | 
|  | 383 |  | 
|  | 384 | // get display width/height | 
|  | 385 | DisplayInfo info; | 
|  | 386 | SurfaceComposerClient::getDisplayInfo(mDisplay, &info); | 
|  | 387 | mDisplayWidth = info.w; | 
|  | 388 | mDisplayHeight = info.h; | 
|  | 389 |  | 
|  | 390 | // After a new buffer is queued, SurfaceFlinger is notified and will | 
|  | 391 | // latch the new buffer on next vsync.  Let's heuristically wait for 3 | 
|  | 392 | // vsyncs. | 
|  | 393 | mBufferPostDelay = int32_t(1e6 / info.fps) * 3; | 
|  | 394 |  | 
|  | 395 | mDisplayLayerStack = 0; | 
|  | 396 | // set layer stack (b/68888219) | 
|  | 397 | Transaction t; | 
|  | 398 | t.setDisplayLayerStack(mDisplay, mDisplayLayerStack); | 
|  | 399 | t.apply(); | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | void waitForLayerBuffers() { usleep(mBufferPostDelay); } | 
|  | 403 |  | 
|  | 404 | int32_t mBufferPostDelay; | 
|  | 405 | }; | 
|  | 406 |  | 
|  | 407 | TEST_F(LayerTransactionTest, SetPositionBasic) { | 
|  | 408 | sp<SurfaceControl> layer; | 
|  | 409 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 410 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 411 |  | 
|  | 412 | { | 
|  | 413 | SCOPED_TRACE("default position"); | 
|  | 414 | auto shot = screenshot(); | 
|  | 415 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 416 | shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | Transaction().setPosition(layer, 5, 10).apply(); | 
|  | 420 | { | 
|  | 421 | SCOPED_TRACE("new position"); | 
|  | 422 | auto shot = screenshot(); | 
|  | 423 | shot->expectColor(Rect(5, 10, 37, 42), Color::RED); | 
|  | 424 | shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK); | 
|  | 425 | } | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | TEST_F(LayerTransactionTest, SetPositionRounding) { | 
|  | 429 | sp<SurfaceControl> layer; | 
|  | 430 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 431 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 432 |  | 
|  | 433 | // GLES requires only 4 bits of subpixel precision during rasterization | 
|  | 434 | // XXX GLES composition does not match HWC composition due to precision | 
|  | 435 | // loss (b/69315223) | 
|  | 436 | const float epsilon = 1.0f / 16.0f; | 
|  | 437 | Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply(); | 
|  | 438 | { | 
|  | 439 | SCOPED_TRACE("rounding down"); | 
|  | 440 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 | Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply(); | 
|  | 444 | { | 
|  | 445 | SCOPED_TRACE("rounding up"); | 
|  | 446 | screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED); | 
|  | 447 | } | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | TEST_F(LayerTransactionTest, SetPositionOutOfBounds) { | 
|  | 451 | sp<SurfaceControl> layer; | 
|  | 452 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 453 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 454 |  | 
|  | 455 | Transaction().setPosition(layer, -32, -32).apply(); | 
|  | 456 | { | 
|  | 457 | SCOPED_TRACE("negative coordinates"); | 
|  | 458 | screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK); | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply(); | 
|  | 462 | { | 
|  | 463 | SCOPED_TRACE("positive coordinates"); | 
|  | 464 | screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK); | 
|  | 465 | } | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) { | 
|  | 469 | sp<SurfaceControl> layer; | 
|  | 470 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 471 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 472 |  | 
|  | 473 | // partially out of bounds | 
|  | 474 | Transaction().setPosition(layer, -30, -30).apply(); | 
|  | 475 | { | 
|  | 476 | SCOPED_TRACE("negative coordinates"); | 
|  | 477 | screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED); | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply(); | 
|  | 481 | { | 
|  | 482 | SCOPED_TRACE("positive coordinates"); | 
|  | 483 | screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth, | 
|  | 484 | mDisplayHeight), | 
|  | 485 | Color::RED); | 
|  | 486 | } | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | TEST_F(LayerTransactionTest, SetPositionWithResize) { | 
|  | 490 | sp<SurfaceControl> layer; | 
|  | 491 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 492 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 493 |  | 
|  | 494 | // setPosition is applied immediately by default, with or without resize | 
|  | 495 | // pending | 
|  | 496 | Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply(); | 
|  | 497 | { | 
|  | 498 | SCOPED_TRACE("resize pending"); | 
|  | 499 | auto shot = screenshot(); | 
|  | 500 | shot->expectColor(Rect(5, 10, 37, 42), Color::RED); | 
|  | 501 | shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK); | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 505 | { | 
|  | 506 | SCOPED_TRACE("resize applied"); | 
|  | 507 | screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED); | 
|  | 508 | } | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | TEST_F(LayerTransactionTest, SetPositionWithNextResize) { | 
|  | 512 | sp<SurfaceControl> layer; | 
|  | 513 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 514 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 515 |  | 
|  | 516 | // request setPosition to be applied with the next resize | 
|  | 517 | Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply(); | 
|  | 518 | { | 
|  | 519 | SCOPED_TRACE("new position pending"); | 
|  | 520 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | Transaction().setPosition(layer, 15, 20).apply(); | 
|  | 524 | { | 
|  | 525 | SCOPED_TRACE("pending new position modified"); | 
|  | 526 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | Transaction().setSize(layer, 64, 64).apply(); | 
|  | 530 | { | 
|  | 531 | SCOPED_TRACE("resize pending"); | 
|  | 532 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 533 | } | 
|  | 534 |  | 
|  | 535 | // finally resize and latch the buffer | 
|  | 536 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 537 | { | 
|  | 538 | SCOPED_TRACE("new position applied"); | 
|  | 539 | screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED); | 
|  | 540 | } | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) { | 
|  | 544 | sp<SurfaceControl> layer; | 
|  | 545 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 546 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 547 |  | 
|  | 548 | // setPosition is not immediate even with SCALE_TO_WINDOW override | 
|  | 549 | Transaction() | 
|  | 550 | .setPosition(layer, 5, 10) | 
|  | 551 | .setSize(layer, 64, 64) | 
|  | 552 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) | 
|  | 553 | .setGeometryAppliesWithResize(layer) | 
|  | 554 | .apply(); | 
|  | 555 | { | 
|  | 556 | SCOPED_TRACE("new position pending"); | 
|  | 557 | screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED); | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 561 | { | 
|  | 562 | SCOPED_TRACE("new position applied"); | 
|  | 563 | screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED); | 
|  | 564 | } | 
|  | 565 | } | 
|  | 566 |  | 
| Chia-I Wu | 0eaea31 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 567 | TEST_F(LayerTransactionTest, SetSizeBasic) { | 
|  | 568 | sp<SurfaceControl> layer; | 
|  | 569 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 570 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 571 |  | 
|  | 572 | Transaction().setSize(layer, 64, 64).apply(); | 
|  | 573 | { | 
|  | 574 | SCOPED_TRACE("resize pending"); | 
|  | 575 | auto shot = screenshot(); | 
|  | 576 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 577 | shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 581 | { | 
|  | 582 | SCOPED_TRACE("resize applied"); | 
|  | 583 | auto shot = screenshot(); | 
|  | 584 | shot->expectColor(Rect(0, 0, 64, 64), Color::RED); | 
|  | 585 | shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK); | 
|  | 586 | } | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | TEST_F(LayerTransactionTest, SetSizeInvalid) { | 
|  | 590 | // cannot test robustness against invalid sizes (zero or really huge) | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 | TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) { | 
|  | 594 | sp<SurfaceControl> layer; | 
|  | 595 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 596 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 597 |  | 
|  | 598 | // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition | 
|  | 599 | Transaction() | 
|  | 600 | .setSize(layer, 64, 64) | 
|  | 601 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) | 
|  | 602 | .apply(); | 
|  | 603 | screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED); | 
|  | 604 | } | 
|  | 605 |  | 
| Chia-I Wu | 0ea0f82 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 606 | TEST_F(LayerTransactionTest, SetZBasic) { | 
|  | 607 | sp<SurfaceControl> layerR; | 
|  | 608 | sp<SurfaceControl> layerG; | 
|  | 609 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 610 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED)); | 
|  | 611 | ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32)); | 
|  | 612 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN)); | 
|  | 613 |  | 
|  | 614 | Transaction().setLayer(layerR, mLayerZBase + 1).apply(); | 
|  | 615 | { | 
|  | 616 | SCOPED_TRACE("layerR"); | 
|  | 617 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | Transaction().setLayer(layerG, mLayerZBase + 2).apply(); | 
|  | 621 | { | 
|  | 622 | SCOPED_TRACE("layerG"); | 
|  | 623 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN); | 
|  | 624 | } | 
|  | 625 | } | 
|  | 626 |  | 
|  | 627 | TEST_F(LayerTransactionTest, SetZNegative) { | 
|  | 628 | sp<SurfaceControl> layerR; | 
|  | 629 | sp<SurfaceControl> layerG; | 
|  | 630 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 631 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED)); | 
|  | 632 | ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32)); | 
|  | 633 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN)); | 
|  | 634 |  | 
|  | 635 | Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply(); | 
|  | 636 | { | 
|  | 637 | SCOPED_TRACE("layerR"); | 
|  | 638 | sp<ScreenCapture> screenshot; | 
|  | 639 | ScreenCapture::captureScreen(&screenshot, -2, -1); | 
|  | 640 | screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 641 | } | 
|  | 642 |  | 
|  | 643 | Transaction().setLayer(layerR, -3).apply(); | 
|  | 644 | { | 
|  | 645 | SCOPED_TRACE("layerG"); | 
|  | 646 | sp<ScreenCapture> screenshot; | 
|  | 647 | ScreenCapture::captureScreen(&screenshot, -3, -1); | 
|  | 648 | screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN); | 
|  | 649 | } | 
|  | 650 | } | 
|  | 651 |  | 
| Chia-I Wu | 4931330 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 652 | TEST_F(LayerTransactionTest, SetRelativeZBasic) { | 
|  | 653 | sp<SurfaceControl> layerR; | 
|  | 654 | sp<SurfaceControl> layerG; | 
|  | 655 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 656 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED)); | 
|  | 657 | ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32)); | 
|  | 658 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN)); | 
|  | 659 |  | 
|  | 660 | Transaction() | 
|  | 661 | .setPosition(layerG, 16, 16) | 
|  | 662 | .setRelativeLayer(layerG, layerR->getHandle(), 1) | 
|  | 663 | .apply(); | 
|  | 664 | { | 
|  | 665 | SCOPED_TRACE("layerG above"); | 
|  | 666 | auto shot = screenshot(); | 
|  | 667 | shot->expectColor(Rect(0, 0, 16, 16), Color::RED); | 
|  | 668 | shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN); | 
|  | 669 | } | 
|  | 670 |  | 
|  | 671 | Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply(); | 
|  | 672 | { | 
|  | 673 | SCOPED_TRACE("layerG below"); | 
|  | 674 | auto shot = screenshot(); | 
|  | 675 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 676 | shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN); | 
|  | 677 | } | 
|  | 678 | } | 
|  | 679 |  | 
| Chia-I Wu | ec2d985 | 2017-11-21 09:21:01 -0800 | [diff] [blame] | 680 | TEST_F(LayerTransactionTest, SetRelativeZNegative) { | 
|  | 681 | sp<SurfaceControl> layerR; | 
|  | 682 | sp<SurfaceControl> layerG; | 
|  | 683 | sp<SurfaceControl> layerB; | 
|  | 684 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 685 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED)); | 
|  | 686 | ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32)); | 
|  | 687 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN)); | 
|  | 688 | ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32)); | 
|  | 689 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE)); | 
|  | 690 |  | 
|  | 691 | // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2 | 
|  | 692 | Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply(); | 
|  | 693 |  | 
|  | 694 | sp<ScreenCapture> screenshot; | 
|  | 695 | // only layerB is in this range | 
|  | 696 | ScreenCapture::captureScreen(&screenshot, -2, -1); | 
|  | 697 | screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE); | 
|  | 698 | } | 
|  | 699 |  | 
| Chia-I Wu | 4931330 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 700 | TEST_F(LayerTransactionTest, SetRelativeZGroup) { | 
|  | 701 | sp<SurfaceControl> layerR; | 
|  | 702 | sp<SurfaceControl> layerG; | 
|  | 703 | sp<SurfaceControl> layerB; | 
|  | 704 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 705 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED)); | 
|  | 706 | ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32)); | 
|  | 707 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN)); | 
|  | 708 | ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32)); | 
|  | 709 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE)); | 
|  | 710 |  | 
|  | 711 | // layerR = 0, layerG = layerR + 3, layerB = 2 | 
|  | 712 | Transaction() | 
|  | 713 | .setPosition(layerG, 8, 8) | 
|  | 714 | .setRelativeLayer(layerG, layerR->getHandle(), 3) | 
|  | 715 | .setPosition(layerB, 16, 16) | 
|  | 716 | .setLayer(layerB, mLayerZBase + 2) | 
|  | 717 | .apply(); | 
|  | 718 | { | 
|  | 719 | SCOPED_TRACE("(layerR < layerG) < layerB"); | 
|  | 720 | auto shot = screenshot(); | 
|  | 721 | shot->expectColor(Rect(0, 0, 8, 8), Color::RED); | 
|  | 722 | shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN); | 
|  | 723 | shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE); | 
|  | 724 | } | 
|  | 725 |  | 
|  | 726 | // layerR = 4, layerG = layerR + 3, layerB = 2 | 
|  | 727 | Transaction().setLayer(layerR, mLayerZBase + 4).apply(); | 
|  | 728 | { | 
|  | 729 | SCOPED_TRACE("layerB < (layerR < layerG)"); | 
|  | 730 | auto shot = screenshot(); | 
|  | 731 | shot->expectColor(Rect(0, 0, 8, 8), Color::RED); | 
|  | 732 | shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN); | 
|  | 733 | shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE); | 
|  | 734 | } | 
|  | 735 |  | 
|  | 736 | // layerR = 4, layerG = layerR - 3, layerB = 2 | 
|  | 737 | Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply(); | 
|  | 738 | { | 
|  | 739 | SCOPED_TRACE("layerB < (layerG < layerR)"); | 
|  | 740 | auto shot = screenshot(); | 
|  | 741 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 742 | shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN); | 
|  | 743 | shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE); | 
|  | 744 | } | 
|  | 745 |  | 
|  | 746 | // restore to absolute z | 
|  | 747 | // layerR = 4, layerG = 0, layerB = 2 | 
|  | 748 | Transaction().setLayer(layerG, mLayerZBase).apply(); | 
|  | 749 | { | 
|  | 750 | SCOPED_TRACE("layerG < layerB < layerR"); | 
|  | 751 | auto shot = screenshot(); | 
|  | 752 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 753 | shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE); | 
|  | 754 | } | 
|  | 755 |  | 
|  | 756 | // layerR should not affect layerG anymore | 
|  | 757 | // layerR = 1, layerG = 0, layerB = 2 | 
|  | 758 | Transaction().setLayer(layerR, mLayerZBase + 1).apply(); | 
|  | 759 | { | 
|  | 760 | SCOPED_TRACE("layerG < layerR < layerB"); | 
|  | 761 | auto shot = screenshot(); | 
|  | 762 | shot->expectColor(Rect(0, 0, 16, 16), Color::RED); | 
|  | 763 | shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE); | 
|  | 764 | } | 
|  | 765 | } | 
|  | 766 |  | 
|  | 767 | TEST_F(LayerTransactionTest, SetRelativeZBug64572777) { | 
|  | 768 | sp<SurfaceControl> layerR; | 
|  | 769 | sp<SurfaceControl> layerG; | 
|  | 770 |  | 
|  | 771 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 772 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED)); | 
|  | 773 | ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32)); | 
|  | 774 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN)); | 
|  | 775 |  | 
|  | 776 | Transaction() | 
|  | 777 | .setPosition(layerG, 16, 16) | 
|  | 778 | .setRelativeLayer(layerG, layerR->getHandle(), 1) | 
|  | 779 | .apply(); | 
|  | 780 |  | 
|  | 781 | mClient->destroySurface(layerG->getHandle()); | 
|  | 782 | // layerG should have been removed | 
|  | 783 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 784 | } | 
|  | 785 |  | 
| Chia-I Wu | 57b2750 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 786 | TEST_F(LayerTransactionTest, SetFlagsHidden) { | 
|  | 787 | sp<SurfaceControl> layer; | 
|  | 788 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 789 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 790 |  | 
|  | 791 | Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply(); | 
|  | 792 | { | 
|  | 793 | SCOPED_TRACE("layer hidden"); | 
|  | 794 | screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK); | 
|  | 795 | } | 
|  | 796 |  | 
|  | 797 | Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply(); | 
|  | 798 | { | 
|  | 799 | SCOPED_TRACE("layer shown"); | 
|  | 800 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 801 | } | 
|  | 802 | } | 
|  | 803 |  | 
|  | 804 | TEST_F(LayerTransactionTest, SetFlagsOpaque) { | 
|  | 805 | const Color translucentRed = {100, 0, 0, 100}; | 
|  | 806 | sp<SurfaceControl> layerR; | 
|  | 807 | sp<SurfaceControl> layerG; | 
|  | 808 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 809 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed)); | 
|  | 810 | ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32)); | 
|  | 811 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN)); | 
|  | 812 |  | 
|  | 813 | Transaction() | 
|  | 814 | .setLayer(layerR, mLayerZBase + 1) | 
|  | 815 | .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque) | 
|  | 816 | .apply(); | 
|  | 817 | { | 
|  | 818 | SCOPED_TRACE("layerR opaque"); | 
|  | 819 | screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255}); | 
|  | 820 | } | 
|  | 821 |  | 
|  | 822 | Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply(); | 
|  | 823 | { | 
|  | 824 | SCOPED_TRACE("layerR translucent"); | 
|  | 825 | const uint8_t g = uint8_t(255 - translucentRed.a); | 
|  | 826 | screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255}); | 
|  | 827 | } | 
|  | 828 | } | 
|  | 829 |  | 
|  | 830 | TEST_F(LayerTransactionTest, SetFlagsSecure) { | 
|  | 831 | sp<SurfaceControl> layer; | 
|  | 832 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 833 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 834 |  | 
|  | 835 | sp<ISurfaceComposer> composer = ComposerService::getComposerService(); | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 836 | sp<GraphicBuffer> outBuffer; | 
| Chia-I Wu | 57b2750 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 837 | Transaction() | 
|  | 838 | .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure) | 
|  | 839 | .apply(true); | 
|  | 840 | ASSERT_EQ(PERMISSION_DENIED, | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 841 | composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase, | 
| Chia-I Wu | 57b2750 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 842 | false)); | 
|  | 843 |  | 
|  | 844 | Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true); | 
|  | 845 | ASSERT_EQ(NO_ERROR, | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 846 | composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase, | 
| Chia-I Wu | 57b2750 | 2017-10-31 10:14:40 -0700 | [diff] [blame] | 847 | false)); | 
|  | 848 | } | 
|  | 849 |  | 
| Chia-I Wu | 2113bdd | 2017-11-01 15:16:35 -0700 | [diff] [blame] | 850 | TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) { | 
|  | 851 | const Rect top(0, 0, 32, 16); | 
|  | 852 | const Rect bottom(0, 16, 32, 32); | 
|  | 853 | sp<SurfaceControl> layer; | 
|  | 854 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 855 |  | 
|  | 856 | ANativeWindow_Buffer buffer; | 
|  | 857 | ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer)); | 
|  | 858 | ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT)); | 
|  | 859 | ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED)); | 
|  | 860 | // setTransparentRegionHint always applies to the following buffer | 
|  | 861 | Transaction().setTransparentRegionHint(layer, Region(top)).apply(); | 
|  | 862 | ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer)); | 
|  | 863 | { | 
|  | 864 | SCOPED_TRACE("top transparent"); | 
|  | 865 | auto shot = screenshot(); | 
|  | 866 | shot->expectColor(top, Color::BLACK); | 
|  | 867 | shot->expectColor(bottom, Color::RED); | 
|  | 868 | } | 
|  | 869 |  | 
|  | 870 | Transaction().setTransparentRegionHint(layer, Region(bottom)).apply(); | 
|  | 871 | { | 
|  | 872 | SCOPED_TRACE("transparent region hint pending"); | 
|  | 873 | auto shot = screenshot(); | 
|  | 874 | shot->expectColor(top, Color::BLACK); | 
|  | 875 | shot->expectColor(bottom, Color::RED); | 
|  | 876 | } | 
|  | 877 |  | 
|  | 878 | ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer)); | 
|  | 879 | ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED)); | 
|  | 880 | ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT)); | 
|  | 881 | ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer)); | 
|  | 882 | { | 
|  | 883 | SCOPED_TRACE("bottom transparent"); | 
|  | 884 | auto shot = screenshot(); | 
|  | 885 | shot->expectColor(top, Color::RED); | 
|  | 886 | shot->expectColor(bottom, Color::BLACK); | 
|  | 887 | } | 
|  | 888 | } | 
|  | 889 |  | 
|  | 890 | TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) { | 
|  | 891 | sp<SurfaceControl> layerTransparent; | 
|  | 892 | sp<SurfaceControl> layerR; | 
|  | 893 | ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32)); | 
|  | 894 | ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32)); | 
|  | 895 |  | 
|  | 896 | // check that transparent region hint is bound by the layer size | 
|  | 897 | Transaction() | 
|  | 898 | .setTransparentRegionHint(layerTransparent, | 
|  | 899 | Region(Rect(0, 0, mDisplayWidth, mDisplayHeight))) | 
|  | 900 | .setPosition(layerR, 16, 16) | 
|  | 901 | .setLayer(layerR, mLayerZBase + 1) | 
|  | 902 | .apply(); | 
|  | 903 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT)); | 
|  | 904 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED)); | 
|  | 905 | screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED); | 
|  | 906 | } | 
|  | 907 |  | 
| Chia-I Wu | a8a515e | 2017-11-01 15:16:35 -0700 | [diff] [blame] | 908 | TEST_F(LayerTransactionTest, SetAlphaBasic) { | 
|  | 909 | sp<SurfaceControl> layer1; | 
|  | 910 | sp<SurfaceControl> layer2; | 
|  | 911 | ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32)); | 
|  | 912 | ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32)); | 
|  | 913 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255})); | 
|  | 914 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255})); | 
|  | 915 |  | 
|  | 916 | Transaction() | 
|  | 917 | .setAlpha(layer1, 0.25f) | 
|  | 918 | .setAlpha(layer2, 0.75f) | 
|  | 919 | .setPosition(layer2, 16, 0) | 
|  | 920 | .setLayer(layer2, mLayerZBase + 1) | 
|  | 921 | .apply(); | 
|  | 922 | { | 
|  | 923 | auto shot = screenshot(); | 
|  | 924 | uint8_t r = 16; // 64 * 0.25f | 
|  | 925 | uint8_t g = 48; // 64 * 0.75f | 
|  | 926 | shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255}); | 
|  | 927 | shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255}); | 
|  | 928 |  | 
|  | 929 | r /= 4; // r * (1.0f - 0.75f) | 
|  | 930 | shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255}); | 
|  | 931 | } | 
|  | 932 | } | 
|  | 933 |  | 
|  | 934 | TEST_F(LayerTransactionTest, SetAlphaClamped) { | 
|  | 935 | const Color color = {64, 0, 0, 255}; | 
|  | 936 | sp<SurfaceControl> layer; | 
|  | 937 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 938 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color)); | 
|  | 939 |  | 
|  | 940 | Transaction().setAlpha(layer, 2.0f).apply(); | 
|  | 941 | { | 
|  | 942 | SCOPED_TRACE("clamped to 1.0f"); | 
|  | 943 | screenshot()->expectColor(Rect(0, 0, 32, 32), color); | 
|  | 944 | } | 
|  | 945 |  | 
|  | 946 | Transaction().setAlpha(layer, -1.0f).apply(); | 
|  | 947 | { | 
|  | 948 | SCOPED_TRACE("clamped to 0.0f"); | 
|  | 949 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK); | 
|  | 950 | } | 
|  | 951 | } | 
|  | 952 |  | 
| Chia-I Wu | e4ef610 | 2017-11-01 15:16:35 -0700 | [diff] [blame] | 953 | TEST_F(LayerTransactionTest, SetColorBasic) { | 
|  | 954 | sp<SurfaceControl> bufferLayer; | 
|  | 955 | sp<SurfaceControl> colorLayer; | 
|  | 956 | ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32)); | 
|  | 957 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED)); | 
|  | 958 | ASSERT_NO_FATAL_FAILURE( | 
|  | 959 | colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor)); | 
|  | 960 |  | 
|  | 961 | Transaction().setLayer(colorLayer, mLayerZBase + 1).apply(); | 
|  | 962 | { | 
|  | 963 | SCOPED_TRACE("default color"); | 
|  | 964 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK); | 
|  | 965 | } | 
|  | 966 |  | 
|  | 967 | const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f); | 
|  | 968 | const Color expected = {15, 51, 85, 255}; | 
|  | 969 | // this is handwavy, but the precison loss scaled by 255 (8-bit per | 
|  | 970 | // channel) should be less than one | 
|  | 971 | const uint8_t tolerance = 1; | 
|  | 972 | Transaction().setColor(colorLayer, color).apply(); | 
|  | 973 | { | 
|  | 974 | SCOPED_TRACE("new color"); | 
|  | 975 | screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance); | 
|  | 976 | } | 
|  | 977 | } | 
|  | 978 |  | 
|  | 979 | TEST_F(LayerTransactionTest, SetColorClamped) { | 
|  | 980 | sp<SurfaceControl> colorLayer; | 
|  | 981 | ASSERT_NO_FATAL_FAILURE( | 
|  | 982 | colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor)); | 
|  | 983 |  | 
|  | 984 | Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply(); | 
|  | 985 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 986 | } | 
|  | 987 |  | 
|  | 988 | TEST_F(LayerTransactionTest, SetColorWithAlpha) { | 
|  | 989 | sp<SurfaceControl> bufferLayer; | 
|  | 990 | sp<SurfaceControl> colorLayer; | 
|  | 991 | ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32)); | 
|  | 992 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED)); | 
|  | 993 | ASSERT_NO_FATAL_FAILURE( | 
|  | 994 | colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor)); | 
|  | 995 |  | 
|  | 996 | const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f); | 
|  | 997 | const float alpha = 0.25f; | 
|  | 998 | const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f); | 
|  | 999 | // this is handwavy, but the precison loss scaled by 255 (8-bit per | 
|  | 1000 | // channel) should be less than one | 
|  | 1001 | const uint8_t tolerance = 1; | 
|  | 1002 | Transaction() | 
|  | 1003 | .setColor(colorLayer, color) | 
|  | 1004 | .setAlpha(colorLayer, alpha) | 
|  | 1005 | .setLayer(colorLayer, mLayerZBase + 1) | 
|  | 1006 | .apply(); | 
|  | 1007 | screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255}, | 
|  | 1008 | tolerance); | 
|  | 1009 | } | 
|  | 1010 |  | 
| Adrian Roos | b7a9650 | 2018-04-08 11:38:55 -0700 | [diff] [blame] | 1011 | TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) { | 
|  | 1012 | sp<SurfaceControl> bufferLayer; | 
|  | 1013 | sp<SurfaceControl> parentLayer; | 
|  | 1014 | sp<SurfaceControl> colorLayer; | 
|  | 1015 | ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32)); | 
|  | 1016 | ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32)); | 
|  | 1017 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED)); | 
|  | 1018 | ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer( | 
|  | 1019 | "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor)); | 
|  | 1020 |  | 
|  | 1021 | const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f); | 
|  | 1022 | const float alpha = 0.25f; | 
|  | 1023 | const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f); | 
|  | 1024 | // this is handwavy, but the precision loss scaled by 255 (8-bit per | 
|  | 1025 | // channel) should be less than one | 
|  | 1026 | const uint8_t tolerance = 1; | 
|  | 1027 | Transaction() | 
|  | 1028 | .reparent(colorLayer, parentLayer->getHandle()) | 
|  | 1029 | .setColor(colorLayer, color) | 
|  | 1030 | .setAlpha(parentLayer, alpha) | 
|  | 1031 | .setLayer(parentLayer, mLayerZBase + 1) | 
|  | 1032 | .apply(); | 
|  | 1033 | screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255}, | 
|  | 1034 | tolerance); | 
|  | 1035 | } | 
|  | 1036 |  | 
| Chia-I Wu | e4ef610 | 2017-11-01 15:16:35 -0700 | [diff] [blame] | 1037 | TEST_F(LayerTransactionTest, SetColorWithBuffer) { | 
|  | 1038 | sp<SurfaceControl> bufferLayer; | 
|  | 1039 | ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32)); | 
|  | 1040 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED)); | 
|  | 1041 |  | 
|  | 1042 | // color is ignored | 
|  | 1043 | Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply(); | 
|  | 1044 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1045 | } | 
|  | 1046 |  | 
| Chia-I Wu | 3d22f3a | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 1047 | TEST_F(LayerTransactionTest, SetLayerStackBasic) { | 
|  | 1048 | sp<SurfaceControl> layer; | 
|  | 1049 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1050 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1051 |  | 
|  | 1052 | Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply(); | 
|  | 1053 | { | 
|  | 1054 | SCOPED_TRACE("non-existing layer stack"); | 
|  | 1055 | screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK); | 
|  | 1056 | } | 
|  | 1057 |  | 
|  | 1058 | Transaction().setLayerStack(layer, mDisplayLayerStack).apply(); | 
|  | 1059 | { | 
|  | 1060 | SCOPED_TRACE("original layer stack"); | 
|  | 1061 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1062 | } | 
|  | 1063 | } | 
|  | 1064 |  | 
| Chia-I Wu | 93853fe | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 1065 | TEST_F(LayerTransactionTest, SetMatrixBasic) { | 
|  | 1066 | sp<SurfaceControl> layer; | 
|  | 1067 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1068 | ASSERT_NO_FATAL_FAILURE( | 
|  | 1069 | fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE)); | 
|  | 1070 |  | 
|  | 1071 | Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply(); | 
|  | 1072 | { | 
|  | 1073 | SCOPED_TRACE("IDENTITY"); | 
|  | 1074 | screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE, | 
|  | 1075 | Color::WHITE); | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply(); | 
|  | 1079 | { | 
|  | 1080 | SCOPED_TRACE("FLIP_H"); | 
|  | 1081 | screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE, | 
|  | 1082 | Color::BLUE); | 
|  | 1083 | } | 
|  | 1084 |  | 
|  | 1085 | Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply(); | 
|  | 1086 | { | 
|  | 1087 | SCOPED_TRACE("FLIP_V"); | 
|  | 1088 | screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED, | 
|  | 1089 | Color::GREEN); | 
|  | 1090 | } | 
|  | 1091 |  | 
|  | 1092 | Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply(); | 
|  | 1093 | { | 
|  | 1094 | SCOPED_TRACE("ROT_90"); | 
|  | 1095 | screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE, | 
|  | 1096 | Color::GREEN); | 
|  | 1097 | } | 
|  | 1098 |  | 
|  | 1099 | Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply(); | 
|  | 1100 | { | 
|  | 1101 | SCOPED_TRACE("SCALE"); | 
|  | 1102 | screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE, | 
|  | 1103 | Color::WHITE, true /* filtered */); | 
|  | 1104 | } | 
|  | 1105 | } | 
|  | 1106 |  | 
|  | 1107 | TEST_F(LayerTransactionTest, SetMatrixRot45) { | 
|  | 1108 | sp<SurfaceControl> layer; | 
|  | 1109 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1110 | ASSERT_NO_FATAL_FAILURE( | 
|  | 1111 | fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE)); | 
|  | 1112 |  | 
|  | 1113 | const float rot = M_SQRT1_2; // 45 degrees | 
|  | 1114 | const float trans = M_SQRT2 * 16.0f; | 
|  | 1115 | Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply(); | 
|  | 1116 |  | 
|  | 1117 | auto shot = screenshot(); | 
|  | 1118 | // check a 8x8 region inside each color | 
|  | 1119 | auto get8x8Rect = [](int32_t centerX, int32_t centerY) { | 
|  | 1120 | const int32_t halfL = 4; | 
|  | 1121 | return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL); | 
|  | 1122 | }; | 
|  | 1123 | const int32_t unit = int32_t(trans / 2); | 
|  | 1124 | shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED); | 
|  | 1125 | shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN); | 
|  | 1126 | shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE); | 
|  | 1127 | shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE); | 
|  | 1128 | } | 
|  | 1129 |  | 
|  | 1130 | TEST_F(LayerTransactionTest, SetMatrixWithResize) { | 
|  | 1131 | sp<SurfaceControl> layer; | 
|  | 1132 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1133 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1134 |  | 
|  | 1135 | // setMatrix is applied after any pending resize, unlike setPosition | 
|  | 1136 | Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply(); | 
|  | 1137 | { | 
|  | 1138 | SCOPED_TRACE("resize pending"); | 
|  | 1139 | auto shot = screenshot(); | 
|  | 1140 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1141 | shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); | 
|  | 1142 | } | 
|  | 1143 |  | 
|  | 1144 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1145 | { | 
|  | 1146 | SCOPED_TRACE("resize applied"); | 
|  | 1147 | screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED); | 
|  | 1148 | } | 
|  | 1149 | } | 
|  | 1150 |  | 
|  | 1151 | TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) { | 
|  | 1152 | sp<SurfaceControl> layer; | 
|  | 1153 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1154 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1155 |  | 
|  | 1156 | // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition | 
|  | 1157 | Transaction() | 
|  | 1158 | .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f) | 
|  | 1159 | .setSize(layer, 64, 64) | 
|  | 1160 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) | 
|  | 1161 | .apply(); | 
|  | 1162 | screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED); | 
|  | 1163 | } | 
|  | 1164 |  | 
| Chia-I Wu | a56b204 | 2017-11-01 15:16:35 -0700 | [diff] [blame] | 1165 | TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) { | 
|  | 1166 | sp<SurfaceControl> layer; | 
|  | 1167 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1168 | ASSERT_NO_FATAL_FAILURE( | 
|  | 1169 | fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE)); | 
|  | 1170 |  | 
|  | 1171 | // XXX SCALE_CROP is not respected; calling setSize and | 
|  | 1172 | // setOverrideScalingMode in separate transactions does not work | 
|  | 1173 | // (b/69315456) | 
|  | 1174 | Transaction() | 
|  | 1175 | .setSize(layer, 64, 16) | 
|  | 1176 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) | 
|  | 1177 | .apply(); | 
|  | 1178 | { | 
|  | 1179 | SCOPED_TRACE("SCALE_TO_WINDOW"); | 
|  | 1180 | screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE, | 
|  | 1181 | Color::WHITE, true /* filtered */); | 
|  | 1182 | } | 
|  | 1183 | } | 
|  | 1184 |  | 
| Chia-I Wu | 04dcca8 | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 1185 | TEST_F(LayerTransactionTest, SetCropBasic) { | 
|  | 1186 | sp<SurfaceControl> layer; | 
|  | 1187 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1188 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1189 | const Rect crop(8, 8, 24, 24); | 
|  | 1190 |  | 
|  | 1191 | Transaction().setCrop(layer, crop).apply(); | 
|  | 1192 | auto shot = screenshot(); | 
|  | 1193 | shot->expectColor(crop, Color::RED); | 
|  | 1194 | shot->expectBorder(crop, Color::BLACK); | 
|  | 1195 | } | 
|  | 1196 |  | 
|  | 1197 | TEST_F(LayerTransactionTest, SetCropEmpty) { | 
|  | 1198 | sp<SurfaceControl> layer; | 
|  | 1199 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1200 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1201 |  | 
|  | 1202 | { | 
|  | 1203 | SCOPED_TRACE("empty rect"); | 
|  | 1204 | Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply(); | 
|  | 1205 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1206 | } | 
|  | 1207 |  | 
|  | 1208 | { | 
|  | 1209 | SCOPED_TRACE("negative rect"); | 
|  | 1210 | Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply(); | 
|  | 1211 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1212 | } | 
|  | 1213 | } | 
|  | 1214 |  | 
|  | 1215 | TEST_F(LayerTransactionTest, SetCropOutOfBounds) { | 
|  | 1216 | sp<SurfaceControl> layer; | 
|  | 1217 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1218 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1219 |  | 
|  | 1220 | Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply(); | 
|  | 1221 | auto shot = screenshot(); | 
|  | 1222 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1223 | shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); | 
|  | 1224 | } | 
|  | 1225 |  | 
|  | 1226 | TEST_F(LayerTransactionTest, SetCropWithTranslation) { | 
|  | 1227 | sp<SurfaceControl> layer; | 
|  | 1228 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1229 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1230 |  | 
|  | 1231 | const Point position(32, 32); | 
|  | 1232 | const Rect crop(8, 8, 24, 24); | 
|  | 1233 | Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply(); | 
|  | 1234 | auto shot = screenshot(); | 
|  | 1235 | shot->expectColor(crop + position, Color::RED); | 
|  | 1236 | shot->expectBorder(crop + position, Color::BLACK); | 
|  | 1237 | } | 
|  | 1238 |  | 
|  | 1239 | TEST_F(LayerTransactionTest, SetCropWithScale) { | 
|  | 1240 | sp<SurfaceControl> layer; | 
|  | 1241 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1242 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1243 |  | 
|  | 1244 | // crop is affected by matrix | 
|  | 1245 | Transaction() | 
|  | 1246 | .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f) | 
|  | 1247 | .setCrop(layer, Rect(8, 8, 24, 24)) | 
|  | 1248 | .apply(); | 
|  | 1249 | auto shot = screenshot(); | 
|  | 1250 | shot->expectColor(Rect(16, 16, 48, 48), Color::RED); | 
|  | 1251 | shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK); | 
|  | 1252 | } | 
|  | 1253 |  | 
|  | 1254 | TEST_F(LayerTransactionTest, SetCropWithResize) { | 
|  | 1255 | sp<SurfaceControl> layer; | 
|  | 1256 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1257 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1258 |  | 
|  | 1259 | // setCrop is applied immediately by default, with or without resize pending | 
|  | 1260 | Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply(); | 
|  | 1261 | { | 
|  | 1262 | SCOPED_TRACE("resize pending"); | 
|  | 1263 | auto shot = screenshot(); | 
|  | 1264 | shot->expectColor(Rect(8, 8, 24, 24), Color::RED); | 
|  | 1265 | shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK); | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1269 | { | 
|  | 1270 | SCOPED_TRACE("resize applied"); | 
|  | 1271 | auto shot = screenshot(); | 
|  | 1272 | shot->expectColor(Rect(8, 8, 16, 16), Color::RED); | 
|  | 1273 | shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK); | 
|  | 1274 | } | 
|  | 1275 | } | 
|  | 1276 |  | 
|  | 1277 | TEST_F(LayerTransactionTest, SetCropWithNextResize) { | 
|  | 1278 | sp<SurfaceControl> layer; | 
|  | 1279 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1280 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1281 |  | 
|  | 1282 | // request setCrop to be applied with the next resize | 
|  | 1283 | Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply(); | 
|  | 1284 | { | 
|  | 1285 | SCOPED_TRACE("waiting for next resize"); | 
|  | 1286 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1287 | } | 
|  | 1288 |  | 
|  | 1289 | Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply(); | 
|  | 1290 | { | 
|  | 1291 | SCOPED_TRACE("pending crop modified"); | 
|  | 1292 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1293 | } | 
|  | 1294 |  | 
|  | 1295 | Transaction().setSize(layer, 16, 16).apply(); | 
|  | 1296 | { | 
|  | 1297 | SCOPED_TRACE("resize pending"); | 
|  | 1298 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1299 | } | 
|  | 1300 |  | 
|  | 1301 | // finally resize | 
|  | 1302 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1303 | { | 
|  | 1304 | SCOPED_TRACE("new crop applied"); | 
|  | 1305 | auto shot = screenshot(); | 
|  | 1306 | shot->expectColor(Rect(4, 4, 12, 12), Color::RED); | 
|  | 1307 | shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK); | 
|  | 1308 | } | 
|  | 1309 | } | 
|  | 1310 |  | 
|  | 1311 | TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) { | 
|  | 1312 | sp<SurfaceControl> layer; | 
|  | 1313 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1314 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1315 |  | 
|  | 1316 | // setCrop is not immediate even with SCALE_TO_WINDOW override | 
|  | 1317 | Transaction() | 
|  | 1318 | .setCrop(layer, Rect(4, 4, 12, 12)) | 
|  | 1319 | .setSize(layer, 16, 16) | 
|  | 1320 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) | 
|  | 1321 | .setGeometryAppliesWithResize(layer) | 
|  | 1322 | .apply(); | 
|  | 1323 | { | 
|  | 1324 | SCOPED_TRACE("new crop pending"); | 
|  | 1325 | auto shot = screenshot(); | 
|  | 1326 | shot->expectColor(Rect(0, 0, 16, 16), Color::RED); | 
|  | 1327 | shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK); | 
|  | 1328 | } | 
|  | 1329 |  | 
|  | 1330 | // XXX crop is never latched without other geometry change (b/69315677) | 
|  | 1331 | Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply(); | 
|  | 1332 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1333 | Transaction().setPosition(layer, 0, 0).apply(); | 
|  | 1334 | { | 
|  | 1335 | SCOPED_TRACE("new crop applied"); | 
|  | 1336 | auto shot = screenshot(); | 
|  | 1337 | shot->expectColor(Rect(4, 4, 12, 12), Color::RED); | 
|  | 1338 | shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK); | 
|  | 1339 | } | 
|  | 1340 | } | 
|  | 1341 |  | 
| Chia-I Wu | cdd71a5 | 2017-11-02 08:30:27 -0700 | [diff] [blame] | 1342 | TEST_F(LayerTransactionTest, SetFinalCropBasic) { | 
|  | 1343 | sp<SurfaceControl> layer; | 
|  | 1344 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1345 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1346 | const Rect crop(8, 8, 24, 24); | 
|  | 1347 |  | 
|  | 1348 | // same as in SetCropBasic | 
|  | 1349 | Transaction().setFinalCrop(layer, crop).apply(); | 
|  | 1350 | auto shot = screenshot(); | 
|  | 1351 | shot->expectColor(crop, Color::RED); | 
|  | 1352 | shot->expectBorder(crop, Color::BLACK); | 
|  | 1353 | } | 
|  | 1354 |  | 
|  | 1355 | TEST_F(LayerTransactionTest, SetFinalCropEmpty) { | 
|  | 1356 | sp<SurfaceControl> layer; | 
|  | 1357 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1358 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1359 |  | 
|  | 1360 | // same as in SetCropEmpty | 
|  | 1361 | { | 
|  | 1362 | SCOPED_TRACE("empty rect"); | 
|  | 1363 | Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply(); | 
|  | 1364 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1365 | } | 
|  | 1366 |  | 
|  | 1367 | { | 
|  | 1368 | SCOPED_TRACE("negative rect"); | 
|  | 1369 | Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply(); | 
|  | 1370 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1371 | } | 
|  | 1372 | } | 
|  | 1373 |  | 
|  | 1374 | TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) { | 
|  | 1375 | sp<SurfaceControl> layer; | 
|  | 1376 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1377 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1378 |  | 
|  | 1379 | // same as in SetCropOutOfBounds | 
|  | 1380 | Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply(); | 
|  | 1381 | auto shot = screenshot(); | 
|  | 1382 | shot->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1383 | shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK); | 
|  | 1384 | } | 
|  | 1385 |  | 
|  | 1386 | TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) { | 
|  | 1387 | sp<SurfaceControl> layer; | 
|  | 1388 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1389 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1390 |  | 
|  | 1391 | // final crop is applied post-translation | 
|  | 1392 | Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply(); | 
|  | 1393 | auto shot = screenshot(); | 
|  | 1394 | shot->expectColor(Rect(16, 16, 24, 24), Color::RED); | 
|  | 1395 | shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK); | 
|  | 1396 | } | 
|  | 1397 |  | 
|  | 1398 | TEST_F(LayerTransactionTest, SetFinalCropWithScale) { | 
|  | 1399 | sp<SurfaceControl> layer; | 
|  | 1400 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1401 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1402 |  | 
|  | 1403 | // final crop is not affected by matrix | 
|  | 1404 | Transaction() | 
|  | 1405 | .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f) | 
|  | 1406 | .setFinalCrop(layer, Rect(8, 8, 24, 24)) | 
|  | 1407 | .apply(); | 
|  | 1408 | auto shot = screenshot(); | 
|  | 1409 | shot->expectColor(Rect(8, 8, 24, 24), Color::RED); | 
|  | 1410 | shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK); | 
|  | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | TEST_F(LayerTransactionTest, SetFinalCropWithResize) { | 
|  | 1414 | sp<SurfaceControl> layer; | 
|  | 1415 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1416 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1417 |  | 
|  | 1418 | // same as in SetCropWithResize | 
|  | 1419 | Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply(); | 
|  | 1420 | { | 
|  | 1421 | SCOPED_TRACE("resize pending"); | 
|  | 1422 | auto shot = screenshot(); | 
|  | 1423 | shot->expectColor(Rect(8, 8, 24, 24), Color::RED); | 
|  | 1424 | shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK); | 
|  | 1425 | } | 
|  | 1426 |  | 
|  | 1427 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1428 | { | 
|  | 1429 | SCOPED_TRACE("resize applied"); | 
|  | 1430 | auto shot = screenshot(); | 
|  | 1431 | shot->expectColor(Rect(8, 8, 16, 16), Color::RED); | 
|  | 1432 | shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK); | 
|  | 1433 | } | 
|  | 1434 | } | 
|  | 1435 |  | 
|  | 1436 | TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) { | 
|  | 1437 | sp<SurfaceControl> layer; | 
|  | 1438 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1439 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1440 |  | 
|  | 1441 | // same as in SetCropWithNextResize | 
|  | 1442 | Transaction() | 
|  | 1443 | .setFinalCrop(layer, Rect(8, 8, 24, 24)) | 
|  | 1444 | .setGeometryAppliesWithResize(layer) | 
|  | 1445 | .apply(); | 
|  | 1446 | { | 
|  | 1447 | SCOPED_TRACE("waiting for next resize"); | 
|  | 1448 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1449 | } | 
|  | 1450 |  | 
|  | 1451 | Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply(); | 
|  | 1452 | { | 
|  | 1453 | SCOPED_TRACE("pending final crop modified"); | 
|  | 1454 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1455 | } | 
|  | 1456 |  | 
|  | 1457 | Transaction().setSize(layer, 16, 16).apply(); | 
|  | 1458 | { | 
|  | 1459 | SCOPED_TRACE("resize pending"); | 
|  | 1460 | screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED); | 
|  | 1461 | } | 
|  | 1462 |  | 
|  | 1463 | // finally resize | 
|  | 1464 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1465 | { | 
|  | 1466 | SCOPED_TRACE("new final crop applied"); | 
|  | 1467 | auto shot = screenshot(); | 
|  | 1468 | shot->expectColor(Rect(4, 4, 12, 12), Color::RED); | 
|  | 1469 | shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK); | 
|  | 1470 | } | 
|  | 1471 | } | 
|  | 1472 |  | 
|  | 1473 | TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) { | 
|  | 1474 | sp<SurfaceControl> layer; | 
|  | 1475 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); | 
|  | 1476 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1477 |  | 
|  | 1478 | // same as in SetCropWithNextResizeScaleToWindow | 
|  | 1479 | Transaction() | 
|  | 1480 | .setFinalCrop(layer, Rect(4, 4, 12, 12)) | 
|  | 1481 | .setSize(layer, 16, 16) | 
|  | 1482 | .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) | 
|  | 1483 | .setGeometryAppliesWithResize(layer) | 
|  | 1484 | .apply(); | 
|  | 1485 | { | 
|  | 1486 | SCOPED_TRACE("new final crop pending"); | 
|  | 1487 | auto shot = screenshot(); | 
|  | 1488 | shot->expectColor(Rect(0, 0, 16, 16), Color::RED); | 
|  | 1489 | shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK); | 
|  | 1490 | } | 
|  | 1491 |  | 
|  | 1492 | // XXX final crop is never latched without other geometry change (b/69315677) | 
|  | 1493 | Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply(); | 
|  | 1494 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED)); | 
|  | 1495 | Transaction().setPosition(layer, 0, 0).apply(); | 
|  | 1496 | { | 
|  | 1497 | SCOPED_TRACE("new final crop applied"); | 
|  | 1498 | auto shot = screenshot(); | 
|  | 1499 | shot->expectColor(Rect(4, 4, 12, 12), Color::RED); | 
|  | 1500 | shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK); | 
|  | 1501 | } | 
|  | 1502 | } | 
|  | 1503 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 1504 | class LayerUpdateTest : public LayerTransactionTest { | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1505 | protected: | 
|  | 1506 | virtual void SetUp() { | 
|  | 1507 | mComposerClient = new SurfaceComposerClient; | 
|  | 1508 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); | 
|  | 1509 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1510 | sp<IBinder> display( | 
|  | 1511 | SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); | 
| Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 1512 | DisplayInfo info; | 
| Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 1513 | SurfaceComposerClient::getDisplayInfo(display, &info); | 
| Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 1514 |  | 
|  | 1515 | ssize_t displayWidth = info.w; | 
|  | 1516 | ssize_t displayHeight = info.h; | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1517 |  | 
|  | 1518 | // Background surface | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1519 | mBGSurfaceControl = | 
|  | 1520 | mComposerClient->createSurface(String8("BG Test Surface"), displayWidth, | 
|  | 1521 | displayHeight, PIXEL_FORMAT_RGBA_8888, 0); | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 1522 | ASSERT_TRUE(mBGSurfaceControl != nullptr); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1523 | ASSERT_TRUE(mBGSurfaceControl->isValid()); | 
|  | 1524 | fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195); | 
|  | 1525 |  | 
|  | 1526 | // Foreground surface | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1527 | mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64, | 
|  | 1528 | PIXEL_FORMAT_RGBA_8888, 0); | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 1529 | ASSERT_TRUE(mFGSurfaceControl != nullptr); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1530 | ASSERT_TRUE(mFGSurfaceControl->isValid()); | 
|  | 1531 |  | 
|  | 1532 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); | 
|  | 1533 |  | 
|  | 1534 | // Synchronization surface | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1535 | mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1, | 
|  | 1536 | PIXEL_FORMAT_RGBA_8888, 0); | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 1537 | ASSERT_TRUE(mSyncSurfaceControl != nullptr); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1538 | ASSERT_TRUE(mSyncSurfaceControl->isValid()); | 
|  | 1539 |  | 
|  | 1540 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); | 
|  | 1541 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1542 | asTransaction([&](Transaction& t) { | 
|  | 1543 | t.setDisplayLayerStack(display, 0); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1544 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1545 | t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl); | 
| Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 1546 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1547 | t.setLayer(mFGSurfaceControl, INT32_MAX - 1) | 
|  | 1548 | .setPosition(mFGSurfaceControl, 64, 64) | 
|  | 1549 | .show(mFGSurfaceControl); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1550 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1551 | t.setLayer(mSyncSurfaceControl, INT32_MAX - 1) | 
|  | 1552 | .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2) | 
|  | 1553 | .show(mSyncSurfaceControl); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1554 | }); | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1555 | } | 
|  | 1556 |  | 
|  | 1557 | virtual void TearDown() { | 
|  | 1558 | mComposerClient->dispose(); | 
|  | 1559 | mBGSurfaceControl = 0; | 
|  | 1560 | mFGSurfaceControl = 0; | 
|  | 1561 | mSyncSurfaceControl = 0; | 
|  | 1562 | mComposerClient = 0; | 
|  | 1563 | } | 
|  | 1564 |  | 
|  | 1565 | void waitForPostedBuffers() { | 
|  | 1566 | // Since the sync surface is in synchronous mode (i.e. double buffered) | 
|  | 1567 | // posting three buffers to it should ensure that at least two | 
|  | 1568 | // SurfaceFlinger::handlePageFlip calls have been made, which should | 
|  | 1569 | // guaranteed that a buffer posted to another Surface has been retired. | 
|  | 1570 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); | 
|  | 1571 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); | 
|  | 1572 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); | 
|  | 1573 | } | 
|  | 1574 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1575 | void asTransaction(const std::function<void(Transaction&)>& exec) { | 
|  | 1576 | Transaction t; | 
|  | 1577 | exec(t); | 
|  | 1578 | t.apply(true); | 
|  | 1579 | } | 
|  | 1580 |  | 
| Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 1581 | sp<SurfaceComposerClient> mComposerClient; | 
|  | 1582 | sp<SurfaceControl> mBGSurfaceControl; | 
|  | 1583 | sp<SurfaceControl> mFGSurfaceControl; | 
|  | 1584 |  | 
|  | 1585 | // This surface is used to ensure that the buffers posted to | 
|  | 1586 | // mFGSurfaceControl have been picked up by SurfaceFlinger. | 
|  | 1587 | sp<SurfaceControl> mSyncSurfaceControl; | 
|  | 1588 | }; | 
|  | 1589 |  | 
| Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 1590 | TEST_F(LayerUpdateTest, RelativesAreNotDetached) { | 
|  | 1591 | sp<ScreenCapture> sc; | 
|  | 1592 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1593 | sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10, | 
|  | 1594 | 10, PIXEL_FORMAT_RGBA_8888, 0); | 
| Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 1595 | fillSurfaceRGBA8(relative, 10, 10, 10); | 
|  | 1596 | waitForPostedBuffers(); | 
|  | 1597 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1598 | Transaction{} | 
|  | 1599 | .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1) | 
| Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 1600 | .setPosition(relative, 64, 64) | 
|  | 1601 | .apply(); | 
|  | 1602 |  | 
|  | 1603 | { | 
|  | 1604 | // The relative should be on top of the FG control. | 
|  | 1605 | ScreenCapture::captureScreen(&sc); | 
|  | 1606 | sc->checkPixel(64, 64, 10, 10, 10); | 
|  | 1607 | } | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1608 | Transaction{}.detachChildren(mFGSurfaceControl).apply(); | 
| Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 1609 |  | 
|  | 1610 | { | 
|  | 1611 | // Nothing should change at this point. | 
|  | 1612 | ScreenCapture::captureScreen(&sc); | 
|  | 1613 | sc->checkPixel(64, 64, 10, 10, 10); | 
|  | 1614 | } | 
|  | 1615 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1616 | Transaction{}.hide(relative).apply(); | 
| Robert Carr | 7f619b2 | 2017-11-06 12:56:35 -0800 | [diff] [blame] | 1617 |  | 
|  | 1618 | { | 
|  | 1619 | // Ensure that the relative was actually hidden, rather than | 
|  | 1620 | // being left in the detached but visible state. | 
|  | 1621 | ScreenCapture::captureScreen(&sc); | 
|  | 1622 | sc->expectFGColor(64, 64); | 
|  | 1623 | } | 
|  | 1624 | } | 
|  | 1625 |  | 
| Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1626 | class GeometryLatchingTest : public LayerUpdateTest { | 
|  | 1627 | protected: | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1628 | void EXPECT_INITIAL_STATE(const char* trace) { | 
| Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1629 | SCOPED_TRACE(trace); | 
|  | 1630 | ScreenCapture::captureScreen(&sc); | 
|  | 1631 | // We find the leading edge of the FG surface. | 
|  | 1632 | sc->expectFGColor(127, 127); | 
|  | 1633 | sc->expectBGColor(128, 128); | 
|  | 1634 | } | 
| Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1635 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1636 | void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); } | 
| Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1637 |  | 
|  | 1638 | void unlockFGBuffer() { | 
|  | 1639 | sp<Surface> s = mFGSurfaceControl->getSurface(); | 
|  | 1640 | ASSERT_EQ(NO_ERROR, s->unlockAndPost()); | 
|  | 1641 | waitForPostedBuffers(); | 
|  | 1642 | } | 
|  | 1643 |  | 
| Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1644 | void completeFGResize() { | 
|  | 1645 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); | 
|  | 1646 | waitForPostedBuffers(); | 
|  | 1647 | } | 
|  | 1648 | void restoreInitialState() { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1649 | asTransaction([&](Transaction& t) { | 
|  | 1650 | t.setSize(mFGSurfaceControl, 64, 64); | 
|  | 1651 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 1652 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64)); | 
|  | 1653 | t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); | 
|  | 1654 | }); | 
| Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1655 |  | 
|  | 1656 | EXPECT_INITIAL_STATE("After restoring initial state"); | 
|  | 1657 | } | 
|  | 1658 | sp<ScreenCapture> sc; | 
|  | 1659 | }; | 
|  | 1660 |  | 
| Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1661 | class CropLatchingTest : public GeometryLatchingTest { | 
|  | 1662 | protected: | 
|  | 1663 | void EXPECT_CROPPED_STATE(const char* trace) { | 
|  | 1664 | SCOPED_TRACE(trace); | 
|  | 1665 | ScreenCapture::captureScreen(&sc); | 
|  | 1666 | // The edge should be moved back one pixel by our crop. | 
|  | 1667 | sc->expectFGColor(126, 126); | 
|  | 1668 | sc->expectBGColor(127, 127); | 
|  | 1669 | sc->expectBGColor(128, 128); | 
|  | 1670 | } | 
| chaviw | 59f5c56 | 2017-06-28 16:39:06 -0700 | [diff] [blame] | 1671 |  | 
|  | 1672 | void EXPECT_RESIZE_STATE(const char* trace) { | 
|  | 1673 | SCOPED_TRACE(trace); | 
|  | 1674 | ScreenCapture::captureScreen(&sc); | 
|  | 1675 | // The FG is now resized too 128,128 at 64,64 | 
|  | 1676 | sc->expectFGColor(64, 64); | 
|  | 1677 | sc->expectFGColor(191, 191); | 
|  | 1678 | sc->expectBGColor(192, 192); | 
|  | 1679 | } | 
| Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1680 | }; | 
|  | 1681 |  | 
| Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1682 | // In this test we ensure that setGeometryAppliesWithResize actually demands | 
|  | 1683 | // a buffer of the new size, and not just any size. | 
|  | 1684 | TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) { | 
|  | 1685 | EXPECT_INITIAL_STATE("before anything"); | 
|  | 1686 | // Normally the crop applies immediately even while a resize is pending. | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1687 | asTransaction([&](Transaction& t) { | 
|  | 1688 | t.setSize(mFGSurfaceControl, 128, 128); | 
|  | 1689 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); | 
|  | 1690 | }); | 
| Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1691 |  | 
|  | 1692 | EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); | 
|  | 1693 |  | 
|  | 1694 | restoreInitialState(); | 
|  | 1695 |  | 
|  | 1696 | // In order to prepare to submit a buffer at the wrong size, we acquire it prior to | 
|  | 1697 | // initiating the resize. | 
|  | 1698 | lockAndFillFGBuffer(); | 
|  | 1699 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1700 | asTransaction([&](Transaction& t) { | 
|  | 1701 | t.setSize(mFGSurfaceControl, 128, 128); | 
|  | 1702 | t.setGeometryAppliesWithResize(mFGSurfaceControl); | 
|  | 1703 | t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127)); | 
|  | 1704 | }); | 
| Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1705 |  | 
|  | 1706 | EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); | 
|  | 1707 |  | 
|  | 1708 | // We now submit our old buffer, at the old size, and ensure it doesn't | 
|  | 1709 | // trigger geometry latching. | 
|  | 1710 | unlockFGBuffer(); | 
|  | 1711 |  | 
|  | 1712 | EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)"); | 
|  | 1713 |  | 
|  | 1714 | completeFGResize(); | 
|  | 1715 |  | 
|  | 1716 | EXPECT_CROPPED_STATE("after the resize finishes"); | 
|  | 1717 | } | 
|  | 1718 |  | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1719 | TEST_F(LayerUpdateTest, DeferredTransactionTest) { | 
|  | 1720 | sp<ScreenCapture> sc; | 
|  | 1721 | { | 
|  | 1722 | SCOPED_TRACE("before anything"); | 
|  | 1723 | ScreenCapture::captureScreen(&sc); | 
| Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1724 | sc->expectBGColor(32, 32); | 
|  | 1725 | sc->expectFGColor(96, 96); | 
|  | 1726 | sc->expectBGColor(160, 160); | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1727 | } | 
|  | 1728 |  | 
|  | 1729 | // set up two deferred transactions on different frames | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1730 | asTransaction([&](Transaction& t) { | 
|  | 1731 | t.setAlpha(mFGSurfaceControl, 0.75); | 
|  | 1732 | t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(), | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1733 | mSyncSurfaceControl->getSurface()->getNextFrameNumber()); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1734 | }); | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1735 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1736 | asTransaction([&](Transaction& t) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1737 | t.setPosition(mFGSurfaceControl, 128, 128); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1738 | t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(), | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1739 | mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1740 | }); | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1741 |  | 
|  | 1742 | { | 
|  | 1743 | SCOPED_TRACE("before any trigger"); | 
|  | 1744 | ScreenCapture::captureScreen(&sc); | 
| Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1745 | sc->expectBGColor(32, 32); | 
|  | 1746 | sc->expectFGColor(96, 96); | 
|  | 1747 | sc->expectBGColor(160, 160); | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1748 | } | 
|  | 1749 |  | 
|  | 1750 | // should trigger the first deferred transaction, but not the second one | 
|  | 1751 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); | 
|  | 1752 | { | 
|  | 1753 | SCOPED_TRACE("after first trigger"); | 
|  | 1754 | ScreenCapture::captureScreen(&sc); | 
| Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1755 | sc->expectBGColor(32, 32); | 
|  | 1756 | sc->checkPixel(96, 96, 162, 63, 96); | 
|  | 1757 | sc->expectBGColor(160, 160); | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1758 | } | 
|  | 1759 |  | 
|  | 1760 | // should show up immediately since it's not deferred | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1761 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); }); | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1762 |  | 
|  | 1763 | // trigger the second deferred transaction | 
|  | 1764 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); | 
|  | 1765 | { | 
|  | 1766 | SCOPED_TRACE("after second trigger"); | 
|  | 1767 | ScreenCapture::captureScreen(&sc); | 
| Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame] | 1768 | sc->expectBGColor(32, 32); | 
|  | 1769 | sc->expectBGColor(96, 96); | 
|  | 1770 | sc->expectFGColor(160, 160); | 
| Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1771 | } | 
|  | 1772 | } | 
|  | 1773 |  | 
| Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1774 | TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) { | 
|  | 1775 | sp<ScreenCapture> sc; | 
|  | 1776 |  | 
|  | 1777 | sp<SurfaceControl> childNoBuffer = | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1778 | mComposerClient->createSurface(String8("Bufferless child"), 10, 10, | 
|  | 1779 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); | 
|  | 1780 | sp<SurfaceControl> childBuffer = | 
|  | 1781 | mComposerClient->createSurface(String8("Buffered child"), 20, 20, | 
|  | 1782 | PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get()); | 
| Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1783 | fillSurfaceRGBA8(childBuffer, 200, 200, 200); | 
|  | 1784 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1785 | SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true); | 
| Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1786 |  | 
|  | 1787 | { | 
|  | 1788 | ScreenCapture::captureScreen(&sc); | 
|  | 1789 | sc->expectChildColor(73, 73); | 
|  | 1790 | sc->expectFGColor(74, 74); | 
|  | 1791 | } | 
|  | 1792 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1793 | SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true); | 
| Robert Carr | e392b55 | 2017-09-19 12:16:05 -0700 | [diff] [blame] | 1794 |  | 
|  | 1795 | { | 
|  | 1796 | ScreenCapture::captureScreen(&sc); | 
|  | 1797 | sc->expectChildColor(73, 73); | 
|  | 1798 | sc->expectChildColor(74, 74); | 
|  | 1799 | } | 
|  | 1800 | } | 
|  | 1801 |  | 
| Robert Carr | 2c5f6d2 | 2017-09-26 12:30:35 -0700 | [diff] [blame] | 1802 | TEST_F(LayerUpdateTest, MergingTransactions) { | 
|  | 1803 | sp<ScreenCapture> sc; | 
|  | 1804 | { | 
|  | 1805 | SCOPED_TRACE("before move"); | 
|  | 1806 | ScreenCapture::captureScreen(&sc); | 
|  | 1807 | sc->expectBGColor(0, 12); | 
|  | 1808 | sc->expectFGColor(75, 75); | 
|  | 1809 | sc->expectBGColor(145, 145); | 
|  | 1810 | } | 
|  | 1811 |  | 
|  | 1812 | Transaction t1, t2; | 
|  | 1813 | t1.setPosition(mFGSurfaceControl, 128, 128); | 
|  | 1814 | t2.setPosition(mFGSurfaceControl, 0, 0); | 
|  | 1815 | // We expect that the position update from t2 now | 
|  | 1816 | // overwrites the position update from t1. | 
|  | 1817 | t1.merge(std::move(t2)); | 
|  | 1818 | t1.apply(); | 
|  | 1819 |  | 
|  | 1820 | { | 
|  | 1821 | ScreenCapture::captureScreen(&sc); | 
|  | 1822 | sc->expectFGColor(1, 1); | 
|  | 1823 | } | 
|  | 1824 | } | 
|  | 1825 |  | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1826 | class ChildLayerTest : public LayerUpdateTest { | 
|  | 1827 | protected: | 
|  | 1828 | void SetUp() override { | 
|  | 1829 | LayerUpdateTest::SetUp(); | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1830 | mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10, | 
|  | 1831 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1832 | fillSurfaceRGBA8(mChild, 200, 200, 200); | 
|  | 1833 |  | 
|  | 1834 | { | 
|  | 1835 | SCOPED_TRACE("before anything"); | 
|  | 1836 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1837 | mCapture->expectChildColor(64, 64); | 
|  | 1838 | } | 
|  | 1839 | } | 
|  | 1840 | void TearDown() override { | 
|  | 1841 | LayerUpdateTest::TearDown(); | 
|  | 1842 | mChild = 0; | 
|  | 1843 | } | 
|  | 1844 |  | 
|  | 1845 | sp<SurfaceControl> mChild; | 
|  | 1846 | sp<ScreenCapture> mCapture; | 
|  | 1847 | }; | 
|  | 1848 |  | 
|  | 1849 | TEST_F(ChildLayerTest, ChildLayerPositioning) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1850 | asTransaction([&](Transaction& t) { | 
|  | 1851 | t.show(mChild); | 
|  | 1852 | t.setPosition(mChild, 10, 10); | 
|  | 1853 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 1854 | }); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1855 |  | 
|  | 1856 | { | 
|  | 1857 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1858 | // Top left of foreground must now be visible | 
|  | 1859 | mCapture->expectFGColor(64, 64); | 
|  | 1860 | // But 10 pixels in we should see the child surface | 
|  | 1861 | mCapture->expectChildColor(74, 74); | 
|  | 1862 | // And 10 more pixels we should be back to the foreground surface | 
|  | 1863 | mCapture->expectFGColor(84, 84); | 
|  | 1864 | } | 
|  | 1865 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1866 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); }); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1867 |  | 
|  | 1868 | { | 
|  | 1869 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1870 | // Top left of foreground should now be at 0, 0 | 
|  | 1871 | mCapture->expectFGColor(0, 0); | 
|  | 1872 | // But 10 pixels in we should see the child surface | 
|  | 1873 | mCapture->expectChildColor(10, 10); | 
|  | 1874 | // And 10 more pixels we should be back to the foreground surface | 
|  | 1875 | mCapture->expectFGColor(20, 20); | 
|  | 1876 | } | 
|  | 1877 | } | 
|  | 1878 |  | 
| Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1879 | TEST_F(ChildLayerTest, ChildLayerCropping) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1880 | asTransaction([&](Transaction& t) { | 
|  | 1881 | t.show(mChild); | 
|  | 1882 | t.setPosition(mChild, 0, 0); | 
|  | 1883 | t.setPosition(mFGSurfaceControl, 0, 0); | 
|  | 1884 | t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5)); | 
|  | 1885 | }); | 
| Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1886 |  | 
|  | 1887 | { | 
|  | 1888 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1889 | mCapture->expectChildColor(0, 0); | 
|  | 1890 | mCapture->expectChildColor(4, 4); | 
|  | 1891 | mCapture->expectBGColor(5, 5); | 
|  | 1892 | } | 
|  | 1893 | } | 
|  | 1894 |  | 
|  | 1895 | TEST_F(ChildLayerTest, ChildLayerFinalCropping) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1896 | asTransaction([&](Transaction& t) { | 
|  | 1897 | t.show(mChild); | 
|  | 1898 | t.setPosition(mChild, 0, 0); | 
|  | 1899 | t.setPosition(mFGSurfaceControl, 0, 0); | 
|  | 1900 | t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5)); | 
|  | 1901 | }); | 
| Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 1902 |  | 
|  | 1903 | { | 
|  | 1904 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1905 | mCapture->expectChildColor(0, 0); | 
|  | 1906 | mCapture->expectChildColor(4, 4); | 
|  | 1907 | mCapture->expectBGColor(5, 5); | 
|  | 1908 | } | 
|  | 1909 | } | 
|  | 1910 |  | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1911 | TEST_F(ChildLayerTest, ChildLayerConstraints) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1912 | asTransaction([&](Transaction& t) { | 
|  | 1913 | t.show(mChild); | 
|  | 1914 | t.setPosition(mFGSurfaceControl, 0, 0); | 
|  | 1915 | t.setPosition(mChild, 63, 63); | 
|  | 1916 | }); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1917 |  | 
|  | 1918 | { | 
|  | 1919 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1920 | mCapture->expectFGColor(0, 0); | 
|  | 1921 | // Last pixel in foreground should now be the child. | 
|  | 1922 | mCapture->expectChildColor(63, 63); | 
|  | 1923 | // But the child should be constrained and the next pixel | 
|  | 1924 | // must be the background | 
|  | 1925 | mCapture->expectBGColor(64, 64); | 
|  | 1926 | } | 
|  | 1927 | } | 
|  | 1928 |  | 
|  | 1929 | TEST_F(ChildLayerTest, ChildLayerScaling) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1930 | asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); }); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1931 |  | 
|  | 1932 | // Find the boundary between the parent and child | 
|  | 1933 | { | 
|  | 1934 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1935 | mCapture->expectChildColor(9, 9); | 
|  | 1936 | mCapture->expectFGColor(10, 10); | 
|  | 1937 | } | 
|  | 1938 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1939 | asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); }); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1940 |  | 
|  | 1941 | // The boundary should be twice as far from the origin now. | 
|  | 1942 | // The pixels from the last test should all be child now | 
|  | 1943 | { | 
|  | 1944 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1945 | mCapture->expectChildColor(9, 9); | 
|  | 1946 | mCapture->expectChildColor(10, 10); | 
|  | 1947 | mCapture->expectChildColor(19, 19); | 
|  | 1948 | mCapture->expectFGColor(20, 20); | 
|  | 1949 | } | 
|  | 1950 | } | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1951 |  | 
| Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1952 | TEST_F(ChildLayerTest, ChildLayerAlpha) { | 
|  | 1953 | fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254); | 
|  | 1954 | fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0); | 
|  | 1955 | fillSurfaceRGBA8(mChild, 0, 254, 0); | 
|  | 1956 | waitForPostedBuffers(); | 
|  | 1957 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1958 | asTransaction([&](Transaction& t) { | 
|  | 1959 | t.show(mChild); | 
|  | 1960 | t.setPosition(mChild, 0, 0); | 
|  | 1961 | t.setPosition(mFGSurfaceControl, 0, 0); | 
|  | 1962 | }); | 
| Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1963 |  | 
|  | 1964 | { | 
|  | 1965 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1966 | // Unblended child color | 
|  | 1967 | mCapture->checkPixel(0, 0, 0, 254, 0); | 
|  | 1968 | } | 
|  | 1969 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1970 | asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); }); | 
| Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1971 |  | 
|  | 1972 | { | 
|  | 1973 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1974 | // Child and BG blended. | 
|  | 1975 | mCapture->checkPixel(0, 0, 127, 127, 0); | 
|  | 1976 | } | 
|  | 1977 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 1978 | asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); }); | 
| Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1979 |  | 
|  | 1980 | { | 
|  | 1981 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1982 | // Child and BG blended. | 
|  | 1983 | mCapture->checkPixel(0, 0, 95, 64, 95); | 
|  | 1984 | } | 
|  | 1985 | } | 
|  | 1986 |  | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1987 | TEST_F(ChildLayerTest, ReparentChildren) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 1988 | asTransaction([&](Transaction& t) { | 
|  | 1989 | t.show(mChild); | 
|  | 1990 | t.setPosition(mChild, 10, 10); | 
|  | 1991 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 1992 | }); | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 1993 |  | 
|  | 1994 | { | 
|  | 1995 | ScreenCapture::captureScreen(&mCapture); | 
|  | 1996 | // Top left of foreground must now be visible | 
|  | 1997 | mCapture->expectFGColor(64, 64); | 
|  | 1998 | // But 10 pixels in we should see the child surface | 
|  | 1999 | mCapture->expectChildColor(74, 74); | 
|  | 2000 | // And 10 more pixels we should be back to the foreground surface | 
|  | 2001 | mCapture->expectFGColor(84, 84); | 
|  | 2002 | } | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2003 |  | 
|  | 2004 | asTransaction([&](Transaction& t) { | 
|  | 2005 | t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle()); | 
|  | 2006 | }); | 
|  | 2007 |  | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2008 | { | 
|  | 2009 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2010 | mCapture->expectFGColor(64, 64); | 
|  | 2011 | // In reparenting we should have exposed the entire foreground surface. | 
|  | 2012 | mCapture->expectFGColor(74, 74); | 
|  | 2013 | // And the child layer should now begin at 10, 10 (since the BG | 
|  | 2014 | // layer is at (0, 0)). | 
|  | 2015 | mCapture->expectBGColor(9, 9); | 
|  | 2016 | mCapture->expectChildColor(10, 10); | 
|  | 2017 | } | 
|  | 2018 | } | 
|  | 2019 |  | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 2020 | TEST_F(ChildLayerTest, DetachChildrenSameClient) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2021 | asTransaction([&](Transaction& t) { | 
|  | 2022 | t.show(mChild); | 
|  | 2023 | t.setPosition(mChild, 10, 10); | 
|  | 2024 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 2025 | }); | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2026 |  | 
|  | 2027 | { | 
|  | 2028 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2029 | // Top left of foreground must now be visible | 
|  | 2030 | mCapture->expectFGColor(64, 64); | 
|  | 2031 | // But 10 pixels in we should see the child surface | 
|  | 2032 | mCapture->expectChildColor(74, 74); | 
|  | 2033 | // And 10 more pixels we should be back to the foreground surface | 
|  | 2034 | mCapture->expectFGColor(84, 84); | 
|  | 2035 | } | 
|  | 2036 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2037 | asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); }); | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2038 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2039 | asTransaction([&](Transaction& t) { t.hide(mChild); }); | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2040 |  | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 2041 | // Since the child has the same client as the parent, it will not get | 
|  | 2042 | // detached and will be hidden. | 
|  | 2043 | { | 
|  | 2044 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2045 | mCapture->expectFGColor(64, 64); | 
|  | 2046 | mCapture->expectFGColor(74, 74); | 
|  | 2047 | mCapture->expectFGColor(84, 84); | 
|  | 2048 | } | 
|  | 2049 | } | 
|  | 2050 |  | 
|  | 2051 | TEST_F(ChildLayerTest, DetachChildrenDifferentClient) { | 
|  | 2052 | sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient; | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2053 | sp<SurfaceControl> mChildNewClient = | 
|  | 2054 | mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10, | 
|  | 2055 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 2056 |  | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 2057 | ASSERT_TRUE(mChildNewClient != nullptr); | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 2058 | ASSERT_TRUE(mChildNewClient->isValid()); | 
|  | 2059 |  | 
|  | 2060 | fillSurfaceRGBA8(mChildNewClient, 200, 200, 200); | 
|  | 2061 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2062 | asTransaction([&](Transaction& t) { | 
|  | 2063 | t.hide(mChild); | 
|  | 2064 | t.show(mChildNewClient); | 
|  | 2065 | t.setPosition(mChildNewClient, 10, 10); | 
|  | 2066 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 2067 | }); | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 2068 |  | 
|  | 2069 | { | 
|  | 2070 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2071 | // Top left of foreground must now be visible | 
|  | 2072 | mCapture->expectFGColor(64, 64); | 
|  | 2073 | // But 10 pixels in we should see the child surface | 
|  | 2074 | mCapture->expectChildColor(74, 74); | 
|  | 2075 | // And 10 more pixels we should be back to the foreground surface | 
|  | 2076 | mCapture->expectFGColor(84, 84); | 
|  | 2077 | } | 
|  | 2078 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2079 | asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); }); | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 2080 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2081 | asTransaction([&](Transaction& t) { t.hide(mChildNewClient); }); | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 2082 |  | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2083 | // Nothing should have changed. | 
|  | 2084 | { | 
|  | 2085 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2086 | mCapture->expectFGColor(64, 64); | 
|  | 2087 | mCapture->expectChildColor(74, 74); | 
|  | 2088 | mCapture->expectFGColor(84, 84); | 
|  | 2089 | } | 
|  | 2090 | } | 
|  | 2091 |  | 
| Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 2092 | TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2093 | asTransaction([&](Transaction& t) { | 
|  | 2094 | t.show(mChild); | 
|  | 2095 | t.setPosition(mChild, 0, 0); | 
|  | 2096 | t.setPosition(mFGSurfaceControl, 0, 0); | 
|  | 2097 | }); | 
| Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 2098 |  | 
|  | 2099 | { | 
|  | 2100 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2101 | // We've positioned the child in the top left. | 
|  | 2102 | mCapture->expectChildColor(0, 0); | 
|  | 2103 | // But it's only 10x10. | 
|  | 2104 | mCapture->expectFGColor(10, 10); | 
|  | 2105 | } | 
|  | 2106 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2107 | asTransaction([&](Transaction& t) { | 
|  | 2108 | t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); | 
|  | 2109 | // We cause scaling by 2. | 
|  | 2110 | t.setSize(mFGSurfaceControl, 128, 128); | 
|  | 2111 | }); | 
| Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 2112 |  | 
|  | 2113 | { | 
|  | 2114 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2115 | // We've positioned the child in the top left. | 
|  | 2116 | mCapture->expectChildColor(0, 0); | 
|  | 2117 | mCapture->expectChildColor(10, 10); | 
|  | 2118 | mCapture->expectChildColor(19, 19); | 
|  | 2119 | // And now it should be scaled all the way to 20x20 | 
|  | 2120 | mCapture->expectFGColor(20, 20); | 
|  | 2121 | } | 
|  | 2122 | } | 
|  | 2123 |  | 
| Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 2124 | // Regression test for b/37673612 | 
|  | 2125 | TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2126 | asTransaction([&](Transaction& t) { | 
|  | 2127 | t.show(mChild); | 
|  | 2128 | t.setPosition(mChild, 0, 0); | 
|  | 2129 | t.setPosition(mFGSurfaceControl, 0, 0); | 
|  | 2130 | }); | 
| Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 2131 |  | 
|  | 2132 | { | 
|  | 2133 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2134 | // We've positioned the child in the top left. | 
|  | 2135 | mCapture->expectChildColor(0, 0); | 
|  | 2136 | // But it's only 10x10. | 
|  | 2137 | mCapture->expectFGColor(10, 10); | 
|  | 2138 | } | 
| Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 2139 | // We set things up as in b/37673612 so that there is a mismatch between the buffer size and | 
|  | 2140 | // the WM specified state size. | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2141 | asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); }); | 
| Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 2142 | sp<Surface> s = mFGSurfaceControl->getSurface(); | 
|  | 2143 | auto anw = static_cast<ANativeWindow*>(s.get()); | 
|  | 2144 | native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90); | 
|  | 2145 | native_window_set_buffers_dimensions(anw, 64, 128); | 
|  | 2146 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); | 
|  | 2147 | waitForPostedBuffers(); | 
|  | 2148 |  | 
|  | 2149 | { | 
|  | 2150 | // The child should still be in the same place and not have any strange scaling as in | 
|  | 2151 | // b/37673612. | 
|  | 2152 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2153 | mCapture->expectChildColor(0, 0); | 
|  | 2154 | mCapture->expectFGColor(10, 10); | 
|  | 2155 | } | 
|  | 2156 | } | 
|  | 2157 |  | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2158 | TEST_F(ChildLayerTest, Bug36858924) { | 
|  | 2159 | // Destroy the child layer | 
|  | 2160 | mChild.clear(); | 
|  | 2161 |  | 
|  | 2162 | // Now recreate it as hidden | 
|  | 2163 | mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10, | 
|  | 2164 | PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden, | 
|  | 2165 | mFGSurfaceControl.get()); | 
|  | 2166 |  | 
|  | 2167 | // Show the child layer in a deferred transaction | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2168 | asTransaction([&](Transaction& t) { | 
|  | 2169 | t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(), | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2170 | mFGSurfaceControl->getSurface()->getNextFrameNumber()); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2171 | t.show(mChild); | 
|  | 2172 | }); | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2173 |  | 
|  | 2174 | // Render the foreground surface a few times | 
|  | 2175 | // | 
|  | 2176 | // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third | 
|  | 2177 | // frame because SurfaceFlinger would never process the deferred transaction and would therefore | 
|  | 2178 | // never acquire/release the first buffer | 
|  | 2179 | ALOGI("Filling 1"); | 
|  | 2180 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); | 
|  | 2181 | ALOGI("Filling 2"); | 
|  | 2182 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255); | 
|  | 2183 | ALOGI("Filling 3"); | 
|  | 2184 | fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0); | 
|  | 2185 | ALOGI("Filling 4"); | 
|  | 2186 | fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); | 
|  | 2187 | } | 
|  | 2188 |  | 
| chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 2189 | TEST_F(ChildLayerTest, Reparent) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2190 | asTransaction([&](Transaction& t) { | 
|  | 2191 | t.show(mChild); | 
|  | 2192 | t.setPosition(mChild, 10, 10); | 
|  | 2193 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 2194 | }); | 
| chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 2195 |  | 
|  | 2196 | { | 
|  | 2197 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2198 | // Top left of foreground must now be visible | 
|  | 2199 | mCapture->expectFGColor(64, 64); | 
|  | 2200 | // But 10 pixels in we should see the child surface | 
|  | 2201 | mCapture->expectChildColor(74, 74); | 
|  | 2202 | // And 10 more pixels we should be back to the foreground surface | 
|  | 2203 | mCapture->expectFGColor(84, 84); | 
|  | 2204 | } | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2205 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2206 | asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); }); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2207 |  | 
| chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 2208 | { | 
|  | 2209 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2210 | mCapture->expectFGColor(64, 64); | 
|  | 2211 | // In reparenting we should have exposed the entire foreground surface. | 
|  | 2212 | mCapture->expectFGColor(74, 74); | 
|  | 2213 | // And the child layer should now begin at 10, 10 (since the BG | 
|  | 2214 | // layer is at (0, 0)). | 
|  | 2215 | mCapture->expectBGColor(9, 9); | 
|  | 2216 | mCapture->expectChildColor(10, 10); | 
|  | 2217 | } | 
|  | 2218 | } | 
|  | 2219 |  | 
| chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 2220 | TEST_F(ChildLayerTest, ReparentToNoParent) { | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2221 | asTransaction([&](Transaction& t) { | 
|  | 2222 | t.show(mChild); | 
|  | 2223 | t.setPosition(mChild, 10, 10); | 
|  | 2224 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 2225 | }); | 
| chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 2226 |  | 
|  | 2227 | { | 
|  | 2228 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2229 | // Top left of foreground must now be visible | 
|  | 2230 | mCapture->expectFGColor(64, 64); | 
|  | 2231 | // But 10 pixels in we should see the child surface | 
|  | 2232 | mCapture->expectChildColor(74, 74); | 
|  | 2233 | // And 10 more pixels we should be back to the foreground surface | 
|  | 2234 | mCapture->expectFGColor(84, 84); | 
|  | 2235 | } | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2236 | asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); }); | 
| chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 2237 | { | 
|  | 2238 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2239 | // Nothing should have changed. | 
|  | 2240 | mCapture->expectFGColor(64, 64); | 
|  | 2241 | mCapture->expectChildColor(74, 74); | 
|  | 2242 | mCapture->expectFGColor(84, 84); | 
|  | 2243 | } | 
|  | 2244 | } | 
|  | 2245 |  | 
|  | 2246 | TEST_F(ChildLayerTest, ReparentFromNoParent) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2247 | sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10, | 
|  | 2248 | PIXEL_FORMAT_RGBA_8888, 0); | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 2249 | ASSERT_TRUE(newSurface != nullptr); | 
| chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 2250 | ASSERT_TRUE(newSurface->isValid()); | 
|  | 2251 |  | 
|  | 2252 | fillSurfaceRGBA8(newSurface, 63, 195, 63); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2253 | asTransaction([&](Transaction& t) { | 
|  | 2254 | t.hide(mChild); | 
|  | 2255 | t.show(newSurface); | 
|  | 2256 | t.setPosition(newSurface, 10, 10); | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2257 | t.setLayer(newSurface, INT32_MAX - 2); | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 2258 | t.setPosition(mFGSurfaceControl, 64, 64); | 
|  | 2259 | }); | 
| chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 2260 |  | 
|  | 2261 | { | 
|  | 2262 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2263 | // Top left of foreground must now be visible | 
|  | 2264 | mCapture->expectFGColor(64, 64); | 
|  | 2265 | // At 10, 10 we should see the new surface | 
|  | 2266 | mCapture->checkPixel(10, 10, 63, 195, 63); | 
|  | 2267 | } | 
|  | 2268 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2269 | asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); }); | 
| chaviw | f1961f7 | 2017-09-18 16:41:07 -0700 | [diff] [blame] | 2270 |  | 
|  | 2271 | { | 
|  | 2272 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2273 | // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from | 
|  | 2274 | // mFGSurface, putting it at 74, 74. | 
|  | 2275 | mCapture->expectFGColor(64, 64); | 
|  | 2276 | mCapture->checkPixel(74, 74, 63, 195, 63); | 
|  | 2277 | mCapture->expectFGColor(84, 84); | 
|  | 2278 | } | 
|  | 2279 | } | 
|  | 2280 |  | 
| chaviw | c967433 | 2017-08-28 12:32:18 -0700 | [diff] [blame] | 2281 | TEST_F(ChildLayerTest, NestedChildren) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2282 | sp<SurfaceControl> grandchild = | 
|  | 2283 | mComposerClient->createSurface(String8("Grandchild surface"), 10, 10, | 
|  | 2284 | PIXEL_FORMAT_RGBA_8888, 0, mChild.get()); | 
| chaviw | c967433 | 2017-08-28 12:32:18 -0700 | [diff] [blame] | 2285 | fillSurfaceRGBA8(grandchild, 50, 50, 50); | 
|  | 2286 |  | 
|  | 2287 | { | 
|  | 2288 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2289 | // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer | 
|  | 2290 | // which begins at 64, 64 | 
|  | 2291 | mCapture->checkPixel(64, 64, 50, 50, 50); | 
|  | 2292 | } | 
|  | 2293 | } | 
|  | 2294 |  | 
| Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 2295 | TEST_F(ChildLayerTest, ChildLayerRelativeLayer) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2296 | sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128, | 
|  | 2297 | 128, PIXEL_FORMAT_RGBA_8888, 0); | 
| Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 2298 | fillSurfaceRGBA8(relative, 255, 255, 255); | 
|  | 2299 |  | 
|  | 2300 | Transaction t; | 
|  | 2301 | t.setLayer(relative, INT32_MAX) | 
|  | 2302 | .setRelativeLayer(mChild, relative->getHandle(), 1) | 
|  | 2303 | .setPosition(mFGSurfaceControl, 0, 0) | 
|  | 2304 | .apply(true); | 
|  | 2305 |  | 
|  | 2306 | // We expect that the child should have been elevated above our | 
|  | 2307 | // INT_MAX layer even though it's not a child of it. | 
|  | 2308 | { | 
|  | 2309 | ScreenCapture::captureScreen(&mCapture); | 
|  | 2310 | mCapture->expectChildColor(0, 0); | 
|  | 2311 | mCapture->expectChildColor(9, 9); | 
|  | 2312 | mCapture->checkPixel(10, 10, 255, 255, 255); | 
|  | 2313 | } | 
|  | 2314 | } | 
|  | 2315 |  | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2316 | class ScreenCaptureTest : public LayerUpdateTest { | 
|  | 2317 | protected: | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2318 | std::unique_ptr<ScreenCapture> mCapture; | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2319 | }; | 
|  | 2320 |  | 
|  | 2321 | TEST_F(ScreenCaptureTest, CaptureSingleLayer) { | 
|  | 2322 | auto bgHandle = mBGSurfaceControl->getHandle(); | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2323 | ScreenCapture::captureLayers(&mCapture, bgHandle); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2324 | mCapture->expectBGColor(0, 0); | 
|  | 2325 | // Doesn't capture FG layer which is at 64, 64 | 
|  | 2326 | mCapture->expectBGColor(64, 64); | 
|  | 2327 | } | 
|  | 2328 |  | 
|  | 2329 | TEST_F(ScreenCaptureTest, CaptureLayerWithChild) { | 
|  | 2330 | auto fgHandle = mFGSurfaceControl->getHandle(); | 
|  | 2331 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2332 | sp<SurfaceControl> child = | 
|  | 2333 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2334 | 0, mFGSurfaceControl.get()); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2335 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2336 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2337 | SurfaceComposerClient::Transaction().show(child).apply(true); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2338 |  | 
|  | 2339 | // Captures mFGSurfaceControl layer and its child. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2340 | ScreenCapture::captureLayers(&mCapture, fgHandle); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2341 | mCapture->expectFGColor(10, 10); | 
|  | 2342 | mCapture->expectChildColor(0, 0); | 
|  | 2343 | } | 
|  | 2344 |  | 
| Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 2345 | TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) { | 
|  | 2346 | auto fgHandle = mFGSurfaceControl->getHandle(); | 
|  | 2347 |  | 
|  | 2348 | sp<SurfaceControl> child = | 
|  | 2349 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2350 | 0, mFGSurfaceControl.get()); | 
|  | 2351 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2352 |  | 
|  | 2353 | SurfaceComposerClient::Transaction().show(child).apply(true); | 
|  | 2354 |  | 
|  | 2355 | // Captures mFGSurfaceControl's child | 
|  | 2356 | ScreenCapture::captureChildLayers(&mCapture, fgHandle); | 
|  | 2357 | mCapture->checkPixel(10, 10, 0, 0, 0); | 
|  | 2358 | mCapture->expectChildColor(0, 0); | 
|  | 2359 | } | 
|  | 2360 |  | 
| chaviw | 50da504 | 2018-04-09 13:49:37 -0700 | [diff] [blame] | 2361 | TEST_F(ScreenCaptureTest, CaptureTransparent) { | 
|  | 2362 | sp<SurfaceControl> child = | 
|  | 2363 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2364 | 0, mFGSurfaceControl.get()); | 
|  | 2365 |  | 
|  | 2366 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2367 |  | 
|  | 2368 | SurfaceComposerClient::Transaction().show(child).apply(true); | 
|  | 2369 |  | 
|  | 2370 | auto childHandle = child->getHandle(); | 
|  | 2371 |  | 
|  | 2372 | // Captures child | 
|  | 2373 | ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20}); | 
|  | 2374 | mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255}); | 
|  | 2375 | // Area outside of child's bounds is transparent. | 
|  | 2376 | mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0}); | 
|  | 2377 | } | 
|  | 2378 |  | 
| chaviw | 4b129c2 | 2018-04-09 16:19:43 -0700 | [diff] [blame] | 2379 | TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) { | 
|  | 2380 | auto fgHandle = mFGSurfaceControl->getHandle(); | 
|  | 2381 |  | 
|  | 2382 | sp<SurfaceControl> child = | 
|  | 2383 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2384 | 0, mFGSurfaceControl.get()); | 
|  | 2385 | sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 10, | 
|  | 2386 | 10, PIXEL_FORMAT_RGBA_8888, 0); | 
|  | 2387 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2388 | fillSurfaceRGBA8(relative, 100, 100, 100); | 
|  | 2389 |  | 
|  | 2390 | SurfaceComposerClient::Transaction() | 
|  | 2391 | .show(child) | 
|  | 2392 | // Set relative layer above fg layer so should be shown above when computing all layers. | 
|  | 2393 | .setRelativeLayer(relative, fgHandle, 1) | 
|  | 2394 | .show(relative) | 
|  | 2395 | .apply(true); | 
|  | 2396 |  | 
|  | 2397 | // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured. | 
|  | 2398 | ScreenCapture::captureLayers(&mCapture, fgHandle); | 
|  | 2399 | mCapture->expectFGColor(10, 10); | 
|  | 2400 | mCapture->expectChildColor(0, 0); | 
|  | 2401 | } | 
|  | 2402 |  | 
|  | 2403 | TEST_F(ScreenCaptureTest, CaptureRelativeInTree) { | 
|  | 2404 | auto fgHandle = mFGSurfaceControl->getHandle(); | 
|  | 2405 |  | 
|  | 2406 | sp<SurfaceControl> child = | 
|  | 2407 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2408 | 0, mFGSurfaceControl.get()); | 
|  | 2409 | sp<SurfaceControl> relative = | 
|  | 2410 | mComposerClient->createSurface(String8("Relative surface"), 10, 10, | 
|  | 2411 | PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get()); | 
|  | 2412 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2413 | fillSurfaceRGBA8(relative, 100, 100, 100); | 
|  | 2414 |  | 
|  | 2415 | SurfaceComposerClient::Transaction() | 
|  | 2416 | .show(child) | 
|  | 2417 | // Set relative layer below fg layer but relative to child layer so it should be shown | 
|  | 2418 | // above child layer. | 
|  | 2419 | .setLayer(relative, -1) | 
|  | 2420 | .setRelativeLayer(relative, child->getHandle(), 1) | 
|  | 2421 | .show(relative) | 
|  | 2422 | .apply(true); | 
|  | 2423 |  | 
|  | 2424 | // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its | 
|  | 2425 | // relative value should be taken into account, placing it above child layer. | 
|  | 2426 | ScreenCapture::captureLayers(&mCapture, fgHandle); | 
|  | 2427 | mCapture->expectFGColor(10, 10); | 
|  | 2428 | // Relative layer is showing on top of child layer | 
|  | 2429 | mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255}); | 
|  | 2430 | } | 
| Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 2431 |  | 
|  | 2432 | // In the following tests we verify successful skipping of a parent layer, | 
|  | 2433 | // so we use the same verification logic and only change how we mutate | 
|  | 2434 | // the parent layer to verify that various properties are ignored. | 
|  | 2435 | class ScreenCaptureChildOnlyTest : public LayerUpdateTest { | 
|  | 2436 | public: | 
|  | 2437 | void SetUp() override { | 
|  | 2438 | LayerUpdateTest::SetUp(); | 
|  | 2439 |  | 
|  | 2440 | mChild = | 
|  | 2441 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2442 | 0, mFGSurfaceControl.get()); | 
|  | 2443 | fillSurfaceRGBA8(mChild, 200, 200, 200); | 
|  | 2444 |  | 
|  | 2445 | SurfaceComposerClient::Transaction().show(mChild).apply(true); | 
|  | 2446 | } | 
|  | 2447 |  | 
|  | 2448 | void verify() { | 
|  | 2449 | auto fgHandle = mFGSurfaceControl->getHandle(); | 
|  | 2450 | ScreenCapture::captureChildLayers(&mCapture, fgHandle); | 
|  | 2451 | mCapture->checkPixel(10, 10, 0, 0, 0); | 
|  | 2452 | mCapture->expectChildColor(0, 0); | 
|  | 2453 | } | 
|  | 2454 |  | 
|  | 2455 | std::unique_ptr<ScreenCapture> mCapture; | 
|  | 2456 | sp<SurfaceControl> mChild; | 
|  | 2457 | }; | 
|  | 2458 |  | 
|  | 2459 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) { | 
|  | 2460 |  | 
|  | 2461 | SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true); | 
|  | 2462 |  | 
|  | 2463 | // Even though the parent is hidden we should still capture the child. | 
|  | 2464 | verify(); | 
|  | 2465 | } | 
|  | 2466 |  | 
|  | 2467 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) { | 
|  | 2468 |  | 
|  | 2469 | SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true); | 
|  | 2470 |  | 
|  | 2471 | // Even though the parent is cropped out we should still capture the child. | 
|  | 2472 | verify(); | 
|  | 2473 | } | 
|  | 2474 |  | 
|  | 2475 | TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) { | 
|  | 2476 |  | 
|  | 2477 | SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2); | 
|  | 2478 |  | 
|  | 2479 | // We should not inherit the parent scaling. | 
|  | 2480 | verify(); | 
|  | 2481 | } | 
|  | 2482 |  | 
| Robert Carr | 15eae09 | 2018-03-23 13:43:53 -0700 | [diff] [blame] | 2483 | TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) { | 
|  | 2484 | SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true); | 
|  | 2485 |  | 
|  | 2486 | // Even though the parent is hidden we should still capture the child. | 
|  | 2487 | verify(); | 
|  | 2488 |  | 
|  | 2489 | // Verify everything was properly hidden when rendering the full-screen. | 
|  | 2490 | screenshot()->expectBGColor(0,0); | 
|  | 2491 | } | 
|  | 2492 |  | 
|  | 2493 |  | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2494 | TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) { | 
|  | 2495 | auto fgHandle = mFGSurfaceControl->getHandle(); | 
|  | 2496 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2497 | sp<SurfaceControl> child = | 
|  | 2498 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2499 | 0, mFGSurfaceControl.get()); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2500 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2501 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2502 | sp<SurfaceControl> grandchild = | 
|  | 2503 | mComposerClient->createSurface(String8("Grandchild surface"), 5, 5, | 
|  | 2504 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2505 |  | 
|  | 2506 | fillSurfaceRGBA8(grandchild, 50, 50, 50); | 
|  | 2507 | SurfaceComposerClient::Transaction() | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2508 | .show(child) | 
|  | 2509 | .setPosition(grandchild, 5, 5) | 
|  | 2510 | .show(grandchild) | 
|  | 2511 | .apply(true); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2512 |  | 
|  | 2513 | // Captures mFGSurfaceControl, its child, and the grandchild. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2514 | ScreenCapture::captureLayers(&mCapture, fgHandle); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2515 | mCapture->expectFGColor(10, 10); | 
|  | 2516 | mCapture->expectChildColor(0, 0); | 
|  | 2517 | mCapture->checkPixel(5, 5, 50, 50, 50); | 
|  | 2518 | } | 
|  | 2519 |  | 
|  | 2520 | TEST_F(ScreenCaptureTest, CaptureChildOnly) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2521 | sp<SurfaceControl> child = | 
|  | 2522 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2523 | 0, mFGSurfaceControl.get()); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2524 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2525 | auto childHandle = child->getHandle(); | 
|  | 2526 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2527 | SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2528 |  | 
|  | 2529 | // Captures only the child layer, and not the parent. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2530 | ScreenCapture::captureLayers(&mCapture, childHandle); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2531 | mCapture->expectChildColor(0, 0); | 
|  | 2532 | mCapture->expectChildColor(9, 9); | 
|  | 2533 | } | 
|  | 2534 |  | 
|  | 2535 | TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) { | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2536 | sp<SurfaceControl> child = | 
|  | 2537 | mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, | 
|  | 2538 | 0, mFGSurfaceControl.get()); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2539 | fillSurfaceRGBA8(child, 200, 200, 200); | 
|  | 2540 | auto childHandle = child->getHandle(); | 
|  | 2541 |  | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2542 | sp<SurfaceControl> grandchild = | 
|  | 2543 | mComposerClient->createSurface(String8("Grandchild surface"), 5, 5, | 
|  | 2544 | PIXEL_FORMAT_RGBA_8888, 0, child.get()); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2545 | fillSurfaceRGBA8(grandchild, 50, 50, 50); | 
|  | 2546 |  | 
|  | 2547 | SurfaceComposerClient::Transaction() | 
| Chia-I Wu | 1078bbb | 2017-10-20 11:29:02 -0700 | [diff] [blame] | 2548 | .show(child) | 
|  | 2549 | .setPosition(grandchild, 5, 5) | 
|  | 2550 | .show(grandchild) | 
|  | 2551 | .apply(true); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2552 |  | 
|  | 2553 | auto grandchildHandle = grandchild->getHandle(); | 
|  | 2554 |  | 
|  | 2555 | // Captures only the grandchild. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2556 | ScreenCapture::captureLayers(&mCapture, grandchildHandle); | 
| chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2557 | mCapture->checkPixel(0, 0, 50, 50, 50); | 
|  | 2558 | mCapture->checkPixel(4, 4, 50, 50, 50); | 
|  | 2559 | } | 
|  | 2560 |  | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2561 | TEST_F(ScreenCaptureTest, CaptureCrop) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2562 | sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60, | 
|  | 2563 | PIXEL_FORMAT_RGBA_8888, 0); | 
|  | 2564 | sp<SurfaceControl> blueLayer = | 
|  | 2565 | mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888, | 
|  | 2566 | 0, redLayer.get()); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2567 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2568 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED)); | 
|  | 2569 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE)); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2570 |  | 
|  | 2571 | SurfaceComposerClient::Transaction() | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2572 | .setLayer(redLayer, INT32_MAX - 1) | 
|  | 2573 | .show(redLayer) | 
|  | 2574 | .show(blueLayer) | 
|  | 2575 | .apply(true); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2576 |  | 
|  | 2577 | auto redLayerHandle = redLayer->getHandle(); | 
|  | 2578 |  | 
|  | 2579 | // Capturing full screen should have both red and blue are visible. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2580 | ScreenCapture::captureLayers(&mCapture, redLayerHandle); | 
|  | 2581 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); | 
|  | 2582 | // red area below the blue area | 
|  | 2583 | mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED); | 
|  | 2584 | // red area to the right of the blue area | 
|  | 2585 | mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2586 |  | 
|  | 2587 | Rect crop = Rect(0, 0, 30, 30); | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2588 | ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2589 | // Capturing the cropped screen, cropping out the shown red area, should leave only the blue | 
|  | 2590 | // area visible. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2591 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2592 | mCapture->checkPixel(30, 30, 0, 0, 0); | 
|  | 2593 | } | 
|  | 2594 |  | 
|  | 2595 | TEST_F(ScreenCaptureTest, CaptureSize) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2596 | sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60, | 
|  | 2597 | PIXEL_FORMAT_RGBA_8888, 0); | 
|  | 2598 | sp<SurfaceControl> blueLayer = | 
|  | 2599 | mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888, | 
|  | 2600 | 0, redLayer.get()); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2601 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2602 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED)); | 
|  | 2603 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE)); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2604 |  | 
|  | 2605 | SurfaceComposerClient::Transaction() | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2606 | .setLayer(redLayer, INT32_MAX - 1) | 
|  | 2607 | .show(redLayer) | 
|  | 2608 | .show(blueLayer) | 
|  | 2609 | .apply(true); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2610 |  | 
|  | 2611 | auto redLayerHandle = redLayer->getHandle(); | 
|  | 2612 |  | 
|  | 2613 | // Capturing full screen should have both red and blue are visible. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2614 | ScreenCapture::captureLayers(&mCapture, redLayerHandle); | 
|  | 2615 | mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE); | 
|  | 2616 | // red area below the blue area | 
|  | 2617 | mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED); | 
|  | 2618 | // red area to the right of the blue area | 
|  | 2619 | mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2620 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2621 | ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2622 | // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area. | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2623 | mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE); | 
|  | 2624 | // red area below the blue area | 
|  | 2625 | mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED); | 
|  | 2626 | // red area to the right of the blue area | 
|  | 2627 | mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2628 | mCapture->checkPixel(30, 30, 0, 0, 0); | 
|  | 2629 | } | 
|  | 2630 |  | 
|  | 2631 | TEST_F(ScreenCaptureTest, CaptureInvalidLayer) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2632 | sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60, | 
|  | 2633 | PIXEL_FORMAT_RGBA_8888, 0); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2634 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2635 | ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED)); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2636 |  | 
|  | 2637 | auto redLayerHandle = redLayer->getHandle(); | 
|  | 2638 | mComposerClient->destroySurface(redLayerHandle); | 
|  | 2639 | SurfaceComposerClient::Transaction().apply(true); | 
|  | 2640 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2641 | sp<GraphicBuffer> outBuffer; | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2642 |  | 
|  | 2643 | // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2644 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); | 
|  | 2645 | ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0)); | 
| chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 2646 | } | 
|  | 2647 |  | 
| chaviw | 8e3fe5d | 2018-02-22 10:55:42 -0800 | [diff] [blame] | 2648 |  | 
|  | 2649 | class DereferenceSurfaceControlTest : public LayerTransactionTest { | 
|  | 2650 | protected: | 
|  | 2651 | void SetUp() override { | 
|  | 2652 | LayerTransactionTest::SetUp(); | 
|  | 2653 | bgLayer = createLayer("BG layer", 20, 20); | 
|  | 2654 | fillLayerColor(bgLayer, Color::RED); | 
|  | 2655 | fgLayer = createLayer("FG layer", 20, 20); | 
|  | 2656 | fillLayerColor(fgLayer, Color::BLUE); | 
|  | 2657 | Transaction().setLayer(fgLayer, mLayerZBase + 1).apply(); | 
|  | 2658 | { | 
|  | 2659 | SCOPED_TRACE("before anything"); | 
|  | 2660 | auto shot = screenshot(); | 
|  | 2661 | shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE); | 
|  | 2662 | } | 
|  | 2663 | } | 
|  | 2664 | void TearDown() override { | 
|  | 2665 | LayerTransactionTest::TearDown(); | 
|  | 2666 | bgLayer = 0; | 
|  | 2667 | fgLayer = 0; | 
|  | 2668 | } | 
|  | 2669 |  | 
|  | 2670 | sp<SurfaceControl> bgLayer; | 
|  | 2671 | sp<SurfaceControl> fgLayer; | 
|  | 2672 | }; | 
|  | 2673 |  | 
|  | 2674 | TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) { | 
|  | 2675 | fgLayer = nullptr; | 
|  | 2676 | { | 
|  | 2677 | SCOPED_TRACE("after setting null"); | 
|  | 2678 | auto shot = screenshot(); | 
|  | 2679 | shot->expectColor(Rect(0, 0, 20, 20), Color::RED); | 
|  | 2680 | } | 
|  | 2681 | } | 
|  | 2682 |  | 
|  | 2683 | TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) { | 
|  | 2684 | auto transaction = Transaction().show(fgLayer); | 
|  | 2685 | fgLayer = nullptr; | 
|  | 2686 | { | 
|  | 2687 | SCOPED_TRACE("after setting null"); | 
|  | 2688 | auto shot = screenshot(); | 
|  | 2689 | shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE); | 
|  | 2690 | } | 
|  | 2691 | } | 
|  | 2692 |  | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2693 | } // namespace android |