blob: 4762ce258ef640e346a48b9b14390e8f61b8b32f [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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700547class LayerUpdateTest : public ::testing::Test {
548protected:
549 virtual void SetUp() {
550 mComposerClient = new SurfaceComposerClient;
551 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
552
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700553 sp<IBinder> display(
554 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700555 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700556 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700557
558 ssize_t displayWidth = info.w;
559 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700560
561 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700562 mBGSurfaceControl =
563 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
564 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700565 ASSERT_TRUE(mBGSurfaceControl != NULL);
566 ASSERT_TRUE(mBGSurfaceControl->isValid());
567 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
568
569 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700570 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
571 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700572 ASSERT_TRUE(mFGSurfaceControl != NULL);
573 ASSERT_TRUE(mFGSurfaceControl->isValid());
574
575 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
576
577 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700578 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
579 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700580 ASSERT_TRUE(mSyncSurfaceControl != NULL);
581 ASSERT_TRUE(mSyncSurfaceControl->isValid());
582
583 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
584
Robert Carr4cdc58f2017-08-23 14:22:20 -0700585 asTransaction([&](Transaction& t) {
586 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700587
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700588 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700589
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700590 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
591 .setPosition(mFGSurfaceControl, 64, 64)
592 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700593
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700594 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
595 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
596 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700597 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700598 }
599
600 virtual void TearDown() {
601 mComposerClient->dispose();
602 mBGSurfaceControl = 0;
603 mFGSurfaceControl = 0;
604 mSyncSurfaceControl = 0;
605 mComposerClient = 0;
606 }
607
608 void waitForPostedBuffers() {
609 // Since the sync surface is in synchronous mode (i.e. double buffered)
610 // posting three buffers to it should ensure that at least two
611 // SurfaceFlinger::handlePageFlip calls have been made, which should
612 // guaranteed that a buffer posted to another Surface has been retired.
613 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
614 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
615 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
616 }
617
Robert Carr4cdc58f2017-08-23 14:22:20 -0700618 void asTransaction(const std::function<void(Transaction&)>& exec) {
619 Transaction t;
620 exec(t);
621 t.apply(true);
622 }
623
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700624 sp<SurfaceComposerClient> mComposerClient;
625 sp<SurfaceControl> mBGSurfaceControl;
626 sp<SurfaceControl> mFGSurfaceControl;
627
628 // This surface is used to ensure that the buffers posted to
629 // mFGSurfaceControl have been picked up by SurfaceFlinger.
630 sp<SurfaceControl> mSyncSurfaceControl;
631};
632
Robert Carr7f619b22017-11-06 12:56:35 -0800633TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
634 sp<ScreenCapture> sc;
635
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700636 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
637 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -0800638 fillSurfaceRGBA8(relative, 10, 10, 10);
639 waitForPostedBuffers();
640
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700641 Transaction{}
642 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -0800643 .setPosition(relative, 64, 64)
644 .apply();
645
646 {
647 // The relative should be on top of the FG control.
648 ScreenCapture::captureScreen(&sc);
649 sc->checkPixel(64, 64, 10, 10, 10);
650 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700651 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800652
653 {
654 // Nothing should change at this point.
655 ScreenCapture::captureScreen(&sc);
656 sc->checkPixel(64, 64, 10, 10, 10);
657 }
658
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700659 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800660
661 {
662 // Ensure that the relative was actually hidden, rather than
663 // being left in the detached but visible state.
664 ScreenCapture::captureScreen(&sc);
665 sc->expectFGColor(64, 64);
666 }
667}
668
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700669TEST_F(LayerUpdateTest, LayerMoveWorks) {
670 sp<ScreenCapture> sc;
671 {
672 SCOPED_TRACE("before move");
673 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800674 sc->expectBGColor(0, 12);
675 sc->expectFGColor(75, 75);
676 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700677 }
678
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700679 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700680
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700681 {
682 // This should reflect the new position, but not the new color.
683 SCOPED_TRACE("after move, before redraw");
684 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800685 sc->expectBGColor(24, 24);
686 sc->expectBGColor(75, 75);
687 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700688 }
689
690 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
691 waitForPostedBuffers();
692 {
693 // This should reflect the new position and the new color.
694 SCOPED_TRACE("after redraw");
695 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800696 sc->expectBGColor(24, 24);
697 sc->expectBGColor(75, 75);
698 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700699 }
700}
701
702TEST_F(LayerUpdateTest, LayerResizeWorks) {
703 sp<ScreenCapture> sc;
704 {
705 SCOPED_TRACE("before resize");
706 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800707 sc->expectBGColor(0, 12);
708 sc->expectFGColor(75, 75);
709 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700710 }
711
Steve Block9d453682011-12-20 16:23:08 +0000712 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700713 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +0000714 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700715 {
716 // This should not reflect the new size or color because SurfaceFlinger
717 // has not yet received a buffer of the correct size.
718 SCOPED_TRACE("after resize, before redraw");
719 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800720 sc->expectBGColor(0, 12);
721 sc->expectFGColor(75, 75);
722 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700723 }
724
Steve Block9d453682011-12-20 16:23:08 +0000725 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700726 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
727 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000728 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700729 {
730 // This should reflect the new size and the new color.
731 SCOPED_TRACE("after redraw");
732 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800733 sc->expectBGColor(24, 24);
734 sc->checkPixel(75, 75, 63, 195, 63);
735 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700736 }
737}
738
Haixia Shid5750962015-07-27 16:50:49 -0700739TEST_F(LayerUpdateTest, LayerCropWorks) {
740 sp<ScreenCapture> sc;
741 {
742 SCOPED_TRACE("before crop");
743 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800744 sc->expectBGColor(24, 24);
745 sc->expectFGColor(75, 75);
746 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700747 }
748
Robert Carr4cdc58f2017-08-23 14:22:20 -0700749 asTransaction([&](Transaction& t) {
750 Rect cropRect(16, 16, 32, 32);
751 t.setCrop(mFGSurfaceControl, cropRect);
752 });
Haixia Shid5750962015-07-27 16:50:49 -0700753 {
754 // This should crop the foreground surface.
755 SCOPED_TRACE("after crop");
756 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800757 sc->expectBGColor(24, 24);
758 sc->expectBGColor(75, 75);
759 sc->expectFGColor(95, 80);
760 sc->expectFGColor(80, 95);
761 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700762 }
763}
764
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000765TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
766 sp<ScreenCapture> sc;
767 {
768 SCOPED_TRACE("before crop");
769 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800770 sc->expectBGColor(24, 24);
771 sc->expectFGColor(75, 75);
772 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000773 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700774 asTransaction([&](Transaction& t) {
775 Rect cropRect(16, 16, 32, 32);
776 t.setFinalCrop(mFGSurfaceControl, cropRect);
777 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000778 {
779 // This should crop the foreground surface.
780 SCOPED_TRACE("after crop");
781 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800782 sc->expectBGColor(24, 24);
783 sc->expectBGColor(75, 75);
784 sc->expectBGColor(95, 80);
785 sc->expectBGColor(80, 95);
786 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000787 }
788}
789
Haixia Shid5750962015-07-27 16:50:49 -0700790TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
791 sp<ScreenCapture> sc;
792 {
793 SCOPED_TRACE("before setLayer");
794 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800795 sc->expectBGColor(24, 24);
796 sc->expectFGColor(75, 75);
797 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700798 }
799
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700800 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700801
Haixia Shid5750962015-07-27 16:50:49 -0700802 {
803 // This should hide the foreground surface beneath the background.
804 SCOPED_TRACE("after setLayer");
805 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800806 sc->expectBGColor(24, 24);
807 sc->expectBGColor(75, 75);
808 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700809 }
810}
811
812TEST_F(LayerUpdateTest, LayerShowHideWorks) {
813 sp<ScreenCapture> sc;
814 {
815 SCOPED_TRACE("before hide");
816 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800817 sc->expectBGColor(24, 24);
818 sc->expectFGColor(75, 75);
819 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700820 }
821
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700822 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700823
Haixia Shid5750962015-07-27 16:50:49 -0700824 {
825 // This should hide the foreground surface.
826 SCOPED_TRACE("after hide, before show");
827 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800828 sc->expectBGColor(24, 24);
829 sc->expectBGColor(75, 75);
830 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700831 }
832
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700833 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700834
Haixia Shid5750962015-07-27 16:50:49 -0700835 {
836 // This should show the foreground surface.
837 SCOPED_TRACE("after show");
838 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800839 sc->expectBGColor(24, 24);
840 sc->expectFGColor(75, 75);
841 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700842 }
843}
844
845TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
846 sp<ScreenCapture> sc;
847 {
848 SCOPED_TRACE("before setAlpha");
849 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800850 sc->expectBGColor(24, 24);
851 sc->expectFGColor(75, 75);
852 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700853 }
854
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700855 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700856
Haixia Shid5750962015-07-27 16:50:49 -0700857 {
858 // This should set foreground to be 75% opaque.
859 SCOPED_TRACE("after setAlpha");
860 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800861 sc->expectBGColor(24, 24);
862 sc->checkPixel(75, 75, 162, 63, 96);
863 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700864 }
865}
866
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700867TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
868 sp<ScreenCapture> sc;
869 {
870 SCOPED_TRACE("before setLayerStack");
871 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800872 sc->expectBGColor(24, 24);
873 sc->expectFGColor(75, 75);
874 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700875 }
876
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700877 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700878 {
879 // This should hide the foreground surface since it goes to a different
880 // layer stack.
881 SCOPED_TRACE("after setLayerStack");
882 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800883 sc->expectBGColor(24, 24);
884 sc->expectBGColor(75, 75);
885 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700886 }
887}
888
889TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
890 sp<ScreenCapture> sc;
891 {
892 SCOPED_TRACE("before setFlags");
893 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800894 sc->expectBGColor(24, 24);
895 sc->expectFGColor(75, 75);
896 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700897 }
898
Robert Carr4cdc58f2017-08-23 14:22:20 -0700899 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700900 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700901 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700902 {
903 // This should hide the foreground surface
904 SCOPED_TRACE("after setFlags");
905 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800906 sc->expectBGColor(24, 24);
907 sc->expectBGColor(75, 75);
908 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700909 }
910}
911
912TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
913 sp<ScreenCapture> sc;
914 {
915 SCOPED_TRACE("before setMatrix");
916 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800917 sc->expectBGColor(24, 24);
918 sc->expectFGColor(91, 96);
919 sc->expectFGColor(96, 101);
920 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700921 }
922
Robert Carr4cdc58f2017-08-23 14:22:20 -0700923 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700924 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700925 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700926 {
927 SCOPED_TRACE("after setMatrix");
928 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800929 sc->expectBGColor(24, 24);
930 sc->expectFGColor(91, 96);
931 sc->expectBGColor(96, 91);
932 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700933 }
934}
935
Robert Carr8d5227b2017-03-16 15:41:03 -0700936class GeometryLatchingTest : public LayerUpdateTest {
937protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700938 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -0700939 SCOPED_TRACE(trace);
940 ScreenCapture::captureScreen(&sc);
941 // We find the leading edge of the FG surface.
942 sc->expectFGColor(127, 127);
943 sc->expectBGColor(128, 128);
944 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700945
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700946 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -0700947
948 void unlockFGBuffer() {
949 sp<Surface> s = mFGSurfaceControl->getSurface();
950 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
951 waitForPostedBuffers();
952 }
953
Robert Carr8d5227b2017-03-16 15:41:03 -0700954 void completeFGResize() {
955 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
956 waitForPostedBuffers();
957 }
958 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700959 asTransaction([&](Transaction& t) {
960 t.setSize(mFGSurfaceControl, 64, 64);
961 t.setPosition(mFGSurfaceControl, 64, 64);
962 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
963 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
964 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700965
966 EXPECT_INITIAL_STATE("After restoring initial state");
967 }
968 sp<ScreenCapture> sc;
969};
970
971TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
972 EXPECT_INITIAL_STATE("before anything");
973
974 // By default position can be updated even while
975 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700976 asTransaction([&](Transaction& t) {
977 t.setSize(mFGSurfaceControl, 32, 32);
978 t.setPosition(mFGSurfaceControl, 100, 100);
979 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700980
981 {
982 SCOPED_TRACE("After moving surface");
983 ScreenCapture::captureScreen(&sc);
984 // If we moved, the FG Surface should cover up what was previously BG
985 // however if we didn't move the FG wouldn't be large enough now.
986 sc->expectFGColor(163, 163);
987 }
988
989 restoreInitialState();
990
991 // Now we repeat with setGeometryAppliesWithResize
992 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -0700993 asTransaction([&](Transaction& t) {
994 t.setGeometryAppliesWithResize(mFGSurfaceControl);
995 t.setSize(mFGSurfaceControl, 32, 32);
996 t.setPosition(mFGSurfaceControl, 100, 100);
997 });
Robert Carr8d5227b2017-03-16 15:41:03 -0700998
999 {
1000 SCOPED_TRACE("While resize is pending");
1001 ScreenCapture::captureScreen(&sc);
1002 // This time we shouldn't have moved, so the BG color
1003 // should still be visible.
1004 sc->expectBGColor(128, 128);
1005 }
1006
1007 completeFGResize();
1008
1009 {
1010 SCOPED_TRACE("After the resize");
1011 ScreenCapture::captureScreen(&sc);
1012 // But after the resize completes, we should move
1013 // and the FG should be visible here.
1014 sc->expectFGColor(128, 128);
1015 }
1016}
1017
1018class CropLatchingTest : public GeometryLatchingTest {
1019protected:
1020 void EXPECT_CROPPED_STATE(const char* trace) {
1021 SCOPED_TRACE(trace);
1022 ScreenCapture::captureScreen(&sc);
1023 // The edge should be moved back one pixel by our crop.
1024 sc->expectFGColor(126, 126);
1025 sc->expectBGColor(127, 127);
1026 sc->expectBGColor(128, 128);
1027 }
chaviw59f5c562017-06-28 16:39:06 -07001028
1029 void EXPECT_RESIZE_STATE(const char* trace) {
1030 SCOPED_TRACE(trace);
1031 ScreenCapture::captureScreen(&sc);
1032 // The FG is now resized too 128,128 at 64,64
1033 sc->expectFGColor(64, 64);
1034 sc->expectFGColor(191, 191);
1035 sc->expectBGColor(192, 192);
1036 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001037};
1038
1039TEST_F(CropLatchingTest, CropLatching) {
1040 EXPECT_INITIAL_STATE("before anything");
1041 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001042 asTransaction([&](Transaction& t) {
1043 t.setSize(mFGSurfaceControl, 128, 128);
1044 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1045 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001046
1047 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1048
1049 restoreInitialState();
1050
Robert Carr4cdc58f2017-08-23 14:22:20 -07001051 asTransaction([&](Transaction& t) {
1052 t.setSize(mFGSurfaceControl, 128, 128);
1053 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1054 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1055 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001056
1057 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1058
1059 completeFGResize();
1060
1061 EXPECT_CROPPED_STATE("after the resize finishes");
1062}
1063
1064TEST_F(CropLatchingTest, FinalCropLatching) {
1065 EXPECT_INITIAL_STATE("before anything");
1066 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001067 asTransaction([&](Transaction& t) {
1068 t.setSize(mFGSurfaceControl, 128, 128);
1069 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1070 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001071
1072 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1073
1074 restoreInitialState();
1075
Robert Carr4cdc58f2017-08-23 14:22:20 -07001076 asTransaction([&](Transaction& t) {
1077 t.setSize(mFGSurfaceControl, 128, 128);
1078 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1079 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1080 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001081
1082 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1083
1084 completeFGResize();
1085
1086 EXPECT_CROPPED_STATE("after the resize finishes");
1087}
1088
Robert Carr7bf247e2017-05-18 14:02:49 -07001089// In this test we ensure that setGeometryAppliesWithResize actually demands
1090// a buffer of the new size, and not just any size.
1091TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1092 EXPECT_INITIAL_STATE("before anything");
1093 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001094 asTransaction([&](Transaction& t) {
1095 t.setSize(mFGSurfaceControl, 128, 128);
1096 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1097 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001098
1099 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1100
1101 restoreInitialState();
1102
1103 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1104 // initiating the resize.
1105 lockAndFillFGBuffer();
1106
Robert Carr4cdc58f2017-08-23 14:22:20 -07001107 asTransaction([&](Transaction& t) {
1108 t.setSize(mFGSurfaceControl, 128, 128);
1109 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1110 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1111 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001112
1113 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1114
1115 // We now submit our old buffer, at the old size, and ensure it doesn't
1116 // trigger geometry latching.
1117 unlockFGBuffer();
1118
1119 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1120
1121 completeFGResize();
1122
1123 EXPECT_CROPPED_STATE("after the resize finishes");
1124}
1125
1126TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1127 EXPECT_INITIAL_STATE("before anything");
1128 // In this scenario, we attempt to set the final crop a second time while the resize
1129 // is still pending, and ensure we are successful. Success meaning the second crop
1130 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001131 asTransaction([&](Transaction& t) {
1132 t.setSize(mFGSurfaceControl, 128, 128);
1133 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1134 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1135 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001136
chaviw59f5c562017-06-28 16:39:06 -07001137 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1138
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001139 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001140
chaviw59f5c562017-06-28 16:39:06 -07001141 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001142
1143 completeFGResize();
1144
chaviw59f5c562017-06-28 16:39:06 -07001145 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001146}
1147
Pablo Ceballos05289c22016-04-14 15:49:55 -07001148TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1149 sp<ScreenCapture> sc;
1150 {
1151 SCOPED_TRACE("before anything");
1152 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001153 sc->expectBGColor(32, 32);
1154 sc->expectFGColor(96, 96);
1155 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001156 }
1157
1158 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001159 asTransaction([&](Transaction& t) {
1160 t.setAlpha(mFGSurfaceControl, 0.75);
1161 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001162 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001163 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001164
Robert Carr4cdc58f2017-08-23 14:22:20 -07001165 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001166 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001167 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001168 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001169 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001170
1171 {
1172 SCOPED_TRACE("before any trigger");
1173 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001174 sc->expectBGColor(32, 32);
1175 sc->expectFGColor(96, 96);
1176 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001177 }
1178
1179 // should trigger the first deferred transaction, but not the second one
1180 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1181 {
1182 SCOPED_TRACE("after first trigger");
1183 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001184 sc->expectBGColor(32, 32);
1185 sc->checkPixel(96, 96, 162, 63, 96);
1186 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001187 }
1188
1189 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001190 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001191
1192 // trigger the second deferred transaction
1193 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1194 {
1195 SCOPED_TRACE("after second trigger");
1196 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001197 sc->expectBGColor(32, 32);
1198 sc->expectBGColor(96, 96);
1199 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001200 }
1201}
1202
Robert Carrdb66e622017-04-10 16:55:57 -07001203TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1204 sp<ScreenCapture> sc;
1205 {
1206 SCOPED_TRACE("before adding relative surface");
1207 ScreenCapture::captureScreen(&sc);
1208 sc->expectBGColor(24, 24);
1209 sc->expectFGColor(75, 75);
1210 sc->expectBGColor(145, 145);
1211 }
1212
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001213 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1214 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001215 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1216 waitForPostedBuffers();
1217
1218 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001219 asTransaction([&](Transaction& t) {
1220 t.setPosition(relativeSurfaceControl, 64, 64);
1221 t.show(relativeSurfaceControl);
1222 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1223 });
Robert Carrdb66e622017-04-10 16:55:57 -07001224
1225 {
1226 SCOPED_TRACE("after adding relative surface");
1227 ScreenCapture::captureScreen(&sc);
1228 // our relative surface should be visible now.
1229 sc->checkPixel(75, 75, 255, 177, 177);
1230 }
1231
1232 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001233 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001234
1235 {
1236 SCOPED_TRACE("after set layer");
1237 ScreenCapture::captureScreen(&sc);
1238 // now the FG surface should be visible again.
1239 sc->expectFGColor(75, 75);
1240 }
1241}
1242
Robert Carre392b552017-09-19 12:16:05 -07001243TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1244 sp<ScreenCapture> sc;
1245
1246 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001247 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1248 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1249 sp<SurfaceControl> childBuffer =
1250 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1251 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001252 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1253
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001254 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001255
1256 {
1257 ScreenCapture::captureScreen(&sc);
1258 sc->expectChildColor(73, 73);
1259 sc->expectFGColor(74, 74);
1260 }
1261
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001262 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001263
1264 {
1265 ScreenCapture::captureScreen(&sc);
1266 sc->expectChildColor(73, 73);
1267 sc->expectChildColor(74, 74);
1268 }
1269}
1270
Robert Carr2c5f6d22017-09-26 12:30:35 -07001271TEST_F(LayerUpdateTest, MergingTransactions) {
1272 sp<ScreenCapture> sc;
1273 {
1274 SCOPED_TRACE("before move");
1275 ScreenCapture::captureScreen(&sc);
1276 sc->expectBGColor(0, 12);
1277 sc->expectFGColor(75, 75);
1278 sc->expectBGColor(145, 145);
1279 }
1280
1281 Transaction t1, t2;
1282 t1.setPosition(mFGSurfaceControl, 128, 128);
1283 t2.setPosition(mFGSurfaceControl, 0, 0);
1284 // We expect that the position update from t2 now
1285 // overwrites the position update from t1.
1286 t1.merge(std::move(t2));
1287 t1.apply();
1288
1289 {
1290 ScreenCapture::captureScreen(&sc);
1291 sc->expectFGColor(1, 1);
1292 }
1293}
1294
Robert Carr1f0a16a2016-10-24 16:27:39 -07001295class ChildLayerTest : public LayerUpdateTest {
1296protected:
1297 void SetUp() override {
1298 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001299 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1300 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001301 fillSurfaceRGBA8(mChild, 200, 200, 200);
1302
1303 {
1304 SCOPED_TRACE("before anything");
1305 ScreenCapture::captureScreen(&mCapture);
1306 mCapture->expectChildColor(64, 64);
1307 }
1308 }
1309 void TearDown() override {
1310 LayerUpdateTest::TearDown();
1311 mChild = 0;
1312 }
1313
1314 sp<SurfaceControl> mChild;
1315 sp<ScreenCapture> mCapture;
1316};
1317
1318TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001319 asTransaction([&](Transaction& t) {
1320 t.show(mChild);
1321 t.setPosition(mChild, 10, 10);
1322 t.setPosition(mFGSurfaceControl, 64, 64);
1323 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001324
1325 {
1326 ScreenCapture::captureScreen(&mCapture);
1327 // Top left of foreground must now be visible
1328 mCapture->expectFGColor(64, 64);
1329 // But 10 pixels in we should see the child surface
1330 mCapture->expectChildColor(74, 74);
1331 // And 10 more pixels we should be back to the foreground surface
1332 mCapture->expectFGColor(84, 84);
1333 }
1334
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001335 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001336
1337 {
1338 ScreenCapture::captureScreen(&mCapture);
1339 // Top left of foreground should now be at 0, 0
1340 mCapture->expectFGColor(0, 0);
1341 // But 10 pixels in we should see the child surface
1342 mCapture->expectChildColor(10, 10);
1343 // And 10 more pixels we should be back to the foreground surface
1344 mCapture->expectFGColor(20, 20);
1345 }
1346}
1347
Robert Carr41b08b52017-06-01 16:11:34 -07001348TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001349 asTransaction([&](Transaction& t) {
1350 t.show(mChild);
1351 t.setPosition(mChild, 0, 0);
1352 t.setPosition(mFGSurfaceControl, 0, 0);
1353 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1354 });
Robert Carr41b08b52017-06-01 16:11:34 -07001355
1356 {
1357 ScreenCapture::captureScreen(&mCapture);
1358 mCapture->expectChildColor(0, 0);
1359 mCapture->expectChildColor(4, 4);
1360 mCapture->expectBGColor(5, 5);
1361 }
1362}
1363
1364TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001365 asTransaction([&](Transaction& t) {
1366 t.show(mChild);
1367 t.setPosition(mChild, 0, 0);
1368 t.setPosition(mFGSurfaceControl, 0, 0);
1369 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1370 });
Robert Carr41b08b52017-06-01 16:11:34 -07001371
1372 {
1373 ScreenCapture::captureScreen(&mCapture);
1374 mCapture->expectChildColor(0, 0);
1375 mCapture->expectChildColor(4, 4);
1376 mCapture->expectBGColor(5, 5);
1377 }
1378}
1379
Robert Carr1f0a16a2016-10-24 16:27:39 -07001380TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001381 asTransaction([&](Transaction& t) {
1382 t.show(mChild);
1383 t.setPosition(mFGSurfaceControl, 0, 0);
1384 t.setPosition(mChild, 63, 63);
1385 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001386
1387 {
1388 ScreenCapture::captureScreen(&mCapture);
1389 mCapture->expectFGColor(0, 0);
1390 // Last pixel in foreground should now be the child.
1391 mCapture->expectChildColor(63, 63);
1392 // But the child should be constrained and the next pixel
1393 // must be the background
1394 mCapture->expectBGColor(64, 64);
1395 }
1396}
1397
1398TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001399 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001400
1401 // Find the boundary between the parent and child
1402 {
1403 ScreenCapture::captureScreen(&mCapture);
1404 mCapture->expectChildColor(9, 9);
1405 mCapture->expectFGColor(10, 10);
1406 }
1407
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001408 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001409
1410 // The boundary should be twice as far from the origin now.
1411 // The pixels from the last test should all be child now
1412 {
1413 ScreenCapture::captureScreen(&mCapture);
1414 mCapture->expectChildColor(9, 9);
1415 mCapture->expectChildColor(10, 10);
1416 mCapture->expectChildColor(19, 19);
1417 mCapture->expectFGColor(20, 20);
1418 }
1419}
Robert Carr9524cb32017-02-13 11:32:32 -08001420
Robert Carr6452f122017-03-21 10:41:29 -07001421TEST_F(ChildLayerTest, ChildLayerAlpha) {
1422 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1423 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1424 fillSurfaceRGBA8(mChild, 0, 254, 0);
1425 waitForPostedBuffers();
1426
Robert Carr4cdc58f2017-08-23 14:22:20 -07001427 asTransaction([&](Transaction& t) {
1428 t.show(mChild);
1429 t.setPosition(mChild, 0, 0);
1430 t.setPosition(mFGSurfaceControl, 0, 0);
1431 });
Robert Carr6452f122017-03-21 10:41:29 -07001432
1433 {
1434 ScreenCapture::captureScreen(&mCapture);
1435 // Unblended child color
1436 mCapture->checkPixel(0, 0, 0, 254, 0);
1437 }
1438
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001439 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001440
1441 {
1442 ScreenCapture::captureScreen(&mCapture);
1443 // Child and BG blended.
1444 mCapture->checkPixel(0, 0, 127, 127, 0);
1445 }
1446
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001447 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001448
1449 {
1450 ScreenCapture::captureScreen(&mCapture);
1451 // Child and BG blended.
1452 mCapture->checkPixel(0, 0, 95, 64, 95);
1453 }
1454}
1455
Robert Carr9524cb32017-02-13 11:32:32 -08001456TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001457 asTransaction([&](Transaction& t) {
1458 t.show(mChild);
1459 t.setPosition(mChild, 10, 10);
1460 t.setPosition(mFGSurfaceControl, 64, 64);
1461 });
Robert Carr9524cb32017-02-13 11:32:32 -08001462
1463 {
1464 ScreenCapture::captureScreen(&mCapture);
1465 // Top left of foreground must now be visible
1466 mCapture->expectFGColor(64, 64);
1467 // But 10 pixels in we should see the child surface
1468 mCapture->expectChildColor(74, 74);
1469 // And 10 more pixels we should be back to the foreground surface
1470 mCapture->expectFGColor(84, 84);
1471 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001472
1473 asTransaction([&](Transaction& t) {
1474 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1475 });
1476
Robert Carr9524cb32017-02-13 11:32:32 -08001477 {
1478 ScreenCapture::captureScreen(&mCapture);
1479 mCapture->expectFGColor(64, 64);
1480 // In reparenting we should have exposed the entire foreground surface.
1481 mCapture->expectFGColor(74, 74);
1482 // And the child layer should now begin at 10, 10 (since the BG
1483 // layer is at (0, 0)).
1484 mCapture->expectBGColor(9, 9);
1485 mCapture->expectChildColor(10, 10);
1486 }
1487}
1488
chaviw161410b02017-07-27 10:46:08 -07001489TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001490 asTransaction([&](Transaction& t) {
1491 t.show(mChild);
1492 t.setPosition(mChild, 10, 10);
1493 t.setPosition(mFGSurfaceControl, 64, 64);
1494 });
Robert Carr9524cb32017-02-13 11:32:32 -08001495
1496 {
1497 ScreenCapture::captureScreen(&mCapture);
1498 // Top left of foreground must now be visible
1499 mCapture->expectFGColor(64, 64);
1500 // But 10 pixels in we should see the child surface
1501 mCapture->expectChildColor(74, 74);
1502 // And 10 more pixels we should be back to the foreground surface
1503 mCapture->expectFGColor(84, 84);
1504 }
1505
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001506 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001507
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001508 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001509
chaviw161410b02017-07-27 10:46:08 -07001510 // Since the child has the same client as the parent, it will not get
1511 // detached and will be hidden.
1512 {
1513 ScreenCapture::captureScreen(&mCapture);
1514 mCapture->expectFGColor(64, 64);
1515 mCapture->expectFGColor(74, 74);
1516 mCapture->expectFGColor(84, 84);
1517 }
1518}
1519
1520TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1521 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001522 sp<SurfaceControl> mChildNewClient =
1523 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1524 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001525
1526 ASSERT_TRUE(mChildNewClient != NULL);
1527 ASSERT_TRUE(mChildNewClient->isValid());
1528
1529 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1530
Robert Carr4cdc58f2017-08-23 14:22:20 -07001531 asTransaction([&](Transaction& t) {
1532 t.hide(mChild);
1533 t.show(mChildNewClient);
1534 t.setPosition(mChildNewClient, 10, 10);
1535 t.setPosition(mFGSurfaceControl, 64, 64);
1536 });
chaviw161410b02017-07-27 10:46:08 -07001537
1538 {
1539 ScreenCapture::captureScreen(&mCapture);
1540 // Top left of foreground must now be visible
1541 mCapture->expectFGColor(64, 64);
1542 // But 10 pixels in we should see the child surface
1543 mCapture->expectChildColor(74, 74);
1544 // And 10 more pixels we should be back to the foreground surface
1545 mCapture->expectFGColor(84, 84);
1546 }
1547
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001548 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001549
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001550 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001551
Robert Carr9524cb32017-02-13 11:32:32 -08001552 // Nothing should have changed.
1553 {
1554 ScreenCapture::captureScreen(&mCapture);
1555 mCapture->expectFGColor(64, 64);
1556 mCapture->expectChildColor(74, 74);
1557 mCapture->expectFGColor(84, 84);
1558 }
1559}
1560
Robert Carr9b429f42017-04-17 14:56:57 -07001561TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001562 asTransaction([&](Transaction& t) {
1563 t.show(mChild);
1564 t.setPosition(mChild, 0, 0);
1565 t.setPosition(mFGSurfaceControl, 0, 0);
1566 });
Robert Carr9b429f42017-04-17 14:56:57 -07001567
1568 {
1569 ScreenCapture::captureScreen(&mCapture);
1570 // We've positioned the child in the top left.
1571 mCapture->expectChildColor(0, 0);
1572 // But it's only 10x10.
1573 mCapture->expectFGColor(10, 10);
1574 }
1575
Robert Carr4cdc58f2017-08-23 14:22:20 -07001576 asTransaction([&](Transaction& t) {
1577 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1578 // We cause scaling by 2.
1579 t.setSize(mFGSurfaceControl, 128, 128);
1580 });
Robert Carr9b429f42017-04-17 14:56:57 -07001581
1582 {
1583 ScreenCapture::captureScreen(&mCapture);
1584 // We've positioned the child in the top left.
1585 mCapture->expectChildColor(0, 0);
1586 mCapture->expectChildColor(10, 10);
1587 mCapture->expectChildColor(19, 19);
1588 // And now it should be scaled all the way to 20x20
1589 mCapture->expectFGColor(20, 20);
1590 }
1591}
1592
Robert Carr1725eee2017-04-26 18:32:15 -07001593// Regression test for b/37673612
1594TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001595 asTransaction([&](Transaction& t) {
1596 t.show(mChild);
1597 t.setPosition(mChild, 0, 0);
1598 t.setPosition(mFGSurfaceControl, 0, 0);
1599 });
Robert Carr1725eee2017-04-26 18:32:15 -07001600
1601 {
1602 ScreenCapture::captureScreen(&mCapture);
1603 // We've positioned the child in the top left.
1604 mCapture->expectChildColor(0, 0);
1605 // But it's only 10x10.
1606 mCapture->expectFGColor(10, 10);
1607 }
Robert Carr1725eee2017-04-26 18:32:15 -07001608 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1609 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001610 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001611 sp<Surface> s = mFGSurfaceControl->getSurface();
1612 auto anw = static_cast<ANativeWindow*>(s.get());
1613 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1614 native_window_set_buffers_dimensions(anw, 64, 128);
1615 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1616 waitForPostedBuffers();
1617
1618 {
1619 // The child should still be in the same place and not have any strange scaling as in
1620 // b/37673612.
1621 ScreenCapture::captureScreen(&mCapture);
1622 mCapture->expectChildColor(0, 0);
1623 mCapture->expectFGColor(10, 10);
1624 }
1625}
1626
Dan Stoza412903f2017-04-27 13:42:17 -07001627TEST_F(ChildLayerTest, Bug36858924) {
1628 // Destroy the child layer
1629 mChild.clear();
1630
1631 // Now recreate it as hidden
1632 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1633 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1634 mFGSurfaceControl.get());
1635
1636 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001637 asTransaction([&](Transaction& t) {
1638 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001639 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001640 t.show(mChild);
1641 });
Dan Stoza412903f2017-04-27 13:42:17 -07001642
1643 // Render the foreground surface a few times
1644 //
1645 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1646 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1647 // never acquire/release the first buffer
1648 ALOGI("Filling 1");
1649 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1650 ALOGI("Filling 2");
1651 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1652 ALOGI("Filling 3");
1653 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1654 ALOGI("Filling 4");
1655 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1656}
1657
chaviwf1961f72017-09-18 16:41:07 -07001658TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001659 asTransaction([&](Transaction& t) {
1660 t.show(mChild);
1661 t.setPosition(mChild, 10, 10);
1662 t.setPosition(mFGSurfaceControl, 64, 64);
1663 });
chaviw06178942017-07-27 10:25:59 -07001664
1665 {
1666 ScreenCapture::captureScreen(&mCapture);
1667 // Top left of foreground must now be visible
1668 mCapture->expectFGColor(64, 64);
1669 // But 10 pixels in we should see the child surface
1670 mCapture->expectChildColor(74, 74);
1671 // And 10 more pixels we should be back to the foreground surface
1672 mCapture->expectFGColor(84, 84);
1673 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001674
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001675 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001676
chaviw06178942017-07-27 10:25:59 -07001677 {
1678 ScreenCapture::captureScreen(&mCapture);
1679 mCapture->expectFGColor(64, 64);
1680 // In reparenting we should have exposed the entire foreground surface.
1681 mCapture->expectFGColor(74, 74);
1682 // And the child layer should now begin at 10, 10 (since the BG
1683 // layer is at (0, 0)).
1684 mCapture->expectBGColor(9, 9);
1685 mCapture->expectChildColor(10, 10);
1686 }
1687}
1688
chaviwf1961f72017-09-18 16:41:07 -07001689TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001690 asTransaction([&](Transaction& t) {
1691 t.show(mChild);
1692 t.setPosition(mChild, 10, 10);
1693 t.setPosition(mFGSurfaceControl, 64, 64);
1694 });
chaviwf1961f72017-09-18 16:41:07 -07001695
1696 {
1697 ScreenCapture::captureScreen(&mCapture);
1698 // Top left of foreground must now be visible
1699 mCapture->expectFGColor(64, 64);
1700 // But 10 pixels in we should see the child surface
1701 mCapture->expectChildColor(74, 74);
1702 // And 10 more pixels we should be back to the foreground surface
1703 mCapture->expectFGColor(84, 84);
1704 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001705 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07001706 {
1707 ScreenCapture::captureScreen(&mCapture);
1708 // Nothing should have changed.
1709 mCapture->expectFGColor(64, 64);
1710 mCapture->expectChildColor(74, 74);
1711 mCapture->expectFGColor(84, 84);
1712 }
1713}
1714
1715TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001716 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
1717 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07001718 ASSERT_TRUE(newSurface != NULL);
1719 ASSERT_TRUE(newSurface->isValid());
1720
1721 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001722 asTransaction([&](Transaction& t) {
1723 t.hide(mChild);
1724 t.show(newSurface);
1725 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001726 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001727 t.setPosition(mFGSurfaceControl, 64, 64);
1728 });
chaviwf1961f72017-09-18 16:41:07 -07001729
1730 {
1731 ScreenCapture::captureScreen(&mCapture);
1732 // Top left of foreground must now be visible
1733 mCapture->expectFGColor(64, 64);
1734 // At 10, 10 we should see the new surface
1735 mCapture->checkPixel(10, 10, 63, 195, 63);
1736 }
1737
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001738 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07001739
1740 {
1741 ScreenCapture::captureScreen(&mCapture);
1742 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1743 // mFGSurface, putting it at 74, 74.
1744 mCapture->expectFGColor(64, 64);
1745 mCapture->checkPixel(74, 74, 63, 195, 63);
1746 mCapture->expectFGColor(84, 84);
1747 }
1748}
1749
chaviwc9674332017-08-28 12:32:18 -07001750TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001751 sp<SurfaceControl> grandchild =
1752 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
1753 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07001754 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1755
1756 {
1757 ScreenCapture::captureScreen(&mCapture);
1758 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1759 // which begins at 64, 64
1760 mCapture->checkPixel(64, 64, 50, 50, 50);
1761 }
1762}
1763
Robert Carr503c7042017-09-27 15:06:08 -07001764TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001765 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
1766 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07001767 fillSurfaceRGBA8(relative, 255, 255, 255);
1768
1769 Transaction t;
1770 t.setLayer(relative, INT32_MAX)
1771 .setRelativeLayer(mChild, relative->getHandle(), 1)
1772 .setPosition(mFGSurfaceControl, 0, 0)
1773 .apply(true);
1774
1775 // We expect that the child should have been elevated above our
1776 // INT_MAX layer even though it's not a child of it.
1777 {
1778 ScreenCapture::captureScreen(&mCapture);
1779 mCapture->expectChildColor(0, 0);
1780 mCapture->expectChildColor(9, 9);
1781 mCapture->checkPixel(10, 10, 255, 255, 255);
1782 }
1783}
1784
chaviw13fdc492017-06-27 12:40:18 -07001785class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001786protected:
chaviw13fdc492017-06-27 12:40:18 -07001787 void SetUp() override {
1788 LayerUpdateTest::SetUp();
1789
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001790 mLayerColorControl =
1791 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
1792 PIXEL_FORMAT_RGBA_8888,
1793 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07001794
1795 ASSERT_TRUE(mLayerColorControl != NULL);
1796 ASSERT_TRUE(mLayerColorControl->isValid());
1797
Robert Carr4cdc58f2017-08-23 14:22:20 -07001798 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001799 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001800 t.setPosition(mLayerColorControl, 140, 140);
1801 t.hide(mLayerColorControl);
1802 t.hide(mFGSurfaceControl);
1803 });
chaviw13fdc492017-06-27 12:40:18 -07001804 }
1805
1806 void TearDown() override {
1807 LayerUpdateTest::TearDown();
1808 mLayerColorControl = 0;
1809 }
1810
1811 sp<SurfaceControl> mLayerColorControl;
1812};
1813
1814TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1815 sp<ScreenCapture> sc;
1816
1817 {
1818 SCOPED_TRACE("before setColor");
1819 ScreenCapture::captureScreen(&sc);
1820 sc->expectBGColor(145, 145);
1821 }
1822
Robert Carr4cdc58f2017-08-23 14:22:20 -07001823 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001824 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001825 t.setColor(mLayerColorControl, color);
1826 t.show(mLayerColorControl);
1827 });
chaviw13fdc492017-06-27 12:40:18 -07001828
chaviw13fdc492017-06-27 12:40:18 -07001829 {
1830 // There should now be a color
1831 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001832
chaviw13fdc492017-06-27 12:40:18 -07001833 ScreenCapture::captureScreen(&sc);
1834 sc->checkPixel(145, 145, 43, 207, 131);
1835 }
1836}
1837
1838TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1839 sp<ScreenCapture> sc;
1840 {
1841 SCOPED_TRACE("before setColor");
1842 ScreenCapture::captureScreen(&sc);
1843 sc->expectBGColor(145, 145);
1844 }
1845
Robert Carr4cdc58f2017-08-23 14:22:20 -07001846 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001847 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001848 t.setColor(mLayerColorControl, color);
1849 t.setAlpha(mLayerColorControl, .75f);
1850 t.show(mLayerColorControl);
1851 });
1852
chaviw13fdc492017-06-27 12:40:18 -07001853 {
1854 // There should now be a color with .75 alpha
1855 SCOPED_TRACE("after setColor");
1856 ScreenCapture::captureScreen(&sc);
1857 sc->checkPixel(145, 145, 48, 171, 147);
1858 }
1859}
1860
1861TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1862 sp<ScreenCapture> sc;
1863 {
1864 SCOPED_TRACE("before setColor");
1865 ScreenCapture::captureScreen(&sc);
1866 sc->expectBGColor(145, 145);
1867 }
1868
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001869 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001870
chaviw13fdc492017-06-27 12:40:18 -07001871 {
1872 // There should now be set to 0,0,0 (black) as default.
1873 SCOPED_TRACE("after setColor");
1874 ScreenCapture::captureScreen(&sc);
1875 sc->checkPixel(145, 145, 0, 0, 0);
1876 }
1877}
1878
chaviwa76b2712017-09-20 12:02:26 -07001879class ScreenCaptureTest : public LayerUpdateTest {
1880protected:
1881 std::unique_ptr<CaptureLayer> mCapture;
1882};
1883
1884TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
1885 auto bgHandle = mBGSurfaceControl->getHandle();
1886 CaptureLayer::captureScreen(&mCapture, bgHandle);
1887 mCapture->expectBGColor(0, 0);
1888 // Doesn't capture FG layer which is at 64, 64
1889 mCapture->expectBGColor(64, 64);
1890}
1891
1892TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
1893 auto fgHandle = mFGSurfaceControl->getHandle();
1894
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001895 sp<SurfaceControl> child =
1896 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1897 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001898 fillSurfaceRGBA8(child, 200, 200, 200);
1899
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001900 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001901
1902 // Captures mFGSurfaceControl layer and its child.
1903 CaptureLayer::captureScreen(&mCapture, fgHandle);
1904 mCapture->expectFGColor(10, 10);
1905 mCapture->expectChildColor(0, 0);
1906}
1907
1908TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
1909 auto fgHandle = mFGSurfaceControl->getHandle();
1910
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001911 sp<SurfaceControl> child =
1912 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1913 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001914 fillSurfaceRGBA8(child, 200, 200, 200);
1915
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001916 sp<SurfaceControl> grandchild =
1917 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
1918 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07001919
1920 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1921 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001922 .show(child)
1923 .setPosition(grandchild, 5, 5)
1924 .show(grandchild)
1925 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001926
1927 // Captures mFGSurfaceControl, its child, and the grandchild.
1928 CaptureLayer::captureScreen(&mCapture, fgHandle);
1929 mCapture->expectFGColor(10, 10);
1930 mCapture->expectChildColor(0, 0);
1931 mCapture->checkPixel(5, 5, 50, 50, 50);
1932}
1933
1934TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001935 sp<SurfaceControl> child =
1936 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1937 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001938 fillSurfaceRGBA8(child, 200, 200, 200);
1939 auto childHandle = child->getHandle();
1940
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001941 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001942
1943 // Captures only the child layer, and not the parent.
1944 CaptureLayer::captureScreen(&mCapture, childHandle);
1945 mCapture->expectChildColor(0, 0);
1946 mCapture->expectChildColor(9, 9);
1947}
1948
1949TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
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 auto childHandle = child->getHandle();
1955
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001956 sp<SurfaceControl> grandchild =
1957 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
1958 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07001959 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1960
1961 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001962 .show(child)
1963 .setPosition(grandchild, 5, 5)
1964 .show(grandchild)
1965 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001966
1967 auto grandchildHandle = grandchild->getHandle();
1968
1969 // Captures only the grandchild.
1970 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
1971 mCapture->checkPixel(0, 0, 50, 50, 50);
1972 mCapture->checkPixel(4, 4, 50, 50, 50);
1973}
1974
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001975} // namespace android