blob: 57a55fde4555a28fdc5e2a9fa076527836886c9c [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
chaviwa76b2712017-09-20 12:02:26 -0700119class CaptureLayer {
120public:
121 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
122 sp<IGraphicBufferProducer> producer;
123 sp<IGraphicBufferConsumer> consumer;
124 BufferQueue::createBufferQueue(&producer, &consumer);
125 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
126 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
127 sp<IBinder> display(sf->getBuiltInDisplay(
128 ISurfaceComposer::eDisplayIdMain));
129 SurfaceComposerClient::Transaction().apply(true);
130 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
131 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
132 }
133
134 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
135 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
136 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
137 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
138 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
139 String8 err(String8::format("pixel @ (%3d, %3d): "
140 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
141 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
142 EXPECT_EQ(String8(), err) << err.string();
143 }
144 }
145
146 void expectFGColor(uint32_t x, uint32_t y) {
147 checkPixel(x, y, 195, 63, 63);
148 }
149
150 void expectBGColor(uint32_t x, uint32_t y) {
151 checkPixel(x, y, 63, 63, 195);
152 }
153
154 void expectChildColor(uint32_t x, uint32_t y) {
155 checkPixel(x, y, 200, 200, 200);
156 }
157
158 CaptureLayer(const sp<CpuConsumer>& cc) :
159 mCC(cc) {
160 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
161 }
162
163 ~CaptureLayer() {
164 mCC->unlockBuffer(mBuffer);
165 }
166
167private:
168 sp<CpuConsumer> mCC;
169 CpuConsumer::LockedBuffer mBuffer;
170};
171
172
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700173class LayerUpdateTest : public ::testing::Test {
174protected:
175 virtual void SetUp() {
176 mComposerClient = new SurfaceComposerClient;
177 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
178
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700179 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
180 ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700181 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700182 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700183
184 ssize_t displayWidth = info.w;
185 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700186
187 // Background surface
188 mBGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700189 String8("BG Test Surface"), displayWidth, displayHeight,
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700190 PIXEL_FORMAT_RGBA_8888, 0);
191 ASSERT_TRUE(mBGSurfaceControl != NULL);
192 ASSERT_TRUE(mBGSurfaceControl->isValid());
193 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
194
195 // Foreground surface
196 mFGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700197 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700198 ASSERT_TRUE(mFGSurfaceControl != NULL);
199 ASSERT_TRUE(mFGSurfaceControl->isValid());
200
201 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
202
203 // Synchronization surface
204 mSyncSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700205 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700206 ASSERT_TRUE(mSyncSurfaceControl != NULL);
207 ASSERT_TRUE(mSyncSurfaceControl->isValid());
208
209 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
210
Robert Carr4cdc58f2017-08-23 14:22:20 -0700211 asTransaction([&](Transaction& t) {
212 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700213
Robert Carr4cdc58f2017-08-23 14:22:20 -0700214 t.setLayer(mBGSurfaceControl, INT32_MAX-2)
215 .show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700216
Robert Carr4cdc58f2017-08-23 14:22:20 -0700217 t.setLayer(mFGSurfaceControl, INT32_MAX-1)
218 .setPosition(mFGSurfaceControl, 64, 64)
219 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700220
Robert Carr4cdc58f2017-08-23 14:22:20 -0700221 t.setLayer(mSyncSurfaceControl, INT32_MAX-1)
222 .setPosition(mSyncSurfaceControl, displayWidth-2,
223 displayHeight-2)
224 .show(mSyncSurfaceControl);
225 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700226 }
227
228 virtual void TearDown() {
229 mComposerClient->dispose();
230 mBGSurfaceControl = 0;
231 mFGSurfaceControl = 0;
232 mSyncSurfaceControl = 0;
233 mComposerClient = 0;
234 }
235
236 void waitForPostedBuffers() {
237 // Since the sync surface is in synchronous mode (i.e. double buffered)
238 // posting three buffers to it should ensure that at least two
239 // SurfaceFlinger::handlePageFlip calls have been made, which should
240 // guaranteed that a buffer posted to another Surface has been retired.
241 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
242 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
243 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
244 }
245
Robert Carr4cdc58f2017-08-23 14:22:20 -0700246 void asTransaction(const std::function<void(Transaction&)>& exec) {
247 Transaction t;
248 exec(t);
249 t.apply(true);
250 }
251
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700252 sp<SurfaceComposerClient> mComposerClient;
253 sp<SurfaceControl> mBGSurfaceControl;
254 sp<SurfaceControl> mFGSurfaceControl;
255
256 // This surface is used to ensure that the buffers posted to
257 // mFGSurfaceControl have been picked up by SurfaceFlinger.
258 sp<SurfaceControl> mSyncSurfaceControl;
259};
260
Robert Carr7f619b22017-11-06 12:56:35 -0800261TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
262 sp<ScreenCapture> sc;
263
264 sp<SurfaceControl> relative = mComposerClient->createSurface(
265 String8("relativeTestSurface"), 10, 10, PIXEL_FORMAT_RGBA_8888, 0);
266 fillSurfaceRGBA8(relative, 10, 10, 10);
267 waitForPostedBuffers();
268
269 Transaction{}.setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
270 .setPosition(relative, 64, 64)
271 .apply();
272
273 {
274 // The relative should be on top of the FG control.
275 ScreenCapture::captureScreen(&sc);
276 sc->checkPixel(64, 64, 10, 10, 10);
277 }
278 Transaction{}.detachChildren(mFGSurfaceControl)
279 .apply();
280
281 {
282 // Nothing should change at this point.
283 ScreenCapture::captureScreen(&sc);
284 sc->checkPixel(64, 64, 10, 10, 10);
285 }
286
287 Transaction{}.hide(relative)
288 .apply();
289
290 {
291 // Ensure that the relative was actually hidden, rather than
292 // being left in the detached but visible state.
293 ScreenCapture::captureScreen(&sc);
294 sc->expectFGColor(64, 64);
295 }
296}
297
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700298TEST_F(LayerUpdateTest, LayerMoveWorks) {
299 sp<ScreenCapture> sc;
300 {
301 SCOPED_TRACE("before move");
302 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800303 sc->expectBGColor(0, 12);
304 sc->expectFGColor(75, 75);
305 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700306 }
307
Robert Carr4cdc58f2017-08-23 14:22:20 -0700308 asTransaction([&](Transaction& t) {
309 t.setPosition(mFGSurfaceControl, 128, 128);
310 });
311
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700312 {
313 // This should reflect the new position, but not the new color.
314 SCOPED_TRACE("after move, before redraw");
315 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800316 sc->expectBGColor(24, 24);
317 sc->expectBGColor(75, 75);
318 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700319 }
320
321 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
322 waitForPostedBuffers();
323 {
324 // This should reflect the new position and the new color.
325 SCOPED_TRACE("after redraw");
326 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800327 sc->expectBGColor(24, 24);
328 sc->expectBGColor(75, 75);
329 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700330 }
331}
332
333TEST_F(LayerUpdateTest, LayerResizeWorks) {
334 sp<ScreenCapture> sc;
335 {
336 SCOPED_TRACE("before resize");
337 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800338 sc->expectBGColor(0, 12);
339 sc->expectFGColor(75, 75);
340 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700341 }
342
Steve Block9d453682011-12-20 16:23:08 +0000343 ALOGD("resizing");
Robert Carr4cdc58f2017-08-23 14:22:20 -0700344 asTransaction([&](Transaction& t) {
345 t.setSize(mFGSurfaceControl, 128, 128);
346 });
Steve Block9d453682011-12-20 16:23:08 +0000347 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700348 {
349 // This should not reflect the new size or color because SurfaceFlinger
350 // has not yet received a buffer of the correct size.
351 SCOPED_TRACE("after resize, before redraw");
352 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800353 sc->expectBGColor(0, 12);
354 sc->expectFGColor(75, 75);
355 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700356 }
357
Steve Block9d453682011-12-20 16:23:08 +0000358 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700359 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
360 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000361 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700362 {
363 // This should reflect the new size and the new color.
364 SCOPED_TRACE("after redraw");
365 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800366 sc->expectBGColor(24, 24);
367 sc->checkPixel(75, 75, 63, 195, 63);
368 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700369 }
370}
371
Haixia Shid5750962015-07-27 16:50:49 -0700372TEST_F(LayerUpdateTest, LayerCropWorks) {
373 sp<ScreenCapture> sc;
374 {
375 SCOPED_TRACE("before crop");
376 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800377 sc->expectBGColor(24, 24);
378 sc->expectFGColor(75, 75);
379 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700380 }
381
Robert Carr4cdc58f2017-08-23 14:22:20 -0700382 asTransaction([&](Transaction& t) {
383 Rect cropRect(16, 16, 32, 32);
384 t.setCrop(mFGSurfaceControl, cropRect);
385 });
Haixia Shid5750962015-07-27 16:50:49 -0700386 {
387 // This should crop the foreground surface.
388 SCOPED_TRACE("after crop");
389 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800390 sc->expectBGColor(24, 24);
391 sc->expectBGColor(75, 75);
392 sc->expectFGColor(95, 80);
393 sc->expectFGColor(80, 95);
394 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700395 }
396}
397
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000398TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
399 sp<ScreenCapture> sc;
400 {
401 SCOPED_TRACE("before crop");
402 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800403 sc->expectBGColor(24, 24);
404 sc->expectFGColor(75, 75);
405 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000406 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700407 asTransaction([&](Transaction& t) {
408 Rect cropRect(16, 16, 32, 32);
409 t.setFinalCrop(mFGSurfaceControl, cropRect);
410 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000411 {
412 // This should crop the foreground surface.
413 SCOPED_TRACE("after crop");
414 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800415 sc->expectBGColor(24, 24);
416 sc->expectBGColor(75, 75);
417 sc->expectBGColor(95, 80);
418 sc->expectBGColor(80, 95);
419 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420 }
421}
422
Haixia Shid5750962015-07-27 16:50:49 -0700423TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
424 sp<ScreenCapture> sc;
425 {
426 SCOPED_TRACE("before setLayer");
427 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800428 sc->expectBGColor(24, 24);
429 sc->expectFGColor(75, 75);
430 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700431 }
432
Robert Carr4cdc58f2017-08-23 14:22:20 -0700433 asTransaction([&](Transaction& t) {
434 t.setLayer(mFGSurfaceControl, INT_MAX - 3);
435 });
436
Haixia Shid5750962015-07-27 16:50:49 -0700437 {
438 // This should hide the foreground surface beneath the background.
439 SCOPED_TRACE("after setLayer");
440 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800441 sc->expectBGColor(24, 24);
442 sc->expectBGColor(75, 75);
443 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700444 }
445}
446
447TEST_F(LayerUpdateTest, LayerShowHideWorks) {
448 sp<ScreenCapture> sc;
449 {
450 SCOPED_TRACE("before hide");
451 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800452 sc->expectBGColor(24, 24);
453 sc->expectFGColor(75, 75);
454 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700455 }
456
Robert Carr4cdc58f2017-08-23 14:22:20 -0700457 asTransaction([&](Transaction& t) {
458 t.hide(mFGSurfaceControl);
459 });
460
Haixia Shid5750962015-07-27 16:50:49 -0700461 {
462 // This should hide the foreground surface.
463 SCOPED_TRACE("after hide, before show");
464 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800465 sc->expectBGColor(24, 24);
466 sc->expectBGColor(75, 75);
467 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700468 }
469
Robert Carr4cdc58f2017-08-23 14:22:20 -0700470 asTransaction([&](Transaction& t) {
471 t.show(mFGSurfaceControl);
472 });
473
Haixia Shid5750962015-07-27 16:50:49 -0700474 {
475 // This should show the foreground surface.
476 SCOPED_TRACE("after show");
477 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800478 sc->expectBGColor(24, 24);
479 sc->expectFGColor(75, 75);
480 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700481 }
482}
483
484TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
485 sp<ScreenCapture> sc;
486 {
487 SCOPED_TRACE("before setAlpha");
488 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800489 sc->expectBGColor(24, 24);
490 sc->expectFGColor(75, 75);
491 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700492 }
493
Robert Carr4cdc58f2017-08-23 14:22:20 -0700494 asTransaction([&](Transaction& t) {
495 t.setAlpha(mFGSurfaceControl, 0.75f);
496 });
497
Haixia Shid5750962015-07-27 16:50:49 -0700498 {
499 // This should set foreground to be 75% opaque.
500 SCOPED_TRACE("after setAlpha");
501 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800502 sc->expectBGColor(24, 24);
503 sc->checkPixel(75, 75, 162, 63, 96);
504 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700505 }
506}
507
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700508TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
509 sp<ScreenCapture> sc;
510 {
511 SCOPED_TRACE("before setLayerStack");
512 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800513 sc->expectBGColor(24, 24);
514 sc->expectFGColor(75, 75);
515 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700516 }
517
Robert Carr4cdc58f2017-08-23 14:22:20 -0700518 asTransaction([&](Transaction& t) {
519 t.setLayerStack(mFGSurfaceControl, 1);
520 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700521 {
522 // This should hide the foreground surface since it goes to a different
523 // layer stack.
524 SCOPED_TRACE("after setLayerStack");
525 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800526 sc->expectBGColor(24, 24);
527 sc->expectBGColor(75, 75);
528 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700529 }
530}
531
532TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
533 sp<ScreenCapture> sc;
534 {
535 SCOPED_TRACE("before setFlags");
536 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800537 sc->expectBGColor(24, 24);
538 sc->expectFGColor(75, 75);
539 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700540 }
541
Robert Carr4cdc58f2017-08-23 14:22:20 -0700542 asTransaction([&](Transaction& t) {
543 t.setFlags(mFGSurfaceControl,
544 layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
545 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700546 {
547 // This should hide the foreground surface
548 SCOPED_TRACE("after setFlags");
549 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800550 sc->expectBGColor(24, 24);
551 sc->expectBGColor(75, 75);
552 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700553 }
554}
555
556TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
557 sp<ScreenCapture> sc;
558 {
559 SCOPED_TRACE("before setMatrix");
560 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800561 sc->expectBGColor(24, 24);
562 sc->expectFGColor(91, 96);
563 sc->expectFGColor(96, 101);
564 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700565 }
566
Robert Carr4cdc58f2017-08-23 14:22:20 -0700567 asTransaction([&](Transaction& t) {
568 t.setMatrix(mFGSurfaceControl,
569 M_SQRT1_2, M_SQRT1_2,
570 -M_SQRT1_2, M_SQRT1_2);
571 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700572 {
573 SCOPED_TRACE("after setMatrix");
574 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800575 sc->expectBGColor(24, 24);
576 sc->expectFGColor(91, 96);
577 sc->expectBGColor(96, 91);
578 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700579 }
580}
581
Robert Carr8d5227b2017-03-16 15:41:03 -0700582class GeometryLatchingTest : public LayerUpdateTest {
583protected:
584 void EXPECT_INITIAL_STATE(const char * trace) {
585 SCOPED_TRACE(trace);
586 ScreenCapture::captureScreen(&sc);
587 // We find the leading edge of the FG surface.
588 sc->expectFGColor(127, 127);
589 sc->expectBGColor(128, 128);
590 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700591
592 void lockAndFillFGBuffer() {
593 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
594 }
595
596 void unlockFGBuffer() {
597 sp<Surface> s = mFGSurfaceControl->getSurface();
598 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
599 waitForPostedBuffers();
600 }
601
Robert Carr8d5227b2017-03-16 15:41:03 -0700602 void completeFGResize() {
603 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
604 waitForPostedBuffers();
605 }
606 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700607 asTransaction([&](Transaction& t) {
608 t.setSize(mFGSurfaceControl, 64, 64);
609 t.setPosition(mFGSurfaceControl, 64, 64);
610 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
611 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
612 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700613
614 EXPECT_INITIAL_STATE("After restoring initial state");
615 }
616 sp<ScreenCapture> sc;
617};
618
619TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
620 EXPECT_INITIAL_STATE("before anything");
621
622 // By default position can be updated even while
623 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700624 asTransaction([&](Transaction& t) {
625 t.setSize(mFGSurfaceControl, 32, 32);
626 t.setPosition(mFGSurfaceControl, 100, 100);
627 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700628
629 {
630 SCOPED_TRACE("After moving surface");
631 ScreenCapture::captureScreen(&sc);
632 // If we moved, the FG Surface should cover up what was previously BG
633 // however if we didn't move the FG wouldn't be large enough now.
634 sc->expectFGColor(163, 163);
635 }
636
637 restoreInitialState();
638
639 // Now we repeat with setGeometryAppliesWithResize
640 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700641 asTransaction([&](Transaction& t) {
642 t.setGeometryAppliesWithResize(mFGSurfaceControl);
643 t.setSize(mFGSurfaceControl, 32, 32);
644 t.setPosition(mFGSurfaceControl, 100, 100);
645 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700646
647 {
648 SCOPED_TRACE("While resize is pending");
649 ScreenCapture::captureScreen(&sc);
650 // This time we shouldn't have moved, so the BG color
651 // should still be visible.
652 sc->expectBGColor(128, 128);
653 }
654
655 completeFGResize();
656
657 {
658 SCOPED_TRACE("After the resize");
659 ScreenCapture::captureScreen(&sc);
660 // But after the resize completes, we should move
661 // and the FG should be visible here.
662 sc->expectFGColor(128, 128);
663 }
664}
665
666class CropLatchingTest : public GeometryLatchingTest {
667protected:
668 void EXPECT_CROPPED_STATE(const char* trace) {
669 SCOPED_TRACE(trace);
670 ScreenCapture::captureScreen(&sc);
671 // The edge should be moved back one pixel by our crop.
672 sc->expectFGColor(126, 126);
673 sc->expectBGColor(127, 127);
674 sc->expectBGColor(128, 128);
675 }
chaviw59f5c562017-06-28 16:39:06 -0700676
677 void EXPECT_RESIZE_STATE(const char* trace) {
678 SCOPED_TRACE(trace);
679 ScreenCapture::captureScreen(&sc);
680 // The FG is now resized too 128,128 at 64,64
681 sc->expectFGColor(64, 64);
682 sc->expectFGColor(191, 191);
683 sc->expectBGColor(192, 192);
684 }
Robert Carr8d5227b2017-03-16 15:41:03 -0700685};
686
687TEST_F(CropLatchingTest, CropLatching) {
688 EXPECT_INITIAL_STATE("before anything");
689 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700690 asTransaction([&](Transaction& t) {
691 t.setSize(mFGSurfaceControl, 128, 128);
692 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
693 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700694
695 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
696
697 restoreInitialState();
698
Robert Carr4cdc58f2017-08-23 14:22:20 -0700699 asTransaction([&](Transaction& t) {
700 t.setSize(mFGSurfaceControl, 128, 128);
701 t.setGeometryAppliesWithResize(mFGSurfaceControl);
702 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
703 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700704
705 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
706
707 completeFGResize();
708
709 EXPECT_CROPPED_STATE("after the resize finishes");
710}
711
712TEST_F(CropLatchingTest, FinalCropLatching) {
713 EXPECT_INITIAL_STATE("before anything");
714 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700715 asTransaction([&](Transaction& t) {
716 t.setSize(mFGSurfaceControl, 128, 128);
717 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
718 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700719
720 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
721
722 restoreInitialState();
723
Robert Carr4cdc58f2017-08-23 14:22:20 -0700724 asTransaction([&](Transaction& t) {
725 t.setSize(mFGSurfaceControl, 128, 128);
726 t.setGeometryAppliesWithResize(mFGSurfaceControl);
727 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
728 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700729
730 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
731
732 completeFGResize();
733
734 EXPECT_CROPPED_STATE("after the resize finishes");
735}
736
Robert Carr7bf247e2017-05-18 14:02:49 -0700737// In this test we ensure that setGeometryAppliesWithResize actually demands
738// a buffer of the new size, and not just any size.
739TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
740 EXPECT_INITIAL_STATE("before anything");
741 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700742 asTransaction([&](Transaction& t) {
743 t.setSize(mFGSurfaceControl, 128, 128);
744 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
745 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700746
747 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
748
749 restoreInitialState();
750
751 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
752 // initiating the resize.
753 lockAndFillFGBuffer();
754
Robert Carr4cdc58f2017-08-23 14:22:20 -0700755 asTransaction([&](Transaction& t) {
756 t.setSize(mFGSurfaceControl, 128, 128);
757 t.setGeometryAppliesWithResize(mFGSurfaceControl);
758 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
759 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700760
761 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
762
763 // We now submit our old buffer, at the old size, and ensure it doesn't
764 // trigger geometry latching.
765 unlockFGBuffer();
766
767 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
768
769 completeFGResize();
770
771 EXPECT_CROPPED_STATE("after the resize finishes");
772}
773
774TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
775 EXPECT_INITIAL_STATE("before anything");
776 // In this scenario, we attempt to set the final crop a second time while the resize
777 // is still pending, and ensure we are successful. Success meaning the second crop
778 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700779 asTransaction([&](Transaction& t) {
780 t.setSize(mFGSurfaceControl, 128, 128);
781 t.setGeometryAppliesWithResize(mFGSurfaceControl);
782 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
783 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700784
chaviw59f5c562017-06-28 16:39:06 -0700785 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
786
Robert Carr4cdc58f2017-08-23 14:22:20 -0700787 asTransaction([&](Transaction& t) {
788 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
789 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700790
chaviw59f5c562017-06-28 16:39:06 -0700791 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -0700792
793 completeFGResize();
794
chaviw59f5c562017-06-28 16:39:06 -0700795 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -0700796}
797
Pablo Ceballos05289c22016-04-14 15:49:55 -0700798TEST_F(LayerUpdateTest, DeferredTransactionTest) {
799 sp<ScreenCapture> sc;
800 {
801 SCOPED_TRACE("before anything");
802 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800803 sc->expectBGColor(32, 32);
804 sc->expectFGColor(96, 96);
805 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700806 }
807
808 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -0700809 asTransaction([&](Transaction& t) {
810 t.setAlpha(mFGSurfaceControl, 0.75);
811 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
812 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
813 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700814
Robert Carr4cdc58f2017-08-23 14:22:20 -0700815 asTransaction([&](Transaction& t) {
816 t.setPosition(mFGSurfaceControl, 128,128);
817 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
818 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
819 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700820
821 {
822 SCOPED_TRACE("before any trigger");
823 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800824 sc->expectBGColor(32, 32);
825 sc->expectFGColor(96, 96);
826 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700827 }
828
829 // should trigger the first deferred transaction, but not the second one
830 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
831 {
832 SCOPED_TRACE("after first trigger");
833 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800834 sc->expectBGColor(32, 32);
835 sc->checkPixel(96, 96, 162, 63, 96);
836 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700837 }
838
839 // should show up immediately since it's not deferred
Robert Carr4cdc58f2017-08-23 14:22:20 -0700840 asTransaction([&](Transaction& t) {
841 t.setAlpha(mFGSurfaceControl, 1.0);
842 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700843
844 // trigger the second deferred transaction
845 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
846 {
847 SCOPED_TRACE("after second trigger");
848 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800849 sc->expectBGColor(32, 32);
850 sc->expectBGColor(96, 96);
851 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700852 }
853}
854
Robert Carrdb66e622017-04-10 16:55:57 -0700855TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
856 sp<ScreenCapture> sc;
857 {
858 SCOPED_TRACE("before adding relative surface");
859 ScreenCapture::captureScreen(&sc);
860 sc->expectBGColor(24, 24);
861 sc->expectFGColor(75, 75);
862 sc->expectBGColor(145, 145);
863 }
864
865 auto relativeSurfaceControl = mComposerClient->createSurface(
866 String8("Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
867 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
868 waitForPostedBuffers();
869
870 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700871 asTransaction([&](Transaction& t) {
872 t.setPosition(relativeSurfaceControl, 64, 64);
873 t.show(relativeSurfaceControl);
874 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
875 });
Robert Carrdb66e622017-04-10 16:55:57 -0700876
877 {
878 SCOPED_TRACE("after adding relative surface");
879 ScreenCapture::captureScreen(&sc);
880 // our relative surface should be visible now.
881 sc->checkPixel(75, 75, 255, 177, 177);
882 }
883
884 // A call to setLayer will override a call to setRelativeLayer
Robert Carr4cdc58f2017-08-23 14:22:20 -0700885 asTransaction([&](Transaction& t) {
886 t.setLayer(relativeSurfaceControl, 0);
887 });
Robert Carrdb66e622017-04-10 16:55:57 -0700888
889 {
890 SCOPED_TRACE("after set layer");
891 ScreenCapture::captureScreen(&sc);
892 // now the FG surface should be visible again.
893 sc->expectFGColor(75, 75);
894 }
895}
896
Robert Carre392b552017-09-19 12:16:05 -0700897TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
898 sp<ScreenCapture> sc;
899
900 sp<SurfaceControl> childNoBuffer =
901 mComposerClient->createSurface(String8("Bufferless child"),
902 10, 10, PIXEL_FORMAT_RGBA_8888,
903 0, mFGSurfaceControl.get());
904 sp<SurfaceControl> childBuffer = mComposerClient->createSurface(
905 String8("Buffered child"), 20, 20,
906 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
907 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
908
Robert Carr4cdc58f2017-08-23 14:22:20 -0700909 SurfaceComposerClient::Transaction{}
910 .show(childNoBuffer)
911 .show(childBuffer)
912 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700913
914 {
915 ScreenCapture::captureScreen(&sc);
916 sc->expectChildColor(73, 73);
917 sc->expectFGColor(74, 74);
918 }
919
Robert Carr4cdc58f2017-08-23 14:22:20 -0700920 SurfaceComposerClient::Transaction{}
921 .setSize(childNoBuffer, 20, 20)
922 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700923
924 {
925 ScreenCapture::captureScreen(&sc);
926 sc->expectChildColor(73, 73);
927 sc->expectChildColor(74, 74);
928 }
929}
930
Robert Carr2c5f6d22017-09-26 12:30:35 -0700931TEST_F(LayerUpdateTest, MergingTransactions) {
932 sp<ScreenCapture> sc;
933 {
934 SCOPED_TRACE("before move");
935 ScreenCapture::captureScreen(&sc);
936 sc->expectBGColor(0, 12);
937 sc->expectFGColor(75, 75);
938 sc->expectBGColor(145, 145);
939 }
940
941 Transaction t1, t2;
942 t1.setPosition(mFGSurfaceControl, 128, 128);
943 t2.setPosition(mFGSurfaceControl, 0, 0);
944 // We expect that the position update from t2 now
945 // overwrites the position update from t1.
946 t1.merge(std::move(t2));
947 t1.apply();
948
949 {
950 ScreenCapture::captureScreen(&sc);
951 sc->expectFGColor(1, 1);
952 }
953}
954
Robert Carr1f0a16a2016-10-24 16:27:39 -0700955class ChildLayerTest : public LayerUpdateTest {
956protected:
957 void SetUp() override {
958 LayerUpdateTest::SetUp();
959 mChild = mComposerClient->createSurface(
960 String8("Child surface"),
961 10, 10, PIXEL_FORMAT_RGBA_8888,
962 0, mFGSurfaceControl.get());
963 fillSurfaceRGBA8(mChild, 200, 200, 200);
964
965 {
966 SCOPED_TRACE("before anything");
967 ScreenCapture::captureScreen(&mCapture);
968 mCapture->expectChildColor(64, 64);
969 }
970 }
971 void TearDown() override {
972 LayerUpdateTest::TearDown();
973 mChild = 0;
974 }
975
976 sp<SurfaceControl> mChild;
977 sp<ScreenCapture> mCapture;
978};
979
980TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700981 asTransaction([&](Transaction& t) {
982 t.show(mChild);
983 t.setPosition(mChild, 10, 10);
984 t.setPosition(mFGSurfaceControl, 64, 64);
985 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700986
987 {
988 ScreenCapture::captureScreen(&mCapture);
989 // Top left of foreground must now be visible
990 mCapture->expectFGColor(64, 64);
991 // But 10 pixels in we should see the child surface
992 mCapture->expectChildColor(74, 74);
993 // And 10 more pixels we should be back to the foreground surface
994 mCapture->expectFGColor(84, 84);
995 }
996
Robert Carr4cdc58f2017-08-23 14:22:20 -0700997 asTransaction([&](Transaction& t) {
998 t.setPosition(mFGSurfaceControl, 0, 0);
999 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001000
1001 {
1002 ScreenCapture::captureScreen(&mCapture);
1003 // Top left of foreground should now be at 0, 0
1004 mCapture->expectFGColor(0, 0);
1005 // But 10 pixels in we should see the child surface
1006 mCapture->expectChildColor(10, 10);
1007 // And 10 more pixels we should be back to the foreground surface
1008 mCapture->expectFGColor(20, 20);
1009 }
1010}
1011
Robert Carr41b08b52017-06-01 16:11:34 -07001012TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001013 asTransaction([&](Transaction& t) {
1014 t.show(mChild);
1015 t.setPosition(mChild, 0, 0);
1016 t.setPosition(mFGSurfaceControl, 0, 0);
1017 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1018 });
Robert Carr41b08b52017-06-01 16:11:34 -07001019
1020 {
1021 ScreenCapture::captureScreen(&mCapture);
1022 mCapture->expectChildColor(0, 0);
1023 mCapture->expectChildColor(4, 4);
1024 mCapture->expectBGColor(5, 5);
1025 }
1026}
1027
1028TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001029 asTransaction([&](Transaction& t) {
1030 t.show(mChild);
1031 t.setPosition(mChild, 0, 0);
1032 t.setPosition(mFGSurfaceControl, 0, 0);
1033 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1034 });
Robert Carr41b08b52017-06-01 16:11:34 -07001035
1036 {
1037 ScreenCapture::captureScreen(&mCapture);
1038 mCapture->expectChildColor(0, 0);
1039 mCapture->expectChildColor(4, 4);
1040 mCapture->expectBGColor(5, 5);
1041 }
1042}
1043
Robert Carr1f0a16a2016-10-24 16:27:39 -07001044TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001045 asTransaction([&](Transaction& t) {
1046 t.show(mChild);
1047 t.setPosition(mFGSurfaceControl, 0, 0);
1048 t.setPosition(mChild, 63, 63);
1049 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001050
1051 {
1052 ScreenCapture::captureScreen(&mCapture);
1053 mCapture->expectFGColor(0, 0);
1054 // Last pixel in foreground should now be the child.
1055 mCapture->expectChildColor(63, 63);
1056 // But the child should be constrained and the next pixel
1057 // must be the background
1058 mCapture->expectBGColor(64, 64);
1059 }
1060}
1061
1062TEST_F(ChildLayerTest, ChildLayerScaling) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001063 asTransaction([&](Transaction& t) {
1064 t.setPosition(mFGSurfaceControl, 0, 0);
1065 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001066
1067 // Find the boundary between the parent and child
1068 {
1069 ScreenCapture::captureScreen(&mCapture);
1070 mCapture->expectChildColor(9, 9);
1071 mCapture->expectFGColor(10, 10);
1072 }
1073
Robert Carr4cdc58f2017-08-23 14:22:20 -07001074 asTransaction([&](Transaction& t) {
1075 t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0);
1076 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001077
1078 // The boundary should be twice as far from the origin now.
1079 // The pixels from the last test should all be child now
1080 {
1081 ScreenCapture::captureScreen(&mCapture);
1082 mCapture->expectChildColor(9, 9);
1083 mCapture->expectChildColor(10, 10);
1084 mCapture->expectChildColor(19, 19);
1085 mCapture->expectFGColor(20, 20);
1086 }
1087}
Robert Carr9524cb32017-02-13 11:32:32 -08001088
Robert Carr6452f122017-03-21 10:41:29 -07001089TEST_F(ChildLayerTest, ChildLayerAlpha) {
1090 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1091 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1092 fillSurfaceRGBA8(mChild, 0, 254, 0);
1093 waitForPostedBuffers();
1094
Robert Carr4cdc58f2017-08-23 14:22:20 -07001095 asTransaction([&](Transaction& t) {
1096 t.show(mChild);
1097 t.setPosition(mChild, 0, 0);
1098 t.setPosition(mFGSurfaceControl, 0, 0);
1099 });
Robert Carr6452f122017-03-21 10:41:29 -07001100
1101 {
1102 ScreenCapture::captureScreen(&mCapture);
1103 // Unblended child color
1104 mCapture->checkPixel(0, 0, 0, 254, 0);
1105 }
1106
Robert Carr4cdc58f2017-08-23 14:22:20 -07001107 asTransaction([&](Transaction& t) {
1108 t.setAlpha(mChild, 0.5);
1109 });
Robert Carr6452f122017-03-21 10:41:29 -07001110
1111 {
1112 ScreenCapture::captureScreen(&mCapture);
1113 // Child and BG blended.
1114 mCapture->checkPixel(0, 0, 127, 127, 0);
1115 }
1116
Robert Carr4cdc58f2017-08-23 14:22:20 -07001117 asTransaction([&](Transaction& t) {
1118 t.setAlpha(mFGSurfaceControl, 0.5);
1119 });
Robert Carr6452f122017-03-21 10:41:29 -07001120
1121 {
1122 ScreenCapture::captureScreen(&mCapture);
1123 // Child and BG blended.
1124 mCapture->checkPixel(0, 0, 95, 64, 95);
1125 }
1126}
1127
Robert Carr9524cb32017-02-13 11:32:32 -08001128TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001129 asTransaction([&](Transaction& t) {
1130 t.show(mChild);
1131 t.setPosition(mChild, 10, 10);
1132 t.setPosition(mFGSurfaceControl, 64, 64);
1133 });
Robert Carr9524cb32017-02-13 11:32:32 -08001134
1135 {
1136 ScreenCapture::captureScreen(&mCapture);
1137 // Top left of foreground must now be visible
1138 mCapture->expectFGColor(64, 64);
1139 // But 10 pixels in we should see the child surface
1140 mCapture->expectChildColor(74, 74);
1141 // And 10 more pixels we should be back to the foreground surface
1142 mCapture->expectFGColor(84, 84);
1143 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001144
1145 asTransaction([&](Transaction& t) {
1146 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1147 });
1148
Robert Carr9524cb32017-02-13 11:32:32 -08001149 {
1150 ScreenCapture::captureScreen(&mCapture);
1151 mCapture->expectFGColor(64, 64);
1152 // In reparenting we should have exposed the entire foreground surface.
1153 mCapture->expectFGColor(74, 74);
1154 // And the child layer should now begin at 10, 10 (since the BG
1155 // layer is at (0, 0)).
1156 mCapture->expectBGColor(9, 9);
1157 mCapture->expectChildColor(10, 10);
1158 }
1159}
1160
chaviw161410b02017-07-27 10:46:08 -07001161TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001162 asTransaction([&](Transaction& t) {
1163 t.show(mChild);
1164 t.setPosition(mChild, 10, 10);
1165 t.setPosition(mFGSurfaceControl, 64, 64);
1166 });
Robert Carr9524cb32017-02-13 11:32:32 -08001167
1168 {
1169 ScreenCapture::captureScreen(&mCapture);
1170 // Top left of foreground must now be visible
1171 mCapture->expectFGColor(64, 64);
1172 // But 10 pixels in we should see the child surface
1173 mCapture->expectChildColor(74, 74);
1174 // And 10 more pixels we should be back to the foreground surface
1175 mCapture->expectFGColor(84, 84);
1176 }
1177
Robert Carr4cdc58f2017-08-23 14:22:20 -07001178 asTransaction([&](Transaction& t) {
1179 t.detachChildren(mFGSurfaceControl);
1180 });
Robert Carr9524cb32017-02-13 11:32:32 -08001181
Robert Carr4cdc58f2017-08-23 14:22:20 -07001182 asTransaction([&](Transaction& t) {
1183 t.hide(mChild);
1184 });
Robert Carr9524cb32017-02-13 11:32:32 -08001185
chaviw161410b02017-07-27 10:46:08 -07001186 // Since the child has the same client as the parent, it will not get
1187 // detached and will be hidden.
1188 {
1189 ScreenCapture::captureScreen(&mCapture);
1190 mCapture->expectFGColor(64, 64);
1191 mCapture->expectFGColor(74, 74);
1192 mCapture->expectFGColor(84, 84);
1193 }
1194}
1195
1196TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1197 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
1198 sp<SurfaceControl> mChildNewClient = mNewComposerClient->createSurface(
1199 String8("New Child Test Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1200 0, mFGSurfaceControl.get());
1201
1202 ASSERT_TRUE(mChildNewClient != NULL);
1203 ASSERT_TRUE(mChildNewClient->isValid());
1204
1205 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1206
Robert Carr4cdc58f2017-08-23 14:22:20 -07001207 asTransaction([&](Transaction& t) {
1208 t.hide(mChild);
1209 t.show(mChildNewClient);
1210 t.setPosition(mChildNewClient, 10, 10);
1211 t.setPosition(mFGSurfaceControl, 64, 64);
1212 });
chaviw161410b02017-07-27 10:46:08 -07001213
1214 {
1215 ScreenCapture::captureScreen(&mCapture);
1216 // Top left of foreground must now be visible
1217 mCapture->expectFGColor(64, 64);
1218 // But 10 pixels in we should see the child surface
1219 mCapture->expectChildColor(74, 74);
1220 // And 10 more pixels we should be back to the foreground surface
1221 mCapture->expectFGColor(84, 84);
1222 }
1223
Robert Carr4cdc58f2017-08-23 14:22:20 -07001224 asTransaction([&](Transaction& t) {
1225 t.detachChildren(mFGSurfaceControl);
1226 });
chaviw161410b02017-07-27 10:46:08 -07001227
Robert Carr4cdc58f2017-08-23 14:22:20 -07001228 asTransaction([&](Transaction& t) {
1229 t.hide(mChildNewClient);
1230 });
chaviw161410b02017-07-27 10:46:08 -07001231
Robert Carr9524cb32017-02-13 11:32:32 -08001232 // Nothing should have changed.
1233 {
1234 ScreenCapture::captureScreen(&mCapture);
1235 mCapture->expectFGColor(64, 64);
1236 mCapture->expectChildColor(74, 74);
1237 mCapture->expectFGColor(84, 84);
1238 }
1239}
1240
Robert Carr9b429f42017-04-17 14:56:57 -07001241TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001242 asTransaction([&](Transaction& t) {
1243 t.show(mChild);
1244 t.setPosition(mChild, 0, 0);
1245 t.setPosition(mFGSurfaceControl, 0, 0);
1246 });
Robert Carr9b429f42017-04-17 14:56:57 -07001247
1248 {
1249 ScreenCapture::captureScreen(&mCapture);
1250 // We've positioned the child in the top left.
1251 mCapture->expectChildColor(0, 0);
1252 // But it's only 10x10.
1253 mCapture->expectFGColor(10, 10);
1254 }
1255
Robert Carr4cdc58f2017-08-23 14:22:20 -07001256 asTransaction([&](Transaction& t) {
1257 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1258 // We cause scaling by 2.
1259 t.setSize(mFGSurfaceControl, 128, 128);
1260 });
Robert Carr9b429f42017-04-17 14:56:57 -07001261
1262 {
1263 ScreenCapture::captureScreen(&mCapture);
1264 // We've positioned the child in the top left.
1265 mCapture->expectChildColor(0, 0);
1266 mCapture->expectChildColor(10, 10);
1267 mCapture->expectChildColor(19, 19);
1268 // And now it should be scaled all the way to 20x20
1269 mCapture->expectFGColor(20, 20);
1270 }
1271}
1272
Robert Carr1725eee2017-04-26 18:32:15 -07001273// Regression test for b/37673612
1274TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001275 asTransaction([&](Transaction& t) {
1276 t.show(mChild);
1277 t.setPosition(mChild, 0, 0);
1278 t.setPosition(mFGSurfaceControl, 0, 0);
1279 });
Robert Carr1725eee2017-04-26 18:32:15 -07001280
1281 {
1282 ScreenCapture::captureScreen(&mCapture);
1283 // We've positioned the child in the top left.
1284 mCapture->expectChildColor(0, 0);
1285 // But it's only 10x10.
1286 mCapture->expectFGColor(10, 10);
1287 }
Robert Carr1725eee2017-04-26 18:32:15 -07001288 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1289 // the WM specified state size.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001290 asTransaction([&](Transaction& t) {
1291 t.setSize(mFGSurfaceControl, 128, 64);
1292 });
Robert Carr1725eee2017-04-26 18:32:15 -07001293 sp<Surface> s = mFGSurfaceControl->getSurface();
1294 auto anw = static_cast<ANativeWindow*>(s.get());
1295 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1296 native_window_set_buffers_dimensions(anw, 64, 128);
1297 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1298 waitForPostedBuffers();
1299
1300 {
1301 // The child should still be in the same place and not have any strange scaling as in
1302 // b/37673612.
1303 ScreenCapture::captureScreen(&mCapture);
1304 mCapture->expectChildColor(0, 0);
1305 mCapture->expectFGColor(10, 10);
1306 }
1307}
1308
Dan Stoza412903f2017-04-27 13:42:17 -07001309TEST_F(ChildLayerTest, Bug36858924) {
1310 // Destroy the child layer
1311 mChild.clear();
1312
1313 // Now recreate it as hidden
1314 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1315 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1316 mFGSurfaceControl.get());
1317
1318 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001319 asTransaction([&](Transaction& t) {
1320 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
1321 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1322 t.show(mChild);
1323 });
Dan Stoza412903f2017-04-27 13:42:17 -07001324
1325 // Render the foreground surface a few times
1326 //
1327 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1328 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1329 // never acquire/release the first buffer
1330 ALOGI("Filling 1");
1331 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1332 ALOGI("Filling 2");
1333 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1334 ALOGI("Filling 3");
1335 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1336 ALOGI("Filling 4");
1337 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1338}
1339
chaviwf1961f72017-09-18 16:41:07 -07001340TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001341 asTransaction([&](Transaction& t) {
1342 t.show(mChild);
1343 t.setPosition(mChild, 10, 10);
1344 t.setPosition(mFGSurfaceControl, 64, 64);
1345 });
chaviw06178942017-07-27 10:25:59 -07001346
1347 {
1348 ScreenCapture::captureScreen(&mCapture);
1349 // Top left of foreground must now be visible
1350 mCapture->expectFGColor(64, 64);
1351 // But 10 pixels in we should see the child surface
1352 mCapture->expectChildColor(74, 74);
1353 // And 10 more pixels we should be back to the foreground surface
1354 mCapture->expectFGColor(84, 84);
1355 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001356
1357 asTransaction([&](Transaction& t) {
1358 t.reparent(mChild, mBGSurfaceControl->getHandle());
1359 });
1360
chaviw06178942017-07-27 10:25:59 -07001361 {
1362 ScreenCapture::captureScreen(&mCapture);
1363 mCapture->expectFGColor(64, 64);
1364 // In reparenting we should have exposed the entire foreground surface.
1365 mCapture->expectFGColor(74, 74);
1366 // And the child layer should now begin at 10, 10 (since the BG
1367 // layer is at (0, 0)).
1368 mCapture->expectBGColor(9, 9);
1369 mCapture->expectChildColor(10, 10);
1370 }
1371}
1372
chaviwf1961f72017-09-18 16:41:07 -07001373TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001374 asTransaction([&](Transaction& t) {
1375 t.show(mChild);
1376 t.setPosition(mChild, 10, 10);
1377 t.setPosition(mFGSurfaceControl, 64, 64);
1378 });
chaviwf1961f72017-09-18 16:41:07 -07001379
1380 {
1381 ScreenCapture::captureScreen(&mCapture);
1382 // Top left of foreground must now be visible
1383 mCapture->expectFGColor(64, 64);
1384 // But 10 pixels in we should see the child surface
1385 mCapture->expectChildColor(74, 74);
1386 // And 10 more pixels we should be back to the foreground surface
1387 mCapture->expectFGColor(84, 84);
1388 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001389 asTransaction([&](Transaction& t) {
1390 t.reparent(mChild, nullptr);
1391 });
chaviwf1961f72017-09-18 16:41:07 -07001392 {
1393 ScreenCapture::captureScreen(&mCapture);
1394 // Nothing should have changed.
1395 mCapture->expectFGColor(64, 64);
1396 mCapture->expectChildColor(74, 74);
1397 mCapture->expectFGColor(84, 84);
1398 }
1399}
1400
1401TEST_F(ChildLayerTest, ReparentFromNoParent) {
1402 sp<SurfaceControl> newSurface = mComposerClient->createSurface(
1403 String8("New Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, 0);
1404 ASSERT_TRUE(newSurface != NULL);
1405 ASSERT_TRUE(newSurface->isValid());
1406
1407 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001408 asTransaction([&](Transaction& t) {
1409 t.hide(mChild);
1410 t.show(newSurface);
1411 t.setPosition(newSurface, 10, 10);
1412 t.setLayer(newSurface, INT32_MAX-2);
1413 t.setPosition(mFGSurfaceControl, 64, 64);
1414 });
chaviwf1961f72017-09-18 16:41:07 -07001415
1416 {
1417 ScreenCapture::captureScreen(&mCapture);
1418 // Top left of foreground must now be visible
1419 mCapture->expectFGColor(64, 64);
1420 // At 10, 10 we should see the new surface
1421 mCapture->checkPixel(10, 10, 63, 195, 63);
1422 }
1423
Robert Carr4cdc58f2017-08-23 14:22:20 -07001424 asTransaction([&](Transaction& t) {
1425 t.reparent(newSurface, mFGSurfaceControl->getHandle());
1426 });
chaviwf1961f72017-09-18 16:41:07 -07001427
1428 {
1429 ScreenCapture::captureScreen(&mCapture);
1430 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1431 // mFGSurface, putting it at 74, 74.
1432 mCapture->expectFGColor(64, 64);
1433 mCapture->checkPixel(74, 74, 63, 195, 63);
1434 mCapture->expectFGColor(84, 84);
1435 }
1436}
1437
chaviwc9674332017-08-28 12:32:18 -07001438TEST_F(ChildLayerTest, NestedChildren) {
1439 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1440 String8("Grandchild surface"),
1441 10, 10, PIXEL_FORMAT_RGBA_8888,
1442 0, mChild.get());
1443 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1444
1445 {
1446 ScreenCapture::captureScreen(&mCapture);
1447 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1448 // which begins at 64, 64
1449 mCapture->checkPixel(64, 64, 50, 50, 50);
1450 }
1451}
1452
chaviw13fdc492017-06-27 12:40:18 -07001453class LayerColorTest : public LayerUpdateTest {
1454 protected:
1455 void SetUp() override {
1456 LayerUpdateTest::SetUp();
1457
1458 mLayerColorControl = mComposerClient->createSurface(
1459 String8("Layer color surface"),
1460 128, 128, PIXEL_FORMAT_RGBA_8888,
1461 ISurfaceComposerClient::eFXSurfaceColor);
1462
1463 ASSERT_TRUE(mLayerColorControl != NULL);
1464 ASSERT_TRUE(mLayerColorControl->isValid());
1465
Robert Carr4cdc58f2017-08-23 14:22:20 -07001466 asTransaction([&](Transaction& t) {
1467 t.setLayer(mLayerColorControl, INT32_MAX-1);
1468 t.setPosition(mLayerColorControl, 140, 140);
1469 t.hide(mLayerColorControl);
1470 t.hide(mFGSurfaceControl);
1471 });
chaviw13fdc492017-06-27 12:40:18 -07001472 }
1473
1474 void TearDown() override {
1475 LayerUpdateTest::TearDown();
1476 mLayerColorControl = 0;
1477 }
1478
1479 sp<SurfaceControl> mLayerColorControl;
1480};
1481
1482TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1483 sp<ScreenCapture> sc;
1484
1485 {
1486 SCOPED_TRACE("before setColor");
1487 ScreenCapture::captureScreen(&sc);
1488 sc->expectBGColor(145, 145);
1489 }
1490
Robert Carr4cdc58f2017-08-23 14:22:20 -07001491 asTransaction([&](Transaction& t) {
1492 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1493 t.setColor(mLayerColorControl, color);
1494 t.show(mLayerColorControl);
1495 });
chaviw13fdc492017-06-27 12:40:18 -07001496
chaviw13fdc492017-06-27 12:40:18 -07001497 {
1498 // There should now be a color
1499 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001500
chaviw13fdc492017-06-27 12:40:18 -07001501 ScreenCapture::captureScreen(&sc);
1502 sc->checkPixel(145, 145, 43, 207, 131);
1503 }
1504}
1505
1506TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1507 sp<ScreenCapture> sc;
1508 {
1509 SCOPED_TRACE("before setColor");
1510 ScreenCapture::captureScreen(&sc);
1511 sc->expectBGColor(145, 145);
1512 }
1513
Robert Carr4cdc58f2017-08-23 14:22:20 -07001514 asTransaction([&](Transaction& t) {
1515 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1516 t.setColor(mLayerColorControl, color);
1517 t.setAlpha(mLayerColorControl, .75f);
1518 t.show(mLayerColorControl);
1519 });
1520
chaviw13fdc492017-06-27 12:40:18 -07001521 {
1522 // There should now be a color with .75 alpha
1523 SCOPED_TRACE("after setColor");
1524 ScreenCapture::captureScreen(&sc);
1525 sc->checkPixel(145, 145, 48, 171, 147);
1526 }
1527}
1528
1529TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1530 sp<ScreenCapture> sc;
1531 {
1532 SCOPED_TRACE("before setColor");
1533 ScreenCapture::captureScreen(&sc);
1534 sc->expectBGColor(145, 145);
1535 }
1536
Robert Carr4cdc58f2017-08-23 14:22:20 -07001537 asTransaction([&](Transaction& t) {
1538 t.show(mLayerColorControl);
1539 });
1540
chaviw13fdc492017-06-27 12:40:18 -07001541 {
1542 // There should now be set to 0,0,0 (black) as default.
1543 SCOPED_TRACE("after setColor");
1544 ScreenCapture::captureScreen(&sc);
1545 sc->checkPixel(145, 145, 0, 0, 0);
1546 }
1547}
1548
chaviwa76b2712017-09-20 12:02:26 -07001549class ScreenCaptureTest : public LayerUpdateTest {
1550protected:
1551 std::unique_ptr<CaptureLayer> mCapture;
1552};
1553
1554TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
1555 auto bgHandle = mBGSurfaceControl->getHandle();
1556 CaptureLayer::captureScreen(&mCapture, bgHandle);
1557 mCapture->expectBGColor(0, 0);
1558 // Doesn't capture FG layer which is at 64, 64
1559 mCapture->expectBGColor(64, 64);
1560}
1561
1562TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
1563 auto fgHandle = mFGSurfaceControl->getHandle();
1564
1565 sp<SurfaceControl> child = mComposerClient->createSurface(
1566 String8("Child surface"),
1567 10, 10, PIXEL_FORMAT_RGBA_8888,
1568 0, mFGSurfaceControl.get());
1569 fillSurfaceRGBA8(child, 200, 200, 200);
1570
1571 SurfaceComposerClient::Transaction()
1572 .show(child)
1573 .apply(true);
1574
1575 // Captures mFGSurfaceControl layer and its child.
1576 CaptureLayer::captureScreen(&mCapture, fgHandle);
1577 mCapture->expectFGColor(10, 10);
1578 mCapture->expectChildColor(0, 0);
1579}
1580
1581TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
1582 auto fgHandle = mFGSurfaceControl->getHandle();
1583
1584 sp<SurfaceControl> child = mComposerClient->createSurface(
1585 String8("Child surface"),
1586 10, 10, PIXEL_FORMAT_RGBA_8888,
1587 0, mFGSurfaceControl.get());
1588 fillSurfaceRGBA8(child, 200, 200, 200);
1589
1590 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1591 String8("Grandchild surface"), 5, 5,
1592 PIXEL_FORMAT_RGBA_8888, 0, child.get());
1593
1594 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1595 SurfaceComposerClient::Transaction()
1596 .show(child)
1597 .setPosition(grandchild, 5, 5)
1598 .show(grandchild)
1599 .apply(true);
1600
1601 // Captures mFGSurfaceControl, its child, and the grandchild.
1602 CaptureLayer::captureScreen(&mCapture, fgHandle);
1603 mCapture->expectFGColor(10, 10);
1604 mCapture->expectChildColor(0, 0);
1605 mCapture->checkPixel(5, 5, 50, 50, 50);
1606}
1607
1608TEST_F(ScreenCaptureTest, CaptureChildOnly) {
1609 sp<SurfaceControl> child = mComposerClient->createSurface(
1610 String8("Child surface"),
1611 10, 10, PIXEL_FORMAT_RGBA_8888,
1612 0, mFGSurfaceControl.get());
1613 fillSurfaceRGBA8(child, 200, 200, 200);
1614 auto childHandle = child->getHandle();
1615
1616 SurfaceComposerClient::Transaction()
1617 .setPosition(child, 5, 5)
1618 .show(child)
1619 .apply(true);
1620
1621 // Captures only the child layer, and not the parent.
1622 CaptureLayer::captureScreen(&mCapture, childHandle);
1623 mCapture->expectChildColor(0, 0);
1624 mCapture->expectChildColor(9, 9);
1625}
1626
1627TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
1628 sp<SurfaceControl> child = mComposerClient->createSurface(
1629 String8("Child surface"),
1630 10, 10, PIXEL_FORMAT_RGBA_8888,
1631 0, mFGSurfaceControl.get());
1632 fillSurfaceRGBA8(child, 200, 200, 200);
1633 auto childHandle = child->getHandle();
1634
1635 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1636 String8("Grandchild surface"), 5, 5,
1637 PIXEL_FORMAT_RGBA_8888, 0, child.get());
1638 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1639
1640 SurfaceComposerClient::Transaction()
1641 .show(child)
1642 .setPosition(grandchild, 5, 5)
1643 .show(grandchild)
1644 .apply(true);
1645
1646 auto grandchildHandle = grandchild->getHandle();
1647
1648 // Captures only the grandchild.
1649 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
1650 mCapture->checkPixel(0, 0, 50, 50, 50);
1651 mCapture->checkPixel(4, 4, 50, 50, 50);
1652}
1653
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001654}