blob: d285785c83430b28baf1f4caf55744bbfd12fb4b [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 Carre392b552017-09-19 12:16:05 -0700792TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
793 sp<ScreenCapture> sc;
794
795 sp<SurfaceControl> childNoBuffer =
796 mComposerClient->createSurface(String8("Bufferless child"),
797 10, 10, PIXEL_FORMAT_RGBA_8888,
798 0, mFGSurfaceControl.get());
799 sp<SurfaceControl> childBuffer = mComposerClient->createSurface(
800 String8("Buffered child"), 20, 20,
801 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
802 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
803
804 SurfaceComposerClient::openGlobalTransaction();
805 childNoBuffer->show();
806 childBuffer->show();
807 SurfaceComposerClient::closeGlobalTransaction();
808
809
810 {
811 ScreenCapture::captureScreen(&sc);
812 sc->expectChildColor(73, 73);
813 sc->expectFGColor(74, 74);
814 }
815
816 SurfaceComposerClient::openGlobalTransaction();
817 childNoBuffer->setSize(20, 20);
818 SurfaceComposerClient::closeGlobalTransaction(true);
819
820 {
821 ScreenCapture::captureScreen(&sc);
822 sc->expectChildColor(73, 73);
823 sc->expectChildColor(74, 74);
824 }
825}
826
Robert Carr1f0a16a2016-10-24 16:27:39 -0700827class ChildLayerTest : public LayerUpdateTest {
828protected:
829 void SetUp() override {
830 LayerUpdateTest::SetUp();
831 mChild = mComposerClient->createSurface(
832 String8("Child surface"),
833 10, 10, PIXEL_FORMAT_RGBA_8888,
834 0, mFGSurfaceControl.get());
835 fillSurfaceRGBA8(mChild, 200, 200, 200);
836
837 {
838 SCOPED_TRACE("before anything");
839 ScreenCapture::captureScreen(&mCapture);
840 mCapture->expectChildColor(64, 64);
841 }
842 }
843 void TearDown() override {
844 LayerUpdateTest::TearDown();
845 mChild = 0;
846 }
847
848 sp<SurfaceControl> mChild;
849 sp<ScreenCapture> mCapture;
850};
851
852TEST_F(ChildLayerTest, ChildLayerPositioning) {
853 SurfaceComposerClient::openGlobalTransaction();
854 mChild->show();
855 mChild->setPosition(10, 10);
856 mFGSurfaceControl->setPosition(64, 64);
857 SurfaceComposerClient::closeGlobalTransaction(true);
858
859 {
860 ScreenCapture::captureScreen(&mCapture);
861 // Top left of foreground must now be visible
862 mCapture->expectFGColor(64, 64);
863 // But 10 pixels in we should see the child surface
864 mCapture->expectChildColor(74, 74);
865 // And 10 more pixels we should be back to the foreground surface
866 mCapture->expectFGColor(84, 84);
867 }
868
869 SurfaceComposerClient::openGlobalTransaction();
870 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0));
871 SurfaceComposerClient::closeGlobalTransaction(true);
872
873 {
874 ScreenCapture::captureScreen(&mCapture);
875 // Top left of foreground should now be at 0, 0
876 mCapture->expectFGColor(0, 0);
877 // But 10 pixels in we should see the child surface
878 mCapture->expectChildColor(10, 10);
879 // And 10 more pixels we should be back to the foreground surface
880 mCapture->expectFGColor(20, 20);
881 }
882}
883
Robert Carr41b08b52017-06-01 16:11:34 -0700884TEST_F(ChildLayerTest, ChildLayerCropping) {
885 SurfaceComposerClient::openGlobalTransaction();
886 mChild->show();
887 mChild->setPosition(0, 0);
888 mFGSurfaceControl->setPosition(0, 0);
889 mFGSurfaceControl->setCrop(Rect(0, 0, 5, 5));
890 SurfaceComposerClient::closeGlobalTransaction(true);
891
892 {
893 ScreenCapture::captureScreen(&mCapture);
894 mCapture->expectChildColor(0, 0);
895 mCapture->expectChildColor(4, 4);
896 mCapture->expectBGColor(5, 5);
897 }
898}
899
900TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
901 SurfaceComposerClient::openGlobalTransaction();
902 mChild->show();
903 mChild->setPosition(0, 0);
904 mFGSurfaceControl->setPosition(0, 0);
905 mFGSurfaceControl->setFinalCrop(Rect(0, 0, 5, 5));
906 SurfaceComposerClient::closeGlobalTransaction(true);
907
908 {
909 ScreenCapture::captureScreen(&mCapture);
910 mCapture->expectChildColor(0, 0);
911 mCapture->expectChildColor(4, 4);
912 mCapture->expectBGColor(5, 5);
913 }
914}
915
Robert Carr1f0a16a2016-10-24 16:27:39 -0700916TEST_F(ChildLayerTest, ChildLayerConstraints) {
917 SurfaceComposerClient::openGlobalTransaction();
918 mChild->show();
919 mFGSurfaceControl->setPosition(0, 0);
920 mChild->setPosition(63, 63);
921 SurfaceComposerClient::closeGlobalTransaction(true);
922
923 {
924 ScreenCapture::captureScreen(&mCapture);
925 mCapture->expectFGColor(0, 0);
926 // Last pixel in foreground should now be the child.
927 mCapture->expectChildColor(63, 63);
928 // But the child should be constrained and the next pixel
929 // must be the background
930 mCapture->expectBGColor(64, 64);
931 }
932}
933
934TEST_F(ChildLayerTest, ChildLayerScaling) {
935 SurfaceComposerClient::openGlobalTransaction();
936 mFGSurfaceControl->setPosition(0, 0);
937 SurfaceComposerClient::closeGlobalTransaction(true);
938
939 // Find the boundary between the parent and child
940 {
941 ScreenCapture::captureScreen(&mCapture);
942 mCapture->expectChildColor(9, 9);
943 mCapture->expectFGColor(10, 10);
944 }
945
946 SurfaceComposerClient::openGlobalTransaction();
947 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0);
948 SurfaceComposerClient::closeGlobalTransaction(true);
949
950 // The boundary should be twice as far from the origin now.
951 // The pixels from the last test should all be child now
952 {
953 ScreenCapture::captureScreen(&mCapture);
954 mCapture->expectChildColor(9, 9);
955 mCapture->expectChildColor(10, 10);
956 mCapture->expectChildColor(19, 19);
957 mCapture->expectFGColor(20, 20);
958 }
959}
Robert Carr9524cb32017-02-13 11:32:32 -0800960
Robert Carr6452f122017-03-21 10:41:29 -0700961TEST_F(ChildLayerTest, ChildLayerAlpha) {
962 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
963 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
964 fillSurfaceRGBA8(mChild, 0, 254, 0);
965 waitForPostedBuffers();
966
967 SurfaceComposerClient::openGlobalTransaction();
968 mChild->show();
969 mChild->setPosition(0, 0);
970 mFGSurfaceControl->setPosition(0, 0);
971 SurfaceComposerClient::closeGlobalTransaction(true);
972
973 {
974 ScreenCapture::captureScreen(&mCapture);
975 // Unblended child color
976 mCapture->checkPixel(0, 0, 0, 254, 0);
977 }
978
979 SurfaceComposerClient::openGlobalTransaction();
980 ASSERT_EQ(NO_ERROR, mChild->setAlpha(0.5));
981 SurfaceComposerClient::closeGlobalTransaction(true);
982
983 {
984 ScreenCapture::captureScreen(&mCapture);
985 // Child and BG blended.
986 mCapture->checkPixel(0, 0, 127, 127, 0);
987 }
988
989 SurfaceComposerClient::openGlobalTransaction();
990 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.5));
991 SurfaceComposerClient::closeGlobalTransaction(true);
992
993 {
994 ScreenCapture::captureScreen(&mCapture);
995 // Child and BG blended.
996 mCapture->checkPixel(0, 0, 95, 64, 95);
997 }
998}
999
Robert Carr9524cb32017-02-13 11:32:32 -08001000TEST_F(ChildLayerTest, ReparentChildren) {
1001 SurfaceComposerClient::openGlobalTransaction();
1002 mChild->show();
1003 mChild->setPosition(10, 10);
1004 mFGSurfaceControl->setPosition(64, 64);
1005 SurfaceComposerClient::closeGlobalTransaction(true);
1006
1007 {
1008 ScreenCapture::captureScreen(&mCapture);
1009 // Top left of foreground must now be visible
1010 mCapture->expectFGColor(64, 64);
1011 // But 10 pixels in we should see the child surface
1012 mCapture->expectChildColor(74, 74);
1013 // And 10 more pixels we should be back to the foreground surface
1014 mCapture->expectFGColor(84, 84);
1015 }
1016 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle());
1017 {
1018 ScreenCapture::captureScreen(&mCapture);
1019 mCapture->expectFGColor(64, 64);
1020 // In reparenting we should have exposed the entire foreground surface.
1021 mCapture->expectFGColor(74, 74);
1022 // And the child layer should now begin at 10, 10 (since the BG
1023 // layer is at (0, 0)).
1024 mCapture->expectBGColor(9, 9);
1025 mCapture->expectChildColor(10, 10);
1026 }
1027}
1028
chaviw161410b02017-07-27 10:46:08 -07001029TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr9524cb32017-02-13 11:32:32 -08001030 SurfaceComposerClient::openGlobalTransaction();
1031 mChild->show();
1032 mChild->setPosition(10, 10);
1033 mFGSurfaceControl->setPosition(64, 64);
1034 SurfaceComposerClient::closeGlobalTransaction(true);
1035
1036 {
1037 ScreenCapture::captureScreen(&mCapture);
1038 // Top left of foreground must now be visible
1039 mCapture->expectFGColor(64, 64);
1040 // But 10 pixels in we should see the child surface
1041 mCapture->expectChildColor(74, 74);
1042 // And 10 more pixels we should be back to the foreground surface
1043 mCapture->expectFGColor(84, 84);
1044 }
1045
1046 SurfaceComposerClient::openGlobalTransaction();
1047 mFGSurfaceControl->detachChildren();
Robert Carr8d5227b2017-03-16 15:41:03 -07001048 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -08001049
1050 SurfaceComposerClient::openGlobalTransaction();
1051 mChild->hide();
Robert Carr8d5227b2017-03-16 15:41:03 -07001052 SurfaceComposerClient::closeGlobalTransaction(true);
Robert Carr9524cb32017-02-13 11:32:32 -08001053
chaviw161410b02017-07-27 10:46:08 -07001054 // Since the child has the same client as the parent, it will not get
1055 // detached and will be hidden.
1056 {
1057 ScreenCapture::captureScreen(&mCapture);
1058 mCapture->expectFGColor(64, 64);
1059 mCapture->expectFGColor(74, 74);
1060 mCapture->expectFGColor(84, 84);
1061 }
1062}
1063
1064TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1065 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
1066 sp<SurfaceControl> mChildNewClient = mNewComposerClient->createSurface(
1067 String8("New Child Test Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1068 0, mFGSurfaceControl.get());
1069
1070 ASSERT_TRUE(mChildNewClient != NULL);
1071 ASSERT_TRUE(mChildNewClient->isValid());
1072
1073 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1074
1075 SurfaceComposerClient::openGlobalTransaction();
1076 mChild->hide();
1077 mChildNewClient->show();
1078 mChildNewClient->setPosition(10, 10);
1079 mFGSurfaceControl->setPosition(64, 64);
1080 SurfaceComposerClient::closeGlobalTransaction(true);
1081
1082 {
1083 ScreenCapture::captureScreen(&mCapture);
1084 // Top left of foreground must now be visible
1085 mCapture->expectFGColor(64, 64);
1086 // But 10 pixels in we should see the child surface
1087 mCapture->expectChildColor(74, 74);
1088 // And 10 more pixels we should be back to the foreground surface
1089 mCapture->expectFGColor(84, 84);
1090 }
1091
1092 SurfaceComposerClient::openGlobalTransaction();
1093 mFGSurfaceControl->detachChildren();
1094 SurfaceComposerClient::closeGlobalTransaction(true);
1095
1096 SurfaceComposerClient::openGlobalTransaction();
1097 mChildNewClient->hide();
1098 SurfaceComposerClient::closeGlobalTransaction(true);
1099
Robert Carr9524cb32017-02-13 11:32:32 -08001100 // Nothing should have changed.
1101 {
1102 ScreenCapture::captureScreen(&mCapture);
1103 mCapture->expectFGColor(64, 64);
1104 mCapture->expectChildColor(74, 74);
1105 mCapture->expectFGColor(84, 84);
1106 }
1107}
1108
Robert Carr9b429f42017-04-17 14:56:57 -07001109TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
1110 SurfaceComposerClient::openGlobalTransaction();
1111 mChild->show();
1112 mChild->setPosition(0, 0);
1113 mFGSurfaceControl->setPosition(0, 0);
1114 SurfaceComposerClient::closeGlobalTransaction(true);
1115
1116 {
1117 ScreenCapture::captureScreen(&mCapture);
1118 // We've positioned the child in the top left.
1119 mCapture->expectChildColor(0, 0);
1120 // But it's only 10x10.
1121 mCapture->expectFGColor(10, 10);
1122 }
1123
1124 SurfaceComposerClient::openGlobalTransaction();
1125 mFGSurfaceControl->setOverrideScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1126 // We cause scaling by 2.
1127 mFGSurfaceControl->setSize(128, 128);
1128 SurfaceComposerClient::closeGlobalTransaction();
1129
1130 {
1131 ScreenCapture::captureScreen(&mCapture);
1132 // We've positioned the child in the top left.
1133 mCapture->expectChildColor(0, 0);
1134 mCapture->expectChildColor(10, 10);
1135 mCapture->expectChildColor(19, 19);
1136 // And now it should be scaled all the way to 20x20
1137 mCapture->expectFGColor(20, 20);
1138 }
1139}
1140
Robert Carr1725eee2017-04-26 18:32:15 -07001141// Regression test for b/37673612
1142TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
1143 SurfaceComposerClient::openGlobalTransaction();
1144 mChild->show();
1145 mChild->setPosition(0, 0);
1146 mFGSurfaceControl->setPosition(0, 0);
1147 SurfaceComposerClient::closeGlobalTransaction(true);
1148
1149 {
1150 ScreenCapture::captureScreen(&mCapture);
1151 // We've positioned the child in the top left.
1152 mCapture->expectChildColor(0, 0);
1153 // But it's only 10x10.
1154 mCapture->expectFGColor(10, 10);
1155 }
1156
1157
1158 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1159 // the WM specified state size.
1160 mFGSurfaceControl->setSize(128, 64);
1161 sp<Surface> s = mFGSurfaceControl->getSurface();
1162 auto anw = static_cast<ANativeWindow*>(s.get());
1163 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1164 native_window_set_buffers_dimensions(anw, 64, 128);
1165 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1166 waitForPostedBuffers();
1167
1168 {
1169 // The child should still be in the same place and not have any strange scaling as in
1170 // b/37673612.
1171 ScreenCapture::captureScreen(&mCapture);
1172 mCapture->expectChildColor(0, 0);
1173 mCapture->expectFGColor(10, 10);
1174 }
1175}
1176
Dan Stoza412903f2017-04-27 13:42:17 -07001177TEST_F(ChildLayerTest, Bug36858924) {
1178 // Destroy the child layer
1179 mChild.clear();
1180
1181 // Now recreate it as hidden
1182 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1183 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1184 mFGSurfaceControl.get());
1185
1186 // Show the child layer in a deferred transaction
1187 SurfaceComposerClient::openGlobalTransaction();
1188 mChild->deferTransactionUntil(mFGSurfaceControl->getHandle(),
1189 mFGSurfaceControl->getSurface()->getNextFrameNumber());
1190 mChild->show();
1191 SurfaceComposerClient::closeGlobalTransaction(true);
1192
1193 // Render the foreground surface a few times
1194 //
1195 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1196 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1197 // never acquire/release the first buffer
1198 ALOGI("Filling 1");
1199 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1200 ALOGI("Filling 2");
1201 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1202 ALOGI("Filling 3");
1203 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1204 ALOGI("Filling 4");
1205 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1206}
1207
chaviwf1961f72017-09-18 16:41:07 -07001208TEST_F(ChildLayerTest, Reparent) {
chaviw06178942017-07-27 10:25:59 -07001209 SurfaceComposerClient::openGlobalTransaction();
1210 mChild->show();
1211 mChild->setPosition(10, 10);
1212 mFGSurfaceControl->setPosition(64, 64);
1213 SurfaceComposerClient::closeGlobalTransaction(true);
1214
1215 {
1216 ScreenCapture::captureScreen(&mCapture);
1217 // Top left of foreground must now be visible
1218 mCapture->expectFGColor(64, 64);
1219 // But 10 pixels in we should see the child surface
1220 mCapture->expectChildColor(74, 74);
1221 // And 10 more pixels we should be back to the foreground surface
1222 mCapture->expectFGColor(84, 84);
1223 }
chaviwf1961f72017-09-18 16:41:07 -07001224 mChild->reparent(mBGSurfaceControl->getHandle());
chaviw06178942017-07-27 10:25:59 -07001225 {
1226 ScreenCapture::captureScreen(&mCapture);
1227 mCapture->expectFGColor(64, 64);
1228 // In reparenting we should have exposed the entire foreground surface.
1229 mCapture->expectFGColor(74, 74);
1230 // And the child layer should now begin at 10, 10 (since the BG
1231 // layer is at (0, 0)).
1232 mCapture->expectBGColor(9, 9);
1233 mCapture->expectChildColor(10, 10);
1234 }
1235}
1236
chaviwf1961f72017-09-18 16:41:07 -07001237TEST_F(ChildLayerTest, ReparentToNoParent) {
1238 SurfaceComposerClient::openGlobalTransaction();
1239 mChild->show();
1240 mChild->setPosition(10, 10);
1241 mFGSurfaceControl->setPosition(64, 64);
1242 SurfaceComposerClient::closeGlobalTransaction(true);
1243
1244 {
1245 ScreenCapture::captureScreen(&mCapture);
1246 // Top left of foreground must now be visible
1247 mCapture->expectFGColor(64, 64);
1248 // But 10 pixels in we should see the child surface
1249 mCapture->expectChildColor(74, 74);
1250 // And 10 more pixels we should be back to the foreground surface
1251 mCapture->expectFGColor(84, 84);
1252 }
1253 mChild->reparent(nullptr);
1254 {
1255 ScreenCapture::captureScreen(&mCapture);
1256 // Nothing should have changed.
1257 mCapture->expectFGColor(64, 64);
1258 mCapture->expectChildColor(74, 74);
1259 mCapture->expectFGColor(84, 84);
1260 }
1261}
1262
1263TEST_F(ChildLayerTest, ReparentFromNoParent) {
1264 sp<SurfaceControl> newSurface = mComposerClient->createSurface(
1265 String8("New Surface"), 10, 10, PIXEL_FORMAT_RGBA_8888, 0);
1266 ASSERT_TRUE(newSurface != NULL);
1267 ASSERT_TRUE(newSurface->isValid());
1268
1269 fillSurfaceRGBA8(newSurface, 63, 195, 63);
1270 SurfaceComposerClient::openGlobalTransaction();
1271 mChild->hide();
1272 newSurface->show();
1273 newSurface->setPosition(10, 10);
1274 newSurface->setLayer(INT32_MAX-2);
1275 mFGSurfaceControl->setPosition(64, 64);
1276 SurfaceComposerClient::closeGlobalTransaction(true);
1277
1278 {
1279 ScreenCapture::captureScreen(&mCapture);
1280 // Top left of foreground must now be visible
1281 mCapture->expectFGColor(64, 64);
1282 // At 10, 10 we should see the new surface
1283 mCapture->checkPixel(10, 10, 63, 195, 63);
1284 }
1285
1286 SurfaceComposerClient::openGlobalTransaction();
1287 newSurface->reparent(mFGSurfaceControl->getHandle());
1288 SurfaceComposerClient::closeGlobalTransaction(true);
1289
1290 {
1291 ScreenCapture::captureScreen(&mCapture);
1292 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1293 // mFGSurface, putting it at 74, 74.
1294 mCapture->expectFGColor(64, 64);
1295 mCapture->checkPixel(74, 74, 63, 195, 63);
1296 mCapture->expectFGColor(84, 84);
1297 }
1298}
1299
chaviwc9674332017-08-28 12:32:18 -07001300TEST_F(ChildLayerTest, NestedChildren) {
1301 sp<SurfaceControl> grandchild = mComposerClient->createSurface(
1302 String8("Grandchild surface"),
1303 10, 10, PIXEL_FORMAT_RGBA_8888,
1304 0, mChild.get());
1305 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1306
1307 {
1308 ScreenCapture::captureScreen(&mCapture);
1309 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1310 // which begins at 64, 64
1311 mCapture->checkPixel(64, 64, 50, 50, 50);
1312 }
1313}
1314
chaviw13fdc492017-06-27 12:40:18 -07001315class LayerColorTest : public LayerUpdateTest {
1316 protected:
1317 void SetUp() override {
1318 LayerUpdateTest::SetUp();
1319
1320 mLayerColorControl = mComposerClient->createSurface(
1321 String8("Layer color surface"),
1322 128, 128, PIXEL_FORMAT_RGBA_8888,
1323 ISurfaceComposerClient::eFXSurfaceColor);
1324
1325 ASSERT_TRUE(mLayerColorControl != NULL);
1326 ASSERT_TRUE(mLayerColorControl->isValid());
1327
1328 SurfaceComposerClient::openGlobalTransaction();
1329 ASSERT_EQ(NO_ERROR, mLayerColorControl->setLayer(INT32_MAX-1));
1330 ASSERT_EQ(NO_ERROR, mLayerColorControl->setPosition(140, 140));
1331 ASSERT_EQ(NO_ERROR, mLayerColorControl->hide());
1332 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->hide());
1333 SurfaceComposerClient::closeGlobalTransaction(true);
1334 }
1335
1336 void TearDown() override {
1337 LayerUpdateTest::TearDown();
1338 mLayerColorControl = 0;
1339 }
1340
1341 sp<SurfaceControl> mLayerColorControl;
1342};
1343
1344TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1345 sp<ScreenCapture> sc;
1346
1347 {
1348 SCOPED_TRACE("before setColor");
1349 ScreenCapture::captureScreen(&sc);
1350 sc->expectBGColor(145, 145);
1351 }
1352
1353
1354 SurfaceComposerClient::openGlobalTransaction();
1355 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1356 mLayerColorControl->setColor(color);
1357 mLayerColorControl->show();
1358 SurfaceComposerClient::closeGlobalTransaction(true);
1359 {
1360 // There should now be a color
1361 SCOPED_TRACE("after setColor");
1362 ScreenCapture::captureScreen(&sc);
1363 sc->checkPixel(145, 145, 43, 207, 131);
1364 }
1365}
1366
1367TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1368 sp<ScreenCapture> sc;
1369 {
1370 SCOPED_TRACE("before setColor");
1371 ScreenCapture::captureScreen(&sc);
1372 sc->expectBGColor(145, 145);
1373 }
1374
1375 SurfaceComposerClient::openGlobalTransaction();
1376 half3 color(43.0f/255.0f, 207.0f/255.0f, 131.0f/255.0f);
1377 mLayerColorControl->setColor(color);
1378 mLayerColorControl->setAlpha(.75f);
1379 mLayerColorControl->show();
1380 SurfaceComposerClient::closeGlobalTransaction(true);
1381 {
1382 // There should now be a color with .75 alpha
1383 SCOPED_TRACE("after setColor");
1384 ScreenCapture::captureScreen(&sc);
1385 sc->checkPixel(145, 145, 48, 171, 147);
1386 }
1387}
1388
1389TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1390 sp<ScreenCapture> sc;
1391 {
1392 SCOPED_TRACE("before setColor");
1393 ScreenCapture::captureScreen(&sc);
1394 sc->expectBGColor(145, 145);
1395 }
1396
1397 SurfaceComposerClient::openGlobalTransaction();
1398 mLayerColorControl->show();
1399 SurfaceComposerClient::closeGlobalTransaction(true);
1400 {
1401 // There should now be set to 0,0,0 (black) as default.
1402 SCOPED_TRACE("after setColor");
1403 ScreenCapture::captureScreen(&sc);
1404 sc->checkPixel(145, 145, 0, 0, 0);
1405 }
1406}
1407
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001408}