blob: 845bc739129defd48d1b7c52f1fce8e92903541a [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
18#include <functional>
19#include <limits>
20#include <ostream>
21
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070022#include <gtest/gtest.h>
23
Michael Lentine5a16a622015-05-21 13:48:24 -070024#include <android/native_window.h>
25
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070027#include <gui/LayerState.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
31#include <private/gui/ComposerService.h>
32
Mathias Agopianc666cae2012-07-25 18:56:13 -070033#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070034#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070035#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070036
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070037#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070038#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070039
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040namespace android {
41
Chia-I Wu718daf82017-10-20 11:57:17 -070042namespace {
43
44struct Color {
45 uint8_t r;
46 uint8_t g;
47 uint8_t b;
48 uint8_t a;
49
50 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070051 static const Color GREEN;
Chia-I Wu718daf82017-10-20 11:57:17 -070052 static const Color BLACK;
53};
54
55const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070056const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070057const Color Color::BLACK{0, 0, 0, 255};
58
59std::ostream& operator<<(std::ostream& os, const Color& color) {
60 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
61 return os;
62}
63
64// Fill a region with the specified color.
65void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
66 int32_t x = rect.left;
67 int32_t y = rect.top;
68 int32_t width = rect.right - rect.left;
69 int32_t height = rect.bottom - rect.top;
70
71 if (x < 0) {
72 width += x;
73 x = 0;
74 }
75 if (y < 0) {
76 height += y;
77 y = 0;
78 }
79 if (x + width > buffer.width) {
80 x = std::min(x, buffer.width);
81 width = buffer.width - x;
82 }
83 if (y + height > buffer.height) {
84 y = std::min(y, buffer.height);
85 height = buffer.height - y;
86 }
87
88 for (int32_t j = 0; j < height; j++) {
89 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
90 for (int32_t i = 0; i < width; i++) {
91 dst[0] = color.r;
92 dst[1] = color.g;
93 dst[2] = color.b;
94 dst[3] = color.a;
95 dst += 4;
96 }
97 }
98}
99
100// Check if a region has the specified color.
101void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect,
102 const Color& color, uint8_t tolerance) {
103 int32_t x = rect.left;
104 int32_t y = rect.top;
105 int32_t width = rect.right - rect.left;
106 int32_t height = rect.bottom - rect.top;
107
108 if (x + width > int32_t(buffer.width)) {
109 x = std::min(x, int32_t(buffer.width));
110 width = buffer.width - x;
111 }
112 if (y + height > int32_t(buffer.height)) {
113 y = std::min(y, int32_t(buffer.height));
114 height = buffer.height - y;
115 }
116
117 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
118 uint8_t tmp = a >= b ? a - b : b - a;
119 return tmp <= tolerance;
120 };
121 for (int32_t j = 0; j < height; j++) {
122 const uint8_t* src =
123 static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4;
124 for (int32_t i = 0; i < width; i++) {
125 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
126 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
127 << "pixel @ (" << x + i << ", " << y + j << "): "
128 << "expected (" << color << "), "
129 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
130 src += 4;
131 }
132 }
133}
134
135} // anonymous namespace
136
Robert Carr4cdc58f2017-08-23 14:22:20 -0700137using Transaction = SurfaceComposerClient::Transaction;
138
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700139// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700140static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
141 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800142 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700143 sp<Surface> s = sc->getSurface();
144 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800145 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
146 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700147 for (int y = 0; y < outBuffer.height; y++) {
148 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700149 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700150 pixel[0] = r;
151 pixel[1] = g;
152 pixel[2] = b;
153 pixel[3] = 255;
154 }
155 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700156 if (unlock) {
157 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
158 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700159}
160
161// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
162// individual pixel values for testing purposes.
163class ScreenCapture : public RefBase {
164public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700165 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
166 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700167 sp<IGraphicBufferProducer> producer;
168 sp<IGraphicBufferConsumer> consumer;
169 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -0700170 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700171 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700172 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700173 SurfaceComposerClient::Transaction().apply(true);
174
Chia-I Wu718daf82017-10-20 11:57:17 -0700175 ASSERT_EQ(NO_ERROR,
176 sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false));
Michael Lentine5a16a622015-05-21 13:48:24 -0700177 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700178 }
179
Chia-I Wu718daf82017-10-20 11:57:17 -0700180 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
181 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
182 expectBufferColor(mBuf, rect, color, tolerance);
183 }
184
185 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
186 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
187 const bool leftBorder = rect.left > 0;
188 const bool topBorder = rect.top > 0;
189 const bool rightBorder = rect.right < int32_t(mBuf.width);
190 const bool bottomBorder = rect.bottom < int32_t(mBuf.height);
191
192 if (topBorder) {
193 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
194 if (leftBorder) {
195 top.left -= 1;
196 }
197 if (rightBorder) {
198 top.right += 1;
199 }
200 expectColor(top, color, tolerance);
201 }
202 if (leftBorder) {
203 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
204 expectColor(left, color, tolerance);
205 }
206 if (rightBorder) {
207 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
208 expectColor(right, color, tolerance);
209 }
210 if (bottomBorder) {
211 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
212 if (leftBorder) {
213 bottom.left -= 1;
214 }
215 if (rightBorder) {
216 bottom.right += 1;
217 }
218 expectColor(bottom, color, tolerance);
219 }
220 }
221
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700222 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700223 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
224 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
225 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700226 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
227 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700228 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
229 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700230 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700231 }
232 }
233
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700234 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700235
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700236 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700237
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700238 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700239
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700240private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700241 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700242 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
243 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700244
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700245 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700246
247 sp<CpuConsumer> mCC;
248 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700249};
250
chaviwa76b2712017-09-20 12:02:26 -0700251class CaptureLayer {
252public:
253 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
254 sp<IGraphicBufferProducer> producer;
255 sp<IGraphicBufferConsumer> consumer;
256 BufferQueue::createBufferQueue(&producer, &consumer);
257 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
258 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700259 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700260 SurfaceComposerClient::Transaction().apply(true);
261 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
262 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
263 }
264
265 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
266 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
267 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
268 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
269 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
270 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700271 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700272 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
273 EXPECT_EQ(String8(), err) << err.string();
274 }
275 }
276
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700277 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700278
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700279 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700280
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700281 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700282
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700283 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700284 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
285 }
286
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700287 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700288
289private:
290 sp<CpuConsumer> mCC;
291 CpuConsumer::LockedBuffer mBuffer;
292};
293
Chia-I Wu718daf82017-10-20 11:57:17 -0700294class LayerTransactionTest : public ::testing::Test {
295protected:
296 void SetUp() override {
297 mClient = new SurfaceComposerClient;
298 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
299
300 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
301 }
302
303 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
304 uint32_t flags = 0) {
305 auto layer =
306 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
307 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
308
309 status_t error = Transaction()
310 .setLayerStack(layer, mDisplayLayerStack)
311 .setLayer(layer, mLayerZBase)
312 .apply();
313 if (error != NO_ERROR) {
314 ADD_FAILURE() << "failed to initialize SurfaceControl";
315 layer.clear();
316 }
317
318 return layer;
319 }
320
321 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
322 // wait for previous transactions (such as setSize) to complete
323 Transaction().apply(true);
324
325 ANativeWindow_Buffer buffer = {};
326 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
327
328 return buffer;
329 }
330
331 void postLayerBuffer(const sp<SurfaceControl>& layer) {
332 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
333
334 // wait for the newly posted buffer to be latched
335 waitForLayerBuffers();
336 }
337
338 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
339 ANativeWindow_Buffer buffer;
340 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
341 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
342 postLayerBuffer(layer);
343 }
344
345 sp<ScreenCapture> screenshot() {
346 sp<ScreenCapture> screenshot;
347 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
348 return screenshot;
349 }
350
351 sp<SurfaceComposerClient> mClient;
352
353 sp<IBinder> mDisplay;
354 uint32_t mDisplayWidth;
355 uint32_t mDisplayHeight;
356 uint32_t mDisplayLayerStack;
357
358 // leave room for ~256 layers
359 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
360
361private:
362 void SetUpDisplay() {
363 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
364 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
365
366 // get display width/height
367 DisplayInfo info;
368 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
369 mDisplayWidth = info.w;
370 mDisplayHeight = info.h;
371
372 // After a new buffer is queued, SurfaceFlinger is notified and will
373 // latch the new buffer on next vsync. Let's heuristically wait for 3
374 // vsyncs.
375 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
376
377 mDisplayLayerStack = 0;
378 // set layer stack (b/68888219)
379 Transaction t;
380 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
381 t.apply();
382 }
383
384 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
385
386 int32_t mBufferPostDelay;
387};
388
389TEST_F(LayerTransactionTest, SetPositionBasic) {
390 sp<SurfaceControl> layer;
391 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
392 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
393
394 {
395 SCOPED_TRACE("default position");
396 auto shot = screenshot();
397 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
398 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
399 }
400
401 Transaction().setPosition(layer, 5, 10).apply();
402 {
403 SCOPED_TRACE("new position");
404 auto shot = screenshot();
405 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
406 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
407 }
408}
409
410TEST_F(LayerTransactionTest, SetPositionRounding) {
411 sp<SurfaceControl> layer;
412 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
413 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
414
415 // GLES requires only 4 bits of subpixel precision during rasterization
416 // XXX GLES composition does not match HWC composition due to precision
417 // loss (b/69315223)
418 const float epsilon = 1.0f / 16.0f;
419 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
420 {
421 SCOPED_TRACE("rounding down");
422 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
423 }
424
425 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
426 {
427 SCOPED_TRACE("rounding up");
428 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
429 }
430}
431
432TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
433 sp<SurfaceControl> layer;
434 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
435 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
436
437 Transaction().setPosition(layer, -32, -32).apply();
438 {
439 SCOPED_TRACE("negative coordinates");
440 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
441 }
442
443 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
444 {
445 SCOPED_TRACE("positive coordinates");
446 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
447 }
448}
449
450TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
451 sp<SurfaceControl> layer;
452 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
453 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
454
455 // partially out of bounds
456 Transaction().setPosition(layer, -30, -30).apply();
457 {
458 SCOPED_TRACE("negative coordinates");
459 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
460 }
461
462 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
463 {
464 SCOPED_TRACE("positive coordinates");
465 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
466 mDisplayHeight),
467 Color::RED);
468 }
469}
470
471TEST_F(LayerTransactionTest, SetPositionWithResize) {
472 sp<SurfaceControl> layer;
473 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
474 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
475
476 // setPosition is applied immediately by default, with or without resize
477 // pending
478 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
479 {
480 SCOPED_TRACE("resize pending");
481 auto shot = screenshot();
482 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
483 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
484 }
485
486 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
487 {
488 SCOPED_TRACE("resize applied");
489 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
490 }
491}
492
493TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
494 sp<SurfaceControl> layer;
495 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
496 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
497
498 // request setPosition to be applied with the next resize
499 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
500 {
501 SCOPED_TRACE("new position pending");
502 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
503 }
504
505 Transaction().setPosition(layer, 15, 20).apply();
506 {
507 SCOPED_TRACE("pending new position modified");
508 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
509 }
510
511 Transaction().setSize(layer, 64, 64).apply();
512 {
513 SCOPED_TRACE("resize pending");
514 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
515 }
516
517 // finally resize and latch the buffer
518 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
519 {
520 SCOPED_TRACE("new position applied");
521 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
522 }
523}
524
525TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
526 sp<SurfaceControl> layer;
527 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
528 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
529
530 // setPosition is not immediate even with SCALE_TO_WINDOW override
531 Transaction()
532 .setPosition(layer, 5, 10)
533 .setSize(layer, 64, 64)
534 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
535 .setGeometryAppliesWithResize(layer)
536 .apply();
537 {
538 SCOPED_TRACE("new position pending");
539 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
540 }
541
542 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
543 {
544 SCOPED_TRACE("new position applied");
545 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
546 }
547}
548
Chia-I Wu0eaea312017-10-31 10:14:40 -0700549TEST_F(LayerTransactionTest, SetSizeBasic) {
550 sp<SurfaceControl> layer;
551 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
552 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
553
554 Transaction().setSize(layer, 64, 64).apply();
555 {
556 SCOPED_TRACE("resize pending");
557 auto shot = screenshot();
558 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
559 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
560 }
561
562 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
563 {
564 SCOPED_TRACE("resize applied");
565 auto shot = screenshot();
566 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
567 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
568 }
569}
570
571TEST_F(LayerTransactionTest, SetSizeInvalid) {
572 // cannot test robustness against invalid sizes (zero or really huge)
573}
574
575TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
576 sp<SurfaceControl> layer;
577 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
578 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
579
580 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
581 Transaction()
582 .setSize(layer, 64, 64)
583 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
584 .apply();
585 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
586}
587
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700588TEST_F(LayerTransactionTest, SetZBasic) {
589 sp<SurfaceControl> layerR;
590 sp<SurfaceControl> layerG;
591 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
592 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
593 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
594 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
595
596 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
597 {
598 SCOPED_TRACE("layerR");
599 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
600 }
601
602 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
603 {
604 SCOPED_TRACE("layerG");
605 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
606 }
607}
608
609TEST_F(LayerTransactionTest, SetZNegative) {
610 sp<SurfaceControl> layerR;
611 sp<SurfaceControl> layerG;
612 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
613 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
614 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
615 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
616
617 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
618 {
619 SCOPED_TRACE("layerR");
620 sp<ScreenCapture> screenshot;
621 ScreenCapture::captureScreen(&screenshot, -2, -1);
622 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
623 }
624
625 Transaction().setLayer(layerR, -3).apply();
626 {
627 SCOPED_TRACE("layerG");
628 sp<ScreenCapture> screenshot;
629 ScreenCapture::captureScreen(&screenshot, -3, -1);
630 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
631 }
632}
633
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700634class LayerUpdateTest : public ::testing::Test {
635protected:
636 virtual void SetUp() {
637 mComposerClient = new SurfaceComposerClient;
638 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
639
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700640 sp<IBinder> display(
641 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700642 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700643 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700644
645 ssize_t displayWidth = info.w;
646 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700647
648 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700649 mBGSurfaceControl =
650 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
651 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700652 ASSERT_TRUE(mBGSurfaceControl != NULL);
653 ASSERT_TRUE(mBGSurfaceControl->isValid());
654 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
655
656 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700657 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
658 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700659 ASSERT_TRUE(mFGSurfaceControl != NULL);
660 ASSERT_TRUE(mFGSurfaceControl->isValid());
661
662 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
663
664 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700665 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
666 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700667 ASSERT_TRUE(mSyncSurfaceControl != NULL);
668 ASSERT_TRUE(mSyncSurfaceControl->isValid());
669
670 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
671
Robert Carr4cdc58f2017-08-23 14:22:20 -0700672 asTransaction([&](Transaction& t) {
673 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700674
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700675 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700676
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700677 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
678 .setPosition(mFGSurfaceControl, 64, 64)
679 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700680
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700681 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
682 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
683 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700684 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700685 }
686
687 virtual void TearDown() {
688 mComposerClient->dispose();
689 mBGSurfaceControl = 0;
690 mFGSurfaceControl = 0;
691 mSyncSurfaceControl = 0;
692 mComposerClient = 0;
693 }
694
695 void waitForPostedBuffers() {
696 // Since the sync surface is in synchronous mode (i.e. double buffered)
697 // posting three buffers to it should ensure that at least two
698 // SurfaceFlinger::handlePageFlip calls have been made, which should
699 // guaranteed that a buffer posted to another Surface has been retired.
700 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
701 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
702 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
703 }
704
Robert Carr4cdc58f2017-08-23 14:22:20 -0700705 void asTransaction(const std::function<void(Transaction&)>& exec) {
706 Transaction t;
707 exec(t);
708 t.apply(true);
709 }
710
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700711 sp<SurfaceComposerClient> mComposerClient;
712 sp<SurfaceControl> mBGSurfaceControl;
713 sp<SurfaceControl> mFGSurfaceControl;
714
715 // This surface is used to ensure that the buffers posted to
716 // mFGSurfaceControl have been picked up by SurfaceFlinger.
717 sp<SurfaceControl> mSyncSurfaceControl;
718};
719
Robert Carr7f619b22017-11-06 12:56:35 -0800720TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
721 sp<ScreenCapture> sc;
722
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700723 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
724 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -0800725 fillSurfaceRGBA8(relative, 10, 10, 10);
726 waitForPostedBuffers();
727
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700728 Transaction{}
729 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -0800730 .setPosition(relative, 64, 64)
731 .apply();
732
733 {
734 // The relative should be on top of the FG control.
735 ScreenCapture::captureScreen(&sc);
736 sc->checkPixel(64, 64, 10, 10, 10);
737 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700738 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800739
740 {
741 // Nothing should change at this point.
742 ScreenCapture::captureScreen(&sc);
743 sc->checkPixel(64, 64, 10, 10, 10);
744 }
745
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700746 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800747
748 {
749 // Ensure that the relative was actually hidden, rather than
750 // being left in the detached but visible state.
751 ScreenCapture::captureScreen(&sc);
752 sc->expectFGColor(64, 64);
753 }
754}
755
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700756TEST_F(LayerUpdateTest, LayerMoveWorks) {
757 sp<ScreenCapture> sc;
758 {
759 SCOPED_TRACE("before move");
760 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800761 sc->expectBGColor(0, 12);
762 sc->expectFGColor(75, 75);
763 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700764 }
765
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700766 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700767
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700768 {
769 // This should reflect the new position, but not the new color.
770 SCOPED_TRACE("after move, before redraw");
771 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800772 sc->expectBGColor(24, 24);
773 sc->expectBGColor(75, 75);
774 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700775 }
776
777 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
778 waitForPostedBuffers();
779 {
780 // This should reflect the new position and the new color.
781 SCOPED_TRACE("after redraw");
782 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800783 sc->expectBGColor(24, 24);
784 sc->expectBGColor(75, 75);
785 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700786 }
787}
788
789TEST_F(LayerUpdateTest, LayerResizeWorks) {
790 sp<ScreenCapture> sc;
791 {
792 SCOPED_TRACE("before resize");
793 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800794 sc->expectBGColor(0, 12);
795 sc->expectFGColor(75, 75);
796 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700797 }
798
Steve Block9d453682011-12-20 16:23:08 +0000799 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700800 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +0000801 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700802 {
803 // This should not reflect the new size or color because SurfaceFlinger
804 // has not yet received a buffer of the correct size.
805 SCOPED_TRACE("after resize, before redraw");
806 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800807 sc->expectBGColor(0, 12);
808 sc->expectFGColor(75, 75);
809 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700810 }
811
Steve Block9d453682011-12-20 16:23:08 +0000812 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700813 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
814 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000815 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700816 {
817 // This should reflect the new size and the new color.
818 SCOPED_TRACE("after redraw");
819 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800820 sc->expectBGColor(24, 24);
821 sc->checkPixel(75, 75, 63, 195, 63);
822 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700823 }
824}
825
Haixia Shid5750962015-07-27 16:50:49 -0700826TEST_F(LayerUpdateTest, LayerCropWorks) {
827 sp<ScreenCapture> sc;
828 {
829 SCOPED_TRACE("before crop");
830 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800831 sc->expectBGColor(24, 24);
832 sc->expectFGColor(75, 75);
833 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700834 }
835
Robert Carr4cdc58f2017-08-23 14:22:20 -0700836 asTransaction([&](Transaction& t) {
837 Rect cropRect(16, 16, 32, 32);
838 t.setCrop(mFGSurfaceControl, cropRect);
839 });
Haixia Shid5750962015-07-27 16:50:49 -0700840 {
841 // This should crop the foreground surface.
842 SCOPED_TRACE("after crop");
843 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800844 sc->expectBGColor(24, 24);
845 sc->expectBGColor(75, 75);
846 sc->expectFGColor(95, 80);
847 sc->expectFGColor(80, 95);
848 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -0700849 }
850}
851
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000852TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
853 sp<ScreenCapture> sc;
854 {
855 SCOPED_TRACE("before crop");
856 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800857 sc->expectBGColor(24, 24);
858 sc->expectFGColor(75, 75);
859 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000860 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700861 asTransaction([&](Transaction& t) {
862 Rect cropRect(16, 16, 32, 32);
863 t.setFinalCrop(mFGSurfaceControl, cropRect);
864 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000865 {
866 // This should crop the foreground surface.
867 SCOPED_TRACE("after crop");
868 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800869 sc->expectBGColor(24, 24);
870 sc->expectBGColor(75, 75);
871 sc->expectBGColor(95, 80);
872 sc->expectBGColor(80, 95);
873 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000874 }
875}
876
Haixia Shid5750962015-07-27 16:50:49 -0700877TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
878 sp<ScreenCapture> sc;
879 {
880 SCOPED_TRACE("before setLayer");
881 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800882 sc->expectBGColor(24, 24);
883 sc->expectFGColor(75, 75);
884 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700885 }
886
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700887 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700888
Haixia Shid5750962015-07-27 16:50:49 -0700889 {
890 // This should hide the foreground surface beneath the background.
891 SCOPED_TRACE("after setLayer");
892 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800893 sc->expectBGColor(24, 24);
894 sc->expectBGColor(75, 75);
895 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700896 }
897}
898
899TEST_F(LayerUpdateTest, LayerShowHideWorks) {
900 sp<ScreenCapture> sc;
901 {
902 SCOPED_TRACE("before hide");
903 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800904 sc->expectBGColor(24, 24);
905 sc->expectFGColor(75, 75);
906 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700907 }
908
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700909 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700910
Haixia Shid5750962015-07-27 16:50:49 -0700911 {
912 // This should hide the foreground surface.
913 SCOPED_TRACE("after hide, before show");
914 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800915 sc->expectBGColor(24, 24);
916 sc->expectBGColor(75, 75);
917 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700918 }
919
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700920 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700921
Haixia Shid5750962015-07-27 16:50:49 -0700922 {
923 // This should show the foreground surface.
924 SCOPED_TRACE("after show");
925 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800926 sc->expectBGColor(24, 24);
927 sc->expectFGColor(75, 75);
928 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700929 }
930}
931
932TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
933 sp<ScreenCapture> sc;
934 {
935 SCOPED_TRACE("before setAlpha");
936 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800937 sc->expectBGColor(24, 24);
938 sc->expectFGColor(75, 75);
939 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700940 }
941
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700942 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -0700943
Haixia Shid5750962015-07-27 16:50:49 -0700944 {
945 // This should set foreground to be 75% opaque.
946 SCOPED_TRACE("after setAlpha");
947 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800948 sc->expectBGColor(24, 24);
949 sc->checkPixel(75, 75, 162, 63, 96);
950 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -0700951 }
952}
953
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700954TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
955 sp<ScreenCapture> sc;
956 {
957 SCOPED_TRACE("before setLayerStack");
958 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800959 sc->expectBGColor(24, 24);
960 sc->expectFGColor(75, 75);
961 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700962 }
963
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700964 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700965 {
966 // This should hide the foreground surface since it goes to a different
967 // layer stack.
968 SCOPED_TRACE("after setLayerStack");
969 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800970 sc->expectBGColor(24, 24);
971 sc->expectBGColor(75, 75);
972 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700973 }
974}
975
976TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
977 sp<ScreenCapture> sc;
978 {
979 SCOPED_TRACE("before setFlags");
980 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800981 sc->expectBGColor(24, 24);
982 sc->expectFGColor(75, 75);
983 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700984 }
985
Robert Carr4cdc58f2017-08-23 14:22:20 -0700986 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700987 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700988 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700989 {
990 // This should hide the foreground surface
991 SCOPED_TRACE("after setFlags");
992 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -0800993 sc->expectBGColor(24, 24);
994 sc->expectBGColor(75, 75);
995 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700996 }
997}
998
999TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1000 sp<ScreenCapture> sc;
1001 {
1002 SCOPED_TRACE("before setMatrix");
1003 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001004 sc->expectBGColor(24, 24);
1005 sc->expectFGColor(91, 96);
1006 sc->expectFGColor(96, 101);
1007 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001008 }
1009
Robert Carr4cdc58f2017-08-23 14:22:20 -07001010 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001011 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001012 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001013 {
1014 SCOPED_TRACE("after setMatrix");
1015 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001016 sc->expectBGColor(24, 24);
1017 sc->expectFGColor(91, 96);
1018 sc->expectBGColor(96, 91);
1019 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001020 }
1021}
1022
Robert Carr8d5227b2017-03-16 15:41:03 -07001023class GeometryLatchingTest : public LayerUpdateTest {
1024protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001025 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001026 SCOPED_TRACE(trace);
1027 ScreenCapture::captureScreen(&sc);
1028 // We find the leading edge of the FG surface.
1029 sc->expectFGColor(127, 127);
1030 sc->expectBGColor(128, 128);
1031 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001032
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001033 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001034
1035 void unlockFGBuffer() {
1036 sp<Surface> s = mFGSurfaceControl->getSurface();
1037 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1038 waitForPostedBuffers();
1039 }
1040
Robert Carr8d5227b2017-03-16 15:41:03 -07001041 void completeFGResize() {
1042 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1043 waitForPostedBuffers();
1044 }
1045 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001046 asTransaction([&](Transaction& t) {
1047 t.setSize(mFGSurfaceControl, 64, 64);
1048 t.setPosition(mFGSurfaceControl, 64, 64);
1049 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1050 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1051 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001052
1053 EXPECT_INITIAL_STATE("After restoring initial state");
1054 }
1055 sp<ScreenCapture> sc;
1056};
1057
1058TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1059 EXPECT_INITIAL_STATE("before anything");
1060
1061 // By default position can be updated even while
1062 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001063 asTransaction([&](Transaction& t) {
1064 t.setSize(mFGSurfaceControl, 32, 32);
1065 t.setPosition(mFGSurfaceControl, 100, 100);
1066 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001067
1068 {
1069 SCOPED_TRACE("After moving surface");
1070 ScreenCapture::captureScreen(&sc);
1071 // If we moved, the FG Surface should cover up what was previously BG
1072 // however if we didn't move the FG wouldn't be large enough now.
1073 sc->expectFGColor(163, 163);
1074 }
1075
1076 restoreInitialState();
1077
1078 // Now we repeat with setGeometryAppliesWithResize
1079 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001080 asTransaction([&](Transaction& t) {
1081 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1082 t.setSize(mFGSurfaceControl, 32, 32);
1083 t.setPosition(mFGSurfaceControl, 100, 100);
1084 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001085
1086 {
1087 SCOPED_TRACE("While resize is pending");
1088 ScreenCapture::captureScreen(&sc);
1089 // This time we shouldn't have moved, so the BG color
1090 // should still be visible.
1091 sc->expectBGColor(128, 128);
1092 }
1093
1094 completeFGResize();
1095
1096 {
1097 SCOPED_TRACE("After the resize");
1098 ScreenCapture::captureScreen(&sc);
1099 // But after the resize completes, we should move
1100 // and the FG should be visible here.
1101 sc->expectFGColor(128, 128);
1102 }
1103}
1104
1105class CropLatchingTest : public GeometryLatchingTest {
1106protected:
1107 void EXPECT_CROPPED_STATE(const char* trace) {
1108 SCOPED_TRACE(trace);
1109 ScreenCapture::captureScreen(&sc);
1110 // The edge should be moved back one pixel by our crop.
1111 sc->expectFGColor(126, 126);
1112 sc->expectBGColor(127, 127);
1113 sc->expectBGColor(128, 128);
1114 }
chaviw59f5c562017-06-28 16:39:06 -07001115
1116 void EXPECT_RESIZE_STATE(const char* trace) {
1117 SCOPED_TRACE(trace);
1118 ScreenCapture::captureScreen(&sc);
1119 // The FG is now resized too 128,128 at 64,64
1120 sc->expectFGColor(64, 64);
1121 sc->expectFGColor(191, 191);
1122 sc->expectBGColor(192, 192);
1123 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001124};
1125
1126TEST_F(CropLatchingTest, CropLatching) {
1127 EXPECT_INITIAL_STATE("before anything");
1128 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001129 asTransaction([&](Transaction& t) {
1130 t.setSize(mFGSurfaceControl, 128, 128);
1131 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1132 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001133
1134 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1135
1136 restoreInitialState();
1137
Robert Carr4cdc58f2017-08-23 14:22:20 -07001138 asTransaction([&](Transaction& t) {
1139 t.setSize(mFGSurfaceControl, 128, 128);
1140 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1141 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1142 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001143
1144 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1145
1146 completeFGResize();
1147
1148 EXPECT_CROPPED_STATE("after the resize finishes");
1149}
1150
1151TEST_F(CropLatchingTest, FinalCropLatching) {
1152 EXPECT_INITIAL_STATE("before anything");
1153 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001154 asTransaction([&](Transaction& t) {
1155 t.setSize(mFGSurfaceControl, 128, 128);
1156 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1157 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001158
1159 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1160
1161 restoreInitialState();
1162
Robert Carr4cdc58f2017-08-23 14:22:20 -07001163 asTransaction([&](Transaction& t) {
1164 t.setSize(mFGSurfaceControl, 128, 128);
1165 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1166 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1167 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001168
1169 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1170
1171 completeFGResize();
1172
1173 EXPECT_CROPPED_STATE("after the resize finishes");
1174}
1175
Robert Carr7bf247e2017-05-18 14:02:49 -07001176// In this test we ensure that setGeometryAppliesWithResize actually demands
1177// a buffer of the new size, and not just any size.
1178TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1179 EXPECT_INITIAL_STATE("before anything");
1180 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001181 asTransaction([&](Transaction& t) {
1182 t.setSize(mFGSurfaceControl, 128, 128);
1183 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1184 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001185
1186 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1187
1188 restoreInitialState();
1189
1190 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1191 // initiating the resize.
1192 lockAndFillFGBuffer();
1193
Robert Carr4cdc58f2017-08-23 14:22:20 -07001194 asTransaction([&](Transaction& t) {
1195 t.setSize(mFGSurfaceControl, 128, 128);
1196 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1197 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1198 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001199
1200 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1201
1202 // We now submit our old buffer, at the old size, and ensure it doesn't
1203 // trigger geometry latching.
1204 unlockFGBuffer();
1205
1206 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1207
1208 completeFGResize();
1209
1210 EXPECT_CROPPED_STATE("after the resize finishes");
1211}
1212
1213TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1214 EXPECT_INITIAL_STATE("before anything");
1215 // In this scenario, we attempt to set the final crop a second time while the resize
1216 // is still pending, and ensure we are successful. Success meaning the second crop
1217 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001218 asTransaction([&](Transaction& t) {
1219 t.setSize(mFGSurfaceControl, 128, 128);
1220 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1221 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1222 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001223
chaviw59f5c562017-06-28 16:39:06 -07001224 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1225
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001226 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001227
chaviw59f5c562017-06-28 16:39:06 -07001228 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001229
1230 completeFGResize();
1231
chaviw59f5c562017-06-28 16:39:06 -07001232 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001233}
1234
Pablo Ceballos05289c22016-04-14 15:49:55 -07001235TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1236 sp<ScreenCapture> sc;
1237 {
1238 SCOPED_TRACE("before anything");
1239 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001240 sc->expectBGColor(32, 32);
1241 sc->expectFGColor(96, 96);
1242 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001243 }
1244
1245 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001246 asTransaction([&](Transaction& t) {
1247 t.setAlpha(mFGSurfaceControl, 0.75);
1248 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001249 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001250 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001251
Robert Carr4cdc58f2017-08-23 14:22:20 -07001252 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001253 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001254 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001255 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001256 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001257
1258 {
1259 SCOPED_TRACE("before any trigger");
1260 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001261 sc->expectBGColor(32, 32);
1262 sc->expectFGColor(96, 96);
1263 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001264 }
1265
1266 // should trigger the first deferred transaction, but not the second one
1267 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1268 {
1269 SCOPED_TRACE("after first trigger");
1270 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001271 sc->expectBGColor(32, 32);
1272 sc->checkPixel(96, 96, 162, 63, 96);
1273 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001274 }
1275
1276 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001277 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001278
1279 // trigger the second deferred transaction
1280 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1281 {
1282 SCOPED_TRACE("after second trigger");
1283 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001284 sc->expectBGColor(32, 32);
1285 sc->expectBGColor(96, 96);
1286 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001287 }
1288}
1289
Robert Carrdb66e622017-04-10 16:55:57 -07001290TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1291 sp<ScreenCapture> sc;
1292 {
1293 SCOPED_TRACE("before adding relative surface");
1294 ScreenCapture::captureScreen(&sc);
1295 sc->expectBGColor(24, 24);
1296 sc->expectFGColor(75, 75);
1297 sc->expectBGColor(145, 145);
1298 }
1299
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001300 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1301 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001302 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1303 waitForPostedBuffers();
1304
1305 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001306 asTransaction([&](Transaction& t) {
1307 t.setPosition(relativeSurfaceControl, 64, 64);
1308 t.show(relativeSurfaceControl);
1309 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1310 });
Robert Carrdb66e622017-04-10 16:55:57 -07001311
1312 {
1313 SCOPED_TRACE("after adding relative surface");
1314 ScreenCapture::captureScreen(&sc);
1315 // our relative surface should be visible now.
1316 sc->checkPixel(75, 75, 255, 177, 177);
1317 }
1318
1319 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001320 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001321
1322 {
1323 SCOPED_TRACE("after set layer");
1324 ScreenCapture::captureScreen(&sc);
1325 // now the FG surface should be visible again.
1326 sc->expectFGColor(75, 75);
1327 }
1328}
1329
Robert Carre392b552017-09-19 12:16:05 -07001330TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1331 sp<ScreenCapture> sc;
1332
1333 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001334 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1335 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1336 sp<SurfaceControl> childBuffer =
1337 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1338 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001339 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1340
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001341 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001342
1343 {
1344 ScreenCapture::captureScreen(&sc);
1345 sc->expectChildColor(73, 73);
1346 sc->expectFGColor(74, 74);
1347 }
1348
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001349 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001350
1351 {
1352 ScreenCapture::captureScreen(&sc);
1353 sc->expectChildColor(73, 73);
1354 sc->expectChildColor(74, 74);
1355 }
1356}
1357
Robert Carr2c5f6d22017-09-26 12:30:35 -07001358TEST_F(LayerUpdateTest, MergingTransactions) {
1359 sp<ScreenCapture> sc;
1360 {
1361 SCOPED_TRACE("before move");
1362 ScreenCapture::captureScreen(&sc);
1363 sc->expectBGColor(0, 12);
1364 sc->expectFGColor(75, 75);
1365 sc->expectBGColor(145, 145);
1366 }
1367
1368 Transaction t1, t2;
1369 t1.setPosition(mFGSurfaceControl, 128, 128);
1370 t2.setPosition(mFGSurfaceControl, 0, 0);
1371 // We expect that the position update from t2 now
1372 // overwrites the position update from t1.
1373 t1.merge(std::move(t2));
1374 t1.apply();
1375
1376 {
1377 ScreenCapture::captureScreen(&sc);
1378 sc->expectFGColor(1, 1);
1379 }
1380}
1381
Robert Carr1f0a16a2016-10-24 16:27:39 -07001382class ChildLayerTest : public LayerUpdateTest {
1383protected:
1384 void SetUp() override {
1385 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001386 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1387 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001388 fillSurfaceRGBA8(mChild, 200, 200, 200);
1389
1390 {
1391 SCOPED_TRACE("before anything");
1392 ScreenCapture::captureScreen(&mCapture);
1393 mCapture->expectChildColor(64, 64);
1394 }
1395 }
1396 void TearDown() override {
1397 LayerUpdateTest::TearDown();
1398 mChild = 0;
1399 }
1400
1401 sp<SurfaceControl> mChild;
1402 sp<ScreenCapture> mCapture;
1403};
1404
1405TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001406 asTransaction([&](Transaction& t) {
1407 t.show(mChild);
1408 t.setPosition(mChild, 10, 10);
1409 t.setPosition(mFGSurfaceControl, 64, 64);
1410 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001411
1412 {
1413 ScreenCapture::captureScreen(&mCapture);
1414 // Top left of foreground must now be visible
1415 mCapture->expectFGColor(64, 64);
1416 // But 10 pixels in we should see the child surface
1417 mCapture->expectChildColor(74, 74);
1418 // And 10 more pixels we should be back to the foreground surface
1419 mCapture->expectFGColor(84, 84);
1420 }
1421
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001422 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001423
1424 {
1425 ScreenCapture::captureScreen(&mCapture);
1426 // Top left of foreground should now be at 0, 0
1427 mCapture->expectFGColor(0, 0);
1428 // But 10 pixels in we should see the child surface
1429 mCapture->expectChildColor(10, 10);
1430 // And 10 more pixels we should be back to the foreground surface
1431 mCapture->expectFGColor(20, 20);
1432 }
1433}
1434
Robert Carr41b08b52017-06-01 16:11:34 -07001435TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001436 asTransaction([&](Transaction& t) {
1437 t.show(mChild);
1438 t.setPosition(mChild, 0, 0);
1439 t.setPosition(mFGSurfaceControl, 0, 0);
1440 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1441 });
Robert Carr41b08b52017-06-01 16:11:34 -07001442
1443 {
1444 ScreenCapture::captureScreen(&mCapture);
1445 mCapture->expectChildColor(0, 0);
1446 mCapture->expectChildColor(4, 4);
1447 mCapture->expectBGColor(5, 5);
1448 }
1449}
1450
1451TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001452 asTransaction([&](Transaction& t) {
1453 t.show(mChild);
1454 t.setPosition(mChild, 0, 0);
1455 t.setPosition(mFGSurfaceControl, 0, 0);
1456 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1457 });
Robert Carr41b08b52017-06-01 16:11:34 -07001458
1459 {
1460 ScreenCapture::captureScreen(&mCapture);
1461 mCapture->expectChildColor(0, 0);
1462 mCapture->expectChildColor(4, 4);
1463 mCapture->expectBGColor(5, 5);
1464 }
1465}
1466
Robert Carr1f0a16a2016-10-24 16:27:39 -07001467TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001468 asTransaction([&](Transaction& t) {
1469 t.show(mChild);
1470 t.setPosition(mFGSurfaceControl, 0, 0);
1471 t.setPosition(mChild, 63, 63);
1472 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001473
1474 {
1475 ScreenCapture::captureScreen(&mCapture);
1476 mCapture->expectFGColor(0, 0);
1477 // Last pixel in foreground should now be the child.
1478 mCapture->expectChildColor(63, 63);
1479 // But the child should be constrained and the next pixel
1480 // must be the background
1481 mCapture->expectBGColor(64, 64);
1482 }
1483}
1484
1485TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001486 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001487
1488 // Find the boundary between the parent and child
1489 {
1490 ScreenCapture::captureScreen(&mCapture);
1491 mCapture->expectChildColor(9, 9);
1492 mCapture->expectFGColor(10, 10);
1493 }
1494
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001495 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001496
1497 // The boundary should be twice as far from the origin now.
1498 // The pixels from the last test should all be child now
1499 {
1500 ScreenCapture::captureScreen(&mCapture);
1501 mCapture->expectChildColor(9, 9);
1502 mCapture->expectChildColor(10, 10);
1503 mCapture->expectChildColor(19, 19);
1504 mCapture->expectFGColor(20, 20);
1505 }
1506}
Robert Carr9524cb32017-02-13 11:32:32 -08001507
Robert Carr6452f122017-03-21 10:41:29 -07001508TEST_F(ChildLayerTest, ChildLayerAlpha) {
1509 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1510 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1511 fillSurfaceRGBA8(mChild, 0, 254, 0);
1512 waitForPostedBuffers();
1513
Robert Carr4cdc58f2017-08-23 14:22:20 -07001514 asTransaction([&](Transaction& t) {
1515 t.show(mChild);
1516 t.setPosition(mChild, 0, 0);
1517 t.setPosition(mFGSurfaceControl, 0, 0);
1518 });
Robert Carr6452f122017-03-21 10:41:29 -07001519
1520 {
1521 ScreenCapture::captureScreen(&mCapture);
1522 // Unblended child color
1523 mCapture->checkPixel(0, 0, 0, 254, 0);
1524 }
1525
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001526 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001527
1528 {
1529 ScreenCapture::captureScreen(&mCapture);
1530 // Child and BG blended.
1531 mCapture->checkPixel(0, 0, 127, 127, 0);
1532 }
1533
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001534 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001535
1536 {
1537 ScreenCapture::captureScreen(&mCapture);
1538 // Child and BG blended.
1539 mCapture->checkPixel(0, 0, 95, 64, 95);
1540 }
1541}
1542
Robert Carr9524cb32017-02-13 11:32:32 -08001543TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001544 asTransaction([&](Transaction& t) {
1545 t.show(mChild);
1546 t.setPosition(mChild, 10, 10);
1547 t.setPosition(mFGSurfaceControl, 64, 64);
1548 });
Robert Carr9524cb32017-02-13 11:32:32 -08001549
1550 {
1551 ScreenCapture::captureScreen(&mCapture);
1552 // Top left of foreground must now be visible
1553 mCapture->expectFGColor(64, 64);
1554 // But 10 pixels in we should see the child surface
1555 mCapture->expectChildColor(74, 74);
1556 // And 10 more pixels we should be back to the foreground surface
1557 mCapture->expectFGColor(84, 84);
1558 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001559
1560 asTransaction([&](Transaction& t) {
1561 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1562 });
1563
Robert Carr9524cb32017-02-13 11:32:32 -08001564 {
1565 ScreenCapture::captureScreen(&mCapture);
1566 mCapture->expectFGColor(64, 64);
1567 // In reparenting we should have exposed the entire foreground surface.
1568 mCapture->expectFGColor(74, 74);
1569 // And the child layer should now begin at 10, 10 (since the BG
1570 // layer is at (0, 0)).
1571 mCapture->expectBGColor(9, 9);
1572 mCapture->expectChildColor(10, 10);
1573 }
1574}
1575
chaviw161410b02017-07-27 10:46:08 -07001576TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001577 asTransaction([&](Transaction& t) {
1578 t.show(mChild);
1579 t.setPosition(mChild, 10, 10);
1580 t.setPosition(mFGSurfaceControl, 64, 64);
1581 });
Robert Carr9524cb32017-02-13 11:32:32 -08001582
1583 {
1584 ScreenCapture::captureScreen(&mCapture);
1585 // Top left of foreground must now be visible
1586 mCapture->expectFGColor(64, 64);
1587 // But 10 pixels in we should see the child surface
1588 mCapture->expectChildColor(74, 74);
1589 // And 10 more pixels we should be back to the foreground surface
1590 mCapture->expectFGColor(84, 84);
1591 }
1592
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001593 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001594
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001595 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001596
chaviw161410b02017-07-27 10:46:08 -07001597 // Since the child has the same client as the parent, it will not get
1598 // detached and will be hidden.
1599 {
1600 ScreenCapture::captureScreen(&mCapture);
1601 mCapture->expectFGColor(64, 64);
1602 mCapture->expectFGColor(74, 74);
1603 mCapture->expectFGColor(84, 84);
1604 }
1605}
1606
1607TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1608 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001609 sp<SurfaceControl> mChildNewClient =
1610 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1611 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001612
1613 ASSERT_TRUE(mChildNewClient != NULL);
1614 ASSERT_TRUE(mChildNewClient->isValid());
1615
1616 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1617
Robert Carr4cdc58f2017-08-23 14:22:20 -07001618 asTransaction([&](Transaction& t) {
1619 t.hide(mChild);
1620 t.show(mChildNewClient);
1621 t.setPosition(mChildNewClient, 10, 10);
1622 t.setPosition(mFGSurfaceControl, 64, 64);
1623 });
chaviw161410b02017-07-27 10:46:08 -07001624
1625 {
1626 ScreenCapture::captureScreen(&mCapture);
1627 // Top left of foreground must now be visible
1628 mCapture->expectFGColor(64, 64);
1629 // But 10 pixels in we should see the child surface
1630 mCapture->expectChildColor(74, 74);
1631 // And 10 more pixels we should be back to the foreground surface
1632 mCapture->expectFGColor(84, 84);
1633 }
1634
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001635 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001636
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001637 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001638
Robert Carr9524cb32017-02-13 11:32:32 -08001639 // Nothing should have changed.
1640 {
1641 ScreenCapture::captureScreen(&mCapture);
1642 mCapture->expectFGColor(64, 64);
1643 mCapture->expectChildColor(74, 74);
1644 mCapture->expectFGColor(84, 84);
1645 }
1646}
1647
Robert Carr9b429f42017-04-17 14:56:57 -07001648TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001649 asTransaction([&](Transaction& t) {
1650 t.show(mChild);
1651 t.setPosition(mChild, 0, 0);
1652 t.setPosition(mFGSurfaceControl, 0, 0);
1653 });
Robert Carr9b429f42017-04-17 14:56:57 -07001654
1655 {
1656 ScreenCapture::captureScreen(&mCapture);
1657 // We've positioned the child in the top left.
1658 mCapture->expectChildColor(0, 0);
1659 // But it's only 10x10.
1660 mCapture->expectFGColor(10, 10);
1661 }
1662
Robert Carr4cdc58f2017-08-23 14:22:20 -07001663 asTransaction([&](Transaction& t) {
1664 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1665 // We cause scaling by 2.
1666 t.setSize(mFGSurfaceControl, 128, 128);
1667 });
Robert Carr9b429f42017-04-17 14:56:57 -07001668
1669 {
1670 ScreenCapture::captureScreen(&mCapture);
1671 // We've positioned the child in the top left.
1672 mCapture->expectChildColor(0, 0);
1673 mCapture->expectChildColor(10, 10);
1674 mCapture->expectChildColor(19, 19);
1675 // And now it should be scaled all the way to 20x20
1676 mCapture->expectFGColor(20, 20);
1677 }
1678}
1679
Robert Carr1725eee2017-04-26 18:32:15 -07001680// Regression test for b/37673612
1681TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001682 asTransaction([&](Transaction& t) {
1683 t.show(mChild);
1684 t.setPosition(mChild, 0, 0);
1685 t.setPosition(mFGSurfaceControl, 0, 0);
1686 });
Robert Carr1725eee2017-04-26 18:32:15 -07001687
1688 {
1689 ScreenCapture::captureScreen(&mCapture);
1690 // We've positioned the child in the top left.
1691 mCapture->expectChildColor(0, 0);
1692 // But it's only 10x10.
1693 mCapture->expectFGColor(10, 10);
1694 }
Robert Carr1725eee2017-04-26 18:32:15 -07001695 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1696 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001697 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001698 sp<Surface> s = mFGSurfaceControl->getSurface();
1699 auto anw = static_cast<ANativeWindow*>(s.get());
1700 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1701 native_window_set_buffers_dimensions(anw, 64, 128);
1702 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1703 waitForPostedBuffers();
1704
1705 {
1706 // The child should still be in the same place and not have any strange scaling as in
1707 // b/37673612.
1708 ScreenCapture::captureScreen(&mCapture);
1709 mCapture->expectChildColor(0, 0);
1710 mCapture->expectFGColor(10, 10);
1711 }
1712}
1713
Dan Stoza412903f2017-04-27 13:42:17 -07001714TEST_F(ChildLayerTest, Bug36858924) {
1715 // Destroy the child layer
1716 mChild.clear();
1717
1718 // Now recreate it as hidden
1719 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1720 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1721 mFGSurfaceControl.get());
1722
1723 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001724 asTransaction([&](Transaction& t) {
1725 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001726 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001727 t.show(mChild);
1728 });
Dan Stoza412903f2017-04-27 13:42:17 -07001729
1730 // Render the foreground surface a few times
1731 //
1732 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1733 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1734 // never acquire/release the first buffer
1735 ALOGI("Filling 1");
1736 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1737 ALOGI("Filling 2");
1738 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1739 ALOGI("Filling 3");
1740 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1741 ALOGI("Filling 4");
1742 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1743}
1744
chaviwf1961f72017-09-18 16:41:07 -07001745TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001746 asTransaction([&](Transaction& t) {
1747 t.show(mChild);
1748 t.setPosition(mChild, 10, 10);
1749 t.setPosition(mFGSurfaceControl, 64, 64);
1750 });
chaviw06178942017-07-27 10:25:59 -07001751
1752 {
1753 ScreenCapture::captureScreen(&mCapture);
1754 // Top left of foreground must now be visible
1755 mCapture->expectFGColor(64, 64);
1756 // But 10 pixels in we should see the child surface
1757 mCapture->expectChildColor(74, 74);
1758 // And 10 more pixels we should be back to the foreground surface
1759 mCapture->expectFGColor(84, 84);
1760 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001761
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001762 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001763
chaviw06178942017-07-27 10:25:59 -07001764 {
1765 ScreenCapture::captureScreen(&mCapture);
1766 mCapture->expectFGColor(64, 64);
1767 // In reparenting we should have exposed the entire foreground surface.
1768 mCapture->expectFGColor(74, 74);
1769 // And the child layer should now begin at 10, 10 (since the BG
1770 // layer is at (0, 0)).
1771 mCapture->expectBGColor(9, 9);
1772 mCapture->expectChildColor(10, 10);
1773 }
1774}
1775
chaviwf1961f72017-09-18 16:41:07 -07001776TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001777 asTransaction([&](Transaction& t) {
1778 t.show(mChild);
1779 t.setPosition(mChild, 10, 10);
1780 t.setPosition(mFGSurfaceControl, 64, 64);
1781 });
chaviwf1961f72017-09-18 16:41:07 -07001782
1783 {
1784 ScreenCapture::captureScreen(&mCapture);
1785 // Top left of foreground must now be visible
1786 mCapture->expectFGColor(64, 64);
1787 // But 10 pixels in we should see the child surface
1788 mCapture->expectChildColor(74, 74);
1789 // And 10 more pixels we should be back to the foreground surface
1790 mCapture->expectFGColor(84, 84);
1791 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001792 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07001793 {
1794 ScreenCapture::captureScreen(&mCapture);
1795 // Nothing should have changed.
1796 mCapture->expectFGColor(64, 64);
1797 mCapture->expectChildColor(74, 74);
1798 mCapture->expectFGColor(84, 84);
1799 }
1800}
1801
1802TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001803 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
1804 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07001805 ASSERT_TRUE(newSurface != NULL);
1806 ASSERT_TRUE(newSurface->isValid());
1807
1808 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001809 asTransaction([&](Transaction& t) {
1810 t.hide(mChild);
1811 t.show(newSurface);
1812 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001813 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001814 t.setPosition(mFGSurfaceControl, 64, 64);
1815 });
chaviwf1961f72017-09-18 16:41:07 -07001816
1817 {
1818 ScreenCapture::captureScreen(&mCapture);
1819 // Top left of foreground must now be visible
1820 mCapture->expectFGColor(64, 64);
1821 // At 10, 10 we should see the new surface
1822 mCapture->checkPixel(10, 10, 63, 195, 63);
1823 }
1824
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001825 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07001826
1827 {
1828 ScreenCapture::captureScreen(&mCapture);
1829 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
1830 // mFGSurface, putting it at 74, 74.
1831 mCapture->expectFGColor(64, 64);
1832 mCapture->checkPixel(74, 74, 63, 195, 63);
1833 mCapture->expectFGColor(84, 84);
1834 }
1835}
1836
chaviwc9674332017-08-28 12:32:18 -07001837TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001838 sp<SurfaceControl> grandchild =
1839 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
1840 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07001841 fillSurfaceRGBA8(grandchild, 50, 50, 50);
1842
1843 {
1844 ScreenCapture::captureScreen(&mCapture);
1845 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
1846 // which begins at 64, 64
1847 mCapture->checkPixel(64, 64, 50, 50, 50);
1848 }
1849}
1850
Robert Carr503c7042017-09-27 15:06:08 -07001851TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001852 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
1853 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07001854 fillSurfaceRGBA8(relative, 255, 255, 255);
1855
1856 Transaction t;
1857 t.setLayer(relative, INT32_MAX)
1858 .setRelativeLayer(mChild, relative->getHandle(), 1)
1859 .setPosition(mFGSurfaceControl, 0, 0)
1860 .apply(true);
1861
1862 // We expect that the child should have been elevated above our
1863 // INT_MAX layer even though it's not a child of it.
1864 {
1865 ScreenCapture::captureScreen(&mCapture);
1866 mCapture->expectChildColor(0, 0);
1867 mCapture->expectChildColor(9, 9);
1868 mCapture->checkPixel(10, 10, 255, 255, 255);
1869 }
1870}
1871
chaviw13fdc492017-06-27 12:40:18 -07001872class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001873protected:
chaviw13fdc492017-06-27 12:40:18 -07001874 void SetUp() override {
1875 LayerUpdateTest::SetUp();
1876
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001877 mLayerColorControl =
1878 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
1879 PIXEL_FORMAT_RGBA_8888,
1880 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07001881
1882 ASSERT_TRUE(mLayerColorControl != NULL);
1883 ASSERT_TRUE(mLayerColorControl->isValid());
1884
Robert Carr4cdc58f2017-08-23 14:22:20 -07001885 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001886 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001887 t.setPosition(mLayerColorControl, 140, 140);
1888 t.hide(mLayerColorControl);
1889 t.hide(mFGSurfaceControl);
1890 });
chaviw13fdc492017-06-27 12:40:18 -07001891 }
1892
1893 void TearDown() override {
1894 LayerUpdateTest::TearDown();
1895 mLayerColorControl = 0;
1896 }
1897
1898 sp<SurfaceControl> mLayerColorControl;
1899};
1900
1901TEST_F(LayerColorTest, ColorLayerNoAlpha) {
1902 sp<ScreenCapture> sc;
1903
1904 {
1905 SCOPED_TRACE("before setColor");
1906 ScreenCapture::captureScreen(&sc);
1907 sc->expectBGColor(145, 145);
1908 }
1909
Robert Carr4cdc58f2017-08-23 14:22:20 -07001910 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001911 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001912 t.setColor(mLayerColorControl, color);
1913 t.show(mLayerColorControl);
1914 });
chaviw13fdc492017-06-27 12:40:18 -07001915
chaviw13fdc492017-06-27 12:40:18 -07001916 {
1917 // There should now be a color
1918 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07001919
chaviw13fdc492017-06-27 12:40:18 -07001920 ScreenCapture::captureScreen(&sc);
1921 sc->checkPixel(145, 145, 43, 207, 131);
1922 }
1923}
1924
1925TEST_F(LayerColorTest, ColorLayerWithAlpha) {
1926 sp<ScreenCapture> sc;
1927 {
1928 SCOPED_TRACE("before setColor");
1929 ScreenCapture::captureScreen(&sc);
1930 sc->expectBGColor(145, 145);
1931 }
1932
Robert Carr4cdc58f2017-08-23 14:22:20 -07001933 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001934 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001935 t.setColor(mLayerColorControl, color);
1936 t.setAlpha(mLayerColorControl, .75f);
1937 t.show(mLayerColorControl);
1938 });
1939
chaviw13fdc492017-06-27 12:40:18 -07001940 {
1941 // There should now be a color with .75 alpha
1942 SCOPED_TRACE("after setColor");
1943 ScreenCapture::captureScreen(&sc);
1944 sc->checkPixel(145, 145, 48, 171, 147);
1945 }
1946}
1947
1948TEST_F(LayerColorTest, ColorLayerWithNoColor) {
1949 sp<ScreenCapture> sc;
1950 {
1951 SCOPED_TRACE("before setColor");
1952 ScreenCapture::captureScreen(&sc);
1953 sc->expectBGColor(145, 145);
1954 }
1955
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001956 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001957
chaviw13fdc492017-06-27 12:40:18 -07001958 {
1959 // There should now be set to 0,0,0 (black) as default.
1960 SCOPED_TRACE("after setColor");
1961 ScreenCapture::captureScreen(&sc);
1962 sc->checkPixel(145, 145, 0, 0, 0);
1963 }
1964}
1965
chaviwa76b2712017-09-20 12:02:26 -07001966class ScreenCaptureTest : public LayerUpdateTest {
1967protected:
1968 std::unique_ptr<CaptureLayer> mCapture;
1969};
1970
1971TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
1972 auto bgHandle = mBGSurfaceControl->getHandle();
1973 CaptureLayer::captureScreen(&mCapture, bgHandle);
1974 mCapture->expectBGColor(0, 0);
1975 // Doesn't capture FG layer which is at 64, 64
1976 mCapture->expectBGColor(64, 64);
1977}
1978
1979TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
1980 auto fgHandle = mFGSurfaceControl->getHandle();
1981
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001982 sp<SurfaceControl> child =
1983 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
1984 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07001985 fillSurfaceRGBA8(child, 200, 200, 200);
1986
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001987 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07001988
1989 // Captures mFGSurfaceControl layer and its child.
1990 CaptureLayer::captureScreen(&mCapture, fgHandle);
1991 mCapture->expectFGColor(10, 10);
1992 mCapture->expectChildColor(0, 0);
1993}
1994
1995TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
1996 auto fgHandle = mFGSurfaceControl->getHandle();
1997
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001998 sp<SurfaceControl> child =
1999 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2000 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002001 fillSurfaceRGBA8(child, 200, 200, 200);
2002
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002003 sp<SurfaceControl> grandchild =
2004 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2005 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002006
2007 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2008 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002009 .show(child)
2010 .setPosition(grandchild, 5, 5)
2011 .show(grandchild)
2012 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002013
2014 // Captures mFGSurfaceControl, its child, and the grandchild.
2015 CaptureLayer::captureScreen(&mCapture, fgHandle);
2016 mCapture->expectFGColor(10, 10);
2017 mCapture->expectChildColor(0, 0);
2018 mCapture->checkPixel(5, 5, 50, 50, 50);
2019}
2020
2021TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002022 sp<SurfaceControl> child =
2023 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2024 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002025 fillSurfaceRGBA8(child, 200, 200, 200);
2026 auto childHandle = child->getHandle();
2027
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002028 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002029
2030 // Captures only the child layer, and not the parent.
2031 CaptureLayer::captureScreen(&mCapture, childHandle);
2032 mCapture->expectChildColor(0, 0);
2033 mCapture->expectChildColor(9, 9);
2034}
2035
2036TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002037 sp<SurfaceControl> child =
2038 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2039 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002040 fillSurfaceRGBA8(child, 200, 200, 200);
2041 auto childHandle = child->getHandle();
2042
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002043 sp<SurfaceControl> grandchild =
2044 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2045 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002046 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2047
2048 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002049 .show(child)
2050 .setPosition(grandchild, 5, 5)
2051 .show(grandchild)
2052 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002053
2054 auto grandchildHandle = grandchild->getHandle();
2055
2056 // Captures only the grandchild.
2057 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2058 mCapture->checkPixel(0, 0, 50, 50, 50);
2059 mCapture->checkPixel(4, 4, 50, 50, 50);
2060}
2061
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002062} // namespace android