blob: 5095b41d0e0e4276fc79b7e7acc36bdbed544b1f [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
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
18#include <functional>
19#include <limits>
20#include <ostream>
21
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070022#include <gtest/gtest.h>
23
Michael Lentine5a16a622015-05-21 13:48:24 -070024#include <android/native_window.h>
25
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070027#include <gui/LayerState.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
31#include <private/gui/ComposerService.h>
32
Mathias Agopianc666cae2012-07-25 18:56:13 -070033#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070034#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070035#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070036
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070037#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070038#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070039
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040namespace android {
41
Chia-I Wu718daf82017-10-20 11:57:17 -070042namespace {
43
44struct Color {
45 uint8_t r;
46 uint8_t g;
47 uint8_t b;
48 uint8_t a;
49
50 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070051 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070052 static const Color BLUE;
Chia-I Wu718daf82017-10-20 11:57:17 -070053 static const Color BLACK;
54};
55
56const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070057const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070058const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070059const Color Color::BLACK{0, 0, 0, 255};
60
61std::ostream& operator<<(std::ostream& os, const Color& color) {
62 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
63 return os;
64}
65
66// Fill a region with the specified color.
67void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
68 int32_t x = rect.left;
69 int32_t y = rect.top;
70 int32_t width = rect.right - rect.left;
71 int32_t height = rect.bottom - rect.top;
72
73 if (x < 0) {
74 width += x;
75 x = 0;
76 }
77 if (y < 0) {
78 height += y;
79 y = 0;
80 }
81 if (x + width > buffer.width) {
82 x = std::min(x, buffer.width);
83 width = buffer.width - x;
84 }
85 if (y + height > buffer.height) {
86 y = std::min(y, buffer.height);
87 height = buffer.height - y;
88 }
89
90 for (int32_t j = 0; j < height; j++) {
91 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
92 for (int32_t i = 0; i < width; i++) {
93 dst[0] = color.r;
94 dst[1] = color.g;
95 dst[2] = color.b;
96 dst[3] = color.a;
97 dst += 4;
98 }
99 }
100}
101
102// Check if a region has the specified color.
103void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect,
104 const Color& color, uint8_t tolerance) {
105 int32_t x = rect.left;
106 int32_t y = rect.top;
107 int32_t width = rect.right - rect.left;
108 int32_t height = rect.bottom - rect.top;
109
110 if (x + width > int32_t(buffer.width)) {
111 x = std::min(x, int32_t(buffer.width));
112 width = buffer.width - x;
113 }
114 if (y + height > int32_t(buffer.height)) {
115 y = std::min(y, int32_t(buffer.height));
116 height = buffer.height - y;
117 }
118
119 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
120 uint8_t tmp = a >= b ? a - b : b - a;
121 return tmp <= tolerance;
122 };
123 for (int32_t j = 0; j < height; j++) {
124 const uint8_t* src =
125 static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4;
126 for (int32_t i = 0; i < width; i++) {
127 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
128 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
129 << "pixel @ (" << x + i << ", " << y + j << "): "
130 << "expected (" << color << "), "
131 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
132 src += 4;
133 }
134 }
135}
136
137} // anonymous namespace
138
Robert Carr4cdc58f2017-08-23 14:22:20 -0700139using Transaction = SurfaceComposerClient::Transaction;
140
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700141// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700142static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
143 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800144 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700145 sp<Surface> s = sc->getSurface();
146 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800147 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
148 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700149 for (int y = 0; y < outBuffer.height; y++) {
150 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700151 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700152 pixel[0] = r;
153 pixel[1] = g;
154 pixel[2] = b;
155 pixel[3] = 255;
156 }
157 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700158 if (unlock) {
159 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
160 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700161}
162
163// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
164// individual pixel values for testing purposes.
165class ScreenCapture : public RefBase {
166public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700167 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
168 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700169 sp<IGraphicBufferProducer> producer;
170 sp<IGraphicBufferConsumer> consumer;
171 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -0700172 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700173 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700174 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700175 SurfaceComposerClient::Transaction().apply(true);
176
Chia-I Wu718daf82017-10-20 11:57:17 -0700177 ASSERT_EQ(NO_ERROR,
178 sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false));
Michael Lentine5a16a622015-05-21 13:48:24 -0700179 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700180 }
181
Chia-I Wu718daf82017-10-20 11:57:17 -0700182 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
183 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
184 expectBufferColor(mBuf, rect, color, tolerance);
185 }
186
187 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
188 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
189 const bool leftBorder = rect.left > 0;
190 const bool topBorder = rect.top > 0;
191 const bool rightBorder = rect.right < int32_t(mBuf.width);
192 const bool bottomBorder = rect.bottom < int32_t(mBuf.height);
193
194 if (topBorder) {
195 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
196 if (leftBorder) {
197 top.left -= 1;
198 }
199 if (rightBorder) {
200 top.right += 1;
201 }
202 expectColor(top, color, tolerance);
203 }
204 if (leftBorder) {
205 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
206 expectColor(left, color, tolerance);
207 }
208 if (rightBorder) {
209 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
210 expectColor(right, color, tolerance);
211 }
212 if (bottomBorder) {
213 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
214 if (leftBorder) {
215 bottom.left -= 1;
216 }
217 if (rightBorder) {
218 bottom.right += 1;
219 }
220 expectColor(bottom, color, tolerance);
221 }
222 }
223
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700224 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700225 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
226 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
227 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700228 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
229 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700230 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
231 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700232 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700233 }
234 }
235
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700236 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700237
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700238 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700239
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700240 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700241
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700242private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700243 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700244 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
245 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700246
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700247 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700248
249 sp<CpuConsumer> mCC;
250 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700251};
252
chaviwa76b2712017-09-20 12:02:26 -0700253class CaptureLayer {
254public:
255 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
256 sp<IGraphicBufferProducer> producer;
257 sp<IGraphicBufferConsumer> consumer;
258 BufferQueue::createBufferQueue(&producer, &consumer);
259 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
260 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700261 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700262 SurfaceComposerClient::Transaction().apply(true);
263 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
264 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
265 }
266
267 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
268 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
269 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
270 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
271 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
272 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700273 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700274 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
275 EXPECT_EQ(String8(), err) << err.string();
276 }
277 }
278
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700279 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700280
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700281 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700282
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700283 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700284
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700285 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700286 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
287 }
288
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700289 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700290
291private:
292 sp<CpuConsumer> mCC;
293 CpuConsumer::LockedBuffer mBuffer;
294};
295
Chia-I Wu718daf82017-10-20 11:57:17 -0700296class LayerTransactionTest : public ::testing::Test {
297protected:
298 void SetUp() override {
299 mClient = new SurfaceComposerClient;
300 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
301
302 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
303 }
304
305 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
306 uint32_t flags = 0) {
307 auto layer =
308 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
309 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
310
311 status_t error = Transaction()
312 .setLayerStack(layer, mDisplayLayerStack)
313 .setLayer(layer, mLayerZBase)
314 .apply();
315 if (error != NO_ERROR) {
316 ADD_FAILURE() << "failed to initialize SurfaceControl";
317 layer.clear();
318 }
319
320 return layer;
321 }
322
323 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
324 // wait for previous transactions (such as setSize) to complete
325 Transaction().apply(true);
326
327 ANativeWindow_Buffer buffer = {};
328 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
329
330 return buffer;
331 }
332
333 void postLayerBuffer(const sp<SurfaceControl>& layer) {
334 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
335
336 // wait for the newly posted buffer to be latched
337 waitForLayerBuffers();
338 }
339
340 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
341 ANativeWindow_Buffer buffer;
342 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
343 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
344 postLayerBuffer(layer);
345 }
346
347 sp<ScreenCapture> screenshot() {
348 sp<ScreenCapture> screenshot;
349 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
350 return screenshot;
351 }
352
353 sp<SurfaceComposerClient> mClient;
354
355 sp<IBinder> mDisplay;
356 uint32_t mDisplayWidth;
357 uint32_t mDisplayHeight;
358 uint32_t mDisplayLayerStack;
359
360 // leave room for ~256 layers
361 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
362
363private:
364 void SetUpDisplay() {
365 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
366 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
367
368 // get display width/height
369 DisplayInfo info;
370 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
371 mDisplayWidth = info.w;
372 mDisplayHeight = info.h;
373
374 // After a new buffer is queued, SurfaceFlinger is notified and will
375 // latch the new buffer on next vsync. Let's heuristically wait for 3
376 // vsyncs.
377 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
378
379 mDisplayLayerStack = 0;
380 // set layer stack (b/68888219)
381 Transaction t;
382 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
383 t.apply();
384 }
385
386 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
387
388 int32_t mBufferPostDelay;
389};
390
391TEST_F(LayerTransactionTest, SetPositionBasic) {
392 sp<SurfaceControl> layer;
393 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
394 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
395
396 {
397 SCOPED_TRACE("default position");
398 auto shot = screenshot();
399 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
400 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
401 }
402
403 Transaction().setPosition(layer, 5, 10).apply();
404 {
405 SCOPED_TRACE("new position");
406 auto shot = screenshot();
407 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
408 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
409 }
410}
411
412TEST_F(LayerTransactionTest, SetPositionRounding) {
413 sp<SurfaceControl> layer;
414 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
415 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
416
417 // GLES requires only 4 bits of subpixel precision during rasterization
418 // XXX GLES composition does not match HWC composition due to precision
419 // loss (b/69315223)
420 const float epsilon = 1.0f / 16.0f;
421 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
422 {
423 SCOPED_TRACE("rounding down");
424 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
425 }
426
427 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
428 {
429 SCOPED_TRACE("rounding up");
430 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
431 }
432}
433
434TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
435 sp<SurfaceControl> layer;
436 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
437 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
438
439 Transaction().setPosition(layer, -32, -32).apply();
440 {
441 SCOPED_TRACE("negative coordinates");
442 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
443 }
444
445 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
446 {
447 SCOPED_TRACE("positive coordinates");
448 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
449 }
450}
451
452TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
453 sp<SurfaceControl> layer;
454 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
455 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
456
457 // partially out of bounds
458 Transaction().setPosition(layer, -30, -30).apply();
459 {
460 SCOPED_TRACE("negative coordinates");
461 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
462 }
463
464 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
465 {
466 SCOPED_TRACE("positive coordinates");
467 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
468 mDisplayHeight),
469 Color::RED);
470 }
471}
472
473TEST_F(LayerTransactionTest, SetPositionWithResize) {
474 sp<SurfaceControl> layer;
475 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
476 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
477
478 // setPosition is applied immediately by default, with or without resize
479 // pending
480 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
481 {
482 SCOPED_TRACE("resize pending");
483 auto shot = screenshot();
484 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
485 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
486 }
487
488 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
489 {
490 SCOPED_TRACE("resize applied");
491 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
492 }
493}
494
495TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
496 sp<SurfaceControl> layer;
497 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
498 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
499
500 // request setPosition to be applied with the next resize
501 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
502 {
503 SCOPED_TRACE("new position pending");
504 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
505 }
506
507 Transaction().setPosition(layer, 15, 20).apply();
508 {
509 SCOPED_TRACE("pending new position modified");
510 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
511 }
512
513 Transaction().setSize(layer, 64, 64).apply();
514 {
515 SCOPED_TRACE("resize pending");
516 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
517 }
518
519 // finally resize and latch the buffer
520 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
521 {
522 SCOPED_TRACE("new position applied");
523 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
524 }
525}
526
527TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
528 sp<SurfaceControl> layer;
529 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
530 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
531
532 // setPosition is not immediate even with SCALE_TO_WINDOW override
533 Transaction()
534 .setPosition(layer, 5, 10)
535 .setSize(layer, 64, 64)
536 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
537 .setGeometryAppliesWithResize(layer)
538 .apply();
539 {
540 SCOPED_TRACE("new position pending");
541 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
542 }
543
544 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
545 {
546 SCOPED_TRACE("new position applied");
547 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
548 }
549}
550
Chia-I Wu0eaea312017-10-31 10:14:40 -0700551TEST_F(LayerTransactionTest, SetSizeBasic) {
552 sp<SurfaceControl> layer;
553 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
554 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
555
556 Transaction().setSize(layer, 64, 64).apply();
557 {
558 SCOPED_TRACE("resize pending");
559 auto shot = screenshot();
560 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
561 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
562 }
563
564 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
565 {
566 SCOPED_TRACE("resize applied");
567 auto shot = screenshot();
568 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
569 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
570 }
571}
572
573TEST_F(LayerTransactionTest, SetSizeInvalid) {
574 // cannot test robustness against invalid sizes (zero or really huge)
575}
576
577TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
578 sp<SurfaceControl> layer;
579 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
580 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
581
582 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
583 Transaction()
584 .setSize(layer, 64, 64)
585 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
586 .apply();
587 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
588}
589
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700590TEST_F(LayerTransactionTest, SetZBasic) {
591 sp<SurfaceControl> layerR;
592 sp<SurfaceControl> layerG;
593 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
594 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
595 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
596 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
597
598 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
599 {
600 SCOPED_TRACE("layerR");
601 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
602 }
603
604 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
605 {
606 SCOPED_TRACE("layerG");
607 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
608 }
609}
610
611TEST_F(LayerTransactionTest, SetZNegative) {
612 sp<SurfaceControl> layerR;
613 sp<SurfaceControl> layerG;
614 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
615 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
616 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
617 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
618
619 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
620 {
621 SCOPED_TRACE("layerR");
622 sp<ScreenCapture> screenshot;
623 ScreenCapture::captureScreen(&screenshot, -2, -1);
624 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
625 }
626
627 Transaction().setLayer(layerR, -3).apply();
628 {
629 SCOPED_TRACE("layerG");
630 sp<ScreenCapture> screenshot;
631 ScreenCapture::captureScreen(&screenshot, -3, -1);
632 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
633 }
634}
635
Chia-I Wu49313302017-10-31 10:14:40 -0700636TEST_F(LayerTransactionTest, SetRelativeZBasic) {
637 sp<SurfaceControl> layerR;
638 sp<SurfaceControl> layerG;
639 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
640 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
641 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
642 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
643
644 Transaction()
645 .setPosition(layerG, 16, 16)
646 .setRelativeLayer(layerG, layerR->getHandle(), 1)
647 .apply();
648 {
649 SCOPED_TRACE("layerG above");
650 auto shot = screenshot();
651 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
652 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
653 }
654
655 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
656 {
657 SCOPED_TRACE("layerG below");
658 auto shot = screenshot();
659 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
660 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
661 }
662}
663
664TEST_F(LayerTransactionTest, SetRelativeZGroup) {
665 sp<SurfaceControl> layerR;
666 sp<SurfaceControl> layerG;
667 sp<SurfaceControl> layerB;
668 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
669 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
670 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
671 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
672 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
673 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
674
675 // layerR = 0, layerG = layerR + 3, layerB = 2
676 Transaction()
677 .setPosition(layerG, 8, 8)
678 .setRelativeLayer(layerG, layerR->getHandle(), 3)
679 .setPosition(layerB, 16, 16)
680 .setLayer(layerB, mLayerZBase + 2)
681 .apply();
682 {
683 SCOPED_TRACE("(layerR < layerG) < layerB");
684 auto shot = screenshot();
685 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
686 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
687 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
688 }
689
690 // layerR = 4, layerG = layerR + 3, layerB = 2
691 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
692 {
693 SCOPED_TRACE("layerB < (layerR < layerG)");
694 auto shot = screenshot();
695 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
696 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
697 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
698 }
699
700 // layerR = 4, layerG = layerR - 3, layerB = 2
701 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
702 {
703 SCOPED_TRACE("layerB < (layerG < layerR)");
704 auto shot = screenshot();
705 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
706 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
707 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
708 }
709
710 // restore to absolute z
711 // layerR = 4, layerG = 0, layerB = 2
712 Transaction().setLayer(layerG, mLayerZBase).apply();
713 {
714 SCOPED_TRACE("layerG < layerB < layerR");
715 auto shot = screenshot();
716 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
717 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
718 }
719
720 // layerR should not affect layerG anymore
721 // layerR = 1, layerG = 0, layerB = 2
722 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
723 {
724 SCOPED_TRACE("layerG < layerR < layerB");
725 auto shot = screenshot();
726 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
727 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
728 }
729}
730
731TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
732 sp<SurfaceControl> layerR;
733 sp<SurfaceControl> layerG;
734
735 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
736 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
737 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
738 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
739
740 Transaction()
741 .setPosition(layerG, 16, 16)
742 .setRelativeLayer(layerG, layerR->getHandle(), 1)
743 .apply();
744
745 mClient->destroySurface(layerG->getHandle());
746 // layerG should have been removed
747 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
748}
749
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700750class LayerUpdateTest : public ::testing::Test {
751protected:
752 virtual void SetUp() {
753 mComposerClient = new SurfaceComposerClient;
754 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
755
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700756 sp<IBinder> display(
757 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700758 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700759 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700760
761 ssize_t displayWidth = info.w;
762 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700763
764 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700765 mBGSurfaceControl =
766 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
767 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700768 ASSERT_TRUE(mBGSurfaceControl != NULL);
769 ASSERT_TRUE(mBGSurfaceControl->isValid());
770 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
771
772 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700773 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
774 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700775 ASSERT_TRUE(mFGSurfaceControl != NULL);
776 ASSERT_TRUE(mFGSurfaceControl->isValid());
777
778 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
779
780 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700781 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
782 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700783 ASSERT_TRUE(mSyncSurfaceControl != NULL);
784 ASSERT_TRUE(mSyncSurfaceControl->isValid());
785
786 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
787
Robert Carr4cdc58f2017-08-23 14:22:20 -0700788 asTransaction([&](Transaction& t) {
789 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700790
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700791 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700792
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700793 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
794 .setPosition(mFGSurfaceControl, 64, 64)
795 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700796
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700797 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
798 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
799 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700800 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700801 }
802
803 virtual void TearDown() {
804 mComposerClient->dispose();
805 mBGSurfaceControl = 0;
806 mFGSurfaceControl = 0;
807 mSyncSurfaceControl = 0;
808 mComposerClient = 0;
809 }
810
811 void waitForPostedBuffers() {
812 // Since the sync surface is in synchronous mode (i.e. double buffered)
813 // posting three buffers to it should ensure that at least two
814 // SurfaceFlinger::handlePageFlip calls have been made, which should
815 // guaranteed that a buffer posted to another Surface has been retired.
816 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
817 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
818 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
819 }
820
Robert Carr4cdc58f2017-08-23 14:22:20 -0700821 void asTransaction(const std::function<void(Transaction&)>& exec) {
822 Transaction t;
823 exec(t);
824 t.apply(true);
825 }
826
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700827 sp<SurfaceComposerClient> mComposerClient;
828 sp<SurfaceControl> mBGSurfaceControl;
829 sp<SurfaceControl> mFGSurfaceControl;
830
831 // This surface is used to ensure that the buffers posted to
832 // mFGSurfaceControl have been picked up by SurfaceFlinger.
833 sp<SurfaceControl> mSyncSurfaceControl;
834};
835
Robert Carr7f619b22017-11-06 12:56:35 -0800836TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
837 sp<ScreenCapture> sc;
838
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700839 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
840 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -0800841 fillSurfaceRGBA8(relative, 10, 10, 10);
842 waitForPostedBuffers();
843
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700844 Transaction{}
845 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -0800846 .setPosition(relative, 64, 64)
847 .apply();
848
849 {
850 // The relative should be on top of the FG control.
851 ScreenCapture::captureScreen(&sc);
852 sc->checkPixel(64, 64, 10, 10, 10);
853 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700854 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800855
856 {
857 // Nothing should change at this point.
858 ScreenCapture::captureScreen(&sc);
859 sc->checkPixel(64, 64, 10, 10, 10);
860 }
861
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700862 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800863
864 {
865 // Ensure that the relative was actually hidden, rather than
866 // being left in the detached but visible state.
867 ScreenCapture::captureScreen(&sc);
868 sc->expectFGColor(64, 64);
869 }
870}
871
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700872TEST_F(LayerUpdateTest, LayerMoveWorks) {
873 sp<ScreenCapture> sc;
874 {
875 SCOPED_TRACE("before move");
876 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800877 sc->expectBGColor(0, 12);
878 sc->expectFGColor(75, 75);
879 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700880 }
881
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700882 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700883
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700884 {
885 // This should reflect the new position, but not the new color.
886 SCOPED_TRACE("after move, before redraw");
887 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800888 sc->expectBGColor(24, 24);
889 sc->expectBGColor(75, 75);
890 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700891 }
892
893 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
894 waitForPostedBuffers();
895 {
896 // This should reflect the new position and the new color.
897 SCOPED_TRACE("after redraw");
898 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800899 sc->expectBGColor(24, 24);
900 sc->expectBGColor(75, 75);
901 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700902 }
903}
904
905TEST_F(LayerUpdateTest, LayerResizeWorks) {
906 sp<ScreenCapture> sc;
907 {
908 SCOPED_TRACE("before resize");
909 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800910 sc->expectBGColor(0, 12);
911 sc->expectFGColor(75, 75);
912 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700913 }
914
Steve Block9d453682011-12-20 16:23:08 +0000915 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700916 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +0000917 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700918 {
919 // This should not reflect the new size or color because SurfaceFlinger
920 // has not yet received a buffer of the correct size.
921 SCOPED_TRACE("after resize, before redraw");
922 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800923 sc->expectBGColor(0, 12);
924 sc->expectFGColor(75, 75);
925 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700926 }
927
Steve Block9d453682011-12-20 16:23:08 +0000928 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700929 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
930 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000931 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700932 {
933 // This should reflect the new size and the new color.
934 SCOPED_TRACE("after redraw");
935 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800936 sc->expectBGColor(24, 24);
937 sc->checkPixel(75, 75, 63, 195, 63);
938 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700939 }
940}
941
Haixia Shid5750962015-07-27 16:50:49 -0700942TEST_F(LayerUpdateTest, LayerCropWorks) {
943 sp<ScreenCapture> sc;
944 {
945 SCOPED_TRACE("before crop");
946 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800947 sc->expectBGColor(24, 24);
948 sc->expectFGColor(75, 75);
949 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700950 }
951
Robert Carr4cdc58f2017-08-23 14:22:20 -0700952 asTransaction([&](Transaction& t) {
953 Rect cropRect(16, 16, 32, 32);
954 t.setCrop(mFGSurfaceControl, cropRect);
955 });
Haixia Shid5750962015-07-27 16:50:49 -0700956 {
957 // This should crop the foreground surface.
958 SCOPED_TRACE("after crop");
959 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800960 sc->expectBGColor(24, 24);
961 sc->expectBGColor(75, 75);
962 sc->expectFGColor(95, 80);
963 sc->expectFGColor(80, 95);
964 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700965 }
966}
967
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000968TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
969 sp<ScreenCapture> sc;
970 {
971 SCOPED_TRACE("before crop");
972 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800973 sc->expectBGColor(24, 24);
974 sc->expectFGColor(75, 75);
975 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000976 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700977 asTransaction([&](Transaction& t) {
978 Rect cropRect(16, 16, 32, 32);
979 t.setFinalCrop(mFGSurfaceControl, cropRect);
980 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000981 {
982 // This should crop the foreground surface.
983 SCOPED_TRACE("after crop");
984 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800985 sc->expectBGColor(24, 24);
986 sc->expectBGColor(75, 75);
987 sc->expectBGColor(95, 80);
988 sc->expectBGColor(80, 95);
989 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000990 }
991}
992
Haixia Shid5750962015-07-27 16:50:49 -0700993TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
994 sp<ScreenCapture> sc;
995 {
996 SCOPED_TRACE("before setLayer");
997 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800998 sc->expectBGColor(24, 24);
999 sc->expectFGColor(75, 75);
1000 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001001 }
1002
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001003 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001004
Haixia Shid5750962015-07-27 16:50:49 -07001005 {
1006 // This should hide the foreground surface beneath the background.
1007 SCOPED_TRACE("after setLayer");
1008 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001009 sc->expectBGColor(24, 24);
1010 sc->expectBGColor(75, 75);
1011 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001012 }
1013}
1014
1015TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1016 sp<ScreenCapture> sc;
1017 {
1018 SCOPED_TRACE("before hide");
1019 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001020 sc->expectBGColor(24, 24);
1021 sc->expectFGColor(75, 75);
1022 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001023 }
1024
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001025 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001026
Haixia Shid5750962015-07-27 16:50:49 -07001027 {
1028 // This should hide the foreground surface.
1029 SCOPED_TRACE("after hide, before show");
1030 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001031 sc->expectBGColor(24, 24);
1032 sc->expectBGColor(75, 75);
1033 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001034 }
1035
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001036 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001037
Haixia Shid5750962015-07-27 16:50:49 -07001038 {
1039 // This should show the foreground surface.
1040 SCOPED_TRACE("after show");
1041 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001042 sc->expectBGColor(24, 24);
1043 sc->expectFGColor(75, 75);
1044 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001045 }
1046}
1047
1048TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1049 sp<ScreenCapture> sc;
1050 {
1051 SCOPED_TRACE("before setAlpha");
1052 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001053 sc->expectBGColor(24, 24);
1054 sc->expectFGColor(75, 75);
1055 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001056 }
1057
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001058 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001059
Haixia Shid5750962015-07-27 16:50:49 -07001060 {
1061 // This should set foreground to be 75% opaque.
1062 SCOPED_TRACE("after setAlpha");
1063 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001064 sc->expectBGColor(24, 24);
1065 sc->checkPixel(75, 75, 162, 63, 96);
1066 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001067 }
1068}
1069
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001070TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1071 sp<ScreenCapture> sc;
1072 {
1073 SCOPED_TRACE("before setLayerStack");
1074 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001075 sc->expectBGColor(24, 24);
1076 sc->expectFGColor(75, 75);
1077 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001078 }
1079
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001080 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001081 {
1082 // This should hide the foreground surface since it goes to a different
1083 // layer stack.
1084 SCOPED_TRACE("after setLayerStack");
1085 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001086 sc->expectBGColor(24, 24);
1087 sc->expectBGColor(75, 75);
1088 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001089 }
1090}
1091
1092TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1093 sp<ScreenCapture> sc;
1094 {
1095 SCOPED_TRACE("before setFlags");
1096 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001097 sc->expectBGColor(24, 24);
1098 sc->expectFGColor(75, 75);
1099 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001100 }
1101
Robert Carr4cdc58f2017-08-23 14:22:20 -07001102 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001103 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001104 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001105 {
1106 // This should hide the foreground surface
1107 SCOPED_TRACE("after setFlags");
1108 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001109 sc->expectBGColor(24, 24);
1110 sc->expectBGColor(75, 75);
1111 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001112 }
1113}
1114
1115TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1116 sp<ScreenCapture> sc;
1117 {
1118 SCOPED_TRACE("before setMatrix");
1119 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001120 sc->expectBGColor(24, 24);
1121 sc->expectFGColor(91, 96);
1122 sc->expectFGColor(96, 101);
1123 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001124 }
1125
Robert Carr4cdc58f2017-08-23 14:22:20 -07001126 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001127 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001128 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001129 {
1130 SCOPED_TRACE("after setMatrix");
1131 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001132 sc->expectBGColor(24, 24);
1133 sc->expectFGColor(91, 96);
1134 sc->expectBGColor(96, 91);
1135 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001136 }
1137}
1138
Robert Carr8d5227b2017-03-16 15:41:03 -07001139class GeometryLatchingTest : public LayerUpdateTest {
1140protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001141 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001142 SCOPED_TRACE(trace);
1143 ScreenCapture::captureScreen(&sc);
1144 // We find the leading edge of the FG surface.
1145 sc->expectFGColor(127, 127);
1146 sc->expectBGColor(128, 128);
1147 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001148
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001149 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001150
1151 void unlockFGBuffer() {
1152 sp<Surface> s = mFGSurfaceControl->getSurface();
1153 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1154 waitForPostedBuffers();
1155 }
1156
Robert Carr8d5227b2017-03-16 15:41:03 -07001157 void completeFGResize() {
1158 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1159 waitForPostedBuffers();
1160 }
1161 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001162 asTransaction([&](Transaction& t) {
1163 t.setSize(mFGSurfaceControl, 64, 64);
1164 t.setPosition(mFGSurfaceControl, 64, 64);
1165 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1166 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1167 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001168
1169 EXPECT_INITIAL_STATE("After restoring initial state");
1170 }
1171 sp<ScreenCapture> sc;
1172};
1173
1174TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1175 EXPECT_INITIAL_STATE("before anything");
1176
1177 // By default position can be updated even while
1178 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001179 asTransaction([&](Transaction& t) {
1180 t.setSize(mFGSurfaceControl, 32, 32);
1181 t.setPosition(mFGSurfaceControl, 100, 100);
1182 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001183
1184 {
1185 SCOPED_TRACE("After moving surface");
1186 ScreenCapture::captureScreen(&sc);
1187 // If we moved, the FG Surface should cover up what was previously BG
1188 // however if we didn't move the FG wouldn't be large enough now.
1189 sc->expectFGColor(163, 163);
1190 }
1191
1192 restoreInitialState();
1193
1194 // Now we repeat with setGeometryAppliesWithResize
1195 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001196 asTransaction([&](Transaction& t) {
1197 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1198 t.setSize(mFGSurfaceControl, 32, 32);
1199 t.setPosition(mFGSurfaceControl, 100, 100);
1200 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001201
1202 {
1203 SCOPED_TRACE("While resize is pending");
1204 ScreenCapture::captureScreen(&sc);
1205 // This time we shouldn't have moved, so the BG color
1206 // should still be visible.
1207 sc->expectBGColor(128, 128);
1208 }
1209
1210 completeFGResize();
1211
1212 {
1213 SCOPED_TRACE("After the resize");
1214 ScreenCapture::captureScreen(&sc);
1215 // But after the resize completes, we should move
1216 // and the FG should be visible here.
1217 sc->expectFGColor(128, 128);
1218 }
1219}
1220
1221class CropLatchingTest : public GeometryLatchingTest {
1222protected:
1223 void EXPECT_CROPPED_STATE(const char* trace) {
1224 SCOPED_TRACE(trace);
1225 ScreenCapture::captureScreen(&sc);
1226 // The edge should be moved back one pixel by our crop.
1227 sc->expectFGColor(126, 126);
1228 sc->expectBGColor(127, 127);
1229 sc->expectBGColor(128, 128);
1230 }
chaviw59f5c562017-06-28 16:39:06 -07001231
1232 void EXPECT_RESIZE_STATE(const char* trace) {
1233 SCOPED_TRACE(trace);
1234 ScreenCapture::captureScreen(&sc);
1235 // The FG is now resized too 128,128 at 64,64
1236 sc->expectFGColor(64, 64);
1237 sc->expectFGColor(191, 191);
1238 sc->expectBGColor(192, 192);
1239 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001240};
1241
1242TEST_F(CropLatchingTest, CropLatching) {
1243 EXPECT_INITIAL_STATE("before anything");
1244 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001245 asTransaction([&](Transaction& t) {
1246 t.setSize(mFGSurfaceControl, 128, 128);
1247 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1248 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001249
1250 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1251
1252 restoreInitialState();
1253
Robert Carr4cdc58f2017-08-23 14:22:20 -07001254 asTransaction([&](Transaction& t) {
1255 t.setSize(mFGSurfaceControl, 128, 128);
1256 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1257 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1258 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001259
1260 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1261
1262 completeFGResize();
1263
1264 EXPECT_CROPPED_STATE("after the resize finishes");
1265}
1266
1267TEST_F(CropLatchingTest, FinalCropLatching) {
1268 EXPECT_INITIAL_STATE("before anything");
1269 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001270 asTransaction([&](Transaction& t) {
1271 t.setSize(mFGSurfaceControl, 128, 128);
1272 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1273 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001274
1275 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1276
1277 restoreInitialState();
1278
Robert Carr4cdc58f2017-08-23 14:22:20 -07001279 asTransaction([&](Transaction& t) {
1280 t.setSize(mFGSurfaceControl, 128, 128);
1281 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1282 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1283 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001284
1285 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1286
1287 completeFGResize();
1288
1289 EXPECT_CROPPED_STATE("after the resize finishes");
1290}
1291
Robert Carr7bf247e2017-05-18 14:02:49 -07001292// In this test we ensure that setGeometryAppliesWithResize actually demands
1293// a buffer of the new size, and not just any size.
1294TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1295 EXPECT_INITIAL_STATE("before anything");
1296 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001297 asTransaction([&](Transaction& t) {
1298 t.setSize(mFGSurfaceControl, 128, 128);
1299 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1300 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001301
1302 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1303
1304 restoreInitialState();
1305
1306 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1307 // initiating the resize.
1308 lockAndFillFGBuffer();
1309
Robert Carr4cdc58f2017-08-23 14:22:20 -07001310 asTransaction([&](Transaction& t) {
1311 t.setSize(mFGSurfaceControl, 128, 128);
1312 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1313 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1314 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001315
1316 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1317
1318 // We now submit our old buffer, at the old size, and ensure it doesn't
1319 // trigger geometry latching.
1320 unlockFGBuffer();
1321
1322 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1323
1324 completeFGResize();
1325
1326 EXPECT_CROPPED_STATE("after the resize finishes");
1327}
1328
1329TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1330 EXPECT_INITIAL_STATE("before anything");
1331 // In this scenario, we attempt to set the final crop a second time while the resize
1332 // is still pending, and ensure we are successful. Success meaning the second crop
1333 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001334 asTransaction([&](Transaction& t) {
1335 t.setSize(mFGSurfaceControl, 128, 128);
1336 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1337 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1338 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001339
chaviw59f5c562017-06-28 16:39:06 -07001340 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1341
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001342 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001343
chaviw59f5c562017-06-28 16:39:06 -07001344 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001345
1346 completeFGResize();
1347
chaviw59f5c562017-06-28 16:39:06 -07001348 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001349}
1350
Pablo Ceballos05289c22016-04-14 15:49:55 -07001351TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1352 sp<ScreenCapture> sc;
1353 {
1354 SCOPED_TRACE("before anything");
1355 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001356 sc->expectBGColor(32, 32);
1357 sc->expectFGColor(96, 96);
1358 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001359 }
1360
1361 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001362 asTransaction([&](Transaction& t) {
1363 t.setAlpha(mFGSurfaceControl, 0.75);
1364 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001365 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001366 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001367
Robert Carr4cdc58f2017-08-23 14:22:20 -07001368 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001369 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001370 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001371 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001372 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001373
1374 {
1375 SCOPED_TRACE("before any trigger");
1376 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001377 sc->expectBGColor(32, 32);
1378 sc->expectFGColor(96, 96);
1379 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001380 }
1381
1382 // should trigger the first deferred transaction, but not the second one
1383 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1384 {
1385 SCOPED_TRACE("after first trigger");
1386 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001387 sc->expectBGColor(32, 32);
1388 sc->checkPixel(96, 96, 162, 63, 96);
1389 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001390 }
1391
1392 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001393 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001394
1395 // trigger the second deferred transaction
1396 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1397 {
1398 SCOPED_TRACE("after second trigger");
1399 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001400 sc->expectBGColor(32, 32);
1401 sc->expectBGColor(96, 96);
1402 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001403 }
1404}
1405
Robert Carrdb66e622017-04-10 16:55:57 -07001406TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1407 sp<ScreenCapture> sc;
1408 {
1409 SCOPED_TRACE("before adding relative surface");
1410 ScreenCapture::captureScreen(&sc);
1411 sc->expectBGColor(24, 24);
1412 sc->expectFGColor(75, 75);
1413 sc->expectBGColor(145, 145);
1414 }
1415
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001416 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1417 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001418 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1419 waitForPostedBuffers();
1420
1421 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001422 asTransaction([&](Transaction& t) {
1423 t.setPosition(relativeSurfaceControl, 64, 64);
1424 t.show(relativeSurfaceControl);
1425 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1426 });
Robert Carrdb66e622017-04-10 16:55:57 -07001427
1428 {
1429 SCOPED_TRACE("after adding relative surface");
1430 ScreenCapture::captureScreen(&sc);
1431 // our relative surface should be visible now.
1432 sc->checkPixel(75, 75, 255, 177, 177);
1433 }
1434
1435 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001436 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001437
1438 {
1439 SCOPED_TRACE("after set layer");
1440 ScreenCapture::captureScreen(&sc);
1441 // now the FG surface should be visible again.
1442 sc->expectFGColor(75, 75);
1443 }
1444}
1445
Robert Carre392b552017-09-19 12:16:05 -07001446TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1447 sp<ScreenCapture> sc;
1448
1449 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001450 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1451 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1452 sp<SurfaceControl> childBuffer =
1453 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1454 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001455 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1456
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001457 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001458
1459 {
1460 ScreenCapture::captureScreen(&sc);
1461 sc->expectChildColor(73, 73);
1462 sc->expectFGColor(74, 74);
1463 }
1464
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001465 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001466
1467 {
1468 ScreenCapture::captureScreen(&sc);
1469 sc->expectChildColor(73, 73);
1470 sc->expectChildColor(74, 74);
1471 }
1472}
1473
Robert Carr2c5f6d22017-09-26 12:30:35 -07001474TEST_F(LayerUpdateTest, MergingTransactions) {
1475 sp<ScreenCapture> sc;
1476 {
1477 SCOPED_TRACE("before move");
1478 ScreenCapture::captureScreen(&sc);
1479 sc->expectBGColor(0, 12);
1480 sc->expectFGColor(75, 75);
1481 sc->expectBGColor(145, 145);
1482 }
1483
1484 Transaction t1, t2;
1485 t1.setPosition(mFGSurfaceControl, 128, 128);
1486 t2.setPosition(mFGSurfaceControl, 0, 0);
1487 // We expect that the position update from t2 now
1488 // overwrites the position update from t1.
1489 t1.merge(std::move(t2));
1490 t1.apply();
1491
1492 {
1493 ScreenCapture::captureScreen(&sc);
1494 sc->expectFGColor(1, 1);
1495 }
1496}
1497
Robert Carr1f0a16a2016-10-24 16:27:39 -07001498class ChildLayerTest : public LayerUpdateTest {
1499protected:
1500 void SetUp() override {
1501 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001502 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1503 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001504 fillSurfaceRGBA8(mChild, 200, 200, 200);
1505
1506 {
1507 SCOPED_TRACE("before anything");
1508 ScreenCapture::captureScreen(&mCapture);
1509 mCapture->expectChildColor(64, 64);
1510 }
1511 }
1512 void TearDown() override {
1513 LayerUpdateTest::TearDown();
1514 mChild = 0;
1515 }
1516
1517 sp<SurfaceControl> mChild;
1518 sp<ScreenCapture> mCapture;
1519};
1520
1521TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001522 asTransaction([&](Transaction& t) {
1523 t.show(mChild);
1524 t.setPosition(mChild, 10, 10);
1525 t.setPosition(mFGSurfaceControl, 64, 64);
1526 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001527
1528 {
1529 ScreenCapture::captureScreen(&mCapture);
1530 // Top left of foreground must now be visible
1531 mCapture->expectFGColor(64, 64);
1532 // But 10 pixels in we should see the child surface
1533 mCapture->expectChildColor(74, 74);
1534 // And 10 more pixels we should be back to the foreground surface
1535 mCapture->expectFGColor(84, 84);
1536 }
1537
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001538 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001539
1540 {
1541 ScreenCapture::captureScreen(&mCapture);
1542 // Top left of foreground should now be at 0, 0
1543 mCapture->expectFGColor(0, 0);
1544 // But 10 pixels in we should see the child surface
1545 mCapture->expectChildColor(10, 10);
1546 // And 10 more pixels we should be back to the foreground surface
1547 mCapture->expectFGColor(20, 20);
1548 }
1549}
1550
Robert Carr41b08b52017-06-01 16:11:34 -07001551TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001552 asTransaction([&](Transaction& t) {
1553 t.show(mChild);
1554 t.setPosition(mChild, 0, 0);
1555 t.setPosition(mFGSurfaceControl, 0, 0);
1556 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1557 });
Robert Carr41b08b52017-06-01 16:11:34 -07001558
1559 {
1560 ScreenCapture::captureScreen(&mCapture);
1561 mCapture->expectChildColor(0, 0);
1562 mCapture->expectChildColor(4, 4);
1563 mCapture->expectBGColor(5, 5);
1564 }
1565}
1566
1567TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001568 asTransaction([&](Transaction& t) {
1569 t.show(mChild);
1570 t.setPosition(mChild, 0, 0);
1571 t.setPosition(mFGSurfaceControl, 0, 0);
1572 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1573 });
Robert Carr41b08b52017-06-01 16:11:34 -07001574
1575 {
1576 ScreenCapture::captureScreen(&mCapture);
1577 mCapture->expectChildColor(0, 0);
1578 mCapture->expectChildColor(4, 4);
1579 mCapture->expectBGColor(5, 5);
1580 }
1581}
1582
Robert Carr1f0a16a2016-10-24 16:27:39 -07001583TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001584 asTransaction([&](Transaction& t) {
1585 t.show(mChild);
1586 t.setPosition(mFGSurfaceControl, 0, 0);
1587 t.setPosition(mChild, 63, 63);
1588 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001589
1590 {
1591 ScreenCapture::captureScreen(&mCapture);
1592 mCapture->expectFGColor(0, 0);
1593 // Last pixel in foreground should now be the child.
1594 mCapture->expectChildColor(63, 63);
1595 // But the child should be constrained and the next pixel
1596 // must be the background
1597 mCapture->expectBGColor(64, 64);
1598 }
1599}
1600
1601TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001602 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001603
1604 // Find the boundary between the parent and child
1605 {
1606 ScreenCapture::captureScreen(&mCapture);
1607 mCapture->expectChildColor(9, 9);
1608 mCapture->expectFGColor(10, 10);
1609 }
1610
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001611 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001612
1613 // The boundary should be twice as far from the origin now.
1614 // The pixels from the last test should all be child now
1615 {
1616 ScreenCapture::captureScreen(&mCapture);
1617 mCapture->expectChildColor(9, 9);
1618 mCapture->expectChildColor(10, 10);
1619 mCapture->expectChildColor(19, 19);
1620 mCapture->expectFGColor(20, 20);
1621 }
1622}
Robert Carr9524cb32017-02-13 11:32:32 -08001623
Robert Carr6452f122017-03-21 10:41:29 -07001624TEST_F(ChildLayerTest, ChildLayerAlpha) {
1625 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1626 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1627 fillSurfaceRGBA8(mChild, 0, 254, 0);
1628 waitForPostedBuffers();
1629
Robert Carr4cdc58f2017-08-23 14:22:20 -07001630 asTransaction([&](Transaction& t) {
1631 t.show(mChild);
1632 t.setPosition(mChild, 0, 0);
1633 t.setPosition(mFGSurfaceControl, 0, 0);
1634 });
Robert Carr6452f122017-03-21 10:41:29 -07001635
1636 {
1637 ScreenCapture::captureScreen(&mCapture);
1638 // Unblended child color
1639 mCapture->checkPixel(0, 0, 0, 254, 0);
1640 }
1641
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001642 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001643
1644 {
1645 ScreenCapture::captureScreen(&mCapture);
1646 // Child and BG blended.
1647 mCapture->checkPixel(0, 0, 127, 127, 0);
1648 }
1649
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001650 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001651
1652 {
1653 ScreenCapture::captureScreen(&mCapture);
1654 // Child and BG blended.
1655 mCapture->checkPixel(0, 0, 95, 64, 95);
1656 }
1657}
1658
Robert Carr9524cb32017-02-13 11:32:32 -08001659TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001660 asTransaction([&](Transaction& t) {
1661 t.show(mChild);
1662 t.setPosition(mChild, 10, 10);
1663 t.setPosition(mFGSurfaceControl, 64, 64);
1664 });
Robert Carr9524cb32017-02-13 11:32:32 -08001665
1666 {
1667 ScreenCapture::captureScreen(&mCapture);
1668 // Top left of foreground must now be visible
1669 mCapture->expectFGColor(64, 64);
1670 // But 10 pixels in we should see the child surface
1671 mCapture->expectChildColor(74, 74);
1672 // And 10 more pixels we should be back to the foreground surface
1673 mCapture->expectFGColor(84, 84);
1674 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001675
1676 asTransaction([&](Transaction& t) {
1677 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1678 });
1679
Robert Carr9524cb32017-02-13 11:32:32 -08001680 {
1681 ScreenCapture::captureScreen(&mCapture);
1682 mCapture->expectFGColor(64, 64);
1683 // In reparenting we should have exposed the entire foreground surface.
1684 mCapture->expectFGColor(74, 74);
1685 // And the child layer should now begin at 10, 10 (since the BG
1686 // layer is at (0, 0)).
1687 mCapture->expectBGColor(9, 9);
1688 mCapture->expectChildColor(10, 10);
1689 }
1690}
1691
chaviw161410b02017-07-27 10:46:08 -07001692TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001693 asTransaction([&](Transaction& t) {
1694 t.show(mChild);
1695 t.setPosition(mChild, 10, 10);
1696 t.setPosition(mFGSurfaceControl, 64, 64);
1697 });
Robert Carr9524cb32017-02-13 11:32:32 -08001698
1699 {
1700 ScreenCapture::captureScreen(&mCapture);
1701 // Top left of foreground must now be visible
1702 mCapture->expectFGColor(64, 64);
1703 // But 10 pixels in we should see the child surface
1704 mCapture->expectChildColor(74, 74);
1705 // And 10 more pixels we should be back to the foreground surface
1706 mCapture->expectFGColor(84, 84);
1707 }
1708
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001709 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001710
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001711 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001712
chaviw161410b02017-07-27 10:46:08 -07001713 // Since the child has the same client as the parent, it will not get
1714 // detached and will be hidden.
1715 {
1716 ScreenCapture::captureScreen(&mCapture);
1717 mCapture->expectFGColor(64, 64);
1718 mCapture->expectFGColor(74, 74);
1719 mCapture->expectFGColor(84, 84);
1720 }
1721}
1722
1723TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1724 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001725 sp<SurfaceControl> mChildNewClient =
1726 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1727 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001728
1729 ASSERT_TRUE(mChildNewClient != NULL);
1730 ASSERT_TRUE(mChildNewClient->isValid());
1731
1732 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1733
Robert Carr4cdc58f2017-08-23 14:22:20 -07001734 asTransaction([&](Transaction& t) {
1735 t.hide(mChild);
1736 t.show(mChildNewClient);
1737 t.setPosition(mChildNewClient, 10, 10);
1738 t.setPosition(mFGSurfaceControl, 64, 64);
1739 });
chaviw161410b02017-07-27 10:46:08 -07001740
1741 {
1742 ScreenCapture::captureScreen(&mCapture);
1743 // Top left of foreground must now be visible
1744 mCapture->expectFGColor(64, 64);
1745 // But 10 pixels in we should see the child surface
1746 mCapture->expectChildColor(74, 74);
1747 // And 10 more pixels we should be back to the foreground surface
1748 mCapture->expectFGColor(84, 84);
1749 }
1750
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001751 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001752
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001753 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001754
Robert Carr9524cb32017-02-13 11:32:32 -08001755 // Nothing should have changed.
1756 {
1757 ScreenCapture::captureScreen(&mCapture);
1758 mCapture->expectFGColor(64, 64);
1759 mCapture->expectChildColor(74, 74);
1760 mCapture->expectFGColor(84, 84);
1761 }
1762}
1763
Robert Carr9b429f42017-04-17 14:56:57 -07001764TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001765 asTransaction([&](Transaction& t) {
1766 t.show(mChild);
1767 t.setPosition(mChild, 0, 0);
1768 t.setPosition(mFGSurfaceControl, 0, 0);
1769 });
Robert Carr9b429f42017-04-17 14:56:57 -07001770
1771 {
1772 ScreenCapture::captureScreen(&mCapture);
1773 // We've positioned the child in the top left.
1774 mCapture->expectChildColor(0, 0);
1775 // But it's only 10x10.
1776 mCapture->expectFGColor(10, 10);
1777 }
1778
Robert Carr4cdc58f2017-08-23 14:22:20 -07001779 asTransaction([&](Transaction& t) {
1780 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1781 // We cause scaling by 2.
1782 t.setSize(mFGSurfaceControl, 128, 128);
1783 });
Robert Carr9b429f42017-04-17 14:56:57 -07001784
1785 {
1786 ScreenCapture::captureScreen(&mCapture);
1787 // We've positioned the child in the top left.
1788 mCapture->expectChildColor(0, 0);
1789 mCapture->expectChildColor(10, 10);
1790 mCapture->expectChildColor(19, 19);
1791 // And now it should be scaled all the way to 20x20
1792 mCapture->expectFGColor(20, 20);
1793 }
1794}
1795
Robert Carr1725eee2017-04-26 18:32:15 -07001796// Regression test for b/37673612
1797TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001798 asTransaction([&](Transaction& t) {
1799 t.show(mChild);
1800 t.setPosition(mChild, 0, 0);
1801 t.setPosition(mFGSurfaceControl, 0, 0);
1802 });
Robert Carr1725eee2017-04-26 18:32:15 -07001803
1804 {
1805 ScreenCapture::captureScreen(&mCapture);
1806 // We've positioned the child in the top left.
1807 mCapture->expectChildColor(0, 0);
1808 // But it's only 10x10.
1809 mCapture->expectFGColor(10, 10);
1810 }
Robert Carr1725eee2017-04-26 18:32:15 -07001811 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1812 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001813 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001814 sp<Surface> s = mFGSurfaceControl->getSurface();
1815 auto anw = static_cast<ANativeWindow*>(s.get());
1816 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1817 native_window_set_buffers_dimensions(anw, 64, 128);
1818 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1819 waitForPostedBuffers();
1820
1821 {
1822 // The child should still be in the same place and not have any strange scaling as in
1823 // b/37673612.
1824 ScreenCapture::captureScreen(&mCapture);
1825 mCapture->expectChildColor(0, 0);
1826 mCapture->expectFGColor(10, 10);
1827 }
1828}
1829
Dan Stoza412903f2017-04-27 13:42:17 -07001830TEST_F(ChildLayerTest, Bug36858924) {
1831 // Destroy the child layer
1832 mChild.clear();
1833
1834 // Now recreate it as hidden
1835 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1836 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1837 mFGSurfaceControl.get());
1838
1839 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001840 asTransaction([&](Transaction& t) {
1841 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001842 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001843 t.show(mChild);
1844 });
Dan Stoza412903f2017-04-27 13:42:17 -07001845
1846 // Render the foreground surface a few times
1847 //
1848 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1849 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1850 // never acquire/release the first buffer
1851 ALOGI("Filling 1");
1852 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1853 ALOGI("Filling 2");
1854 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1855 ALOGI("Filling 3");
1856 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1857 ALOGI("Filling 4");
1858 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1859}
1860
chaviwf1961f72017-09-18 16:41:07 -07001861TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001862 asTransaction([&](Transaction& t) {
1863 t.show(mChild);
1864 t.setPosition(mChild, 10, 10);
1865 t.setPosition(mFGSurfaceControl, 64, 64);
1866 });
chaviw06178942017-07-27 10:25:59 -07001867
1868 {
1869 ScreenCapture::captureScreen(&mCapture);
1870 // Top left of foreground must now be visible
1871 mCapture->expectFGColor(64, 64);
1872 // But 10 pixels in we should see the child surface
1873 mCapture->expectChildColor(74, 74);
1874 // And 10 more pixels we should be back to the foreground surface
1875 mCapture->expectFGColor(84, 84);
1876 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001877
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001878 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001879
chaviw06178942017-07-27 10:25:59 -07001880 {
1881 ScreenCapture::captureScreen(&mCapture);
1882 mCapture->expectFGColor(64, 64);
1883 // In reparenting we should have exposed the entire foreground surface.
1884 mCapture->expectFGColor(74, 74);
1885 // And the child layer should now begin at 10, 10 (since the BG
1886 // layer is at (0, 0)).
1887 mCapture->expectBGColor(9, 9);
1888 mCapture->expectChildColor(10, 10);
1889 }
1890}
1891
chaviwf1961f72017-09-18 16:41:07 -07001892TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001893 asTransaction([&](Transaction& t) {
1894 t.show(mChild);
1895 t.setPosition(mChild, 10, 10);
1896 t.setPosition(mFGSurfaceControl, 64, 64);
1897 });
chaviwf1961f72017-09-18 16:41:07 -07001898
1899 {
1900 ScreenCapture::captureScreen(&mCapture);
1901 // Top left of foreground must now be visible
1902 mCapture->expectFGColor(64, 64);
1903 // But 10 pixels in we should see the child surface
1904 mCapture->expectChildColor(74, 74);
1905 // And 10 more pixels we should be back to the foreground surface
1906 mCapture->expectFGColor(84, 84);
1907 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001908 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07001909 {
1910 ScreenCapture::captureScreen(&mCapture);
1911 // Nothing should have changed.
1912 mCapture->expectFGColor(64, 64);
1913 mCapture->expectChildColor(74, 74);
1914 mCapture->expectFGColor(84, 84);
1915 }
1916}
1917
1918TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001919 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
1920 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07001921 ASSERT_TRUE(newSurface != NULL);
1922 ASSERT_TRUE(newSurface->isValid());
1923
1924 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001925 asTransaction([&](Transaction& t) {
1926 t.hide(mChild);
1927 t.show(newSurface);
1928 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001929 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001930 t.setPosition(mFGSurfaceControl, 64, 64);
1931 });
chaviwf1961f72017-09-18 16:41:07 -07001932
1933 {
1934 ScreenCapture::captureScreen(&mCapture);
1935 // Top left of foreground must now be visible
1936 mCapture->expectFGColor(64, 64);
1937 // At 10, 10 we should see the new surface
1938 mCapture->checkPixel(10, 10, 63, 195, 63);
1939 }
1940
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001941 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07001942
1943 {
1944 ScreenCapture::captureScreen(&mCapture);
1945 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1946 // mFGSurface, putting it at 74, 74.
1947 mCapture->expectFGColor(64, 64);
1948 mCapture->checkPixel(74, 74, 63, 195, 63);
1949 mCapture->expectFGColor(84, 84);
1950 }
1951}
1952
chaviwc9674332017-08-28 12:32:18 -07001953TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001954 sp<SurfaceControl> grandchild =
1955 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
1956 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07001957 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1958
1959 {
1960 ScreenCapture::captureScreen(&mCapture);
1961 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1962 // which begins at 64, 64
1963 mCapture->checkPixel(64, 64, 50, 50, 50);
1964 }
1965}
1966
Robert Carr503c7042017-09-27 15:06:08 -07001967TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001968 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
1969 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07001970 fillSurfaceRGBA8(relative, 255, 255, 255);
1971
1972 Transaction t;
1973 t.setLayer(relative, INT32_MAX)
1974 .setRelativeLayer(mChild, relative->getHandle(), 1)
1975 .setPosition(mFGSurfaceControl, 0, 0)
1976 .apply(true);
1977
1978 // We expect that the child should have been elevated above our
1979 // INT_MAX layer even though it's not a child of it.
1980 {
1981 ScreenCapture::captureScreen(&mCapture);
1982 mCapture->expectChildColor(0, 0);
1983 mCapture->expectChildColor(9, 9);
1984 mCapture->checkPixel(10, 10, 255, 255, 255);
1985 }
1986}
1987
chaviw13fdc492017-06-27 12:40:18 -07001988class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001989protected:
chaviw13fdc492017-06-27 12:40:18 -07001990 void SetUp() override {
1991 LayerUpdateTest::SetUp();
1992
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001993 mLayerColorControl =
1994 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
1995 PIXEL_FORMAT_RGBA_8888,
1996 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07001997
1998 ASSERT_TRUE(mLayerColorControl != NULL);
1999 ASSERT_TRUE(mLayerColorControl->isValid());
2000
Robert Carr4cdc58f2017-08-23 14:22:20 -07002001 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002002 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002003 t.setPosition(mLayerColorControl, 140, 140);
2004 t.hide(mLayerColorControl);
2005 t.hide(mFGSurfaceControl);
2006 });
chaviw13fdc492017-06-27 12:40:18 -07002007 }
2008
2009 void TearDown() override {
2010 LayerUpdateTest::TearDown();
2011 mLayerColorControl = 0;
2012 }
2013
2014 sp<SurfaceControl> mLayerColorControl;
2015};
2016
2017TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2018 sp<ScreenCapture> sc;
2019
2020 {
2021 SCOPED_TRACE("before setColor");
2022 ScreenCapture::captureScreen(&sc);
2023 sc->expectBGColor(145, 145);
2024 }
2025
Robert Carr4cdc58f2017-08-23 14:22:20 -07002026 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002027 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002028 t.setColor(mLayerColorControl, color);
2029 t.show(mLayerColorControl);
2030 });
chaviw13fdc492017-06-27 12:40:18 -07002031
chaviw13fdc492017-06-27 12:40:18 -07002032 {
2033 // There should now be a color
2034 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002035
chaviw13fdc492017-06-27 12:40:18 -07002036 ScreenCapture::captureScreen(&sc);
2037 sc->checkPixel(145, 145, 43, 207, 131);
2038 }
2039}
2040
2041TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2042 sp<ScreenCapture> sc;
2043 {
2044 SCOPED_TRACE("before setColor");
2045 ScreenCapture::captureScreen(&sc);
2046 sc->expectBGColor(145, 145);
2047 }
2048
Robert Carr4cdc58f2017-08-23 14:22:20 -07002049 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002050 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002051 t.setColor(mLayerColorControl, color);
2052 t.setAlpha(mLayerColorControl, .75f);
2053 t.show(mLayerColorControl);
2054 });
2055
chaviw13fdc492017-06-27 12:40:18 -07002056 {
2057 // There should now be a color with .75 alpha
2058 SCOPED_TRACE("after setColor");
2059 ScreenCapture::captureScreen(&sc);
2060 sc->checkPixel(145, 145, 48, 171, 147);
2061 }
2062}
2063
2064TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2065 sp<ScreenCapture> sc;
2066 {
2067 SCOPED_TRACE("before setColor");
2068 ScreenCapture::captureScreen(&sc);
2069 sc->expectBGColor(145, 145);
2070 }
2071
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002072 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002073
chaviw13fdc492017-06-27 12:40:18 -07002074 {
2075 // There should now be set to 0,0,0 (black) as default.
2076 SCOPED_TRACE("after setColor");
2077 ScreenCapture::captureScreen(&sc);
2078 sc->checkPixel(145, 145, 0, 0, 0);
2079 }
2080}
2081
chaviwa76b2712017-09-20 12:02:26 -07002082class ScreenCaptureTest : public LayerUpdateTest {
2083protected:
2084 std::unique_ptr<CaptureLayer> mCapture;
2085};
2086
2087TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2088 auto bgHandle = mBGSurfaceControl->getHandle();
2089 CaptureLayer::captureScreen(&mCapture, bgHandle);
2090 mCapture->expectBGColor(0, 0);
2091 // Doesn't capture FG layer which is at 64, 64
2092 mCapture->expectBGColor(64, 64);
2093}
2094
2095TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2096 auto fgHandle = mFGSurfaceControl->getHandle();
2097
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002098 sp<SurfaceControl> child =
2099 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2100 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002101 fillSurfaceRGBA8(child, 200, 200, 200);
2102
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002103 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002104
2105 // Captures mFGSurfaceControl layer and its child.
2106 CaptureLayer::captureScreen(&mCapture, fgHandle);
2107 mCapture->expectFGColor(10, 10);
2108 mCapture->expectChildColor(0, 0);
2109}
2110
2111TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2112 auto fgHandle = mFGSurfaceControl->getHandle();
2113
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002114 sp<SurfaceControl> child =
2115 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2116 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002117 fillSurfaceRGBA8(child, 200, 200, 200);
2118
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002119 sp<SurfaceControl> grandchild =
2120 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2121 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002122
2123 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2124 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002125 .show(child)
2126 .setPosition(grandchild, 5, 5)
2127 .show(grandchild)
2128 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002129
2130 // Captures mFGSurfaceControl, its child, and the grandchild.
2131 CaptureLayer::captureScreen(&mCapture, fgHandle);
2132 mCapture->expectFGColor(10, 10);
2133 mCapture->expectChildColor(0, 0);
2134 mCapture->checkPixel(5, 5, 50, 50, 50);
2135}
2136
2137TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002138 sp<SurfaceControl> child =
2139 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2140 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002141 fillSurfaceRGBA8(child, 200, 200, 200);
2142 auto childHandle = child->getHandle();
2143
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002144 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002145
2146 // Captures only the child layer, and not the parent.
2147 CaptureLayer::captureScreen(&mCapture, childHandle);
2148 mCapture->expectChildColor(0, 0);
2149 mCapture->expectChildColor(9, 9);
2150}
2151
2152TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002153 sp<SurfaceControl> child =
2154 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2155 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002156 fillSurfaceRGBA8(child, 200, 200, 200);
2157 auto childHandle = child->getHandle();
2158
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002159 sp<SurfaceControl> grandchild =
2160 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2161 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002162 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2163
2164 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002165 .show(child)
2166 .setPosition(grandchild, 5, 5)
2167 .show(grandchild)
2168 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002169
2170 auto grandchildHandle = grandchild->getHandle();
2171
2172 // Captures only the grandchild.
2173 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2174 mCapture->checkPixel(0, 0, 50, 50, 50);
2175 mCapture->checkPixel(4, 4, 50, 50, 50);
2176}
2177
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002178} // namespace android