blob: 8900a4d25809b8bbda39345f6791834c6b24698f [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>
chaviw13fdc492017-06-27 12:40:18 -070031#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070032
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070033namespace android {
34
35// Fill an RGBA_8888 formatted surface with a single color.
36static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc,
Robert Carr7bf247e2017-05-18 14:02:49 -070037 uint8_t r, uint8_t g, uint8_t b, bool unlock=true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070039 sp<Surface> s = sc->getSurface();
40 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
42 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070043 for (int y = 0; y < outBuffer.height; y++) {
44 for (int x = 0; x < outBuffer.width; x++) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045 uint8_t* pixel = img + (4 * (y*outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070046 pixel[0] = r;
47 pixel[1] = g;
48 pixel[2] = b;
49 pixel[3] = 255;
50 }
51 }
Robert Carr7bf247e2017-05-18 14:02:49 -070052 if (unlock) {
53 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
54 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070055}
56
57// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
58// individual pixel values for testing purposes.
59class ScreenCapture : public RefBase {
60public:
61 static void captureScreen(sp<ScreenCapture>* sc) {
Michael Lentine5a16a622015-05-21 13:48:24 -070062 sp<IGraphicBufferProducer> producer;
63 sp<IGraphicBufferConsumer> consumer;
64 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -070065 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070066 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Michael Lentine5a16a622015-05-21 13:48:24 -070067 sp<IBinder> display(sf->getBuiltInDisplay(
68 ISurfaceComposer::eDisplayIdMain));
Pablo Ceballos15311bd2016-06-01 18:53:40 -070069 SurfaceComposerClient::openGlobalTransaction();
70 SurfaceComposerClient::closeGlobalTransaction(true);
Michael Lentine5a16a622015-05-21 13:48:24 -070071 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0,
72 0, INT_MAX, false));
73 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070074 }
75
76 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -070077 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
78 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
79 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070080 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
81 String8 err(String8::format("pixel @ (%3d, %3d): "
82 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
83 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070084 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070085 }
86 }
87
Robert Carr1f0a16a2016-10-24 16:27:39 -070088 void expectFGColor(uint32_t x, uint32_t y) {
89 checkPixel(x, y, 195, 63, 63);
90 }
91
92 void expectBGColor(uint32_t x, uint32_t y) {
93 checkPixel(x, y, 63, 63, 195);
94 }
95
96 void expectChildColor(uint32_t x, uint32_t y) {
97 checkPixel(x, y, 200, 200, 200);
98 }
99
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700100private:
Michael Lentine5a16a622015-05-21 13:48:24 -0700101 ScreenCapture(const sp<CpuConsumer>& cc) :
102 mCC(cc) {
103 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
104 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700105
Michael Lentine5a16a622015-05-21 13:48:24 -0700106 ~ScreenCapture() {
107 mCC->unlockBuffer(mBuf);
108 }
109
110 sp<CpuConsumer> mCC;
111 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700112};
113
114class LayerUpdateTest : public ::testing::Test {
115protected:
116 virtual void SetUp() {
117 mComposerClient = new SurfaceComposerClient;
118 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
119
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700120 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
121 ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700122 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700123 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700124
125 ssize_t displayWidth = info.w;
126 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700127
128 // Background surface
129 mBGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700130 String8("BG Test Surface"), displayWidth, displayHeight,
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700131 PIXEL_FORMAT_RGBA_8888, 0);
132 ASSERT_TRUE(mBGSurfaceControl != NULL);
133 ASSERT_TRUE(mBGSurfaceControl->isValid());
134 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
135
136 // Foreground surface
137 mFGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700138 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700139 ASSERT_TRUE(mFGSurfaceControl != NULL);
140 ASSERT_TRUE(mFGSurfaceControl->isValid());
141
142 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
143
144 // Synchronization surface
145 mSyncSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700146 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700147 ASSERT_TRUE(mSyncSurfaceControl != NULL);
148 ASSERT_TRUE(mSyncSurfaceControl->isValid());
149
150 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
151
152 SurfaceComposerClient::openGlobalTransaction();
153
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700154 mComposerClient->setDisplayLayerStack(display, 0);
155
Robert Carr1f0a16a2016-10-24 16:27:39 -0700156 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT32_MAX-2));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700157 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show());
158
Robert Carr1f0a16a2016-10-24 16:27:39 -0700159 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700160 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64));
161 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
162
Robert Carr1f0a16a2016-10-24 16:27:39 -0700163 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700164 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setPosition(displayWidth-2,
165 displayHeight-2));
166 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->show());
167
168 SurfaceComposerClient::closeGlobalTransaction(true);
169 }
170
171 virtual void TearDown() {
172 mComposerClient->dispose();
173 mBGSurfaceControl = 0;
174 mFGSurfaceControl = 0;
175 mSyncSurfaceControl = 0;
176 mComposerClient = 0;
177 }
178
179 void waitForPostedBuffers() {
180 // Since the sync surface is in synchronous mode (i.e. double buffered)
181 // posting three buffers to it should ensure that at least two
182 // SurfaceFlinger::handlePageFlip calls have been made, which should
183 // guaranteed that a buffer posted to another Surface has been retired.
184 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
185 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
186 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
187 }
188
189 sp<SurfaceComposerClient> mComposerClient;
190 sp<SurfaceControl> mBGSurfaceControl;
191 sp<SurfaceControl> mFGSurfaceControl;
192
193 // This surface is used to ensure that the buffers posted to
194 // mFGSurfaceControl have been picked up by SurfaceFlinger.
195 sp<SurfaceControl> mSyncSurfaceControl;
196};
197
198TEST_F(LayerUpdateTest, LayerMoveWorks) {
199 sp<ScreenCapture> sc;
200 {
201 SCOPED_TRACE("before move");
202 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800203 sc->expectBGColor(0, 12);
204 sc->expectFGColor(75, 75);
205 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700206 }
207
208 SurfaceComposerClient::openGlobalTransaction();
209 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128, 128));
210 SurfaceComposerClient::closeGlobalTransaction(true);
211 {
212 // This should reflect the new position, but not the new color.
213 SCOPED_TRACE("after move, before redraw");
214 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800215 sc->expectBGColor(24, 24);
216 sc->expectBGColor(75, 75);
217 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700218 }
219
220 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
221 waitForPostedBuffers();
222 {
223 // This should reflect the new position and the new color.
224 SCOPED_TRACE("after redraw");
225 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800226 sc->expectBGColor(24, 24);
227 sc->expectBGColor(75, 75);
228 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700229 }
230}
231
232TEST_F(LayerUpdateTest, LayerResizeWorks) {
233 sp<ScreenCapture> sc;
234 {
235 SCOPED_TRACE("before resize");
236 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800237 sc->expectBGColor(0, 12);
238 sc->expectFGColor(75, 75);
239 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700240 }
241
Steve Block9d453682011-12-20 16:23:08 +0000242 ALOGD("resizing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700243 SurfaceComposerClient::openGlobalTransaction();
244 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128));
245 SurfaceComposerClient::closeGlobalTransaction(true);
Steve Block9d453682011-12-20 16:23:08 +0000246 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700247 {
248 // This should not reflect the new size or color because SurfaceFlinger
249 // has not yet received a buffer of the correct size.
250 SCOPED_TRACE("after resize, before redraw");
251 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800252 sc->expectBGColor(0, 12);
253 sc->expectFGColor(75, 75);
254 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700255 }
256
Steve Block9d453682011-12-20 16:23:08 +0000257 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700258 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
259 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000260 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700261 {
262 // This should reflect the new size and the new color.
263 SCOPED_TRACE("after redraw");
264 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800265 sc->expectBGColor(24, 24);
266 sc->checkPixel(75, 75, 63, 195, 63);
267 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700268 }
269}
270
Haixia Shid5750962015-07-27 16:50:49 -0700271TEST_F(LayerUpdateTest, LayerCropWorks) {
272 sp<ScreenCapture> sc;
273 {
274 SCOPED_TRACE("before crop");
275 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800276 sc->expectBGColor(24, 24);
277 sc->expectFGColor(75, 75);
278 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700279 }
280
281 SurfaceComposerClient::openGlobalTransaction();
282 Rect cropRect(16, 16, 32, 32);
283 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setCrop(cropRect));
284 SurfaceComposerClient::closeGlobalTransaction(true);
285 {
286 // This should crop the foreground surface.
287 SCOPED_TRACE("after crop");
288 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800289 sc->expectBGColor(24, 24);
290 sc->expectBGColor(75, 75);
291 sc->expectFGColor(95, 80);
292 sc->expectFGColor(80, 95);
293 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700294 }
295}
296
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000297TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
298 sp<ScreenCapture> sc;
299 {
300 SCOPED_TRACE("before crop");
301 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800302 sc->expectBGColor(24, 24);
303 sc->expectFGColor(75, 75);
304 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000305 }
306 SurfaceComposerClient::openGlobalTransaction();
307 Rect cropRect(16, 16, 32, 32);
308 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFinalCrop(cropRect));
309 SurfaceComposerClient::closeGlobalTransaction(true);
310 {
311 // This should crop the foreground surface.
312 SCOPED_TRACE("after crop");
313 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800314 sc->expectBGColor(24, 24);
315 sc->expectBGColor(75, 75);
316 sc->expectBGColor(95, 80);
317 sc->expectBGColor(80, 95);
318 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000319 }
320}
321
Haixia Shid5750962015-07-27 16:50:49 -0700322TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
323 sp<ScreenCapture> sc;
324 {
325 SCOPED_TRACE("before setLayer");
326 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800327 sc->expectBGColor(24, 24);
328 sc->expectFGColor(75, 75);
329 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700330 }
331
332 SurfaceComposerClient::openGlobalTransaction();
333 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT_MAX - 3));
334 SurfaceComposerClient::closeGlobalTransaction(true);
335 {
336 // This should hide the foreground surface beneath the background.
337 SCOPED_TRACE("after setLayer");
338 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800339 sc->expectBGColor(24, 24);
340 sc->expectBGColor(75, 75);
341 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700342 }
343}
344
345TEST_F(LayerUpdateTest, LayerShowHideWorks) {
346 sp<ScreenCapture> sc;
347 {
348 SCOPED_TRACE("before hide");
349 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800350 sc->expectBGColor(24, 24);
351 sc->expectFGColor(75, 75);
352 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700353 }
354
355 SurfaceComposerClient::openGlobalTransaction();
356 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->hide());
357 SurfaceComposerClient::closeGlobalTransaction(true);
358 {
359 // This should hide the foreground surface.
360 SCOPED_TRACE("after hide, before show");
361 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800362 sc->expectBGColor(24, 24);
363 sc->expectBGColor(75, 75);
364 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700365 }
366
367 SurfaceComposerClient::openGlobalTransaction();
368 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
369 SurfaceComposerClient::closeGlobalTransaction(true);
370 {
371 // This should show the foreground surface.
372 SCOPED_TRACE("after show");
373 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800374 sc->expectBGColor(24, 24);
375 sc->expectFGColor(75, 75);
376 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700377 }
378}
379
380TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
381 sp<ScreenCapture> sc;
382 {
383 SCOPED_TRACE("before setAlpha");
384 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800385 sc->expectBGColor(24, 24);
386 sc->expectFGColor(75, 75);
387 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700388 }
389
390 SurfaceComposerClient::openGlobalTransaction();
391 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75f));
392 SurfaceComposerClient::closeGlobalTransaction(true);
393 {
394 // This should set foreground to be 75% opaque.
395 SCOPED_TRACE("after setAlpha");
396 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800397 sc->expectBGColor(24, 24);
398 sc->checkPixel(75, 75, 162, 63, 96);
399 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700400 }
401}
402
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700403TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
404 sp<ScreenCapture> sc;
405 {
406 SCOPED_TRACE("before setLayerStack");
407 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800408 sc->expectBGColor(24, 24);
409 sc->expectFGColor(75, 75);
410 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700411 }
412
413 SurfaceComposerClient::openGlobalTransaction();
414 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayerStack(1));
415 SurfaceComposerClient::closeGlobalTransaction(true);
416 {
417 // This should hide the foreground surface since it goes to a different
418 // layer stack.
419 SCOPED_TRACE("after setLayerStack");
420 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800421 sc->expectBGColor(24, 24);
422 sc->expectBGColor(75, 75);
423 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700424 }
425}
426
427TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
428 sp<ScreenCapture> sc;
429 {
430 SCOPED_TRACE("before setFlags");
431 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800432 sc->expectBGColor(24, 24);
433 sc->expectFGColor(75, 75);
434 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700435 }
436
437 SurfaceComposerClient::openGlobalTransaction();
438 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFlags(
439 layer_state_t::eLayerHidden, layer_state_t::eLayerHidden));
440 SurfaceComposerClient::closeGlobalTransaction(true);
441 {
442 // This should hide the foreground surface
443 SCOPED_TRACE("after setFlags");
444 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800445 sc->expectBGColor(24, 24);
446 sc->expectBGColor(75, 75);
447 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700448 }
449}
450
451TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
452 sp<ScreenCapture> sc;
453 {
454 SCOPED_TRACE("before setMatrix");
455 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800456 sc->expectBGColor(24, 24);
457 sc->expectFGColor(91, 96);
458 sc->expectFGColor(96, 101);
459 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700460 }
461
462 SurfaceComposerClient::openGlobalTransaction();
463 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setMatrix(M_SQRT1_2, M_SQRT1_2,
464 -M_SQRT1_2, M_SQRT1_2));
465 SurfaceComposerClient::closeGlobalTransaction(true);
466 {
467 SCOPED_TRACE("after setMatrix");
468 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800469 sc->expectBGColor(24, 24);
470 sc->expectFGColor(91, 96);
471 sc->expectBGColor(96, 91);
472 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700473 }
474}
475
Robert Carr8d5227b2017-03-16 15:41:03 -0700476class GeometryLatchingTest : public LayerUpdateTest {
477protected:
478 void EXPECT_INITIAL_STATE(const char * trace) {
479 SCOPED_TRACE(trace);
480 ScreenCapture::captureScreen(&sc);
481 // We find the leading edge of the FG surface.
482 sc->expectFGColor(127, 127);
483 sc->expectBGColor(128, 128);
484 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700485
486 void lockAndFillFGBuffer() {
487 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
488 }
489
490 void unlockFGBuffer() {
491 sp<Surface> s = mFGSurfaceControl->getSurface();
492 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
493 waitForPostedBuffers();
494 }
495
Robert Carr8d5227b2017-03-16 15:41:03 -0700496 void completeFGResize() {
497 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
498 waitForPostedBuffers();
499 }
500 void restoreInitialState() {
501 SurfaceComposerClient::openGlobalTransaction();
502 mFGSurfaceControl->setSize(64, 64);
503 mFGSurfaceControl->setPosition(64, 64);
504 mFGSurfaceControl->setCrop(Rect(0, 0, 64, 64));
505 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1));
506 SurfaceComposerClient::closeGlobalTransaction(true);
507
508 EXPECT_INITIAL_STATE("After restoring initial state");
509 }
510 sp<ScreenCapture> sc;
511};
512
513TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
514 EXPECT_INITIAL_STATE("before anything");
515
516 // By default position can be updated even while
517 // a resize is pending.
518 SurfaceComposerClient::openGlobalTransaction();
519 mFGSurfaceControl->setSize(32, 32);
520 mFGSurfaceControl->setPosition(100, 100);
521 SurfaceComposerClient::closeGlobalTransaction(true);
522
523 {
524 SCOPED_TRACE("After moving surface");
525 ScreenCapture::captureScreen(&sc);
526 // If we moved, the FG Surface should cover up what was previously BG
527 // however if we didn't move the FG wouldn't be large enough now.
528 sc->expectFGColor(163, 163);
529 }
530
531 restoreInitialState();
532
533 // Now we repeat with setGeometryAppliesWithResize
534 // and verify the position DOESN'T latch.
535 SurfaceComposerClient::openGlobalTransaction();
536 mFGSurfaceControl->setGeometryAppliesWithResize();
537 mFGSurfaceControl->setSize(32, 32);
538 mFGSurfaceControl->setPosition(100, 100);
539 SurfaceComposerClient::closeGlobalTransaction(true);
540
541 {
542 SCOPED_TRACE("While resize is pending");
543 ScreenCapture::captureScreen(&sc);
544 // This time we shouldn't have moved, so the BG color
545 // should still be visible.
546 sc->expectBGColor(128, 128);
547 }
548
549 completeFGResize();
550
551 {
552 SCOPED_TRACE("After the resize");
553 ScreenCapture::captureScreen(&sc);
554 // But after the resize completes, we should move
555 // and the FG should be visible here.
556 sc->expectFGColor(128, 128);
557 }
558}
559
560class CropLatchingTest : public GeometryLatchingTest {
561protected:
562 void EXPECT_CROPPED_STATE(const char* trace) {
563 SCOPED_TRACE(trace);
564 ScreenCapture::captureScreen(&sc);
565 // The edge should be moved back one pixel by our crop.
566 sc->expectFGColor(126, 126);
567 sc->expectBGColor(127, 127);
568 sc->expectBGColor(128, 128);
569 }
chaviw59f5c562017-06-28 16:39:06 -0700570
571 void EXPECT_RESIZE_STATE(const char* trace) {
572 SCOPED_TRACE(trace);
573 ScreenCapture::captureScreen(&sc);
574 // The FG is now resized too 128,128 at 64,64
575 sc->expectFGColor(64, 64);
576 sc->expectFGColor(191, 191);
577 sc->expectBGColor(192, 192);
578 }
Robert Carr8d5227b2017-03-16 15:41:03 -0700579};
580
581TEST_F(CropLatchingTest, CropLatching) {
582 EXPECT_INITIAL_STATE("before anything");
583 // Normally the crop applies immediately even while a resize is pending.
584 SurfaceComposerClient::openGlobalTransaction();
585 mFGSurfaceControl->setSize(128, 128);
586 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
587 SurfaceComposerClient::closeGlobalTransaction(true);
588
589 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
590
591 restoreInitialState();
592
593 SurfaceComposerClient::openGlobalTransaction();
594 mFGSurfaceControl->setSize(128, 128);
595 mFGSurfaceControl->setGeometryAppliesWithResize();
596 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63));
597 SurfaceComposerClient::closeGlobalTransaction(true);
598
599 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
600
601 completeFGResize();
602
603 EXPECT_CROPPED_STATE("after the resize finishes");
604}
605
606TEST_F(CropLatchingTest, FinalCropLatching) {
607 EXPECT_INITIAL_STATE("before anything");
608 // Normally the crop applies immediately even while a resize is pending.
609 SurfaceComposerClient::openGlobalTransaction();
610 mFGSurfaceControl->setSize(128, 128);
611 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
612 SurfaceComposerClient::closeGlobalTransaction(true);
613
614 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
615
616 restoreInitialState();
617
618 SurfaceComposerClient::openGlobalTransaction();
619 mFGSurfaceControl->setSize(128, 128);
620 mFGSurfaceControl->setGeometryAppliesWithResize();
621 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
622 SurfaceComposerClient::closeGlobalTransaction(true);
623
624 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
625
626 completeFGResize();
627
628 EXPECT_CROPPED_STATE("after the resize finishes");
629}
630
Robert Carr7bf247e2017-05-18 14:02:49 -0700631// In this test we ensure that setGeometryAppliesWithResize actually demands
632// a buffer of the new size, and not just any size.
633TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
634 EXPECT_INITIAL_STATE("before anything");
635 // Normally the crop applies immediately even while a resize is pending.
636 SurfaceComposerClient::openGlobalTransaction();
637 mFGSurfaceControl->setSize(128, 128);
638 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
639 SurfaceComposerClient::closeGlobalTransaction(true);
640
641 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
642
643 restoreInitialState();
644
645 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
646 // initiating the resize.
647 lockAndFillFGBuffer();
648
649 SurfaceComposerClient::openGlobalTransaction();
650 mFGSurfaceControl->setSize(128, 128);
651 mFGSurfaceControl->setGeometryAppliesWithResize();
652 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
653 SurfaceComposerClient::closeGlobalTransaction(true);
654
655 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
656
657 // We now submit our old buffer, at the old size, and ensure it doesn't
658 // trigger geometry latching.
659 unlockFGBuffer();
660
661 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
662
663 completeFGResize();
664
665 EXPECT_CROPPED_STATE("after the resize finishes");
666}
667
668TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
669 EXPECT_INITIAL_STATE("before anything");
670 // In this scenario, we attempt to set the final crop a second time while the resize
671 // is still pending, and ensure we are successful. Success meaning the second crop
672 // is the one which eventually latches and not the first.
673 SurfaceComposerClient::openGlobalTransaction();
674 mFGSurfaceControl->setSize(128, 128);
675 mFGSurfaceControl->setGeometryAppliesWithResize();
676 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127));
677 SurfaceComposerClient::closeGlobalTransaction(true);
678
chaviw59f5c562017-06-28 16:39:06 -0700679 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
680
Robert Carr7bf247e2017-05-18 14:02:49 -0700681 SurfaceComposerClient::openGlobalTransaction();
682 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1));
683 SurfaceComposerClient::closeGlobalTransaction(true);
684
chaviw59f5c562017-06-28 16:39:06 -0700685 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -0700686
687 completeFGResize();
688
chaviw59f5c562017-06-28 16:39:06 -0700689 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -0700690}
691
Pablo Ceballos05289c22016-04-14 15:49:55 -0700692TEST_F(LayerUpdateTest, DeferredTransactionTest) {
693 sp<ScreenCapture> sc;
694 {
695 SCOPED_TRACE("before anything");
696 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800697 sc->expectBGColor(32, 32);
698 sc->expectFGColor(96, 96);
699 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700700 }
701
702 // set up two deferred transactions on different frames
703 SurfaceComposerClient::openGlobalTransaction();
704 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75));
705 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
706 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
707 SurfaceComposerClient::closeGlobalTransaction(true);
708
709 SurfaceComposerClient::openGlobalTransaction();
710 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128));
711 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
712 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
713 SurfaceComposerClient::closeGlobalTransaction(true);
714
715 {
716 SCOPED_TRACE("before any trigger");
717 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800718 sc->expectBGColor(32, 32);
719 sc->expectFGColor(96, 96);
720 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700721 }
722
723 // should trigger the first deferred transaction, but not the second one
724 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
725 {
726 SCOPED_TRACE("after first trigger");
727 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800728 sc->expectBGColor(32, 32);
729 sc->checkPixel(96, 96, 162, 63, 96);
730 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700731 }
732
733 // should show up immediately since it's not deferred
734 SurfaceComposerClient::openGlobalTransaction();
735 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0));
736 SurfaceComposerClient::closeGlobalTransaction(true);
737
738 // trigger the second deferred transaction
739 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
740 {
741 SCOPED_TRACE("after second trigger");
742 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800743 sc->expectBGColor(32, 32);
744 sc->expectBGColor(96, 96);
745 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -0700746 }
747}
748
Robert Carrdb66e622017-04-10 16:55:57 -0700749TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
750 sp<ScreenCapture> sc;
751 {
752 SCOPED_TRACE("before adding relative surface");
753 ScreenCapture::captureScreen(&sc);
754 sc->expectBGColor(24, 24);
755 sc->expectFGColor(75, 75);
756 sc->expectBGColor(145, 145);
757 }
758
759 auto relativeSurfaceControl = mComposerClient->createSurface(
760 String8("Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
761 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
762 waitForPostedBuffers();
763
764 // Now we stack the surface above the foreground surface and make sure it is visible.
765 SurfaceComposerClient::openGlobalTransaction();
766 relativeSurfaceControl->setPosition(64, 64);
767 relativeSurfaceControl->show();
768 relativeSurfaceControl->setRelativeLayer(mFGSurfaceControl->getHandle(), 1);
769 SurfaceComposerClient::closeGlobalTransaction(true);
770
771
772 {
773 SCOPED_TRACE("after adding relative surface");
774 ScreenCapture::captureScreen(&sc);
775 // our relative surface should be visible now.
776 sc->checkPixel(75, 75, 255, 177, 177);
777 }
778
779 // A call to setLayer will override a call to setRelativeLayer
780 SurfaceComposerClient::openGlobalTransaction();
781 relativeSurfaceControl->setLayer(0);
782 SurfaceComposerClient::closeGlobalTransaction();
783
784 {
785 SCOPED_TRACE("after set layer");
786 ScreenCapture::captureScreen(&sc);
787 // now the FG surface should be visible again.
788 sc->expectFGColor(75, 75);
789 }
790}
791
Robert Carr1f0a16a2016-10-24 16:27:39 -0700792class ChildLayerTest : public LayerUpdateTest {
793protected:
794 void SetUp() override {
795 LayerUpdateTest::SetUp();
796 mChild = mComposerClient->createSurface(
797 String8("Child surface"),
798 10, 10, PIXEL_FORMAT_RGBA_8888,
799 0, mFGSurfaceControl.get());
800 fillSurfaceRGBA8(mChild, 200, 200, 200);
801
802 {
803 SCOPED_TRACE("before anything");
804 ScreenCapture::captureScreen(&mCapture);
805 mCapture->expectChildColor(64, 64);
806 }
807 }
808 void TearDown() override {
809 LayerUpdateTest::TearDown();
810 mChild = 0;
811 }
812
813 sp<SurfaceControl> mChild;
814 sp<ScreenCapture> mCapture;
815};
816
817TEST_F(ChildLayerTest, ChildLayerPositioning) {
818 SurfaceComposerClient::openGlobalTransaction();
819 mChild->show();
820 mChild->setPosition(10, 10);
821 mFGSurfaceControl->setPosition(64, 64);
822 SurfaceComposerClient::closeGlobalTransaction(true);
823
824 {
825 ScreenCapture::captureScreen(&mCapture);
826 // Top left of foreground must now be visible
827 mCapture->expectFGColor(64, 64);
828 // But 10 pixels in we should see the child surface
829 mCapture->expectChildColor(74, 74);
830 // And 10 more pixels we should be back to the foreground surface
831 mCapture->expectFGColor(84, 84);
832 }
833
834 SurfaceComposerClient::openGlobalTransaction();
835 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0));
836 SurfaceComposerClient::closeGlobalTransaction(true);
837
838 {
839 ScreenCapture::captureScreen(&mCapture);
840 // Top left of foreground should now be at 0, 0
841 mCapture->expectFGColor(0, 0);
842 // But 10 pixels in we should see the child surface
843 mCapture->expectChildColor(10, 10);
844 // And 10 more pixels we should be back to the foreground surface
845 mCapture->expectFGColor(20, 20);
846 }
847}
848
Robert Carr41b08b52017-06-01 16:11:34 -0700849TEST_F(ChildLayerTest, ChildLayerCropping) {
850 SurfaceComposerClient::openGlobalTransaction();
851 mChild->show();
852 mChild->setPosition(0, 0);
853 mFGSurfaceControl->setPosition(0, 0);
854 mFGSurfaceControl->setCrop(Rect(0, 0, 5, 5));
855 SurfaceComposerClient::closeGlobalTransaction(true);
856
857 {
858 ScreenCapture::captureScreen(&mCapture);
859 mCapture->expectChildColor(0, 0);
860 mCapture->expectChildColor(4, 4);
861 mCapture->expectBGColor(5, 5);
862 }
863}
864
865TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
866 SurfaceComposerClient::openGlobalTransaction();
867 mChild->show();
868 mChild->setPosition(0, 0);
869 mFGSurfaceControl->setPosition(0, 0);
870 mFGSurfaceControl->setFinalCrop(Rect(0, 0, 5, 5));
871 SurfaceComposerClient::closeGlobalTransaction(true);
872
873 {
874 ScreenCapture::captureScreen(&mCapture);
875 mCapture->expectChildColor(0, 0);
876 mCapture->expectChildColor(4, 4);
877 mCapture->expectBGColor(5, 5);
878 }
879}
880
Robert Carr1f0a16a2016-10-24 16:27:39 -0700881TEST_F(ChildLayerTest, ChildLayerConstraints) {
882 SurfaceComposerClient::openGlobalTransaction();
883 mChild->show();
884 mFGSurfaceControl->setPosition(0, 0);
885 mChild->setPosition(63, 63);
886 SurfaceComposerClient::closeGlobalTransaction(true);
887
888 {
889 ScreenCapture::captureScreen(&mCapture);
890 mCapture->expectFGColor(0, 0);
891 // Last pixel in foreground should now be the child.
892 mCapture->expectChildColor(63, 63);
893 // But the child should be constrained and the next pixel
894 // must be the background
895 mCapture->expectBGColor(64, 64);
896 }
897}
898
899TEST_F(ChildLayerTest, ChildLayerScaling) {
900 SurfaceComposerClient::openGlobalTransaction();
901 mFGSurfaceControl->setPosition(0, 0);
902 SurfaceComposerClient::closeGlobalTransaction(true);
903
904 // Find the boundary between the parent and child
905 {
906 ScreenCapture::captureScreen(&mCapture);
907 mCapture->expectChildColor(9, 9);
908 mCapture->expectFGColor(10, 10);
909 }
910
911 SurfaceComposerClient::openGlobalTransaction();
912 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0);
913 SurfaceComposerClient::closeGlobalTransaction(true);
914
915 // The boundary should be twice as far from the origin now.
916 // The pixels from the last test should all be child now
917 {
918 ScreenCapture::captureScreen(&mCapture);
919 mCapture->expectChildColor(9, 9);
920 mCapture->expectChildColor(10, 10);
921 mCapture->expectChildColor(19, 19);
922 mCapture->expectFGColor(20, 20);
923 }
924}
Robert Carr9524cb32017-02-13 11:32:32 -0800925
Robert Carr6452f122017-03-21 10:41:29 -0700926TEST_F(ChildLayerTest, ChildLayerAlpha) {
927 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
928 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
929 fillSurfaceRGBA8(mChild, 0, 254, 0);
930 waitForPostedBuffers();
931
932 SurfaceComposerClient::openGlobalTransaction();
933 mChild->show();
934 mChild->setPosition(0, 0);
935 mFGSurfaceControl->setPosition(0, 0);
936 SurfaceComposerClient::closeGlobalTransaction(true);
937
938 {
939 ScreenCapture::captureScreen(&mCapture);
940 // Unblended child color
941 mCapture->checkPixel(0, 0, 0, 254, 0);
942 }
943
944 SurfaceComposerClient::openGlobalTransaction();
945 ASSERT_EQ(NO_ERROR, mChild->setAlpha(0.5));
946 SurfaceComposerClient::closeGlobalTransaction(true);
947
948 {
949 ScreenCapture::captureScreen(&mCapture);
950 // Child and BG blended.
951 mCapture->checkPixel(0, 0, 127, 127, 0);
952 }
953
954 SurfaceComposerClient::openGlobalTransaction();
955 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.5));
956 SurfaceComposerClient::closeGlobalTransaction(true);
957
958 {
959 ScreenCapture::captureScreen(&mCapture);
960 // Child and BG blended.
961 mCapture->checkPixel(0, 0, 95, 64, 95);
962 }
963}
964
Robert Carr9524cb32017-02-13 11:32:32 -0800965TEST_F(ChildLayerTest, ReparentChildren) {
966 SurfaceComposerClient::openGlobalTransaction();
967 mChild->show();
968 mChild->setPosition(10, 10);
969 mFGSurfaceControl->setPosition(64, 64);
970 SurfaceComposerClient::closeGlobalTransaction(true);
971
972 {
973 ScreenCapture::captureScreen(&mCapture);
974 // Top left of foreground must now be visible
975 mCapture->expectFGColor(64, 64);
976 // But 10 pixels in we should see the child surface
977 mCapture->expectChildColor(74, 74);
978 // And 10 more pixels we should be back to the foreground surface
979 mCapture->expectFGColor(84, 84);
980 }
981 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle());
982 {
983 ScreenCapture::captureScreen(&mCapture);
984 mCapture->expectFGColor(64, 64);
985 // In reparenting we should have exposed the entire foreground surface.
986 mCapture->expectFGColor(74, 74);
987 // And the child layer should now begin at 10, 10 (since the BG
988 // layer is at (0, 0)).
989 mCapture->expectBGColor(9, 9);
990 mCapture->expectChildColor(10, 10);
991 }
992}
993
chaviw161410b02017-07-27 10:46:08 -0700994TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr9524cb32017-02-13 11:32:32 -0800995 SurfaceComposerClient::openGlobalTransaction();
996 mChild->show();
997 mChild->setPosition(10, 10);
998 mFGSurfaceControl->setPosition(64, 64);
999 SurfaceComposerClient::closeGlobalTransaction(true);
1000
1001 {
1002 ScreenCapture::captureScreen(&mCapture);
1003 // Top left of foreground must now be visible
1004 mCapture->expectFGColor(64, 64);
1005 // But 10 pixels in we should see the child surface
1006 mCapture->expectChildColor(74, 74);
1007 // And 10 more pixels we should be back to the foreground surface
1008 mCapture->expectFGColor(84, 84);
1009 }
1010
1011 SurfaceComposerClient::openGlobalTransaction();
1012 mFGSurfaceControl->detachChildren();
Robert Carr8d5227b2017-03-16 15:41:03 -07001013 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -08001014
1015 SurfaceComposerClient::openGlobalTransaction();
1016 mChild->hide();
Robert Carr8d5227b2017-03-16 15:41:03 -07001017 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -08001018
chaviw161410b02017-07-27 10:46:08 -07001019 // Since the child has the same client as the parent, it will not get
1020 // detached and will be hidden.
1021 {
1022 ScreenCapture::captureScreen(&mCapture);
1023 mCapture->expectFGColor(64, 64);
1024 mCapture->expectFGColor(74, 74);
1025 mCapture->expectFGColor(84, 84);
1026 }
1027}
1028
1029TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1030 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
1031 sp<SurfaceControl> mChildNewClient = mNewComposerClient->createSurface(
1032 String8("New Child Test Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1033 0, mFGSurfaceControl.get());
1034
1035 ASSERT_TRUE(mChildNewClient != NULL);
1036 ASSERT_TRUE(mChildNewClient->isValid());
1037
1038 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1039
1040 SurfaceComposerClient::openGlobalTransaction();
1041 mChild->hide();
1042 mChildNewClient->show();
1043 mChildNewClient->setPosition(10, 10);
1044 mFGSurfaceControl->setPosition(64, 64);
1045 SurfaceComposerClient::closeGlobalTransaction(true);
1046
1047 {
1048 ScreenCapture::captureScreen(&mCapture);
1049 // Top left of foreground must now be visible
1050 mCapture->expectFGColor(64, 64);
1051 // But 10 pixels in we should see the child surface
1052 mCapture->expectChildColor(74, 74);
1053 // And 10 more pixels we should be back to the foreground surface
1054 mCapture->expectFGColor(84, 84);
1055 }
1056
1057 SurfaceComposerClient::openGlobalTransaction();
1058 mFGSurfaceControl->detachChildren();
1059 SurfaceComposerClient::closeGlobalTransaction(true);
1060
1061 SurfaceComposerClient::openGlobalTransaction();
1062 mChildNewClient->hide();
1063 SurfaceComposerClient::closeGlobalTransaction(true);
1064
Robert Carr9524cb32017-02-13 11:32:32 -08001065 // Nothing should have changed.
1066 {
1067 ScreenCapture::captureScreen(&mCapture);
1068 mCapture->expectFGColor(64, 64);
1069 mCapture->expectChildColor(74, 74);
1070 mCapture->expectFGColor(84, 84);
1071 }
1072}
1073
Robert Carr9b429f42017-04-17 14:56:57 -07001074TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
1075 SurfaceComposerClient::openGlobalTransaction();
1076 mChild->show();
1077 mChild->setPosition(0, 0);
1078 mFGSurfaceControl->setPosition(0, 0);
1079 SurfaceComposerClient::closeGlobalTransaction(true);
1080
1081 {
1082 ScreenCapture::captureScreen(&mCapture);
1083 // We've positioned the child in the top left.
1084 mCapture->expectChildColor(0, 0);
1085 // But it's only 10x10.
1086 mCapture->expectFGColor(10, 10);
1087 }
1088
1089 SurfaceComposerClient::openGlobalTransaction();
1090 mFGSurfaceControl->setOverrideScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1091 // We cause scaling by 2.
1092 mFGSurfaceControl->setSize(128, 128);
1093 SurfaceComposerClient::closeGlobalTransaction();
1094
1095 {
1096 ScreenCapture::captureScreen(&mCapture);
1097 // We've positioned the child in the top left.
1098 mCapture->expectChildColor(0, 0);
1099 mCapture->expectChildColor(10, 10);
1100 mCapture->expectChildColor(19, 19);
1101 // And now it should be scaled all the way to 20x20
1102 mCapture->expectFGColor(20, 20);
1103 }
1104}
1105
Robert Carr1725eee2017-04-26 18:32:15 -07001106// Regression test for b/37673612
1107TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
1108 SurfaceComposerClient::openGlobalTransaction();
1109 mChild->show();
1110 mChild->setPosition(0, 0);
1111 mFGSurfaceControl->setPosition(0, 0);
1112 SurfaceComposerClient::closeGlobalTransaction(true);
1113
1114 {
1115 ScreenCapture::captureScreen(&mCapture);
1116 // We've positioned the child in the top left.
1117 mCapture->expectChildColor(0, 0);
1118 // But it's only 10x10.
1119 mCapture->expectFGColor(10, 10);
1120 }
1121
1122
1123 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1124 // the WM specified state size.
1125 mFGSurfaceControl->setSize(128, 64);
1126 sp<Surface> s = mFGSurfaceControl->getSurface();
1127 auto anw = static_cast<ANativeWindow*>(s.get());
1128 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1129 native_window_set_buffers_dimensions(anw, 64, 128);
1130 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1131 waitForPostedBuffers();
1132
1133 {
1134 // The child should still be in the same place and not have any strange scaling as in
1135 // b/37673612.
1136 ScreenCapture::captureScreen(&mCapture);
1137 mCapture->expectChildColor(0, 0);
1138 mCapture->expectFGColor(10, 10);
1139 }
1140}
1141
Dan Stoza412903f2017-04-27 13:42:17 -07001142TEST_F(ChildLayerTest, Bug36858924) {
1143 // Destroy the child layer
1144 mChild.clear();
1145
1146 // Now recreate it as hidden
1147 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1148 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1149 mFGSurfaceControl.get());
1150
1151 // Show the child layer in a deferred transaction
1152 SurfaceComposerClient::openGlobalTransaction();
1153 mChild->deferTransactionUntil(mFGSurfaceControl->getHandle(),
1154 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1155 mChild->show();
1156 SurfaceComposerClient::closeGlobalTransaction(true);
1157
1158 // Render the foreground surface a few times
1159 //
1160 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1161 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1162 // never acquire/release the first buffer
1163 ALOGI("Filling 1");
1164 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1165 ALOGI("Filling 2");
1166 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1167 ALOGI("Filling 3");
1168 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1169 ALOGI("Filling 4");
1170 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1171}
1172
chaviwf1961f72017-09-18 16:41:07 -07001173TEST_F(ChildLayerTest, Reparent) {
chaviw06178942017-07-27 10:25:59 -07001174 SurfaceComposerClient::openGlobalTransaction();
1175 mChild->show();
1176 mChild->setPosition(10, 10);
1177 mFGSurfaceControl->setPosition(64, 64);
1178 SurfaceComposerClient::closeGlobalTransaction(true);
1179
1180 {
1181 ScreenCapture::captureScreen(&mCapture);
1182 // Top left of foreground must now be visible
1183 mCapture->expectFGColor(64, 64);
1184 // But 10 pixels in we should see the child surface
1185 mCapture->expectChildColor(74, 74);
1186 // And 10 more pixels we should be back to the foreground surface
1187 mCapture->expectFGColor(84, 84);
1188 }
chaviwf1961f72017-09-18 16:41:07 -07001189 mChild->reparent(mBGSurfaceControl->getHandle());
chaviw06178942017-07-27 10:25:59 -07001190 {
1191 ScreenCapture::captureScreen(&mCapture);
1192 mCapture->expectFGColor(64, 64);
1193 // In reparenting we should have exposed the entire foreground surface.
1194 mCapture->expectFGColor(74, 74);
1195 // And the child layer should now begin at 10, 10 (since the BG
1196 // layer is at (0, 0)).
1197 mCapture->expectBGColor(9, 9);
1198 mCapture->expectChildColor(10, 10);
1199 }
1200}
1201
chaviwf1961f72017-09-18 16:41:07 -07001202TEST_F(ChildLayerTest, ReparentToNoParent) {
1203 SurfaceComposerClient::openGlobalTransaction();
1204 mChild->show();
1205 mChild->setPosition(10, 10);
1206 mFGSurfaceControl->setPosition(64, 64);
1207 SurfaceComposerClient::closeGlobalTransaction(true);
1208
1209 {
1210 ScreenCapture::captureScreen(&mCapture);
1211 // Top left of foreground must now be visible
1212 mCapture->expectFGColor(64, 64);
1213 // But 10 pixels in we should see the child surface
1214 mCapture->expectChildColor(74, 74);
1215 // And 10 more pixels we should be back to the foreground surface
1216 mCapture->expectFGColor(84, 84);
1217 }
1218 mChild->reparent(nullptr);
1219 {
1220 ScreenCapture::captureScreen(&mCapture);
1221 // Nothing should have changed.
1222 mCapture->expectFGColor(64, 64);
1223 mCapture->expectChildColor(74, 74);
1224 mCapture->expectFGColor(84, 84);
1225 }
1226}
1227
1228TEST_F(ChildLayerTest, ReparentFromNoParent) {
1229 sp<SurfaceControl> newSurface = mComposerClient->createSurface(
1230 String8("New Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, 0);
1231 ASSERT_TRUE(newSurface != NULL);
1232 ASSERT_TRUE(newSurface->isValid());
1233
1234 fillSurfaceRGBA8(newSurface, 63, 195, 63);
1235 SurfaceComposerClient::openGlobalTransaction();
1236 mChild->hide();
1237 newSurface->show();
1238 newSurface->setPosition(10, 10);
1239 newSurface->setLayer(INT32_MAX-2);
1240 mFGSurfaceControl->setPosition(64, 64);
1241 SurfaceComposerClient::closeGlobalTransaction(true);
1242
1243 {
1244 ScreenCapture::captureScreen(&mCapture);
1245 // Top left of foreground must now be visible
1246 mCapture->expectFGColor(64, 64);
1247 // At 10, 10 we should see the new surface
1248 mCapture->checkPixel(10, 10, 63, 195, 63);
1249 }
1250
1251 SurfaceComposerClient::openGlobalTransaction();
1252 newSurface->reparent(mFGSurfaceControl->getHandle());
1253 SurfaceComposerClient::closeGlobalTransaction(true);
1254
1255 {
1256 ScreenCapture::captureScreen(&mCapture);
1257 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1258 // mFGSurface, putting it at 74, 74.
1259 mCapture->expectFGColor(64, 64);
1260 mCapture->checkPixel(74, 74, 63, 195, 63);
1261 mCapture->expectFGColor(84, 84);
1262 }
1263}
1264
chaviwc9674332017-08-28 12:32:18 -07001265TEST_F(ChildLayerTest, NestedChildren) {
1266 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1267 String8("Grandchild surface"),
1268 10, 10, PIXEL_FORMAT_RGBA_8888,
1269 0, mChild.get());
1270 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1271
1272 {
1273 ScreenCapture::captureScreen(&mCapture);
1274 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1275 // which begins at 64, 64
1276 mCapture->checkPixel(64, 64, 50, 50, 50);
1277 }
1278}
1279
chaviw13fdc492017-06-27 12:40:18 -07001280class LayerColorTest : public LayerUpdateTest {
1281 protected:
1282 void SetUp() override {
1283 LayerUpdateTest::SetUp();
1284
1285 mLayerColorControl = mComposerClient->createSurface(
1286 String8("Layer color surface"),
1287 128, 128, PIXEL_FORMAT_RGBA_8888,
1288 ISurfaceComposerClient::eFXSurfaceColor);
1289
1290 ASSERT_TRUE(mLayerColorControl != NULL);
1291 ASSERT_TRUE(mLayerColorControl->isValid());
1292
1293 SurfaceComposerClient::openGlobalTransaction();
1294 ASSERT_EQ(NO_ERROR, mLayerColorControl->setLayer(INT32_MAX-1));
1295 ASSERT_EQ(NO_ERROR, mLayerColorControl->setPosition(140, 140));
1296 ASSERT_EQ(NO_ERROR, mLayerColorControl->hide());
1297 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->hide());
1298 SurfaceComposerClient::closeGlobalTransaction(true);
1299 }
1300
1301 void TearDown() override {
1302 LayerUpdateTest::TearDown();
1303 mLayerColorControl = 0;
1304 }
1305
1306 sp<SurfaceControl> mLayerColorControl;
1307};
1308
1309TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1310 sp<ScreenCapture> sc;
1311
1312 {
1313 SCOPED_TRACE("before setColor");
1314 ScreenCapture::captureScreen(&sc);
1315 sc->expectBGColor(145, 145);
1316 }
1317
1318
1319 SurfaceComposerClient::openGlobalTransaction();
1320 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1321 mLayerColorControl->setColor(color);
1322 mLayerColorControl->show();
1323 SurfaceComposerClient::closeGlobalTransaction(true);
1324 {
1325 // There should now be a color
1326 SCOPED_TRACE("after setColor");
1327 ScreenCapture::captureScreen(&sc);
1328 sc->checkPixel(145, 145, 43, 207, 131);
1329 }
1330}
1331
1332TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1333 sp<ScreenCapture> sc;
1334 {
1335 SCOPED_TRACE("before setColor");
1336 ScreenCapture::captureScreen(&sc);
1337 sc->expectBGColor(145, 145);
1338 }
1339
1340 SurfaceComposerClient::openGlobalTransaction();
1341 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1342 mLayerColorControl->setColor(color);
1343 mLayerColorControl->setAlpha(.75f);
1344 mLayerColorControl->show();
1345 SurfaceComposerClient::closeGlobalTransaction(true);
1346 {
1347 // There should now be a color with .75 alpha
1348 SCOPED_TRACE("after setColor");
1349 ScreenCapture::captureScreen(&sc);
1350 sc->checkPixel(145, 145, 48, 171, 147);
1351 }
1352}
1353
1354TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1355 sp<ScreenCapture> sc;
1356 {
1357 SCOPED_TRACE("before setColor");
1358 ScreenCapture::captureScreen(&sc);
1359 sc->expectBGColor(145, 145);
1360 }
1361
1362 SurfaceComposerClient::openGlobalTransaction();
1363 mLayerColorControl->show();
1364 SurfaceComposerClient::closeGlobalTransaction(true);
1365 {
1366 // There should now be set to 0,0,0 (black) as default.
1367 SCOPED_TRACE("after setColor");
1368 ScreenCapture::captureScreen(&sc);
1369 sc->checkPixel(145, 145, 0, 0, 0);
1370 }
1371}
1372
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001373}