blob: aeb557aad34f8a620027fef80e67e1298989d2f7 [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
Pablo Ceballos05289c22016-04-14 15:49:55 -0700473TEST_F(LayerUpdateTest, DeferredTransactionTest) {
474 sp<ScreenCapture> sc;
475 {
476 SCOPED_TRACE("before anything");
477 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800478 sc->expectBGColor(32, 32);
479 sc->expectFGColor(96, 96);
480 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700481 }
482
483 // set up two deferred transactions on different frames
484 SurfaceComposerClient::openGlobalTransaction();
485 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75));
486 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
487 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
488 SurfaceComposerClient::closeGlobalTransaction(true);
489
490 SurfaceComposerClient::openGlobalTransaction();
491 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128));
492 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
493 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
494 SurfaceComposerClient::closeGlobalTransaction(true);
495
496 {
497 SCOPED_TRACE("before any trigger");
498 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800499 sc->expectBGColor(32, 32);
500 sc->expectFGColor(96, 96);
501 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700502 }
503
504 // should trigger the first deferred transaction, but not the second one
505 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
506 {
507 SCOPED_TRACE("after first trigger");
508 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800509 sc->expectBGColor(32, 32);
510 sc->checkPixel(96, 96, 162, 63, 96);
511 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700512 }
513
514 // should show up immediately since it's not deferred
515 SurfaceComposerClient::openGlobalTransaction();
516 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0));
517 SurfaceComposerClient::closeGlobalTransaction(true);
518
519 // trigger the second deferred transaction
520 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
521 {
522 SCOPED_TRACE("after second trigger");
523 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800524 sc->expectBGColor(32, 32);
525 sc->expectBGColor(96, 96);
526 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700527 }
528}
529
Robert Carr1f0a16a2016-10-24 16:27:39 -0700530class ChildLayerTest : public LayerUpdateTest {
531protected:
532 void SetUp() override {
533 LayerUpdateTest::SetUp();
534 mChild = mComposerClient->createSurface(
535 String8("Child surface"),
536 10, 10, PIXEL_FORMAT_RGBA_8888,
537 0, mFGSurfaceControl.get());
538 fillSurfaceRGBA8(mChild, 200, 200, 200);
539
540 {
541 SCOPED_TRACE("before anything");
542 ScreenCapture::captureScreen(&mCapture);
543 mCapture->expectChildColor(64, 64);
544 }
545 }
546 void TearDown() override {
547 LayerUpdateTest::TearDown();
548 mChild = 0;
549 }
550
551 sp<SurfaceControl> mChild;
552 sp<ScreenCapture> mCapture;
553};
554
555TEST_F(ChildLayerTest, ChildLayerPositioning) {
556 SurfaceComposerClient::openGlobalTransaction();
557 mChild->show();
558 mChild->setPosition(10, 10);
559 mFGSurfaceControl->setPosition(64, 64);
560 SurfaceComposerClient::closeGlobalTransaction(true);
561
562 {
563 ScreenCapture::captureScreen(&mCapture);
564 // Top left of foreground must now be visible
565 mCapture->expectFGColor(64, 64);
566 // But 10 pixels in we should see the child surface
567 mCapture->expectChildColor(74, 74);
568 // And 10 more pixels we should be back to the foreground surface
569 mCapture->expectFGColor(84, 84);
570 }
571
572 SurfaceComposerClient::openGlobalTransaction();
573 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0));
574 SurfaceComposerClient::closeGlobalTransaction(true);
575
576 {
577 ScreenCapture::captureScreen(&mCapture);
578 // Top left of foreground should now be at 0, 0
579 mCapture->expectFGColor(0, 0);
580 // But 10 pixels in we should see the child surface
581 mCapture->expectChildColor(10, 10);
582 // And 10 more pixels we should be back to the foreground surface
583 mCapture->expectFGColor(20, 20);
584 }
585}
586
587TEST_F(ChildLayerTest, ChildLayerConstraints) {
588 SurfaceComposerClient::openGlobalTransaction();
589 mChild->show();
590 mFGSurfaceControl->setPosition(0, 0);
591 mChild->setPosition(63, 63);
592 SurfaceComposerClient::closeGlobalTransaction(true);
593
594 {
595 ScreenCapture::captureScreen(&mCapture);
596 mCapture->expectFGColor(0, 0);
597 // Last pixel in foreground should now be the child.
598 mCapture->expectChildColor(63, 63);
599 // But the child should be constrained and the next pixel
600 // must be the background
601 mCapture->expectBGColor(64, 64);
602 }
603}
604
605TEST_F(ChildLayerTest, ChildLayerScaling) {
606 SurfaceComposerClient::openGlobalTransaction();
607 mFGSurfaceControl->setPosition(0, 0);
608 SurfaceComposerClient::closeGlobalTransaction(true);
609
610 // Find the boundary between the parent and child
611 {
612 ScreenCapture::captureScreen(&mCapture);
613 mCapture->expectChildColor(9, 9);
614 mCapture->expectFGColor(10, 10);
615 }
616
617 SurfaceComposerClient::openGlobalTransaction();
618 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0);
619 SurfaceComposerClient::closeGlobalTransaction(true);
620
621 // The boundary should be twice as far from the origin now.
622 // The pixels from the last test should all be child now
623 {
624 ScreenCapture::captureScreen(&mCapture);
625 mCapture->expectChildColor(9, 9);
626 mCapture->expectChildColor(10, 10);
627 mCapture->expectChildColor(19, 19);
628 mCapture->expectFGColor(20, 20);
629 }
630}
Robert Carr9524cb32017-02-13 11:32:32 -0800631
632TEST_F(ChildLayerTest, ReparentChildren) {
633 SurfaceComposerClient::openGlobalTransaction();
634 mChild->show();
635 mChild->setPosition(10, 10);
636 mFGSurfaceControl->setPosition(64, 64);
637 SurfaceComposerClient::closeGlobalTransaction(true);
638
639 {
640 ScreenCapture::captureScreen(&mCapture);
641 // Top left of foreground must now be visible
642 mCapture->expectFGColor(64, 64);
643 // But 10 pixels in we should see the child surface
644 mCapture->expectChildColor(74, 74);
645 // And 10 more pixels we should be back to the foreground surface
646 mCapture->expectFGColor(84, 84);
647 }
648 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle());
649 {
650 ScreenCapture::captureScreen(&mCapture);
651 mCapture->expectFGColor(64, 64);
652 // In reparenting we should have exposed the entire foreground surface.
653 mCapture->expectFGColor(74, 74);
654 // And the child layer should now begin at 10, 10 (since the BG
655 // layer is at (0, 0)).
656 mCapture->expectBGColor(9, 9);
657 mCapture->expectChildColor(10, 10);
658 }
659}
660
661TEST_F(ChildLayerTest, DetachChildren) {
662 SurfaceComposerClient::openGlobalTransaction();
663 mChild->show();
664 mChild->setPosition(10, 10);
665 mFGSurfaceControl->setPosition(64, 64);
666 SurfaceComposerClient::closeGlobalTransaction(true);
667
668 {
669 ScreenCapture::captureScreen(&mCapture);
670 // Top left of foreground must now be visible
671 mCapture->expectFGColor(64, 64);
672 // But 10 pixels in we should see the child surface
673 mCapture->expectChildColor(74, 74);
674 // And 10 more pixels we should be back to the foreground surface
675 mCapture->expectFGColor(84, 84);
676 }
677
678 SurfaceComposerClient::openGlobalTransaction();
679 mFGSurfaceControl->detachChildren();
680 SurfaceComposerClient::closeGlobalTransaction();
681
682 SurfaceComposerClient::openGlobalTransaction();
683 mChild->hide();
684 SurfaceComposerClient::closeGlobalTransaction();
685
686 // Nothing should have changed.
687 {
688 ScreenCapture::captureScreen(&mCapture);
689 mCapture->expectFGColor(64, 64);
690 mCapture->expectChildColor(74, 74);
691 mCapture->expectFGColor(84, 84);
692 }
693}
694
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700695}