blob: 4ce14f8d3a50954df41bf8f38e4236b43ee669f9 [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>
22#include <gui/Surface.h>
23#include <gui/SurfaceComposerClient.h>
24#include <private/gui/ComposerService.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070025#include <private/gui/LayerState.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080026
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070027#include <utils/String8.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070028#include <ui/DisplayInfo.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070029
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070030#include <math.h>
31
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070032namespace android {
33
34// Fill an RGBA_8888 formatted surface with a single color.
35static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc,
Robert Carr7bf247e2017-05-18 14:02:49 -070036 uint8_t r, uint8_t g, uint8_t b, bool unlock=true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080037 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070038 sp<Surface> s = sc->getSurface();
39 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -080040 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
41 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070042 for (int y = 0; y < outBuffer.height; y++) {
43 for (int x = 0; x < outBuffer.width; x++) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044 uint8_t* pixel = img + (4 * (y*outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070045 pixel[0] = r;
46 pixel[1] = g;
47 pixel[2] = b;
48 pixel[3] = 255;
49 }
50 }
Robert Carr7bf247e2017-05-18 14:02:49 -070051 if (unlock) {
52 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
53 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070054}
55
56// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
57// individual pixel values for testing purposes.
58class ScreenCapture : public RefBase {
59public:
60 static void captureScreen(sp<ScreenCapture>* sc) {
Michael Lentine5a16a622015-05-21 13:48:24 -070061 sp<IGraphicBufferProducer> producer;
62 sp<IGraphicBufferConsumer> consumer;
63 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -070064 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070065 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Michael Lentine5a16a622015-05-21 13:48:24 -070066 sp<IBinder> display(sf->getBuiltInDisplay(
67 ISurfaceComposer::eDisplayIdMain));
Pablo Ceballos15311bd2016-06-01 18:53:40 -070068 SurfaceComposerClient::openGlobalTransaction();
69 SurfaceComposerClient::closeGlobalTransaction(true);
Michael Lentine5a16a622015-05-21 13:48:24 -070070 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0,
71 0, INT_MAX, false));
72 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070073 }
74
75 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -070076 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
77 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
78 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070079 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
80 String8 err(String8::format("pixel @ (%3d, %3d): "
81 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
82 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070083 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070084 }
85 }
86
Robert Carr1f0a16a2016-10-24 16:27:39 -070087 void expectFGColor(uint32_t x, uint32_t y) {
88 checkPixel(x, y, 195, 63, 63);
89 }
90
91 void expectBGColor(uint32_t x, uint32_t y) {
92 checkPixel(x, y, 63, 63, 195);
93 }
94
95 void expectChildColor(uint32_t x, uint32_t y) {
96 checkPixel(x, y, 200, 200, 200);
97 }
98
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070099private:
Michael Lentine5a16a622015-05-21 13:48:24 -0700100 ScreenCapture(const sp<CpuConsumer>& cc) :
101 mCC(cc) {
102 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
103 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700104
Michael Lentine5a16a622015-05-21 13:48:24 -0700105 ~ScreenCapture() {
106 mCC->unlockBuffer(mBuf);
107 }
108
109 sp<CpuConsumer> mCC;
110 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700111};
112
113class LayerUpdateTest : public ::testing::Test {
114protected:
115 virtual void SetUp() {
116 mComposerClient = new SurfaceComposerClient;
117 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
118
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700119 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
120 ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700121 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700122 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700123
124 ssize_t displayWidth = info.w;
125 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700126
127 // Background surface
128 mBGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700129 String8("BG Test Surface"), displayWidth, displayHeight,
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700130 PIXEL_FORMAT_RGBA_8888, 0);
131 ASSERT_TRUE(mBGSurfaceControl != NULL);
132 ASSERT_TRUE(mBGSurfaceControl->isValid());
133 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
134
135 // Foreground surface
136 mFGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700137 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700138 ASSERT_TRUE(mFGSurfaceControl != NULL);
139 ASSERT_TRUE(mFGSurfaceControl->isValid());
140
141 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
142
143 // Synchronization surface
144 mSyncSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700145 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700146 ASSERT_TRUE(mSyncSurfaceControl != NULL);
147 ASSERT_TRUE(mSyncSurfaceControl->isValid());
148
149 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
150
151 SurfaceComposerClient::openGlobalTransaction();
152
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700153 mComposerClient->setDisplayLayerStack(display, 0);
154
Robert Carr1f0a16a2016-10-24 16:27:39 -0700155 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT32_MAX-2));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700156 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show());
157
Robert Carr1f0a16a2016-10-24 16:27:39 -0700158 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700159 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64));
160 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
161
Robert Carr1f0a16a2016-10-24 16:27:39 -0700162 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700163 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setPosition(displayWidth-2,
164 displayHeight-2));
165 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->show());
166
167 SurfaceComposerClient::closeGlobalTransaction(true);
168 }
169
170 virtual void TearDown() {
171 mComposerClient->dispose();
172 mBGSurfaceControl = 0;
173 mFGSurfaceControl = 0;
174 mSyncSurfaceControl = 0;
175 mComposerClient = 0;
176 }
177
178 void waitForPostedBuffers() {
179 // Since the sync surface is in synchronous mode (i.e. double buffered)
180 // posting three buffers to it should ensure that at least two
181 // SurfaceFlinger::handlePageFlip calls have been made, which should
182 // guaranteed that a buffer posted to another Surface has been retired.
183 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
184 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
185 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
186 }
187
188 sp<SurfaceComposerClient> mComposerClient;
189 sp<SurfaceControl> mBGSurfaceControl;
190 sp<SurfaceControl> mFGSurfaceControl;
191
192 // This surface is used to ensure that the buffers posted to
193 // mFGSurfaceControl have been picked up by SurfaceFlinger.
194 sp<SurfaceControl> mSyncSurfaceControl;
195};
196
197TEST_F(LayerUpdateTest, LayerMoveWorks) {
198 sp<ScreenCapture> sc;
199 {
200 SCOPED_TRACE("before move");
201 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800202 sc->expectBGColor(0, 12);
203 sc->expectFGColor(75, 75);
204 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700205 }
206
207 SurfaceComposerClient::openGlobalTransaction();
208 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128, 128));
209 SurfaceComposerClient::closeGlobalTransaction(true);
210 {
211 // This should reflect the new position, but not the new color.
212 SCOPED_TRACE("after move, before redraw");
213 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800214 sc->expectBGColor(24, 24);
215 sc->expectBGColor(75, 75);
216 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700217 }
218
219 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
220 waitForPostedBuffers();
221 {
222 // This should reflect the new position and the new color.
223 SCOPED_TRACE("after redraw");
224 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800225 sc->expectBGColor(24, 24);
226 sc->expectBGColor(75, 75);
227 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700228 }
229}
230
231TEST_F(LayerUpdateTest, LayerResizeWorks) {
232 sp<ScreenCapture> sc;
233 {
234 SCOPED_TRACE("before resize");
235 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800236 sc->expectBGColor(0, 12);
237 sc->expectFGColor(75, 75);
238 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700239 }
240
Steve Block9d453682011-12-20 16:23:08 +0000241 ALOGD("resizing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700242 SurfaceComposerClient::openGlobalTransaction();
243 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128));
244 SurfaceComposerClient::closeGlobalTransaction(true);
Steve Block9d453682011-12-20 16:23:08 +0000245 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700246 {
247 // This should not reflect the new size or color because SurfaceFlinger
248 // has not yet received a buffer of the correct size.
249 SCOPED_TRACE("after resize, before redraw");
250 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800251 sc->expectBGColor(0, 12);
252 sc->expectFGColor(75, 75);
253 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700254 }
255
Steve Block9d453682011-12-20 16:23:08 +0000256 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700257 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
258 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000259 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700260 {
261 // This should reflect the new size and the new color.
262 SCOPED_TRACE("after redraw");
263 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800264 sc->expectBGColor(24, 24);
265 sc->checkPixel(75, 75, 63, 195, 63);
266 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700267 }
268}
269
Haixia Shid5750962015-07-27 16:50:49 -0700270TEST_F(LayerUpdateTest, LayerCropWorks) {
271 sp<ScreenCapture> sc;
272 {
273 SCOPED_TRACE("before crop");
274 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800275 sc->expectBGColor(24, 24);
276 sc->expectFGColor(75, 75);
277 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700278 }
279
280 SurfaceComposerClient::openGlobalTransaction();
281 Rect cropRect(16, 16, 32, 32);
282 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setCrop(cropRect));
283 SurfaceComposerClient::closeGlobalTransaction(true);
284 {
285 // This should crop the foreground surface.
286 SCOPED_TRACE("after crop");
287 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800288 sc->expectBGColor(24, 24);
289 sc->expectBGColor(75, 75);
290 sc->expectFGColor(95, 80);
291 sc->expectFGColor(80, 95);
292 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700293 }
294}
295
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000296TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
297 sp<ScreenCapture> sc;
298 {
299 SCOPED_TRACE("before crop");
300 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800301 sc->expectBGColor(24, 24);
302 sc->expectFGColor(75, 75);
303 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000304 }
305 SurfaceComposerClient::openGlobalTransaction();
306 Rect cropRect(16, 16, 32, 32);
307 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFinalCrop(cropRect));
308 SurfaceComposerClient::closeGlobalTransaction(true);
309 {
310 // This should crop the foreground surface.
311 SCOPED_TRACE("after crop");
312 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800313 sc->expectBGColor(24, 24);
314 sc->expectBGColor(75, 75);
315 sc->expectBGColor(95, 80);
316 sc->expectBGColor(80, 95);
317 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000318 }
319}
320
Haixia Shid5750962015-07-27 16:50:49 -0700321TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
322 sp<ScreenCapture> sc;
323 {
324 SCOPED_TRACE("before setLayer");
325 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800326 sc->expectBGColor(24, 24);
327 sc->expectFGColor(75, 75);
328 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700329 }
330
331 SurfaceComposerClient::openGlobalTransaction();
332 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT_MAX - 3));
333 SurfaceComposerClient::closeGlobalTransaction(true);
334 {
335 // This should hide the foreground surface beneath the background.
336 SCOPED_TRACE("after setLayer");
337 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800338 sc->expectBGColor(24, 24);
339 sc->expectBGColor(75, 75);
340 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700341 }
342}
343
344TEST_F(LayerUpdateTest, LayerShowHideWorks) {
345 sp<ScreenCapture> sc;
346 {
347 SCOPED_TRACE("before hide");
348 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800349 sc->expectBGColor(24, 24);
350 sc->expectFGColor(75, 75);
351 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700352 }
353
354 SurfaceComposerClient::openGlobalTransaction();
355 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->hide());
356 SurfaceComposerClient::closeGlobalTransaction(true);
357 {
358 // This should hide the foreground surface.
359 SCOPED_TRACE("after hide, before show");
360 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800361 sc->expectBGColor(24, 24);
362 sc->expectBGColor(75, 75);
363 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700364 }
365
366 SurfaceComposerClient::openGlobalTransaction();
367 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
368 SurfaceComposerClient::closeGlobalTransaction(true);
369 {
370 // This should show the foreground surface.
371 SCOPED_TRACE("after show");
372 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800373 sc->expectBGColor(24, 24);
374 sc->expectFGColor(75, 75);
375 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700376 }
377}
378
379TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
380 sp<ScreenCapture> sc;
381 {
382 SCOPED_TRACE("before setAlpha");
383 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800384 sc->expectBGColor(24, 24);
385 sc->expectFGColor(75, 75);
386 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700387 }
388
389 SurfaceComposerClient::openGlobalTransaction();
390 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75f));
391 SurfaceComposerClient::closeGlobalTransaction(true);
392 {
393 // This should set foreground to be 75% opaque.
394 SCOPED_TRACE("after setAlpha");
395 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800396 sc->expectBGColor(24, 24);
397 sc->checkPixel(75, 75, 162, 63, 96);
398 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700399 }
400}
401
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700402TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
403 sp<ScreenCapture> sc;
404 {
405 SCOPED_TRACE("before setLayerStack");
406 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800407 sc->expectBGColor(24, 24);
408 sc->expectFGColor(75, 75);
409 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700410 }
411
412 SurfaceComposerClient::openGlobalTransaction();
413 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayerStack(1));
414 SurfaceComposerClient::closeGlobalTransaction(true);
415 {
416 // This should hide the foreground surface since it goes to a different
417 // layer stack.
418 SCOPED_TRACE("after setLayerStack");
419 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800420 sc->expectBGColor(24, 24);
421 sc->expectBGColor(75, 75);
422 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700423 }
424}
425
426TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
427 sp<ScreenCapture> sc;
428 {
429 SCOPED_TRACE("before setFlags");
430 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800431 sc->expectBGColor(24, 24);
432 sc->expectFGColor(75, 75);
433 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700434 }
435
436 SurfaceComposerClient::openGlobalTransaction();
437 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFlags(
438 layer_state_t::eLayerHidden, layer_state_t::eLayerHidden));
439 SurfaceComposerClient::closeGlobalTransaction(true);
440 {
441 // This should hide the foreground surface
442 SCOPED_TRACE("after setFlags");
443 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800444 sc->expectBGColor(24, 24);
445 sc->expectBGColor(75, 75);
446 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700447 }
448}
449
450TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
451 sp<ScreenCapture> sc;
452 {
453 SCOPED_TRACE("before setMatrix");
454 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800455 sc->expectBGColor(24, 24);
456 sc->expectFGColor(91, 96);
457 sc->expectFGColor(96, 101);
458 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700459 }
460
461 SurfaceComposerClient::openGlobalTransaction();
462 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setMatrix(M_SQRT1_2, M_SQRT1_2,
463 -M_SQRT1_2, M_SQRT1_2));
464 SurfaceComposerClient::closeGlobalTransaction(true);
465 {
466 SCOPED_TRACE("after setMatrix");
467 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800468 sc->expectBGColor(24, 24);
469 sc->expectFGColor(91, 96);
470 sc->expectBGColor(96, 91);
471 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700472 }
473}
474
Robert Carr8d5227b2017-03-16 15:41:03 -0700475class GeometryLatchingTest : public LayerUpdateTest {
476protected:
477 void EXPECT_INITIAL_STATE(const char * trace) {
478 SCOPED_TRACE(trace);
479 ScreenCapture::captureScreen(&sc);
480 // We find the leading edge of the FG surface.
481 sc->expectFGColor(127, 127);
482 sc->expectBGColor(128, 128);
483 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700484
485 void lockAndFillFGBuffer() {
486 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
487 }
488
489 void unlockFGBuffer() {
490 sp<Surface> s = mFGSurfaceControl->getSurface();
491 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
492 waitForPostedBuffers();
493 }
494
Robert Carr8d5227b2017-03-16 15:41:03 -0700495 void completeFGResize() {
496 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
497 waitForPostedBuffers();
498 }
499 void restoreInitialState() {
500 SurfaceComposerClient::openGlobalTransaction();
501 mFGSurfaceControl->setSize(64, 64);
502 mFGSurfaceControl->setPosition(64, 64);
503 mFGSurfaceControl->setCrop(Rect(0, 0, 64, 64));
504 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1));
505 SurfaceComposerClient::closeGlobalTransaction(true);
506
507 EXPECT_INITIAL_STATE("After restoring initial state");
508 }
509 sp<ScreenCapture> sc;
510};
511
512TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
513 EXPECT_INITIAL_STATE("before anything");
514
515 // By default position can be updated even while
516 // a resize is pending.
517 SurfaceComposerClient::openGlobalTransaction();
518 mFGSurfaceControl->setSize(32, 32);
519 mFGSurfaceControl->setPosition(100, 100);
520 SurfaceComposerClient::closeGlobalTransaction(true);
521
522 {
523 SCOPED_TRACE("After moving surface");
524 ScreenCapture::captureScreen(&sc);
525 // If we moved, the FG Surface should cover up what was previously BG
526 // however if we didn't move the FG wouldn't be large enough now.
527 sc->expectFGColor(163, 163);
528 }
529
530 restoreInitialState();
531
532 // Now we repeat with setGeometryAppliesWithResize
533 // and verify the position DOESN'T latch.
534 SurfaceComposerClient::openGlobalTransaction();
535 mFGSurfaceControl->setGeometryAppliesWithResize();
536 mFGSurfaceControl->setSize(32, 32);
537 mFGSurfaceControl->setPosition(100, 100);
538 SurfaceComposerClient::closeGlobalTransaction(true);
539
540 {
541 SCOPED_TRACE("While resize is pending");
542 ScreenCapture::captureScreen(&sc);
543 // This time we shouldn't have moved, so the BG color
544 // should still be visible.
545 sc->expectBGColor(128, 128);
546 }
547
548 completeFGResize();
549
550 {
551 SCOPED_TRACE("After the resize");
552 ScreenCapture::captureScreen(&sc);
553 // But after the resize completes, we should move
554 // and the FG should be visible here.
555 sc->expectFGColor(128, 128);
556 }
557}
558
559class CropLatchingTest : public GeometryLatchingTest {
560protected:
561 void EXPECT_CROPPED_STATE(const char* trace) {
562 SCOPED_TRACE(trace);
563 ScreenCapture::captureScreen(&sc);
564 // The edge should be moved back one pixel by our crop.
565 sc->expectFGColor(126, 126);
566 sc->expectBGColor(127, 127);
567 sc->expectBGColor(128, 128);
568 }
chaviw59f5c562017-06-28 16:39:06 -0700569
570 void EXPECT_RESIZE_STATE(const char* trace) {
571 SCOPED_TRACE(trace);
572 ScreenCapture::captureScreen(&sc);
573 // The FG is now resized too 128,128 at 64,64
574 sc->expectFGColor(64, 64);
575 sc->expectFGColor(191, 191);
576 sc->expectBGColor(192, 192);
577 }
Robert Carr8d5227b2017-03-16 15:41:03 -0700578};
579
580TEST_F(CropLatchingTest, CropLatching) {
581 EXPECT_INITIAL_STATE("before anything");
582 // Normally the crop applies immediately even while a resize is pending.
583 SurfaceComposerClient::openGlobalTransaction();
584 mFGSurfaceControl->setSize(128, 128);
585 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
586 SurfaceComposerClient::closeGlobalTransaction(true);
587
588 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
589
590 restoreInitialState();
591
592 SurfaceComposerClient::openGlobalTransaction();
593 mFGSurfaceControl->setSize(128, 128);
594 mFGSurfaceControl->setGeometryAppliesWithResize();
595 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
596 SurfaceComposerClient::closeGlobalTransaction(true);
597
598 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
599
600 completeFGResize();
601
602 EXPECT_CROPPED_STATE("after the resize finishes");
603}
604
605TEST_F(CropLatchingTest, FinalCropLatching) {
606 EXPECT_INITIAL_STATE("before anything");
607 // Normally the crop applies immediately even while a resize is pending.
608 SurfaceComposerClient::openGlobalTransaction();
609 mFGSurfaceControl->setSize(128, 128);
610 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
611 SurfaceComposerClient::closeGlobalTransaction(true);
612
613 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
614
615 restoreInitialState();
616
617 SurfaceComposerClient::openGlobalTransaction();
618 mFGSurfaceControl->setSize(128, 128);
619 mFGSurfaceControl->setGeometryAppliesWithResize();
620 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
621 SurfaceComposerClient::closeGlobalTransaction(true);
622
623 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
624
625 completeFGResize();
626
627 EXPECT_CROPPED_STATE("after the resize finishes");
628}
629
Robert Carr7bf247e2017-05-18 14:02:49 -0700630// In this test we ensure that setGeometryAppliesWithResize actually demands
631// a buffer of the new size, and not just any size.
632TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
633 EXPECT_INITIAL_STATE("before anything");
634 // Normally the crop applies immediately even while a resize is pending.
635 SurfaceComposerClient::openGlobalTransaction();
636 mFGSurfaceControl->setSize(128, 128);
637 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
638 SurfaceComposerClient::closeGlobalTransaction(true);
639
640 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
641
642 restoreInitialState();
643
644 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
645 // initiating the resize.
646 lockAndFillFGBuffer();
647
648 SurfaceComposerClient::openGlobalTransaction();
649 mFGSurfaceControl->setSize(128, 128);
650 mFGSurfaceControl->setGeometryAppliesWithResize();
651 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
652 SurfaceComposerClient::closeGlobalTransaction(true);
653
654 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
655
656 // We now submit our old buffer, at the old size, and ensure it doesn't
657 // trigger geometry latching.
658 unlockFGBuffer();
659
660 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
661
662 completeFGResize();
663
664 EXPECT_CROPPED_STATE("after the resize finishes");
665}
666
667TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
668 EXPECT_INITIAL_STATE("before anything");
669 // In this scenario, we attempt to set the final crop a second time while the resize
670 // is still pending, and ensure we are successful. Success meaning the second crop
671 // is the one which eventually latches and not the first.
672 SurfaceComposerClient::openGlobalTransaction();
673 mFGSurfaceControl->setSize(128, 128);
674 mFGSurfaceControl->setGeometryAppliesWithResize();
675 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
676 SurfaceComposerClient::closeGlobalTransaction(true);
677
chaviw59f5c562017-06-28 16:39:06 -0700678 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
679
Robert Carr7bf247e2017-05-18 14:02:49 -0700680 SurfaceComposerClient::openGlobalTransaction();
681 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1));
682 SurfaceComposerClient::closeGlobalTransaction(true);
683
chaviw59f5c562017-06-28 16:39:06 -0700684 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -0700685
686 completeFGResize();
687
chaviw59f5c562017-06-28 16:39:06 -0700688 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -0700689}
690
Pablo Ceballos05289c22016-04-14 15:49:55 -0700691TEST_F(LayerUpdateTest, DeferredTransactionTest) {
692 sp<ScreenCapture> sc;
693 {
694 SCOPED_TRACE("before anything");
695 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800696 sc->expectBGColor(32, 32);
697 sc->expectFGColor(96, 96);
698 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700699 }
700
701 // set up two deferred transactions on different frames
702 SurfaceComposerClient::openGlobalTransaction();
703 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75));
704 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
705 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
706 SurfaceComposerClient::closeGlobalTransaction(true);
707
708 SurfaceComposerClient::openGlobalTransaction();
709 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128));
710 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
711 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
712 SurfaceComposerClient::closeGlobalTransaction(true);
713
714 {
715 SCOPED_TRACE("before any trigger");
716 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800717 sc->expectBGColor(32, 32);
718 sc->expectFGColor(96, 96);
719 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700720 }
721
722 // should trigger the first deferred transaction, but not the second one
723 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
724 {
725 SCOPED_TRACE("after first trigger");
726 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800727 sc->expectBGColor(32, 32);
728 sc->checkPixel(96, 96, 162, 63, 96);
729 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700730 }
731
732 // should show up immediately since it's not deferred
733 SurfaceComposerClient::openGlobalTransaction();
734 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0));
735 SurfaceComposerClient::closeGlobalTransaction(true);
736
737 // trigger the second deferred transaction
738 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
739 {
740 SCOPED_TRACE("after second trigger");
741 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800742 sc->expectBGColor(32, 32);
743 sc->expectBGColor(96, 96);
744 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700745 }
746}
747
Robert Carrdb66e622017-04-10 16:55:57 -0700748TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
749 sp<ScreenCapture> sc;
750 {
751 SCOPED_TRACE("before adding relative surface");
752 ScreenCapture::captureScreen(&sc);
753 sc->expectBGColor(24, 24);
754 sc->expectFGColor(75, 75);
755 sc->expectBGColor(145, 145);
756 }
757
758 auto relativeSurfaceControl = mComposerClient->createSurface(
759 String8("Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
760 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
761 waitForPostedBuffers();
762
763 // Now we stack the surface above the foreground surface and make sure it is visible.
764 SurfaceComposerClient::openGlobalTransaction();
765 relativeSurfaceControl->setPosition(64, 64);
766 relativeSurfaceControl->show();
767 relativeSurfaceControl->setRelativeLayer(mFGSurfaceControl->getHandle(), 1);
768 SurfaceComposerClient::closeGlobalTransaction(true);
769
770
771 {
772 SCOPED_TRACE("after adding relative surface");
773 ScreenCapture::captureScreen(&sc);
774 // our relative surface should be visible now.
775 sc->checkPixel(75, 75, 255, 177, 177);
776 }
777
778 // A call to setLayer will override a call to setRelativeLayer
779 SurfaceComposerClient::openGlobalTransaction();
780 relativeSurfaceControl->setLayer(0);
781 SurfaceComposerClient::closeGlobalTransaction();
782
783 {
784 SCOPED_TRACE("after set layer");
785 ScreenCapture::captureScreen(&sc);
786 // now the FG surface should be visible again.
787 sc->expectFGColor(75, 75);
788 }
789}
790
Robert Carr1f0a16a2016-10-24 16:27:39 -0700791class ChildLayerTest : public LayerUpdateTest {
792protected:
793 void SetUp() override {
794 LayerUpdateTest::SetUp();
795 mChild = mComposerClient->createSurface(
796 String8("Child surface"),
797 10, 10, PIXEL_FORMAT_RGBA_8888,
798 0, mFGSurfaceControl.get());
799 fillSurfaceRGBA8(mChild, 200, 200, 200);
800
801 {
802 SCOPED_TRACE("before anything");
803 ScreenCapture::captureScreen(&mCapture);
804 mCapture->expectChildColor(64, 64);
805 }
806 }
807 void TearDown() override {
808 LayerUpdateTest::TearDown();
809 mChild = 0;
810 }
811
812 sp<SurfaceControl> mChild;
813 sp<ScreenCapture> mCapture;
814};
815
816TEST_F(ChildLayerTest, ChildLayerPositioning) {
817 SurfaceComposerClient::openGlobalTransaction();
818 mChild->show();
819 mChild->setPosition(10, 10);
820 mFGSurfaceControl->setPosition(64, 64);
821 SurfaceComposerClient::closeGlobalTransaction(true);
822
823 {
824 ScreenCapture::captureScreen(&mCapture);
825 // Top left of foreground must now be visible
826 mCapture->expectFGColor(64, 64);
827 // But 10 pixels in we should see the child surface
828 mCapture->expectChildColor(74, 74);
829 // And 10 more pixels we should be back to the foreground surface
830 mCapture->expectFGColor(84, 84);
831 }
832
833 SurfaceComposerClient::openGlobalTransaction();
834 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0));
835 SurfaceComposerClient::closeGlobalTransaction(true);
836
837 {
838 ScreenCapture::captureScreen(&mCapture);
839 // Top left of foreground should now be at 0, 0
840 mCapture->expectFGColor(0, 0);
841 // But 10 pixels in we should see the child surface
842 mCapture->expectChildColor(10, 10);
843 // And 10 more pixels we should be back to the foreground surface
844 mCapture->expectFGColor(20, 20);
845 }
846}
847
Robert Carr41b08b52017-06-01 16:11:34 -0700848TEST_F(ChildLayerTest, ChildLayerCropping) {
849 SurfaceComposerClient::openGlobalTransaction();
850 mChild->show();
851 mChild->setPosition(0, 0);
852 mFGSurfaceControl->setPosition(0, 0);
853 mFGSurfaceControl->setCrop(Rect(0, 0, 5, 5));
854 SurfaceComposerClient::closeGlobalTransaction(true);
855
856 {
857 ScreenCapture::captureScreen(&mCapture);
858 mCapture->expectChildColor(0, 0);
859 mCapture->expectChildColor(4, 4);
860 mCapture->expectBGColor(5, 5);
861 }
862}
863
864TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
865 SurfaceComposerClient::openGlobalTransaction();
866 mChild->show();
867 mChild->setPosition(0, 0);
868 mFGSurfaceControl->setPosition(0, 0);
869 mFGSurfaceControl->setFinalCrop(Rect(0, 0, 5, 5));
870 SurfaceComposerClient::closeGlobalTransaction(true);
871
872 {
873 ScreenCapture::captureScreen(&mCapture);
874 mCapture->expectChildColor(0, 0);
875 mCapture->expectChildColor(4, 4);
876 mCapture->expectBGColor(5, 5);
877 }
878}
879
Robert Carr1f0a16a2016-10-24 16:27:39 -0700880TEST_F(ChildLayerTest, ChildLayerConstraints) {
881 SurfaceComposerClient::openGlobalTransaction();
882 mChild->show();
883 mFGSurfaceControl->setPosition(0, 0);
884 mChild->setPosition(63, 63);
885 SurfaceComposerClient::closeGlobalTransaction(true);
886
887 {
888 ScreenCapture::captureScreen(&mCapture);
889 mCapture->expectFGColor(0, 0);
890 // Last pixel in foreground should now be the child.
891 mCapture->expectChildColor(63, 63);
892 // But the child should be constrained and the next pixel
893 // must be the background
894 mCapture->expectBGColor(64, 64);
895 }
896}
897
898TEST_F(ChildLayerTest, ChildLayerScaling) {
899 SurfaceComposerClient::openGlobalTransaction();
900 mFGSurfaceControl->setPosition(0, 0);
901 SurfaceComposerClient::closeGlobalTransaction(true);
902
903 // Find the boundary between the parent and child
904 {
905 ScreenCapture::captureScreen(&mCapture);
906 mCapture->expectChildColor(9, 9);
907 mCapture->expectFGColor(10, 10);
908 }
909
910 SurfaceComposerClient::openGlobalTransaction();
911 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0);
912 SurfaceComposerClient::closeGlobalTransaction(true);
913
914 // The boundary should be twice as far from the origin now.
915 // The pixels from the last test should all be child now
916 {
917 ScreenCapture::captureScreen(&mCapture);
918 mCapture->expectChildColor(9, 9);
919 mCapture->expectChildColor(10, 10);
920 mCapture->expectChildColor(19, 19);
921 mCapture->expectFGColor(20, 20);
922 }
923}
Robert Carr9524cb32017-02-13 11:32:32 -0800924
Robert Carr6452f122017-03-21 10:41:29 -0700925TEST_F(ChildLayerTest, ChildLayerAlpha) {
926 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
927 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
928 fillSurfaceRGBA8(mChild, 0, 254, 0);
929 waitForPostedBuffers();
930
931 SurfaceComposerClient::openGlobalTransaction();
932 mChild->show();
933 mChild->setPosition(0, 0);
934 mFGSurfaceControl->setPosition(0, 0);
935 SurfaceComposerClient::closeGlobalTransaction(true);
936
937 {
938 ScreenCapture::captureScreen(&mCapture);
939 // Unblended child color
940 mCapture->checkPixel(0, 0, 0, 254, 0);
941 }
942
943 SurfaceComposerClient::openGlobalTransaction();
944 ASSERT_EQ(NO_ERROR, mChild->setAlpha(0.5));
945 SurfaceComposerClient::closeGlobalTransaction(true);
946
947 {
948 ScreenCapture::captureScreen(&mCapture);
949 // Child and BG blended.
950 mCapture->checkPixel(0, 0, 127, 127, 0);
951 }
952
953 SurfaceComposerClient::openGlobalTransaction();
954 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.5));
955 SurfaceComposerClient::closeGlobalTransaction(true);
956
957 {
958 ScreenCapture::captureScreen(&mCapture);
959 // Child and BG blended.
960 mCapture->checkPixel(0, 0, 95, 64, 95);
961 }
962}
963
Robert Carr9524cb32017-02-13 11:32:32 -0800964TEST_F(ChildLayerTest, ReparentChildren) {
965 SurfaceComposerClient::openGlobalTransaction();
966 mChild->show();
967 mChild->setPosition(10, 10);
968 mFGSurfaceControl->setPosition(64, 64);
969 SurfaceComposerClient::closeGlobalTransaction(true);
970
971 {
972 ScreenCapture::captureScreen(&mCapture);
973 // Top left of foreground must now be visible
974 mCapture->expectFGColor(64, 64);
975 // But 10 pixels in we should see the child surface
976 mCapture->expectChildColor(74, 74);
977 // And 10 more pixels we should be back to the foreground surface
978 mCapture->expectFGColor(84, 84);
979 }
980 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle());
981 {
982 ScreenCapture::captureScreen(&mCapture);
983 mCapture->expectFGColor(64, 64);
984 // In reparenting we should have exposed the entire foreground surface.
985 mCapture->expectFGColor(74, 74);
986 // And the child layer should now begin at 10, 10 (since the BG
987 // layer is at (0, 0)).
988 mCapture->expectBGColor(9, 9);
989 mCapture->expectChildColor(10, 10);
990 }
991}
992
993TEST_F(ChildLayerTest, DetachChildren) {
994 SurfaceComposerClient::openGlobalTransaction();
995 mChild->show();
996 mChild->setPosition(10, 10);
997 mFGSurfaceControl->setPosition(64, 64);
998 SurfaceComposerClient::closeGlobalTransaction(true);
999
1000 {
1001 ScreenCapture::captureScreen(&mCapture);
1002 // Top left of foreground must now be visible
1003 mCapture->expectFGColor(64, 64);
1004 // But 10 pixels in we should see the child surface
1005 mCapture->expectChildColor(74, 74);
1006 // And 10 more pixels we should be back to the foreground surface
1007 mCapture->expectFGColor(84, 84);
1008 }
1009
1010 SurfaceComposerClient::openGlobalTransaction();
1011 mFGSurfaceControl->detachChildren();
Robert Carr8d5227b2017-03-16 15:41:03 -07001012 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -08001013
1014 SurfaceComposerClient::openGlobalTransaction();
1015 mChild->hide();
Robert Carr8d5227b2017-03-16 15:41:03 -07001016 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -08001017
1018 // Nothing should have changed.
1019 {
1020 ScreenCapture::captureScreen(&mCapture);
1021 mCapture->expectFGColor(64, 64);
1022 mCapture->expectChildColor(74, 74);
1023 mCapture->expectFGColor(84, 84);
1024 }
1025}
1026
Robert Carr9b429f42017-04-17 14:56:57 -07001027TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
1028 SurfaceComposerClient::openGlobalTransaction();
1029 mChild->show();
1030 mChild->setPosition(0, 0);
1031 mFGSurfaceControl->setPosition(0, 0);
1032 SurfaceComposerClient::closeGlobalTransaction(true);
1033
1034 {
1035 ScreenCapture::captureScreen(&mCapture);
1036 // We've positioned the child in the top left.
1037 mCapture->expectChildColor(0, 0);
1038 // But it's only 10x10.
1039 mCapture->expectFGColor(10, 10);
1040 }
1041
1042 SurfaceComposerClient::openGlobalTransaction();
1043 mFGSurfaceControl->setOverrideScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1044 // We cause scaling by 2.
1045 mFGSurfaceControl->setSize(128, 128);
1046 SurfaceComposerClient::closeGlobalTransaction();
1047
1048 {
1049 ScreenCapture::captureScreen(&mCapture);
1050 // We've positioned the child in the top left.
1051 mCapture->expectChildColor(0, 0);
1052 mCapture->expectChildColor(10, 10);
1053 mCapture->expectChildColor(19, 19);
1054 // And now it should be scaled all the way to 20x20
1055 mCapture->expectFGColor(20, 20);
1056 }
1057}
1058
Robert Carr1725eee2017-04-26 18:32:15 -07001059// Regression test for b/37673612
1060TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
1061 SurfaceComposerClient::openGlobalTransaction();
1062 mChild->show();
1063 mChild->setPosition(0, 0);
1064 mFGSurfaceControl->setPosition(0, 0);
1065 SurfaceComposerClient::closeGlobalTransaction(true);
1066
1067 {
1068 ScreenCapture::captureScreen(&mCapture);
1069 // We've positioned the child in the top left.
1070 mCapture->expectChildColor(0, 0);
1071 // But it's only 10x10.
1072 mCapture->expectFGColor(10, 10);
1073 }
1074
1075
1076 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1077 // the WM specified state size.
1078 mFGSurfaceControl->setSize(128, 64);
1079 sp<Surface> s = mFGSurfaceControl->getSurface();
1080 auto anw = static_cast<ANativeWindow*>(s.get());
1081 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1082 native_window_set_buffers_dimensions(anw, 64, 128);
1083 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1084 waitForPostedBuffers();
1085
1086 {
1087 // The child should still be in the same place and not have any strange scaling as in
1088 // b/37673612.
1089 ScreenCapture::captureScreen(&mCapture);
1090 mCapture->expectChildColor(0, 0);
1091 mCapture->expectFGColor(10, 10);
1092 }
1093}
1094
Dan Stoza412903f2017-04-27 13:42:17 -07001095TEST_F(ChildLayerTest, Bug36858924) {
1096 // Destroy the child layer
1097 mChild.clear();
1098
1099 // Now recreate it as hidden
1100 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1101 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1102 mFGSurfaceControl.get());
1103
1104 // Show the child layer in a deferred transaction
1105 SurfaceComposerClient::openGlobalTransaction();
1106 mChild->deferTransactionUntil(mFGSurfaceControl->getHandle(),
1107 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1108 mChild->show();
1109 SurfaceComposerClient::closeGlobalTransaction(true);
1110
1111 // Render the foreground surface a few times
1112 //
1113 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1114 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1115 // never acquire/release the first buffer
1116 ALOGI("Filling 1");
1117 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1118 ALOGI("Filling 2");
1119 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1120 ALOGI("Filling 3");
1121 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1122 ALOGI("Filling 4");
1123 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1124}
1125
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001126}