blob: 16a16a53b1232f8080ed8165cc79c036ac7ccaec [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
261TEST_F(LayerUpdateTest, LayerMoveWorks) {
262 sp<ScreenCapture> sc;
263 {
264 SCOPED_TRACE("before move");
265 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800266 sc->expectBGColor(0, 12);
267 sc->expectFGColor(75, 75);
268 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700269 }
270
Robert Carr4cdc58f2017-08-23 14:22:20 -0700271 asTransaction([&](Transaction& t) {
272 t.setPosition(mFGSurfaceControl, 128, 128);
273 });
274
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700275 {
276 // This should reflect the new position, but not the new color.
277 SCOPED_TRACE("after move, before redraw");
278 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800279 sc->expectBGColor(24, 24);
280 sc->expectBGColor(75, 75);
281 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700282 }
283
284 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
285 waitForPostedBuffers();
286 {
287 // This should reflect the new position and the new color.
288 SCOPED_TRACE("after redraw");
289 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800290 sc->expectBGColor(24, 24);
291 sc->expectBGColor(75, 75);
292 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700293 }
294}
295
296TEST_F(LayerUpdateTest, LayerResizeWorks) {
297 sp<ScreenCapture> sc;
298 {
299 SCOPED_TRACE("before resize");
300 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800301 sc->expectBGColor(0, 12);
302 sc->expectFGColor(75, 75);
303 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700304 }
305
Steve Block9d453682011-12-20 16:23:08 +0000306 ALOGD("resizing");
Robert Carr4cdc58f2017-08-23 14:22:20 -0700307 asTransaction([&](Transaction& t) {
308 t.setSize(mFGSurfaceControl, 128, 128);
309 });
Steve Block9d453682011-12-20 16:23:08 +0000310 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700311 {
312 // This should not reflect the new size or color because SurfaceFlinger
313 // has not yet received a buffer of the correct size.
314 SCOPED_TRACE("after resize, before redraw");
315 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800316 sc->expectBGColor(0, 12);
317 sc->expectFGColor(75, 75);
318 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700319 }
320
Steve Block9d453682011-12-20 16:23:08 +0000321 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700322 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
323 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000324 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700325 {
326 // This should reflect the new size and the new color.
327 SCOPED_TRACE("after redraw");
328 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800329 sc->expectBGColor(24, 24);
330 sc->checkPixel(75, 75, 63, 195, 63);
331 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700332 }
333}
334
Haixia Shid5750962015-07-27 16:50:49 -0700335TEST_F(LayerUpdateTest, LayerCropWorks) {
336 sp<ScreenCapture> sc;
337 {
338 SCOPED_TRACE("before crop");
339 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800340 sc->expectBGColor(24, 24);
341 sc->expectFGColor(75, 75);
342 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700343 }
344
Robert Carr4cdc58f2017-08-23 14:22:20 -0700345 asTransaction([&](Transaction& t) {
346 Rect cropRect(16, 16, 32, 32);
347 t.setCrop(mFGSurfaceControl, cropRect);
348 });
Haixia Shid5750962015-07-27 16:50:49 -0700349 {
350 // This should crop the foreground surface.
351 SCOPED_TRACE("after crop");
352 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800353 sc->expectBGColor(24, 24);
354 sc->expectBGColor(75, 75);
355 sc->expectFGColor(95, 80);
356 sc->expectFGColor(80, 95);
357 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700358 }
359}
360
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000361TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
362 sp<ScreenCapture> sc;
363 {
364 SCOPED_TRACE("before crop");
365 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800366 sc->expectBGColor(24, 24);
367 sc->expectFGColor(75, 75);
368 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000369 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700370 asTransaction([&](Transaction& t) {
371 Rect cropRect(16, 16, 32, 32);
372 t.setFinalCrop(mFGSurfaceControl, cropRect);
373 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000374 {
375 // This should crop the foreground surface.
376 SCOPED_TRACE("after crop");
377 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800378 sc->expectBGColor(24, 24);
379 sc->expectBGColor(75, 75);
380 sc->expectBGColor(95, 80);
381 sc->expectBGColor(80, 95);
382 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000383 }
384}
385
Haixia Shid5750962015-07-27 16:50:49 -0700386TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
387 sp<ScreenCapture> sc;
388 {
389 SCOPED_TRACE("before setLayer");
390 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800391 sc->expectBGColor(24, 24);
392 sc->expectFGColor(75, 75);
393 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700394 }
395
Robert Carr4cdc58f2017-08-23 14:22:20 -0700396 asTransaction([&](Transaction& t) {
397 t.setLayer(mFGSurfaceControl, INT_MAX - 3);
398 });
399
Haixia Shid5750962015-07-27 16:50:49 -0700400 {
401 // This should hide the foreground surface beneath the background.
402 SCOPED_TRACE("after setLayer");
403 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800404 sc->expectBGColor(24, 24);
405 sc->expectBGColor(75, 75);
406 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700407 }
408}
409
410TEST_F(LayerUpdateTest, LayerShowHideWorks) {
411 sp<ScreenCapture> sc;
412 {
413 SCOPED_TRACE("before hide");
414 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800415 sc->expectBGColor(24, 24);
416 sc->expectFGColor(75, 75);
417 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700418 }
419
Robert Carr4cdc58f2017-08-23 14:22:20 -0700420 asTransaction([&](Transaction& t) {
421 t.hide(mFGSurfaceControl);
422 });
423
Haixia Shid5750962015-07-27 16:50:49 -0700424 {
425 // This should hide the foreground surface.
426 SCOPED_TRACE("after hide, before show");
427 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800428 sc->expectBGColor(24, 24);
429 sc->expectBGColor(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.show(mFGSurfaceControl);
435 });
436
Haixia Shid5750962015-07-27 16:50:49 -0700437 {
438 // This should show the foreground surface.
439 SCOPED_TRACE("after show");
440 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800441 sc->expectBGColor(24, 24);
442 sc->expectFGColor(75, 75);
443 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700444 }
445}
446
447TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
448 sp<ScreenCapture> sc;
449 {
450 SCOPED_TRACE("before setAlpha");
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.setAlpha(mFGSurfaceControl, 0.75f);
459 });
460
Haixia Shid5750962015-07-27 16:50:49 -0700461 {
462 // This should set foreground to be 75% opaque.
463 SCOPED_TRACE("after setAlpha");
464 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800465 sc->expectBGColor(24, 24);
466 sc->checkPixel(75, 75, 162, 63, 96);
467 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700468 }
469}
470
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700471TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
472 sp<ScreenCapture> sc;
473 {
474 SCOPED_TRACE("before setLayerStack");
475 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800476 sc->expectBGColor(24, 24);
477 sc->expectFGColor(75, 75);
478 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700479 }
480
Robert Carr4cdc58f2017-08-23 14:22:20 -0700481 asTransaction([&](Transaction& t) {
482 t.setLayerStack(mFGSurfaceControl, 1);
483 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700484 {
485 // This should hide the foreground surface since it goes to a different
486 // layer stack.
487 SCOPED_TRACE("after setLayerStack");
488 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800489 sc->expectBGColor(24, 24);
490 sc->expectBGColor(75, 75);
491 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700492 }
493}
494
495TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
496 sp<ScreenCapture> sc;
497 {
498 SCOPED_TRACE("before setFlags");
499 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800500 sc->expectBGColor(24, 24);
501 sc->expectFGColor(75, 75);
502 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700503 }
504
Robert Carr4cdc58f2017-08-23 14:22:20 -0700505 asTransaction([&](Transaction& t) {
506 t.setFlags(mFGSurfaceControl,
507 layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
508 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700509 {
510 // This should hide the foreground surface
511 SCOPED_TRACE("after setFlags");
512 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800513 sc->expectBGColor(24, 24);
514 sc->expectBGColor(75, 75);
515 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700516 }
517}
518
519TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
520 sp<ScreenCapture> sc;
521 {
522 SCOPED_TRACE("before setMatrix");
523 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800524 sc->expectBGColor(24, 24);
525 sc->expectFGColor(91, 96);
526 sc->expectFGColor(96, 101);
527 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700528 }
529
Robert Carr4cdc58f2017-08-23 14:22:20 -0700530 asTransaction([&](Transaction& t) {
531 t.setMatrix(mFGSurfaceControl,
532 M_SQRT1_2, M_SQRT1_2,
533 -M_SQRT1_2, M_SQRT1_2);
534 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700535 {
536 SCOPED_TRACE("after setMatrix");
537 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800538 sc->expectBGColor(24, 24);
539 sc->expectFGColor(91, 96);
540 sc->expectBGColor(96, 91);
541 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700542 }
543}
544
Robert Carr8d5227b2017-03-16 15:41:03 -0700545class GeometryLatchingTest : public LayerUpdateTest {
546protected:
547 void EXPECT_INITIAL_STATE(const char * trace) {
548 SCOPED_TRACE(trace);
549 ScreenCapture::captureScreen(&sc);
550 // We find the leading edge of the FG surface.
551 sc->expectFGColor(127, 127);
552 sc->expectBGColor(128, 128);
553 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700554
555 void lockAndFillFGBuffer() {
556 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
557 }
558
559 void unlockFGBuffer() {
560 sp<Surface> s = mFGSurfaceControl->getSurface();
561 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
562 waitForPostedBuffers();
563 }
564
Robert Carr8d5227b2017-03-16 15:41:03 -0700565 void completeFGResize() {
566 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
567 waitForPostedBuffers();
568 }
569 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700570 asTransaction([&](Transaction& t) {
571 t.setSize(mFGSurfaceControl, 64, 64);
572 t.setPosition(mFGSurfaceControl, 64, 64);
573 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
574 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
575 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700576
577 EXPECT_INITIAL_STATE("After restoring initial state");
578 }
579 sp<ScreenCapture> sc;
580};
581
582TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
583 EXPECT_INITIAL_STATE("before anything");
584
585 // By default position can be updated even while
586 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700587 asTransaction([&](Transaction& t) {
588 t.setSize(mFGSurfaceControl, 32, 32);
589 t.setPosition(mFGSurfaceControl, 100, 100);
590 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700591
592 {
593 SCOPED_TRACE("After moving surface");
594 ScreenCapture::captureScreen(&sc);
595 // If we moved, the FG Surface should cover up what was previously BG
596 // however if we didn't move the FG wouldn't be large enough now.
597 sc->expectFGColor(163, 163);
598 }
599
600 restoreInitialState();
601
602 // Now we repeat with setGeometryAppliesWithResize
603 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700604 asTransaction([&](Transaction& t) {
605 t.setGeometryAppliesWithResize(mFGSurfaceControl);
606 t.setSize(mFGSurfaceControl, 32, 32);
607 t.setPosition(mFGSurfaceControl, 100, 100);
608 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700609
610 {
611 SCOPED_TRACE("While resize is pending");
612 ScreenCapture::captureScreen(&sc);
613 // This time we shouldn't have moved, so the BG color
614 // should still be visible.
615 sc->expectBGColor(128, 128);
616 }
617
618 completeFGResize();
619
620 {
621 SCOPED_TRACE("After the resize");
622 ScreenCapture::captureScreen(&sc);
623 // But after the resize completes, we should move
624 // and the FG should be visible here.
625 sc->expectFGColor(128, 128);
626 }
627}
628
629class CropLatchingTest : public GeometryLatchingTest {
630protected:
631 void EXPECT_CROPPED_STATE(const char* trace) {
632 SCOPED_TRACE(trace);
633 ScreenCapture::captureScreen(&sc);
634 // The edge should be moved back one pixel by our crop.
635 sc->expectFGColor(126, 126);
636 sc->expectBGColor(127, 127);
637 sc->expectBGColor(128, 128);
638 }
chaviw59f5c562017-06-28 16:39:06 -0700639
640 void EXPECT_RESIZE_STATE(const char* trace) {
641 SCOPED_TRACE(trace);
642 ScreenCapture::captureScreen(&sc);
643 // The FG is now resized too 128,128 at 64,64
644 sc->expectFGColor(64, 64);
645 sc->expectFGColor(191, 191);
646 sc->expectBGColor(192, 192);
647 }
Robert Carr8d5227b2017-03-16 15:41:03 -0700648};
649
650TEST_F(CropLatchingTest, CropLatching) {
651 EXPECT_INITIAL_STATE("before anything");
652 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700653 asTransaction([&](Transaction& t) {
654 t.setSize(mFGSurfaceControl, 128, 128);
655 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
656 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700657
658 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
659
660 restoreInitialState();
661
Robert Carr4cdc58f2017-08-23 14:22:20 -0700662 asTransaction([&](Transaction& t) {
663 t.setSize(mFGSurfaceControl, 128, 128);
664 t.setGeometryAppliesWithResize(mFGSurfaceControl);
665 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
666 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700667
668 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
669
670 completeFGResize();
671
672 EXPECT_CROPPED_STATE("after the resize finishes");
673}
674
675TEST_F(CropLatchingTest, FinalCropLatching) {
676 EXPECT_INITIAL_STATE("before anything");
677 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700678 asTransaction([&](Transaction& t) {
679 t.setSize(mFGSurfaceControl, 128, 128);
680 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
681 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700682
683 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
684
685 restoreInitialState();
686
Robert Carr4cdc58f2017-08-23 14:22:20 -0700687 asTransaction([&](Transaction& t) {
688 t.setSize(mFGSurfaceControl, 128, 128);
689 t.setGeometryAppliesWithResize(mFGSurfaceControl);
690 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
691 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700692
693 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
694
695 completeFGResize();
696
697 EXPECT_CROPPED_STATE("after the resize finishes");
698}
699
Robert Carr7bf247e2017-05-18 14:02:49 -0700700// In this test we ensure that setGeometryAppliesWithResize actually demands
701// a buffer of the new size, and not just any size.
702TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
703 EXPECT_INITIAL_STATE("before anything");
704 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700705 asTransaction([&](Transaction& t) {
706 t.setSize(mFGSurfaceControl, 128, 128);
707 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
708 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700709
710 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
711
712 restoreInitialState();
713
714 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
715 // initiating the resize.
716 lockAndFillFGBuffer();
717
Robert Carr4cdc58f2017-08-23 14:22:20 -0700718 asTransaction([&](Transaction& t) {
719 t.setSize(mFGSurfaceControl, 128, 128);
720 t.setGeometryAppliesWithResize(mFGSurfaceControl);
721 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
722 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700723
724 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
725
726 // We now submit our old buffer, at the old size, and ensure it doesn't
727 // trigger geometry latching.
728 unlockFGBuffer();
729
730 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
731
732 completeFGResize();
733
734 EXPECT_CROPPED_STATE("after the resize finishes");
735}
736
737TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
738 EXPECT_INITIAL_STATE("before anything");
739 // In this scenario, we attempt to set the final crop a second time while the resize
740 // is still pending, and ensure we are successful. Success meaning the second crop
741 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700742 asTransaction([&](Transaction& t) {
743 t.setSize(mFGSurfaceControl, 128, 128);
744 t.setGeometryAppliesWithResize(mFGSurfaceControl);
745 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
746 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700747
chaviw59f5c562017-06-28 16:39:06 -0700748 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
749
Robert Carr4cdc58f2017-08-23 14:22:20 -0700750 asTransaction([&](Transaction& t) {
751 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
752 });
Robert Carr7bf247e2017-05-18 14:02:49 -0700753
chaviw59f5c562017-06-28 16:39:06 -0700754 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -0700755
756 completeFGResize();
757
chaviw59f5c562017-06-28 16:39:06 -0700758 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -0700759}
760
Pablo Ceballos05289c22016-04-14 15:49:55 -0700761TEST_F(LayerUpdateTest, DeferredTransactionTest) {
762 sp<ScreenCapture> sc;
763 {
764 SCOPED_TRACE("before anything");
765 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800766 sc->expectBGColor(32, 32);
767 sc->expectFGColor(96, 96);
768 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700769 }
770
771 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -0700772 asTransaction([&](Transaction& t) {
773 t.setAlpha(mFGSurfaceControl, 0.75);
774 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
775 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
776 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700777
Robert Carr4cdc58f2017-08-23 14:22:20 -0700778 asTransaction([&](Transaction& t) {
779 t.setPosition(mFGSurfaceControl, 128,128);
780 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
781 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
782 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700783
784 {
785 SCOPED_TRACE("before any trigger");
786 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800787 sc->expectBGColor(32, 32);
788 sc->expectFGColor(96, 96);
789 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700790 }
791
792 // should trigger the first deferred transaction, but not the second one
793 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
794 {
795 SCOPED_TRACE("after first trigger");
796 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800797 sc->expectBGColor(32, 32);
798 sc->checkPixel(96, 96, 162, 63, 96);
799 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700800 }
801
802 // should show up immediately since it's not deferred
Robert Carr4cdc58f2017-08-23 14:22:20 -0700803 asTransaction([&](Transaction& t) {
804 t.setAlpha(mFGSurfaceControl, 1.0);
805 });
Pablo Ceballos05289c22016-04-14 15:49:55 -0700806
807 // trigger the second deferred transaction
808 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
809 {
810 SCOPED_TRACE("after second trigger");
811 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800812 sc->expectBGColor(32, 32);
813 sc->expectBGColor(96, 96);
814 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700815 }
816}
817
Robert Carrdb66e622017-04-10 16:55:57 -0700818TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
819 sp<ScreenCapture> sc;
820 {
821 SCOPED_TRACE("before adding relative surface");
822 ScreenCapture::captureScreen(&sc);
823 sc->expectBGColor(24, 24);
824 sc->expectFGColor(75, 75);
825 sc->expectBGColor(145, 145);
826 }
827
828 auto relativeSurfaceControl = mComposerClient->createSurface(
829 String8("Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
830 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
831 waitForPostedBuffers();
832
833 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700834 asTransaction([&](Transaction& t) {
835 t.setPosition(relativeSurfaceControl, 64, 64);
836 t.show(relativeSurfaceControl);
837 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
838 });
Robert Carrdb66e622017-04-10 16:55:57 -0700839
840 {
841 SCOPED_TRACE("after adding relative surface");
842 ScreenCapture::captureScreen(&sc);
843 // our relative surface should be visible now.
844 sc->checkPixel(75, 75, 255, 177, 177);
845 }
846
847 // A call to setLayer will override a call to setRelativeLayer
Robert Carr4cdc58f2017-08-23 14:22:20 -0700848 asTransaction([&](Transaction& t) {
849 t.setLayer(relativeSurfaceControl, 0);
850 });
Robert Carrdb66e622017-04-10 16:55:57 -0700851
852 {
853 SCOPED_TRACE("after set layer");
854 ScreenCapture::captureScreen(&sc);
855 // now the FG surface should be visible again.
856 sc->expectFGColor(75, 75);
857 }
858}
859
Robert Carre392b552017-09-19 12:16:05 -0700860TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
861 sp<ScreenCapture> sc;
862
863 sp<SurfaceControl> childNoBuffer =
864 mComposerClient->createSurface(String8("Bufferless child"),
865 10, 10, PIXEL_FORMAT_RGBA_8888,
866 0, mFGSurfaceControl.get());
867 sp<SurfaceControl> childBuffer = mComposerClient->createSurface(
868 String8("Buffered child"), 20, 20,
869 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
870 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
871
Robert Carr4cdc58f2017-08-23 14:22:20 -0700872 SurfaceComposerClient::Transaction{}
873 .show(childNoBuffer)
874 .show(childBuffer)
875 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700876
877 {
878 ScreenCapture::captureScreen(&sc);
879 sc->expectChildColor(73, 73);
880 sc->expectFGColor(74, 74);
881 }
882
Robert Carr4cdc58f2017-08-23 14:22:20 -0700883 SurfaceComposerClient::Transaction{}
884 .setSize(childNoBuffer, 20, 20)
885 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -0700886
887 {
888 ScreenCapture::captureScreen(&sc);
889 sc->expectChildColor(73, 73);
890 sc->expectChildColor(74, 74);
891 }
892}
893
Robert Carr2c5f6d22017-09-26 12:30:35 -0700894TEST_F(LayerUpdateTest, MergingTransactions) {
895 sp<ScreenCapture> sc;
896 {
897 SCOPED_TRACE("before move");
898 ScreenCapture::captureScreen(&sc);
899 sc->expectBGColor(0, 12);
900 sc->expectFGColor(75, 75);
901 sc->expectBGColor(145, 145);
902 }
903
904 Transaction t1, t2;
905 t1.setPosition(mFGSurfaceControl, 128, 128);
906 t2.setPosition(mFGSurfaceControl, 0, 0);
907 // We expect that the position update from t2 now
908 // overwrites the position update from t1.
909 t1.merge(std::move(t2));
910 t1.apply();
911
912 {
913 ScreenCapture::captureScreen(&sc);
914 sc->expectFGColor(1, 1);
915 }
916}
917
Robert Carr1f0a16a2016-10-24 16:27:39 -0700918class ChildLayerTest : public LayerUpdateTest {
919protected:
920 void SetUp() override {
921 LayerUpdateTest::SetUp();
922 mChild = mComposerClient->createSurface(
923 String8("Child surface"),
924 10, 10, PIXEL_FORMAT_RGBA_8888,
925 0, mFGSurfaceControl.get());
926 fillSurfaceRGBA8(mChild, 200, 200, 200);
927
928 {
929 SCOPED_TRACE("before anything");
930 ScreenCapture::captureScreen(&mCapture);
931 mCapture->expectChildColor(64, 64);
932 }
933 }
934 void TearDown() override {
935 LayerUpdateTest::TearDown();
936 mChild = 0;
937 }
938
939 sp<SurfaceControl> mChild;
940 sp<ScreenCapture> mCapture;
941};
942
943TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700944 asTransaction([&](Transaction& t) {
945 t.show(mChild);
946 t.setPosition(mChild, 10, 10);
947 t.setPosition(mFGSurfaceControl, 64, 64);
948 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700949
950 {
951 ScreenCapture::captureScreen(&mCapture);
952 // Top left of foreground must now be visible
953 mCapture->expectFGColor(64, 64);
954 // But 10 pixels in we should see the child surface
955 mCapture->expectChildColor(74, 74);
956 // And 10 more pixels we should be back to the foreground surface
957 mCapture->expectFGColor(84, 84);
958 }
959
Robert Carr4cdc58f2017-08-23 14:22:20 -0700960 asTransaction([&](Transaction& t) {
961 t.setPosition(mFGSurfaceControl, 0, 0);
962 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700963
964 {
965 ScreenCapture::captureScreen(&mCapture);
966 // Top left of foreground should now be at 0, 0
967 mCapture->expectFGColor(0, 0);
968 // But 10 pixels in we should see the child surface
969 mCapture->expectChildColor(10, 10);
970 // And 10 more pixels we should be back to the foreground surface
971 mCapture->expectFGColor(20, 20);
972 }
973}
974
Robert Carr41b08b52017-06-01 16:11:34 -0700975TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700976 asTransaction([&](Transaction& t) {
977 t.show(mChild);
978 t.setPosition(mChild, 0, 0);
979 t.setPosition(mFGSurfaceControl, 0, 0);
980 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
981 });
Robert Carr41b08b52017-06-01 16:11:34 -0700982
983 {
984 ScreenCapture::captureScreen(&mCapture);
985 mCapture->expectChildColor(0, 0);
986 mCapture->expectChildColor(4, 4);
987 mCapture->expectBGColor(5, 5);
988 }
989}
990
991TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700992 asTransaction([&](Transaction& t) {
993 t.show(mChild);
994 t.setPosition(mChild, 0, 0);
995 t.setPosition(mFGSurfaceControl, 0, 0);
996 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
997 });
Robert Carr41b08b52017-06-01 16:11:34 -0700998
999 {
1000 ScreenCapture::captureScreen(&mCapture);
1001 mCapture->expectChildColor(0, 0);
1002 mCapture->expectChildColor(4, 4);
1003 mCapture->expectBGColor(5, 5);
1004 }
1005}
1006
Robert Carr1f0a16a2016-10-24 16:27:39 -07001007TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001008 asTransaction([&](Transaction& t) {
1009 t.show(mChild);
1010 t.setPosition(mFGSurfaceControl, 0, 0);
1011 t.setPosition(mChild, 63, 63);
1012 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001013
1014 {
1015 ScreenCapture::captureScreen(&mCapture);
1016 mCapture->expectFGColor(0, 0);
1017 // Last pixel in foreground should now be the child.
1018 mCapture->expectChildColor(63, 63);
1019 // But the child should be constrained and the next pixel
1020 // must be the background
1021 mCapture->expectBGColor(64, 64);
1022 }
1023}
1024
1025TEST_F(ChildLayerTest, ChildLayerScaling) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001026 asTransaction([&](Transaction& t) {
1027 t.setPosition(mFGSurfaceControl, 0, 0);
1028 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001029
1030 // Find the boundary between the parent and child
1031 {
1032 ScreenCapture::captureScreen(&mCapture);
1033 mCapture->expectChildColor(9, 9);
1034 mCapture->expectFGColor(10, 10);
1035 }
1036
Robert Carr4cdc58f2017-08-23 14:22:20 -07001037 asTransaction([&](Transaction& t) {
1038 t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0);
1039 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001040
1041 // The boundary should be twice as far from the origin now.
1042 // The pixels from the last test should all be child now
1043 {
1044 ScreenCapture::captureScreen(&mCapture);
1045 mCapture->expectChildColor(9, 9);
1046 mCapture->expectChildColor(10, 10);
1047 mCapture->expectChildColor(19, 19);
1048 mCapture->expectFGColor(20, 20);
1049 }
1050}
Robert Carr9524cb32017-02-13 11:32:32 -08001051
Robert Carr6452f122017-03-21 10:41:29 -07001052TEST_F(ChildLayerTest, ChildLayerAlpha) {
1053 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1054 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1055 fillSurfaceRGBA8(mChild, 0, 254, 0);
1056 waitForPostedBuffers();
1057
Robert Carr4cdc58f2017-08-23 14:22:20 -07001058 asTransaction([&](Transaction& t) {
1059 t.show(mChild);
1060 t.setPosition(mChild, 0, 0);
1061 t.setPosition(mFGSurfaceControl, 0, 0);
1062 });
Robert Carr6452f122017-03-21 10:41:29 -07001063
1064 {
1065 ScreenCapture::captureScreen(&mCapture);
1066 // Unblended child color
1067 mCapture->checkPixel(0, 0, 0, 254, 0);
1068 }
1069
Robert Carr4cdc58f2017-08-23 14:22:20 -07001070 asTransaction([&](Transaction& t) {
1071 t.setAlpha(mChild, 0.5);
1072 });
Robert Carr6452f122017-03-21 10:41:29 -07001073
1074 {
1075 ScreenCapture::captureScreen(&mCapture);
1076 // Child and BG blended.
1077 mCapture->checkPixel(0, 0, 127, 127, 0);
1078 }
1079
Robert Carr4cdc58f2017-08-23 14:22:20 -07001080 asTransaction([&](Transaction& t) {
1081 t.setAlpha(mFGSurfaceControl, 0.5);
1082 });
Robert Carr6452f122017-03-21 10:41:29 -07001083
1084 {
1085 ScreenCapture::captureScreen(&mCapture);
1086 // Child and BG blended.
1087 mCapture->checkPixel(0, 0, 95, 64, 95);
1088 }
1089}
1090
Robert Carr9524cb32017-02-13 11:32:32 -08001091TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001092 asTransaction([&](Transaction& t) {
1093 t.show(mChild);
1094 t.setPosition(mChild, 10, 10);
1095 t.setPosition(mFGSurfaceControl, 64, 64);
1096 });
Robert Carr9524cb32017-02-13 11:32:32 -08001097
1098 {
1099 ScreenCapture::captureScreen(&mCapture);
1100 // Top left of foreground must now be visible
1101 mCapture->expectFGColor(64, 64);
1102 // But 10 pixels in we should see the child surface
1103 mCapture->expectChildColor(74, 74);
1104 // And 10 more pixels we should be back to the foreground surface
1105 mCapture->expectFGColor(84, 84);
1106 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001107
1108 asTransaction([&](Transaction& t) {
1109 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1110 });
1111
Robert Carr9524cb32017-02-13 11:32:32 -08001112 {
1113 ScreenCapture::captureScreen(&mCapture);
1114 mCapture->expectFGColor(64, 64);
1115 // In reparenting we should have exposed the entire foreground surface.
1116 mCapture->expectFGColor(74, 74);
1117 // And the child layer should now begin at 10, 10 (since the BG
1118 // layer is at (0, 0)).
1119 mCapture->expectBGColor(9, 9);
1120 mCapture->expectChildColor(10, 10);
1121 }
1122}
1123
chaviw161410b02017-07-27 10:46:08 -07001124TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001125 asTransaction([&](Transaction& t) {
1126 t.show(mChild);
1127 t.setPosition(mChild, 10, 10);
1128 t.setPosition(mFGSurfaceControl, 64, 64);
1129 });
Robert Carr9524cb32017-02-13 11:32:32 -08001130
1131 {
1132 ScreenCapture::captureScreen(&mCapture);
1133 // Top left of foreground must now be visible
1134 mCapture->expectFGColor(64, 64);
1135 // But 10 pixels in we should see the child surface
1136 mCapture->expectChildColor(74, 74);
1137 // And 10 more pixels we should be back to the foreground surface
1138 mCapture->expectFGColor(84, 84);
1139 }
1140
Robert Carr4cdc58f2017-08-23 14:22:20 -07001141 asTransaction([&](Transaction& t) {
1142 t.detachChildren(mFGSurfaceControl);
1143 });
Robert Carr9524cb32017-02-13 11:32:32 -08001144
Robert Carr4cdc58f2017-08-23 14:22:20 -07001145 asTransaction([&](Transaction& t) {
1146 t.hide(mChild);
1147 });
Robert Carr9524cb32017-02-13 11:32:32 -08001148
chaviw161410b02017-07-27 10:46:08 -07001149 // Since the child has the same client as the parent, it will not get
1150 // detached and will be hidden.
1151 {
1152 ScreenCapture::captureScreen(&mCapture);
1153 mCapture->expectFGColor(64, 64);
1154 mCapture->expectFGColor(74, 74);
1155 mCapture->expectFGColor(84, 84);
1156 }
1157}
1158
1159TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1160 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
1161 sp<SurfaceControl> mChildNewClient = mNewComposerClient->createSurface(
1162 String8("New Child Test Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1163 0, mFGSurfaceControl.get());
1164
1165 ASSERT_TRUE(mChildNewClient != NULL);
1166 ASSERT_TRUE(mChildNewClient->isValid());
1167
1168 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1169
Robert Carr4cdc58f2017-08-23 14:22:20 -07001170 asTransaction([&](Transaction& t) {
1171 t.hide(mChild);
1172 t.show(mChildNewClient);
1173 t.setPosition(mChildNewClient, 10, 10);
1174 t.setPosition(mFGSurfaceControl, 64, 64);
1175 });
chaviw161410b02017-07-27 10:46:08 -07001176
1177 {
1178 ScreenCapture::captureScreen(&mCapture);
1179 // Top left of foreground must now be visible
1180 mCapture->expectFGColor(64, 64);
1181 // But 10 pixels in we should see the child surface
1182 mCapture->expectChildColor(74, 74);
1183 // And 10 more pixels we should be back to the foreground surface
1184 mCapture->expectFGColor(84, 84);
1185 }
1186
Robert Carr4cdc58f2017-08-23 14:22:20 -07001187 asTransaction([&](Transaction& t) {
1188 t.detachChildren(mFGSurfaceControl);
1189 });
chaviw161410b02017-07-27 10:46:08 -07001190
Robert Carr4cdc58f2017-08-23 14:22:20 -07001191 asTransaction([&](Transaction& t) {
1192 t.hide(mChildNewClient);
1193 });
chaviw161410b02017-07-27 10:46:08 -07001194
Robert Carr9524cb32017-02-13 11:32:32 -08001195 // Nothing should have changed.
1196 {
1197 ScreenCapture::captureScreen(&mCapture);
1198 mCapture->expectFGColor(64, 64);
1199 mCapture->expectChildColor(74, 74);
1200 mCapture->expectFGColor(84, 84);
1201 }
1202}
1203
Robert Carr9b429f42017-04-17 14:56:57 -07001204TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001205 asTransaction([&](Transaction& t) {
1206 t.show(mChild);
1207 t.setPosition(mChild, 0, 0);
1208 t.setPosition(mFGSurfaceControl, 0, 0);
1209 });
Robert Carr9b429f42017-04-17 14:56:57 -07001210
1211 {
1212 ScreenCapture::captureScreen(&mCapture);
1213 // We've positioned the child in the top left.
1214 mCapture->expectChildColor(0, 0);
1215 // But it's only 10x10.
1216 mCapture->expectFGColor(10, 10);
1217 }
1218
Robert Carr4cdc58f2017-08-23 14:22:20 -07001219 asTransaction([&](Transaction& t) {
1220 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1221 // We cause scaling by 2.
1222 t.setSize(mFGSurfaceControl, 128, 128);
1223 });
Robert Carr9b429f42017-04-17 14:56:57 -07001224
1225 {
1226 ScreenCapture::captureScreen(&mCapture);
1227 // We've positioned the child in the top left.
1228 mCapture->expectChildColor(0, 0);
1229 mCapture->expectChildColor(10, 10);
1230 mCapture->expectChildColor(19, 19);
1231 // And now it should be scaled all the way to 20x20
1232 mCapture->expectFGColor(20, 20);
1233 }
1234}
1235
Robert Carr1725eee2017-04-26 18:32:15 -07001236// Regression test for b/37673612
1237TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001238 asTransaction([&](Transaction& t) {
1239 t.show(mChild);
1240 t.setPosition(mChild, 0, 0);
1241 t.setPosition(mFGSurfaceControl, 0, 0);
1242 });
Robert Carr1725eee2017-04-26 18:32:15 -07001243
1244 {
1245 ScreenCapture::captureScreen(&mCapture);
1246 // We've positioned the child in the top left.
1247 mCapture->expectChildColor(0, 0);
1248 // But it's only 10x10.
1249 mCapture->expectFGColor(10, 10);
1250 }
Robert Carr1725eee2017-04-26 18:32:15 -07001251 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1252 // the WM specified state size.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001253 asTransaction([&](Transaction& t) {
1254 t.setSize(mFGSurfaceControl, 128, 64);
1255 });
Robert Carr1725eee2017-04-26 18:32:15 -07001256 sp<Surface> s = mFGSurfaceControl->getSurface();
1257 auto anw = static_cast<ANativeWindow*>(s.get());
1258 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1259 native_window_set_buffers_dimensions(anw, 64, 128);
1260 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1261 waitForPostedBuffers();
1262
1263 {
1264 // The child should still be in the same place and not have any strange scaling as in
1265 // b/37673612.
1266 ScreenCapture::captureScreen(&mCapture);
1267 mCapture->expectChildColor(0, 0);
1268 mCapture->expectFGColor(10, 10);
1269 }
1270}
1271
Dan Stoza412903f2017-04-27 13:42:17 -07001272TEST_F(ChildLayerTest, Bug36858924) {
1273 // Destroy the child layer
1274 mChild.clear();
1275
1276 // Now recreate it as hidden
1277 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1278 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1279 mFGSurfaceControl.get());
1280
1281 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001282 asTransaction([&](Transaction& t) {
1283 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
1284 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1285 t.show(mChild);
1286 });
Dan Stoza412903f2017-04-27 13:42:17 -07001287
1288 // Render the foreground surface a few times
1289 //
1290 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1291 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1292 // never acquire/release the first buffer
1293 ALOGI("Filling 1");
1294 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1295 ALOGI("Filling 2");
1296 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1297 ALOGI("Filling 3");
1298 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1299 ALOGI("Filling 4");
1300 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1301}
1302
chaviwf1961f72017-09-18 16:41:07 -07001303TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001304 asTransaction([&](Transaction& t) {
1305 t.show(mChild);
1306 t.setPosition(mChild, 10, 10);
1307 t.setPosition(mFGSurfaceControl, 64, 64);
1308 });
chaviw06178942017-07-27 10:25:59 -07001309
1310 {
1311 ScreenCapture::captureScreen(&mCapture);
1312 // Top left of foreground must now be visible
1313 mCapture->expectFGColor(64, 64);
1314 // But 10 pixels in we should see the child surface
1315 mCapture->expectChildColor(74, 74);
1316 // And 10 more pixels we should be back to the foreground surface
1317 mCapture->expectFGColor(84, 84);
1318 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001319
1320 asTransaction([&](Transaction& t) {
1321 t.reparent(mChild, mBGSurfaceControl->getHandle());
1322 });
1323
chaviw06178942017-07-27 10:25:59 -07001324 {
1325 ScreenCapture::captureScreen(&mCapture);
1326 mCapture->expectFGColor(64, 64);
1327 // In reparenting we should have exposed the entire foreground surface.
1328 mCapture->expectFGColor(74, 74);
1329 // And the child layer should now begin at 10, 10 (since the BG
1330 // layer is at (0, 0)).
1331 mCapture->expectBGColor(9, 9);
1332 mCapture->expectChildColor(10, 10);
1333 }
1334}
1335
chaviwf1961f72017-09-18 16:41:07 -07001336TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001337 asTransaction([&](Transaction& t) {
1338 t.show(mChild);
1339 t.setPosition(mChild, 10, 10);
1340 t.setPosition(mFGSurfaceControl, 64, 64);
1341 });
chaviwf1961f72017-09-18 16:41:07 -07001342
1343 {
1344 ScreenCapture::captureScreen(&mCapture);
1345 // Top left of foreground must now be visible
1346 mCapture->expectFGColor(64, 64);
1347 // But 10 pixels in we should see the child surface
1348 mCapture->expectChildColor(74, 74);
1349 // And 10 more pixels we should be back to the foreground surface
1350 mCapture->expectFGColor(84, 84);
1351 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001352 asTransaction([&](Transaction& t) {
1353 t.reparent(mChild, nullptr);
1354 });
chaviwf1961f72017-09-18 16:41:07 -07001355 {
1356 ScreenCapture::captureScreen(&mCapture);
1357 // Nothing should have changed.
1358 mCapture->expectFGColor(64, 64);
1359 mCapture->expectChildColor(74, 74);
1360 mCapture->expectFGColor(84, 84);
1361 }
1362}
1363
1364TEST_F(ChildLayerTest, ReparentFromNoParent) {
1365 sp<SurfaceControl> newSurface = mComposerClient->createSurface(
1366 String8("New Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, 0);
1367 ASSERT_TRUE(newSurface != NULL);
1368 ASSERT_TRUE(newSurface->isValid());
1369
1370 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001371 asTransaction([&](Transaction& t) {
1372 t.hide(mChild);
1373 t.show(newSurface);
1374 t.setPosition(newSurface, 10, 10);
1375 t.setLayer(newSurface, INT32_MAX-2);
1376 t.setPosition(mFGSurfaceControl, 64, 64);
1377 });
chaviwf1961f72017-09-18 16:41:07 -07001378
1379 {
1380 ScreenCapture::captureScreen(&mCapture);
1381 // Top left of foreground must now be visible
1382 mCapture->expectFGColor(64, 64);
1383 // At 10, 10 we should see the new surface
1384 mCapture->checkPixel(10, 10, 63, 195, 63);
1385 }
1386
Robert Carr4cdc58f2017-08-23 14:22:20 -07001387 asTransaction([&](Transaction& t) {
1388 t.reparent(newSurface, mFGSurfaceControl->getHandle());
1389 });
chaviwf1961f72017-09-18 16:41:07 -07001390
1391 {
1392 ScreenCapture::captureScreen(&mCapture);
1393 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1394 // mFGSurface, putting it at 74, 74.
1395 mCapture->expectFGColor(64, 64);
1396 mCapture->checkPixel(74, 74, 63, 195, 63);
1397 mCapture->expectFGColor(84, 84);
1398 }
1399}
1400
chaviwc9674332017-08-28 12:32:18 -07001401TEST_F(ChildLayerTest, NestedChildren) {
1402 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1403 String8("Grandchild surface"),
1404 10, 10, PIXEL_FORMAT_RGBA_8888,
1405 0, mChild.get());
1406 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1407
1408 {
1409 ScreenCapture::captureScreen(&mCapture);
1410 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1411 // which begins at 64, 64
1412 mCapture->checkPixel(64, 64, 50, 50, 50);
1413 }
1414}
1415
chaviw13fdc492017-06-27 12:40:18 -07001416class LayerColorTest : public LayerUpdateTest {
1417 protected:
1418 void SetUp() override {
1419 LayerUpdateTest::SetUp();
1420
1421 mLayerColorControl = mComposerClient->createSurface(
1422 String8("Layer color surface"),
1423 128, 128, PIXEL_FORMAT_RGBA_8888,
1424 ISurfaceComposerClient::eFXSurfaceColor);
1425
1426 ASSERT_TRUE(mLayerColorControl != NULL);
1427 ASSERT_TRUE(mLayerColorControl->isValid());
1428
Robert Carr4cdc58f2017-08-23 14:22:20 -07001429 asTransaction([&](Transaction& t) {
1430 t.setLayer(mLayerColorControl, INT32_MAX-1);
1431 t.setPosition(mLayerColorControl, 140, 140);
1432 t.hide(mLayerColorControl);
1433 t.hide(mFGSurfaceControl);
1434 });
chaviw13fdc492017-06-27 12:40:18 -07001435 }
1436
1437 void TearDown() override {
1438 LayerUpdateTest::TearDown();
1439 mLayerColorControl = 0;
1440 }
1441
1442 sp<SurfaceControl> mLayerColorControl;
1443};
1444
1445TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1446 sp<ScreenCapture> sc;
1447
1448 {
1449 SCOPED_TRACE("before setColor");
1450 ScreenCapture::captureScreen(&sc);
1451 sc->expectBGColor(145, 145);
1452 }
1453
Robert Carr4cdc58f2017-08-23 14:22:20 -07001454 asTransaction([&](Transaction& t) {
1455 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1456 t.setColor(mLayerColorControl, color);
1457 t.show(mLayerColorControl);
1458 });
chaviw13fdc492017-06-27 12:40:18 -07001459
chaviw13fdc492017-06-27 12:40:18 -07001460 {
1461 // There should now be a color
1462 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001463
chaviw13fdc492017-06-27 12:40:18 -07001464 ScreenCapture::captureScreen(&sc);
1465 sc->checkPixel(145, 145, 43, 207, 131);
1466 }
1467}
1468
1469TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1470 sp<ScreenCapture> sc;
1471 {
1472 SCOPED_TRACE("before setColor");
1473 ScreenCapture::captureScreen(&sc);
1474 sc->expectBGColor(145, 145);
1475 }
1476
Robert Carr4cdc58f2017-08-23 14:22:20 -07001477 asTransaction([&](Transaction& t) {
1478 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1479 t.setColor(mLayerColorControl, color);
1480 t.setAlpha(mLayerColorControl, .75f);
1481 t.show(mLayerColorControl);
1482 });
1483
chaviw13fdc492017-06-27 12:40:18 -07001484 {
1485 // There should now be a color with .75 alpha
1486 SCOPED_TRACE("after setColor");
1487 ScreenCapture::captureScreen(&sc);
1488 sc->checkPixel(145, 145, 48, 171, 147);
1489 }
1490}
1491
1492TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1493 sp<ScreenCapture> sc;
1494 {
1495 SCOPED_TRACE("before setColor");
1496 ScreenCapture::captureScreen(&sc);
1497 sc->expectBGColor(145, 145);
1498 }
1499
Robert Carr4cdc58f2017-08-23 14:22:20 -07001500 asTransaction([&](Transaction& t) {
1501 t.show(mLayerColorControl);
1502 });
1503
chaviw13fdc492017-06-27 12:40:18 -07001504 {
1505 // There should now be set to 0,0,0 (black) as default.
1506 SCOPED_TRACE("after setColor");
1507 ScreenCapture::captureScreen(&sc);
1508 sc->checkPixel(145, 145, 0, 0, 0);
1509 }
1510}
1511
chaviwa76b2712017-09-20 12:02:26 -07001512class ScreenCaptureTest : public LayerUpdateTest {
1513protected:
1514 std::unique_ptr<CaptureLayer> mCapture;
1515};
1516
1517TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
1518 auto bgHandle = mBGSurfaceControl->getHandle();
1519 CaptureLayer::captureScreen(&mCapture, bgHandle);
1520 mCapture->expectBGColor(0, 0);
1521 // Doesn't capture FG layer which is at 64, 64
1522 mCapture->expectBGColor(64, 64);
1523}
1524
1525TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
1526 auto fgHandle = mFGSurfaceControl->getHandle();
1527
1528 sp<SurfaceControl> child = mComposerClient->createSurface(
1529 String8("Child surface"),
1530 10, 10, PIXEL_FORMAT_RGBA_8888,
1531 0, mFGSurfaceControl.get());
1532 fillSurfaceRGBA8(child, 200, 200, 200);
1533
1534 SurfaceComposerClient::Transaction()
1535 .show(child)
1536 .apply(true);
1537
1538 // Captures mFGSurfaceControl layer and its child.
1539 CaptureLayer::captureScreen(&mCapture, fgHandle);
1540 mCapture->expectFGColor(10, 10);
1541 mCapture->expectChildColor(0, 0);
1542}
1543
1544TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
1545 auto fgHandle = mFGSurfaceControl->getHandle();
1546
1547 sp<SurfaceControl> child = mComposerClient->createSurface(
1548 String8("Child surface"),
1549 10, 10, PIXEL_FORMAT_RGBA_8888,
1550 0, mFGSurfaceControl.get());
1551 fillSurfaceRGBA8(child, 200, 200, 200);
1552
1553 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1554 String8("Grandchild surface"), 5, 5,
1555 PIXEL_FORMAT_RGBA_8888, 0, child.get());
1556
1557 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1558 SurfaceComposerClient::Transaction()
1559 .show(child)
1560 .setPosition(grandchild, 5, 5)
1561 .show(grandchild)
1562 .apply(true);
1563
1564 // Captures mFGSurfaceControl, its child, and the grandchild.
1565 CaptureLayer::captureScreen(&mCapture, fgHandle);
1566 mCapture->expectFGColor(10, 10);
1567 mCapture->expectChildColor(0, 0);
1568 mCapture->checkPixel(5, 5, 50, 50, 50);
1569}
1570
1571TEST_F(ScreenCaptureTest, CaptureChildOnly) {
1572 sp<SurfaceControl> child = mComposerClient->createSurface(
1573 String8("Child surface"),
1574 10, 10, PIXEL_FORMAT_RGBA_8888,
1575 0, mFGSurfaceControl.get());
1576 fillSurfaceRGBA8(child, 200, 200, 200);
1577 auto childHandle = child->getHandle();
1578
1579 SurfaceComposerClient::Transaction()
1580 .setPosition(child, 5, 5)
1581 .show(child)
1582 .apply(true);
1583
1584 // Captures only the child layer, and not the parent.
1585 CaptureLayer::captureScreen(&mCapture, childHandle);
1586 mCapture->expectChildColor(0, 0);
1587 mCapture->expectChildColor(9, 9);
1588}
1589
1590TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
1591 sp<SurfaceControl> child = mComposerClient->createSurface(
1592 String8("Child surface"),
1593 10, 10, PIXEL_FORMAT_RGBA_8888,
1594 0, mFGSurfaceControl.get());
1595 fillSurfaceRGBA8(child, 200, 200, 200);
1596 auto childHandle = child->getHandle();
1597
1598 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1599 String8("Grandchild surface"), 5, 5,
1600 PIXEL_FORMAT_RGBA_8888, 0, child.get());
1601 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1602
1603 SurfaceComposerClient::Transaction()
1604 .show(child)
1605 .setPosition(grandchild, 5, 5)
1606 .show(grandchild)
1607 .apply(true);
1608
1609 auto grandchildHandle = grandchild->getHandle();
1610
1611 // Captures only the grandchild.
1612 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
1613 mCapture->checkPixel(0, 0, 50, 50, 50);
1614 mCapture->checkPixel(4, 4, 50, 50, 50);
1615}
1616
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001617}