blob: 508dc673bdd89b79c76be4f393ff030acb7453fc [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;
51 static const Color BLACK;
52};
53
54const Color Color::RED{255, 0, 0, 255};
55const Color Color::BLACK{0, 0, 0, 255};
56
57std::ostream& operator<<(std::ostream& os, const Color& color) {
58 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
59 return os;
60}
61
62// Fill a region with the specified color.
63void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
64 int32_t x = rect.left;
65 int32_t y = rect.top;
66 int32_t width = rect.right - rect.left;
67 int32_t height = rect.bottom - rect.top;
68
69 if (x < 0) {
70 width += x;
71 x = 0;
72 }
73 if (y < 0) {
74 height += y;
75 y = 0;
76 }
77 if (x + width > buffer.width) {
78 x = std::min(x, buffer.width);
79 width = buffer.width - x;
80 }
81 if (y + height > buffer.height) {
82 y = std::min(y, buffer.height);
83 height = buffer.height - y;
84 }
85
86 for (int32_t j = 0; j < height; j++) {
87 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
88 for (int32_t i = 0; i < width; i++) {
89 dst[0] = color.r;
90 dst[1] = color.g;
91 dst[2] = color.b;
92 dst[3] = color.a;
93 dst += 4;
94 }
95 }
96}
97
98// Check if a region has the specified color.
99void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect,
100 const Color& color, uint8_t tolerance) {
101 int32_t x = rect.left;
102 int32_t y = rect.top;
103 int32_t width = rect.right - rect.left;
104 int32_t height = rect.bottom - rect.top;
105
106 if (x + width > int32_t(buffer.width)) {
107 x = std::min(x, int32_t(buffer.width));
108 width = buffer.width - x;
109 }
110 if (y + height > int32_t(buffer.height)) {
111 y = std::min(y, int32_t(buffer.height));
112 height = buffer.height - y;
113 }
114
115 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
116 uint8_t tmp = a >= b ? a - b : b - a;
117 return tmp <= tolerance;
118 };
119 for (int32_t j = 0; j < height; j++) {
120 const uint8_t* src =
121 static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4;
122 for (int32_t i = 0; i < width; i++) {
123 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
124 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
125 << "pixel @ (" << x + i << ", " << y + j << "): "
126 << "expected (" << color << "), "
127 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
128 src += 4;
129 }
130 }
131}
132
133} // anonymous namespace
134
Robert Carr4cdc58f2017-08-23 14:22:20 -0700135using Transaction = SurfaceComposerClient::Transaction;
136
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700137// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700138static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
139 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800140 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700141 sp<Surface> s = sc->getSurface();
142 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800143 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
144 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700145 for (int y = 0; y < outBuffer.height; y++) {
146 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700147 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700148 pixel[0] = r;
149 pixel[1] = g;
150 pixel[2] = b;
151 pixel[3] = 255;
152 }
153 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700154 if (unlock) {
155 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
156 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700157}
158
159// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
160// individual pixel values for testing purposes.
161class ScreenCapture : public RefBase {
162public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700163 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
164 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700165 sp<IGraphicBufferProducer> producer;
166 sp<IGraphicBufferConsumer> consumer;
167 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -0700168 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700169 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700170 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700171 SurfaceComposerClient::Transaction().apply(true);
172
Chia-I Wu718daf82017-10-20 11:57:17 -0700173 ASSERT_EQ(NO_ERROR,
174 sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false));
Michael Lentine5a16a622015-05-21 13:48:24 -0700175 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700176 }
177
Chia-I Wu718daf82017-10-20 11:57:17 -0700178 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
179 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
180 expectBufferColor(mBuf, rect, color, tolerance);
181 }
182
183 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
184 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
185 const bool leftBorder = rect.left > 0;
186 const bool topBorder = rect.top > 0;
187 const bool rightBorder = rect.right < int32_t(mBuf.width);
188 const bool bottomBorder = rect.bottom < int32_t(mBuf.height);
189
190 if (topBorder) {
191 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
192 if (leftBorder) {
193 top.left -= 1;
194 }
195 if (rightBorder) {
196 top.right += 1;
197 }
198 expectColor(top, color, tolerance);
199 }
200 if (leftBorder) {
201 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
202 expectColor(left, color, tolerance);
203 }
204 if (rightBorder) {
205 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
206 expectColor(right, color, tolerance);
207 }
208 if (bottomBorder) {
209 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
210 if (leftBorder) {
211 bottom.left -= 1;
212 }
213 if (rightBorder) {
214 bottom.right += 1;
215 }
216 expectColor(bottom, color, tolerance);
217 }
218 }
219
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700220 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700221 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
222 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
223 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700224 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
225 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700226 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
227 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700228 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700229 }
230 }
231
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700232 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700233
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700234 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700235
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700236 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700237
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700238private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700239 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700240 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
241 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700242
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700243 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700244
245 sp<CpuConsumer> mCC;
246 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700247};
248
chaviwa76b2712017-09-20 12:02:26 -0700249class CaptureLayer {
250public:
251 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
252 sp<IGraphicBufferProducer> producer;
253 sp<IGraphicBufferConsumer> consumer;
254 BufferQueue::createBufferQueue(&producer, &consumer);
255 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
256 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700257 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700258 SurfaceComposerClient::Transaction().apply(true);
259 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
260 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
261 }
262
263 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
264 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
265 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
266 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
267 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
268 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700269 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700270 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
271 EXPECT_EQ(String8(), err) << err.string();
272 }
273 }
274
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700275 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700276
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700277 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700278
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700279 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700280
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700281 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700282 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
283 }
284
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700285 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700286
287private:
288 sp<CpuConsumer> mCC;
289 CpuConsumer::LockedBuffer mBuffer;
290};
291
Chia-I Wu718daf82017-10-20 11:57:17 -0700292class LayerTransactionTest : public ::testing::Test {
293protected:
294 void SetUp() override {
295 mClient = new SurfaceComposerClient;
296 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
297
298 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
299 }
300
301 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
302 uint32_t flags = 0) {
303 auto layer =
304 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
305 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
306
307 status_t error = Transaction()
308 .setLayerStack(layer, mDisplayLayerStack)
309 .setLayer(layer, mLayerZBase)
310 .apply();
311 if (error != NO_ERROR) {
312 ADD_FAILURE() << "failed to initialize SurfaceControl";
313 layer.clear();
314 }
315
316 return layer;
317 }
318
319 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
320 // wait for previous transactions (such as setSize) to complete
321 Transaction().apply(true);
322
323 ANativeWindow_Buffer buffer = {};
324 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
325
326 return buffer;
327 }
328
329 void postLayerBuffer(const sp<SurfaceControl>& layer) {
330 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
331
332 // wait for the newly posted buffer to be latched
333 waitForLayerBuffers();
334 }
335
336 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
337 ANativeWindow_Buffer buffer;
338 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
339 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
340 postLayerBuffer(layer);
341 }
342
343 sp<ScreenCapture> screenshot() {
344 sp<ScreenCapture> screenshot;
345 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
346 return screenshot;
347 }
348
349 sp<SurfaceComposerClient> mClient;
350
351 sp<IBinder> mDisplay;
352 uint32_t mDisplayWidth;
353 uint32_t mDisplayHeight;
354 uint32_t mDisplayLayerStack;
355
356 // leave room for ~256 layers
357 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
358
359private:
360 void SetUpDisplay() {
361 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
362 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
363
364 // get display width/height
365 DisplayInfo info;
366 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
367 mDisplayWidth = info.w;
368 mDisplayHeight = info.h;
369
370 // After a new buffer is queued, SurfaceFlinger is notified and will
371 // latch the new buffer on next vsync. Let's heuristically wait for 3
372 // vsyncs.
373 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
374
375 mDisplayLayerStack = 0;
376 // set layer stack (b/68888219)
377 Transaction t;
378 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
379 t.apply();
380 }
381
382 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
383
384 int32_t mBufferPostDelay;
385};
386
387TEST_F(LayerTransactionTest, SetPositionBasic) {
388 sp<SurfaceControl> layer;
389 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
390 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
391
392 {
393 SCOPED_TRACE("default position");
394 auto shot = screenshot();
395 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
396 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
397 }
398
399 Transaction().setPosition(layer, 5, 10).apply();
400 {
401 SCOPED_TRACE("new position");
402 auto shot = screenshot();
403 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
404 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
405 }
406}
407
408TEST_F(LayerTransactionTest, SetPositionRounding) {
409 sp<SurfaceControl> layer;
410 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
411 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
412
413 // GLES requires only 4 bits of subpixel precision during rasterization
414 // XXX GLES composition does not match HWC composition due to precision
415 // loss (b/69315223)
416 const float epsilon = 1.0f / 16.0f;
417 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
418 {
419 SCOPED_TRACE("rounding down");
420 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
421 }
422
423 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
424 {
425 SCOPED_TRACE("rounding up");
426 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
427 }
428}
429
430TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
431 sp<SurfaceControl> layer;
432 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
433 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
434
435 Transaction().setPosition(layer, -32, -32).apply();
436 {
437 SCOPED_TRACE("negative coordinates");
438 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
439 }
440
441 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
442 {
443 SCOPED_TRACE("positive coordinates");
444 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
445 }
446}
447
448TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
449 sp<SurfaceControl> layer;
450 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
451 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
452
453 // partially out of bounds
454 Transaction().setPosition(layer, -30, -30).apply();
455 {
456 SCOPED_TRACE("negative coordinates");
457 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
458 }
459
460 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
461 {
462 SCOPED_TRACE("positive coordinates");
463 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
464 mDisplayHeight),
465 Color::RED);
466 }
467}
468
469TEST_F(LayerTransactionTest, SetPositionWithResize) {
470 sp<SurfaceControl> layer;
471 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
472 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
473
474 // setPosition is applied immediately by default, with or without resize
475 // pending
476 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
477 {
478 SCOPED_TRACE("resize pending");
479 auto shot = screenshot();
480 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
481 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
482 }
483
484 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
485 {
486 SCOPED_TRACE("resize applied");
487 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
488 }
489}
490
491TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
492 sp<SurfaceControl> layer;
493 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
494 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
495
496 // request setPosition to be applied with the next resize
497 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
498 {
499 SCOPED_TRACE("new position pending");
500 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
501 }
502
503 Transaction().setPosition(layer, 15, 20).apply();
504 {
505 SCOPED_TRACE("pending new position modified");
506 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
507 }
508
509 Transaction().setSize(layer, 64, 64).apply();
510 {
511 SCOPED_TRACE("resize pending");
512 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
513 }
514
515 // finally resize and latch the buffer
516 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
517 {
518 SCOPED_TRACE("new position applied");
519 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
520 }
521}
522
523TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
524 sp<SurfaceControl> layer;
525 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
526 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
527
528 // setPosition is not immediate even with SCALE_TO_WINDOW override
529 Transaction()
530 .setPosition(layer, 5, 10)
531 .setSize(layer, 64, 64)
532 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
533 .setGeometryAppliesWithResize(layer)
534 .apply();
535 {
536 SCOPED_TRACE("new position pending");
537 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
538 }
539
540 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
541 {
542 SCOPED_TRACE("new position applied");
543 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
544 }
545}
546
Chia-I Wu0eaea312017-10-31 10:14:40 -0700547TEST_F(LayerTransactionTest, SetSizeBasic) {
548 sp<SurfaceControl> layer;
549 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
550 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
551
552 Transaction().setSize(layer, 64, 64).apply();
553 {
554 SCOPED_TRACE("resize pending");
555 auto shot = screenshot();
556 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
557 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
558 }
559
560 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
561 {
562 SCOPED_TRACE("resize applied");
563 auto shot = screenshot();
564 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
565 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
566 }
567}
568
569TEST_F(LayerTransactionTest, SetSizeInvalid) {
570 // cannot test robustness against invalid sizes (zero or really huge)
571}
572
573TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
574 sp<SurfaceControl> layer;
575 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
576 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
577
578 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
579 Transaction()
580 .setSize(layer, 64, 64)
581 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
582 .apply();
583 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
584}
585
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700586class LayerUpdateTest : public ::testing::Test {
587protected:
588 virtual void SetUp() {
589 mComposerClient = new SurfaceComposerClient;
590 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
591
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700592 sp<IBinder> display(
593 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700594 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700595 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700596
597 ssize_t displayWidth = info.w;
598 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700599
600 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700601 mBGSurfaceControl =
602 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
603 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700604 ASSERT_TRUE(mBGSurfaceControl != NULL);
605 ASSERT_TRUE(mBGSurfaceControl->isValid());
606 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
607
608 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700609 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
610 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700611 ASSERT_TRUE(mFGSurfaceControl != NULL);
612 ASSERT_TRUE(mFGSurfaceControl->isValid());
613
614 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
615
616 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700617 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
618 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700619 ASSERT_TRUE(mSyncSurfaceControl != NULL);
620 ASSERT_TRUE(mSyncSurfaceControl->isValid());
621
622 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
623
Robert Carr4cdc58f2017-08-23 14:22:20 -0700624 asTransaction([&](Transaction& t) {
625 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700626
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700627 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700628
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700629 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
630 .setPosition(mFGSurfaceControl, 64, 64)
631 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700632
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700633 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
634 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
635 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700636 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700637 }
638
639 virtual void TearDown() {
640 mComposerClient->dispose();
641 mBGSurfaceControl = 0;
642 mFGSurfaceControl = 0;
643 mSyncSurfaceControl = 0;
644 mComposerClient = 0;
645 }
646
647 void waitForPostedBuffers() {
648 // Since the sync surface is in synchronous mode (i.e. double buffered)
649 // posting three buffers to it should ensure that at least two
650 // SurfaceFlinger::handlePageFlip calls have been made, which should
651 // guaranteed that a buffer posted to another Surface has been retired.
652 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
653 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
654 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
655 }
656
Robert Carr4cdc58f2017-08-23 14:22:20 -0700657 void asTransaction(const std::function<void(Transaction&)>& exec) {
658 Transaction t;
659 exec(t);
660 t.apply(true);
661 }
662
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700663 sp<SurfaceComposerClient> mComposerClient;
664 sp<SurfaceControl> mBGSurfaceControl;
665 sp<SurfaceControl> mFGSurfaceControl;
666
667 // This surface is used to ensure that the buffers posted to
668 // mFGSurfaceControl have been picked up by SurfaceFlinger.
669 sp<SurfaceControl> mSyncSurfaceControl;
670};
671
Robert Carr7f619b22017-11-06 12:56:35 -0800672TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
673 sp<ScreenCapture> sc;
674
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700675 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
676 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -0800677 fillSurfaceRGBA8(relative, 10, 10, 10);
678 waitForPostedBuffers();
679
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700680 Transaction{}
681 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -0800682 .setPosition(relative, 64, 64)
683 .apply();
684
685 {
686 // The relative should be on top of the FG control.
687 ScreenCapture::captureScreen(&sc);
688 sc->checkPixel(64, 64, 10, 10, 10);
689 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700690 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800691
692 {
693 // Nothing should change at this point.
694 ScreenCapture::captureScreen(&sc);
695 sc->checkPixel(64, 64, 10, 10, 10);
696 }
697
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700698 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800699
700 {
701 // Ensure that the relative was actually hidden, rather than
702 // being left in the detached but visible state.
703 ScreenCapture::captureScreen(&sc);
704 sc->expectFGColor(64, 64);
705 }
706}
707
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700708TEST_F(LayerUpdateTest, LayerMoveWorks) {
709 sp<ScreenCapture> sc;
710 {
711 SCOPED_TRACE("before move");
712 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800713 sc->expectBGColor(0, 12);
714 sc->expectFGColor(75, 75);
715 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700716 }
717
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700718 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700719
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700720 {
721 // This should reflect the new position, but not the new color.
722 SCOPED_TRACE("after move, before redraw");
723 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800724 sc->expectBGColor(24, 24);
725 sc->expectBGColor(75, 75);
726 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700727 }
728
729 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
730 waitForPostedBuffers();
731 {
732 // This should reflect the new position and the new color.
733 SCOPED_TRACE("after redraw");
734 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800735 sc->expectBGColor(24, 24);
736 sc->expectBGColor(75, 75);
737 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700738 }
739}
740
741TEST_F(LayerUpdateTest, LayerResizeWorks) {
742 sp<ScreenCapture> sc;
743 {
744 SCOPED_TRACE("before resize");
745 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800746 sc->expectBGColor(0, 12);
747 sc->expectFGColor(75, 75);
748 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700749 }
750
Steve Block9d453682011-12-20 16:23:08 +0000751 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700752 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +0000753 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700754 {
755 // This should not reflect the new size or color because SurfaceFlinger
756 // has not yet received a buffer of the correct size.
757 SCOPED_TRACE("after resize, before redraw");
758 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800759 sc->expectBGColor(0, 12);
760 sc->expectFGColor(75, 75);
761 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700762 }
763
Steve Block9d453682011-12-20 16:23:08 +0000764 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700765 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
766 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000767 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700768 {
769 // This should reflect the new size and the new color.
770 SCOPED_TRACE("after redraw");
771 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800772 sc->expectBGColor(24, 24);
773 sc->checkPixel(75, 75, 63, 195, 63);
774 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700775 }
776}
777
Haixia Shid5750962015-07-27 16:50:49 -0700778TEST_F(LayerUpdateTest, LayerCropWorks) {
779 sp<ScreenCapture> sc;
780 {
781 SCOPED_TRACE("before crop");
782 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800783 sc->expectBGColor(24, 24);
784 sc->expectFGColor(75, 75);
785 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700786 }
787
Robert Carr4cdc58f2017-08-23 14:22:20 -0700788 asTransaction([&](Transaction& t) {
789 Rect cropRect(16, 16, 32, 32);
790 t.setCrop(mFGSurfaceControl, cropRect);
791 });
Haixia Shid5750962015-07-27 16:50:49 -0700792 {
793 // This should crop the foreground surface.
794 SCOPED_TRACE("after crop");
795 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800796 sc->expectBGColor(24, 24);
797 sc->expectBGColor(75, 75);
798 sc->expectFGColor(95, 80);
799 sc->expectFGColor(80, 95);
800 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700801 }
802}
803
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000804TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
805 sp<ScreenCapture> sc;
806 {
807 SCOPED_TRACE("before crop");
808 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800809 sc->expectBGColor(24, 24);
810 sc->expectFGColor(75, 75);
811 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000812 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700813 asTransaction([&](Transaction& t) {
814 Rect cropRect(16, 16, 32, 32);
815 t.setFinalCrop(mFGSurfaceControl, cropRect);
816 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000817 {
818 // This should crop the foreground surface.
819 SCOPED_TRACE("after crop");
820 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800821 sc->expectBGColor(24, 24);
822 sc->expectBGColor(75, 75);
823 sc->expectBGColor(95, 80);
824 sc->expectBGColor(80, 95);
825 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000826 }
827}
828
Haixia Shid5750962015-07-27 16:50:49 -0700829TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
830 sp<ScreenCapture> sc;
831 {
832 SCOPED_TRACE("before setLayer");
833 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800834 sc->expectBGColor(24, 24);
835 sc->expectFGColor(75, 75);
836 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700837 }
838
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700839 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700840
Haixia Shid5750962015-07-27 16:50:49 -0700841 {
842 // This should hide the foreground surface beneath the background.
843 SCOPED_TRACE("after setLayer");
844 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800845 sc->expectBGColor(24, 24);
846 sc->expectBGColor(75, 75);
847 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700848 }
849}
850
851TEST_F(LayerUpdateTest, LayerShowHideWorks) {
852 sp<ScreenCapture> sc;
853 {
854 SCOPED_TRACE("before hide");
855 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800856 sc->expectBGColor(24, 24);
857 sc->expectFGColor(75, 75);
858 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700859 }
860
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700861 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700862
Haixia Shid5750962015-07-27 16:50:49 -0700863 {
864 // This should hide the foreground surface.
865 SCOPED_TRACE("after hide, before show");
866 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800867 sc->expectBGColor(24, 24);
868 sc->expectBGColor(75, 75);
869 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700870 }
871
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700872 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700873
Haixia Shid5750962015-07-27 16:50:49 -0700874 {
875 // This should show the foreground surface.
876 SCOPED_TRACE("after show");
877 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800878 sc->expectBGColor(24, 24);
879 sc->expectFGColor(75, 75);
880 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700881 }
882}
883
884TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
885 sp<ScreenCapture> sc;
886 {
887 SCOPED_TRACE("before setAlpha");
888 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800889 sc->expectBGColor(24, 24);
890 sc->expectFGColor(75, 75);
891 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700892 }
893
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700894 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700895
Haixia Shid5750962015-07-27 16:50:49 -0700896 {
897 // This should set foreground to be 75% opaque.
898 SCOPED_TRACE("after setAlpha");
899 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800900 sc->expectBGColor(24, 24);
901 sc->checkPixel(75, 75, 162, 63, 96);
902 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700903 }
904}
905
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700906TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
907 sp<ScreenCapture> sc;
908 {
909 SCOPED_TRACE("before setLayerStack");
910 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800911 sc->expectBGColor(24, 24);
912 sc->expectFGColor(75, 75);
913 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700914 }
915
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700916 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700917 {
918 // This should hide the foreground surface since it goes to a different
919 // layer stack.
920 SCOPED_TRACE("after setLayerStack");
921 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800922 sc->expectBGColor(24, 24);
923 sc->expectBGColor(75, 75);
924 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700925 }
926}
927
928TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
929 sp<ScreenCapture> sc;
930 {
931 SCOPED_TRACE("before setFlags");
932 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800933 sc->expectBGColor(24, 24);
934 sc->expectFGColor(75, 75);
935 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700936 }
937
Robert Carr4cdc58f2017-08-23 14:22:20 -0700938 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700939 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700940 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700941 {
942 // This should hide the foreground surface
943 SCOPED_TRACE("after setFlags");
944 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800945 sc->expectBGColor(24, 24);
946 sc->expectBGColor(75, 75);
947 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700948 }
949}
950
951TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
952 sp<ScreenCapture> sc;
953 {
954 SCOPED_TRACE("before setMatrix");
955 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800956 sc->expectBGColor(24, 24);
957 sc->expectFGColor(91, 96);
958 sc->expectFGColor(96, 101);
959 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700960 }
961
Robert Carr4cdc58f2017-08-23 14:22:20 -0700962 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700963 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700964 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700965 {
966 SCOPED_TRACE("after setMatrix");
967 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800968 sc->expectBGColor(24, 24);
969 sc->expectFGColor(91, 96);
970 sc->expectBGColor(96, 91);
971 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700972 }
973}
974
Robert Carr8d5227b2017-03-16 15:41:03 -0700975class GeometryLatchingTest : public LayerUpdateTest {
976protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700977 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -0700978 SCOPED_TRACE(trace);
979 ScreenCapture::captureScreen(&sc);
980 // We find the leading edge of the FG surface.
981 sc->expectFGColor(127, 127);
982 sc->expectBGColor(128, 128);
983 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700984
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700985 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -0700986
987 void unlockFGBuffer() {
988 sp<Surface> s = mFGSurfaceControl->getSurface();
989 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
990 waitForPostedBuffers();
991 }
992
Robert Carr8d5227b2017-03-16 15:41:03 -0700993 void completeFGResize() {
994 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
995 waitForPostedBuffers();
996 }
997 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700998 asTransaction([&](Transaction& t) {
999 t.setSize(mFGSurfaceControl, 64, 64);
1000 t.setPosition(mFGSurfaceControl, 64, 64);
1001 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1002 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1003 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001004
1005 EXPECT_INITIAL_STATE("After restoring initial state");
1006 }
1007 sp<ScreenCapture> sc;
1008};
1009
1010TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1011 EXPECT_INITIAL_STATE("before anything");
1012
1013 // By default position can be updated even while
1014 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001015 asTransaction([&](Transaction& t) {
1016 t.setSize(mFGSurfaceControl, 32, 32);
1017 t.setPosition(mFGSurfaceControl, 100, 100);
1018 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001019
1020 {
1021 SCOPED_TRACE("After moving surface");
1022 ScreenCapture::captureScreen(&sc);
1023 // If we moved, the FG Surface should cover up what was previously BG
1024 // however if we didn't move the FG wouldn't be large enough now.
1025 sc->expectFGColor(163, 163);
1026 }
1027
1028 restoreInitialState();
1029
1030 // Now we repeat with setGeometryAppliesWithResize
1031 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001032 asTransaction([&](Transaction& t) {
1033 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1034 t.setSize(mFGSurfaceControl, 32, 32);
1035 t.setPosition(mFGSurfaceControl, 100, 100);
1036 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001037
1038 {
1039 SCOPED_TRACE("While resize is pending");
1040 ScreenCapture::captureScreen(&sc);
1041 // This time we shouldn't have moved, so the BG color
1042 // should still be visible.
1043 sc->expectBGColor(128, 128);
1044 }
1045
1046 completeFGResize();
1047
1048 {
1049 SCOPED_TRACE("After the resize");
1050 ScreenCapture::captureScreen(&sc);
1051 // But after the resize completes, we should move
1052 // and the FG should be visible here.
1053 sc->expectFGColor(128, 128);
1054 }
1055}
1056
1057class CropLatchingTest : public GeometryLatchingTest {
1058protected:
1059 void EXPECT_CROPPED_STATE(const char* trace) {
1060 SCOPED_TRACE(trace);
1061 ScreenCapture::captureScreen(&sc);
1062 // The edge should be moved back one pixel by our crop.
1063 sc->expectFGColor(126, 126);
1064 sc->expectBGColor(127, 127);
1065 sc->expectBGColor(128, 128);
1066 }
chaviw59f5c562017-06-28 16:39:06 -07001067
1068 void EXPECT_RESIZE_STATE(const char* trace) {
1069 SCOPED_TRACE(trace);
1070 ScreenCapture::captureScreen(&sc);
1071 // The FG is now resized too 128,128 at 64,64
1072 sc->expectFGColor(64, 64);
1073 sc->expectFGColor(191, 191);
1074 sc->expectBGColor(192, 192);
1075 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001076};
1077
1078TEST_F(CropLatchingTest, CropLatching) {
1079 EXPECT_INITIAL_STATE("before anything");
1080 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001081 asTransaction([&](Transaction& t) {
1082 t.setSize(mFGSurfaceControl, 128, 128);
1083 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1084 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001085
1086 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1087
1088 restoreInitialState();
1089
Robert Carr4cdc58f2017-08-23 14:22:20 -07001090 asTransaction([&](Transaction& t) {
1091 t.setSize(mFGSurfaceControl, 128, 128);
1092 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1093 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1094 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001095
1096 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1097
1098 completeFGResize();
1099
1100 EXPECT_CROPPED_STATE("after the resize finishes");
1101}
1102
1103TEST_F(CropLatchingTest, FinalCropLatching) {
1104 EXPECT_INITIAL_STATE("before anything");
1105 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001106 asTransaction([&](Transaction& t) {
1107 t.setSize(mFGSurfaceControl, 128, 128);
1108 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1109 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001110
1111 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1112
1113 restoreInitialState();
1114
Robert Carr4cdc58f2017-08-23 14:22:20 -07001115 asTransaction([&](Transaction& t) {
1116 t.setSize(mFGSurfaceControl, 128, 128);
1117 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1118 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1119 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001120
1121 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1122
1123 completeFGResize();
1124
1125 EXPECT_CROPPED_STATE("after the resize finishes");
1126}
1127
Robert Carr7bf247e2017-05-18 14:02:49 -07001128// In this test we ensure that setGeometryAppliesWithResize actually demands
1129// a buffer of the new size, and not just any size.
1130TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1131 EXPECT_INITIAL_STATE("before anything");
1132 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001133 asTransaction([&](Transaction& t) {
1134 t.setSize(mFGSurfaceControl, 128, 128);
1135 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1136 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001137
1138 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1139
1140 restoreInitialState();
1141
1142 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1143 // initiating the resize.
1144 lockAndFillFGBuffer();
1145
Robert Carr4cdc58f2017-08-23 14:22:20 -07001146 asTransaction([&](Transaction& t) {
1147 t.setSize(mFGSurfaceControl, 128, 128);
1148 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1149 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1150 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001151
1152 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1153
1154 // We now submit our old buffer, at the old size, and ensure it doesn't
1155 // trigger geometry latching.
1156 unlockFGBuffer();
1157
1158 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1159
1160 completeFGResize();
1161
1162 EXPECT_CROPPED_STATE("after the resize finishes");
1163}
1164
1165TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1166 EXPECT_INITIAL_STATE("before anything");
1167 // In this scenario, we attempt to set the final crop a second time while the resize
1168 // is still pending, and ensure we are successful. Success meaning the second crop
1169 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001170 asTransaction([&](Transaction& t) {
1171 t.setSize(mFGSurfaceControl, 128, 128);
1172 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1173 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1174 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001175
chaviw59f5c562017-06-28 16:39:06 -07001176 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1177
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001178 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001179
chaviw59f5c562017-06-28 16:39:06 -07001180 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001181
1182 completeFGResize();
1183
chaviw59f5c562017-06-28 16:39:06 -07001184 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001185}
1186
Pablo Ceballos05289c22016-04-14 15:49:55 -07001187TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1188 sp<ScreenCapture> sc;
1189 {
1190 SCOPED_TRACE("before anything");
1191 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001192 sc->expectBGColor(32, 32);
1193 sc->expectFGColor(96, 96);
1194 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001195 }
1196
1197 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001198 asTransaction([&](Transaction& t) {
1199 t.setAlpha(mFGSurfaceControl, 0.75);
1200 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001201 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001202 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001203
Robert Carr4cdc58f2017-08-23 14:22:20 -07001204 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001205 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001206 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001207 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001208 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001209
1210 {
1211 SCOPED_TRACE("before any trigger");
1212 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001213 sc->expectBGColor(32, 32);
1214 sc->expectFGColor(96, 96);
1215 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001216 }
1217
1218 // should trigger the first deferred transaction, but not the second one
1219 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1220 {
1221 SCOPED_TRACE("after first trigger");
1222 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001223 sc->expectBGColor(32, 32);
1224 sc->checkPixel(96, 96, 162, 63, 96);
1225 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001226 }
1227
1228 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001229 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001230
1231 // trigger the second deferred transaction
1232 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1233 {
1234 SCOPED_TRACE("after second trigger");
1235 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001236 sc->expectBGColor(32, 32);
1237 sc->expectBGColor(96, 96);
1238 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001239 }
1240}
1241
Robert Carrdb66e622017-04-10 16:55:57 -07001242TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1243 sp<ScreenCapture> sc;
1244 {
1245 SCOPED_TRACE("before adding relative surface");
1246 ScreenCapture::captureScreen(&sc);
1247 sc->expectBGColor(24, 24);
1248 sc->expectFGColor(75, 75);
1249 sc->expectBGColor(145, 145);
1250 }
1251
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001252 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1253 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001254 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1255 waitForPostedBuffers();
1256
1257 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001258 asTransaction([&](Transaction& t) {
1259 t.setPosition(relativeSurfaceControl, 64, 64);
1260 t.show(relativeSurfaceControl);
1261 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1262 });
Robert Carrdb66e622017-04-10 16:55:57 -07001263
1264 {
1265 SCOPED_TRACE("after adding relative surface");
1266 ScreenCapture::captureScreen(&sc);
1267 // our relative surface should be visible now.
1268 sc->checkPixel(75, 75, 255, 177, 177);
1269 }
1270
1271 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001272 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001273
1274 {
1275 SCOPED_TRACE("after set layer");
1276 ScreenCapture::captureScreen(&sc);
1277 // now the FG surface should be visible again.
1278 sc->expectFGColor(75, 75);
1279 }
1280}
1281
Robert Carre392b552017-09-19 12:16:05 -07001282TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1283 sp<ScreenCapture> sc;
1284
1285 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001286 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1287 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1288 sp<SurfaceControl> childBuffer =
1289 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1290 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001291 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1292
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001293 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001294
1295 {
1296 ScreenCapture::captureScreen(&sc);
1297 sc->expectChildColor(73, 73);
1298 sc->expectFGColor(74, 74);
1299 }
1300
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001301 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001302
1303 {
1304 ScreenCapture::captureScreen(&sc);
1305 sc->expectChildColor(73, 73);
1306 sc->expectChildColor(74, 74);
1307 }
1308}
1309
Robert Carr2c5f6d22017-09-26 12:30:35 -07001310TEST_F(LayerUpdateTest, MergingTransactions) {
1311 sp<ScreenCapture> sc;
1312 {
1313 SCOPED_TRACE("before move");
1314 ScreenCapture::captureScreen(&sc);
1315 sc->expectBGColor(0, 12);
1316 sc->expectFGColor(75, 75);
1317 sc->expectBGColor(145, 145);
1318 }
1319
1320 Transaction t1, t2;
1321 t1.setPosition(mFGSurfaceControl, 128, 128);
1322 t2.setPosition(mFGSurfaceControl, 0, 0);
1323 // We expect that the position update from t2 now
1324 // overwrites the position update from t1.
1325 t1.merge(std::move(t2));
1326 t1.apply();
1327
1328 {
1329 ScreenCapture::captureScreen(&sc);
1330 sc->expectFGColor(1, 1);
1331 }
1332}
1333
Robert Carr1f0a16a2016-10-24 16:27:39 -07001334class ChildLayerTest : public LayerUpdateTest {
1335protected:
1336 void SetUp() override {
1337 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001338 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1339 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001340 fillSurfaceRGBA8(mChild, 200, 200, 200);
1341
1342 {
1343 SCOPED_TRACE("before anything");
1344 ScreenCapture::captureScreen(&mCapture);
1345 mCapture->expectChildColor(64, 64);
1346 }
1347 }
1348 void TearDown() override {
1349 LayerUpdateTest::TearDown();
1350 mChild = 0;
1351 }
1352
1353 sp<SurfaceControl> mChild;
1354 sp<ScreenCapture> mCapture;
1355};
1356
1357TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001358 asTransaction([&](Transaction& t) {
1359 t.show(mChild);
1360 t.setPosition(mChild, 10, 10);
1361 t.setPosition(mFGSurfaceControl, 64, 64);
1362 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001363
1364 {
1365 ScreenCapture::captureScreen(&mCapture);
1366 // Top left of foreground must now be visible
1367 mCapture->expectFGColor(64, 64);
1368 // But 10 pixels in we should see the child surface
1369 mCapture->expectChildColor(74, 74);
1370 // And 10 more pixels we should be back to the foreground surface
1371 mCapture->expectFGColor(84, 84);
1372 }
1373
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001374 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001375
1376 {
1377 ScreenCapture::captureScreen(&mCapture);
1378 // Top left of foreground should now be at 0, 0
1379 mCapture->expectFGColor(0, 0);
1380 // But 10 pixels in we should see the child surface
1381 mCapture->expectChildColor(10, 10);
1382 // And 10 more pixels we should be back to the foreground surface
1383 mCapture->expectFGColor(20, 20);
1384 }
1385}
1386
Robert Carr41b08b52017-06-01 16:11:34 -07001387TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001388 asTransaction([&](Transaction& t) {
1389 t.show(mChild);
1390 t.setPosition(mChild, 0, 0);
1391 t.setPosition(mFGSurfaceControl, 0, 0);
1392 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1393 });
Robert Carr41b08b52017-06-01 16:11:34 -07001394
1395 {
1396 ScreenCapture::captureScreen(&mCapture);
1397 mCapture->expectChildColor(0, 0);
1398 mCapture->expectChildColor(4, 4);
1399 mCapture->expectBGColor(5, 5);
1400 }
1401}
1402
1403TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001404 asTransaction([&](Transaction& t) {
1405 t.show(mChild);
1406 t.setPosition(mChild, 0, 0);
1407 t.setPosition(mFGSurfaceControl, 0, 0);
1408 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1409 });
Robert Carr41b08b52017-06-01 16:11:34 -07001410
1411 {
1412 ScreenCapture::captureScreen(&mCapture);
1413 mCapture->expectChildColor(0, 0);
1414 mCapture->expectChildColor(4, 4);
1415 mCapture->expectBGColor(5, 5);
1416 }
1417}
1418
Robert Carr1f0a16a2016-10-24 16:27:39 -07001419TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001420 asTransaction([&](Transaction& t) {
1421 t.show(mChild);
1422 t.setPosition(mFGSurfaceControl, 0, 0);
1423 t.setPosition(mChild, 63, 63);
1424 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001425
1426 {
1427 ScreenCapture::captureScreen(&mCapture);
1428 mCapture->expectFGColor(0, 0);
1429 // Last pixel in foreground should now be the child.
1430 mCapture->expectChildColor(63, 63);
1431 // But the child should be constrained and the next pixel
1432 // must be the background
1433 mCapture->expectBGColor(64, 64);
1434 }
1435}
1436
1437TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001438 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001439
1440 // Find the boundary between the parent and child
1441 {
1442 ScreenCapture::captureScreen(&mCapture);
1443 mCapture->expectChildColor(9, 9);
1444 mCapture->expectFGColor(10, 10);
1445 }
1446
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001447 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001448
1449 // The boundary should be twice as far from the origin now.
1450 // The pixels from the last test should all be child now
1451 {
1452 ScreenCapture::captureScreen(&mCapture);
1453 mCapture->expectChildColor(9, 9);
1454 mCapture->expectChildColor(10, 10);
1455 mCapture->expectChildColor(19, 19);
1456 mCapture->expectFGColor(20, 20);
1457 }
1458}
Robert Carr9524cb32017-02-13 11:32:32 -08001459
Robert Carr6452f122017-03-21 10:41:29 -07001460TEST_F(ChildLayerTest, ChildLayerAlpha) {
1461 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1462 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1463 fillSurfaceRGBA8(mChild, 0, 254, 0);
1464 waitForPostedBuffers();
1465
Robert Carr4cdc58f2017-08-23 14:22:20 -07001466 asTransaction([&](Transaction& t) {
1467 t.show(mChild);
1468 t.setPosition(mChild, 0, 0);
1469 t.setPosition(mFGSurfaceControl, 0, 0);
1470 });
Robert Carr6452f122017-03-21 10:41:29 -07001471
1472 {
1473 ScreenCapture::captureScreen(&mCapture);
1474 // Unblended child color
1475 mCapture->checkPixel(0, 0, 0, 254, 0);
1476 }
1477
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001478 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001479
1480 {
1481 ScreenCapture::captureScreen(&mCapture);
1482 // Child and BG blended.
1483 mCapture->checkPixel(0, 0, 127, 127, 0);
1484 }
1485
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001486 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001487
1488 {
1489 ScreenCapture::captureScreen(&mCapture);
1490 // Child and BG blended.
1491 mCapture->checkPixel(0, 0, 95, 64, 95);
1492 }
1493}
1494
Robert Carr9524cb32017-02-13 11:32:32 -08001495TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001496 asTransaction([&](Transaction& t) {
1497 t.show(mChild);
1498 t.setPosition(mChild, 10, 10);
1499 t.setPosition(mFGSurfaceControl, 64, 64);
1500 });
Robert Carr9524cb32017-02-13 11:32:32 -08001501
1502 {
1503 ScreenCapture::captureScreen(&mCapture);
1504 // Top left of foreground must now be visible
1505 mCapture->expectFGColor(64, 64);
1506 // But 10 pixels in we should see the child surface
1507 mCapture->expectChildColor(74, 74);
1508 // And 10 more pixels we should be back to the foreground surface
1509 mCapture->expectFGColor(84, 84);
1510 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001511
1512 asTransaction([&](Transaction& t) {
1513 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1514 });
1515
Robert Carr9524cb32017-02-13 11:32:32 -08001516 {
1517 ScreenCapture::captureScreen(&mCapture);
1518 mCapture->expectFGColor(64, 64);
1519 // In reparenting we should have exposed the entire foreground surface.
1520 mCapture->expectFGColor(74, 74);
1521 // And the child layer should now begin at 10, 10 (since the BG
1522 // layer is at (0, 0)).
1523 mCapture->expectBGColor(9, 9);
1524 mCapture->expectChildColor(10, 10);
1525 }
1526}
1527
chaviw161410b02017-07-27 10:46:08 -07001528TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001529 asTransaction([&](Transaction& t) {
1530 t.show(mChild);
1531 t.setPosition(mChild, 10, 10);
1532 t.setPosition(mFGSurfaceControl, 64, 64);
1533 });
Robert Carr9524cb32017-02-13 11:32:32 -08001534
1535 {
1536 ScreenCapture::captureScreen(&mCapture);
1537 // Top left of foreground must now be visible
1538 mCapture->expectFGColor(64, 64);
1539 // But 10 pixels in we should see the child surface
1540 mCapture->expectChildColor(74, 74);
1541 // And 10 more pixels we should be back to the foreground surface
1542 mCapture->expectFGColor(84, 84);
1543 }
1544
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001545 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001546
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001547 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001548
chaviw161410b02017-07-27 10:46:08 -07001549 // Since the child has the same client as the parent, it will not get
1550 // detached and will be hidden.
1551 {
1552 ScreenCapture::captureScreen(&mCapture);
1553 mCapture->expectFGColor(64, 64);
1554 mCapture->expectFGColor(74, 74);
1555 mCapture->expectFGColor(84, 84);
1556 }
1557}
1558
1559TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1560 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001561 sp<SurfaceControl> mChildNewClient =
1562 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1563 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001564
1565 ASSERT_TRUE(mChildNewClient != NULL);
1566 ASSERT_TRUE(mChildNewClient->isValid());
1567
1568 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1569
Robert Carr4cdc58f2017-08-23 14:22:20 -07001570 asTransaction([&](Transaction& t) {
1571 t.hide(mChild);
1572 t.show(mChildNewClient);
1573 t.setPosition(mChildNewClient, 10, 10);
1574 t.setPosition(mFGSurfaceControl, 64, 64);
1575 });
chaviw161410b02017-07-27 10:46:08 -07001576
1577 {
1578 ScreenCapture::captureScreen(&mCapture);
1579 // Top left of foreground must now be visible
1580 mCapture->expectFGColor(64, 64);
1581 // But 10 pixels in we should see the child surface
1582 mCapture->expectChildColor(74, 74);
1583 // And 10 more pixels we should be back to the foreground surface
1584 mCapture->expectFGColor(84, 84);
1585 }
1586
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001587 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001588
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001589 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001590
Robert Carr9524cb32017-02-13 11:32:32 -08001591 // Nothing should have changed.
1592 {
1593 ScreenCapture::captureScreen(&mCapture);
1594 mCapture->expectFGColor(64, 64);
1595 mCapture->expectChildColor(74, 74);
1596 mCapture->expectFGColor(84, 84);
1597 }
1598}
1599
Robert Carr9b429f42017-04-17 14:56:57 -07001600TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001601 asTransaction([&](Transaction& t) {
1602 t.show(mChild);
1603 t.setPosition(mChild, 0, 0);
1604 t.setPosition(mFGSurfaceControl, 0, 0);
1605 });
Robert Carr9b429f42017-04-17 14:56:57 -07001606
1607 {
1608 ScreenCapture::captureScreen(&mCapture);
1609 // We've positioned the child in the top left.
1610 mCapture->expectChildColor(0, 0);
1611 // But it's only 10x10.
1612 mCapture->expectFGColor(10, 10);
1613 }
1614
Robert Carr4cdc58f2017-08-23 14:22:20 -07001615 asTransaction([&](Transaction& t) {
1616 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1617 // We cause scaling by 2.
1618 t.setSize(mFGSurfaceControl, 128, 128);
1619 });
Robert Carr9b429f42017-04-17 14:56:57 -07001620
1621 {
1622 ScreenCapture::captureScreen(&mCapture);
1623 // We've positioned the child in the top left.
1624 mCapture->expectChildColor(0, 0);
1625 mCapture->expectChildColor(10, 10);
1626 mCapture->expectChildColor(19, 19);
1627 // And now it should be scaled all the way to 20x20
1628 mCapture->expectFGColor(20, 20);
1629 }
1630}
1631
Robert Carr1725eee2017-04-26 18:32:15 -07001632// Regression test for b/37673612
1633TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001634 asTransaction([&](Transaction& t) {
1635 t.show(mChild);
1636 t.setPosition(mChild, 0, 0);
1637 t.setPosition(mFGSurfaceControl, 0, 0);
1638 });
Robert Carr1725eee2017-04-26 18:32:15 -07001639
1640 {
1641 ScreenCapture::captureScreen(&mCapture);
1642 // We've positioned the child in the top left.
1643 mCapture->expectChildColor(0, 0);
1644 // But it's only 10x10.
1645 mCapture->expectFGColor(10, 10);
1646 }
Robert Carr1725eee2017-04-26 18:32:15 -07001647 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1648 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001649 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001650 sp<Surface> s = mFGSurfaceControl->getSurface();
1651 auto anw = static_cast<ANativeWindow*>(s.get());
1652 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1653 native_window_set_buffers_dimensions(anw, 64, 128);
1654 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1655 waitForPostedBuffers();
1656
1657 {
1658 // The child should still be in the same place and not have any strange scaling as in
1659 // b/37673612.
1660 ScreenCapture::captureScreen(&mCapture);
1661 mCapture->expectChildColor(0, 0);
1662 mCapture->expectFGColor(10, 10);
1663 }
1664}
1665
Dan Stoza412903f2017-04-27 13:42:17 -07001666TEST_F(ChildLayerTest, Bug36858924) {
1667 // Destroy the child layer
1668 mChild.clear();
1669
1670 // Now recreate it as hidden
1671 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1672 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1673 mFGSurfaceControl.get());
1674
1675 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001676 asTransaction([&](Transaction& t) {
1677 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001678 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001679 t.show(mChild);
1680 });
Dan Stoza412903f2017-04-27 13:42:17 -07001681
1682 // Render the foreground surface a few times
1683 //
1684 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1685 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1686 // never acquire/release the first buffer
1687 ALOGI("Filling 1");
1688 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1689 ALOGI("Filling 2");
1690 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1691 ALOGI("Filling 3");
1692 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1693 ALOGI("Filling 4");
1694 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1695}
1696
chaviwf1961f72017-09-18 16:41:07 -07001697TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001698 asTransaction([&](Transaction& t) {
1699 t.show(mChild);
1700 t.setPosition(mChild, 10, 10);
1701 t.setPosition(mFGSurfaceControl, 64, 64);
1702 });
chaviw06178942017-07-27 10:25:59 -07001703
1704 {
1705 ScreenCapture::captureScreen(&mCapture);
1706 // Top left of foreground must now be visible
1707 mCapture->expectFGColor(64, 64);
1708 // But 10 pixels in we should see the child surface
1709 mCapture->expectChildColor(74, 74);
1710 // And 10 more pixels we should be back to the foreground surface
1711 mCapture->expectFGColor(84, 84);
1712 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001713
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001714 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001715
chaviw06178942017-07-27 10:25:59 -07001716 {
1717 ScreenCapture::captureScreen(&mCapture);
1718 mCapture->expectFGColor(64, 64);
1719 // In reparenting we should have exposed the entire foreground surface.
1720 mCapture->expectFGColor(74, 74);
1721 // And the child layer should now begin at 10, 10 (since the BG
1722 // layer is at (0, 0)).
1723 mCapture->expectBGColor(9, 9);
1724 mCapture->expectChildColor(10, 10);
1725 }
1726}
1727
chaviwf1961f72017-09-18 16:41:07 -07001728TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001729 asTransaction([&](Transaction& t) {
1730 t.show(mChild);
1731 t.setPosition(mChild, 10, 10);
1732 t.setPosition(mFGSurfaceControl, 64, 64);
1733 });
chaviwf1961f72017-09-18 16:41:07 -07001734
1735 {
1736 ScreenCapture::captureScreen(&mCapture);
1737 // Top left of foreground must now be visible
1738 mCapture->expectFGColor(64, 64);
1739 // But 10 pixels in we should see the child surface
1740 mCapture->expectChildColor(74, 74);
1741 // And 10 more pixels we should be back to the foreground surface
1742 mCapture->expectFGColor(84, 84);
1743 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001744 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07001745 {
1746 ScreenCapture::captureScreen(&mCapture);
1747 // Nothing should have changed.
1748 mCapture->expectFGColor(64, 64);
1749 mCapture->expectChildColor(74, 74);
1750 mCapture->expectFGColor(84, 84);
1751 }
1752}
1753
1754TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001755 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
1756 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07001757 ASSERT_TRUE(newSurface != NULL);
1758 ASSERT_TRUE(newSurface->isValid());
1759
1760 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001761 asTransaction([&](Transaction& t) {
1762 t.hide(mChild);
1763 t.show(newSurface);
1764 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001765 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001766 t.setPosition(mFGSurfaceControl, 64, 64);
1767 });
chaviwf1961f72017-09-18 16:41:07 -07001768
1769 {
1770 ScreenCapture::captureScreen(&mCapture);
1771 // Top left of foreground must now be visible
1772 mCapture->expectFGColor(64, 64);
1773 // At 10, 10 we should see the new surface
1774 mCapture->checkPixel(10, 10, 63, 195, 63);
1775 }
1776
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001777 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07001778
1779 {
1780 ScreenCapture::captureScreen(&mCapture);
1781 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1782 // mFGSurface, putting it at 74, 74.
1783 mCapture->expectFGColor(64, 64);
1784 mCapture->checkPixel(74, 74, 63, 195, 63);
1785 mCapture->expectFGColor(84, 84);
1786 }
1787}
1788
chaviwc9674332017-08-28 12:32:18 -07001789TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001790 sp<SurfaceControl> grandchild =
1791 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
1792 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07001793 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1794
1795 {
1796 ScreenCapture::captureScreen(&mCapture);
1797 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1798 // which begins at 64, 64
1799 mCapture->checkPixel(64, 64, 50, 50, 50);
1800 }
1801}
1802
Robert Carr503c7042017-09-27 15:06:08 -07001803TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001804 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
1805 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07001806 fillSurfaceRGBA8(relative, 255, 255, 255);
1807
1808 Transaction t;
1809 t.setLayer(relative, INT32_MAX)
1810 .setRelativeLayer(mChild, relative->getHandle(), 1)
1811 .setPosition(mFGSurfaceControl, 0, 0)
1812 .apply(true);
1813
1814 // We expect that the child should have been elevated above our
1815 // INT_MAX layer even though it's not a child of it.
1816 {
1817 ScreenCapture::captureScreen(&mCapture);
1818 mCapture->expectChildColor(0, 0);
1819 mCapture->expectChildColor(9, 9);
1820 mCapture->checkPixel(10, 10, 255, 255, 255);
1821 }
1822}
1823
chaviw13fdc492017-06-27 12:40:18 -07001824class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001825protected:
chaviw13fdc492017-06-27 12:40:18 -07001826 void SetUp() override {
1827 LayerUpdateTest::SetUp();
1828
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001829 mLayerColorControl =
1830 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
1831 PIXEL_FORMAT_RGBA_8888,
1832 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07001833
1834 ASSERT_TRUE(mLayerColorControl != NULL);
1835 ASSERT_TRUE(mLayerColorControl->isValid());
1836
Robert Carr4cdc58f2017-08-23 14:22:20 -07001837 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001838 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001839 t.setPosition(mLayerColorControl, 140, 140);
1840 t.hide(mLayerColorControl);
1841 t.hide(mFGSurfaceControl);
1842 });
chaviw13fdc492017-06-27 12:40:18 -07001843 }
1844
1845 void TearDown() override {
1846 LayerUpdateTest::TearDown();
1847 mLayerColorControl = 0;
1848 }
1849
1850 sp<SurfaceControl> mLayerColorControl;
1851};
1852
1853TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1854 sp<ScreenCapture> sc;
1855
1856 {
1857 SCOPED_TRACE("before setColor");
1858 ScreenCapture::captureScreen(&sc);
1859 sc->expectBGColor(145, 145);
1860 }
1861
Robert Carr4cdc58f2017-08-23 14:22:20 -07001862 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001863 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001864 t.setColor(mLayerColorControl, color);
1865 t.show(mLayerColorControl);
1866 });
chaviw13fdc492017-06-27 12:40:18 -07001867
chaviw13fdc492017-06-27 12:40:18 -07001868 {
1869 // There should now be a color
1870 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001871
chaviw13fdc492017-06-27 12:40:18 -07001872 ScreenCapture::captureScreen(&sc);
1873 sc->checkPixel(145, 145, 43, 207, 131);
1874 }
1875}
1876
1877TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1878 sp<ScreenCapture> sc;
1879 {
1880 SCOPED_TRACE("before setColor");
1881 ScreenCapture::captureScreen(&sc);
1882 sc->expectBGColor(145, 145);
1883 }
1884
Robert Carr4cdc58f2017-08-23 14:22:20 -07001885 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001886 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001887 t.setColor(mLayerColorControl, color);
1888 t.setAlpha(mLayerColorControl, .75f);
1889 t.show(mLayerColorControl);
1890 });
1891
chaviw13fdc492017-06-27 12:40:18 -07001892 {
1893 // There should now be a color with .75 alpha
1894 SCOPED_TRACE("after setColor");
1895 ScreenCapture::captureScreen(&sc);
1896 sc->checkPixel(145, 145, 48, 171, 147);
1897 }
1898}
1899
1900TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1901 sp<ScreenCapture> sc;
1902 {
1903 SCOPED_TRACE("before setColor");
1904 ScreenCapture::captureScreen(&sc);
1905 sc->expectBGColor(145, 145);
1906 }
1907
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001908 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001909
chaviw13fdc492017-06-27 12:40:18 -07001910 {
1911 // There should now be set to 0,0,0 (black) as default.
1912 SCOPED_TRACE("after setColor");
1913 ScreenCapture::captureScreen(&sc);
1914 sc->checkPixel(145, 145, 0, 0, 0);
1915 }
1916}
1917
chaviwa76b2712017-09-20 12:02:26 -07001918class ScreenCaptureTest : public LayerUpdateTest {
1919protected:
1920 std::unique_ptr<CaptureLayer> mCapture;
1921};
1922
1923TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
1924 auto bgHandle = mBGSurfaceControl->getHandle();
1925 CaptureLayer::captureScreen(&mCapture, bgHandle);
1926 mCapture->expectBGColor(0, 0);
1927 // Doesn't capture FG layer which is at 64, 64
1928 mCapture->expectBGColor(64, 64);
1929}
1930
1931TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
1932 auto fgHandle = mFGSurfaceControl->getHandle();
1933
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001934 sp<SurfaceControl> child =
1935 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1936 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001937 fillSurfaceRGBA8(child, 200, 200, 200);
1938
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001939 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001940
1941 // Captures mFGSurfaceControl layer and its child.
1942 CaptureLayer::captureScreen(&mCapture, fgHandle);
1943 mCapture->expectFGColor(10, 10);
1944 mCapture->expectChildColor(0, 0);
1945}
1946
1947TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
1948 auto fgHandle = mFGSurfaceControl->getHandle();
1949
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001950 sp<SurfaceControl> child =
1951 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1952 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001953 fillSurfaceRGBA8(child, 200, 200, 200);
1954
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001955 sp<SurfaceControl> grandchild =
1956 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
1957 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07001958
1959 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1960 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001961 .show(child)
1962 .setPosition(grandchild, 5, 5)
1963 .show(grandchild)
1964 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001965
1966 // Captures mFGSurfaceControl, its child, and the grandchild.
1967 CaptureLayer::captureScreen(&mCapture, fgHandle);
1968 mCapture->expectFGColor(10, 10);
1969 mCapture->expectChildColor(0, 0);
1970 mCapture->checkPixel(5, 5, 50, 50, 50);
1971}
1972
1973TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001974 sp<SurfaceControl> child =
1975 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1976 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001977 fillSurfaceRGBA8(child, 200, 200, 200);
1978 auto childHandle = child->getHandle();
1979
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001980 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001981
1982 // Captures only the child layer, and not the parent.
1983 CaptureLayer::captureScreen(&mCapture, childHandle);
1984 mCapture->expectChildColor(0, 0);
1985 mCapture->expectChildColor(9, 9);
1986}
1987
1988TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001989 sp<SurfaceControl> child =
1990 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1991 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001992 fillSurfaceRGBA8(child, 200, 200, 200);
1993 auto childHandle = child->getHandle();
1994
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001995 sp<SurfaceControl> grandchild =
1996 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
1997 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07001998 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1999
2000 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002001 .show(child)
2002 .setPosition(grandchild, 5, 5)
2003 .show(grandchild)
2004 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002005
2006 auto grandchildHandle = grandchild->getHandle();
2007
2008 // Captures only the grandchild.
2009 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2010 mCapture->checkPixel(0, 0, 50, 50, 50);
2011 mCapture->checkPixel(4, 4, 50, 50, 50);
2012}
2013
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002014} // namespace android