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 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 19 | #include <android/native_window.h> |
| 20 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 21 | #include <binder/IMemory.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 22 | |
| 23 | #include <gui/ISurfaceComposer.h> |
| 24 | #include <gui/Surface.h> |
| 25 | #include <gui/SurfaceComposerClient.h> |
| 26 | #include <private/gui/ComposerService.h> |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 27 | #include <private/gui/LayerState.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 28 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 29 | #include <utils/String8.h> |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 30 | #include <ui/DisplayInfo.h> |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 31 | |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 32 | #include <math.h> |
| 33 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
| 36 | // Fill an RGBA_8888 formatted surface with a single color. |
| 37 | static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, |
| 38 | uint8_t r, uint8_t g, uint8_t b) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 39 | ANativeWindow_Buffer outBuffer; |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 40 | sp<Surface> s = sc->getSurface(); |
| 41 | ASSERT_TRUE(s != NULL); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 42 | ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL)); |
| 43 | uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 44 | for (int y = 0; y < outBuffer.height; y++) { |
| 45 | for (int x = 0; x < outBuffer.width; x++) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 46 | uint8_t* pixel = img + (4 * (y*outBuffer.stride + x)); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 47 | pixel[0] = r; |
| 48 | pixel[1] = g; |
| 49 | pixel[2] = b; |
| 50 | pixel[3] = 255; |
| 51 | } |
| 52 | } |
| 53 | ASSERT_EQ(NO_ERROR, s->unlockAndPost()); |
| 54 | } |
| 55 | |
| 56 | // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check |
| 57 | // individual pixel values for testing purposes. |
| 58 | class ScreenCapture : public RefBase { |
| 59 | public: |
| 60 | static void captureScreen(sp<ScreenCapture>* sc) { |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 61 | sp<IGraphicBufferProducer> producer; |
| 62 | sp<IGraphicBufferConsumer> consumer; |
| 63 | BufferQueue::createBufferQueue(&producer, &consumer); |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 64 | sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 65 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 66 | sp<IBinder> display(sf->getBuiltInDisplay( |
| 67 | ISurfaceComposer::eDisplayIdMain)); |
Pablo Ceballos | 15311bd | 2016-06-01 18:53:40 -0700 | [diff] [blame] | 68 | SurfaceComposerClient::openGlobalTransaction(); |
| 69 | SurfaceComposerClient::closeGlobalTransaction(true); |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 70 | ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0, |
| 71 | 0, INT_MAX, false)); |
| 72 | *sc = new ScreenCapture(cpuConsumer); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 76 | ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format); |
| 77 | const uint8_t* img = static_cast<const uint8_t*>(mBuf.data); |
| 78 | const uint8_t* pixel = img + (4 * (y * mBuf.stride + x)); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 79 | if (r != pixel[0] || g != pixel[1] || b != pixel[2]) { |
| 80 | String8 err(String8::format("pixel @ (%3d, %3d): " |
| 81 | "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]", |
| 82 | x, y, r, g, b, pixel[0], pixel[1], pixel[2])); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 83 | EXPECT_EQ(String8(), err) << err.string(); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 87 | void expectFGColor(uint32_t x, uint32_t y) { |
| 88 | checkPixel(x, y, 195, 63, 63); |
| 89 | } |
| 90 | |
| 91 | void expectBGColor(uint32_t x, uint32_t y) { |
| 92 | checkPixel(x, y, 63, 63, 195); |
| 93 | } |
| 94 | |
| 95 | void expectChildColor(uint32_t x, uint32_t y) { |
| 96 | checkPixel(x, y, 200, 200, 200); |
| 97 | } |
| 98 | |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 99 | private: |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 100 | ScreenCapture(const sp<CpuConsumer>& cc) : |
| 101 | mCC(cc) { |
| 102 | EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf)); |
| 103 | } |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 104 | |
Michael Lentine | 5a16a62 | 2015-05-21 13:48:24 -0700 | [diff] [blame] | 105 | ~ScreenCapture() { |
| 106 | mCC->unlockBuffer(mBuf); |
| 107 | } |
| 108 | |
| 109 | sp<CpuConsumer> mCC; |
| 110 | CpuConsumer::LockedBuffer mBuf; |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | class LayerUpdateTest : public ::testing::Test { |
| 114 | protected: |
| 115 | virtual void SetUp() { |
| 116 | mComposerClient = new SurfaceComposerClient; |
| 117 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
| 118 | |
Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 119 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay( |
| 120 | ISurfaceComposer::eDisplayIdMain)); |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 121 | DisplayInfo info; |
Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 122 | SurfaceComposerClient::getDisplayInfo(display, &info); |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 123 | |
| 124 | ssize_t displayWidth = info.w; |
| 125 | ssize_t displayHeight = info.h; |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 126 | |
| 127 | // Background surface |
| 128 | mBGSurfaceControl = mComposerClient->createSurface( |
Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 129 | String8("BG Test Surface"), displayWidth, displayHeight, |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 130 | PIXEL_FORMAT_RGBA_8888, 0); |
| 131 | ASSERT_TRUE(mBGSurfaceControl != NULL); |
| 132 | ASSERT_TRUE(mBGSurfaceControl->isValid()); |
| 133 | fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195); |
| 134 | |
| 135 | // Foreground surface |
| 136 | mFGSurfaceControl = mComposerClient->createSurface( |
Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 137 | String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(mFGSurfaceControl != NULL); |
| 139 | ASSERT_TRUE(mFGSurfaceControl->isValid()); |
| 140 | |
| 141 | fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); |
| 142 | |
| 143 | // Synchronization surface |
| 144 | mSyncSurfaceControl = mComposerClient->createSurface( |
Jeff Brown | 9d4e3d2 | 2012-08-24 20:00:51 -0700 | [diff] [blame] | 145 | String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 146 | ASSERT_TRUE(mSyncSurfaceControl != NULL); |
| 147 | ASSERT_TRUE(mSyncSurfaceControl->isValid()); |
| 148 | |
| 149 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 150 | |
| 151 | SurfaceComposerClient::openGlobalTransaction(); |
| 152 | |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 153 | mComposerClient->setDisplayLayerStack(display, 0); |
| 154 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 155 | ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT32_MAX-2)); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 156 | ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show()); |
| 157 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 158 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT32_MAX-1)); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 159 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64)); |
| 160 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show()); |
| 161 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 162 | ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT32_MAX-1)); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 163 | ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setPosition(displayWidth-2, |
| 164 | displayHeight-2)); |
| 165 | ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->show()); |
| 166 | |
| 167 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 168 | } |
| 169 | |
| 170 | virtual void TearDown() { |
| 171 | mComposerClient->dispose(); |
| 172 | mBGSurfaceControl = 0; |
| 173 | mFGSurfaceControl = 0; |
| 174 | mSyncSurfaceControl = 0; |
| 175 | mComposerClient = 0; |
| 176 | } |
| 177 | |
| 178 | void waitForPostedBuffers() { |
| 179 | // Since the sync surface is in synchronous mode (i.e. double buffered) |
| 180 | // posting three buffers to it should ensure that at least two |
| 181 | // SurfaceFlinger::handlePageFlip calls have been made, which should |
| 182 | // guaranteed that a buffer posted to another Surface has been retired. |
| 183 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 184 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 185 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 186 | } |
| 187 | |
| 188 | sp<SurfaceComposerClient> mComposerClient; |
| 189 | sp<SurfaceControl> mBGSurfaceControl; |
| 190 | sp<SurfaceControl> mFGSurfaceControl; |
| 191 | |
| 192 | // This surface is used to ensure that the buffers posted to |
| 193 | // mFGSurfaceControl have been picked up by SurfaceFlinger. |
| 194 | sp<SurfaceControl> mSyncSurfaceControl; |
| 195 | }; |
| 196 | |
| 197 | TEST_F(LayerUpdateTest, LayerMoveWorks) { |
| 198 | sp<ScreenCapture> sc; |
| 199 | { |
| 200 | SCOPED_TRACE("before move"); |
| 201 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 202 | sc->expectBGColor(0, 12); |
| 203 | sc->expectFGColor(75, 75); |
| 204 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | SurfaceComposerClient::openGlobalTransaction(); |
| 208 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128, 128)); |
| 209 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 210 | { |
| 211 | // This should reflect the new position, but not the new color. |
| 212 | SCOPED_TRACE("after move, before redraw"); |
| 213 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 214 | sc->expectBGColor(24, 24); |
| 215 | sc->expectBGColor(75, 75); |
| 216 | sc->expectFGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63); |
| 220 | waitForPostedBuffers(); |
| 221 | { |
| 222 | // This should reflect the new position and the new color. |
| 223 | SCOPED_TRACE("after redraw"); |
| 224 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 225 | sc->expectBGColor(24, 24); |
| 226 | sc->expectBGColor(75, 75); |
| 227 | sc->checkPixel(145, 145, 63, 195, 63); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | TEST_F(LayerUpdateTest, LayerResizeWorks) { |
| 232 | sp<ScreenCapture> sc; |
| 233 | { |
| 234 | SCOPED_TRACE("before resize"); |
| 235 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 236 | sc->expectBGColor(0, 12); |
| 237 | sc->expectFGColor(75, 75); |
| 238 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 241 | ALOGD("resizing"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 242 | SurfaceComposerClient::openGlobalTransaction(); |
| 243 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128)); |
| 244 | SurfaceComposerClient::closeGlobalTransaction(true); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 245 | ALOGD("resized"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 246 | { |
| 247 | // This should not reflect the new size or color because SurfaceFlinger |
| 248 | // has not yet received a buffer of the correct size. |
| 249 | SCOPED_TRACE("after resize, before redraw"); |
| 250 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 251 | sc->expectBGColor(0, 12); |
| 252 | sc->expectFGColor(75, 75); |
| 253 | sc->expectBGColor(145, 145); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 256 | ALOGD("drawing"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 257 | fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63); |
| 258 | waitForPostedBuffers(); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 259 | ALOGD("drawn"); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 260 | { |
| 261 | // This should reflect the new size and the new color. |
| 262 | SCOPED_TRACE("after redraw"); |
| 263 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 264 | sc->expectBGColor(24, 24); |
| 265 | sc->checkPixel(75, 75, 63, 195, 63); |
| 266 | sc->checkPixel(145, 145, 63, 195, 63); |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 270 | TEST_F(LayerUpdateTest, LayerCropWorks) { |
| 271 | sp<ScreenCapture> sc; |
| 272 | { |
| 273 | SCOPED_TRACE("before crop"); |
| 274 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 275 | sc->expectBGColor(24, 24); |
| 276 | sc->expectFGColor(75, 75); |
| 277 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | SurfaceComposerClient::openGlobalTransaction(); |
| 281 | Rect cropRect(16, 16, 32, 32); |
| 282 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setCrop(cropRect)); |
| 283 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 284 | { |
| 285 | // This should crop the foreground surface. |
| 286 | SCOPED_TRACE("after crop"); |
| 287 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 288 | sc->expectBGColor(24, 24); |
| 289 | sc->expectBGColor(75, 75); |
| 290 | sc->expectFGColor(95, 80); |
| 291 | sc->expectFGColor(80, 95); |
| 292 | sc->expectBGColor(96, 96); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 296 | TEST_F(LayerUpdateTest, LayerFinalCropWorks) { |
| 297 | sp<ScreenCapture> sc; |
| 298 | { |
| 299 | SCOPED_TRACE("before crop"); |
| 300 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 301 | sc->expectBGColor(24, 24); |
| 302 | sc->expectFGColor(75, 75); |
| 303 | sc->expectBGColor(145, 145); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 304 | } |
| 305 | SurfaceComposerClient::openGlobalTransaction(); |
| 306 | Rect cropRect(16, 16, 32, 32); |
| 307 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFinalCrop(cropRect)); |
| 308 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 309 | { |
| 310 | // This should crop the foreground surface. |
| 311 | SCOPED_TRACE("after crop"); |
| 312 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 313 | sc->expectBGColor(24, 24); |
| 314 | sc->expectBGColor(75, 75); |
| 315 | sc->expectBGColor(95, 80); |
| 316 | sc->expectBGColor(80, 95); |
| 317 | sc->expectBGColor(96, 96); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 321 | TEST_F(LayerUpdateTest, LayerSetLayerWorks) { |
| 322 | sp<ScreenCapture> sc; |
| 323 | { |
| 324 | SCOPED_TRACE("before setLayer"); |
| 325 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 326 | sc->expectBGColor(24, 24); |
| 327 | sc->expectFGColor(75, 75); |
| 328 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | SurfaceComposerClient::openGlobalTransaction(); |
| 332 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT_MAX - 3)); |
| 333 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 334 | { |
| 335 | // This should hide the foreground surface beneath the background. |
| 336 | SCOPED_TRACE("after setLayer"); |
| 337 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 338 | sc->expectBGColor(24, 24); |
| 339 | sc->expectBGColor(75, 75); |
| 340 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | TEST_F(LayerUpdateTest, LayerShowHideWorks) { |
| 345 | sp<ScreenCapture> sc; |
| 346 | { |
| 347 | SCOPED_TRACE("before hide"); |
| 348 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 349 | sc->expectBGColor(24, 24); |
| 350 | sc->expectFGColor(75, 75); |
| 351 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | SurfaceComposerClient::openGlobalTransaction(); |
| 355 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->hide()); |
| 356 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 357 | { |
| 358 | // This should hide the foreground surface. |
| 359 | SCOPED_TRACE("after hide, before show"); |
| 360 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 361 | sc->expectBGColor(24, 24); |
| 362 | sc->expectBGColor(75, 75); |
| 363 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | SurfaceComposerClient::openGlobalTransaction(); |
| 367 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show()); |
| 368 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 369 | { |
| 370 | // This should show the foreground surface. |
| 371 | SCOPED_TRACE("after show"); |
| 372 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 373 | sc->expectBGColor(24, 24); |
| 374 | sc->expectFGColor(75, 75); |
| 375 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | TEST_F(LayerUpdateTest, LayerSetAlphaWorks) { |
| 380 | sp<ScreenCapture> sc; |
| 381 | { |
| 382 | SCOPED_TRACE("before setAlpha"); |
| 383 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 384 | sc->expectBGColor(24, 24); |
| 385 | sc->expectFGColor(75, 75); |
| 386 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | SurfaceComposerClient::openGlobalTransaction(); |
| 390 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75f)); |
| 391 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 392 | { |
| 393 | // This should set foreground to be 75% opaque. |
| 394 | SCOPED_TRACE("after setAlpha"); |
| 395 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 396 | sc->expectBGColor(24, 24); |
| 397 | sc->checkPixel(75, 75, 162, 63, 96); |
| 398 | sc->expectBGColor(145, 145); |
Haixia Shi | d575096 | 2015-07-27 16:50:49 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 402 | TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) { |
| 403 | sp<ScreenCapture> sc; |
| 404 | { |
| 405 | SCOPED_TRACE("before setLayerStack"); |
| 406 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 407 | sc->expectBGColor(24, 24); |
| 408 | sc->expectFGColor(75, 75); |
| 409 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | SurfaceComposerClient::openGlobalTransaction(); |
| 413 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayerStack(1)); |
| 414 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 415 | { |
| 416 | // This should hide the foreground surface since it goes to a different |
| 417 | // layer stack. |
| 418 | SCOPED_TRACE("after setLayerStack"); |
| 419 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 420 | sc->expectBGColor(24, 24); |
| 421 | sc->expectBGColor(75, 75); |
| 422 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | TEST_F(LayerUpdateTest, LayerSetFlagsWorks) { |
| 427 | sp<ScreenCapture> sc; |
| 428 | { |
| 429 | SCOPED_TRACE("before setFlags"); |
| 430 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 431 | sc->expectBGColor(24, 24); |
| 432 | sc->expectFGColor(75, 75); |
| 433 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | SurfaceComposerClient::openGlobalTransaction(); |
| 437 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFlags( |
| 438 | layer_state_t::eLayerHidden, layer_state_t::eLayerHidden)); |
| 439 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 440 | { |
| 441 | // This should hide the foreground surface |
| 442 | SCOPED_TRACE("after setFlags"); |
| 443 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 444 | sc->expectBGColor(24, 24); |
| 445 | sc->expectBGColor(75, 75); |
| 446 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | TEST_F(LayerUpdateTest, LayerSetMatrixWorks) { |
| 451 | sp<ScreenCapture> sc; |
| 452 | { |
| 453 | SCOPED_TRACE("before setMatrix"); |
| 454 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 455 | sc->expectBGColor(24, 24); |
| 456 | sc->expectFGColor(91, 96); |
| 457 | sc->expectFGColor(96, 101); |
| 458 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | SurfaceComposerClient::openGlobalTransaction(); |
| 462 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setMatrix(M_SQRT1_2, M_SQRT1_2, |
| 463 | -M_SQRT1_2, M_SQRT1_2)); |
| 464 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 465 | { |
| 466 | SCOPED_TRACE("after setMatrix"); |
| 467 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 468 | sc->expectBGColor(24, 24); |
| 469 | sc->expectFGColor(91, 96); |
| 470 | sc->expectBGColor(96, 91); |
| 471 | sc->expectBGColor(145, 145); |
Pablo Ceballos | 5e4fcbe | 2015-09-02 09:53:16 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 475 | TEST_F(LayerUpdateTest, DeferredTransactionTest) { |
| 476 | sp<ScreenCapture> sc; |
| 477 | { |
| 478 | SCOPED_TRACE("before anything"); |
| 479 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 480 | sc->expectBGColor(32, 32); |
| 481 | sc->expectFGColor(96, 96); |
| 482 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // set up two deferred transactions on different frames |
| 486 | SurfaceComposerClient::openGlobalTransaction(); |
| 487 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75)); |
| 488 | mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(), |
| 489 | mSyncSurfaceControl->getSurface()->getNextFrameNumber()); |
| 490 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 491 | |
| 492 | SurfaceComposerClient::openGlobalTransaction(); |
| 493 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128)); |
| 494 | mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(), |
| 495 | mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1); |
| 496 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 497 | |
| 498 | { |
| 499 | SCOPED_TRACE("before any trigger"); |
| 500 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 501 | sc->expectBGColor(32, 32); |
| 502 | sc->expectFGColor(96, 96); |
| 503 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // should trigger the first deferred transaction, but not the second one |
| 507 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 508 | { |
| 509 | SCOPED_TRACE("after first trigger"); |
| 510 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 511 | sc->expectBGColor(32, 32); |
| 512 | sc->checkPixel(96, 96, 162, 63, 96); |
| 513 | sc->expectBGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // should show up immediately since it's not deferred |
| 517 | SurfaceComposerClient::openGlobalTransaction(); |
| 518 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0)); |
| 519 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 520 | |
| 521 | // trigger the second deferred transaction |
| 522 | fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); |
| 523 | { |
| 524 | SCOPED_TRACE("after second trigger"); |
| 525 | ScreenCapture::captureScreen(&sc); |
Robert Carr | 2b91c82 | 2017-02-21 19:41:24 -0800 | [diff] [blame^] | 526 | sc->expectBGColor(32, 32); |
| 527 | sc->expectBGColor(96, 96); |
| 528 | sc->expectFGColor(160, 160); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 532 | class ChildLayerTest : public LayerUpdateTest { |
| 533 | protected: |
| 534 | void SetUp() override { |
| 535 | LayerUpdateTest::SetUp(); |
| 536 | mChild = mComposerClient->createSurface( |
| 537 | String8("Child surface"), |
| 538 | 10, 10, PIXEL_FORMAT_RGBA_8888, |
| 539 | 0, mFGSurfaceControl.get()); |
| 540 | fillSurfaceRGBA8(mChild, 200, 200, 200); |
| 541 | |
| 542 | { |
| 543 | SCOPED_TRACE("before anything"); |
| 544 | ScreenCapture::captureScreen(&mCapture); |
| 545 | mCapture->expectChildColor(64, 64); |
| 546 | } |
| 547 | } |
| 548 | void TearDown() override { |
| 549 | LayerUpdateTest::TearDown(); |
| 550 | mChild = 0; |
| 551 | } |
| 552 | |
| 553 | sp<SurfaceControl> mChild; |
| 554 | sp<ScreenCapture> mCapture; |
| 555 | }; |
| 556 | |
| 557 | TEST_F(ChildLayerTest, ChildLayerPositioning) { |
| 558 | SurfaceComposerClient::openGlobalTransaction(); |
| 559 | mChild->show(); |
| 560 | mChild->setPosition(10, 10); |
| 561 | mFGSurfaceControl->setPosition(64, 64); |
| 562 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 563 | |
| 564 | { |
| 565 | ScreenCapture::captureScreen(&mCapture); |
| 566 | // Top left of foreground must now be visible |
| 567 | mCapture->expectFGColor(64, 64); |
| 568 | // But 10 pixels in we should see the child surface |
| 569 | mCapture->expectChildColor(74, 74); |
| 570 | // And 10 more pixels we should be back to the foreground surface |
| 571 | mCapture->expectFGColor(84, 84); |
| 572 | } |
| 573 | |
| 574 | SurfaceComposerClient::openGlobalTransaction(); |
| 575 | ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0)); |
| 576 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 577 | |
| 578 | { |
| 579 | ScreenCapture::captureScreen(&mCapture); |
| 580 | // Top left of foreground should now be at 0, 0 |
| 581 | mCapture->expectFGColor(0, 0); |
| 582 | // But 10 pixels in we should see the child surface |
| 583 | mCapture->expectChildColor(10, 10); |
| 584 | // And 10 more pixels we should be back to the foreground surface |
| 585 | mCapture->expectFGColor(20, 20); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | TEST_F(ChildLayerTest, ChildLayerConstraints) { |
| 590 | SurfaceComposerClient::openGlobalTransaction(); |
| 591 | mChild->show(); |
| 592 | mFGSurfaceControl->setPosition(0, 0); |
| 593 | mChild->setPosition(63, 63); |
| 594 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 595 | |
| 596 | { |
| 597 | ScreenCapture::captureScreen(&mCapture); |
| 598 | mCapture->expectFGColor(0, 0); |
| 599 | // Last pixel in foreground should now be the child. |
| 600 | mCapture->expectChildColor(63, 63); |
| 601 | // But the child should be constrained and the next pixel |
| 602 | // must be the background |
| 603 | mCapture->expectBGColor(64, 64); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | TEST_F(ChildLayerTest, ChildLayerScaling) { |
| 608 | SurfaceComposerClient::openGlobalTransaction(); |
| 609 | mFGSurfaceControl->setPosition(0, 0); |
| 610 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 611 | |
| 612 | // Find the boundary between the parent and child |
| 613 | { |
| 614 | ScreenCapture::captureScreen(&mCapture); |
| 615 | mCapture->expectChildColor(9, 9); |
| 616 | mCapture->expectFGColor(10, 10); |
| 617 | } |
| 618 | |
| 619 | SurfaceComposerClient::openGlobalTransaction(); |
| 620 | mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0); |
| 621 | SurfaceComposerClient::closeGlobalTransaction(true); |
| 622 | |
| 623 | // The boundary should be twice as far from the origin now. |
| 624 | // The pixels from the last test should all be child now |
| 625 | { |
| 626 | ScreenCapture::captureScreen(&mCapture); |
| 627 | mCapture->expectChildColor(9, 9); |
| 628 | mCapture->expectChildColor(10, 10); |
| 629 | mCapture->expectChildColor(19, 19); |
| 630 | mCapture->expectFGColor(20, 20); |
| 631 | } |
| 632 | } |
Jamie Gennis | 23c2c5d | 2011-10-11 19:22:19 -0700 | [diff] [blame] | 633 | } |