blob: 9bbab7e3f80f4b757839e15ad596c26507b553b6 [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
Chia-I Wu57b27502017-10-31 10:14:40 -0700750TEST_F(LayerTransactionTest, SetFlagsHidden) {
751 sp<SurfaceControl> layer;
752 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
753 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
754
755 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
756 {
757 SCOPED_TRACE("layer hidden");
758 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
759 }
760
761 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
762 {
763 SCOPED_TRACE("layer shown");
764 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
765 }
766}
767
768TEST_F(LayerTransactionTest, SetFlagsOpaque) {
769 const Color translucentRed = {100, 0, 0, 100};
770 sp<SurfaceControl> layerR;
771 sp<SurfaceControl> layerG;
772 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
773 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
774 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
775 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
776
777 Transaction()
778 .setLayer(layerR, mLayerZBase + 1)
779 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
780 .apply();
781 {
782 SCOPED_TRACE("layerR opaque");
783 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
784 }
785
786 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
787 {
788 SCOPED_TRACE("layerR translucent");
789 const uint8_t g = uint8_t(255 - translucentRed.a);
790 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
791 }
792}
793
794TEST_F(LayerTransactionTest, SetFlagsSecure) {
795 sp<SurfaceControl> layer;
796 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
797 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
798
799 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
800 sp<IGraphicBufferProducer> producer;
801 sp<IGraphicBufferConsumer> consumer;
802 BufferQueue::createBufferQueue(&producer, &consumer);
803 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
804
805 Transaction()
806 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
807 .apply(true);
808 ASSERT_EQ(PERMISSION_DENIED,
809 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
810 false));
811
812 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
813 ASSERT_EQ(NO_ERROR,
814 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
815 false));
816}
817
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700818class LayerUpdateTest : public ::testing::Test {
819protected:
820 virtual void SetUp() {
821 mComposerClient = new SurfaceComposerClient;
822 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
823
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700824 sp<IBinder> display(
825 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700826 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700827 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700828
829 ssize_t displayWidth = info.w;
830 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700831
832 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700833 mBGSurfaceControl =
834 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
835 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700836 ASSERT_TRUE(mBGSurfaceControl != NULL);
837 ASSERT_TRUE(mBGSurfaceControl->isValid());
838 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
839
840 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700841 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
842 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700843 ASSERT_TRUE(mFGSurfaceControl != NULL);
844 ASSERT_TRUE(mFGSurfaceControl->isValid());
845
846 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
847
848 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700849 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
850 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700851 ASSERT_TRUE(mSyncSurfaceControl != NULL);
852 ASSERT_TRUE(mSyncSurfaceControl->isValid());
853
854 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
855
Robert Carr4cdc58f2017-08-23 14:22:20 -0700856 asTransaction([&](Transaction& t) {
857 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700858
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700859 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700860
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700861 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
862 .setPosition(mFGSurfaceControl, 64, 64)
863 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700864
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700865 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
866 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
867 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700868 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700869 }
870
871 virtual void TearDown() {
872 mComposerClient->dispose();
873 mBGSurfaceControl = 0;
874 mFGSurfaceControl = 0;
875 mSyncSurfaceControl = 0;
876 mComposerClient = 0;
877 }
878
879 void waitForPostedBuffers() {
880 // Since the sync surface is in synchronous mode (i.e. double buffered)
881 // posting three buffers to it should ensure that at least two
882 // SurfaceFlinger::handlePageFlip calls have been made, which should
883 // guaranteed that a buffer posted to another Surface has been retired.
884 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
885 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
886 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
887 }
888
Robert Carr4cdc58f2017-08-23 14:22:20 -0700889 void asTransaction(const std::function<void(Transaction&)>& exec) {
890 Transaction t;
891 exec(t);
892 t.apply(true);
893 }
894
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700895 sp<SurfaceComposerClient> mComposerClient;
896 sp<SurfaceControl> mBGSurfaceControl;
897 sp<SurfaceControl> mFGSurfaceControl;
898
899 // This surface is used to ensure that the buffers posted to
900 // mFGSurfaceControl have been picked up by SurfaceFlinger.
901 sp<SurfaceControl> mSyncSurfaceControl;
902};
903
Robert Carr7f619b22017-11-06 12:56:35 -0800904TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
905 sp<ScreenCapture> sc;
906
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700907 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
908 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -0800909 fillSurfaceRGBA8(relative, 10, 10, 10);
910 waitForPostedBuffers();
911
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700912 Transaction{}
913 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -0800914 .setPosition(relative, 64, 64)
915 .apply();
916
917 {
918 // The relative should be on top of the FG control.
919 ScreenCapture::captureScreen(&sc);
920 sc->checkPixel(64, 64, 10, 10, 10);
921 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700922 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800923
924 {
925 // Nothing should change at this point.
926 ScreenCapture::captureScreen(&sc);
927 sc->checkPixel(64, 64, 10, 10, 10);
928 }
929
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700930 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800931
932 {
933 // Ensure that the relative was actually hidden, rather than
934 // being left in the detached but visible state.
935 ScreenCapture::captureScreen(&sc);
936 sc->expectFGColor(64, 64);
937 }
938}
939
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700940TEST_F(LayerUpdateTest, LayerMoveWorks) {
941 sp<ScreenCapture> sc;
942 {
943 SCOPED_TRACE("before move");
944 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800945 sc->expectBGColor(0, 12);
946 sc->expectFGColor(75, 75);
947 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700948 }
949
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700950 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700951
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700952 {
953 // This should reflect the new position, but not the new color.
954 SCOPED_TRACE("after move, before redraw");
955 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800956 sc->expectBGColor(24, 24);
957 sc->expectBGColor(75, 75);
958 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700959 }
960
961 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
962 waitForPostedBuffers();
963 {
964 // This should reflect the new position and the new color.
965 SCOPED_TRACE("after redraw");
966 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800967 sc->expectBGColor(24, 24);
968 sc->expectBGColor(75, 75);
969 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700970 }
971}
972
973TEST_F(LayerUpdateTest, LayerResizeWorks) {
974 sp<ScreenCapture> sc;
975 {
976 SCOPED_TRACE("before resize");
977 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800978 sc->expectBGColor(0, 12);
979 sc->expectFGColor(75, 75);
980 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700981 }
982
Steve Block9d453682011-12-20 16:23:08 +0000983 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700984 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +0000985 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700986 {
987 // This should not reflect the new size or color because SurfaceFlinger
988 // has not yet received a buffer of the correct size.
989 SCOPED_TRACE("after resize, before redraw");
990 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800991 sc->expectBGColor(0, 12);
992 sc->expectFGColor(75, 75);
993 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700994 }
995
Steve Block9d453682011-12-20 16:23:08 +0000996 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700997 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
998 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000999 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001000 {
1001 // This should reflect the new size and the new color.
1002 SCOPED_TRACE("after redraw");
1003 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001004 sc->expectBGColor(24, 24);
1005 sc->checkPixel(75, 75, 63, 195, 63);
1006 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001007 }
1008}
1009
Haixia Shid5750962015-07-27 16:50:49 -07001010TEST_F(LayerUpdateTest, LayerCropWorks) {
1011 sp<ScreenCapture> sc;
1012 {
1013 SCOPED_TRACE("before crop");
1014 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001015 sc->expectBGColor(24, 24);
1016 sc->expectFGColor(75, 75);
1017 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001018 }
1019
Robert Carr4cdc58f2017-08-23 14:22:20 -07001020 asTransaction([&](Transaction& t) {
1021 Rect cropRect(16, 16, 32, 32);
1022 t.setCrop(mFGSurfaceControl, cropRect);
1023 });
Haixia Shid5750962015-07-27 16:50:49 -07001024 {
1025 // This should crop the foreground surface.
1026 SCOPED_TRACE("after crop");
1027 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001028 sc->expectBGColor(24, 24);
1029 sc->expectBGColor(75, 75);
1030 sc->expectFGColor(95, 80);
1031 sc->expectFGColor(80, 95);
1032 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001033 }
1034}
1035
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001036TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1037 sp<ScreenCapture> sc;
1038 {
1039 SCOPED_TRACE("before crop");
1040 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001041 sc->expectBGColor(24, 24);
1042 sc->expectFGColor(75, 75);
1043 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001044 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001045 asTransaction([&](Transaction& t) {
1046 Rect cropRect(16, 16, 32, 32);
1047 t.setFinalCrop(mFGSurfaceControl, cropRect);
1048 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001049 {
1050 // This should crop the foreground surface.
1051 SCOPED_TRACE("after crop");
1052 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001053 sc->expectBGColor(24, 24);
1054 sc->expectBGColor(75, 75);
1055 sc->expectBGColor(95, 80);
1056 sc->expectBGColor(80, 95);
1057 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001058 }
1059}
1060
Haixia Shid5750962015-07-27 16:50:49 -07001061TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1062 sp<ScreenCapture> sc;
1063 {
1064 SCOPED_TRACE("before setLayer");
1065 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001066 sc->expectBGColor(24, 24);
1067 sc->expectFGColor(75, 75);
1068 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001069 }
1070
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001071 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001072
Haixia Shid5750962015-07-27 16:50:49 -07001073 {
1074 // This should hide the foreground surface beneath the background.
1075 SCOPED_TRACE("after setLayer");
1076 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001077 sc->expectBGColor(24, 24);
1078 sc->expectBGColor(75, 75);
1079 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001080 }
1081}
1082
1083TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1084 sp<ScreenCapture> sc;
1085 {
1086 SCOPED_TRACE("before hide");
1087 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001088 sc->expectBGColor(24, 24);
1089 sc->expectFGColor(75, 75);
1090 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001091 }
1092
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001093 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001094
Haixia Shid5750962015-07-27 16:50:49 -07001095 {
1096 // This should hide the foreground surface.
1097 SCOPED_TRACE("after hide, before show");
1098 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001099 sc->expectBGColor(24, 24);
1100 sc->expectBGColor(75, 75);
1101 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001102 }
1103
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001104 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001105
Haixia Shid5750962015-07-27 16:50:49 -07001106 {
1107 // This should show the foreground surface.
1108 SCOPED_TRACE("after show");
1109 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001110 sc->expectBGColor(24, 24);
1111 sc->expectFGColor(75, 75);
1112 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001113 }
1114}
1115
1116TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1117 sp<ScreenCapture> sc;
1118 {
1119 SCOPED_TRACE("before setAlpha");
1120 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001121 sc->expectBGColor(24, 24);
1122 sc->expectFGColor(75, 75);
1123 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001124 }
1125
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001126 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001127
Haixia Shid5750962015-07-27 16:50:49 -07001128 {
1129 // This should set foreground to be 75% opaque.
1130 SCOPED_TRACE("after setAlpha");
1131 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001132 sc->expectBGColor(24, 24);
1133 sc->checkPixel(75, 75, 162, 63, 96);
1134 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001135 }
1136}
1137
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001138TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1139 sp<ScreenCapture> sc;
1140 {
1141 SCOPED_TRACE("before setLayerStack");
1142 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001143 sc->expectBGColor(24, 24);
1144 sc->expectFGColor(75, 75);
1145 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001146 }
1147
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001148 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001149 {
1150 // This should hide the foreground surface since it goes to a different
1151 // layer stack.
1152 SCOPED_TRACE("after setLayerStack");
1153 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001154 sc->expectBGColor(24, 24);
1155 sc->expectBGColor(75, 75);
1156 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001157 }
1158}
1159
1160TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1161 sp<ScreenCapture> sc;
1162 {
1163 SCOPED_TRACE("before setFlags");
1164 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001165 sc->expectBGColor(24, 24);
1166 sc->expectFGColor(75, 75);
1167 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001168 }
1169
Robert Carr4cdc58f2017-08-23 14:22:20 -07001170 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001171 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001172 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001173 {
1174 // This should hide the foreground surface
1175 SCOPED_TRACE("after setFlags");
1176 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001177 sc->expectBGColor(24, 24);
1178 sc->expectBGColor(75, 75);
1179 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001180 }
1181}
1182
1183TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1184 sp<ScreenCapture> sc;
1185 {
1186 SCOPED_TRACE("before setMatrix");
1187 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001188 sc->expectBGColor(24, 24);
1189 sc->expectFGColor(91, 96);
1190 sc->expectFGColor(96, 101);
1191 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001192 }
1193
Robert Carr4cdc58f2017-08-23 14:22:20 -07001194 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001195 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001196 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001197 {
1198 SCOPED_TRACE("after setMatrix");
1199 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001200 sc->expectBGColor(24, 24);
1201 sc->expectFGColor(91, 96);
1202 sc->expectBGColor(96, 91);
1203 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001204 }
1205}
1206
Robert Carr8d5227b2017-03-16 15:41:03 -07001207class GeometryLatchingTest : public LayerUpdateTest {
1208protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001209 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001210 SCOPED_TRACE(trace);
1211 ScreenCapture::captureScreen(&sc);
1212 // We find the leading edge of the FG surface.
1213 sc->expectFGColor(127, 127);
1214 sc->expectBGColor(128, 128);
1215 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001216
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001217 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001218
1219 void unlockFGBuffer() {
1220 sp<Surface> s = mFGSurfaceControl->getSurface();
1221 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1222 waitForPostedBuffers();
1223 }
1224
Robert Carr8d5227b2017-03-16 15:41:03 -07001225 void completeFGResize() {
1226 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1227 waitForPostedBuffers();
1228 }
1229 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001230 asTransaction([&](Transaction& t) {
1231 t.setSize(mFGSurfaceControl, 64, 64);
1232 t.setPosition(mFGSurfaceControl, 64, 64);
1233 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1234 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1235 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001236
1237 EXPECT_INITIAL_STATE("After restoring initial state");
1238 }
1239 sp<ScreenCapture> sc;
1240};
1241
1242TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1243 EXPECT_INITIAL_STATE("before anything");
1244
1245 // By default position can be updated even while
1246 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001247 asTransaction([&](Transaction& t) {
1248 t.setSize(mFGSurfaceControl, 32, 32);
1249 t.setPosition(mFGSurfaceControl, 100, 100);
1250 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001251
1252 {
1253 SCOPED_TRACE("After moving surface");
1254 ScreenCapture::captureScreen(&sc);
1255 // If we moved, the FG Surface should cover up what was previously BG
1256 // however if we didn't move the FG wouldn't be large enough now.
1257 sc->expectFGColor(163, 163);
1258 }
1259
1260 restoreInitialState();
1261
1262 // Now we repeat with setGeometryAppliesWithResize
1263 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001264 asTransaction([&](Transaction& t) {
1265 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1266 t.setSize(mFGSurfaceControl, 32, 32);
1267 t.setPosition(mFGSurfaceControl, 100, 100);
1268 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001269
1270 {
1271 SCOPED_TRACE("While resize is pending");
1272 ScreenCapture::captureScreen(&sc);
1273 // This time we shouldn't have moved, so the BG color
1274 // should still be visible.
1275 sc->expectBGColor(128, 128);
1276 }
1277
1278 completeFGResize();
1279
1280 {
1281 SCOPED_TRACE("After the resize");
1282 ScreenCapture::captureScreen(&sc);
1283 // But after the resize completes, we should move
1284 // and the FG should be visible here.
1285 sc->expectFGColor(128, 128);
1286 }
1287}
1288
1289class CropLatchingTest : public GeometryLatchingTest {
1290protected:
1291 void EXPECT_CROPPED_STATE(const char* trace) {
1292 SCOPED_TRACE(trace);
1293 ScreenCapture::captureScreen(&sc);
1294 // The edge should be moved back one pixel by our crop.
1295 sc->expectFGColor(126, 126);
1296 sc->expectBGColor(127, 127);
1297 sc->expectBGColor(128, 128);
1298 }
chaviw59f5c562017-06-28 16:39:06 -07001299
1300 void EXPECT_RESIZE_STATE(const char* trace) {
1301 SCOPED_TRACE(trace);
1302 ScreenCapture::captureScreen(&sc);
1303 // The FG is now resized too 128,128 at 64,64
1304 sc->expectFGColor(64, 64);
1305 sc->expectFGColor(191, 191);
1306 sc->expectBGColor(192, 192);
1307 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001308};
1309
1310TEST_F(CropLatchingTest, CropLatching) {
1311 EXPECT_INITIAL_STATE("before anything");
1312 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001313 asTransaction([&](Transaction& t) {
1314 t.setSize(mFGSurfaceControl, 128, 128);
1315 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1316 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001317
1318 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1319
1320 restoreInitialState();
1321
Robert Carr4cdc58f2017-08-23 14:22:20 -07001322 asTransaction([&](Transaction& t) {
1323 t.setSize(mFGSurfaceControl, 128, 128);
1324 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1325 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1326 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001327
1328 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1329
1330 completeFGResize();
1331
1332 EXPECT_CROPPED_STATE("after the resize finishes");
1333}
1334
1335TEST_F(CropLatchingTest, FinalCropLatching) {
1336 EXPECT_INITIAL_STATE("before anything");
1337 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001338 asTransaction([&](Transaction& t) {
1339 t.setSize(mFGSurfaceControl, 128, 128);
1340 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1341 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001342
1343 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1344
1345 restoreInitialState();
1346
Robert Carr4cdc58f2017-08-23 14:22:20 -07001347 asTransaction([&](Transaction& t) {
1348 t.setSize(mFGSurfaceControl, 128, 128);
1349 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1350 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1351 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001352
1353 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1354
1355 completeFGResize();
1356
1357 EXPECT_CROPPED_STATE("after the resize finishes");
1358}
1359
Robert Carr7bf247e2017-05-18 14:02:49 -07001360// In this test we ensure that setGeometryAppliesWithResize actually demands
1361// a buffer of the new size, and not just any size.
1362TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1363 EXPECT_INITIAL_STATE("before anything");
1364 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001365 asTransaction([&](Transaction& t) {
1366 t.setSize(mFGSurfaceControl, 128, 128);
1367 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1368 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001369
1370 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1371
1372 restoreInitialState();
1373
1374 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1375 // initiating the resize.
1376 lockAndFillFGBuffer();
1377
Robert Carr4cdc58f2017-08-23 14:22:20 -07001378 asTransaction([&](Transaction& t) {
1379 t.setSize(mFGSurfaceControl, 128, 128);
1380 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1381 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1382 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001383
1384 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1385
1386 // We now submit our old buffer, at the old size, and ensure it doesn't
1387 // trigger geometry latching.
1388 unlockFGBuffer();
1389
1390 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1391
1392 completeFGResize();
1393
1394 EXPECT_CROPPED_STATE("after the resize finishes");
1395}
1396
1397TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1398 EXPECT_INITIAL_STATE("before anything");
1399 // In this scenario, we attempt to set the final crop a second time while the resize
1400 // is still pending, and ensure we are successful. Success meaning the second crop
1401 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001402 asTransaction([&](Transaction& t) {
1403 t.setSize(mFGSurfaceControl, 128, 128);
1404 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1405 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1406 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001407
chaviw59f5c562017-06-28 16:39:06 -07001408 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1409
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001410 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001411
chaviw59f5c562017-06-28 16:39:06 -07001412 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001413
1414 completeFGResize();
1415
chaviw59f5c562017-06-28 16:39:06 -07001416 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001417}
1418
Pablo Ceballos05289c22016-04-14 15:49:55 -07001419TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1420 sp<ScreenCapture> sc;
1421 {
1422 SCOPED_TRACE("before anything");
1423 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001424 sc->expectBGColor(32, 32);
1425 sc->expectFGColor(96, 96);
1426 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001427 }
1428
1429 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001430 asTransaction([&](Transaction& t) {
1431 t.setAlpha(mFGSurfaceControl, 0.75);
1432 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001433 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001434 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001435
Robert Carr4cdc58f2017-08-23 14:22:20 -07001436 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001437 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001438 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001439 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001440 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001441
1442 {
1443 SCOPED_TRACE("before any trigger");
1444 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001445 sc->expectBGColor(32, 32);
1446 sc->expectFGColor(96, 96);
1447 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001448 }
1449
1450 // should trigger the first deferred transaction, but not the second one
1451 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1452 {
1453 SCOPED_TRACE("after first trigger");
1454 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001455 sc->expectBGColor(32, 32);
1456 sc->checkPixel(96, 96, 162, 63, 96);
1457 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001458 }
1459
1460 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001461 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001462
1463 // trigger the second deferred transaction
1464 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1465 {
1466 SCOPED_TRACE("after second trigger");
1467 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001468 sc->expectBGColor(32, 32);
1469 sc->expectBGColor(96, 96);
1470 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001471 }
1472}
1473
Robert Carrdb66e622017-04-10 16:55:57 -07001474TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1475 sp<ScreenCapture> sc;
1476 {
1477 SCOPED_TRACE("before adding relative surface");
1478 ScreenCapture::captureScreen(&sc);
1479 sc->expectBGColor(24, 24);
1480 sc->expectFGColor(75, 75);
1481 sc->expectBGColor(145, 145);
1482 }
1483
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001484 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1485 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001486 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1487 waitForPostedBuffers();
1488
1489 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001490 asTransaction([&](Transaction& t) {
1491 t.setPosition(relativeSurfaceControl, 64, 64);
1492 t.show(relativeSurfaceControl);
1493 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1494 });
Robert Carrdb66e622017-04-10 16:55:57 -07001495
1496 {
1497 SCOPED_TRACE("after adding relative surface");
1498 ScreenCapture::captureScreen(&sc);
1499 // our relative surface should be visible now.
1500 sc->checkPixel(75, 75, 255, 177, 177);
1501 }
1502
1503 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001504 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001505
1506 {
1507 SCOPED_TRACE("after set layer");
1508 ScreenCapture::captureScreen(&sc);
1509 // now the FG surface should be visible again.
1510 sc->expectFGColor(75, 75);
1511 }
1512}
1513
Robert Carre392b552017-09-19 12:16:05 -07001514TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1515 sp<ScreenCapture> sc;
1516
1517 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001518 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1519 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1520 sp<SurfaceControl> childBuffer =
1521 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1522 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001523 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1524
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001525 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001526
1527 {
1528 ScreenCapture::captureScreen(&sc);
1529 sc->expectChildColor(73, 73);
1530 sc->expectFGColor(74, 74);
1531 }
1532
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001533 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001534
1535 {
1536 ScreenCapture::captureScreen(&sc);
1537 sc->expectChildColor(73, 73);
1538 sc->expectChildColor(74, 74);
1539 }
1540}
1541
Robert Carr2c5f6d22017-09-26 12:30:35 -07001542TEST_F(LayerUpdateTest, MergingTransactions) {
1543 sp<ScreenCapture> sc;
1544 {
1545 SCOPED_TRACE("before move");
1546 ScreenCapture::captureScreen(&sc);
1547 sc->expectBGColor(0, 12);
1548 sc->expectFGColor(75, 75);
1549 sc->expectBGColor(145, 145);
1550 }
1551
1552 Transaction t1, t2;
1553 t1.setPosition(mFGSurfaceControl, 128, 128);
1554 t2.setPosition(mFGSurfaceControl, 0, 0);
1555 // We expect that the position update from t2 now
1556 // overwrites the position update from t1.
1557 t1.merge(std::move(t2));
1558 t1.apply();
1559
1560 {
1561 ScreenCapture::captureScreen(&sc);
1562 sc->expectFGColor(1, 1);
1563 }
1564}
1565
Robert Carr1f0a16a2016-10-24 16:27:39 -07001566class ChildLayerTest : public LayerUpdateTest {
1567protected:
1568 void SetUp() override {
1569 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001570 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1571 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001572 fillSurfaceRGBA8(mChild, 200, 200, 200);
1573
1574 {
1575 SCOPED_TRACE("before anything");
1576 ScreenCapture::captureScreen(&mCapture);
1577 mCapture->expectChildColor(64, 64);
1578 }
1579 }
1580 void TearDown() override {
1581 LayerUpdateTest::TearDown();
1582 mChild = 0;
1583 }
1584
1585 sp<SurfaceControl> mChild;
1586 sp<ScreenCapture> mCapture;
1587};
1588
1589TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001590 asTransaction([&](Transaction& t) {
1591 t.show(mChild);
1592 t.setPosition(mChild, 10, 10);
1593 t.setPosition(mFGSurfaceControl, 64, 64);
1594 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001595
1596 {
1597 ScreenCapture::captureScreen(&mCapture);
1598 // Top left of foreground must now be visible
1599 mCapture->expectFGColor(64, 64);
1600 // But 10 pixels in we should see the child surface
1601 mCapture->expectChildColor(74, 74);
1602 // And 10 more pixels we should be back to the foreground surface
1603 mCapture->expectFGColor(84, 84);
1604 }
1605
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001606 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001607
1608 {
1609 ScreenCapture::captureScreen(&mCapture);
1610 // Top left of foreground should now be at 0, 0
1611 mCapture->expectFGColor(0, 0);
1612 // But 10 pixels in we should see the child surface
1613 mCapture->expectChildColor(10, 10);
1614 // And 10 more pixels we should be back to the foreground surface
1615 mCapture->expectFGColor(20, 20);
1616 }
1617}
1618
Robert Carr41b08b52017-06-01 16:11:34 -07001619TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001620 asTransaction([&](Transaction& t) {
1621 t.show(mChild);
1622 t.setPosition(mChild, 0, 0);
1623 t.setPosition(mFGSurfaceControl, 0, 0);
1624 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1625 });
Robert Carr41b08b52017-06-01 16:11:34 -07001626
1627 {
1628 ScreenCapture::captureScreen(&mCapture);
1629 mCapture->expectChildColor(0, 0);
1630 mCapture->expectChildColor(4, 4);
1631 mCapture->expectBGColor(5, 5);
1632 }
1633}
1634
1635TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001636 asTransaction([&](Transaction& t) {
1637 t.show(mChild);
1638 t.setPosition(mChild, 0, 0);
1639 t.setPosition(mFGSurfaceControl, 0, 0);
1640 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1641 });
Robert Carr41b08b52017-06-01 16:11:34 -07001642
1643 {
1644 ScreenCapture::captureScreen(&mCapture);
1645 mCapture->expectChildColor(0, 0);
1646 mCapture->expectChildColor(4, 4);
1647 mCapture->expectBGColor(5, 5);
1648 }
1649}
1650
Robert Carr1f0a16a2016-10-24 16:27:39 -07001651TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001652 asTransaction([&](Transaction& t) {
1653 t.show(mChild);
1654 t.setPosition(mFGSurfaceControl, 0, 0);
1655 t.setPosition(mChild, 63, 63);
1656 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657
1658 {
1659 ScreenCapture::captureScreen(&mCapture);
1660 mCapture->expectFGColor(0, 0);
1661 // Last pixel in foreground should now be the child.
1662 mCapture->expectChildColor(63, 63);
1663 // But the child should be constrained and the next pixel
1664 // must be the background
1665 mCapture->expectBGColor(64, 64);
1666 }
1667}
1668
1669TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001670 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671
1672 // Find the boundary between the parent and child
1673 {
1674 ScreenCapture::captureScreen(&mCapture);
1675 mCapture->expectChildColor(9, 9);
1676 mCapture->expectFGColor(10, 10);
1677 }
1678
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001679 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001680
1681 // The boundary should be twice as far from the origin now.
1682 // The pixels from the last test should all be child now
1683 {
1684 ScreenCapture::captureScreen(&mCapture);
1685 mCapture->expectChildColor(9, 9);
1686 mCapture->expectChildColor(10, 10);
1687 mCapture->expectChildColor(19, 19);
1688 mCapture->expectFGColor(20, 20);
1689 }
1690}
Robert Carr9524cb32017-02-13 11:32:32 -08001691
Robert Carr6452f122017-03-21 10:41:29 -07001692TEST_F(ChildLayerTest, ChildLayerAlpha) {
1693 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1694 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1695 fillSurfaceRGBA8(mChild, 0, 254, 0);
1696 waitForPostedBuffers();
1697
Robert Carr4cdc58f2017-08-23 14:22:20 -07001698 asTransaction([&](Transaction& t) {
1699 t.show(mChild);
1700 t.setPosition(mChild, 0, 0);
1701 t.setPosition(mFGSurfaceControl, 0, 0);
1702 });
Robert Carr6452f122017-03-21 10:41:29 -07001703
1704 {
1705 ScreenCapture::captureScreen(&mCapture);
1706 // Unblended child color
1707 mCapture->checkPixel(0, 0, 0, 254, 0);
1708 }
1709
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001710 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001711
1712 {
1713 ScreenCapture::captureScreen(&mCapture);
1714 // Child and BG blended.
1715 mCapture->checkPixel(0, 0, 127, 127, 0);
1716 }
1717
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001718 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001719
1720 {
1721 ScreenCapture::captureScreen(&mCapture);
1722 // Child and BG blended.
1723 mCapture->checkPixel(0, 0, 95, 64, 95);
1724 }
1725}
1726
Robert Carr9524cb32017-02-13 11:32:32 -08001727TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001728 asTransaction([&](Transaction& t) {
1729 t.show(mChild);
1730 t.setPosition(mChild, 10, 10);
1731 t.setPosition(mFGSurfaceControl, 64, 64);
1732 });
Robert Carr9524cb32017-02-13 11:32:32 -08001733
1734 {
1735 ScreenCapture::captureScreen(&mCapture);
1736 // Top left of foreground must now be visible
1737 mCapture->expectFGColor(64, 64);
1738 // But 10 pixels in we should see the child surface
1739 mCapture->expectChildColor(74, 74);
1740 // And 10 more pixels we should be back to the foreground surface
1741 mCapture->expectFGColor(84, 84);
1742 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001743
1744 asTransaction([&](Transaction& t) {
1745 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1746 });
1747
Robert Carr9524cb32017-02-13 11:32:32 -08001748 {
1749 ScreenCapture::captureScreen(&mCapture);
1750 mCapture->expectFGColor(64, 64);
1751 // In reparenting we should have exposed the entire foreground surface.
1752 mCapture->expectFGColor(74, 74);
1753 // And the child layer should now begin at 10, 10 (since the BG
1754 // layer is at (0, 0)).
1755 mCapture->expectBGColor(9, 9);
1756 mCapture->expectChildColor(10, 10);
1757 }
1758}
1759
chaviw161410b02017-07-27 10:46:08 -07001760TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001761 asTransaction([&](Transaction& t) {
1762 t.show(mChild);
1763 t.setPosition(mChild, 10, 10);
1764 t.setPosition(mFGSurfaceControl, 64, 64);
1765 });
Robert Carr9524cb32017-02-13 11:32:32 -08001766
1767 {
1768 ScreenCapture::captureScreen(&mCapture);
1769 // Top left of foreground must now be visible
1770 mCapture->expectFGColor(64, 64);
1771 // But 10 pixels in we should see the child surface
1772 mCapture->expectChildColor(74, 74);
1773 // And 10 more pixels we should be back to the foreground surface
1774 mCapture->expectFGColor(84, 84);
1775 }
1776
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001777 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001778
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001779 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001780
chaviw161410b02017-07-27 10:46:08 -07001781 // Since the child has the same client as the parent, it will not get
1782 // detached and will be hidden.
1783 {
1784 ScreenCapture::captureScreen(&mCapture);
1785 mCapture->expectFGColor(64, 64);
1786 mCapture->expectFGColor(74, 74);
1787 mCapture->expectFGColor(84, 84);
1788 }
1789}
1790
1791TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1792 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001793 sp<SurfaceControl> mChildNewClient =
1794 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1795 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001796
1797 ASSERT_TRUE(mChildNewClient != NULL);
1798 ASSERT_TRUE(mChildNewClient->isValid());
1799
1800 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1801
Robert Carr4cdc58f2017-08-23 14:22:20 -07001802 asTransaction([&](Transaction& t) {
1803 t.hide(mChild);
1804 t.show(mChildNewClient);
1805 t.setPosition(mChildNewClient, 10, 10);
1806 t.setPosition(mFGSurfaceControl, 64, 64);
1807 });
chaviw161410b02017-07-27 10:46:08 -07001808
1809 {
1810 ScreenCapture::captureScreen(&mCapture);
1811 // Top left of foreground must now be visible
1812 mCapture->expectFGColor(64, 64);
1813 // But 10 pixels in we should see the child surface
1814 mCapture->expectChildColor(74, 74);
1815 // And 10 more pixels we should be back to the foreground surface
1816 mCapture->expectFGColor(84, 84);
1817 }
1818
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001819 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001820
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001821 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001822
Robert Carr9524cb32017-02-13 11:32:32 -08001823 // Nothing should have changed.
1824 {
1825 ScreenCapture::captureScreen(&mCapture);
1826 mCapture->expectFGColor(64, 64);
1827 mCapture->expectChildColor(74, 74);
1828 mCapture->expectFGColor(84, 84);
1829 }
1830}
1831
Robert Carr9b429f42017-04-17 14:56:57 -07001832TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001833 asTransaction([&](Transaction& t) {
1834 t.show(mChild);
1835 t.setPosition(mChild, 0, 0);
1836 t.setPosition(mFGSurfaceControl, 0, 0);
1837 });
Robert Carr9b429f42017-04-17 14:56:57 -07001838
1839 {
1840 ScreenCapture::captureScreen(&mCapture);
1841 // We've positioned the child in the top left.
1842 mCapture->expectChildColor(0, 0);
1843 // But it's only 10x10.
1844 mCapture->expectFGColor(10, 10);
1845 }
1846
Robert Carr4cdc58f2017-08-23 14:22:20 -07001847 asTransaction([&](Transaction& t) {
1848 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1849 // We cause scaling by 2.
1850 t.setSize(mFGSurfaceControl, 128, 128);
1851 });
Robert Carr9b429f42017-04-17 14:56:57 -07001852
1853 {
1854 ScreenCapture::captureScreen(&mCapture);
1855 // We've positioned the child in the top left.
1856 mCapture->expectChildColor(0, 0);
1857 mCapture->expectChildColor(10, 10);
1858 mCapture->expectChildColor(19, 19);
1859 // And now it should be scaled all the way to 20x20
1860 mCapture->expectFGColor(20, 20);
1861 }
1862}
1863
Robert Carr1725eee2017-04-26 18:32:15 -07001864// Regression test for b/37673612
1865TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001866 asTransaction([&](Transaction& t) {
1867 t.show(mChild);
1868 t.setPosition(mChild, 0, 0);
1869 t.setPosition(mFGSurfaceControl, 0, 0);
1870 });
Robert Carr1725eee2017-04-26 18:32:15 -07001871
1872 {
1873 ScreenCapture::captureScreen(&mCapture);
1874 // We've positioned the child in the top left.
1875 mCapture->expectChildColor(0, 0);
1876 // But it's only 10x10.
1877 mCapture->expectFGColor(10, 10);
1878 }
Robert Carr1725eee2017-04-26 18:32:15 -07001879 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1880 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001881 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001882 sp<Surface> s = mFGSurfaceControl->getSurface();
1883 auto anw = static_cast<ANativeWindow*>(s.get());
1884 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1885 native_window_set_buffers_dimensions(anw, 64, 128);
1886 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1887 waitForPostedBuffers();
1888
1889 {
1890 // The child should still be in the same place and not have any strange scaling as in
1891 // b/37673612.
1892 ScreenCapture::captureScreen(&mCapture);
1893 mCapture->expectChildColor(0, 0);
1894 mCapture->expectFGColor(10, 10);
1895 }
1896}
1897
Dan Stoza412903f2017-04-27 13:42:17 -07001898TEST_F(ChildLayerTest, Bug36858924) {
1899 // Destroy the child layer
1900 mChild.clear();
1901
1902 // Now recreate it as hidden
1903 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1904 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1905 mFGSurfaceControl.get());
1906
1907 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001908 asTransaction([&](Transaction& t) {
1909 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001910 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001911 t.show(mChild);
1912 });
Dan Stoza412903f2017-04-27 13:42:17 -07001913
1914 // Render the foreground surface a few times
1915 //
1916 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1917 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1918 // never acquire/release the first buffer
1919 ALOGI("Filling 1");
1920 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1921 ALOGI("Filling 2");
1922 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1923 ALOGI("Filling 3");
1924 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1925 ALOGI("Filling 4");
1926 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1927}
1928
chaviwf1961f72017-09-18 16:41:07 -07001929TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001930 asTransaction([&](Transaction& t) {
1931 t.show(mChild);
1932 t.setPosition(mChild, 10, 10);
1933 t.setPosition(mFGSurfaceControl, 64, 64);
1934 });
chaviw06178942017-07-27 10:25:59 -07001935
1936 {
1937 ScreenCapture::captureScreen(&mCapture);
1938 // Top left of foreground must now be visible
1939 mCapture->expectFGColor(64, 64);
1940 // But 10 pixels in we should see the child surface
1941 mCapture->expectChildColor(74, 74);
1942 // And 10 more pixels we should be back to the foreground surface
1943 mCapture->expectFGColor(84, 84);
1944 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001945
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001946 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001947
chaviw06178942017-07-27 10:25:59 -07001948 {
1949 ScreenCapture::captureScreen(&mCapture);
1950 mCapture->expectFGColor(64, 64);
1951 // In reparenting we should have exposed the entire foreground surface.
1952 mCapture->expectFGColor(74, 74);
1953 // And the child layer should now begin at 10, 10 (since the BG
1954 // layer is at (0, 0)).
1955 mCapture->expectBGColor(9, 9);
1956 mCapture->expectChildColor(10, 10);
1957 }
1958}
1959
chaviwf1961f72017-09-18 16:41:07 -07001960TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001961 asTransaction([&](Transaction& t) {
1962 t.show(mChild);
1963 t.setPosition(mChild, 10, 10);
1964 t.setPosition(mFGSurfaceControl, 64, 64);
1965 });
chaviwf1961f72017-09-18 16:41:07 -07001966
1967 {
1968 ScreenCapture::captureScreen(&mCapture);
1969 // Top left of foreground must now be visible
1970 mCapture->expectFGColor(64, 64);
1971 // But 10 pixels in we should see the child surface
1972 mCapture->expectChildColor(74, 74);
1973 // And 10 more pixels we should be back to the foreground surface
1974 mCapture->expectFGColor(84, 84);
1975 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001976 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07001977 {
1978 ScreenCapture::captureScreen(&mCapture);
1979 // Nothing should have changed.
1980 mCapture->expectFGColor(64, 64);
1981 mCapture->expectChildColor(74, 74);
1982 mCapture->expectFGColor(84, 84);
1983 }
1984}
1985
1986TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001987 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
1988 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07001989 ASSERT_TRUE(newSurface != NULL);
1990 ASSERT_TRUE(newSurface->isValid());
1991
1992 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001993 asTransaction([&](Transaction& t) {
1994 t.hide(mChild);
1995 t.show(newSurface);
1996 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001997 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001998 t.setPosition(mFGSurfaceControl, 64, 64);
1999 });
chaviwf1961f72017-09-18 16:41:07 -07002000
2001 {
2002 ScreenCapture::captureScreen(&mCapture);
2003 // Top left of foreground must now be visible
2004 mCapture->expectFGColor(64, 64);
2005 // At 10, 10 we should see the new surface
2006 mCapture->checkPixel(10, 10, 63, 195, 63);
2007 }
2008
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002009 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002010
2011 {
2012 ScreenCapture::captureScreen(&mCapture);
2013 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2014 // mFGSurface, putting it at 74, 74.
2015 mCapture->expectFGColor(64, 64);
2016 mCapture->checkPixel(74, 74, 63, 195, 63);
2017 mCapture->expectFGColor(84, 84);
2018 }
2019}
2020
chaviwc9674332017-08-28 12:32:18 -07002021TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002022 sp<SurfaceControl> grandchild =
2023 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2024 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002025 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2026
2027 {
2028 ScreenCapture::captureScreen(&mCapture);
2029 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2030 // which begins at 64, 64
2031 mCapture->checkPixel(64, 64, 50, 50, 50);
2032 }
2033}
2034
Robert Carr503c7042017-09-27 15:06:08 -07002035TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002036 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2037 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002038 fillSurfaceRGBA8(relative, 255, 255, 255);
2039
2040 Transaction t;
2041 t.setLayer(relative, INT32_MAX)
2042 .setRelativeLayer(mChild, relative->getHandle(), 1)
2043 .setPosition(mFGSurfaceControl, 0, 0)
2044 .apply(true);
2045
2046 // We expect that the child should have been elevated above our
2047 // INT_MAX layer even though it's not a child of it.
2048 {
2049 ScreenCapture::captureScreen(&mCapture);
2050 mCapture->expectChildColor(0, 0);
2051 mCapture->expectChildColor(9, 9);
2052 mCapture->checkPixel(10, 10, 255, 255, 255);
2053 }
2054}
2055
chaviw13fdc492017-06-27 12:40:18 -07002056class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002057protected:
chaviw13fdc492017-06-27 12:40:18 -07002058 void SetUp() override {
2059 LayerUpdateTest::SetUp();
2060
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002061 mLayerColorControl =
2062 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2063 PIXEL_FORMAT_RGBA_8888,
2064 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002065
2066 ASSERT_TRUE(mLayerColorControl != NULL);
2067 ASSERT_TRUE(mLayerColorControl->isValid());
2068
Robert Carr4cdc58f2017-08-23 14:22:20 -07002069 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002070 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002071 t.setPosition(mLayerColorControl, 140, 140);
2072 t.hide(mLayerColorControl);
2073 t.hide(mFGSurfaceControl);
2074 });
chaviw13fdc492017-06-27 12:40:18 -07002075 }
2076
2077 void TearDown() override {
2078 LayerUpdateTest::TearDown();
2079 mLayerColorControl = 0;
2080 }
2081
2082 sp<SurfaceControl> mLayerColorControl;
2083};
2084
2085TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2086 sp<ScreenCapture> sc;
2087
2088 {
2089 SCOPED_TRACE("before setColor");
2090 ScreenCapture::captureScreen(&sc);
2091 sc->expectBGColor(145, 145);
2092 }
2093
Robert Carr4cdc58f2017-08-23 14:22:20 -07002094 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002095 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002096 t.setColor(mLayerColorControl, color);
2097 t.show(mLayerColorControl);
2098 });
chaviw13fdc492017-06-27 12:40:18 -07002099
chaviw13fdc492017-06-27 12:40:18 -07002100 {
2101 // There should now be a color
2102 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002103
chaviw13fdc492017-06-27 12:40:18 -07002104 ScreenCapture::captureScreen(&sc);
2105 sc->checkPixel(145, 145, 43, 207, 131);
2106 }
2107}
2108
2109TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2110 sp<ScreenCapture> sc;
2111 {
2112 SCOPED_TRACE("before setColor");
2113 ScreenCapture::captureScreen(&sc);
2114 sc->expectBGColor(145, 145);
2115 }
2116
Robert Carr4cdc58f2017-08-23 14:22:20 -07002117 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002118 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002119 t.setColor(mLayerColorControl, color);
2120 t.setAlpha(mLayerColorControl, .75f);
2121 t.show(mLayerColorControl);
2122 });
2123
chaviw13fdc492017-06-27 12:40:18 -07002124 {
2125 // There should now be a color with .75 alpha
2126 SCOPED_TRACE("after setColor");
2127 ScreenCapture::captureScreen(&sc);
2128 sc->checkPixel(145, 145, 48, 171, 147);
2129 }
2130}
2131
2132TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2133 sp<ScreenCapture> sc;
2134 {
2135 SCOPED_TRACE("before setColor");
2136 ScreenCapture::captureScreen(&sc);
2137 sc->expectBGColor(145, 145);
2138 }
2139
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002140 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002141
chaviw13fdc492017-06-27 12:40:18 -07002142 {
2143 // There should now be set to 0,0,0 (black) as default.
2144 SCOPED_TRACE("after setColor");
2145 ScreenCapture::captureScreen(&sc);
2146 sc->checkPixel(145, 145, 0, 0, 0);
2147 }
2148}
2149
chaviwa76b2712017-09-20 12:02:26 -07002150class ScreenCaptureTest : public LayerUpdateTest {
2151protected:
2152 std::unique_ptr<CaptureLayer> mCapture;
2153};
2154
2155TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2156 auto bgHandle = mBGSurfaceControl->getHandle();
2157 CaptureLayer::captureScreen(&mCapture, bgHandle);
2158 mCapture->expectBGColor(0, 0);
2159 // Doesn't capture FG layer which is at 64, 64
2160 mCapture->expectBGColor(64, 64);
2161}
2162
2163TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2164 auto fgHandle = mFGSurfaceControl->getHandle();
2165
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002166 sp<SurfaceControl> child =
2167 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2168 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002169 fillSurfaceRGBA8(child, 200, 200, 200);
2170
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002171 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002172
2173 // Captures mFGSurfaceControl layer and its child.
2174 CaptureLayer::captureScreen(&mCapture, fgHandle);
2175 mCapture->expectFGColor(10, 10);
2176 mCapture->expectChildColor(0, 0);
2177}
2178
2179TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2180 auto fgHandle = mFGSurfaceControl->getHandle();
2181
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002182 sp<SurfaceControl> child =
2183 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2184 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002185 fillSurfaceRGBA8(child, 200, 200, 200);
2186
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002187 sp<SurfaceControl> grandchild =
2188 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2189 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002190
2191 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2192 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002193 .show(child)
2194 .setPosition(grandchild, 5, 5)
2195 .show(grandchild)
2196 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002197
2198 // Captures mFGSurfaceControl, its child, and the grandchild.
2199 CaptureLayer::captureScreen(&mCapture, fgHandle);
2200 mCapture->expectFGColor(10, 10);
2201 mCapture->expectChildColor(0, 0);
2202 mCapture->checkPixel(5, 5, 50, 50, 50);
2203}
2204
2205TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002206 sp<SurfaceControl> child =
2207 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2208 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002209 fillSurfaceRGBA8(child, 200, 200, 200);
2210 auto childHandle = child->getHandle();
2211
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002212 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002213
2214 // Captures only the child layer, and not the parent.
2215 CaptureLayer::captureScreen(&mCapture, childHandle);
2216 mCapture->expectChildColor(0, 0);
2217 mCapture->expectChildColor(9, 9);
2218}
2219
2220TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002221 sp<SurfaceControl> child =
2222 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2223 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002224 fillSurfaceRGBA8(child, 200, 200, 200);
2225 auto childHandle = child->getHandle();
2226
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002227 sp<SurfaceControl> grandchild =
2228 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2229 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002230 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2231
2232 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002233 .show(child)
2234 .setPosition(grandchild, 5, 5)
2235 .show(grandchild)
2236 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002237
2238 auto grandchildHandle = grandchild->getHandle();
2239
2240 // Captures only the grandchild.
2241 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2242 mCapture->checkPixel(0, 0, 50, 50, 50);
2243 mCapture->checkPixel(4, 4, 50, 50, 50);
2244}
2245
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002246} // namespace android