blob: a46ba48026772db79cfbb49e2daf46df8d88769e [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,
36 uint8_t r, uint8_t g, uint8_t b) {
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 }
51 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
52}
53
54// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
55// individual pixel values for testing purposes.
56class ScreenCapture : public RefBase {
57public:
58 static void captureScreen(sp<ScreenCapture>* sc) {
Michael Lentine5a16a622015-05-21 13:48:24 -070059 sp<IGraphicBufferProducer> producer;
60 sp<IGraphicBufferConsumer> consumer;
61 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -070062 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070063 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Michael Lentine5a16a622015-05-21 13:48:24 -070064 sp<IBinder> display(sf->getBuiltInDisplay(
65 ISurfaceComposer::eDisplayIdMain));
Pablo Ceballos15311bd2016-06-01 18:53:40 -070066 SurfaceComposerClient::openGlobalTransaction();
67 SurfaceComposerClient::closeGlobalTransaction(true);
Michael Lentine5a16a622015-05-21 13:48:24 -070068 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0,
69 0, INT_MAX, false));
70 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070071 }
72
73 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -070074 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
75 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
76 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070077 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
78 String8 err(String8::format("pixel @ (%3d, %3d): "
79 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
80 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070081 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070082 }
83 }
84
Robert Carr1f0a16a2016-10-24 16:27:39 -070085 void expectFGColor(uint32_t x, uint32_t y) {
86 checkPixel(x, y, 195, 63, 63);
87 }
88
89 void expectBGColor(uint32_t x, uint32_t y) {
90 checkPixel(x, y, 63, 63, 195);
91 }
92
93 void expectChildColor(uint32_t x, uint32_t y) {
94 checkPixel(x, y, 200, 200, 200);
95 }
96
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070097private:
Michael Lentine5a16a622015-05-21 13:48:24 -070098 ScreenCapture(const sp<CpuConsumer>& cc) :
99 mCC(cc) {
100 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
101 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700102
Michael Lentine5a16a622015-05-21 13:48:24 -0700103 ~ScreenCapture() {
104 mCC->unlockBuffer(mBuf);
105 }
106
107 sp<CpuConsumer> mCC;
108 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700109};
110
111class LayerUpdateTest : public ::testing::Test {
112protected:
113 virtual void SetUp() {
114 mComposerClient = new SurfaceComposerClient;
115 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
116
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700117 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
118 ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700119 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700120 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700121
122 ssize_t displayWidth = info.w;
123 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700124
125 // Background surface
126 mBGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700127 String8("BG Test Surface"), displayWidth, displayHeight,
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700128 PIXEL_FORMAT_RGBA_8888, 0);
129 ASSERT_TRUE(mBGSurfaceControl != NULL);
130 ASSERT_TRUE(mBGSurfaceControl->isValid());
131 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
132
133 // Foreground surface
134 mFGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700135 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700136 ASSERT_TRUE(mFGSurfaceControl != NULL);
137 ASSERT_TRUE(mFGSurfaceControl->isValid());
138
139 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
140
141 // Synchronization surface
142 mSyncSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700143 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700144 ASSERT_TRUE(mSyncSurfaceControl != NULL);
145 ASSERT_TRUE(mSyncSurfaceControl->isValid());
146
147 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
148
149 SurfaceComposerClient::openGlobalTransaction();
150
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700151 mComposerClient->setDisplayLayerStack(display, 0);
152
Robert Carr1f0a16a2016-10-24 16:27:39 -0700153 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT32_MAX-2));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700154 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show());
155
Robert Carr1f0a16a2016-10-24 16:27:39 -0700156 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700157 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64));
158 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
159
Robert Carr1f0a16a2016-10-24 16:27:39 -0700160 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700161 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setPosition(displayWidth-2,
162 displayHeight-2));
163 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->show());
164
165 SurfaceComposerClient::closeGlobalTransaction(true);
166 }
167
168 virtual void TearDown() {
169 mComposerClient->dispose();
170 mBGSurfaceControl = 0;
171 mFGSurfaceControl = 0;
172 mSyncSurfaceControl = 0;
173 mComposerClient = 0;
174 }
175
176 void waitForPostedBuffers() {
177 // Since the sync surface is in synchronous mode (i.e. double buffered)
178 // posting three buffers to it should ensure that at least two
179 // SurfaceFlinger::handlePageFlip calls have been made, which should
180 // guaranteed that a buffer posted to another Surface has been retired.
181 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
182 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
183 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
184 }
185
186 sp<SurfaceComposerClient> mComposerClient;
187 sp<SurfaceControl> mBGSurfaceControl;
188 sp<SurfaceControl> mFGSurfaceControl;
189
190 // This surface is used to ensure that the buffers posted to
191 // mFGSurfaceControl have been picked up by SurfaceFlinger.
192 sp<SurfaceControl> mSyncSurfaceControl;
193};
194
195TEST_F(LayerUpdateTest, LayerMoveWorks) {
196 sp<ScreenCapture> sc;
197 {
198 SCOPED_TRACE("before move");
199 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800200 sc->expectBGColor(0, 12);
201 sc->expectFGColor(75, 75);
202 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700203 }
204
205 SurfaceComposerClient::openGlobalTransaction();
206 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128, 128));
207 SurfaceComposerClient::closeGlobalTransaction(true);
208 {
209 // This should reflect the new position, but not the new color.
210 SCOPED_TRACE("after move, before redraw");
211 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800212 sc->expectBGColor(24, 24);
213 sc->expectBGColor(75, 75);
214 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700215 }
216
217 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
218 waitForPostedBuffers();
219 {
220 // This should reflect the new position and the new color.
221 SCOPED_TRACE("after redraw");
222 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800223 sc->expectBGColor(24, 24);
224 sc->expectBGColor(75, 75);
225 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700226 }
227}
228
229TEST_F(LayerUpdateTest, LayerResizeWorks) {
230 sp<ScreenCapture> sc;
231 {
232 SCOPED_TRACE("before resize");
233 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800234 sc->expectBGColor(0, 12);
235 sc->expectFGColor(75, 75);
236 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700237 }
238
Steve Block9d453682011-12-20 16:23:08 +0000239 ALOGD("resizing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700240 SurfaceComposerClient::openGlobalTransaction();
241 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128));
242 SurfaceComposerClient::closeGlobalTransaction(true);
Steve Block9d453682011-12-20 16:23:08 +0000243 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700244 {
245 // This should not reflect the new size or color because SurfaceFlinger
246 // has not yet received a buffer of the correct size.
247 SCOPED_TRACE("after resize, before redraw");
248 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800249 sc->expectBGColor(0, 12);
250 sc->expectFGColor(75, 75);
251 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700252 }
253
Steve Block9d453682011-12-20 16:23:08 +0000254 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700255 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
256 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000257 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700258 {
259 // This should reflect the new size and the new color.
260 SCOPED_TRACE("after redraw");
261 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800262 sc->expectBGColor(24, 24);
263 sc->checkPixel(75, 75, 63, 195, 63);
264 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700265 }
266}
267
Haixia Shid5750962015-07-27 16:50:49 -0700268TEST_F(LayerUpdateTest, LayerCropWorks) {
269 sp<ScreenCapture> sc;
270 {
271 SCOPED_TRACE("before crop");
272 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800273 sc->expectBGColor(24, 24);
274 sc->expectFGColor(75, 75);
275 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700276 }
277
278 SurfaceComposerClient::openGlobalTransaction();
279 Rect cropRect(16, 16, 32, 32);
280 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setCrop(cropRect));
281 SurfaceComposerClient::closeGlobalTransaction(true);
282 {
283 // This should crop the foreground surface.
284 SCOPED_TRACE("after crop");
285 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800286 sc->expectBGColor(24, 24);
287 sc->expectBGColor(75, 75);
288 sc->expectFGColor(95, 80);
289 sc->expectFGColor(80, 95);
290 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700291 }
292}
293
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000294TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
295 sp<ScreenCapture> sc;
296 {
297 SCOPED_TRACE("before crop");
298 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800299 sc->expectBGColor(24, 24);
300 sc->expectFGColor(75, 75);
301 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000302 }
303 SurfaceComposerClient::openGlobalTransaction();
304 Rect cropRect(16, 16, 32, 32);
305 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFinalCrop(cropRect));
306 SurfaceComposerClient::closeGlobalTransaction(true);
307 {
308 // This should crop the foreground surface.
309 SCOPED_TRACE("after crop");
310 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800311 sc->expectBGColor(24, 24);
312 sc->expectBGColor(75, 75);
313 sc->expectBGColor(95, 80);
314 sc->expectBGColor(80, 95);
315 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000316 }
317}
318
Haixia Shid5750962015-07-27 16:50:49 -0700319TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
320 sp<ScreenCapture> sc;
321 {
322 SCOPED_TRACE("before setLayer");
323 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800324 sc->expectBGColor(24, 24);
325 sc->expectFGColor(75, 75);
326 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700327 }
328
329 SurfaceComposerClient::openGlobalTransaction();
330 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT_MAX - 3));
331 SurfaceComposerClient::closeGlobalTransaction(true);
332 {
333 // This should hide the foreground surface beneath the background.
334 SCOPED_TRACE("after setLayer");
335 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800336 sc->expectBGColor(24, 24);
337 sc->expectBGColor(75, 75);
338 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700339 }
340}
341
342TEST_F(LayerUpdateTest, LayerShowHideWorks) {
343 sp<ScreenCapture> sc;
344 {
345 SCOPED_TRACE("before hide");
346 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800347 sc->expectBGColor(24, 24);
348 sc->expectFGColor(75, 75);
349 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700350 }
351
352 SurfaceComposerClient::openGlobalTransaction();
353 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->hide());
354 SurfaceComposerClient::closeGlobalTransaction(true);
355 {
356 // This should hide the foreground surface.
357 SCOPED_TRACE("after hide, before show");
358 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800359 sc->expectBGColor(24, 24);
360 sc->expectBGColor(75, 75);
361 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700362 }
363
364 SurfaceComposerClient::openGlobalTransaction();
365 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
366 SurfaceComposerClient::closeGlobalTransaction(true);
367 {
368 // This should show the foreground surface.
369 SCOPED_TRACE("after show");
370 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800371 sc->expectBGColor(24, 24);
372 sc->expectFGColor(75, 75);
373 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700374 }
375}
376
377TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
378 sp<ScreenCapture> sc;
379 {
380 SCOPED_TRACE("before setAlpha");
381 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800382 sc->expectBGColor(24, 24);
383 sc->expectFGColor(75, 75);
384 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700385 }
386
387 SurfaceComposerClient::openGlobalTransaction();
388 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75f));
389 SurfaceComposerClient::closeGlobalTransaction(true);
390 {
391 // This should set foreground to be 75% opaque.
392 SCOPED_TRACE("after setAlpha");
393 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800394 sc->expectBGColor(24, 24);
395 sc->checkPixel(75, 75, 162, 63, 96);
396 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700397 }
398}
399
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700400TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
401 sp<ScreenCapture> sc;
402 {
403 SCOPED_TRACE("before setLayerStack");
404 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800405 sc->expectBGColor(24, 24);
406 sc->expectFGColor(75, 75);
407 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700408 }
409
410 SurfaceComposerClient::openGlobalTransaction();
411 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayerStack(1));
412 SurfaceComposerClient::closeGlobalTransaction(true);
413 {
414 // This should hide the foreground surface since it goes to a different
415 // layer stack.
416 SCOPED_TRACE("after setLayerStack");
417 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800418 sc->expectBGColor(24, 24);
419 sc->expectBGColor(75, 75);
420 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700421 }
422}
423
424TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
425 sp<ScreenCapture> sc;
426 {
427 SCOPED_TRACE("before setFlags");
428 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800429 sc->expectBGColor(24, 24);
430 sc->expectFGColor(75, 75);
431 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700432 }
433
434 SurfaceComposerClient::openGlobalTransaction();
435 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFlags(
436 layer_state_t::eLayerHidden, layer_state_t::eLayerHidden));
437 SurfaceComposerClient::closeGlobalTransaction(true);
438 {
439 // This should hide the foreground surface
440 SCOPED_TRACE("after setFlags");
441 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800442 sc->expectBGColor(24, 24);
443 sc->expectBGColor(75, 75);
444 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700445 }
446}
447
448TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
449 sp<ScreenCapture> sc;
450 {
451 SCOPED_TRACE("before setMatrix");
452 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800453 sc->expectBGColor(24, 24);
454 sc->expectFGColor(91, 96);
455 sc->expectFGColor(96, 101);
456 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700457 }
458
459 SurfaceComposerClient::openGlobalTransaction();
460 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setMatrix(M_SQRT1_2, M_SQRT1_2,
461 -M_SQRT1_2, M_SQRT1_2));
462 SurfaceComposerClient::closeGlobalTransaction(true);
463 {
464 SCOPED_TRACE("after setMatrix");
465 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800466 sc->expectBGColor(24, 24);
467 sc->expectFGColor(91, 96);
468 sc->expectBGColor(96, 91);
469 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700470 }
471}
472
Robert Carr8d5227b2017-03-16 15:41:03 -0700473class GeometryLatchingTest : public LayerUpdateTest {
474protected:
475 void EXPECT_INITIAL_STATE(const char * trace) {
476 SCOPED_TRACE(trace);
477 ScreenCapture::captureScreen(&sc);
478 // We find the leading edge of the FG surface.
479 sc->expectFGColor(127, 127);
480 sc->expectBGColor(128, 128);
481 }
482 void completeFGResize() {
483 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
484 waitForPostedBuffers();
485 }
486 void restoreInitialState() {
487 SurfaceComposerClient::openGlobalTransaction();
488 mFGSurfaceControl->setSize(64, 64);
489 mFGSurfaceControl->setPosition(64, 64);
490 mFGSurfaceControl->setCrop(Rect(0, 0, 64, 64));
491 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1));
492 SurfaceComposerClient::closeGlobalTransaction(true);
493
494 EXPECT_INITIAL_STATE("After restoring initial state");
495 }
496 sp<ScreenCapture> sc;
497};
498
499TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
500 EXPECT_INITIAL_STATE("before anything");
501
502 // By default position can be updated even while
503 // a resize is pending.
504 SurfaceComposerClient::openGlobalTransaction();
505 mFGSurfaceControl->setSize(32, 32);
506 mFGSurfaceControl->setPosition(100, 100);
507 SurfaceComposerClient::closeGlobalTransaction(true);
508
509 {
510 SCOPED_TRACE("After moving surface");
511 ScreenCapture::captureScreen(&sc);
512 // If we moved, the FG Surface should cover up what was previously BG
513 // however if we didn't move the FG wouldn't be large enough now.
514 sc->expectFGColor(163, 163);
515 }
516
517 restoreInitialState();
518
519 // Now we repeat with setGeometryAppliesWithResize
520 // and verify the position DOESN'T latch.
521 SurfaceComposerClient::openGlobalTransaction();
522 mFGSurfaceControl->setGeometryAppliesWithResize();
523 mFGSurfaceControl->setSize(32, 32);
524 mFGSurfaceControl->setPosition(100, 100);
525 SurfaceComposerClient::closeGlobalTransaction(true);
526
527 {
528 SCOPED_TRACE("While resize is pending");
529 ScreenCapture::captureScreen(&sc);
530 // This time we shouldn't have moved, so the BG color
531 // should still be visible.
532 sc->expectBGColor(128, 128);
533 }
534
535 completeFGResize();
536
537 {
538 SCOPED_TRACE("After the resize");
539 ScreenCapture::captureScreen(&sc);
540 // But after the resize completes, we should move
541 // and the FG should be visible here.
542 sc->expectFGColor(128, 128);
543 }
544}
545
546class CropLatchingTest : public GeometryLatchingTest {
547protected:
548 void EXPECT_CROPPED_STATE(const char* trace) {
549 SCOPED_TRACE(trace);
550 ScreenCapture::captureScreen(&sc);
551 // The edge should be moved back one pixel by our crop.
552 sc->expectFGColor(126, 126);
553 sc->expectBGColor(127, 127);
554 sc->expectBGColor(128, 128);
555 }
556};
557
558TEST_F(CropLatchingTest, CropLatching) {
559 EXPECT_INITIAL_STATE("before anything");
560 // Normally the crop applies immediately even while a resize is pending.
561 SurfaceComposerClient::openGlobalTransaction();
562 mFGSurfaceControl->setSize(128, 128);
563 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
564 SurfaceComposerClient::closeGlobalTransaction(true);
565
566 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
567
568 restoreInitialState();
569
570 SurfaceComposerClient::openGlobalTransaction();
571 mFGSurfaceControl->setSize(128, 128);
572 mFGSurfaceControl->setGeometryAppliesWithResize();
573 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
574 SurfaceComposerClient::closeGlobalTransaction(true);
575
576 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
577
578 completeFGResize();
579
580 EXPECT_CROPPED_STATE("after the resize finishes");
581}
582
583TEST_F(CropLatchingTest, FinalCropLatching) {
584 EXPECT_INITIAL_STATE("before anything");
585 // Normally the crop applies immediately even while a resize is pending.
586 SurfaceComposerClient::openGlobalTransaction();
587 mFGSurfaceControl->setSize(128, 128);
588 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
589 SurfaceComposerClient::closeGlobalTransaction(true);
590
591 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
592
593 restoreInitialState();
594
595 SurfaceComposerClient::openGlobalTransaction();
596 mFGSurfaceControl->setSize(128, 128);
597 mFGSurfaceControl->setGeometryAppliesWithResize();
598 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
599 SurfaceComposerClient::closeGlobalTransaction(true);
600
601 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
602
603 completeFGResize();
604
605 EXPECT_CROPPED_STATE("after the resize finishes");
606}
607
Pablo Ceballos05289c22016-04-14 15:49:55 -0700608TEST_F(LayerUpdateTest, DeferredTransactionTest) {
609 sp<ScreenCapture> sc;
610 {
611 SCOPED_TRACE("before anything");
612 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800613 sc->expectBGColor(32, 32);
614 sc->expectFGColor(96, 96);
615 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700616 }
617
618 // set up two deferred transactions on different frames
619 SurfaceComposerClient::openGlobalTransaction();
620 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75));
621 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
622 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
623 SurfaceComposerClient::closeGlobalTransaction(true);
624
625 SurfaceComposerClient::openGlobalTransaction();
626 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128));
627 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
628 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
629 SurfaceComposerClient::closeGlobalTransaction(true);
630
631 {
632 SCOPED_TRACE("before any trigger");
633 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800634 sc->expectBGColor(32, 32);
635 sc->expectFGColor(96, 96);
636 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700637 }
638
639 // should trigger the first deferred transaction, but not the second one
640 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
641 {
642 SCOPED_TRACE("after first trigger");
643 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800644 sc->expectBGColor(32, 32);
645 sc->checkPixel(96, 96, 162, 63, 96);
646 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700647 }
648
649 // should show up immediately since it's not deferred
650 SurfaceComposerClient::openGlobalTransaction();
651 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0));
652 SurfaceComposerClient::closeGlobalTransaction(true);
653
654 // trigger the second deferred transaction
655 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
656 {
657 SCOPED_TRACE("after second trigger");
658 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800659 sc->expectBGColor(32, 32);
660 sc->expectBGColor(96, 96);
661 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700662 }
663}
664
Robert Carr1f0a16a2016-10-24 16:27:39 -0700665class ChildLayerTest : public LayerUpdateTest {
666protected:
667 void SetUp() override {
668 LayerUpdateTest::SetUp();
669 mChild = mComposerClient->createSurface(
670 String8("Child surface"),
671 10, 10, PIXEL_FORMAT_RGBA_8888,
672 0, mFGSurfaceControl.get());
673 fillSurfaceRGBA8(mChild, 200, 200, 200);
674
675 {
676 SCOPED_TRACE("before anything");
677 ScreenCapture::captureScreen(&mCapture);
678 mCapture->expectChildColor(64, 64);
679 }
680 }
681 void TearDown() override {
682 LayerUpdateTest::TearDown();
683 mChild = 0;
684 }
685
686 sp<SurfaceControl> mChild;
687 sp<ScreenCapture> mCapture;
688};
689
690TEST_F(ChildLayerTest, ChildLayerPositioning) {
691 SurfaceComposerClient::openGlobalTransaction();
692 mChild->show();
693 mChild->setPosition(10, 10);
694 mFGSurfaceControl->setPosition(64, 64);
695 SurfaceComposerClient::closeGlobalTransaction(true);
696
697 {
698 ScreenCapture::captureScreen(&mCapture);
699 // Top left of foreground must now be visible
700 mCapture->expectFGColor(64, 64);
701 // But 10 pixels in we should see the child surface
702 mCapture->expectChildColor(74, 74);
703 // And 10 more pixels we should be back to the foreground surface
704 mCapture->expectFGColor(84, 84);
705 }
706
707 SurfaceComposerClient::openGlobalTransaction();
708 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0));
709 SurfaceComposerClient::closeGlobalTransaction(true);
710
711 {
712 ScreenCapture::captureScreen(&mCapture);
713 // Top left of foreground should now be at 0, 0
714 mCapture->expectFGColor(0, 0);
715 // But 10 pixels in we should see the child surface
716 mCapture->expectChildColor(10, 10);
717 // And 10 more pixels we should be back to the foreground surface
718 mCapture->expectFGColor(20, 20);
719 }
720}
721
722TEST_F(ChildLayerTest, ChildLayerConstraints) {
723 SurfaceComposerClient::openGlobalTransaction();
724 mChild->show();
725 mFGSurfaceControl->setPosition(0, 0);
726 mChild->setPosition(63, 63);
727 SurfaceComposerClient::closeGlobalTransaction(true);
728
729 {
730 ScreenCapture::captureScreen(&mCapture);
731 mCapture->expectFGColor(0, 0);
732 // Last pixel in foreground should now be the child.
733 mCapture->expectChildColor(63, 63);
734 // But the child should be constrained and the next pixel
735 // must be the background
736 mCapture->expectBGColor(64, 64);
737 }
738}
739
740TEST_F(ChildLayerTest, ChildLayerScaling) {
741 SurfaceComposerClient::openGlobalTransaction();
742 mFGSurfaceControl->setPosition(0, 0);
743 SurfaceComposerClient::closeGlobalTransaction(true);
744
745 // Find the boundary between the parent and child
746 {
747 ScreenCapture::captureScreen(&mCapture);
748 mCapture->expectChildColor(9, 9);
749 mCapture->expectFGColor(10, 10);
750 }
751
752 SurfaceComposerClient::openGlobalTransaction();
753 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0);
754 SurfaceComposerClient::closeGlobalTransaction(true);
755
756 // The boundary should be twice as far from the origin now.
757 // The pixels from the last test should all be child now
758 {
759 ScreenCapture::captureScreen(&mCapture);
760 mCapture->expectChildColor(9, 9);
761 mCapture->expectChildColor(10, 10);
762 mCapture->expectChildColor(19, 19);
763 mCapture->expectFGColor(20, 20);
764 }
765}
Robert Carr9524cb32017-02-13 11:32:32 -0800766
Robert Carr6452f122017-03-21 10:41:29 -0700767TEST_F(ChildLayerTest, ChildLayerAlpha) {
768 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
769 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
770 fillSurfaceRGBA8(mChild, 0, 254, 0);
771 waitForPostedBuffers();
772
773 SurfaceComposerClient::openGlobalTransaction();
774 mChild->show();
775 mChild->setPosition(0, 0);
776 mFGSurfaceControl->setPosition(0, 0);
777 SurfaceComposerClient::closeGlobalTransaction(true);
778
779 {
780 ScreenCapture::captureScreen(&mCapture);
781 // Unblended child color
782 mCapture->checkPixel(0, 0, 0, 254, 0);
783 }
784
785 SurfaceComposerClient::openGlobalTransaction();
786 ASSERT_EQ(NO_ERROR, mChild->setAlpha(0.5));
787 SurfaceComposerClient::closeGlobalTransaction(true);
788
789 {
790 ScreenCapture::captureScreen(&mCapture);
791 // Child and BG blended.
792 mCapture->checkPixel(0, 0, 127, 127, 0);
793 }
794
795 SurfaceComposerClient::openGlobalTransaction();
796 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.5));
797 SurfaceComposerClient::closeGlobalTransaction(true);
798
799 {
800 ScreenCapture::captureScreen(&mCapture);
801 // Child and BG blended.
802 mCapture->checkPixel(0, 0, 95, 64, 95);
803 }
804}
805
Robert Carr9524cb32017-02-13 11:32:32 -0800806TEST_F(ChildLayerTest, ReparentChildren) {
807 SurfaceComposerClient::openGlobalTransaction();
808 mChild->show();
809 mChild->setPosition(10, 10);
810 mFGSurfaceControl->setPosition(64, 64);
811 SurfaceComposerClient::closeGlobalTransaction(true);
812
813 {
814 ScreenCapture::captureScreen(&mCapture);
815 // Top left of foreground must now be visible
816 mCapture->expectFGColor(64, 64);
817 // But 10 pixels in we should see the child surface
818 mCapture->expectChildColor(74, 74);
819 // And 10 more pixels we should be back to the foreground surface
820 mCapture->expectFGColor(84, 84);
821 }
822 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle());
823 {
824 ScreenCapture::captureScreen(&mCapture);
825 mCapture->expectFGColor(64, 64);
826 // In reparenting we should have exposed the entire foreground surface.
827 mCapture->expectFGColor(74, 74);
828 // And the child layer should now begin at 10, 10 (since the BG
829 // layer is at (0, 0)).
830 mCapture->expectBGColor(9, 9);
831 mCapture->expectChildColor(10, 10);
832 }
833}
834
835TEST_F(ChildLayerTest, DetachChildren) {
836 SurfaceComposerClient::openGlobalTransaction();
837 mChild->show();
838 mChild->setPosition(10, 10);
839 mFGSurfaceControl->setPosition(64, 64);
840 SurfaceComposerClient::closeGlobalTransaction(true);
841
842 {
843 ScreenCapture::captureScreen(&mCapture);
844 // Top left of foreground must now be visible
845 mCapture->expectFGColor(64, 64);
846 // But 10 pixels in we should see the child surface
847 mCapture->expectChildColor(74, 74);
848 // And 10 more pixels we should be back to the foreground surface
849 mCapture->expectFGColor(84, 84);
850 }
851
852 SurfaceComposerClient::openGlobalTransaction();
853 mFGSurfaceControl->detachChildren();
Robert Carr8d5227b2017-03-16 15:41:03 -0700854 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -0800855
856 SurfaceComposerClient::openGlobalTransaction();
857 mChild->hide();
Robert Carr8d5227b2017-03-16 15:41:03 -0700858 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -0800859
860 // Nothing should have changed.
861 {
862 ScreenCapture::captureScreen(&mCapture);
863 mCapture->expectFGColor(64, 64);
864 mCapture->expectChildColor(74, 74);
865 mCapture->expectFGColor(84, 84);
866 }
867}
868
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700869}