blob: 96b092d4cd431439a5750d4a65d0c8fcd24d702e [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
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 Lentine5a16a622015-05-21 13:48:24 -070019#include <android/native_window.h>
20
Mathias Agopian90ac7992012-02-25 18:48:35 -080021#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070022#include <gui/LayerState.h>
23
Mathias Agopian90ac7992012-02-25 18:48:35 -080024#include <gui/Surface.h>
25#include <gui/SurfaceComposerClient.h>
26#include <private/gui/ComposerService.h>
27
Mathias Agopianc666cae2012-07-25 18:56:13 -070028#include <ui/DisplayInfo.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070029#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070030
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070031#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070032#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070033
Robert Carr4cdc58f2017-08-23 14:22:20 -070034#include <functional>
35
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070036namespace android {
37
Robert Carr4cdc58f2017-08-23 14:22:20 -070038using Transaction = SurfaceComposerClient::Transaction;
39
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -070041static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
42 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070044 sp<Surface> s = sc->getSurface();
45 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -080046 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
47 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070048 for (int y = 0; y < outBuffer.height; y++) {
49 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -070050 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070051 pixel[0] = r;
52 pixel[1] = g;
53 pixel[2] = b;
54 pixel[3] = 255;
55 }
56 }
Robert Carr7bf247e2017-05-18 14:02:49 -070057 if (unlock) {
58 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
59 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070060}
61
62// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
63// individual pixel values for testing purposes.
64class ScreenCapture : public RefBase {
65public:
66 static void captureScreen(sp<ScreenCapture>* sc) {
Michael Lentine5a16a622015-05-21 13:48:24 -070067 sp<IGraphicBufferProducer> producer;
68 sp<IGraphicBufferConsumer> consumer;
69 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -070070 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070071 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -070072 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -070073 SurfaceComposerClient::Transaction().apply(true);
74
Chia-I Wu1078bbb2017-10-20 11:29:02 -070075 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0, 0, INT_MAX, false));
Michael Lentine5a16a622015-05-21 13:48:24 -070076 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070077 }
78
79 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -070080 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
81 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
82 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070083 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
84 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -070085 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
86 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070087 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070088 }
89 }
90
Chia-I Wu1078bbb2017-10-20 11:29:02 -070091 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -070092
Chia-I Wu1078bbb2017-10-20 11:29:02 -070093 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -070094
Chia-I Wu1078bbb2017-10-20 11:29:02 -070095 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -070096
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070097private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -070098 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -070099 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
100 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700101
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700102 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700103
104 sp<CpuConsumer> mCC;
105 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700106};
107
chaviwa76b2712017-09-20 12:02:26 -0700108class CaptureLayer {
109public:
110 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
111 sp<IGraphicBufferProducer> producer;
112 sp<IGraphicBufferConsumer> consumer;
113 BufferQueue::createBufferQueue(&producer, &consumer);
114 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
115 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700116 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700117 SurfaceComposerClient::Transaction().apply(true);
118 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
119 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
120 }
121
122 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
123 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
124 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
125 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
126 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
127 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700128 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700129 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
130 EXPECT_EQ(String8(), err) << err.string();
131 }
132 }
133
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700134 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700135
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700136 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700137
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700138 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700139
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700140 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700141 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
142 }
143
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700144 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700145
146private:
147 sp<CpuConsumer> mCC;
148 CpuConsumer::LockedBuffer mBuffer;
149};
150
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700151class LayerUpdateTest : public ::testing::Test {
152protected:
153 virtual void SetUp() {
154 mComposerClient = new SurfaceComposerClient;
155 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
156
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700157 sp<IBinder> display(
158 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700159 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700160 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700161
162 ssize_t displayWidth = info.w;
163 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700164
165 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700166 mBGSurfaceControl =
167 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
168 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700169 ASSERT_TRUE(mBGSurfaceControl != NULL);
170 ASSERT_TRUE(mBGSurfaceControl->isValid());
171 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
172
173 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700174 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
175 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700176 ASSERT_TRUE(mFGSurfaceControl != NULL);
177 ASSERT_TRUE(mFGSurfaceControl->isValid());
178
179 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
180
181 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700182 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
183 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700184 ASSERT_TRUE(mSyncSurfaceControl != NULL);
185 ASSERT_TRUE(mSyncSurfaceControl->isValid());
186
187 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
188
Robert Carr4cdc58f2017-08-23 14:22:20 -0700189 asTransaction([&](Transaction& t) {
190 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700191
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700192 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700193
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700194 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
195 .setPosition(mFGSurfaceControl, 64, 64)
196 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700197
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700198 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
199 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
200 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700201 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700202 }
203
204 virtual void TearDown() {
205 mComposerClient->dispose();
206 mBGSurfaceControl = 0;
207 mFGSurfaceControl = 0;
208 mSyncSurfaceControl = 0;
209 mComposerClient = 0;
210 }
211
212 void waitForPostedBuffers() {
213 // Since the sync surface is in synchronous mode (i.e. double buffered)
214 // posting three buffers to it should ensure that at least two
215 // SurfaceFlinger::handlePageFlip calls have been made, which should
216 // guaranteed that a buffer posted to another Surface has been retired.
217 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
218 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
219 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
220 }
221
Robert Carr4cdc58f2017-08-23 14:22:20 -0700222 void asTransaction(const std::function<void(Transaction&)>& exec) {
223 Transaction t;
224 exec(t);
225 t.apply(true);
226 }
227
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700228 sp<SurfaceComposerClient> mComposerClient;
229 sp<SurfaceControl> mBGSurfaceControl;
230 sp<SurfaceControl> mFGSurfaceControl;
231
232 // This surface is used to ensure that the buffers posted to
233 // mFGSurfaceControl have been picked up by SurfaceFlinger.
234 sp<SurfaceControl> mSyncSurfaceControl;
235};
236
Robert Carr7f619b22017-11-06 12:56:35 -0800237TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
238 sp<ScreenCapture> sc;
239
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700240 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
241 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -0800242 fillSurfaceRGBA8(relative, 10, 10, 10);
243 waitForPostedBuffers();
244
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700245 Transaction{}
246 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -0800247 .setPosition(relative, 64, 64)
248 .apply();
249
250 {
251 // The relative should be on top of the FG control.
252 ScreenCapture::captureScreen(&sc);
253 sc->checkPixel(64, 64, 10, 10, 10);
254 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700255 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800256
257 {
258 // Nothing should change at this point.
259 ScreenCapture::captureScreen(&sc);
260 sc->checkPixel(64, 64, 10, 10, 10);
261 }
262
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700263 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800264
265 {
266 // Ensure that the relative was actually hidden, rather than
267 // being left in the detached but visible state.
268 ScreenCapture::captureScreen(&sc);
269 sc->expectFGColor(64, 64);
270 }
271}
272
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700273TEST_F(LayerUpdateTest, LayerMoveWorks) {
274 sp<ScreenCapture> sc;
275 {
276 SCOPED_TRACE("before move");
277 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800278 sc->expectBGColor(0, 12);
279 sc->expectFGColor(75, 75);
280 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700281 }
282
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700283 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700284
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700285 {
286 // This should reflect the new position, but not the new color.
287 SCOPED_TRACE("after move, before redraw");
288 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800289 sc->expectBGColor(24, 24);
290 sc->expectBGColor(75, 75);
291 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700292 }
293
294 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
295 waitForPostedBuffers();
296 {
297 // This should reflect the new position and the new color.
298 SCOPED_TRACE("after redraw");
299 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800300 sc->expectBGColor(24, 24);
301 sc->expectBGColor(75, 75);
302 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700303 }
304}
305
306TEST_F(LayerUpdateTest, LayerResizeWorks) {
307 sp<ScreenCapture> sc;
308 {
309 SCOPED_TRACE("before resize");
310 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800311 sc->expectBGColor(0, 12);
312 sc->expectFGColor(75, 75);
313 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700314 }
315
Steve Block9d453682011-12-20 16:23:08 +0000316 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700317 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +0000318 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700319 {
320 // This should not reflect the new size or color because SurfaceFlinger
321 // has not yet received a buffer of the correct size.
322 SCOPED_TRACE("after resize, before redraw");
323 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800324 sc->expectBGColor(0, 12);
325 sc->expectFGColor(75, 75);
326 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700327 }
328
Steve Block9d453682011-12-20 16:23:08 +0000329 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700330 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
331 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000332 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700333 {
334 // This should reflect the new size and the new color.
335 SCOPED_TRACE("after redraw");
336 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800337 sc->expectBGColor(24, 24);
338 sc->checkPixel(75, 75, 63, 195, 63);
339 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700340 }
341}
342
Haixia Shid5750962015-07-27 16:50:49 -0700343TEST_F(LayerUpdateTest, LayerCropWorks) {
344 sp<ScreenCapture> sc;
345 {
346 SCOPED_TRACE("before crop");
347 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800348 sc->expectBGColor(24, 24);
349 sc->expectFGColor(75, 75);
350 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700351 }
352
Robert Carr4cdc58f2017-08-23 14:22:20 -0700353 asTransaction([&](Transaction& t) {
354 Rect cropRect(16, 16, 32, 32);
355 t.setCrop(mFGSurfaceControl, cropRect);
356 });
Haixia Shid5750962015-07-27 16:50:49 -0700357 {
358 // This should crop the foreground surface.
359 SCOPED_TRACE("after crop");
360 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800361 sc->expectBGColor(24, 24);
362 sc->expectBGColor(75, 75);
363 sc->expectFGColor(95, 80);
364 sc->expectFGColor(80, 95);
365 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700366 }
367}
368
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000369TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
370 sp<ScreenCapture> sc;
371 {
372 SCOPED_TRACE("before crop");
373 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800374 sc->expectBGColor(24, 24);
375 sc->expectFGColor(75, 75);
376 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000377 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700378 asTransaction([&](Transaction& t) {
379 Rect cropRect(16, 16, 32, 32);
380 t.setFinalCrop(mFGSurfaceControl, cropRect);
381 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000382 {
383 // This should crop the foreground surface.
384 SCOPED_TRACE("after crop");
385 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800386 sc->expectBGColor(24, 24);
387 sc->expectBGColor(75, 75);
388 sc->expectBGColor(95, 80);
389 sc->expectBGColor(80, 95);
390 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000391 }
392}
393
Haixia Shid5750962015-07-27 16:50:49 -0700394TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
395 sp<ScreenCapture> sc;
396 {
397 SCOPED_TRACE("before setLayer");
398 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800399 sc->expectBGColor(24, 24);
400 sc->expectFGColor(75, 75);
401 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700402 }
403
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700404 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700405
Haixia Shid5750962015-07-27 16:50:49 -0700406 {
407 // This should hide the foreground surface beneath the background.
408 SCOPED_TRACE("after setLayer");
409 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800410 sc->expectBGColor(24, 24);
411 sc->expectBGColor(75, 75);
412 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700413 }
414}
415
416TEST_F(LayerUpdateTest, LayerShowHideWorks) {
417 sp<ScreenCapture> sc;
418 {
419 SCOPED_TRACE("before hide");
420 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800421 sc->expectBGColor(24, 24);
422 sc->expectFGColor(75, 75);
423 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700424 }
425
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700426 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700427
Haixia Shid5750962015-07-27 16:50:49 -0700428 {
429 // This should hide the foreground surface.
430 SCOPED_TRACE("after hide, before show");
431 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800432 sc->expectBGColor(24, 24);
433 sc->expectBGColor(75, 75);
434 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700435 }
436
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700437 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700438
Haixia Shid5750962015-07-27 16:50:49 -0700439 {
440 // This should show the foreground surface.
441 SCOPED_TRACE("after show");
442 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800443 sc->expectBGColor(24, 24);
444 sc->expectFGColor(75, 75);
445 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700446 }
447}
448
449TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
450 sp<ScreenCapture> sc;
451 {
452 SCOPED_TRACE("before setAlpha");
453 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800454 sc->expectBGColor(24, 24);
455 sc->expectFGColor(75, 75);
456 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700457 }
458
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700459 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700460
Haixia Shid5750962015-07-27 16:50:49 -0700461 {
462 // This should set foreground to be 75% opaque.
463 SCOPED_TRACE("after setAlpha");
464 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800465 sc->expectBGColor(24, 24);
466 sc->checkPixel(75, 75, 162, 63, 96);
467 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700468 }
469}
470
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700471TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
472 sp<ScreenCapture> sc;
473 {
474 SCOPED_TRACE("before setLayerStack");
475 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800476 sc->expectBGColor(24, 24);
477 sc->expectFGColor(75, 75);
478 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700479 }
480
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700481 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700482 {
483 // This should hide the foreground surface since it goes to a different
484 // layer stack.
485 SCOPED_TRACE("after setLayerStack");
486 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800487 sc->expectBGColor(24, 24);
488 sc->expectBGColor(75, 75);
489 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700490 }
491}
492
493TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
494 sp<ScreenCapture> sc;
495 {
496 SCOPED_TRACE("before setFlags");
497 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800498 sc->expectBGColor(24, 24);
499 sc->expectFGColor(75, 75);
500 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700501 }
502
Robert Carr4cdc58f2017-08-23 14:22:20 -0700503 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700504 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700505 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700506 {
507 // This should hide the foreground surface
508 SCOPED_TRACE("after setFlags");
509 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800510 sc->expectBGColor(24, 24);
511 sc->expectBGColor(75, 75);
512 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700513 }
514}
515
516TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
517 sp<ScreenCapture> sc;
518 {
519 SCOPED_TRACE("before setMatrix");
520 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800521 sc->expectBGColor(24, 24);
522 sc->expectFGColor(91, 96);
523 sc->expectFGColor(96, 101);
524 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700525 }
526
Robert Carr4cdc58f2017-08-23 14:22:20 -0700527 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700528 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700529 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700530 {
531 SCOPED_TRACE("after setMatrix");
532 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800533 sc->expectBGColor(24, 24);
534 sc->expectFGColor(91, 96);
535 sc->expectBGColor(96, 91);
536 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700537 }
538}
539
Robert Carr8d5227b2017-03-16 15:41:03 -0700540class GeometryLatchingTest : public LayerUpdateTest {
541protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700542 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -0700543 SCOPED_TRACE(trace);
544 ScreenCapture::captureScreen(&sc);
545 // We find the leading edge of the FG surface.
546 sc->expectFGColor(127, 127);
547 sc->expectBGColor(128, 128);
548 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700549
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700550 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -0700551
552 void unlockFGBuffer() {
553 sp<Surface> s = mFGSurfaceControl->getSurface();
554 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
555 waitForPostedBuffers();
556 }
557
Robert Carr8d5227b2017-03-16 15:41:03 -0700558 void completeFGResize() {
559 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
560 waitForPostedBuffers();
561 }
562 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700563 asTransaction([&](Transaction& t) {
564 t.setSize(mFGSurfaceControl, 64, 64);
565 t.setPosition(mFGSurfaceControl, 64, 64);
566 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
567 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
568 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700569
570 EXPECT_INITIAL_STATE("After restoring initial state");
571 }
572 sp<ScreenCapture> sc;
573};
574
575TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
576 EXPECT_INITIAL_STATE("before anything");
577
578 // By default position can be updated even while
579 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700580 asTransaction([&](Transaction& t) {
581 t.setSize(mFGSurfaceControl, 32, 32);
582 t.setPosition(mFGSurfaceControl, 100, 100);
583 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700584
585 {
586 SCOPED_TRACE("After moving surface");
587 ScreenCapture::captureScreen(&sc);
588 // If we moved, the FG Surface should cover up what was previously BG
589 // however if we didn't move the FG wouldn't be large enough now.
590 sc->expectFGColor(163, 163);
591 }
592
593 restoreInitialState();
594
595 // Now we repeat with setGeometryAppliesWithResize
596 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700597 asTransaction([&](Transaction& t) {
598 t.setGeometryAppliesWithResize(mFGSurfaceControl);
599 t.setSize(mFGSurfaceControl, 32, 32);
600 t.setPosition(mFGSurfaceControl, 100, 100);
601 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700602
603 {
604 SCOPED_TRACE("While resize is pending");
605 ScreenCapture::captureScreen(&sc);
606 // This time we shouldn't have moved, so the BG color
607 // should still be visible.
608 sc->expectBGColor(128, 128);
609 }
610
611 completeFGResize();
612
613 {
614 SCOPED_TRACE("After the resize");
615 ScreenCapture::captureScreen(&sc);
616 // But after the resize completes, we should move
617 // and the FG should be visible here.
618 sc->expectFGColor(128, 128);
619 }
620}
621
622class CropLatchingTest : public GeometryLatchingTest {
623protected:
624 void EXPECT_CROPPED_STATE(const char* trace) {
625 SCOPED_TRACE(trace);
626 ScreenCapture::captureScreen(&sc);
627 // The edge should be moved back one pixel by our crop.
628 sc->expectFGColor(126, 126);
629 sc->expectBGColor(127, 127);
630 sc->expectBGColor(128, 128);
631 }
chaviw59f5c562017-06-28 16:39:06 -0700632
633 void EXPECT_RESIZE_STATE(const char* trace) {
634 SCOPED_TRACE(trace);
635 ScreenCapture::captureScreen(&sc);
636 // The FG is now resized too 128,128 at 64,64
637 sc->expectFGColor(64, 64);
638 sc->expectFGColor(191, 191);
639 sc->expectBGColor(192, 192);
640 }
Robert Carr8d5227b2017-03-16 15:41:03 -0700641};
642
643TEST_F(CropLatchingTest, CropLatching) {
644 EXPECT_INITIAL_STATE("before anything");
645 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700646 asTransaction([&](Transaction& t) {
647 t.setSize(mFGSurfaceControl, 128, 128);
648 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
649 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700650
651 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
652
653 restoreInitialState();
654
Robert Carr4cdc58f2017-08-23 14:22:20 -0700655 asTransaction([&](Transaction& t) {
656 t.setSize(mFGSurfaceControl, 128, 128);
657 t.setGeometryAppliesWithResize(mFGSurfaceControl);
658 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
659 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700660
661 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
662
663 completeFGResize();
664
665 EXPECT_CROPPED_STATE("after the resize finishes");
666}
667
668TEST_F(CropLatchingTest, FinalCropLatching) {
669 EXPECT_INITIAL_STATE("before anything");
670 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700671 asTransaction([&](Transaction& t) {
672 t.setSize(mFGSurfaceControl, 128, 128);
673 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
674 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700675
676 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
677
678 restoreInitialState();
679
Robert Carr4cdc58f2017-08-23 14:22:20 -0700680 asTransaction([&](Transaction& t) {
681 t.setSize(mFGSurfaceControl, 128, 128);
682 t.setGeometryAppliesWithResize(mFGSurfaceControl);
683 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
684 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700685
686 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
687
688 completeFGResize();
689
690 EXPECT_CROPPED_STATE("after the resize finishes");
691}
692
Robert Carr7bf247e2017-05-18 14:02:49 -0700693// In this test we ensure that setGeometryAppliesWithResize actually demands
694// a buffer of the new size, and not just any size.
695TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
696 EXPECT_INITIAL_STATE("before anything");
697 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700698 asTransaction([&](Transaction& t) {
699 t.setSize(mFGSurfaceControl, 128, 128);
700 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
701 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700702
703 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
704
705 restoreInitialState();
706
707 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
708 // initiating the resize.
709 lockAndFillFGBuffer();
710
Robert Carr4cdc58f2017-08-23 14:22:20 -0700711 asTransaction([&](Transaction& t) {
712 t.setSize(mFGSurfaceControl, 128, 128);
713 t.setGeometryAppliesWithResize(mFGSurfaceControl);
714 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
715 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700716
717 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
718
719 // We now submit our old buffer, at the old size, and ensure it doesn't
720 // trigger geometry latching.
721 unlockFGBuffer();
722
723 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
724
725 completeFGResize();
726
727 EXPECT_CROPPED_STATE("after the resize finishes");
728}
729
730TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
731 EXPECT_INITIAL_STATE("before anything");
732 // In this scenario, we attempt to set the final crop a second time while the resize
733 // is still pending, and ensure we are successful. Success meaning the second crop
734 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700735 asTransaction([&](Transaction& t) {
736 t.setSize(mFGSurfaceControl, 128, 128);
737 t.setGeometryAppliesWithResize(mFGSurfaceControl);
738 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
739 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700740
chaviw59f5c562017-06-28 16:39:06 -0700741 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
742
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700743 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -0700744
chaviw59f5c562017-06-28 16:39:06 -0700745 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -0700746
747 completeFGResize();
748
chaviw59f5c562017-06-28 16:39:06 -0700749 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -0700750}
751
Pablo Ceballos05289c22016-04-14 15:49:55 -0700752TEST_F(LayerUpdateTest, DeferredTransactionTest) {
753 sp<ScreenCapture> sc;
754 {
755 SCOPED_TRACE("before anything");
756 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800757 sc->expectBGColor(32, 32);
758 sc->expectFGColor(96, 96);
759 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700760 }
761
762 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -0700763 asTransaction([&](Transaction& t) {
764 t.setAlpha(mFGSurfaceControl, 0.75);
765 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700766 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -0700767 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700768
Robert Carr4cdc58f2017-08-23 14:22:20 -0700769 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700770 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700771 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700772 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700773 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700774
775 {
776 SCOPED_TRACE("before any trigger");
777 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800778 sc->expectBGColor(32, 32);
779 sc->expectFGColor(96, 96);
780 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700781 }
782
783 // should trigger the first deferred transaction, but not the second one
784 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
785 {
786 SCOPED_TRACE("after first trigger");
787 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800788 sc->expectBGColor(32, 32);
789 sc->checkPixel(96, 96, 162, 63, 96);
790 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700791 }
792
793 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700794 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700795
796 // trigger the second deferred transaction
797 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
798 {
799 SCOPED_TRACE("after second trigger");
800 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800801 sc->expectBGColor(32, 32);
802 sc->expectBGColor(96, 96);
803 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700804 }
805}
806
Robert Carrdb66e622017-04-10 16:55:57 -0700807TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
808 sp<ScreenCapture> sc;
809 {
810 SCOPED_TRACE("before adding relative surface");
811 ScreenCapture::captureScreen(&sc);
812 sc->expectBGColor(24, 24);
813 sc->expectFGColor(75, 75);
814 sc->expectBGColor(145, 145);
815 }
816
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700817 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
818 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -0700819 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
820 waitForPostedBuffers();
821
822 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700823 asTransaction([&](Transaction& t) {
824 t.setPosition(relativeSurfaceControl, 64, 64);
825 t.show(relativeSurfaceControl);
826 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
827 });
Robert Carrdb66e622017-04-10 16:55:57 -0700828
829 {
830 SCOPED_TRACE("after adding relative surface");
831 ScreenCapture::captureScreen(&sc);
832 // our relative surface should be visible now.
833 sc->checkPixel(75, 75, 255, 177, 177);
834 }
835
836 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700837 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -0700838
839 {
840 SCOPED_TRACE("after set layer");
841 ScreenCapture::captureScreen(&sc);
842 // now the FG surface should be visible again.
843 sc->expectFGColor(75, 75);
844 }
845}
846
Robert Carre392b552017-09-19 12:16:05 -0700847TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
848 sp<ScreenCapture> sc;
849
850 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700851 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
852 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
853 sp<SurfaceControl> childBuffer =
854 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
855 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -0700856 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
857
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700858 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700859
860 {
861 ScreenCapture::captureScreen(&sc);
862 sc->expectChildColor(73, 73);
863 sc->expectFGColor(74, 74);
864 }
865
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700866 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700867
868 {
869 ScreenCapture::captureScreen(&sc);
870 sc->expectChildColor(73, 73);
871 sc->expectChildColor(74, 74);
872 }
873}
874
Robert Carr2c5f6d22017-09-26 12:30:35 -0700875TEST_F(LayerUpdateTest, MergingTransactions) {
876 sp<ScreenCapture> sc;
877 {
878 SCOPED_TRACE("before move");
879 ScreenCapture::captureScreen(&sc);
880 sc->expectBGColor(0, 12);
881 sc->expectFGColor(75, 75);
882 sc->expectBGColor(145, 145);
883 }
884
885 Transaction t1, t2;
886 t1.setPosition(mFGSurfaceControl, 128, 128);
887 t2.setPosition(mFGSurfaceControl, 0, 0);
888 // We expect that the position update from t2 now
889 // overwrites the position update from t1.
890 t1.merge(std::move(t2));
891 t1.apply();
892
893 {
894 ScreenCapture::captureScreen(&sc);
895 sc->expectFGColor(1, 1);
896 }
897}
898
Robert Carr1f0a16a2016-10-24 16:27:39 -0700899class ChildLayerTest : public LayerUpdateTest {
900protected:
901 void SetUp() override {
902 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700903 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
904 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700905 fillSurfaceRGBA8(mChild, 200, 200, 200);
906
907 {
908 SCOPED_TRACE("before anything");
909 ScreenCapture::captureScreen(&mCapture);
910 mCapture->expectChildColor(64, 64);
911 }
912 }
913 void TearDown() override {
914 LayerUpdateTest::TearDown();
915 mChild = 0;
916 }
917
918 sp<SurfaceControl> mChild;
919 sp<ScreenCapture> mCapture;
920};
921
922TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700923 asTransaction([&](Transaction& t) {
924 t.show(mChild);
925 t.setPosition(mChild, 10, 10);
926 t.setPosition(mFGSurfaceControl, 64, 64);
927 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700928
929 {
930 ScreenCapture::captureScreen(&mCapture);
931 // Top left of foreground must now be visible
932 mCapture->expectFGColor(64, 64);
933 // But 10 pixels in we should see the child surface
934 mCapture->expectChildColor(74, 74);
935 // And 10 more pixels we should be back to the foreground surface
936 mCapture->expectFGColor(84, 84);
937 }
938
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700939 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700940
941 {
942 ScreenCapture::captureScreen(&mCapture);
943 // Top left of foreground should now be at 0, 0
944 mCapture->expectFGColor(0, 0);
945 // But 10 pixels in we should see the child surface
946 mCapture->expectChildColor(10, 10);
947 // And 10 more pixels we should be back to the foreground surface
948 mCapture->expectFGColor(20, 20);
949 }
950}
951
Robert Carr41b08b52017-06-01 16:11:34 -0700952TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700953 asTransaction([&](Transaction& t) {
954 t.show(mChild);
955 t.setPosition(mChild, 0, 0);
956 t.setPosition(mFGSurfaceControl, 0, 0);
957 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
958 });
Robert Carr41b08b52017-06-01 16:11:34 -0700959
960 {
961 ScreenCapture::captureScreen(&mCapture);
962 mCapture->expectChildColor(0, 0);
963 mCapture->expectChildColor(4, 4);
964 mCapture->expectBGColor(5, 5);
965 }
966}
967
968TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700969 asTransaction([&](Transaction& t) {
970 t.show(mChild);
971 t.setPosition(mChild, 0, 0);
972 t.setPosition(mFGSurfaceControl, 0, 0);
973 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
974 });
Robert Carr41b08b52017-06-01 16:11:34 -0700975
976 {
977 ScreenCapture::captureScreen(&mCapture);
978 mCapture->expectChildColor(0, 0);
979 mCapture->expectChildColor(4, 4);
980 mCapture->expectBGColor(5, 5);
981 }
982}
983
Robert Carr1f0a16a2016-10-24 16:27:39 -0700984TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700985 asTransaction([&](Transaction& t) {
986 t.show(mChild);
987 t.setPosition(mFGSurfaceControl, 0, 0);
988 t.setPosition(mChild, 63, 63);
989 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700990
991 {
992 ScreenCapture::captureScreen(&mCapture);
993 mCapture->expectFGColor(0, 0);
994 // Last pixel in foreground should now be the child.
995 mCapture->expectChildColor(63, 63);
996 // But the child should be constrained and the next pixel
997 // must be the background
998 mCapture->expectBGColor(64, 64);
999 }
1000}
1001
1002TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001003 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001004
1005 // Find the boundary between the parent and child
1006 {
1007 ScreenCapture::captureScreen(&mCapture);
1008 mCapture->expectChildColor(9, 9);
1009 mCapture->expectFGColor(10, 10);
1010 }
1011
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001012 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001013
1014 // The boundary should be twice as far from the origin now.
1015 // The pixels from the last test should all be child now
1016 {
1017 ScreenCapture::captureScreen(&mCapture);
1018 mCapture->expectChildColor(9, 9);
1019 mCapture->expectChildColor(10, 10);
1020 mCapture->expectChildColor(19, 19);
1021 mCapture->expectFGColor(20, 20);
1022 }
1023}
Robert Carr9524cb32017-02-13 11:32:32 -08001024
Robert Carr6452f122017-03-21 10:41:29 -07001025TEST_F(ChildLayerTest, ChildLayerAlpha) {
1026 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1027 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1028 fillSurfaceRGBA8(mChild, 0, 254, 0);
1029 waitForPostedBuffers();
1030
Robert Carr4cdc58f2017-08-23 14:22:20 -07001031 asTransaction([&](Transaction& t) {
1032 t.show(mChild);
1033 t.setPosition(mChild, 0, 0);
1034 t.setPosition(mFGSurfaceControl, 0, 0);
1035 });
Robert Carr6452f122017-03-21 10:41:29 -07001036
1037 {
1038 ScreenCapture::captureScreen(&mCapture);
1039 // Unblended child color
1040 mCapture->checkPixel(0, 0, 0, 254, 0);
1041 }
1042
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001043 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001044
1045 {
1046 ScreenCapture::captureScreen(&mCapture);
1047 // Child and BG blended.
1048 mCapture->checkPixel(0, 0, 127, 127, 0);
1049 }
1050
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001051 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001052
1053 {
1054 ScreenCapture::captureScreen(&mCapture);
1055 // Child and BG blended.
1056 mCapture->checkPixel(0, 0, 95, 64, 95);
1057 }
1058}
1059
Robert Carr9524cb32017-02-13 11:32:32 -08001060TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001061 asTransaction([&](Transaction& t) {
1062 t.show(mChild);
1063 t.setPosition(mChild, 10, 10);
1064 t.setPosition(mFGSurfaceControl, 64, 64);
1065 });
Robert Carr9524cb32017-02-13 11:32:32 -08001066
1067 {
1068 ScreenCapture::captureScreen(&mCapture);
1069 // Top left of foreground must now be visible
1070 mCapture->expectFGColor(64, 64);
1071 // But 10 pixels in we should see the child surface
1072 mCapture->expectChildColor(74, 74);
1073 // And 10 more pixels we should be back to the foreground surface
1074 mCapture->expectFGColor(84, 84);
1075 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001076
1077 asTransaction([&](Transaction& t) {
1078 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1079 });
1080
Robert Carr9524cb32017-02-13 11:32:32 -08001081 {
1082 ScreenCapture::captureScreen(&mCapture);
1083 mCapture->expectFGColor(64, 64);
1084 // In reparenting we should have exposed the entire foreground surface.
1085 mCapture->expectFGColor(74, 74);
1086 // And the child layer should now begin at 10, 10 (since the BG
1087 // layer is at (0, 0)).
1088 mCapture->expectBGColor(9, 9);
1089 mCapture->expectChildColor(10, 10);
1090 }
1091}
1092
chaviw161410b02017-07-27 10:46:08 -07001093TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001094 asTransaction([&](Transaction& t) {
1095 t.show(mChild);
1096 t.setPosition(mChild, 10, 10);
1097 t.setPosition(mFGSurfaceControl, 64, 64);
1098 });
Robert Carr9524cb32017-02-13 11:32:32 -08001099
1100 {
1101 ScreenCapture::captureScreen(&mCapture);
1102 // Top left of foreground must now be visible
1103 mCapture->expectFGColor(64, 64);
1104 // But 10 pixels in we should see the child surface
1105 mCapture->expectChildColor(74, 74);
1106 // And 10 more pixels we should be back to the foreground surface
1107 mCapture->expectFGColor(84, 84);
1108 }
1109
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001110 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001111
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001112 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001113
chaviw161410b02017-07-27 10:46:08 -07001114 // Since the child has the same client as the parent, it will not get
1115 // detached and will be hidden.
1116 {
1117 ScreenCapture::captureScreen(&mCapture);
1118 mCapture->expectFGColor(64, 64);
1119 mCapture->expectFGColor(74, 74);
1120 mCapture->expectFGColor(84, 84);
1121 }
1122}
1123
1124TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1125 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001126 sp<SurfaceControl> mChildNewClient =
1127 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1128 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001129
1130 ASSERT_TRUE(mChildNewClient != NULL);
1131 ASSERT_TRUE(mChildNewClient->isValid());
1132
1133 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1134
Robert Carr4cdc58f2017-08-23 14:22:20 -07001135 asTransaction([&](Transaction& t) {
1136 t.hide(mChild);
1137 t.show(mChildNewClient);
1138 t.setPosition(mChildNewClient, 10, 10);
1139 t.setPosition(mFGSurfaceControl, 64, 64);
1140 });
chaviw161410b02017-07-27 10:46:08 -07001141
1142 {
1143 ScreenCapture::captureScreen(&mCapture);
1144 // Top left of foreground must now be visible
1145 mCapture->expectFGColor(64, 64);
1146 // But 10 pixels in we should see the child surface
1147 mCapture->expectChildColor(74, 74);
1148 // And 10 more pixels we should be back to the foreground surface
1149 mCapture->expectFGColor(84, 84);
1150 }
1151
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001152 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001153
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001154 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001155
Robert Carr9524cb32017-02-13 11:32:32 -08001156 // Nothing should have changed.
1157 {
1158 ScreenCapture::captureScreen(&mCapture);
1159 mCapture->expectFGColor(64, 64);
1160 mCapture->expectChildColor(74, 74);
1161 mCapture->expectFGColor(84, 84);
1162 }
1163}
1164
Robert Carr9b429f42017-04-17 14:56:57 -07001165TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001166 asTransaction([&](Transaction& t) {
1167 t.show(mChild);
1168 t.setPosition(mChild, 0, 0);
1169 t.setPosition(mFGSurfaceControl, 0, 0);
1170 });
Robert Carr9b429f42017-04-17 14:56:57 -07001171
1172 {
1173 ScreenCapture::captureScreen(&mCapture);
1174 // We've positioned the child in the top left.
1175 mCapture->expectChildColor(0, 0);
1176 // But it's only 10x10.
1177 mCapture->expectFGColor(10, 10);
1178 }
1179
Robert Carr4cdc58f2017-08-23 14:22:20 -07001180 asTransaction([&](Transaction& t) {
1181 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1182 // We cause scaling by 2.
1183 t.setSize(mFGSurfaceControl, 128, 128);
1184 });
Robert Carr9b429f42017-04-17 14:56:57 -07001185
1186 {
1187 ScreenCapture::captureScreen(&mCapture);
1188 // We've positioned the child in the top left.
1189 mCapture->expectChildColor(0, 0);
1190 mCapture->expectChildColor(10, 10);
1191 mCapture->expectChildColor(19, 19);
1192 // And now it should be scaled all the way to 20x20
1193 mCapture->expectFGColor(20, 20);
1194 }
1195}
1196
Robert Carr1725eee2017-04-26 18:32:15 -07001197// Regression test for b/37673612
1198TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001199 asTransaction([&](Transaction& t) {
1200 t.show(mChild);
1201 t.setPosition(mChild, 0, 0);
1202 t.setPosition(mFGSurfaceControl, 0, 0);
1203 });
Robert Carr1725eee2017-04-26 18:32:15 -07001204
1205 {
1206 ScreenCapture::captureScreen(&mCapture);
1207 // We've positioned the child in the top left.
1208 mCapture->expectChildColor(0, 0);
1209 // But it's only 10x10.
1210 mCapture->expectFGColor(10, 10);
1211 }
Robert Carr1725eee2017-04-26 18:32:15 -07001212 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1213 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001214 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001215 sp<Surface> s = mFGSurfaceControl->getSurface();
1216 auto anw = static_cast<ANativeWindow*>(s.get());
1217 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1218 native_window_set_buffers_dimensions(anw, 64, 128);
1219 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1220 waitForPostedBuffers();
1221
1222 {
1223 // The child should still be in the same place and not have any strange scaling as in
1224 // b/37673612.
1225 ScreenCapture::captureScreen(&mCapture);
1226 mCapture->expectChildColor(0, 0);
1227 mCapture->expectFGColor(10, 10);
1228 }
1229}
1230
Dan Stoza412903f2017-04-27 13:42:17 -07001231TEST_F(ChildLayerTest, Bug36858924) {
1232 // Destroy the child layer
1233 mChild.clear();
1234
1235 // Now recreate it as hidden
1236 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1237 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1238 mFGSurfaceControl.get());
1239
1240 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001241 asTransaction([&](Transaction& t) {
1242 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001243 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001244 t.show(mChild);
1245 });
Dan Stoza412903f2017-04-27 13:42:17 -07001246
1247 // Render the foreground surface a few times
1248 //
1249 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1250 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1251 // never acquire/release the first buffer
1252 ALOGI("Filling 1");
1253 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1254 ALOGI("Filling 2");
1255 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1256 ALOGI("Filling 3");
1257 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1258 ALOGI("Filling 4");
1259 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1260}
1261
chaviwf1961f72017-09-18 16:41:07 -07001262TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001263 asTransaction([&](Transaction& t) {
1264 t.show(mChild);
1265 t.setPosition(mChild, 10, 10);
1266 t.setPosition(mFGSurfaceControl, 64, 64);
1267 });
chaviw06178942017-07-27 10:25:59 -07001268
1269 {
1270 ScreenCapture::captureScreen(&mCapture);
1271 // Top left of foreground must now be visible
1272 mCapture->expectFGColor(64, 64);
1273 // But 10 pixels in we should see the child surface
1274 mCapture->expectChildColor(74, 74);
1275 // And 10 more pixels we should be back to the foreground surface
1276 mCapture->expectFGColor(84, 84);
1277 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001278
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001279 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001280
chaviw06178942017-07-27 10:25:59 -07001281 {
1282 ScreenCapture::captureScreen(&mCapture);
1283 mCapture->expectFGColor(64, 64);
1284 // In reparenting we should have exposed the entire foreground surface.
1285 mCapture->expectFGColor(74, 74);
1286 // And the child layer should now begin at 10, 10 (since the BG
1287 // layer is at (0, 0)).
1288 mCapture->expectBGColor(9, 9);
1289 mCapture->expectChildColor(10, 10);
1290 }
1291}
1292
chaviwf1961f72017-09-18 16:41:07 -07001293TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001294 asTransaction([&](Transaction& t) {
1295 t.show(mChild);
1296 t.setPosition(mChild, 10, 10);
1297 t.setPosition(mFGSurfaceControl, 64, 64);
1298 });
chaviwf1961f72017-09-18 16:41:07 -07001299
1300 {
1301 ScreenCapture::captureScreen(&mCapture);
1302 // Top left of foreground must now be visible
1303 mCapture->expectFGColor(64, 64);
1304 // But 10 pixels in we should see the child surface
1305 mCapture->expectChildColor(74, 74);
1306 // And 10 more pixels we should be back to the foreground surface
1307 mCapture->expectFGColor(84, 84);
1308 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001309 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07001310 {
1311 ScreenCapture::captureScreen(&mCapture);
1312 // Nothing should have changed.
1313 mCapture->expectFGColor(64, 64);
1314 mCapture->expectChildColor(74, 74);
1315 mCapture->expectFGColor(84, 84);
1316 }
1317}
1318
1319TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001320 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
1321 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07001322 ASSERT_TRUE(newSurface != NULL);
1323 ASSERT_TRUE(newSurface->isValid());
1324
1325 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001326 asTransaction([&](Transaction& t) {
1327 t.hide(mChild);
1328 t.show(newSurface);
1329 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001330 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001331 t.setPosition(mFGSurfaceControl, 64, 64);
1332 });
chaviwf1961f72017-09-18 16:41:07 -07001333
1334 {
1335 ScreenCapture::captureScreen(&mCapture);
1336 // Top left of foreground must now be visible
1337 mCapture->expectFGColor(64, 64);
1338 // At 10, 10 we should see the new surface
1339 mCapture->checkPixel(10, 10, 63, 195, 63);
1340 }
1341
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001342 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07001343
1344 {
1345 ScreenCapture::captureScreen(&mCapture);
1346 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1347 // mFGSurface, putting it at 74, 74.
1348 mCapture->expectFGColor(64, 64);
1349 mCapture->checkPixel(74, 74, 63, 195, 63);
1350 mCapture->expectFGColor(84, 84);
1351 }
1352}
1353
chaviwc9674332017-08-28 12:32:18 -07001354TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001355 sp<SurfaceControl> grandchild =
1356 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
1357 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07001358 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1359
1360 {
1361 ScreenCapture::captureScreen(&mCapture);
1362 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1363 // which begins at 64, 64
1364 mCapture->checkPixel(64, 64, 50, 50, 50);
1365 }
1366}
1367
Robert Carr503c7042017-09-27 15:06:08 -07001368TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001369 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
1370 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07001371 fillSurfaceRGBA8(relative, 255, 255, 255);
1372
1373 Transaction t;
1374 t.setLayer(relative, INT32_MAX)
1375 .setRelativeLayer(mChild, relative->getHandle(), 1)
1376 .setPosition(mFGSurfaceControl, 0, 0)
1377 .apply(true);
1378
1379 // We expect that the child should have been elevated above our
1380 // INT_MAX layer even though it's not a child of it.
1381 {
1382 ScreenCapture::captureScreen(&mCapture);
1383 mCapture->expectChildColor(0, 0);
1384 mCapture->expectChildColor(9, 9);
1385 mCapture->checkPixel(10, 10, 255, 255, 255);
1386 }
1387}
1388
chaviw13fdc492017-06-27 12:40:18 -07001389class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001390protected:
chaviw13fdc492017-06-27 12:40:18 -07001391 void SetUp() override {
1392 LayerUpdateTest::SetUp();
1393
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001394 mLayerColorControl =
1395 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
1396 PIXEL_FORMAT_RGBA_8888,
1397 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07001398
1399 ASSERT_TRUE(mLayerColorControl != NULL);
1400 ASSERT_TRUE(mLayerColorControl->isValid());
1401
Robert Carr4cdc58f2017-08-23 14:22:20 -07001402 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001403 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001404 t.setPosition(mLayerColorControl, 140, 140);
1405 t.hide(mLayerColorControl);
1406 t.hide(mFGSurfaceControl);
1407 });
chaviw13fdc492017-06-27 12:40:18 -07001408 }
1409
1410 void TearDown() override {
1411 LayerUpdateTest::TearDown();
1412 mLayerColorControl = 0;
1413 }
1414
1415 sp<SurfaceControl> mLayerColorControl;
1416};
1417
1418TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1419 sp<ScreenCapture> sc;
1420
1421 {
1422 SCOPED_TRACE("before setColor");
1423 ScreenCapture::captureScreen(&sc);
1424 sc->expectBGColor(145, 145);
1425 }
1426
Robert Carr4cdc58f2017-08-23 14:22:20 -07001427 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001428 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001429 t.setColor(mLayerColorControl, color);
1430 t.show(mLayerColorControl);
1431 });
chaviw13fdc492017-06-27 12:40:18 -07001432
chaviw13fdc492017-06-27 12:40:18 -07001433 {
1434 // There should now be a color
1435 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001436
chaviw13fdc492017-06-27 12:40:18 -07001437 ScreenCapture::captureScreen(&sc);
1438 sc->checkPixel(145, 145, 43, 207, 131);
1439 }
1440}
1441
1442TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1443 sp<ScreenCapture> sc;
1444 {
1445 SCOPED_TRACE("before setColor");
1446 ScreenCapture::captureScreen(&sc);
1447 sc->expectBGColor(145, 145);
1448 }
1449
Robert Carr4cdc58f2017-08-23 14:22:20 -07001450 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001451 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001452 t.setColor(mLayerColorControl, color);
1453 t.setAlpha(mLayerColorControl, .75f);
1454 t.show(mLayerColorControl);
1455 });
1456
chaviw13fdc492017-06-27 12:40:18 -07001457 {
1458 // There should now be a color with .75 alpha
1459 SCOPED_TRACE("after setColor");
1460 ScreenCapture::captureScreen(&sc);
1461 sc->checkPixel(145, 145, 48, 171, 147);
1462 }
1463}
1464
1465TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1466 sp<ScreenCapture> sc;
1467 {
1468 SCOPED_TRACE("before setColor");
1469 ScreenCapture::captureScreen(&sc);
1470 sc->expectBGColor(145, 145);
1471 }
1472
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001473 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001474
chaviw13fdc492017-06-27 12:40:18 -07001475 {
1476 // There should now be set to 0,0,0 (black) as default.
1477 SCOPED_TRACE("after setColor");
1478 ScreenCapture::captureScreen(&sc);
1479 sc->checkPixel(145, 145, 0, 0, 0);
1480 }
1481}
1482
chaviwa76b2712017-09-20 12:02:26 -07001483class ScreenCaptureTest : public LayerUpdateTest {
1484protected:
1485 std::unique_ptr<CaptureLayer> mCapture;
1486};
1487
1488TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
1489 auto bgHandle = mBGSurfaceControl->getHandle();
1490 CaptureLayer::captureScreen(&mCapture, bgHandle);
1491 mCapture->expectBGColor(0, 0);
1492 // Doesn't capture FG layer which is at 64, 64
1493 mCapture->expectBGColor(64, 64);
1494}
1495
1496TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
1497 auto fgHandle = mFGSurfaceControl->getHandle();
1498
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001499 sp<SurfaceControl> child =
1500 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1501 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001502 fillSurfaceRGBA8(child, 200, 200, 200);
1503
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001504 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001505
1506 // Captures mFGSurfaceControl layer and its child.
1507 CaptureLayer::captureScreen(&mCapture, fgHandle);
1508 mCapture->expectFGColor(10, 10);
1509 mCapture->expectChildColor(0, 0);
1510}
1511
1512TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
1513 auto fgHandle = mFGSurfaceControl->getHandle();
1514
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001515 sp<SurfaceControl> child =
1516 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1517 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001518 fillSurfaceRGBA8(child, 200, 200, 200);
1519
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001520 sp<SurfaceControl> grandchild =
1521 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
1522 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07001523
1524 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1525 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001526 .show(child)
1527 .setPosition(grandchild, 5, 5)
1528 .show(grandchild)
1529 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001530
1531 // Captures mFGSurfaceControl, its child, and the grandchild.
1532 CaptureLayer::captureScreen(&mCapture, fgHandle);
1533 mCapture->expectFGColor(10, 10);
1534 mCapture->expectChildColor(0, 0);
1535 mCapture->checkPixel(5, 5, 50, 50, 50);
1536}
1537
1538TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001539 sp<SurfaceControl> child =
1540 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1541 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001542 fillSurfaceRGBA8(child, 200, 200, 200);
1543 auto childHandle = child->getHandle();
1544
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001545 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001546
1547 // Captures only the child layer, and not the parent.
1548 CaptureLayer::captureScreen(&mCapture, childHandle);
1549 mCapture->expectChildColor(0, 0);
1550 mCapture->expectChildColor(9, 9);
1551}
1552
1553TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001554 sp<SurfaceControl> child =
1555 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1556 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001557 fillSurfaceRGBA8(child, 200, 200, 200);
1558 auto childHandle = child->getHandle();
1559
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001560 sp<SurfaceControl> grandchild =
1561 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
1562 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07001563 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1564
1565 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001566 .show(child)
1567 .setPosition(grandchild, 5, 5)
1568 .show(grandchild)
1569 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001570
1571 auto grandchildHandle = grandchild->getHandle();
1572
1573 // Captures only the grandchild.
1574 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
1575 mCapture->checkPixel(0, 0, 50, 50, 50);
1576 mCapture->checkPixel(4, 4, 50, 50, 50);
1577}
1578
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001579} // namespace android