blob: f61a9784870a37cc018278ca7cc1e7b459cc7b77 [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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070028#include <utils/String8.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070029#include <ui/DisplayInfo.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.
41static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc,
Robert Carr7bf247e2017-05-18 14:02:49 -070042 uint8_t r, uint8_t g, uint8_t b, 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++) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080050 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());
Michael Lentine5a16a622015-05-21 13:48:24 -070072 sp<IBinder> display(sf->getBuiltInDisplay(
73 ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -070074 SurfaceComposerClient::Transaction().apply(true);
75
Michael Lentine5a16a622015-05-21 13:48:24 -070076 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0,
77 0, INT_MAX, false));
78 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070079 }
80
81 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -070082 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
83 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
84 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070085 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
86 String8 err(String8::format("pixel @ (%3d, %3d): "
87 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
88 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070089 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070090 }
91 }
92
Robert Carr1f0a16a2016-10-24 16:27:39 -070093 void expectFGColor(uint32_t x, uint32_t y) {
94 checkPixel(x, y, 195, 63, 63);
95 }
96
97 void expectBGColor(uint32_t x, uint32_t y) {
98 checkPixel(x, y, 63, 63, 195);
99 }
100
101 void expectChildColor(uint32_t x, uint32_t y) {
102 checkPixel(x, y, 200, 200, 200);
103 }
104
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700105private:
Michael Lentine5a16a622015-05-21 13:48:24 -0700106 ScreenCapture(const sp<CpuConsumer>& cc) :
107 mCC(cc) {
108 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
109 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700110
Michael Lentine5a16a622015-05-21 13:48:24 -0700111 ~ScreenCapture() {
112 mCC->unlockBuffer(mBuf);
113 }
114
115 sp<CpuConsumer> mCC;
116 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700117};
118
119class LayerUpdateTest : public ::testing::Test {
120protected:
121 virtual void SetUp() {
122 mComposerClient = new SurfaceComposerClient;
123 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
124
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700125 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
126 ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700127 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700128 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700129
130 ssize_t displayWidth = info.w;
131 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700132
133 // Background surface
134 mBGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700135 String8("BG Test Surface"), displayWidth, displayHeight,
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700136 PIXEL_FORMAT_RGBA_8888, 0);
137 ASSERT_TRUE(mBGSurfaceControl != NULL);
138 ASSERT_TRUE(mBGSurfaceControl->isValid());
139 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
140
141 // Foreground surface
142 mFGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700143 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700144 ASSERT_TRUE(mFGSurfaceControl != NULL);
145 ASSERT_TRUE(mFGSurfaceControl->isValid());
146
147 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
148
149 // Synchronization surface
150 mSyncSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700151 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700152 ASSERT_TRUE(mSyncSurfaceControl != NULL);
153 ASSERT_TRUE(mSyncSurfaceControl->isValid());
154
155 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
156
Robert Carr4cdc58f2017-08-23 14:22:20 -0700157 asTransaction([&](Transaction& t) {
158 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700159
Robert Carr4cdc58f2017-08-23 14:22:20 -0700160 t.setLayer(mBGSurfaceControl, INT32_MAX-2)
161 .show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700162
Robert Carr4cdc58f2017-08-23 14:22:20 -0700163 t.setLayer(mFGSurfaceControl, INT32_MAX-1)
164 .setPosition(mFGSurfaceControl, 64, 64)
165 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700166
Robert Carr4cdc58f2017-08-23 14:22:20 -0700167 t.setLayer(mSyncSurfaceControl, INT32_MAX-1)
168 .setPosition(mSyncSurfaceControl, displayWidth-2,
169 displayHeight-2)
170 .show(mSyncSurfaceControl);
171 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700172 }
173
174 virtual void TearDown() {
175 mComposerClient->dispose();
176 mBGSurfaceControl = 0;
177 mFGSurfaceControl = 0;
178 mSyncSurfaceControl = 0;
179 mComposerClient = 0;
180 }
181
182 void waitForPostedBuffers() {
183 // Since the sync surface is in synchronous mode (i.e. double buffered)
184 // posting three buffers to it should ensure that at least two
185 // SurfaceFlinger::handlePageFlip calls have been made, which should
186 // guaranteed that a buffer posted to another Surface has been retired.
187 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
188 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
189 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
190 }
191
Robert Carr4cdc58f2017-08-23 14:22:20 -0700192 void asTransaction(const std::function<void(Transaction&)>& exec) {
193 Transaction t;
194 exec(t);
195 t.apply(true);
196 }
197
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700198 sp<SurfaceComposerClient> mComposerClient;
199 sp<SurfaceControl> mBGSurfaceControl;
200 sp<SurfaceControl> mFGSurfaceControl;
201
202 // This surface is used to ensure that the buffers posted to
203 // mFGSurfaceControl have been picked up by SurfaceFlinger.
204 sp<SurfaceControl> mSyncSurfaceControl;
205};
206
207TEST_F(LayerUpdateTest, LayerMoveWorks) {
208 sp<ScreenCapture> sc;
209 {
210 SCOPED_TRACE("before move");
211 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800212 sc->expectBGColor(0, 12);
213 sc->expectFGColor(75, 75);
214 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700215 }
216
Robert Carr4cdc58f2017-08-23 14:22:20 -0700217 asTransaction([&](Transaction& t) {
218 t.setPosition(mFGSurfaceControl, 128, 128);
219 });
220
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700221 {
222 // This should reflect the new position, but not the new color.
223 SCOPED_TRACE("after move, before redraw");
224 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800225 sc->expectBGColor(24, 24);
226 sc->expectBGColor(75, 75);
227 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700228 }
229
230 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
231 waitForPostedBuffers();
232 {
233 // This should reflect the new position and the new color.
234 SCOPED_TRACE("after redraw");
235 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800236 sc->expectBGColor(24, 24);
237 sc->expectBGColor(75, 75);
238 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700239 }
240}
241
242TEST_F(LayerUpdateTest, LayerResizeWorks) {
243 sp<ScreenCapture> sc;
244 {
245 SCOPED_TRACE("before resize");
246 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800247 sc->expectBGColor(0, 12);
248 sc->expectFGColor(75, 75);
249 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700250 }
251
Steve Block9d453682011-12-20 16:23:08 +0000252 ALOGD("resizing");
Robert Carr4cdc58f2017-08-23 14:22:20 -0700253 asTransaction([&](Transaction& t) {
254 t.setSize(mFGSurfaceControl, 128, 128);
255 });
Steve Block9d453682011-12-20 16:23:08 +0000256 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700257 {
258 // This should not reflect the new size or color because SurfaceFlinger
259 // has not yet received a buffer of the correct size.
260 SCOPED_TRACE("after resize, before redraw");
261 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800262 sc->expectBGColor(0, 12);
263 sc->expectFGColor(75, 75);
264 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700265 }
266
Steve Block9d453682011-12-20 16:23:08 +0000267 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700268 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
269 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000270 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700271 {
272 // This should reflect the new size and the new color.
273 SCOPED_TRACE("after redraw");
274 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800275 sc->expectBGColor(24, 24);
276 sc->checkPixel(75, 75, 63, 195, 63);
277 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700278 }
279}
280
Haixia Shid5750962015-07-27 16:50:49 -0700281TEST_F(LayerUpdateTest, LayerCropWorks) {
282 sp<ScreenCapture> sc;
283 {
284 SCOPED_TRACE("before crop");
285 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800286 sc->expectBGColor(24, 24);
287 sc->expectFGColor(75, 75);
288 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700289 }
290
Robert Carr4cdc58f2017-08-23 14:22:20 -0700291 asTransaction([&](Transaction& t) {
292 Rect cropRect(16, 16, 32, 32);
293 t.setCrop(mFGSurfaceControl, cropRect);
294 });
Haixia Shid5750962015-07-27 16:50:49 -0700295 {
296 // This should crop the foreground surface.
297 SCOPED_TRACE("after crop");
298 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800299 sc->expectBGColor(24, 24);
300 sc->expectBGColor(75, 75);
301 sc->expectFGColor(95, 80);
302 sc->expectFGColor(80, 95);
303 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700304 }
305}
306
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000307TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
308 sp<ScreenCapture> sc;
309 {
310 SCOPED_TRACE("before crop");
311 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800312 sc->expectBGColor(24, 24);
313 sc->expectFGColor(75, 75);
314 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000315 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700316 asTransaction([&](Transaction& t) {
317 Rect cropRect(16, 16, 32, 32);
318 t.setFinalCrop(mFGSurfaceControl, cropRect);
319 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000320 {
321 // This should crop the foreground surface.
322 SCOPED_TRACE("after crop");
323 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800324 sc->expectBGColor(24, 24);
325 sc->expectBGColor(75, 75);
326 sc->expectBGColor(95, 80);
327 sc->expectBGColor(80, 95);
328 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000329 }
330}
331
Haixia Shid5750962015-07-27 16:50:49 -0700332TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
333 sp<ScreenCapture> sc;
334 {
335 SCOPED_TRACE("before setLayer");
336 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800337 sc->expectBGColor(24, 24);
338 sc->expectFGColor(75, 75);
339 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700340 }
341
Robert Carr4cdc58f2017-08-23 14:22:20 -0700342 asTransaction([&](Transaction& t) {
343 t.setLayer(mFGSurfaceControl, INT_MAX - 3);
344 });
345
Haixia Shid5750962015-07-27 16:50:49 -0700346 {
347 // This should hide the foreground surface beneath the background.
348 SCOPED_TRACE("after setLayer");
349 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800350 sc->expectBGColor(24, 24);
351 sc->expectBGColor(75, 75);
352 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700353 }
354}
355
356TEST_F(LayerUpdateTest, LayerShowHideWorks) {
357 sp<ScreenCapture> sc;
358 {
359 SCOPED_TRACE("before hide");
360 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800361 sc->expectBGColor(24, 24);
362 sc->expectFGColor(75, 75);
363 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700364 }
365
Robert Carr4cdc58f2017-08-23 14:22:20 -0700366 asTransaction([&](Transaction& t) {
367 t.hide(mFGSurfaceControl);
368 });
369
Haixia Shid5750962015-07-27 16:50:49 -0700370 {
371 // This should hide the foreground surface.
372 SCOPED_TRACE("after hide, before show");
373 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800374 sc->expectBGColor(24, 24);
375 sc->expectBGColor(75, 75);
376 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700377 }
378
Robert Carr4cdc58f2017-08-23 14:22:20 -0700379 asTransaction([&](Transaction& t) {
380 t.show(mFGSurfaceControl);
381 });
382
Haixia Shid5750962015-07-27 16:50:49 -0700383 {
384 // This should show the foreground surface.
385 SCOPED_TRACE("after show");
386 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800387 sc->expectBGColor(24, 24);
388 sc->expectFGColor(75, 75);
389 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700390 }
391}
392
393TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
394 sp<ScreenCapture> sc;
395 {
396 SCOPED_TRACE("before setAlpha");
397 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800398 sc->expectBGColor(24, 24);
399 sc->expectFGColor(75, 75);
400 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700401 }
402
Robert Carr4cdc58f2017-08-23 14:22:20 -0700403 asTransaction([&](Transaction& t) {
404 t.setAlpha(mFGSurfaceControl, 0.75f);
405 });
406
Haixia Shid5750962015-07-27 16:50:49 -0700407 {
408 // This should set foreground to be 75% opaque.
409 SCOPED_TRACE("after setAlpha");
410 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800411 sc->expectBGColor(24, 24);
412 sc->checkPixel(75, 75, 162, 63, 96);
413 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700414 }
415}
416
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700417TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
418 sp<ScreenCapture> sc;
419 {
420 SCOPED_TRACE("before setLayerStack");
421 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800422 sc->expectBGColor(24, 24);
423 sc->expectFGColor(75, 75);
424 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700425 }
426
Robert Carr4cdc58f2017-08-23 14:22:20 -0700427 asTransaction([&](Transaction& t) {
428 t.setLayerStack(mFGSurfaceControl, 1);
429 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700430 {
431 // This should hide the foreground surface since it goes to a different
432 // layer stack.
433 SCOPED_TRACE("after setLayerStack");
434 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800435 sc->expectBGColor(24, 24);
436 sc->expectBGColor(75, 75);
437 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700438 }
439}
440
441TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
442 sp<ScreenCapture> sc;
443 {
444 SCOPED_TRACE("before setFlags");
445 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800446 sc->expectBGColor(24, 24);
447 sc->expectFGColor(75, 75);
448 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700449 }
450
Robert Carr4cdc58f2017-08-23 14:22:20 -0700451 asTransaction([&](Transaction& t) {
452 t.setFlags(mFGSurfaceControl,
453 layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
454 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700455 {
456 // This should hide the foreground surface
457 SCOPED_TRACE("after setFlags");
458 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800459 sc->expectBGColor(24, 24);
460 sc->expectBGColor(75, 75);
461 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700462 }
463}
464
465TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
466 sp<ScreenCapture> sc;
467 {
468 SCOPED_TRACE("before setMatrix");
469 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800470 sc->expectBGColor(24, 24);
471 sc->expectFGColor(91, 96);
472 sc->expectFGColor(96, 101);
473 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700474 }
475
Robert Carr4cdc58f2017-08-23 14:22:20 -0700476 asTransaction([&](Transaction& t) {
477 t.setMatrix(mFGSurfaceControl,
478 M_SQRT1_2, M_SQRT1_2,
479 -M_SQRT1_2, M_SQRT1_2);
480 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700481 {
482 SCOPED_TRACE("after setMatrix");
483 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800484 sc->expectBGColor(24, 24);
485 sc->expectFGColor(91, 96);
486 sc->expectBGColor(96, 91);
487 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700488 }
489}
490
Robert Carr8d5227b2017-03-16 15:41:03 -0700491class GeometryLatchingTest : public LayerUpdateTest {
492protected:
493 void EXPECT_INITIAL_STATE(const char * trace) {
494 SCOPED_TRACE(trace);
495 ScreenCapture::captureScreen(&sc);
496 // We find the leading edge of the FG surface.
497 sc->expectFGColor(127, 127);
498 sc->expectBGColor(128, 128);
499 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700500
501 void lockAndFillFGBuffer() {
502 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
503 }
504
505 void unlockFGBuffer() {
506 sp<Surface> s = mFGSurfaceControl->getSurface();
507 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
508 waitForPostedBuffers();
509 }
510
Robert Carr8d5227b2017-03-16 15:41:03 -0700511 void completeFGResize() {
512 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
513 waitForPostedBuffers();
514 }
515 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700516 asTransaction([&](Transaction& t) {
517 t.setSize(mFGSurfaceControl, 64, 64);
518 t.setPosition(mFGSurfaceControl, 64, 64);
519 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
520 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
521 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700522
523 EXPECT_INITIAL_STATE("After restoring initial state");
524 }
525 sp<ScreenCapture> sc;
526};
527
528TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
529 EXPECT_INITIAL_STATE("before anything");
530
531 // By default position can be updated even while
532 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700533 asTransaction([&](Transaction& t) {
534 t.setSize(mFGSurfaceControl, 32, 32);
535 t.setPosition(mFGSurfaceControl, 100, 100);
536 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700537
538 {
539 SCOPED_TRACE("After moving surface");
540 ScreenCapture::captureScreen(&sc);
541 // If we moved, the FG Surface should cover up what was previously BG
542 // however if we didn't move the FG wouldn't be large enough now.
543 sc->expectFGColor(163, 163);
544 }
545
546 restoreInitialState();
547
548 // Now we repeat with setGeometryAppliesWithResize
549 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700550 asTransaction([&](Transaction& t) {
551 t.setGeometryAppliesWithResize(mFGSurfaceControl);
552 t.setSize(mFGSurfaceControl, 32, 32);
553 t.setPosition(mFGSurfaceControl, 100, 100);
554 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700555
556 {
557 SCOPED_TRACE("While resize is pending");
558 ScreenCapture::captureScreen(&sc);
559 // This time we shouldn't have moved, so the BG color
560 // should still be visible.
561 sc->expectBGColor(128, 128);
562 }
563
564 completeFGResize();
565
566 {
567 SCOPED_TRACE("After the resize");
568 ScreenCapture::captureScreen(&sc);
569 // But after the resize completes, we should move
570 // and the FG should be visible here.
571 sc->expectFGColor(128, 128);
572 }
573}
574
575class CropLatchingTest : public GeometryLatchingTest {
576protected:
577 void EXPECT_CROPPED_STATE(const char* trace) {
578 SCOPED_TRACE(trace);
579 ScreenCapture::captureScreen(&sc);
580 // The edge should be moved back one pixel by our crop.
581 sc->expectFGColor(126, 126);
582 sc->expectBGColor(127, 127);
583 sc->expectBGColor(128, 128);
584 }
chaviw59f5c562017-06-28 16:39:06 -0700585
586 void EXPECT_RESIZE_STATE(const char* trace) {
587 SCOPED_TRACE(trace);
588 ScreenCapture::captureScreen(&sc);
589 // The FG is now resized too 128,128 at 64,64
590 sc->expectFGColor(64, 64);
591 sc->expectFGColor(191, 191);
592 sc->expectBGColor(192, 192);
593 }
Robert Carr8d5227b2017-03-16 15:41:03 -0700594};
595
596TEST_F(CropLatchingTest, CropLatching) {
597 EXPECT_INITIAL_STATE("before anything");
598 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700599 asTransaction([&](Transaction& t) {
600 t.setSize(mFGSurfaceControl, 128, 128);
601 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
602 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700603
604 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
605
606 restoreInitialState();
607
Robert Carr4cdc58f2017-08-23 14:22:20 -0700608 asTransaction([&](Transaction& t) {
609 t.setSize(mFGSurfaceControl, 128, 128);
610 t.setGeometryAppliesWithResize(mFGSurfaceControl);
611 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
612 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700613
614 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
615
616 completeFGResize();
617
618 EXPECT_CROPPED_STATE("after the resize finishes");
619}
620
621TEST_F(CropLatchingTest, FinalCropLatching) {
622 EXPECT_INITIAL_STATE("before anything");
623 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700624 asTransaction([&](Transaction& t) {
625 t.setSize(mFGSurfaceControl, 128, 128);
626 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
627 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700628
629 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
630
631 restoreInitialState();
632
Robert Carr4cdc58f2017-08-23 14:22:20 -0700633 asTransaction([&](Transaction& t) {
634 t.setSize(mFGSurfaceControl, 128, 128);
635 t.setGeometryAppliesWithResize(mFGSurfaceControl);
636 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
637 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700638
639 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
640
641 completeFGResize();
642
643 EXPECT_CROPPED_STATE("after the resize finishes");
644}
645
Robert Carr7bf247e2017-05-18 14:02:49 -0700646// In this test we ensure that setGeometryAppliesWithResize actually demands
647// a buffer of the new size, and not just any size.
648TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
649 EXPECT_INITIAL_STATE("before anything");
650 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700651 asTransaction([&](Transaction& t) {
652 t.setSize(mFGSurfaceControl, 128, 128);
653 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
654 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700655
656 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
657
658 restoreInitialState();
659
660 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
661 // initiating the resize.
662 lockAndFillFGBuffer();
663
Robert Carr4cdc58f2017-08-23 14:22:20 -0700664 asTransaction([&](Transaction& t) {
665 t.setSize(mFGSurfaceControl, 128, 128);
666 t.setGeometryAppliesWithResize(mFGSurfaceControl);
667 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
668 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700669
670 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
671
672 // We now submit our old buffer, at the old size, and ensure it doesn't
673 // trigger geometry latching.
674 unlockFGBuffer();
675
676 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
677
678 completeFGResize();
679
680 EXPECT_CROPPED_STATE("after the resize finishes");
681}
682
683TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
684 EXPECT_INITIAL_STATE("before anything");
685 // In this scenario, we attempt to set the final crop a second time while the resize
686 // is still pending, and ensure we are successful. Success meaning the second crop
687 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700688 asTransaction([&](Transaction& t) {
689 t.setSize(mFGSurfaceControl, 128, 128);
690 t.setGeometryAppliesWithResize(mFGSurfaceControl);
691 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
692 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700693
chaviw59f5c562017-06-28 16:39:06 -0700694 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
695
Robert Carr4cdc58f2017-08-23 14:22:20 -0700696 asTransaction([&](Transaction& t) {
697 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
698 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700699
chaviw59f5c562017-06-28 16:39:06 -0700700 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -0700701
702 completeFGResize();
703
chaviw59f5c562017-06-28 16:39:06 -0700704 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -0700705}
706
Pablo Ceballos05289c22016-04-14 15:49:55 -0700707TEST_F(LayerUpdateTest, DeferredTransactionTest) {
708 sp<ScreenCapture> sc;
709 {
710 SCOPED_TRACE("before anything");
711 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800712 sc->expectBGColor(32, 32);
713 sc->expectFGColor(96, 96);
714 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700715 }
716
717 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -0700718 asTransaction([&](Transaction& t) {
719 t.setAlpha(mFGSurfaceControl, 0.75);
720 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
721 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
722 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700723
Robert Carr4cdc58f2017-08-23 14:22:20 -0700724 asTransaction([&](Transaction& t) {
725 t.setPosition(mFGSurfaceControl, 128,128);
726 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
727 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
728 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700729
730 {
731 SCOPED_TRACE("before any trigger");
732 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800733 sc->expectBGColor(32, 32);
734 sc->expectFGColor(96, 96);
735 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700736 }
737
738 // should trigger the first deferred transaction, but not the second one
739 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
740 {
741 SCOPED_TRACE("after first trigger");
742 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800743 sc->expectBGColor(32, 32);
744 sc->checkPixel(96, 96, 162, 63, 96);
745 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700746 }
747
748 // should show up immediately since it's not deferred
Robert Carr4cdc58f2017-08-23 14:22:20 -0700749 asTransaction([&](Transaction& t) {
750 t.setAlpha(mFGSurfaceControl, 1.0);
751 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700752
753 // trigger the second deferred transaction
754 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
755 {
756 SCOPED_TRACE("after second trigger");
757 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800758 sc->expectBGColor(32, 32);
759 sc->expectBGColor(96, 96);
760 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700761 }
762}
763
Robert Carrdb66e622017-04-10 16:55:57 -0700764TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
765 sp<ScreenCapture> sc;
766 {
767 SCOPED_TRACE("before adding relative surface");
768 ScreenCapture::captureScreen(&sc);
769 sc->expectBGColor(24, 24);
770 sc->expectFGColor(75, 75);
771 sc->expectBGColor(145, 145);
772 }
773
774 auto relativeSurfaceControl = mComposerClient->createSurface(
775 String8("Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
776 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
777 waitForPostedBuffers();
778
779 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700780 asTransaction([&](Transaction& t) {
781 t.setPosition(relativeSurfaceControl, 64, 64);
782 t.show(relativeSurfaceControl);
783 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
784 });
Robert Carrdb66e622017-04-10 16:55:57 -0700785
786 {
787 SCOPED_TRACE("after adding relative surface");
788 ScreenCapture::captureScreen(&sc);
789 // our relative surface should be visible now.
790 sc->checkPixel(75, 75, 255, 177, 177);
791 }
792
793 // A call to setLayer will override a call to setRelativeLayer
Robert Carr4cdc58f2017-08-23 14:22:20 -0700794 asTransaction([&](Transaction& t) {
795 t.setLayer(relativeSurfaceControl, 0);
796 });
Robert Carrdb66e622017-04-10 16:55:57 -0700797
798 {
799 SCOPED_TRACE("after set layer");
800 ScreenCapture::captureScreen(&sc);
801 // now the FG surface should be visible again.
802 sc->expectFGColor(75, 75);
803 }
804}
805
Robert Carre392b552017-09-19 12:16:05 -0700806TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
807 sp<ScreenCapture> sc;
808
809 sp<SurfaceControl> childNoBuffer =
810 mComposerClient->createSurface(String8("Bufferless child"),
811 10, 10, PIXEL_FORMAT_RGBA_8888,
812 0, mFGSurfaceControl.get());
813 sp<SurfaceControl> childBuffer = mComposerClient->createSurface(
814 String8("Buffered child"), 20, 20,
815 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
816 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
817
Robert Carr4cdc58f2017-08-23 14:22:20 -0700818 SurfaceComposerClient::Transaction{}
819 .show(childNoBuffer)
820 .show(childBuffer)
821 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700822
823 {
824 ScreenCapture::captureScreen(&sc);
825 sc->expectChildColor(73, 73);
826 sc->expectFGColor(74, 74);
827 }
828
Robert Carr4cdc58f2017-08-23 14:22:20 -0700829 SurfaceComposerClient::Transaction{}
830 .setSize(childNoBuffer, 20, 20)
831 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700832
833 {
834 ScreenCapture::captureScreen(&sc);
835 sc->expectChildColor(73, 73);
836 sc->expectChildColor(74, 74);
837 }
838}
839
Robert Carr1f0a16a2016-10-24 16:27:39 -0700840class ChildLayerTest : public LayerUpdateTest {
841protected:
842 void SetUp() override {
843 LayerUpdateTest::SetUp();
844 mChild = mComposerClient->createSurface(
845 String8("Child surface"),
846 10, 10, PIXEL_FORMAT_RGBA_8888,
847 0, mFGSurfaceControl.get());
848 fillSurfaceRGBA8(mChild, 200, 200, 200);
849
850 {
851 SCOPED_TRACE("before anything");
852 ScreenCapture::captureScreen(&mCapture);
853 mCapture->expectChildColor(64, 64);
854 }
855 }
856 void TearDown() override {
857 LayerUpdateTest::TearDown();
858 mChild = 0;
859 }
860
861 sp<SurfaceControl> mChild;
862 sp<ScreenCapture> mCapture;
863};
864
865TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700866 asTransaction([&](Transaction& t) {
867 t.show(mChild);
868 t.setPosition(mChild, 10, 10);
869 t.setPosition(mFGSurfaceControl, 64, 64);
870 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700871
872 {
873 ScreenCapture::captureScreen(&mCapture);
874 // Top left of foreground must now be visible
875 mCapture->expectFGColor(64, 64);
876 // But 10 pixels in we should see the child surface
877 mCapture->expectChildColor(74, 74);
878 // And 10 more pixels we should be back to the foreground surface
879 mCapture->expectFGColor(84, 84);
880 }
881
Robert Carr4cdc58f2017-08-23 14:22:20 -0700882 asTransaction([&](Transaction& t) {
883 t.setPosition(mFGSurfaceControl, 0, 0);
884 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700885
886 {
887 ScreenCapture::captureScreen(&mCapture);
888 // Top left of foreground should now be at 0, 0
889 mCapture->expectFGColor(0, 0);
890 // But 10 pixels in we should see the child surface
891 mCapture->expectChildColor(10, 10);
892 // And 10 more pixels we should be back to the foreground surface
893 mCapture->expectFGColor(20, 20);
894 }
895}
896
Robert Carr41b08b52017-06-01 16:11:34 -0700897TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700898 asTransaction([&](Transaction& t) {
899 t.show(mChild);
900 t.setPosition(mChild, 0, 0);
901 t.setPosition(mFGSurfaceControl, 0, 0);
902 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
903 });
Robert Carr41b08b52017-06-01 16:11:34 -0700904
905 {
906 ScreenCapture::captureScreen(&mCapture);
907 mCapture->expectChildColor(0, 0);
908 mCapture->expectChildColor(4, 4);
909 mCapture->expectBGColor(5, 5);
910 }
911}
912
913TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700914 asTransaction([&](Transaction& t) {
915 t.show(mChild);
916 t.setPosition(mChild, 0, 0);
917 t.setPosition(mFGSurfaceControl, 0, 0);
918 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
919 });
Robert Carr41b08b52017-06-01 16:11:34 -0700920
921 {
922 ScreenCapture::captureScreen(&mCapture);
923 mCapture->expectChildColor(0, 0);
924 mCapture->expectChildColor(4, 4);
925 mCapture->expectBGColor(5, 5);
926 }
927}
928
Robert Carr1f0a16a2016-10-24 16:27:39 -0700929TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700930 asTransaction([&](Transaction& t) {
931 t.show(mChild);
932 t.setPosition(mFGSurfaceControl, 0, 0);
933 t.setPosition(mChild, 63, 63);
934 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700935
936 {
937 ScreenCapture::captureScreen(&mCapture);
938 mCapture->expectFGColor(0, 0);
939 // Last pixel in foreground should now be the child.
940 mCapture->expectChildColor(63, 63);
941 // But the child should be constrained and the next pixel
942 // must be the background
943 mCapture->expectBGColor(64, 64);
944 }
945}
946
947TEST_F(ChildLayerTest, ChildLayerScaling) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700948 asTransaction([&](Transaction& t) {
949 t.setPosition(mFGSurfaceControl, 0, 0);
950 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700951
952 // Find the boundary between the parent and child
953 {
954 ScreenCapture::captureScreen(&mCapture);
955 mCapture->expectChildColor(9, 9);
956 mCapture->expectFGColor(10, 10);
957 }
958
Robert Carr4cdc58f2017-08-23 14:22:20 -0700959 asTransaction([&](Transaction& t) {
960 t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0);
961 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700962
963 // The boundary should be twice as far from the origin now.
964 // The pixels from the last test should all be child now
965 {
966 ScreenCapture::captureScreen(&mCapture);
967 mCapture->expectChildColor(9, 9);
968 mCapture->expectChildColor(10, 10);
969 mCapture->expectChildColor(19, 19);
970 mCapture->expectFGColor(20, 20);
971 }
972}
Robert Carr9524cb32017-02-13 11:32:32 -0800973
Robert Carr6452f122017-03-21 10:41:29 -0700974TEST_F(ChildLayerTest, ChildLayerAlpha) {
975 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
976 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
977 fillSurfaceRGBA8(mChild, 0, 254, 0);
978 waitForPostedBuffers();
979
Robert Carr4cdc58f2017-08-23 14:22:20 -0700980 asTransaction([&](Transaction& t) {
981 t.show(mChild);
982 t.setPosition(mChild, 0, 0);
983 t.setPosition(mFGSurfaceControl, 0, 0);
984 });
Robert Carr6452f122017-03-21 10:41:29 -0700985
986 {
987 ScreenCapture::captureScreen(&mCapture);
988 // Unblended child color
989 mCapture->checkPixel(0, 0, 0, 254, 0);
990 }
991
Robert Carr4cdc58f2017-08-23 14:22:20 -0700992 asTransaction([&](Transaction& t) {
993 t.setAlpha(mChild, 0.5);
994 });
Robert Carr6452f122017-03-21 10:41:29 -0700995
996 {
997 ScreenCapture::captureScreen(&mCapture);
998 // Child and BG blended.
999 mCapture->checkPixel(0, 0, 127, 127, 0);
1000 }
1001
Robert Carr4cdc58f2017-08-23 14:22:20 -07001002 asTransaction([&](Transaction& t) {
1003 t.setAlpha(mFGSurfaceControl, 0.5);
1004 });
Robert Carr6452f122017-03-21 10:41:29 -07001005
1006 {
1007 ScreenCapture::captureScreen(&mCapture);
1008 // Child and BG blended.
1009 mCapture->checkPixel(0, 0, 95, 64, 95);
1010 }
1011}
1012
Robert Carr9524cb32017-02-13 11:32:32 -08001013TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001014 asTransaction([&](Transaction& t) {
1015 t.show(mChild);
1016 t.setPosition(mChild, 10, 10);
1017 t.setPosition(mFGSurfaceControl, 64, 64);
1018 });
Robert Carr9524cb32017-02-13 11:32:32 -08001019
1020 {
1021 ScreenCapture::captureScreen(&mCapture);
1022 // Top left of foreground must now be visible
1023 mCapture->expectFGColor(64, 64);
1024 // But 10 pixels in we should see the child surface
1025 mCapture->expectChildColor(74, 74);
1026 // And 10 more pixels we should be back to the foreground surface
1027 mCapture->expectFGColor(84, 84);
1028 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001029
1030 asTransaction([&](Transaction& t) {
1031 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1032 });
1033
Robert Carr9524cb32017-02-13 11:32:32 -08001034 {
1035 ScreenCapture::captureScreen(&mCapture);
1036 mCapture->expectFGColor(64, 64);
1037 // In reparenting we should have exposed the entire foreground surface.
1038 mCapture->expectFGColor(74, 74);
1039 // And the child layer should now begin at 10, 10 (since the BG
1040 // layer is at (0, 0)).
1041 mCapture->expectBGColor(9, 9);
1042 mCapture->expectChildColor(10, 10);
1043 }
1044}
1045
chaviw161410b02017-07-27 10:46:08 -07001046TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001047 asTransaction([&](Transaction& t) {
1048 t.show(mChild);
1049 t.setPosition(mChild, 10, 10);
1050 t.setPosition(mFGSurfaceControl, 64, 64);
1051 });
Robert Carr9524cb32017-02-13 11:32:32 -08001052
1053 {
1054 ScreenCapture::captureScreen(&mCapture);
1055 // Top left of foreground must now be visible
1056 mCapture->expectFGColor(64, 64);
1057 // But 10 pixels in we should see the child surface
1058 mCapture->expectChildColor(74, 74);
1059 // And 10 more pixels we should be back to the foreground surface
1060 mCapture->expectFGColor(84, 84);
1061 }
1062
Robert Carr4cdc58f2017-08-23 14:22:20 -07001063 asTransaction([&](Transaction& t) {
1064 t.detachChildren(mFGSurfaceControl);
1065 });
Robert Carr9524cb32017-02-13 11:32:32 -08001066
Robert Carr4cdc58f2017-08-23 14:22:20 -07001067 asTransaction([&](Transaction& t) {
1068 t.hide(mChild);
1069 });
Robert Carr9524cb32017-02-13 11:32:32 -08001070
chaviw161410b02017-07-27 10:46:08 -07001071 // Since the child has the same client as the parent, it will not get
1072 // detached and will be hidden.
1073 {
1074 ScreenCapture::captureScreen(&mCapture);
1075 mCapture->expectFGColor(64, 64);
1076 mCapture->expectFGColor(74, 74);
1077 mCapture->expectFGColor(84, 84);
1078 }
1079}
1080
1081TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1082 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
1083 sp<SurfaceControl> mChildNewClient = mNewComposerClient->createSurface(
1084 String8("New Child Test Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1085 0, mFGSurfaceControl.get());
1086
1087 ASSERT_TRUE(mChildNewClient != NULL);
1088 ASSERT_TRUE(mChildNewClient->isValid());
1089
1090 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1091
Robert Carr4cdc58f2017-08-23 14:22:20 -07001092 asTransaction([&](Transaction& t) {
1093 t.hide(mChild);
1094 t.show(mChildNewClient);
1095 t.setPosition(mChildNewClient, 10, 10);
1096 t.setPosition(mFGSurfaceControl, 64, 64);
1097 });
chaviw161410b02017-07-27 10:46:08 -07001098
1099 {
1100 ScreenCapture::captureScreen(&mCapture);
1101 // Top left of foreground must now be visible
1102 mCapture->expectFGColor(64, 64);
1103 // But 10 pixels in we should see the child surface
1104 mCapture->expectChildColor(74, 74);
1105 // And 10 more pixels we should be back to the foreground surface
1106 mCapture->expectFGColor(84, 84);
1107 }
1108
Robert Carr4cdc58f2017-08-23 14:22:20 -07001109 asTransaction([&](Transaction& t) {
1110 t.detachChildren(mFGSurfaceControl);
1111 });
chaviw161410b02017-07-27 10:46:08 -07001112
Robert Carr4cdc58f2017-08-23 14:22:20 -07001113 asTransaction([&](Transaction& t) {
1114 t.hide(mChildNewClient);
1115 });
chaviw161410b02017-07-27 10:46:08 -07001116
Robert Carr9524cb32017-02-13 11:32:32 -08001117 // Nothing should have changed.
1118 {
1119 ScreenCapture::captureScreen(&mCapture);
1120 mCapture->expectFGColor(64, 64);
1121 mCapture->expectChildColor(74, 74);
1122 mCapture->expectFGColor(84, 84);
1123 }
1124}
1125
Robert Carr9b429f42017-04-17 14:56:57 -07001126TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001127 asTransaction([&](Transaction& t) {
1128 t.show(mChild);
1129 t.setPosition(mChild, 0, 0);
1130 t.setPosition(mFGSurfaceControl, 0, 0);
1131 });
Robert Carr9b429f42017-04-17 14:56:57 -07001132
1133 {
1134 ScreenCapture::captureScreen(&mCapture);
1135 // We've positioned the child in the top left.
1136 mCapture->expectChildColor(0, 0);
1137 // But it's only 10x10.
1138 mCapture->expectFGColor(10, 10);
1139 }
1140
Robert Carr4cdc58f2017-08-23 14:22:20 -07001141 asTransaction([&](Transaction& t) {
1142 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1143 // We cause scaling by 2.
1144 t.setSize(mFGSurfaceControl, 128, 128);
1145 });
Robert Carr9b429f42017-04-17 14:56:57 -07001146
1147 {
1148 ScreenCapture::captureScreen(&mCapture);
1149 // We've positioned the child in the top left.
1150 mCapture->expectChildColor(0, 0);
1151 mCapture->expectChildColor(10, 10);
1152 mCapture->expectChildColor(19, 19);
1153 // And now it should be scaled all the way to 20x20
1154 mCapture->expectFGColor(20, 20);
1155 }
1156}
1157
Robert Carr1725eee2017-04-26 18:32:15 -07001158// Regression test for b/37673612
1159TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001160 asTransaction([&](Transaction& t) {
1161 t.show(mChild);
1162 t.setPosition(mChild, 0, 0);
1163 t.setPosition(mFGSurfaceControl, 0, 0);
1164 });
Robert Carr1725eee2017-04-26 18:32:15 -07001165
1166 {
1167 ScreenCapture::captureScreen(&mCapture);
1168 // We've positioned the child in the top left.
1169 mCapture->expectChildColor(0, 0);
1170 // But it's only 10x10.
1171 mCapture->expectFGColor(10, 10);
1172 }
Robert Carr1725eee2017-04-26 18:32:15 -07001173 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1174 // the WM specified state size.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001175 asTransaction([&](Transaction& t) {
1176 t.setSize(mFGSurfaceControl, 128, 64);
1177 });
Robert Carr1725eee2017-04-26 18:32:15 -07001178 sp<Surface> s = mFGSurfaceControl->getSurface();
1179 auto anw = static_cast<ANativeWindow*>(s.get());
1180 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1181 native_window_set_buffers_dimensions(anw, 64, 128);
1182 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1183 waitForPostedBuffers();
1184
1185 {
1186 // The child should still be in the same place and not have any strange scaling as in
1187 // b/37673612.
1188 ScreenCapture::captureScreen(&mCapture);
1189 mCapture->expectChildColor(0, 0);
1190 mCapture->expectFGColor(10, 10);
1191 }
1192}
1193
Dan Stoza412903f2017-04-27 13:42:17 -07001194TEST_F(ChildLayerTest, Bug36858924) {
1195 // Destroy the child layer
1196 mChild.clear();
1197
1198 // Now recreate it as hidden
1199 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1200 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1201 mFGSurfaceControl.get());
1202
1203 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001204 asTransaction([&](Transaction& t) {
1205 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
1206 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1207 t.show(mChild);
1208 });
Dan Stoza412903f2017-04-27 13:42:17 -07001209
1210 // Render the foreground surface a few times
1211 //
1212 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1213 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1214 // never acquire/release the first buffer
1215 ALOGI("Filling 1");
1216 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1217 ALOGI("Filling 2");
1218 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1219 ALOGI("Filling 3");
1220 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1221 ALOGI("Filling 4");
1222 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1223}
1224
chaviwf1961f72017-09-18 16:41:07 -07001225TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001226 asTransaction([&](Transaction& t) {
1227 t.show(mChild);
1228 t.setPosition(mChild, 10, 10);
1229 t.setPosition(mFGSurfaceControl, 64, 64);
1230 });
chaviw06178942017-07-27 10:25:59 -07001231
1232 {
1233 ScreenCapture::captureScreen(&mCapture);
1234 // Top left of foreground must now be visible
1235 mCapture->expectFGColor(64, 64);
1236 // But 10 pixels in we should see the child surface
1237 mCapture->expectChildColor(74, 74);
1238 // And 10 more pixels we should be back to the foreground surface
1239 mCapture->expectFGColor(84, 84);
1240 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001241
1242 asTransaction([&](Transaction& t) {
1243 t.reparent(mChild, mBGSurfaceControl->getHandle());
1244 });
1245
chaviw06178942017-07-27 10:25:59 -07001246 {
1247 ScreenCapture::captureScreen(&mCapture);
1248 mCapture->expectFGColor(64, 64);
1249 // In reparenting we should have exposed the entire foreground surface.
1250 mCapture->expectFGColor(74, 74);
1251 // And the child layer should now begin at 10, 10 (since the BG
1252 // layer is at (0, 0)).
1253 mCapture->expectBGColor(9, 9);
1254 mCapture->expectChildColor(10, 10);
1255 }
1256}
1257
chaviwf1961f72017-09-18 16:41:07 -07001258TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001259 asTransaction([&](Transaction& t) {
1260 t.show(mChild);
1261 t.setPosition(mChild, 10, 10);
1262 t.setPosition(mFGSurfaceControl, 64, 64);
1263 });
chaviwf1961f72017-09-18 16:41:07 -07001264
1265 {
1266 ScreenCapture::captureScreen(&mCapture);
1267 // Top left of foreground must now be visible
1268 mCapture->expectFGColor(64, 64);
1269 // But 10 pixels in we should see the child surface
1270 mCapture->expectChildColor(74, 74);
1271 // And 10 more pixels we should be back to the foreground surface
1272 mCapture->expectFGColor(84, 84);
1273 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001274 asTransaction([&](Transaction& t) {
1275 t.reparent(mChild, nullptr);
1276 });
chaviwf1961f72017-09-18 16:41:07 -07001277 {
1278 ScreenCapture::captureScreen(&mCapture);
1279 // Nothing should have changed.
1280 mCapture->expectFGColor(64, 64);
1281 mCapture->expectChildColor(74, 74);
1282 mCapture->expectFGColor(84, 84);
1283 }
1284}
1285
1286TEST_F(ChildLayerTest, ReparentFromNoParent) {
1287 sp<SurfaceControl> newSurface = mComposerClient->createSurface(
1288 String8("New Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, 0);
1289 ASSERT_TRUE(newSurface != NULL);
1290 ASSERT_TRUE(newSurface->isValid());
1291
1292 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001293 asTransaction([&](Transaction& t) {
1294 t.hide(mChild);
1295 t.show(newSurface);
1296 t.setPosition(newSurface, 10, 10);
1297 t.setLayer(newSurface, INT32_MAX-2);
1298 t.setPosition(mFGSurfaceControl, 64, 64);
1299 });
chaviwf1961f72017-09-18 16:41:07 -07001300
1301 {
1302 ScreenCapture::captureScreen(&mCapture);
1303 // Top left of foreground must now be visible
1304 mCapture->expectFGColor(64, 64);
1305 // At 10, 10 we should see the new surface
1306 mCapture->checkPixel(10, 10, 63, 195, 63);
1307 }
1308
Robert Carr4cdc58f2017-08-23 14:22:20 -07001309 asTransaction([&](Transaction& t) {
1310 t.reparent(newSurface, mFGSurfaceControl->getHandle());
1311 });
chaviwf1961f72017-09-18 16:41:07 -07001312
1313 {
1314 ScreenCapture::captureScreen(&mCapture);
1315 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1316 // mFGSurface, putting it at 74, 74.
1317 mCapture->expectFGColor(64, 64);
1318 mCapture->checkPixel(74, 74, 63, 195, 63);
1319 mCapture->expectFGColor(84, 84);
1320 }
1321}
1322
chaviwc9674332017-08-28 12:32:18 -07001323TEST_F(ChildLayerTest, NestedChildren) {
1324 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1325 String8("Grandchild surface"),
1326 10, 10, PIXEL_FORMAT_RGBA_8888,
1327 0, mChild.get());
1328 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1329
1330 {
1331 ScreenCapture::captureScreen(&mCapture);
1332 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1333 // which begins at 64, 64
1334 mCapture->checkPixel(64, 64, 50, 50, 50);
1335 }
1336}
1337
chaviw13fdc492017-06-27 12:40:18 -07001338class LayerColorTest : public LayerUpdateTest {
1339 protected:
1340 void SetUp() override {
1341 LayerUpdateTest::SetUp();
1342
1343 mLayerColorControl = mComposerClient->createSurface(
1344 String8("Layer color surface"),
1345 128, 128, PIXEL_FORMAT_RGBA_8888,
1346 ISurfaceComposerClient::eFXSurfaceColor);
1347
1348 ASSERT_TRUE(mLayerColorControl != NULL);
1349 ASSERT_TRUE(mLayerColorControl->isValid());
1350
Robert Carr4cdc58f2017-08-23 14:22:20 -07001351 asTransaction([&](Transaction& t) {
1352 t.setLayer(mLayerColorControl, INT32_MAX-1);
1353 t.setPosition(mLayerColorControl, 140, 140);
1354 t.hide(mLayerColorControl);
1355 t.hide(mFGSurfaceControl);
1356 });
chaviw13fdc492017-06-27 12:40:18 -07001357 }
1358
1359 void TearDown() override {
1360 LayerUpdateTest::TearDown();
1361 mLayerColorControl = 0;
1362 }
1363
1364 sp<SurfaceControl> mLayerColorControl;
1365};
1366
1367TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1368 sp<ScreenCapture> sc;
1369
1370 {
1371 SCOPED_TRACE("before setColor");
1372 ScreenCapture::captureScreen(&sc);
1373 sc->expectBGColor(145, 145);
1374 }
1375
Robert Carr4cdc58f2017-08-23 14:22:20 -07001376 asTransaction([&](Transaction& t) {
1377 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1378 t.setColor(mLayerColorControl, color);
1379 t.show(mLayerColorControl);
1380 });
chaviw13fdc492017-06-27 12:40:18 -07001381
chaviw13fdc492017-06-27 12:40:18 -07001382 {
1383 // There should now be a color
1384 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001385
chaviw13fdc492017-06-27 12:40:18 -07001386 ScreenCapture::captureScreen(&sc);
1387 sc->checkPixel(145, 145, 43, 207, 131);
1388 }
1389}
1390
1391TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1392 sp<ScreenCapture> sc;
1393 {
1394 SCOPED_TRACE("before setColor");
1395 ScreenCapture::captureScreen(&sc);
1396 sc->expectBGColor(145, 145);
1397 }
1398
Robert Carr4cdc58f2017-08-23 14:22:20 -07001399 asTransaction([&](Transaction& t) {
1400 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1401 t.setColor(mLayerColorControl, color);
1402 t.setAlpha(mLayerColorControl, .75f);
1403 t.show(mLayerColorControl);
1404 });
1405
chaviw13fdc492017-06-27 12:40:18 -07001406 {
1407 // There should now be a color with .75 alpha
1408 SCOPED_TRACE("after setColor");
1409 ScreenCapture::captureScreen(&sc);
1410 sc->checkPixel(145, 145, 48, 171, 147);
1411 }
1412}
1413
1414TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1415 sp<ScreenCapture> sc;
1416 {
1417 SCOPED_TRACE("before setColor");
1418 ScreenCapture::captureScreen(&sc);
1419 sc->expectBGColor(145, 145);
1420 }
1421
Robert Carr4cdc58f2017-08-23 14:22:20 -07001422 asTransaction([&](Transaction& t) {
1423 t.show(mLayerColorControl);
1424 });
1425
chaviw13fdc492017-06-27 12:40:18 -07001426 {
1427 // There should now be set to 0,0,0 (black) as default.
1428 SCOPED_TRACE("after setColor");
1429 ScreenCapture::captureScreen(&sc);
1430 sc->checkPixel(145, 145, 0, 0, 0);
1431 }
1432}
1433
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001434}