blob: ec87eeec777c5b674d4a3de0824d03911a3154f6 [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 Carr1f0a16a2016-10-24 16:27:39 -0700894class ChildLayerTest : public LayerUpdateTest {
895protected:
896 void SetUp() override {
897 LayerUpdateTest::SetUp();
898 mChild = mComposerClient->createSurface(
899 String8("Child surface"),
900 10, 10, PIXEL_FORMAT_RGBA_8888,
901 0, mFGSurfaceControl.get());
902 fillSurfaceRGBA8(mChild, 200, 200, 200);
903
904 {
905 SCOPED_TRACE("before anything");
906 ScreenCapture::captureScreen(&mCapture);
907 mCapture->expectChildColor(64, 64);
908 }
909 }
910 void TearDown() override {
911 LayerUpdateTest::TearDown();
912 mChild = 0;
913 }
914
915 sp<SurfaceControl> mChild;
916 sp<ScreenCapture> mCapture;
917};
918
919TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700920 asTransaction([&](Transaction& t) {
921 t.show(mChild);
922 t.setPosition(mChild, 10, 10);
923 t.setPosition(mFGSurfaceControl, 64, 64);
924 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700925
926 {
927 ScreenCapture::captureScreen(&mCapture);
928 // Top left of foreground must now be visible
929 mCapture->expectFGColor(64, 64);
930 // But 10 pixels in we should see the child surface
931 mCapture->expectChildColor(74, 74);
932 // And 10 more pixels we should be back to the foreground surface
933 mCapture->expectFGColor(84, 84);
934 }
935
Robert Carr4cdc58f2017-08-23 14:22:20 -0700936 asTransaction([&](Transaction& t) {
937 t.setPosition(mFGSurfaceControl, 0, 0);
938 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700939
940 {
941 ScreenCapture::captureScreen(&mCapture);
942 // Top left of foreground should now be at 0, 0
943 mCapture->expectFGColor(0, 0);
944 // But 10 pixels in we should see the child surface
945 mCapture->expectChildColor(10, 10);
946 // And 10 more pixels we should be back to the foreground surface
947 mCapture->expectFGColor(20, 20);
948 }
949}
950
Robert Carr41b08b52017-06-01 16:11:34 -0700951TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700952 asTransaction([&](Transaction& t) {
953 t.show(mChild);
954 t.setPosition(mChild, 0, 0);
955 t.setPosition(mFGSurfaceControl, 0, 0);
956 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
957 });
Robert Carr41b08b52017-06-01 16:11:34 -0700958
959 {
960 ScreenCapture::captureScreen(&mCapture);
961 mCapture->expectChildColor(0, 0);
962 mCapture->expectChildColor(4, 4);
963 mCapture->expectBGColor(5, 5);
964 }
965}
966
967TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700968 asTransaction([&](Transaction& t) {
969 t.show(mChild);
970 t.setPosition(mChild, 0, 0);
971 t.setPosition(mFGSurfaceControl, 0, 0);
972 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
973 });
Robert Carr41b08b52017-06-01 16:11:34 -0700974
975 {
976 ScreenCapture::captureScreen(&mCapture);
977 mCapture->expectChildColor(0, 0);
978 mCapture->expectChildColor(4, 4);
979 mCapture->expectBGColor(5, 5);
980 }
981}
982
Robert Carr1f0a16a2016-10-24 16:27:39 -0700983TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700984 asTransaction([&](Transaction& t) {
985 t.show(mChild);
986 t.setPosition(mFGSurfaceControl, 0, 0);
987 t.setPosition(mChild, 63, 63);
988 });
Robert Carr1f0a16a2016-10-24 16:27:39 -0700989
990 {
991 ScreenCapture::captureScreen(&mCapture);
992 mCapture->expectFGColor(0, 0);
993 // Last pixel in foreground should now be the child.
994 mCapture->expectChildColor(63, 63);
995 // But the child should be constrained and the next pixel
996 // must be the background
997 mCapture->expectBGColor(64, 64);
998 }
999}
1000
1001TEST_F(ChildLayerTest, ChildLayerScaling) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001002 asTransaction([&](Transaction& t) {
1003 t.setPosition(mFGSurfaceControl, 0, 0);
1004 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001005
1006 // Find the boundary between the parent and child
1007 {
1008 ScreenCapture::captureScreen(&mCapture);
1009 mCapture->expectChildColor(9, 9);
1010 mCapture->expectFGColor(10, 10);
1011 }
1012
Robert Carr4cdc58f2017-08-23 14:22:20 -07001013 asTransaction([&](Transaction& t) {
1014 t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0);
1015 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001016
1017 // The boundary should be twice as far from the origin now.
1018 // The pixels from the last test should all be child now
1019 {
1020 ScreenCapture::captureScreen(&mCapture);
1021 mCapture->expectChildColor(9, 9);
1022 mCapture->expectChildColor(10, 10);
1023 mCapture->expectChildColor(19, 19);
1024 mCapture->expectFGColor(20, 20);
1025 }
1026}
Robert Carr9524cb32017-02-13 11:32:32 -08001027
Robert Carr6452f122017-03-21 10:41:29 -07001028TEST_F(ChildLayerTest, ChildLayerAlpha) {
1029 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1030 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1031 fillSurfaceRGBA8(mChild, 0, 254, 0);
1032 waitForPostedBuffers();
1033
Robert Carr4cdc58f2017-08-23 14:22:20 -07001034 asTransaction([&](Transaction& t) {
1035 t.show(mChild);
1036 t.setPosition(mChild, 0, 0);
1037 t.setPosition(mFGSurfaceControl, 0, 0);
1038 });
Robert Carr6452f122017-03-21 10:41:29 -07001039
1040 {
1041 ScreenCapture::captureScreen(&mCapture);
1042 // Unblended child color
1043 mCapture->checkPixel(0, 0, 0, 254, 0);
1044 }
1045
Robert Carr4cdc58f2017-08-23 14:22:20 -07001046 asTransaction([&](Transaction& t) {
1047 t.setAlpha(mChild, 0.5);
1048 });
Robert Carr6452f122017-03-21 10:41:29 -07001049
1050 {
1051 ScreenCapture::captureScreen(&mCapture);
1052 // Child and BG blended.
1053 mCapture->checkPixel(0, 0, 127, 127, 0);
1054 }
1055
Robert Carr4cdc58f2017-08-23 14:22:20 -07001056 asTransaction([&](Transaction& t) {
1057 t.setAlpha(mFGSurfaceControl, 0.5);
1058 });
Robert Carr6452f122017-03-21 10:41:29 -07001059
1060 {
1061 ScreenCapture::captureScreen(&mCapture);
1062 // Child and BG blended.
1063 mCapture->checkPixel(0, 0, 95, 64, 95);
1064 }
1065}
1066
Robert Carr9524cb32017-02-13 11:32:32 -08001067TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001068 asTransaction([&](Transaction& t) {
1069 t.show(mChild);
1070 t.setPosition(mChild, 10, 10);
1071 t.setPosition(mFGSurfaceControl, 64, 64);
1072 });
Robert Carr9524cb32017-02-13 11:32:32 -08001073
1074 {
1075 ScreenCapture::captureScreen(&mCapture);
1076 // Top left of foreground must now be visible
1077 mCapture->expectFGColor(64, 64);
1078 // But 10 pixels in we should see the child surface
1079 mCapture->expectChildColor(74, 74);
1080 // And 10 more pixels we should be back to the foreground surface
1081 mCapture->expectFGColor(84, 84);
1082 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001083
1084 asTransaction([&](Transaction& t) {
1085 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1086 });
1087
Robert Carr9524cb32017-02-13 11:32:32 -08001088 {
1089 ScreenCapture::captureScreen(&mCapture);
1090 mCapture->expectFGColor(64, 64);
1091 // In reparenting we should have exposed the entire foreground surface.
1092 mCapture->expectFGColor(74, 74);
1093 // And the child layer should now begin at 10, 10 (since the BG
1094 // layer is at (0, 0)).
1095 mCapture->expectBGColor(9, 9);
1096 mCapture->expectChildColor(10, 10);
1097 }
1098}
1099
chaviw161410b02017-07-27 10:46:08 -07001100TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001101 asTransaction([&](Transaction& t) {
1102 t.show(mChild);
1103 t.setPosition(mChild, 10, 10);
1104 t.setPosition(mFGSurfaceControl, 64, 64);
1105 });
Robert Carr9524cb32017-02-13 11:32:32 -08001106
1107 {
1108 ScreenCapture::captureScreen(&mCapture);
1109 // Top left of foreground must now be visible
1110 mCapture->expectFGColor(64, 64);
1111 // But 10 pixels in we should see the child surface
1112 mCapture->expectChildColor(74, 74);
1113 // And 10 more pixels we should be back to the foreground surface
1114 mCapture->expectFGColor(84, 84);
1115 }
1116
Robert Carr4cdc58f2017-08-23 14:22:20 -07001117 asTransaction([&](Transaction& t) {
1118 t.detachChildren(mFGSurfaceControl);
1119 });
Robert Carr9524cb32017-02-13 11:32:32 -08001120
Robert Carr4cdc58f2017-08-23 14:22:20 -07001121 asTransaction([&](Transaction& t) {
1122 t.hide(mChild);
1123 });
Robert Carr9524cb32017-02-13 11:32:32 -08001124
chaviw161410b02017-07-27 10:46:08 -07001125 // Since the child has the same client as the parent, it will not get
1126 // detached and will be hidden.
1127 {
1128 ScreenCapture::captureScreen(&mCapture);
1129 mCapture->expectFGColor(64, 64);
1130 mCapture->expectFGColor(74, 74);
1131 mCapture->expectFGColor(84, 84);
1132 }
1133}
1134
1135TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1136 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
1137 sp<SurfaceControl> mChildNewClient = mNewComposerClient->createSurface(
1138 String8("New Child Test Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1139 0, mFGSurfaceControl.get());
1140
1141 ASSERT_TRUE(mChildNewClient != NULL);
1142 ASSERT_TRUE(mChildNewClient->isValid());
1143
1144 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1145
Robert Carr4cdc58f2017-08-23 14:22:20 -07001146 asTransaction([&](Transaction& t) {
1147 t.hide(mChild);
1148 t.show(mChildNewClient);
1149 t.setPosition(mChildNewClient, 10, 10);
1150 t.setPosition(mFGSurfaceControl, 64, 64);
1151 });
chaviw161410b02017-07-27 10:46:08 -07001152
1153 {
1154 ScreenCapture::captureScreen(&mCapture);
1155 // Top left of foreground must now be visible
1156 mCapture->expectFGColor(64, 64);
1157 // But 10 pixels in we should see the child surface
1158 mCapture->expectChildColor(74, 74);
1159 // And 10 more pixels we should be back to the foreground surface
1160 mCapture->expectFGColor(84, 84);
1161 }
1162
Robert Carr4cdc58f2017-08-23 14:22:20 -07001163 asTransaction([&](Transaction& t) {
1164 t.detachChildren(mFGSurfaceControl);
1165 });
chaviw161410b02017-07-27 10:46:08 -07001166
Robert Carr4cdc58f2017-08-23 14:22:20 -07001167 asTransaction([&](Transaction& t) {
1168 t.hide(mChildNewClient);
1169 });
chaviw161410b02017-07-27 10:46:08 -07001170
Robert Carr9524cb32017-02-13 11:32:32 -08001171 // Nothing should have changed.
1172 {
1173 ScreenCapture::captureScreen(&mCapture);
1174 mCapture->expectFGColor(64, 64);
1175 mCapture->expectChildColor(74, 74);
1176 mCapture->expectFGColor(84, 84);
1177 }
1178}
1179
Robert Carr9b429f42017-04-17 14:56:57 -07001180TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001181 asTransaction([&](Transaction& t) {
1182 t.show(mChild);
1183 t.setPosition(mChild, 0, 0);
1184 t.setPosition(mFGSurfaceControl, 0, 0);
1185 });
Robert Carr9b429f42017-04-17 14:56:57 -07001186
1187 {
1188 ScreenCapture::captureScreen(&mCapture);
1189 // We've positioned the child in the top left.
1190 mCapture->expectChildColor(0, 0);
1191 // But it's only 10x10.
1192 mCapture->expectFGColor(10, 10);
1193 }
1194
Robert Carr4cdc58f2017-08-23 14:22:20 -07001195 asTransaction([&](Transaction& t) {
1196 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1197 // We cause scaling by 2.
1198 t.setSize(mFGSurfaceControl, 128, 128);
1199 });
Robert Carr9b429f42017-04-17 14:56:57 -07001200
1201 {
1202 ScreenCapture::captureScreen(&mCapture);
1203 // We've positioned the child in the top left.
1204 mCapture->expectChildColor(0, 0);
1205 mCapture->expectChildColor(10, 10);
1206 mCapture->expectChildColor(19, 19);
1207 // And now it should be scaled all the way to 20x20
1208 mCapture->expectFGColor(20, 20);
1209 }
1210}
1211
Robert Carr1725eee2017-04-26 18:32:15 -07001212// Regression test for b/37673612
1213TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001214 asTransaction([&](Transaction& t) {
1215 t.show(mChild);
1216 t.setPosition(mChild, 0, 0);
1217 t.setPosition(mFGSurfaceControl, 0, 0);
1218 });
Robert Carr1725eee2017-04-26 18:32:15 -07001219
1220 {
1221 ScreenCapture::captureScreen(&mCapture);
1222 // We've positioned the child in the top left.
1223 mCapture->expectChildColor(0, 0);
1224 // But it's only 10x10.
1225 mCapture->expectFGColor(10, 10);
1226 }
Robert Carr1725eee2017-04-26 18:32:15 -07001227 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1228 // the WM specified state size.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001229 asTransaction([&](Transaction& t) {
1230 t.setSize(mFGSurfaceControl, 128, 64);
1231 });
Robert Carr1725eee2017-04-26 18:32:15 -07001232 sp<Surface> s = mFGSurfaceControl->getSurface();
1233 auto anw = static_cast<ANativeWindow*>(s.get());
1234 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1235 native_window_set_buffers_dimensions(anw, 64, 128);
1236 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1237 waitForPostedBuffers();
1238
1239 {
1240 // The child should still be in the same place and not have any strange scaling as in
1241 // b/37673612.
1242 ScreenCapture::captureScreen(&mCapture);
1243 mCapture->expectChildColor(0, 0);
1244 mCapture->expectFGColor(10, 10);
1245 }
1246}
1247
Dan Stoza412903f2017-04-27 13:42:17 -07001248TEST_F(ChildLayerTest, Bug36858924) {
1249 // Destroy the child layer
1250 mChild.clear();
1251
1252 // Now recreate it as hidden
1253 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1254 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1255 mFGSurfaceControl.get());
1256
1257 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001258 asTransaction([&](Transaction& t) {
1259 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
1260 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1261 t.show(mChild);
1262 });
Dan Stoza412903f2017-04-27 13:42:17 -07001263
1264 // Render the foreground surface a few times
1265 //
1266 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1267 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1268 // never acquire/release the first buffer
1269 ALOGI("Filling 1");
1270 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1271 ALOGI("Filling 2");
1272 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1273 ALOGI("Filling 3");
1274 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1275 ALOGI("Filling 4");
1276 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1277}
1278
chaviwf1961f72017-09-18 16:41:07 -07001279TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001280 asTransaction([&](Transaction& t) {
1281 t.show(mChild);
1282 t.setPosition(mChild, 10, 10);
1283 t.setPosition(mFGSurfaceControl, 64, 64);
1284 });
chaviw06178942017-07-27 10:25:59 -07001285
1286 {
1287 ScreenCapture::captureScreen(&mCapture);
1288 // Top left of foreground must now be visible
1289 mCapture->expectFGColor(64, 64);
1290 // But 10 pixels in we should see the child surface
1291 mCapture->expectChildColor(74, 74);
1292 // And 10 more pixels we should be back to the foreground surface
1293 mCapture->expectFGColor(84, 84);
1294 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001295
1296 asTransaction([&](Transaction& t) {
1297 t.reparent(mChild, mBGSurfaceControl->getHandle());
1298 });
1299
chaviw06178942017-07-27 10:25:59 -07001300 {
1301 ScreenCapture::captureScreen(&mCapture);
1302 mCapture->expectFGColor(64, 64);
1303 // In reparenting we should have exposed the entire foreground surface.
1304 mCapture->expectFGColor(74, 74);
1305 // And the child layer should now begin at 10, 10 (since the BG
1306 // layer is at (0, 0)).
1307 mCapture->expectBGColor(9, 9);
1308 mCapture->expectChildColor(10, 10);
1309 }
1310}
1311
chaviwf1961f72017-09-18 16:41:07 -07001312TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001313 asTransaction([&](Transaction& t) {
1314 t.show(mChild);
1315 t.setPosition(mChild, 10, 10);
1316 t.setPosition(mFGSurfaceControl, 64, 64);
1317 });
chaviwf1961f72017-09-18 16:41:07 -07001318
1319 {
1320 ScreenCapture::captureScreen(&mCapture);
1321 // Top left of foreground must now be visible
1322 mCapture->expectFGColor(64, 64);
1323 // But 10 pixels in we should see the child surface
1324 mCapture->expectChildColor(74, 74);
1325 // And 10 more pixels we should be back to the foreground surface
1326 mCapture->expectFGColor(84, 84);
1327 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001328 asTransaction([&](Transaction& t) {
1329 t.reparent(mChild, nullptr);
1330 });
chaviwf1961f72017-09-18 16:41:07 -07001331 {
1332 ScreenCapture::captureScreen(&mCapture);
1333 // Nothing should have changed.
1334 mCapture->expectFGColor(64, 64);
1335 mCapture->expectChildColor(74, 74);
1336 mCapture->expectFGColor(84, 84);
1337 }
1338}
1339
1340TEST_F(ChildLayerTest, ReparentFromNoParent) {
1341 sp<SurfaceControl> newSurface = mComposerClient->createSurface(
1342 String8("New Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, 0);
1343 ASSERT_TRUE(newSurface != NULL);
1344 ASSERT_TRUE(newSurface->isValid());
1345
1346 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001347 asTransaction([&](Transaction& t) {
1348 t.hide(mChild);
1349 t.show(newSurface);
1350 t.setPosition(newSurface, 10, 10);
1351 t.setLayer(newSurface, INT32_MAX-2);
1352 t.setPosition(mFGSurfaceControl, 64, 64);
1353 });
chaviwf1961f72017-09-18 16:41:07 -07001354
1355 {
1356 ScreenCapture::captureScreen(&mCapture);
1357 // Top left of foreground must now be visible
1358 mCapture->expectFGColor(64, 64);
1359 // At 10, 10 we should see the new surface
1360 mCapture->checkPixel(10, 10, 63, 195, 63);
1361 }
1362
Robert Carr4cdc58f2017-08-23 14:22:20 -07001363 asTransaction([&](Transaction& t) {
1364 t.reparent(newSurface, mFGSurfaceControl->getHandle());
1365 });
chaviwf1961f72017-09-18 16:41:07 -07001366
1367 {
1368 ScreenCapture::captureScreen(&mCapture);
1369 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1370 // mFGSurface, putting it at 74, 74.
1371 mCapture->expectFGColor(64, 64);
1372 mCapture->checkPixel(74, 74, 63, 195, 63);
1373 mCapture->expectFGColor(84, 84);
1374 }
1375}
1376
chaviwc9674332017-08-28 12:32:18 -07001377TEST_F(ChildLayerTest, NestedChildren) {
1378 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1379 String8("Grandchild surface"),
1380 10, 10, PIXEL_FORMAT_RGBA_8888,
1381 0, mChild.get());
1382 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1383
1384 {
1385 ScreenCapture::captureScreen(&mCapture);
1386 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1387 // which begins at 64, 64
1388 mCapture->checkPixel(64, 64, 50, 50, 50);
1389 }
1390}
1391
chaviw13fdc492017-06-27 12:40:18 -07001392class LayerColorTest : public LayerUpdateTest {
1393 protected:
1394 void SetUp() override {
1395 LayerUpdateTest::SetUp();
1396
1397 mLayerColorControl = mComposerClient->createSurface(
1398 String8("Layer color surface"),
1399 128, 128, PIXEL_FORMAT_RGBA_8888,
1400 ISurfaceComposerClient::eFXSurfaceColor);
1401
1402 ASSERT_TRUE(mLayerColorControl != NULL);
1403 ASSERT_TRUE(mLayerColorControl->isValid());
1404
Robert Carr4cdc58f2017-08-23 14:22:20 -07001405 asTransaction([&](Transaction& t) {
1406 t.setLayer(mLayerColorControl, INT32_MAX-1);
1407 t.setPosition(mLayerColorControl, 140, 140);
1408 t.hide(mLayerColorControl);
1409 t.hide(mFGSurfaceControl);
1410 });
chaviw13fdc492017-06-27 12:40:18 -07001411 }
1412
1413 void TearDown() override {
1414 LayerUpdateTest::TearDown();
1415 mLayerColorControl = 0;
1416 }
1417
1418 sp<SurfaceControl> mLayerColorControl;
1419};
1420
1421TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1422 sp<ScreenCapture> sc;
1423
1424 {
1425 SCOPED_TRACE("before setColor");
1426 ScreenCapture::captureScreen(&sc);
1427 sc->expectBGColor(145, 145);
1428 }
1429
Robert Carr4cdc58f2017-08-23 14:22:20 -07001430 asTransaction([&](Transaction& t) {
1431 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1432 t.setColor(mLayerColorControl, color);
1433 t.show(mLayerColorControl);
1434 });
chaviw13fdc492017-06-27 12:40:18 -07001435
chaviw13fdc492017-06-27 12:40:18 -07001436 {
1437 // There should now be a color
1438 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001439
chaviw13fdc492017-06-27 12:40:18 -07001440 ScreenCapture::captureScreen(&sc);
1441 sc->checkPixel(145, 145, 43, 207, 131);
1442 }
1443}
1444
1445TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1446 sp<ScreenCapture> sc;
1447 {
1448 SCOPED_TRACE("before setColor");
1449 ScreenCapture::captureScreen(&sc);
1450 sc->expectBGColor(145, 145);
1451 }
1452
Robert Carr4cdc58f2017-08-23 14:22:20 -07001453 asTransaction([&](Transaction& t) {
1454 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1455 t.setColor(mLayerColorControl, color);
1456 t.setAlpha(mLayerColorControl, .75f);
1457 t.show(mLayerColorControl);
1458 });
1459
chaviw13fdc492017-06-27 12:40:18 -07001460 {
1461 // There should now be a color with .75 alpha
1462 SCOPED_TRACE("after setColor");
1463 ScreenCapture::captureScreen(&sc);
1464 sc->checkPixel(145, 145, 48, 171, 147);
1465 }
1466}
1467
1468TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1469 sp<ScreenCapture> sc;
1470 {
1471 SCOPED_TRACE("before setColor");
1472 ScreenCapture::captureScreen(&sc);
1473 sc->expectBGColor(145, 145);
1474 }
1475
Robert Carr4cdc58f2017-08-23 14:22:20 -07001476 asTransaction([&](Transaction& t) {
1477 t.show(mLayerColorControl);
1478 });
1479
chaviw13fdc492017-06-27 12:40:18 -07001480 {
1481 // There should now be set to 0,0,0 (black) as default.
1482 SCOPED_TRACE("after setColor");
1483 ScreenCapture::captureScreen(&sc);
1484 sc->checkPixel(145, 145, 0, 0, 0);
1485 }
1486}
1487
chaviwa76b2712017-09-20 12:02:26 -07001488class ScreenCaptureTest : public LayerUpdateTest {
1489protected:
1490 std::unique_ptr<CaptureLayer> mCapture;
1491};
1492
1493TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
1494 auto bgHandle = mBGSurfaceControl->getHandle();
1495 CaptureLayer::captureScreen(&mCapture, bgHandle);
1496 mCapture->expectBGColor(0, 0);
1497 // Doesn't capture FG layer which is at 64, 64
1498 mCapture->expectBGColor(64, 64);
1499}
1500
1501TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
1502 auto fgHandle = mFGSurfaceControl->getHandle();
1503
1504 sp<SurfaceControl> child = mComposerClient->createSurface(
1505 String8("Child surface"),
1506 10, 10, PIXEL_FORMAT_RGBA_8888,
1507 0, mFGSurfaceControl.get());
1508 fillSurfaceRGBA8(child, 200, 200, 200);
1509
1510 SurfaceComposerClient::Transaction()
1511 .show(child)
1512 .apply(true);
1513
1514 // Captures mFGSurfaceControl layer and its child.
1515 CaptureLayer::captureScreen(&mCapture, fgHandle);
1516 mCapture->expectFGColor(10, 10);
1517 mCapture->expectChildColor(0, 0);
1518}
1519
1520TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
1521 auto fgHandle = mFGSurfaceControl->getHandle();
1522
1523 sp<SurfaceControl> child = mComposerClient->createSurface(
1524 String8("Child surface"),
1525 10, 10, PIXEL_FORMAT_RGBA_8888,
1526 0, mFGSurfaceControl.get());
1527 fillSurfaceRGBA8(child, 200, 200, 200);
1528
1529 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1530 String8("Grandchild surface"), 5, 5,
1531 PIXEL_FORMAT_RGBA_8888, 0, child.get());
1532
1533 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1534 SurfaceComposerClient::Transaction()
1535 .show(child)
1536 .setPosition(grandchild, 5, 5)
1537 .show(grandchild)
1538 .apply(true);
1539
1540 // Captures mFGSurfaceControl, its child, and the grandchild.
1541 CaptureLayer::captureScreen(&mCapture, fgHandle);
1542 mCapture->expectFGColor(10, 10);
1543 mCapture->expectChildColor(0, 0);
1544 mCapture->checkPixel(5, 5, 50, 50, 50);
1545}
1546
1547TEST_F(ScreenCaptureTest, CaptureChildOnly) {
1548 sp<SurfaceControl> child = mComposerClient->createSurface(
1549 String8("Child surface"),
1550 10, 10, PIXEL_FORMAT_RGBA_8888,
1551 0, mFGSurfaceControl.get());
1552 fillSurfaceRGBA8(child, 200, 200, 200);
1553 auto childHandle = child->getHandle();
1554
1555 SurfaceComposerClient::Transaction()
1556 .setPosition(child, 5, 5)
1557 .show(child)
1558 .apply(true);
1559
1560 // Captures only the child layer, and not the parent.
1561 CaptureLayer::captureScreen(&mCapture, childHandle);
1562 mCapture->expectChildColor(0, 0);
1563 mCapture->expectChildColor(9, 9);
1564}
1565
1566TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
1567 sp<SurfaceControl> child = mComposerClient->createSurface(
1568 String8("Child surface"),
1569 10, 10, PIXEL_FORMAT_RGBA_8888,
1570 0, mFGSurfaceControl.get());
1571 fillSurfaceRGBA8(child, 200, 200, 200);
1572 auto childHandle = child->getHandle();
1573
1574 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1575 String8("Grandchild surface"), 5, 5,
1576 PIXEL_FORMAT_RGBA_8888, 0, child.get());
1577 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1578
1579 SurfaceComposerClient::Transaction()
1580 .show(child)
1581 .setPosition(grandchild, 5, 5)
1582 .show(grandchild)
1583 .apply(true);
1584
1585 auto grandchildHandle = grandchild->getHandle();
1586
1587 // Captures only the grandchild.
1588 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
1589 mCapture->checkPixel(0, 0, 50, 50, 50);
1590 mCapture->checkPixel(4, 4, 50, 50, 50);
1591}
1592
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001593}