blob: 68519a1ed2338229934d66821c84a1eb1908a22e [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 }
569};
570
571TEST_F(CropLatchingTest, CropLatching) {
572 EXPECT_INITIAL_STATE("before anything");
573 // Normally the crop applies immediately even while a resize is pending.
574 SurfaceComposerClient::openGlobalTransaction();
575 mFGSurfaceControl->setSize(128, 128);
576 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
577 SurfaceComposerClient::closeGlobalTransaction(true);
578
579 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
580
581 restoreInitialState();
582
583 SurfaceComposerClient::openGlobalTransaction();
584 mFGSurfaceControl->setSize(128, 128);
585 mFGSurfaceControl->setGeometryAppliesWithResize();
586 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
587 SurfaceComposerClient::closeGlobalTransaction(true);
588
589 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
590
591 completeFGResize();
592
593 EXPECT_CROPPED_STATE("after the resize finishes");
594}
595
596TEST_F(CropLatchingTest, FinalCropLatching) {
597 EXPECT_INITIAL_STATE("before anything");
598 // Normally the crop applies immediately even while a resize is pending.
599 SurfaceComposerClient::openGlobalTransaction();
600 mFGSurfaceControl->setSize(128, 128);
601 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
602 SurfaceComposerClient::closeGlobalTransaction(true);
603
604 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
605
606 restoreInitialState();
607
608 SurfaceComposerClient::openGlobalTransaction();
609 mFGSurfaceControl->setSize(128, 128);
610 mFGSurfaceControl->setGeometryAppliesWithResize();
611 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
612 SurfaceComposerClient::closeGlobalTransaction(true);
613
614 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
615
616 completeFGResize();
617
618 EXPECT_CROPPED_STATE("after the resize finishes");
619}
620
Robert Carr7bf247e2017-05-18 14:02:49 -0700621// In this test we ensure that setGeometryAppliesWithResize actually demands
622// a buffer of the new size, and not just any size.
623TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
624 EXPECT_INITIAL_STATE("before anything");
625 // Normally the crop applies immediately even while a resize is pending.
626 SurfaceComposerClient::openGlobalTransaction();
627 mFGSurfaceControl->setSize(128, 128);
628 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
629 SurfaceComposerClient::closeGlobalTransaction(true);
630
631 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
632
633 restoreInitialState();
634
635 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
636 // initiating the resize.
637 lockAndFillFGBuffer();
638
639 SurfaceComposerClient::openGlobalTransaction();
640 mFGSurfaceControl->setSize(128, 128);
641 mFGSurfaceControl->setGeometryAppliesWithResize();
642 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
643 SurfaceComposerClient::closeGlobalTransaction(true);
644
645 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
646
647 // We now submit our old buffer, at the old size, and ensure it doesn't
648 // trigger geometry latching.
649 unlockFGBuffer();
650
651 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
652
653 completeFGResize();
654
655 EXPECT_CROPPED_STATE("after the resize finishes");
656}
657
658TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
659 EXPECT_INITIAL_STATE("before anything");
660 // In this scenario, we attempt to set the final crop a second time while the resize
661 // is still pending, and ensure we are successful. Success meaning the second crop
662 // is the one which eventually latches and not the first.
663 SurfaceComposerClient::openGlobalTransaction();
664 mFGSurfaceControl->setSize(128, 128);
665 mFGSurfaceControl->setGeometryAppliesWithResize();
666 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
667 SurfaceComposerClient::closeGlobalTransaction(true);
668
669 SurfaceComposerClient::openGlobalTransaction();
670 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1));
671 SurfaceComposerClient::closeGlobalTransaction(true);
672
673 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
674
675 completeFGResize();
676
677 EXPECT_INITIAL_STATE("after the resize finishes");
678}
679
Pablo Ceballos05289c22016-04-14 15:49:55 -0700680TEST_F(LayerUpdateTest, DeferredTransactionTest) {
681 sp<ScreenCapture> sc;
682 {
683 SCOPED_TRACE("before anything");
684 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800685 sc->expectBGColor(32, 32);
686 sc->expectFGColor(96, 96);
687 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700688 }
689
690 // set up two deferred transactions on different frames
691 SurfaceComposerClient::openGlobalTransaction();
692 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75));
693 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
694 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
695 SurfaceComposerClient::closeGlobalTransaction(true);
696
697 SurfaceComposerClient::openGlobalTransaction();
698 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128));
699 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
700 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
701 SurfaceComposerClient::closeGlobalTransaction(true);
702
703 {
704 SCOPED_TRACE("before any trigger");
705 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800706 sc->expectBGColor(32, 32);
707 sc->expectFGColor(96, 96);
708 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700709 }
710
711 // should trigger the first deferred transaction, but not the second one
712 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
713 {
714 SCOPED_TRACE("after first trigger");
715 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800716 sc->expectBGColor(32, 32);
717 sc->checkPixel(96, 96, 162, 63, 96);
718 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700719 }
720
721 // should show up immediately since it's not deferred
722 SurfaceComposerClient::openGlobalTransaction();
723 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0));
724 SurfaceComposerClient::closeGlobalTransaction(true);
725
726 // trigger the second deferred transaction
727 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
728 {
729 SCOPED_TRACE("after second trigger");
730 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800731 sc->expectBGColor(32, 32);
732 sc->expectBGColor(96, 96);
733 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700734 }
735}
736
Robert Carrdb66e622017-04-10 16:55:57 -0700737TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
738 sp<ScreenCapture> sc;
739 {
740 SCOPED_TRACE("before adding relative surface");
741 ScreenCapture::captureScreen(&sc);
742 sc->expectBGColor(24, 24);
743 sc->expectFGColor(75, 75);
744 sc->expectBGColor(145, 145);
745 }
746
747 auto relativeSurfaceControl = mComposerClient->createSurface(
748 String8("Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
749 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
750 waitForPostedBuffers();
751
752 // Now we stack the surface above the foreground surface and make sure it is visible.
753 SurfaceComposerClient::openGlobalTransaction();
754 relativeSurfaceControl->setPosition(64, 64);
755 relativeSurfaceControl->show();
756 relativeSurfaceControl->setRelativeLayer(mFGSurfaceControl->getHandle(), 1);
757 SurfaceComposerClient::closeGlobalTransaction(true);
758
759
760 {
761 SCOPED_TRACE("after adding relative surface");
762 ScreenCapture::captureScreen(&sc);
763 // our relative surface should be visible now.
764 sc->checkPixel(75, 75, 255, 177, 177);
765 }
766
767 // A call to setLayer will override a call to setRelativeLayer
768 SurfaceComposerClient::openGlobalTransaction();
769 relativeSurfaceControl->setLayer(0);
770 SurfaceComposerClient::closeGlobalTransaction();
771
772 {
773 SCOPED_TRACE("after set layer");
774 ScreenCapture::captureScreen(&sc);
775 // now the FG surface should be visible again.
776 sc->expectFGColor(75, 75);
777 }
778}
779
Robert Carr1f0a16a2016-10-24 16:27:39 -0700780class ChildLayerTest : public LayerUpdateTest {
781protected:
782 void SetUp() override {
783 LayerUpdateTest::SetUp();
784 mChild = mComposerClient->createSurface(
785 String8("Child surface"),
786 10, 10, PIXEL_FORMAT_RGBA_8888,
787 0, mFGSurfaceControl.get());
788 fillSurfaceRGBA8(mChild, 200, 200, 200);
789
790 {
791 SCOPED_TRACE("before anything");
792 ScreenCapture::captureScreen(&mCapture);
793 mCapture->expectChildColor(64, 64);
794 }
795 }
796 void TearDown() override {
797 LayerUpdateTest::TearDown();
798 mChild = 0;
799 }
800
801 sp<SurfaceControl> mChild;
802 sp<ScreenCapture> mCapture;
803};
804
805TEST_F(ChildLayerTest, ChildLayerPositioning) {
806 SurfaceComposerClient::openGlobalTransaction();
807 mChild->show();
808 mChild->setPosition(10, 10);
809 mFGSurfaceControl->setPosition(64, 64);
810 SurfaceComposerClient::closeGlobalTransaction(true);
811
812 {
813 ScreenCapture::captureScreen(&mCapture);
814 // Top left of foreground must now be visible
815 mCapture->expectFGColor(64, 64);
816 // But 10 pixels in we should see the child surface
817 mCapture->expectChildColor(74, 74);
818 // And 10 more pixels we should be back to the foreground surface
819 mCapture->expectFGColor(84, 84);
820 }
821
822 SurfaceComposerClient::openGlobalTransaction();
823 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0));
824 SurfaceComposerClient::closeGlobalTransaction(true);
825
826 {
827 ScreenCapture::captureScreen(&mCapture);
828 // Top left of foreground should now be at 0, 0
829 mCapture->expectFGColor(0, 0);
830 // But 10 pixels in we should see the child surface
831 mCapture->expectChildColor(10, 10);
832 // And 10 more pixels we should be back to the foreground surface
833 mCapture->expectFGColor(20, 20);
834 }
835}
836
837TEST_F(ChildLayerTest, ChildLayerConstraints) {
838 SurfaceComposerClient::openGlobalTransaction();
839 mChild->show();
840 mFGSurfaceControl->setPosition(0, 0);
841 mChild->setPosition(63, 63);
842 SurfaceComposerClient::closeGlobalTransaction(true);
843
844 {
845 ScreenCapture::captureScreen(&mCapture);
846 mCapture->expectFGColor(0, 0);
847 // Last pixel in foreground should now be the child.
848 mCapture->expectChildColor(63, 63);
849 // But the child should be constrained and the next pixel
850 // must be the background
851 mCapture->expectBGColor(64, 64);
852 }
853}
854
855TEST_F(ChildLayerTest, ChildLayerScaling) {
856 SurfaceComposerClient::openGlobalTransaction();
857 mFGSurfaceControl->setPosition(0, 0);
858 SurfaceComposerClient::closeGlobalTransaction(true);
859
860 // Find the boundary between the parent and child
861 {
862 ScreenCapture::captureScreen(&mCapture);
863 mCapture->expectChildColor(9, 9);
864 mCapture->expectFGColor(10, 10);
865 }
866
867 SurfaceComposerClient::openGlobalTransaction();
868 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0);
869 SurfaceComposerClient::closeGlobalTransaction(true);
870
871 // The boundary should be twice as far from the origin now.
872 // The pixels from the last test should all be child now
873 {
874 ScreenCapture::captureScreen(&mCapture);
875 mCapture->expectChildColor(9, 9);
876 mCapture->expectChildColor(10, 10);
877 mCapture->expectChildColor(19, 19);
878 mCapture->expectFGColor(20, 20);
879 }
880}
Robert Carr9524cb32017-02-13 11:32:32 -0800881
Robert Carr6452f122017-03-21 10:41:29 -0700882TEST_F(ChildLayerTest, ChildLayerAlpha) {
883 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
884 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
885 fillSurfaceRGBA8(mChild, 0, 254, 0);
886 waitForPostedBuffers();
887
888 SurfaceComposerClient::openGlobalTransaction();
889 mChild->show();
890 mChild->setPosition(0, 0);
891 mFGSurfaceControl->setPosition(0, 0);
892 SurfaceComposerClient::closeGlobalTransaction(true);
893
894 {
895 ScreenCapture::captureScreen(&mCapture);
896 // Unblended child color
897 mCapture->checkPixel(0, 0, 0, 254, 0);
898 }
899
900 SurfaceComposerClient::openGlobalTransaction();
901 ASSERT_EQ(NO_ERROR, mChild->setAlpha(0.5));
902 SurfaceComposerClient::closeGlobalTransaction(true);
903
904 {
905 ScreenCapture::captureScreen(&mCapture);
906 // Child and BG blended.
907 mCapture->checkPixel(0, 0, 127, 127, 0);
908 }
909
910 SurfaceComposerClient::openGlobalTransaction();
911 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.5));
912 SurfaceComposerClient::closeGlobalTransaction(true);
913
914 {
915 ScreenCapture::captureScreen(&mCapture);
916 // Child and BG blended.
917 mCapture->checkPixel(0, 0, 95, 64, 95);
918 }
919}
920
Robert Carr9524cb32017-02-13 11:32:32 -0800921TEST_F(ChildLayerTest, ReparentChildren) {
922 SurfaceComposerClient::openGlobalTransaction();
923 mChild->show();
924 mChild->setPosition(10, 10);
925 mFGSurfaceControl->setPosition(64, 64);
926 SurfaceComposerClient::closeGlobalTransaction(true);
927
928 {
929 ScreenCapture::captureScreen(&mCapture);
930 // Top left of foreground must now be visible
931 mCapture->expectFGColor(64, 64);
932 // But 10 pixels in we should see the child surface
933 mCapture->expectChildColor(74, 74);
934 // And 10 more pixels we should be back to the foreground surface
935 mCapture->expectFGColor(84, 84);
936 }
937 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle());
938 {
939 ScreenCapture::captureScreen(&mCapture);
940 mCapture->expectFGColor(64, 64);
941 // In reparenting we should have exposed the entire foreground surface.
942 mCapture->expectFGColor(74, 74);
943 // And the child layer should now begin at 10, 10 (since the BG
944 // layer is at (0, 0)).
945 mCapture->expectBGColor(9, 9);
946 mCapture->expectChildColor(10, 10);
947 }
948}
949
950TEST_F(ChildLayerTest, DetachChildren) {
951 SurfaceComposerClient::openGlobalTransaction();
952 mChild->show();
953 mChild->setPosition(10, 10);
954 mFGSurfaceControl->setPosition(64, 64);
955 SurfaceComposerClient::closeGlobalTransaction(true);
956
957 {
958 ScreenCapture::captureScreen(&mCapture);
959 // Top left of foreground must now be visible
960 mCapture->expectFGColor(64, 64);
961 // But 10 pixels in we should see the child surface
962 mCapture->expectChildColor(74, 74);
963 // And 10 more pixels we should be back to the foreground surface
964 mCapture->expectFGColor(84, 84);
965 }
966
967 SurfaceComposerClient::openGlobalTransaction();
968 mFGSurfaceControl->detachChildren();
Robert Carr8d5227b2017-03-16 15:41:03 -0700969 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -0800970
971 SurfaceComposerClient::openGlobalTransaction();
972 mChild->hide();
Robert Carr8d5227b2017-03-16 15:41:03 -0700973 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -0800974
975 // Nothing should have changed.
976 {
977 ScreenCapture::captureScreen(&mCapture);
978 mCapture->expectFGColor(64, 64);
979 mCapture->expectChildColor(74, 74);
980 mCapture->expectFGColor(84, 84);
981 }
982}
983
Robert Carr9b429f42017-04-17 14:56:57 -0700984TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
985 SurfaceComposerClient::openGlobalTransaction();
986 mChild->show();
987 mChild->setPosition(0, 0);
988 mFGSurfaceControl->setPosition(0, 0);
989 SurfaceComposerClient::closeGlobalTransaction(true);
990
991 {
992 ScreenCapture::captureScreen(&mCapture);
993 // We've positioned the child in the top left.
994 mCapture->expectChildColor(0, 0);
995 // But it's only 10x10.
996 mCapture->expectFGColor(10, 10);
997 }
998
999 SurfaceComposerClient::openGlobalTransaction();
1000 mFGSurfaceControl->setOverrideScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1001 // We cause scaling by 2.
1002 mFGSurfaceControl->setSize(128, 128);
1003 SurfaceComposerClient::closeGlobalTransaction();
1004
1005 {
1006 ScreenCapture::captureScreen(&mCapture);
1007 // We've positioned the child in the top left.
1008 mCapture->expectChildColor(0, 0);
1009 mCapture->expectChildColor(10, 10);
1010 mCapture->expectChildColor(19, 19);
1011 // And now it should be scaled all the way to 20x20
1012 mCapture->expectFGColor(20, 20);
1013 }
1014}
1015
Robert Carr1725eee2017-04-26 18:32:15 -07001016// Regression test for b/37673612
1017TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
1018 SurfaceComposerClient::openGlobalTransaction();
1019 mChild->show();
1020 mChild->setPosition(0, 0);
1021 mFGSurfaceControl->setPosition(0, 0);
1022 SurfaceComposerClient::closeGlobalTransaction(true);
1023
1024 {
1025 ScreenCapture::captureScreen(&mCapture);
1026 // We've positioned the child in the top left.
1027 mCapture->expectChildColor(0, 0);
1028 // But it's only 10x10.
1029 mCapture->expectFGColor(10, 10);
1030 }
1031
1032
1033 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1034 // the WM specified state size.
1035 mFGSurfaceControl->setSize(128, 64);
1036 sp<Surface> s = mFGSurfaceControl->getSurface();
1037 auto anw = static_cast<ANativeWindow*>(s.get());
1038 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1039 native_window_set_buffers_dimensions(anw, 64, 128);
1040 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1041 waitForPostedBuffers();
1042
1043 {
1044 // The child should still be in the same place and not have any strange scaling as in
1045 // b/37673612.
1046 ScreenCapture::captureScreen(&mCapture);
1047 mCapture->expectChildColor(0, 0);
1048 mCapture->expectFGColor(10, 10);
1049 }
1050}
1051
Dan Stoza412903f2017-04-27 13:42:17 -07001052TEST_F(ChildLayerTest, Bug36858924) {
1053 // Destroy the child layer
1054 mChild.clear();
1055
1056 // Now recreate it as hidden
1057 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1058 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1059 mFGSurfaceControl.get());
1060
1061 // Show the child layer in a deferred transaction
1062 SurfaceComposerClient::openGlobalTransaction();
1063 mChild->deferTransactionUntil(mFGSurfaceControl->getHandle(),
1064 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1065 mChild->show();
1066 SurfaceComposerClient::closeGlobalTransaction(true);
1067
1068 // Render the foreground surface a few times
1069 //
1070 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1071 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1072 // never acquire/release the first buffer
1073 ALOGI("Filling 1");
1074 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1075 ALOGI("Filling 2");
1076 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1077 ALOGI("Filling 3");
1078 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1079 ALOGI("Filling 4");
1080 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1081}
1082
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001083}