blob: a0c190bbbf9594e10d425811d16fe1d36c0cc63c [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
18#include <functional>
19#include <limits>
20#include <ostream>
21
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070022#include <gtest/gtest.h>
23
Michael Lentine5a16a622015-05-21 13:48:24 -070024#include <android/native_window.h>
25
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070027#include <gui/LayerState.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
31#include <private/gui/ComposerService.h>
32
Mathias Agopianc666cae2012-07-25 18:56:13 -070033#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070034#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070035#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070036
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070037#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070038#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070039
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040namespace android {
41
Chia-I Wu718daf82017-10-20 11:57:17 -070042namespace {
43
44struct Color {
45 uint8_t r;
46 uint8_t g;
47 uint8_t b;
48 uint8_t a;
49
50 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070051 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070052 static const Color BLUE;
Chia-I Wu718daf82017-10-20 11:57:17 -070053 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070054 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070055};
56
57const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070058const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070059const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070060const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070061const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070062
63std::ostream& operator<<(std::ostream& os, const Color& color) {
64 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
65 return os;
66}
67
68// Fill a region with the specified color.
69void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
70 int32_t x = rect.left;
71 int32_t y = rect.top;
72 int32_t width = rect.right - rect.left;
73 int32_t height = rect.bottom - rect.top;
74
75 if (x < 0) {
76 width += x;
77 x = 0;
78 }
79 if (y < 0) {
80 height += y;
81 y = 0;
82 }
83 if (x + width > buffer.width) {
84 x = std::min(x, buffer.width);
85 width = buffer.width - x;
86 }
87 if (y + height > buffer.height) {
88 y = std::min(y, buffer.height);
89 height = buffer.height - y;
90 }
91
92 for (int32_t j = 0; j < height; j++) {
93 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
94 for (int32_t i = 0; i < width; i++) {
95 dst[0] = color.r;
96 dst[1] = color.g;
97 dst[2] = color.b;
98 dst[3] = color.a;
99 dst += 4;
100 }
101 }
102}
103
104// Check if a region has the specified color.
105void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect,
106 const Color& color, uint8_t tolerance) {
107 int32_t x = rect.left;
108 int32_t y = rect.top;
109 int32_t width = rect.right - rect.left;
110 int32_t height = rect.bottom - rect.top;
111
112 if (x + width > int32_t(buffer.width)) {
113 x = std::min(x, int32_t(buffer.width));
114 width = buffer.width - x;
115 }
116 if (y + height > int32_t(buffer.height)) {
117 y = std::min(y, int32_t(buffer.height));
118 height = buffer.height - y;
119 }
120
121 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
122 uint8_t tmp = a >= b ? a - b : b - a;
123 return tmp <= tolerance;
124 };
125 for (int32_t j = 0; j < height; j++) {
126 const uint8_t* src =
127 static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4;
128 for (int32_t i = 0; i < width; i++) {
129 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
130 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
131 << "pixel @ (" << x + i << ", " << y + j << "): "
132 << "expected (" << color << "), "
133 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
134 src += 4;
135 }
136 }
137}
138
139} // anonymous namespace
140
Robert Carr4cdc58f2017-08-23 14:22:20 -0700141using Transaction = SurfaceComposerClient::Transaction;
142
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700143// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700144static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
145 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800146 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700147 sp<Surface> s = sc->getSurface();
148 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800149 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
150 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700151 for (int y = 0; y < outBuffer.height; y++) {
152 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700153 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700154 pixel[0] = r;
155 pixel[1] = g;
156 pixel[2] = b;
157 pixel[3] = 255;
158 }
159 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700160 if (unlock) {
161 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
162 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700163}
164
165// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
166// individual pixel values for testing purposes.
167class ScreenCapture : public RefBase {
168public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700169 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
170 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700171 sp<IGraphicBufferProducer> producer;
172 sp<IGraphicBufferConsumer> consumer;
173 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -0700174 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700175 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700176 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700177 SurfaceComposerClient::Transaction().apply(true);
178
Chia-I Wu718daf82017-10-20 11:57:17 -0700179 ASSERT_EQ(NO_ERROR,
180 sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false));
Michael Lentine5a16a622015-05-21 13:48:24 -0700181 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700182 }
183
Chia-I Wu718daf82017-10-20 11:57:17 -0700184 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
185 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
186 expectBufferColor(mBuf, rect, color, tolerance);
187 }
188
189 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
190 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
191 const bool leftBorder = rect.left > 0;
192 const bool topBorder = rect.top > 0;
193 const bool rightBorder = rect.right < int32_t(mBuf.width);
194 const bool bottomBorder = rect.bottom < int32_t(mBuf.height);
195
196 if (topBorder) {
197 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
198 if (leftBorder) {
199 top.left -= 1;
200 }
201 if (rightBorder) {
202 top.right += 1;
203 }
204 expectColor(top, color, tolerance);
205 }
206 if (leftBorder) {
207 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
208 expectColor(left, color, tolerance);
209 }
210 if (rightBorder) {
211 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
212 expectColor(right, color, tolerance);
213 }
214 if (bottomBorder) {
215 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
216 if (leftBorder) {
217 bottom.left -= 1;
218 }
219 if (rightBorder) {
220 bottom.right += 1;
221 }
222 expectColor(bottom, color, tolerance);
223 }
224 }
225
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700226 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700227 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
228 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
229 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700230 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
231 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700232 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
233 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700234 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700235 }
236 }
237
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700238 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700239
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700240 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700241
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700242 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700243
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700244private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700245 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700246 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
247 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700248
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700249 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700250
251 sp<CpuConsumer> mCC;
252 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700253};
254
chaviwa76b2712017-09-20 12:02:26 -0700255class CaptureLayer {
256public:
257 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
258 sp<IGraphicBufferProducer> producer;
259 sp<IGraphicBufferConsumer> consumer;
260 BufferQueue::createBufferQueue(&producer, &consumer);
261 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
262 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700263 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700264 SurfaceComposerClient::Transaction().apply(true);
265 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
266 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
267 }
268
269 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
270 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
271 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
272 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
273 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
274 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700275 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700276 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
277 EXPECT_EQ(String8(), err) << err.string();
278 }
279 }
280
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700281 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700282
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700283 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700284
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700285 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700286
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700287 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700288 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
289 }
290
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700291 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700292
293private:
294 sp<CpuConsumer> mCC;
295 CpuConsumer::LockedBuffer mBuffer;
296};
297
Chia-I Wu718daf82017-10-20 11:57:17 -0700298class LayerTransactionTest : public ::testing::Test {
299protected:
300 void SetUp() override {
301 mClient = new SurfaceComposerClient;
302 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
303
304 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
305 }
306
307 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
308 uint32_t flags = 0) {
309 auto layer =
310 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
311 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
312
313 status_t error = Transaction()
314 .setLayerStack(layer, mDisplayLayerStack)
315 .setLayer(layer, mLayerZBase)
316 .apply();
317 if (error != NO_ERROR) {
318 ADD_FAILURE() << "failed to initialize SurfaceControl";
319 layer.clear();
320 }
321
322 return layer;
323 }
324
325 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
326 // wait for previous transactions (such as setSize) to complete
327 Transaction().apply(true);
328
329 ANativeWindow_Buffer buffer = {};
330 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
331
332 return buffer;
333 }
334
335 void postLayerBuffer(const sp<SurfaceControl>& layer) {
336 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
337
338 // wait for the newly posted buffer to be latched
339 waitForLayerBuffers();
340 }
341
342 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
343 ANativeWindow_Buffer buffer;
344 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
345 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
346 postLayerBuffer(layer);
347 }
348
349 sp<ScreenCapture> screenshot() {
350 sp<ScreenCapture> screenshot;
351 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
352 return screenshot;
353 }
354
355 sp<SurfaceComposerClient> mClient;
356
357 sp<IBinder> mDisplay;
358 uint32_t mDisplayWidth;
359 uint32_t mDisplayHeight;
360 uint32_t mDisplayLayerStack;
361
362 // leave room for ~256 layers
363 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
364
365private:
366 void SetUpDisplay() {
367 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
368 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
369
370 // get display width/height
371 DisplayInfo info;
372 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
373 mDisplayWidth = info.w;
374 mDisplayHeight = info.h;
375
376 // After a new buffer is queued, SurfaceFlinger is notified and will
377 // latch the new buffer on next vsync. Let's heuristically wait for 3
378 // vsyncs.
379 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
380
381 mDisplayLayerStack = 0;
382 // set layer stack (b/68888219)
383 Transaction t;
384 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
385 t.apply();
386 }
387
388 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
389
390 int32_t mBufferPostDelay;
391};
392
393TEST_F(LayerTransactionTest, SetPositionBasic) {
394 sp<SurfaceControl> layer;
395 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
396 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
397
398 {
399 SCOPED_TRACE("default position");
400 auto shot = screenshot();
401 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
402 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
403 }
404
405 Transaction().setPosition(layer, 5, 10).apply();
406 {
407 SCOPED_TRACE("new position");
408 auto shot = screenshot();
409 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
410 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
411 }
412}
413
414TEST_F(LayerTransactionTest, SetPositionRounding) {
415 sp<SurfaceControl> layer;
416 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
417 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
418
419 // GLES requires only 4 bits of subpixel precision during rasterization
420 // XXX GLES composition does not match HWC composition due to precision
421 // loss (b/69315223)
422 const float epsilon = 1.0f / 16.0f;
423 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
424 {
425 SCOPED_TRACE("rounding down");
426 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
427 }
428
429 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
430 {
431 SCOPED_TRACE("rounding up");
432 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
433 }
434}
435
436TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
437 sp<SurfaceControl> layer;
438 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
439 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
440
441 Transaction().setPosition(layer, -32, -32).apply();
442 {
443 SCOPED_TRACE("negative coordinates");
444 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
445 }
446
447 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
448 {
449 SCOPED_TRACE("positive coordinates");
450 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
451 }
452}
453
454TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
455 sp<SurfaceControl> layer;
456 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
457 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
458
459 // partially out of bounds
460 Transaction().setPosition(layer, -30, -30).apply();
461 {
462 SCOPED_TRACE("negative coordinates");
463 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
464 }
465
466 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
467 {
468 SCOPED_TRACE("positive coordinates");
469 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
470 mDisplayHeight),
471 Color::RED);
472 }
473}
474
475TEST_F(LayerTransactionTest, SetPositionWithResize) {
476 sp<SurfaceControl> layer;
477 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
478 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
479
480 // setPosition is applied immediately by default, with or without resize
481 // pending
482 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
483 {
484 SCOPED_TRACE("resize pending");
485 auto shot = screenshot();
486 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
487 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
488 }
489
490 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
491 {
492 SCOPED_TRACE("resize applied");
493 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
494 }
495}
496
497TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
498 sp<SurfaceControl> layer;
499 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
500 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
501
502 // request setPosition to be applied with the next resize
503 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
504 {
505 SCOPED_TRACE("new position pending");
506 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
507 }
508
509 Transaction().setPosition(layer, 15, 20).apply();
510 {
511 SCOPED_TRACE("pending new position modified");
512 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
513 }
514
515 Transaction().setSize(layer, 64, 64).apply();
516 {
517 SCOPED_TRACE("resize pending");
518 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
519 }
520
521 // finally resize and latch the buffer
522 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
523 {
524 SCOPED_TRACE("new position applied");
525 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
526 }
527}
528
529TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
530 sp<SurfaceControl> layer;
531 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
532 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
533
534 // setPosition is not immediate even with SCALE_TO_WINDOW override
535 Transaction()
536 .setPosition(layer, 5, 10)
537 .setSize(layer, 64, 64)
538 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
539 .setGeometryAppliesWithResize(layer)
540 .apply();
541 {
542 SCOPED_TRACE("new position pending");
543 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
544 }
545
546 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
547 {
548 SCOPED_TRACE("new position applied");
549 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
550 }
551}
552
Chia-I Wu0eaea312017-10-31 10:14:40 -0700553TEST_F(LayerTransactionTest, SetSizeBasic) {
554 sp<SurfaceControl> layer;
555 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
556 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
557
558 Transaction().setSize(layer, 64, 64).apply();
559 {
560 SCOPED_TRACE("resize pending");
561 auto shot = screenshot();
562 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
563 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
564 }
565
566 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
567 {
568 SCOPED_TRACE("resize applied");
569 auto shot = screenshot();
570 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
571 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
572 }
573}
574
575TEST_F(LayerTransactionTest, SetSizeInvalid) {
576 // cannot test robustness against invalid sizes (zero or really huge)
577}
578
579TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
580 sp<SurfaceControl> layer;
581 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
582 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
583
584 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
585 Transaction()
586 .setSize(layer, 64, 64)
587 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
588 .apply();
589 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
590}
591
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700592TEST_F(LayerTransactionTest, SetZBasic) {
593 sp<SurfaceControl> layerR;
594 sp<SurfaceControl> layerG;
595 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
596 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
597 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
598 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
599
600 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
601 {
602 SCOPED_TRACE("layerR");
603 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
604 }
605
606 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
607 {
608 SCOPED_TRACE("layerG");
609 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
610 }
611}
612
613TEST_F(LayerTransactionTest, SetZNegative) {
614 sp<SurfaceControl> layerR;
615 sp<SurfaceControl> layerG;
616 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
617 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
618 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
619 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
620
621 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
622 {
623 SCOPED_TRACE("layerR");
624 sp<ScreenCapture> screenshot;
625 ScreenCapture::captureScreen(&screenshot, -2, -1);
626 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
627 }
628
629 Transaction().setLayer(layerR, -3).apply();
630 {
631 SCOPED_TRACE("layerG");
632 sp<ScreenCapture> screenshot;
633 ScreenCapture::captureScreen(&screenshot, -3, -1);
634 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
635 }
636}
637
Chia-I Wu49313302017-10-31 10:14:40 -0700638TEST_F(LayerTransactionTest, SetRelativeZBasic) {
639 sp<SurfaceControl> layerR;
640 sp<SurfaceControl> layerG;
641 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
642 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
643 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
644 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
645
646 Transaction()
647 .setPosition(layerG, 16, 16)
648 .setRelativeLayer(layerG, layerR->getHandle(), 1)
649 .apply();
650 {
651 SCOPED_TRACE("layerG above");
652 auto shot = screenshot();
653 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
654 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
655 }
656
657 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
658 {
659 SCOPED_TRACE("layerG below");
660 auto shot = screenshot();
661 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
662 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
663 }
664}
665
666TEST_F(LayerTransactionTest, SetRelativeZGroup) {
667 sp<SurfaceControl> layerR;
668 sp<SurfaceControl> layerG;
669 sp<SurfaceControl> layerB;
670 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
671 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
672 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
673 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
674 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
675 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
676
677 // layerR = 0, layerG = layerR + 3, layerB = 2
678 Transaction()
679 .setPosition(layerG, 8, 8)
680 .setRelativeLayer(layerG, layerR->getHandle(), 3)
681 .setPosition(layerB, 16, 16)
682 .setLayer(layerB, mLayerZBase + 2)
683 .apply();
684 {
685 SCOPED_TRACE("(layerR < layerG) < layerB");
686 auto shot = screenshot();
687 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
688 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
689 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
690 }
691
692 // layerR = 4, layerG = layerR + 3, layerB = 2
693 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
694 {
695 SCOPED_TRACE("layerB < (layerR < layerG)");
696 auto shot = screenshot();
697 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
698 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
699 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
700 }
701
702 // layerR = 4, layerG = layerR - 3, layerB = 2
703 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
704 {
705 SCOPED_TRACE("layerB < (layerG < layerR)");
706 auto shot = screenshot();
707 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
708 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
709 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
710 }
711
712 // restore to absolute z
713 // layerR = 4, layerG = 0, layerB = 2
714 Transaction().setLayer(layerG, mLayerZBase).apply();
715 {
716 SCOPED_TRACE("layerG < layerB < layerR");
717 auto shot = screenshot();
718 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
719 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
720 }
721
722 // layerR should not affect layerG anymore
723 // layerR = 1, layerG = 0, layerB = 2
724 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
725 {
726 SCOPED_TRACE("layerG < layerR < layerB");
727 auto shot = screenshot();
728 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
729 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
730 }
731}
732
733TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
734 sp<SurfaceControl> layerR;
735 sp<SurfaceControl> layerG;
736
737 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
738 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
739 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
740 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
741
742 Transaction()
743 .setPosition(layerG, 16, 16)
744 .setRelativeLayer(layerG, layerR->getHandle(), 1)
745 .apply();
746
747 mClient->destroySurface(layerG->getHandle());
748 // layerG should have been removed
749 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
750}
751
Chia-I Wu57b27502017-10-31 10:14:40 -0700752TEST_F(LayerTransactionTest, SetFlagsHidden) {
753 sp<SurfaceControl> layer;
754 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
755 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
756
757 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
758 {
759 SCOPED_TRACE("layer hidden");
760 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
761 }
762
763 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
764 {
765 SCOPED_TRACE("layer shown");
766 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
767 }
768}
769
770TEST_F(LayerTransactionTest, SetFlagsOpaque) {
771 const Color translucentRed = {100, 0, 0, 100};
772 sp<SurfaceControl> layerR;
773 sp<SurfaceControl> layerG;
774 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
775 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
776 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
777 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
778
779 Transaction()
780 .setLayer(layerR, mLayerZBase + 1)
781 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
782 .apply();
783 {
784 SCOPED_TRACE("layerR opaque");
785 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
786 }
787
788 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
789 {
790 SCOPED_TRACE("layerR translucent");
791 const uint8_t g = uint8_t(255 - translucentRed.a);
792 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
793 }
794}
795
796TEST_F(LayerTransactionTest, SetFlagsSecure) {
797 sp<SurfaceControl> layer;
798 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
799 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
800
801 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
802 sp<IGraphicBufferProducer> producer;
803 sp<IGraphicBufferConsumer> consumer;
804 BufferQueue::createBufferQueue(&producer, &consumer);
805 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
806
807 Transaction()
808 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
809 .apply(true);
810 ASSERT_EQ(PERMISSION_DENIED,
811 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
812 false));
813
814 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
815 ASSERT_EQ(NO_ERROR,
816 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
817 false));
818}
819
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700820TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
821 const Rect top(0, 0, 32, 16);
822 const Rect bottom(0, 16, 32, 32);
823 sp<SurfaceControl> layer;
824 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
825
826 ANativeWindow_Buffer buffer;
827 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
828 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
829 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
830 // setTransparentRegionHint always applies to the following buffer
831 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
832 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
833 {
834 SCOPED_TRACE("top transparent");
835 auto shot = screenshot();
836 shot->expectColor(top, Color::BLACK);
837 shot->expectColor(bottom, Color::RED);
838 }
839
840 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
841 {
842 SCOPED_TRACE("transparent region hint pending");
843 auto shot = screenshot();
844 shot->expectColor(top, Color::BLACK);
845 shot->expectColor(bottom, Color::RED);
846 }
847
848 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
849 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
850 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
851 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
852 {
853 SCOPED_TRACE("bottom transparent");
854 auto shot = screenshot();
855 shot->expectColor(top, Color::RED);
856 shot->expectColor(bottom, Color::BLACK);
857 }
858}
859
860TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
861 sp<SurfaceControl> layerTransparent;
862 sp<SurfaceControl> layerR;
863 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
864 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
865
866 // check that transparent region hint is bound by the layer size
867 Transaction()
868 .setTransparentRegionHint(layerTransparent,
869 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
870 .setPosition(layerR, 16, 16)
871 .setLayer(layerR, mLayerZBase + 1)
872 .apply();
873 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
874 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
875 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
876}
877
Chia-I Wua8a515e2017-11-01 15:16:35 -0700878TEST_F(LayerTransactionTest, SetAlphaBasic) {
879 sp<SurfaceControl> layer1;
880 sp<SurfaceControl> layer2;
881 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
882 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
883 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
884 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
885
886 Transaction()
887 .setAlpha(layer1, 0.25f)
888 .setAlpha(layer2, 0.75f)
889 .setPosition(layer2, 16, 0)
890 .setLayer(layer2, mLayerZBase + 1)
891 .apply();
892 {
893 auto shot = screenshot();
894 uint8_t r = 16; // 64 * 0.25f
895 uint8_t g = 48; // 64 * 0.75f
896 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
897 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
898
899 r /= 4; // r * (1.0f - 0.75f)
900 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
901 }
902}
903
904TEST_F(LayerTransactionTest, SetAlphaClamped) {
905 const Color color = {64, 0, 0, 255};
906 sp<SurfaceControl> layer;
907 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
908 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
909
910 Transaction().setAlpha(layer, 2.0f).apply();
911 {
912 SCOPED_TRACE("clamped to 1.0f");
913 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
914 }
915
916 Transaction().setAlpha(layer, -1.0f).apply();
917 {
918 SCOPED_TRACE("clamped to 0.0f");
919 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
920 }
921}
922
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700923class LayerUpdateTest : public ::testing::Test {
924protected:
925 virtual void SetUp() {
926 mComposerClient = new SurfaceComposerClient;
927 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
928
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700929 sp<IBinder> display(
930 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700931 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700932 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700933
934 ssize_t displayWidth = info.w;
935 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700936
937 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700938 mBGSurfaceControl =
939 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
940 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700941 ASSERT_TRUE(mBGSurfaceControl != NULL);
942 ASSERT_TRUE(mBGSurfaceControl->isValid());
943 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
944
945 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700946 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
947 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700948 ASSERT_TRUE(mFGSurfaceControl != NULL);
949 ASSERT_TRUE(mFGSurfaceControl->isValid());
950
951 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
952
953 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700954 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
955 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700956 ASSERT_TRUE(mSyncSurfaceControl != NULL);
957 ASSERT_TRUE(mSyncSurfaceControl->isValid());
958
959 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
960
Robert Carr4cdc58f2017-08-23 14:22:20 -0700961 asTransaction([&](Transaction& t) {
962 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700963
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700964 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700965
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700966 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
967 .setPosition(mFGSurfaceControl, 64, 64)
968 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700969
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700970 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
971 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
972 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700973 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700974 }
975
976 virtual void TearDown() {
977 mComposerClient->dispose();
978 mBGSurfaceControl = 0;
979 mFGSurfaceControl = 0;
980 mSyncSurfaceControl = 0;
981 mComposerClient = 0;
982 }
983
984 void waitForPostedBuffers() {
985 // Since the sync surface is in synchronous mode (i.e. double buffered)
986 // posting three buffers to it should ensure that at least two
987 // SurfaceFlinger::handlePageFlip calls have been made, which should
988 // guaranteed that a buffer posted to another Surface has been retired.
989 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
990 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
991 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
992 }
993
Robert Carr4cdc58f2017-08-23 14:22:20 -0700994 void asTransaction(const std::function<void(Transaction&)>& exec) {
995 Transaction t;
996 exec(t);
997 t.apply(true);
998 }
999
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001000 sp<SurfaceComposerClient> mComposerClient;
1001 sp<SurfaceControl> mBGSurfaceControl;
1002 sp<SurfaceControl> mFGSurfaceControl;
1003
1004 // This surface is used to ensure that the buffers posted to
1005 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1006 sp<SurfaceControl> mSyncSurfaceControl;
1007};
1008
Robert Carr7f619b22017-11-06 12:56:35 -08001009TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1010 sp<ScreenCapture> sc;
1011
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001012 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1013 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001014 fillSurfaceRGBA8(relative, 10, 10, 10);
1015 waitForPostedBuffers();
1016
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001017 Transaction{}
1018 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001019 .setPosition(relative, 64, 64)
1020 .apply();
1021
1022 {
1023 // The relative should be on top of the FG control.
1024 ScreenCapture::captureScreen(&sc);
1025 sc->checkPixel(64, 64, 10, 10, 10);
1026 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001027 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001028
1029 {
1030 // Nothing should change at this point.
1031 ScreenCapture::captureScreen(&sc);
1032 sc->checkPixel(64, 64, 10, 10, 10);
1033 }
1034
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001035 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001036
1037 {
1038 // Ensure that the relative was actually hidden, rather than
1039 // being left in the detached but visible state.
1040 ScreenCapture::captureScreen(&sc);
1041 sc->expectFGColor(64, 64);
1042 }
1043}
1044
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001045TEST_F(LayerUpdateTest, LayerMoveWorks) {
1046 sp<ScreenCapture> sc;
1047 {
1048 SCOPED_TRACE("before move");
1049 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001050 sc->expectBGColor(0, 12);
1051 sc->expectFGColor(75, 75);
1052 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001053 }
1054
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001055 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001056
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001057 {
1058 // This should reflect the new position, but not the new color.
1059 SCOPED_TRACE("after move, before redraw");
1060 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001061 sc->expectBGColor(24, 24);
1062 sc->expectBGColor(75, 75);
1063 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001064 }
1065
1066 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1067 waitForPostedBuffers();
1068 {
1069 // This should reflect the new position and the new color.
1070 SCOPED_TRACE("after redraw");
1071 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001072 sc->expectBGColor(24, 24);
1073 sc->expectBGColor(75, 75);
1074 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001075 }
1076}
1077
1078TEST_F(LayerUpdateTest, LayerResizeWorks) {
1079 sp<ScreenCapture> sc;
1080 {
1081 SCOPED_TRACE("before resize");
1082 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001083 sc->expectBGColor(0, 12);
1084 sc->expectFGColor(75, 75);
1085 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001086 }
1087
Steve Block9d453682011-12-20 16:23:08 +00001088 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001089 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001090 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001091 {
1092 // This should not reflect the new size or color because SurfaceFlinger
1093 // has not yet received a buffer of the correct size.
1094 SCOPED_TRACE("after resize, before redraw");
1095 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001096 sc->expectBGColor(0, 12);
1097 sc->expectFGColor(75, 75);
1098 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001099 }
1100
Steve Block9d453682011-12-20 16:23:08 +00001101 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001102 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1103 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001104 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001105 {
1106 // This should reflect the new size and the new color.
1107 SCOPED_TRACE("after redraw");
1108 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001109 sc->expectBGColor(24, 24);
1110 sc->checkPixel(75, 75, 63, 195, 63);
1111 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001112 }
1113}
1114
Haixia Shid5750962015-07-27 16:50:49 -07001115TEST_F(LayerUpdateTest, LayerCropWorks) {
1116 sp<ScreenCapture> sc;
1117 {
1118 SCOPED_TRACE("before crop");
1119 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001120 sc->expectBGColor(24, 24);
1121 sc->expectFGColor(75, 75);
1122 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001123 }
1124
Robert Carr4cdc58f2017-08-23 14:22:20 -07001125 asTransaction([&](Transaction& t) {
1126 Rect cropRect(16, 16, 32, 32);
1127 t.setCrop(mFGSurfaceControl, cropRect);
1128 });
Haixia Shid5750962015-07-27 16:50:49 -07001129 {
1130 // This should crop the foreground surface.
1131 SCOPED_TRACE("after crop");
1132 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001133 sc->expectBGColor(24, 24);
1134 sc->expectBGColor(75, 75);
1135 sc->expectFGColor(95, 80);
1136 sc->expectFGColor(80, 95);
1137 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001138 }
1139}
1140
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001141TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1142 sp<ScreenCapture> sc;
1143 {
1144 SCOPED_TRACE("before crop");
1145 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001146 sc->expectBGColor(24, 24);
1147 sc->expectFGColor(75, 75);
1148 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001149 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001150 asTransaction([&](Transaction& t) {
1151 Rect cropRect(16, 16, 32, 32);
1152 t.setFinalCrop(mFGSurfaceControl, cropRect);
1153 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001154 {
1155 // This should crop the foreground surface.
1156 SCOPED_TRACE("after crop");
1157 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001158 sc->expectBGColor(24, 24);
1159 sc->expectBGColor(75, 75);
1160 sc->expectBGColor(95, 80);
1161 sc->expectBGColor(80, 95);
1162 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001163 }
1164}
1165
Haixia Shid5750962015-07-27 16:50:49 -07001166TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1167 sp<ScreenCapture> sc;
1168 {
1169 SCOPED_TRACE("before setLayer");
1170 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001171 sc->expectBGColor(24, 24);
1172 sc->expectFGColor(75, 75);
1173 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001174 }
1175
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001176 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001177
Haixia Shid5750962015-07-27 16:50:49 -07001178 {
1179 // This should hide the foreground surface beneath the background.
1180 SCOPED_TRACE("after setLayer");
1181 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001182 sc->expectBGColor(24, 24);
1183 sc->expectBGColor(75, 75);
1184 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001185 }
1186}
1187
1188TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1189 sp<ScreenCapture> sc;
1190 {
1191 SCOPED_TRACE("before hide");
1192 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001193 sc->expectBGColor(24, 24);
1194 sc->expectFGColor(75, 75);
1195 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001196 }
1197
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001198 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001199
Haixia Shid5750962015-07-27 16:50:49 -07001200 {
1201 // This should hide the foreground surface.
1202 SCOPED_TRACE("after hide, before show");
1203 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001204 sc->expectBGColor(24, 24);
1205 sc->expectBGColor(75, 75);
1206 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001207 }
1208
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001209 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001210
Haixia Shid5750962015-07-27 16:50:49 -07001211 {
1212 // This should show the foreground surface.
1213 SCOPED_TRACE("after show");
1214 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001215 sc->expectBGColor(24, 24);
1216 sc->expectFGColor(75, 75);
1217 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001218 }
1219}
1220
1221TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1222 sp<ScreenCapture> sc;
1223 {
1224 SCOPED_TRACE("before setAlpha");
1225 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001226 sc->expectBGColor(24, 24);
1227 sc->expectFGColor(75, 75);
1228 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001229 }
1230
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001231 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001232
Haixia Shid5750962015-07-27 16:50:49 -07001233 {
1234 // This should set foreground to be 75% opaque.
1235 SCOPED_TRACE("after setAlpha");
1236 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001237 sc->expectBGColor(24, 24);
1238 sc->checkPixel(75, 75, 162, 63, 96);
1239 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001240 }
1241}
1242
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001243TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1244 sp<ScreenCapture> sc;
1245 {
1246 SCOPED_TRACE("before setLayerStack");
1247 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001248 sc->expectBGColor(24, 24);
1249 sc->expectFGColor(75, 75);
1250 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001251 }
1252
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001253 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001254 {
1255 // This should hide the foreground surface since it goes to a different
1256 // layer stack.
1257 SCOPED_TRACE("after setLayerStack");
1258 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001259 sc->expectBGColor(24, 24);
1260 sc->expectBGColor(75, 75);
1261 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001262 }
1263}
1264
1265TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1266 sp<ScreenCapture> sc;
1267 {
1268 SCOPED_TRACE("before setFlags");
1269 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001270 sc->expectBGColor(24, 24);
1271 sc->expectFGColor(75, 75);
1272 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001273 }
1274
Robert Carr4cdc58f2017-08-23 14:22:20 -07001275 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001276 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001277 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001278 {
1279 // This should hide the foreground surface
1280 SCOPED_TRACE("after setFlags");
1281 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001282 sc->expectBGColor(24, 24);
1283 sc->expectBGColor(75, 75);
1284 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001285 }
1286}
1287
1288TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1289 sp<ScreenCapture> sc;
1290 {
1291 SCOPED_TRACE("before setMatrix");
1292 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001293 sc->expectBGColor(24, 24);
1294 sc->expectFGColor(91, 96);
1295 sc->expectFGColor(96, 101);
1296 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001297 }
1298
Robert Carr4cdc58f2017-08-23 14:22:20 -07001299 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001300 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001301 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001302 {
1303 SCOPED_TRACE("after setMatrix");
1304 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001305 sc->expectBGColor(24, 24);
1306 sc->expectFGColor(91, 96);
1307 sc->expectBGColor(96, 91);
1308 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001309 }
1310}
1311
Robert Carr8d5227b2017-03-16 15:41:03 -07001312class GeometryLatchingTest : public LayerUpdateTest {
1313protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001314 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001315 SCOPED_TRACE(trace);
1316 ScreenCapture::captureScreen(&sc);
1317 // We find the leading edge of the FG surface.
1318 sc->expectFGColor(127, 127);
1319 sc->expectBGColor(128, 128);
1320 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001321
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001322 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001323
1324 void unlockFGBuffer() {
1325 sp<Surface> s = mFGSurfaceControl->getSurface();
1326 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1327 waitForPostedBuffers();
1328 }
1329
Robert Carr8d5227b2017-03-16 15:41:03 -07001330 void completeFGResize() {
1331 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1332 waitForPostedBuffers();
1333 }
1334 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001335 asTransaction([&](Transaction& t) {
1336 t.setSize(mFGSurfaceControl, 64, 64);
1337 t.setPosition(mFGSurfaceControl, 64, 64);
1338 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1339 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1340 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001341
1342 EXPECT_INITIAL_STATE("After restoring initial state");
1343 }
1344 sp<ScreenCapture> sc;
1345};
1346
1347TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1348 EXPECT_INITIAL_STATE("before anything");
1349
1350 // By default position can be updated even while
1351 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001352 asTransaction([&](Transaction& t) {
1353 t.setSize(mFGSurfaceControl, 32, 32);
1354 t.setPosition(mFGSurfaceControl, 100, 100);
1355 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001356
1357 {
1358 SCOPED_TRACE("After moving surface");
1359 ScreenCapture::captureScreen(&sc);
1360 // If we moved, the FG Surface should cover up what was previously BG
1361 // however if we didn't move the FG wouldn't be large enough now.
1362 sc->expectFGColor(163, 163);
1363 }
1364
1365 restoreInitialState();
1366
1367 // Now we repeat with setGeometryAppliesWithResize
1368 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001369 asTransaction([&](Transaction& t) {
1370 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1371 t.setSize(mFGSurfaceControl, 32, 32);
1372 t.setPosition(mFGSurfaceControl, 100, 100);
1373 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001374
1375 {
1376 SCOPED_TRACE("While resize is pending");
1377 ScreenCapture::captureScreen(&sc);
1378 // This time we shouldn't have moved, so the BG color
1379 // should still be visible.
1380 sc->expectBGColor(128, 128);
1381 }
1382
1383 completeFGResize();
1384
1385 {
1386 SCOPED_TRACE("After the resize");
1387 ScreenCapture::captureScreen(&sc);
1388 // But after the resize completes, we should move
1389 // and the FG should be visible here.
1390 sc->expectFGColor(128, 128);
1391 }
1392}
1393
1394class CropLatchingTest : public GeometryLatchingTest {
1395protected:
1396 void EXPECT_CROPPED_STATE(const char* trace) {
1397 SCOPED_TRACE(trace);
1398 ScreenCapture::captureScreen(&sc);
1399 // The edge should be moved back one pixel by our crop.
1400 sc->expectFGColor(126, 126);
1401 sc->expectBGColor(127, 127);
1402 sc->expectBGColor(128, 128);
1403 }
chaviw59f5c562017-06-28 16:39:06 -07001404
1405 void EXPECT_RESIZE_STATE(const char* trace) {
1406 SCOPED_TRACE(trace);
1407 ScreenCapture::captureScreen(&sc);
1408 // The FG is now resized too 128,128 at 64,64
1409 sc->expectFGColor(64, 64);
1410 sc->expectFGColor(191, 191);
1411 sc->expectBGColor(192, 192);
1412 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001413};
1414
1415TEST_F(CropLatchingTest, CropLatching) {
1416 EXPECT_INITIAL_STATE("before anything");
1417 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001418 asTransaction([&](Transaction& t) {
1419 t.setSize(mFGSurfaceControl, 128, 128);
1420 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1421 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001422
1423 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1424
1425 restoreInitialState();
1426
Robert Carr4cdc58f2017-08-23 14:22:20 -07001427 asTransaction([&](Transaction& t) {
1428 t.setSize(mFGSurfaceControl, 128, 128);
1429 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1430 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1431 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001432
1433 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1434
1435 completeFGResize();
1436
1437 EXPECT_CROPPED_STATE("after the resize finishes");
1438}
1439
1440TEST_F(CropLatchingTest, FinalCropLatching) {
1441 EXPECT_INITIAL_STATE("before anything");
1442 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001443 asTransaction([&](Transaction& t) {
1444 t.setSize(mFGSurfaceControl, 128, 128);
1445 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1446 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001447
1448 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1449
1450 restoreInitialState();
1451
Robert Carr4cdc58f2017-08-23 14:22:20 -07001452 asTransaction([&](Transaction& t) {
1453 t.setSize(mFGSurfaceControl, 128, 128);
1454 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1455 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1456 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001457
1458 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1459
1460 completeFGResize();
1461
1462 EXPECT_CROPPED_STATE("after the resize finishes");
1463}
1464
Robert Carr7bf247e2017-05-18 14:02:49 -07001465// In this test we ensure that setGeometryAppliesWithResize actually demands
1466// a buffer of the new size, and not just any size.
1467TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1468 EXPECT_INITIAL_STATE("before anything");
1469 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001470 asTransaction([&](Transaction& t) {
1471 t.setSize(mFGSurfaceControl, 128, 128);
1472 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1473 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001474
1475 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1476
1477 restoreInitialState();
1478
1479 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1480 // initiating the resize.
1481 lockAndFillFGBuffer();
1482
Robert Carr4cdc58f2017-08-23 14:22:20 -07001483 asTransaction([&](Transaction& t) {
1484 t.setSize(mFGSurfaceControl, 128, 128);
1485 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1486 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1487 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001488
1489 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1490
1491 // We now submit our old buffer, at the old size, and ensure it doesn't
1492 // trigger geometry latching.
1493 unlockFGBuffer();
1494
1495 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1496
1497 completeFGResize();
1498
1499 EXPECT_CROPPED_STATE("after the resize finishes");
1500}
1501
1502TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1503 EXPECT_INITIAL_STATE("before anything");
1504 // In this scenario, we attempt to set the final crop a second time while the resize
1505 // is still pending, and ensure we are successful. Success meaning the second crop
1506 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001507 asTransaction([&](Transaction& t) {
1508 t.setSize(mFGSurfaceControl, 128, 128);
1509 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1510 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1511 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001512
chaviw59f5c562017-06-28 16:39:06 -07001513 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1514
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001515 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001516
chaviw59f5c562017-06-28 16:39:06 -07001517 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001518
1519 completeFGResize();
1520
chaviw59f5c562017-06-28 16:39:06 -07001521 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001522}
1523
Pablo Ceballos05289c22016-04-14 15:49:55 -07001524TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1525 sp<ScreenCapture> sc;
1526 {
1527 SCOPED_TRACE("before anything");
1528 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001529 sc->expectBGColor(32, 32);
1530 sc->expectFGColor(96, 96);
1531 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001532 }
1533
1534 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001535 asTransaction([&](Transaction& t) {
1536 t.setAlpha(mFGSurfaceControl, 0.75);
1537 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001538 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001539 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001540
Robert Carr4cdc58f2017-08-23 14:22:20 -07001541 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001542 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001543 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001544 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001545 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001546
1547 {
1548 SCOPED_TRACE("before any trigger");
1549 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001550 sc->expectBGColor(32, 32);
1551 sc->expectFGColor(96, 96);
1552 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001553 }
1554
1555 // should trigger the first deferred transaction, but not the second one
1556 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1557 {
1558 SCOPED_TRACE("after first trigger");
1559 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001560 sc->expectBGColor(32, 32);
1561 sc->checkPixel(96, 96, 162, 63, 96);
1562 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001563 }
1564
1565 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001566 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001567
1568 // trigger the second deferred transaction
1569 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1570 {
1571 SCOPED_TRACE("after second trigger");
1572 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001573 sc->expectBGColor(32, 32);
1574 sc->expectBGColor(96, 96);
1575 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001576 }
1577}
1578
Robert Carrdb66e622017-04-10 16:55:57 -07001579TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1580 sp<ScreenCapture> sc;
1581 {
1582 SCOPED_TRACE("before adding relative surface");
1583 ScreenCapture::captureScreen(&sc);
1584 sc->expectBGColor(24, 24);
1585 sc->expectFGColor(75, 75);
1586 sc->expectBGColor(145, 145);
1587 }
1588
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001589 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1590 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001591 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1592 waitForPostedBuffers();
1593
1594 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001595 asTransaction([&](Transaction& t) {
1596 t.setPosition(relativeSurfaceControl, 64, 64);
1597 t.show(relativeSurfaceControl);
1598 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1599 });
Robert Carrdb66e622017-04-10 16:55:57 -07001600
1601 {
1602 SCOPED_TRACE("after adding relative surface");
1603 ScreenCapture::captureScreen(&sc);
1604 // our relative surface should be visible now.
1605 sc->checkPixel(75, 75, 255, 177, 177);
1606 }
1607
1608 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001609 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001610
1611 {
1612 SCOPED_TRACE("after set layer");
1613 ScreenCapture::captureScreen(&sc);
1614 // now the FG surface should be visible again.
1615 sc->expectFGColor(75, 75);
1616 }
1617}
1618
Robert Carre392b552017-09-19 12:16:05 -07001619TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1620 sp<ScreenCapture> sc;
1621
1622 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001623 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1624 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1625 sp<SurfaceControl> childBuffer =
1626 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1627 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001628 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1629
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001630 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001631
1632 {
1633 ScreenCapture::captureScreen(&sc);
1634 sc->expectChildColor(73, 73);
1635 sc->expectFGColor(74, 74);
1636 }
1637
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001638 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001639
1640 {
1641 ScreenCapture::captureScreen(&sc);
1642 sc->expectChildColor(73, 73);
1643 sc->expectChildColor(74, 74);
1644 }
1645}
1646
Robert Carr2c5f6d22017-09-26 12:30:35 -07001647TEST_F(LayerUpdateTest, MergingTransactions) {
1648 sp<ScreenCapture> sc;
1649 {
1650 SCOPED_TRACE("before move");
1651 ScreenCapture::captureScreen(&sc);
1652 sc->expectBGColor(0, 12);
1653 sc->expectFGColor(75, 75);
1654 sc->expectBGColor(145, 145);
1655 }
1656
1657 Transaction t1, t2;
1658 t1.setPosition(mFGSurfaceControl, 128, 128);
1659 t2.setPosition(mFGSurfaceControl, 0, 0);
1660 // We expect that the position update from t2 now
1661 // overwrites the position update from t1.
1662 t1.merge(std::move(t2));
1663 t1.apply();
1664
1665 {
1666 ScreenCapture::captureScreen(&sc);
1667 sc->expectFGColor(1, 1);
1668 }
1669}
1670
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671class ChildLayerTest : public LayerUpdateTest {
1672protected:
1673 void SetUp() override {
1674 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001675 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1676 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677 fillSurfaceRGBA8(mChild, 200, 200, 200);
1678
1679 {
1680 SCOPED_TRACE("before anything");
1681 ScreenCapture::captureScreen(&mCapture);
1682 mCapture->expectChildColor(64, 64);
1683 }
1684 }
1685 void TearDown() override {
1686 LayerUpdateTest::TearDown();
1687 mChild = 0;
1688 }
1689
1690 sp<SurfaceControl> mChild;
1691 sp<ScreenCapture> mCapture;
1692};
1693
1694TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001695 asTransaction([&](Transaction& t) {
1696 t.show(mChild);
1697 t.setPosition(mChild, 10, 10);
1698 t.setPosition(mFGSurfaceControl, 64, 64);
1699 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001700
1701 {
1702 ScreenCapture::captureScreen(&mCapture);
1703 // Top left of foreground must now be visible
1704 mCapture->expectFGColor(64, 64);
1705 // But 10 pixels in we should see the child surface
1706 mCapture->expectChildColor(74, 74);
1707 // And 10 more pixels we should be back to the foreground surface
1708 mCapture->expectFGColor(84, 84);
1709 }
1710
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001711 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712
1713 {
1714 ScreenCapture::captureScreen(&mCapture);
1715 // Top left of foreground should now be at 0, 0
1716 mCapture->expectFGColor(0, 0);
1717 // But 10 pixels in we should see the child surface
1718 mCapture->expectChildColor(10, 10);
1719 // And 10 more pixels we should be back to the foreground surface
1720 mCapture->expectFGColor(20, 20);
1721 }
1722}
1723
Robert Carr41b08b52017-06-01 16:11:34 -07001724TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001725 asTransaction([&](Transaction& t) {
1726 t.show(mChild);
1727 t.setPosition(mChild, 0, 0);
1728 t.setPosition(mFGSurfaceControl, 0, 0);
1729 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1730 });
Robert Carr41b08b52017-06-01 16:11:34 -07001731
1732 {
1733 ScreenCapture::captureScreen(&mCapture);
1734 mCapture->expectChildColor(0, 0);
1735 mCapture->expectChildColor(4, 4);
1736 mCapture->expectBGColor(5, 5);
1737 }
1738}
1739
1740TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001741 asTransaction([&](Transaction& t) {
1742 t.show(mChild);
1743 t.setPosition(mChild, 0, 0);
1744 t.setPosition(mFGSurfaceControl, 0, 0);
1745 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1746 });
Robert Carr41b08b52017-06-01 16:11:34 -07001747
1748 {
1749 ScreenCapture::captureScreen(&mCapture);
1750 mCapture->expectChildColor(0, 0);
1751 mCapture->expectChildColor(4, 4);
1752 mCapture->expectBGColor(5, 5);
1753 }
1754}
1755
Robert Carr1f0a16a2016-10-24 16:27:39 -07001756TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001757 asTransaction([&](Transaction& t) {
1758 t.show(mChild);
1759 t.setPosition(mFGSurfaceControl, 0, 0);
1760 t.setPosition(mChild, 63, 63);
1761 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001762
1763 {
1764 ScreenCapture::captureScreen(&mCapture);
1765 mCapture->expectFGColor(0, 0);
1766 // Last pixel in foreground should now be the child.
1767 mCapture->expectChildColor(63, 63);
1768 // But the child should be constrained and the next pixel
1769 // must be the background
1770 mCapture->expectBGColor(64, 64);
1771 }
1772}
1773
1774TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001775 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001776
1777 // Find the boundary between the parent and child
1778 {
1779 ScreenCapture::captureScreen(&mCapture);
1780 mCapture->expectChildColor(9, 9);
1781 mCapture->expectFGColor(10, 10);
1782 }
1783
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001784 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001785
1786 // The boundary should be twice as far from the origin now.
1787 // The pixels from the last test should all be child now
1788 {
1789 ScreenCapture::captureScreen(&mCapture);
1790 mCapture->expectChildColor(9, 9);
1791 mCapture->expectChildColor(10, 10);
1792 mCapture->expectChildColor(19, 19);
1793 mCapture->expectFGColor(20, 20);
1794 }
1795}
Robert Carr9524cb32017-02-13 11:32:32 -08001796
Robert Carr6452f122017-03-21 10:41:29 -07001797TEST_F(ChildLayerTest, ChildLayerAlpha) {
1798 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1799 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1800 fillSurfaceRGBA8(mChild, 0, 254, 0);
1801 waitForPostedBuffers();
1802
Robert Carr4cdc58f2017-08-23 14:22:20 -07001803 asTransaction([&](Transaction& t) {
1804 t.show(mChild);
1805 t.setPosition(mChild, 0, 0);
1806 t.setPosition(mFGSurfaceControl, 0, 0);
1807 });
Robert Carr6452f122017-03-21 10:41:29 -07001808
1809 {
1810 ScreenCapture::captureScreen(&mCapture);
1811 // Unblended child color
1812 mCapture->checkPixel(0, 0, 0, 254, 0);
1813 }
1814
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001815 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001816
1817 {
1818 ScreenCapture::captureScreen(&mCapture);
1819 // Child and BG blended.
1820 mCapture->checkPixel(0, 0, 127, 127, 0);
1821 }
1822
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001823 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001824
1825 {
1826 ScreenCapture::captureScreen(&mCapture);
1827 // Child and BG blended.
1828 mCapture->checkPixel(0, 0, 95, 64, 95);
1829 }
1830}
1831
Robert Carr9524cb32017-02-13 11:32:32 -08001832TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001833 asTransaction([&](Transaction& t) {
1834 t.show(mChild);
1835 t.setPosition(mChild, 10, 10);
1836 t.setPosition(mFGSurfaceControl, 64, 64);
1837 });
Robert Carr9524cb32017-02-13 11:32:32 -08001838
1839 {
1840 ScreenCapture::captureScreen(&mCapture);
1841 // Top left of foreground must now be visible
1842 mCapture->expectFGColor(64, 64);
1843 // But 10 pixels in we should see the child surface
1844 mCapture->expectChildColor(74, 74);
1845 // And 10 more pixels we should be back to the foreground surface
1846 mCapture->expectFGColor(84, 84);
1847 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001848
1849 asTransaction([&](Transaction& t) {
1850 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1851 });
1852
Robert Carr9524cb32017-02-13 11:32:32 -08001853 {
1854 ScreenCapture::captureScreen(&mCapture);
1855 mCapture->expectFGColor(64, 64);
1856 // In reparenting we should have exposed the entire foreground surface.
1857 mCapture->expectFGColor(74, 74);
1858 // And the child layer should now begin at 10, 10 (since the BG
1859 // layer is at (0, 0)).
1860 mCapture->expectBGColor(9, 9);
1861 mCapture->expectChildColor(10, 10);
1862 }
1863}
1864
chaviw161410b02017-07-27 10:46:08 -07001865TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001866 asTransaction([&](Transaction& t) {
1867 t.show(mChild);
1868 t.setPosition(mChild, 10, 10);
1869 t.setPosition(mFGSurfaceControl, 64, 64);
1870 });
Robert Carr9524cb32017-02-13 11:32:32 -08001871
1872 {
1873 ScreenCapture::captureScreen(&mCapture);
1874 // Top left of foreground must now be visible
1875 mCapture->expectFGColor(64, 64);
1876 // But 10 pixels in we should see the child surface
1877 mCapture->expectChildColor(74, 74);
1878 // And 10 more pixels we should be back to the foreground surface
1879 mCapture->expectFGColor(84, 84);
1880 }
1881
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001882 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001883
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001884 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001885
chaviw161410b02017-07-27 10:46:08 -07001886 // Since the child has the same client as the parent, it will not get
1887 // detached and will be hidden.
1888 {
1889 ScreenCapture::captureScreen(&mCapture);
1890 mCapture->expectFGColor(64, 64);
1891 mCapture->expectFGColor(74, 74);
1892 mCapture->expectFGColor(84, 84);
1893 }
1894}
1895
1896TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1897 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001898 sp<SurfaceControl> mChildNewClient =
1899 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1900 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001901
1902 ASSERT_TRUE(mChildNewClient != NULL);
1903 ASSERT_TRUE(mChildNewClient->isValid());
1904
1905 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1906
Robert Carr4cdc58f2017-08-23 14:22:20 -07001907 asTransaction([&](Transaction& t) {
1908 t.hide(mChild);
1909 t.show(mChildNewClient);
1910 t.setPosition(mChildNewClient, 10, 10);
1911 t.setPosition(mFGSurfaceControl, 64, 64);
1912 });
chaviw161410b02017-07-27 10:46:08 -07001913
1914 {
1915 ScreenCapture::captureScreen(&mCapture);
1916 // Top left of foreground must now be visible
1917 mCapture->expectFGColor(64, 64);
1918 // But 10 pixels in we should see the child surface
1919 mCapture->expectChildColor(74, 74);
1920 // And 10 more pixels we should be back to the foreground surface
1921 mCapture->expectFGColor(84, 84);
1922 }
1923
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001924 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001925
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001926 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001927
Robert Carr9524cb32017-02-13 11:32:32 -08001928 // Nothing should have changed.
1929 {
1930 ScreenCapture::captureScreen(&mCapture);
1931 mCapture->expectFGColor(64, 64);
1932 mCapture->expectChildColor(74, 74);
1933 mCapture->expectFGColor(84, 84);
1934 }
1935}
1936
Robert Carr9b429f42017-04-17 14:56:57 -07001937TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001938 asTransaction([&](Transaction& t) {
1939 t.show(mChild);
1940 t.setPosition(mChild, 0, 0);
1941 t.setPosition(mFGSurfaceControl, 0, 0);
1942 });
Robert Carr9b429f42017-04-17 14:56:57 -07001943
1944 {
1945 ScreenCapture::captureScreen(&mCapture);
1946 // We've positioned the child in the top left.
1947 mCapture->expectChildColor(0, 0);
1948 // But it's only 10x10.
1949 mCapture->expectFGColor(10, 10);
1950 }
1951
Robert Carr4cdc58f2017-08-23 14:22:20 -07001952 asTransaction([&](Transaction& t) {
1953 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1954 // We cause scaling by 2.
1955 t.setSize(mFGSurfaceControl, 128, 128);
1956 });
Robert Carr9b429f42017-04-17 14:56:57 -07001957
1958 {
1959 ScreenCapture::captureScreen(&mCapture);
1960 // We've positioned the child in the top left.
1961 mCapture->expectChildColor(0, 0);
1962 mCapture->expectChildColor(10, 10);
1963 mCapture->expectChildColor(19, 19);
1964 // And now it should be scaled all the way to 20x20
1965 mCapture->expectFGColor(20, 20);
1966 }
1967}
1968
Robert Carr1725eee2017-04-26 18:32:15 -07001969// Regression test for b/37673612
1970TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001971 asTransaction([&](Transaction& t) {
1972 t.show(mChild);
1973 t.setPosition(mChild, 0, 0);
1974 t.setPosition(mFGSurfaceControl, 0, 0);
1975 });
Robert Carr1725eee2017-04-26 18:32:15 -07001976
1977 {
1978 ScreenCapture::captureScreen(&mCapture);
1979 // We've positioned the child in the top left.
1980 mCapture->expectChildColor(0, 0);
1981 // But it's only 10x10.
1982 mCapture->expectFGColor(10, 10);
1983 }
Robert Carr1725eee2017-04-26 18:32:15 -07001984 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1985 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001986 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001987 sp<Surface> s = mFGSurfaceControl->getSurface();
1988 auto anw = static_cast<ANativeWindow*>(s.get());
1989 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1990 native_window_set_buffers_dimensions(anw, 64, 128);
1991 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1992 waitForPostedBuffers();
1993
1994 {
1995 // The child should still be in the same place and not have any strange scaling as in
1996 // b/37673612.
1997 ScreenCapture::captureScreen(&mCapture);
1998 mCapture->expectChildColor(0, 0);
1999 mCapture->expectFGColor(10, 10);
2000 }
2001}
2002
Dan Stoza412903f2017-04-27 13:42:17 -07002003TEST_F(ChildLayerTest, Bug36858924) {
2004 // Destroy the child layer
2005 mChild.clear();
2006
2007 // Now recreate it as hidden
2008 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2009 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2010 mFGSurfaceControl.get());
2011
2012 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002013 asTransaction([&](Transaction& t) {
2014 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002015 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002016 t.show(mChild);
2017 });
Dan Stoza412903f2017-04-27 13:42:17 -07002018
2019 // Render the foreground surface a few times
2020 //
2021 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2022 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2023 // never acquire/release the first buffer
2024 ALOGI("Filling 1");
2025 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2026 ALOGI("Filling 2");
2027 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2028 ALOGI("Filling 3");
2029 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2030 ALOGI("Filling 4");
2031 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2032}
2033
chaviwf1961f72017-09-18 16:41:07 -07002034TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002035 asTransaction([&](Transaction& t) {
2036 t.show(mChild);
2037 t.setPosition(mChild, 10, 10);
2038 t.setPosition(mFGSurfaceControl, 64, 64);
2039 });
chaviw06178942017-07-27 10:25:59 -07002040
2041 {
2042 ScreenCapture::captureScreen(&mCapture);
2043 // Top left of foreground must now be visible
2044 mCapture->expectFGColor(64, 64);
2045 // But 10 pixels in we should see the child surface
2046 mCapture->expectChildColor(74, 74);
2047 // And 10 more pixels we should be back to the foreground surface
2048 mCapture->expectFGColor(84, 84);
2049 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002050
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002051 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002052
chaviw06178942017-07-27 10:25:59 -07002053 {
2054 ScreenCapture::captureScreen(&mCapture);
2055 mCapture->expectFGColor(64, 64);
2056 // In reparenting we should have exposed the entire foreground surface.
2057 mCapture->expectFGColor(74, 74);
2058 // And the child layer should now begin at 10, 10 (since the BG
2059 // layer is at (0, 0)).
2060 mCapture->expectBGColor(9, 9);
2061 mCapture->expectChildColor(10, 10);
2062 }
2063}
2064
chaviwf1961f72017-09-18 16:41:07 -07002065TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002066 asTransaction([&](Transaction& t) {
2067 t.show(mChild);
2068 t.setPosition(mChild, 10, 10);
2069 t.setPosition(mFGSurfaceControl, 64, 64);
2070 });
chaviwf1961f72017-09-18 16:41:07 -07002071
2072 {
2073 ScreenCapture::captureScreen(&mCapture);
2074 // Top left of foreground must now be visible
2075 mCapture->expectFGColor(64, 64);
2076 // But 10 pixels in we should see the child surface
2077 mCapture->expectChildColor(74, 74);
2078 // And 10 more pixels we should be back to the foreground surface
2079 mCapture->expectFGColor(84, 84);
2080 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002081 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002082 {
2083 ScreenCapture::captureScreen(&mCapture);
2084 // Nothing should have changed.
2085 mCapture->expectFGColor(64, 64);
2086 mCapture->expectChildColor(74, 74);
2087 mCapture->expectFGColor(84, 84);
2088 }
2089}
2090
2091TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002092 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2093 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002094 ASSERT_TRUE(newSurface != NULL);
2095 ASSERT_TRUE(newSurface->isValid());
2096
2097 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002098 asTransaction([&](Transaction& t) {
2099 t.hide(mChild);
2100 t.show(newSurface);
2101 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002102 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002103 t.setPosition(mFGSurfaceControl, 64, 64);
2104 });
chaviwf1961f72017-09-18 16:41:07 -07002105
2106 {
2107 ScreenCapture::captureScreen(&mCapture);
2108 // Top left of foreground must now be visible
2109 mCapture->expectFGColor(64, 64);
2110 // At 10, 10 we should see the new surface
2111 mCapture->checkPixel(10, 10, 63, 195, 63);
2112 }
2113
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002114 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002115
2116 {
2117 ScreenCapture::captureScreen(&mCapture);
2118 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2119 // mFGSurface, putting it at 74, 74.
2120 mCapture->expectFGColor(64, 64);
2121 mCapture->checkPixel(74, 74, 63, 195, 63);
2122 mCapture->expectFGColor(84, 84);
2123 }
2124}
2125
chaviwc9674332017-08-28 12:32:18 -07002126TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002127 sp<SurfaceControl> grandchild =
2128 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2129 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002130 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2131
2132 {
2133 ScreenCapture::captureScreen(&mCapture);
2134 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2135 // which begins at 64, 64
2136 mCapture->checkPixel(64, 64, 50, 50, 50);
2137 }
2138}
2139
Robert Carr503c7042017-09-27 15:06:08 -07002140TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002141 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2142 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002143 fillSurfaceRGBA8(relative, 255, 255, 255);
2144
2145 Transaction t;
2146 t.setLayer(relative, INT32_MAX)
2147 .setRelativeLayer(mChild, relative->getHandle(), 1)
2148 .setPosition(mFGSurfaceControl, 0, 0)
2149 .apply(true);
2150
2151 // We expect that the child should have been elevated above our
2152 // INT_MAX layer even though it's not a child of it.
2153 {
2154 ScreenCapture::captureScreen(&mCapture);
2155 mCapture->expectChildColor(0, 0);
2156 mCapture->expectChildColor(9, 9);
2157 mCapture->checkPixel(10, 10, 255, 255, 255);
2158 }
2159}
2160
chaviw13fdc492017-06-27 12:40:18 -07002161class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002162protected:
chaviw13fdc492017-06-27 12:40:18 -07002163 void SetUp() override {
2164 LayerUpdateTest::SetUp();
2165
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002166 mLayerColorControl =
2167 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2168 PIXEL_FORMAT_RGBA_8888,
2169 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002170
2171 ASSERT_TRUE(mLayerColorControl != NULL);
2172 ASSERT_TRUE(mLayerColorControl->isValid());
2173
Robert Carr4cdc58f2017-08-23 14:22:20 -07002174 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002175 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002176 t.setPosition(mLayerColorControl, 140, 140);
2177 t.hide(mLayerColorControl);
2178 t.hide(mFGSurfaceControl);
2179 });
chaviw13fdc492017-06-27 12:40:18 -07002180 }
2181
2182 void TearDown() override {
2183 LayerUpdateTest::TearDown();
2184 mLayerColorControl = 0;
2185 }
2186
2187 sp<SurfaceControl> mLayerColorControl;
2188};
2189
2190TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2191 sp<ScreenCapture> sc;
2192
2193 {
2194 SCOPED_TRACE("before setColor");
2195 ScreenCapture::captureScreen(&sc);
2196 sc->expectBGColor(145, 145);
2197 }
2198
Robert Carr4cdc58f2017-08-23 14:22:20 -07002199 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002200 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002201 t.setColor(mLayerColorControl, color);
2202 t.show(mLayerColorControl);
2203 });
chaviw13fdc492017-06-27 12:40:18 -07002204
chaviw13fdc492017-06-27 12:40:18 -07002205 {
2206 // There should now be a color
2207 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002208
chaviw13fdc492017-06-27 12:40:18 -07002209 ScreenCapture::captureScreen(&sc);
2210 sc->checkPixel(145, 145, 43, 207, 131);
2211 }
2212}
2213
2214TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2215 sp<ScreenCapture> sc;
2216 {
2217 SCOPED_TRACE("before setColor");
2218 ScreenCapture::captureScreen(&sc);
2219 sc->expectBGColor(145, 145);
2220 }
2221
Robert Carr4cdc58f2017-08-23 14:22:20 -07002222 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002223 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002224 t.setColor(mLayerColorControl, color);
2225 t.setAlpha(mLayerColorControl, .75f);
2226 t.show(mLayerColorControl);
2227 });
2228
chaviw13fdc492017-06-27 12:40:18 -07002229 {
2230 // There should now be a color with .75 alpha
2231 SCOPED_TRACE("after setColor");
2232 ScreenCapture::captureScreen(&sc);
2233 sc->checkPixel(145, 145, 48, 171, 147);
2234 }
2235}
2236
2237TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2238 sp<ScreenCapture> sc;
2239 {
2240 SCOPED_TRACE("before setColor");
2241 ScreenCapture::captureScreen(&sc);
2242 sc->expectBGColor(145, 145);
2243 }
2244
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002245 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002246
chaviw13fdc492017-06-27 12:40:18 -07002247 {
2248 // There should now be set to 0,0,0 (black) as default.
2249 SCOPED_TRACE("after setColor");
2250 ScreenCapture::captureScreen(&sc);
2251 sc->checkPixel(145, 145, 0, 0, 0);
2252 }
2253}
2254
chaviwa76b2712017-09-20 12:02:26 -07002255class ScreenCaptureTest : public LayerUpdateTest {
2256protected:
2257 std::unique_ptr<CaptureLayer> mCapture;
2258};
2259
2260TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2261 auto bgHandle = mBGSurfaceControl->getHandle();
2262 CaptureLayer::captureScreen(&mCapture, bgHandle);
2263 mCapture->expectBGColor(0, 0);
2264 // Doesn't capture FG layer which is at 64, 64
2265 mCapture->expectBGColor(64, 64);
2266}
2267
2268TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2269 auto fgHandle = mFGSurfaceControl->getHandle();
2270
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002271 sp<SurfaceControl> child =
2272 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2273 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002274 fillSurfaceRGBA8(child, 200, 200, 200);
2275
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002276 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002277
2278 // Captures mFGSurfaceControl layer and its child.
2279 CaptureLayer::captureScreen(&mCapture, fgHandle);
2280 mCapture->expectFGColor(10, 10);
2281 mCapture->expectChildColor(0, 0);
2282}
2283
2284TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2285 auto fgHandle = mFGSurfaceControl->getHandle();
2286
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002287 sp<SurfaceControl> child =
2288 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2289 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002290 fillSurfaceRGBA8(child, 200, 200, 200);
2291
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002292 sp<SurfaceControl> grandchild =
2293 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2294 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002295
2296 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2297 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002298 .show(child)
2299 .setPosition(grandchild, 5, 5)
2300 .show(grandchild)
2301 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002302
2303 // Captures mFGSurfaceControl, its child, and the grandchild.
2304 CaptureLayer::captureScreen(&mCapture, fgHandle);
2305 mCapture->expectFGColor(10, 10);
2306 mCapture->expectChildColor(0, 0);
2307 mCapture->checkPixel(5, 5, 50, 50, 50);
2308}
2309
2310TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002311 sp<SurfaceControl> child =
2312 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2313 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002314 fillSurfaceRGBA8(child, 200, 200, 200);
2315 auto childHandle = child->getHandle();
2316
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002317 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002318
2319 // Captures only the child layer, and not the parent.
2320 CaptureLayer::captureScreen(&mCapture, childHandle);
2321 mCapture->expectChildColor(0, 0);
2322 mCapture->expectChildColor(9, 9);
2323}
2324
2325TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002326 sp<SurfaceControl> child =
2327 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2328 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002329 fillSurfaceRGBA8(child, 200, 200, 200);
2330 auto childHandle = child->getHandle();
2331
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002332 sp<SurfaceControl> grandchild =
2333 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2334 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002335 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2336
2337 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002338 .show(child)
2339 .setPosition(grandchild, 5, 5)
2340 .show(grandchild)
2341 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002342
2343 auto grandchildHandle = grandchild->getHandle();
2344
2345 // Captures only the grandchild.
2346 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2347 mCapture->checkPixel(0, 0, 50, 50, 50);
2348 mCapture->checkPixel(4, 4, 50, 50, 50);
2349}
2350
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002351} // namespace android