blob: 76efc7fb7e1e681c725aade28e93f1ba234914f3 [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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700878class LayerUpdateTest : public ::testing::Test {
879protected:
880 virtual void SetUp() {
881 mComposerClient = new SurfaceComposerClient;
882 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
883
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700884 sp<IBinder> display(
885 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700886 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700887 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700888
889 ssize_t displayWidth = info.w;
890 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700891
892 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700893 mBGSurfaceControl =
894 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
895 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700896 ASSERT_TRUE(mBGSurfaceControl != NULL);
897 ASSERT_TRUE(mBGSurfaceControl->isValid());
898 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
899
900 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700901 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
902 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700903 ASSERT_TRUE(mFGSurfaceControl != NULL);
904 ASSERT_TRUE(mFGSurfaceControl->isValid());
905
906 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
907
908 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700909 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
910 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700911 ASSERT_TRUE(mSyncSurfaceControl != NULL);
912 ASSERT_TRUE(mSyncSurfaceControl->isValid());
913
914 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
915
Robert Carr4cdc58f2017-08-23 14:22:20 -0700916 asTransaction([&](Transaction& t) {
917 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700918
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700919 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700920
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700921 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
922 .setPosition(mFGSurfaceControl, 64, 64)
923 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700924
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700925 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
926 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
927 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700928 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700929 }
930
931 virtual void TearDown() {
932 mComposerClient->dispose();
933 mBGSurfaceControl = 0;
934 mFGSurfaceControl = 0;
935 mSyncSurfaceControl = 0;
936 mComposerClient = 0;
937 }
938
939 void waitForPostedBuffers() {
940 // Since the sync surface is in synchronous mode (i.e. double buffered)
941 // posting three buffers to it should ensure that at least two
942 // SurfaceFlinger::handlePageFlip calls have been made, which should
943 // guaranteed that a buffer posted to another Surface has been retired.
944 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
945 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
946 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
947 }
948
Robert Carr4cdc58f2017-08-23 14:22:20 -0700949 void asTransaction(const std::function<void(Transaction&)>& exec) {
950 Transaction t;
951 exec(t);
952 t.apply(true);
953 }
954
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700955 sp<SurfaceComposerClient> mComposerClient;
956 sp<SurfaceControl> mBGSurfaceControl;
957 sp<SurfaceControl> mFGSurfaceControl;
958
959 // This surface is used to ensure that the buffers posted to
960 // mFGSurfaceControl have been picked up by SurfaceFlinger.
961 sp<SurfaceControl> mSyncSurfaceControl;
962};
963
Robert Carr7f619b22017-11-06 12:56:35 -0800964TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
965 sp<ScreenCapture> sc;
966
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700967 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
968 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -0800969 fillSurfaceRGBA8(relative, 10, 10, 10);
970 waitForPostedBuffers();
971
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700972 Transaction{}
973 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -0800974 .setPosition(relative, 64, 64)
975 .apply();
976
977 {
978 // The relative should be on top of the FG control.
979 ScreenCapture::captureScreen(&sc);
980 sc->checkPixel(64, 64, 10, 10, 10);
981 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700982 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800983
984 {
985 // Nothing should change at this point.
986 ScreenCapture::captureScreen(&sc);
987 sc->checkPixel(64, 64, 10, 10, 10);
988 }
989
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700990 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -0800991
992 {
993 // Ensure that the relative was actually hidden, rather than
994 // being left in the detached but visible state.
995 ScreenCapture::captureScreen(&sc);
996 sc->expectFGColor(64, 64);
997 }
998}
999
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001000TEST_F(LayerUpdateTest, LayerMoveWorks) {
1001 sp<ScreenCapture> sc;
1002 {
1003 SCOPED_TRACE("before move");
1004 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001005 sc->expectBGColor(0, 12);
1006 sc->expectFGColor(75, 75);
1007 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001008 }
1009
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001010 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001011
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001012 {
1013 // This should reflect the new position, but not the new color.
1014 SCOPED_TRACE("after move, before redraw");
1015 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001016 sc->expectBGColor(24, 24);
1017 sc->expectBGColor(75, 75);
1018 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001019 }
1020
1021 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1022 waitForPostedBuffers();
1023 {
1024 // This should reflect the new position and the new color.
1025 SCOPED_TRACE("after redraw");
1026 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001027 sc->expectBGColor(24, 24);
1028 sc->expectBGColor(75, 75);
1029 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001030 }
1031}
1032
1033TEST_F(LayerUpdateTest, LayerResizeWorks) {
1034 sp<ScreenCapture> sc;
1035 {
1036 SCOPED_TRACE("before resize");
1037 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001038 sc->expectBGColor(0, 12);
1039 sc->expectFGColor(75, 75);
1040 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001041 }
1042
Steve Block9d453682011-12-20 16:23:08 +00001043 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001044 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001045 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001046 {
1047 // This should not reflect the new size or color because SurfaceFlinger
1048 // has not yet received a buffer of the correct size.
1049 SCOPED_TRACE("after resize, before redraw");
1050 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001051 sc->expectBGColor(0, 12);
1052 sc->expectFGColor(75, 75);
1053 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001054 }
1055
Steve Block9d453682011-12-20 16:23:08 +00001056 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001057 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1058 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001059 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001060 {
1061 // This should reflect the new size and the new color.
1062 SCOPED_TRACE("after redraw");
1063 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001064 sc->expectBGColor(24, 24);
1065 sc->checkPixel(75, 75, 63, 195, 63);
1066 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001067 }
1068}
1069
Haixia Shid5750962015-07-27 16:50:49 -07001070TEST_F(LayerUpdateTest, LayerCropWorks) {
1071 sp<ScreenCapture> sc;
1072 {
1073 SCOPED_TRACE("before crop");
1074 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001075 sc->expectBGColor(24, 24);
1076 sc->expectFGColor(75, 75);
1077 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001078 }
1079
Robert Carr4cdc58f2017-08-23 14:22:20 -07001080 asTransaction([&](Transaction& t) {
1081 Rect cropRect(16, 16, 32, 32);
1082 t.setCrop(mFGSurfaceControl, cropRect);
1083 });
Haixia Shid5750962015-07-27 16:50:49 -07001084 {
1085 // This should crop the foreground surface.
1086 SCOPED_TRACE("after crop");
1087 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001088 sc->expectBGColor(24, 24);
1089 sc->expectBGColor(75, 75);
1090 sc->expectFGColor(95, 80);
1091 sc->expectFGColor(80, 95);
1092 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001093 }
1094}
1095
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001096TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1097 sp<ScreenCapture> sc;
1098 {
1099 SCOPED_TRACE("before crop");
1100 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001101 sc->expectBGColor(24, 24);
1102 sc->expectFGColor(75, 75);
1103 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001104 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001105 asTransaction([&](Transaction& t) {
1106 Rect cropRect(16, 16, 32, 32);
1107 t.setFinalCrop(mFGSurfaceControl, cropRect);
1108 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001109 {
1110 // This should crop the foreground surface.
1111 SCOPED_TRACE("after crop");
1112 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001113 sc->expectBGColor(24, 24);
1114 sc->expectBGColor(75, 75);
1115 sc->expectBGColor(95, 80);
1116 sc->expectBGColor(80, 95);
1117 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001118 }
1119}
1120
Haixia Shid5750962015-07-27 16:50:49 -07001121TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1122 sp<ScreenCapture> sc;
1123 {
1124 SCOPED_TRACE("before setLayer");
1125 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001126 sc->expectBGColor(24, 24);
1127 sc->expectFGColor(75, 75);
1128 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001129 }
1130
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001131 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001132
Haixia Shid5750962015-07-27 16:50:49 -07001133 {
1134 // This should hide the foreground surface beneath the background.
1135 SCOPED_TRACE("after setLayer");
1136 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001137 sc->expectBGColor(24, 24);
1138 sc->expectBGColor(75, 75);
1139 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001140 }
1141}
1142
1143TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1144 sp<ScreenCapture> sc;
1145 {
1146 SCOPED_TRACE("before hide");
1147 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001148 sc->expectBGColor(24, 24);
1149 sc->expectFGColor(75, 75);
1150 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001151 }
1152
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001153 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001154
Haixia Shid5750962015-07-27 16:50:49 -07001155 {
1156 // This should hide the foreground surface.
1157 SCOPED_TRACE("after hide, before show");
1158 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001159 sc->expectBGColor(24, 24);
1160 sc->expectBGColor(75, 75);
1161 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001162 }
1163
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001164 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001165
Haixia Shid5750962015-07-27 16:50:49 -07001166 {
1167 // This should show the foreground surface.
1168 SCOPED_TRACE("after show");
1169 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001170 sc->expectBGColor(24, 24);
1171 sc->expectFGColor(75, 75);
1172 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001173 }
1174}
1175
1176TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1177 sp<ScreenCapture> sc;
1178 {
1179 SCOPED_TRACE("before setAlpha");
1180 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001181 sc->expectBGColor(24, 24);
1182 sc->expectFGColor(75, 75);
1183 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001184 }
1185
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001186 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001187
Haixia Shid5750962015-07-27 16:50:49 -07001188 {
1189 // This should set foreground to be 75% opaque.
1190 SCOPED_TRACE("after setAlpha");
1191 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001192 sc->expectBGColor(24, 24);
1193 sc->checkPixel(75, 75, 162, 63, 96);
1194 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001195 }
1196}
1197
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001198TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1199 sp<ScreenCapture> sc;
1200 {
1201 SCOPED_TRACE("before setLayerStack");
1202 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001203 sc->expectBGColor(24, 24);
1204 sc->expectFGColor(75, 75);
1205 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001206 }
1207
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001208 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001209 {
1210 // This should hide the foreground surface since it goes to a different
1211 // layer stack.
1212 SCOPED_TRACE("after setLayerStack");
1213 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001214 sc->expectBGColor(24, 24);
1215 sc->expectBGColor(75, 75);
1216 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001217 }
1218}
1219
1220TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1221 sp<ScreenCapture> sc;
1222 {
1223 SCOPED_TRACE("before setFlags");
1224 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001225 sc->expectBGColor(24, 24);
1226 sc->expectFGColor(75, 75);
1227 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001228 }
1229
Robert Carr4cdc58f2017-08-23 14:22:20 -07001230 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001231 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001232 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001233 {
1234 // This should hide the foreground surface
1235 SCOPED_TRACE("after setFlags");
1236 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001237 sc->expectBGColor(24, 24);
1238 sc->expectBGColor(75, 75);
1239 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001240 }
1241}
1242
1243TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1244 sp<ScreenCapture> sc;
1245 {
1246 SCOPED_TRACE("before setMatrix");
1247 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001248 sc->expectBGColor(24, 24);
1249 sc->expectFGColor(91, 96);
1250 sc->expectFGColor(96, 101);
1251 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001252 }
1253
Robert Carr4cdc58f2017-08-23 14:22:20 -07001254 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001255 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001256 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001257 {
1258 SCOPED_TRACE("after setMatrix");
1259 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001260 sc->expectBGColor(24, 24);
1261 sc->expectFGColor(91, 96);
1262 sc->expectBGColor(96, 91);
1263 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001264 }
1265}
1266
Robert Carr8d5227b2017-03-16 15:41:03 -07001267class GeometryLatchingTest : public LayerUpdateTest {
1268protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001269 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001270 SCOPED_TRACE(trace);
1271 ScreenCapture::captureScreen(&sc);
1272 // We find the leading edge of the FG surface.
1273 sc->expectFGColor(127, 127);
1274 sc->expectBGColor(128, 128);
1275 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001276
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001277 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001278
1279 void unlockFGBuffer() {
1280 sp<Surface> s = mFGSurfaceControl->getSurface();
1281 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1282 waitForPostedBuffers();
1283 }
1284
Robert Carr8d5227b2017-03-16 15:41:03 -07001285 void completeFGResize() {
1286 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1287 waitForPostedBuffers();
1288 }
1289 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001290 asTransaction([&](Transaction& t) {
1291 t.setSize(mFGSurfaceControl, 64, 64);
1292 t.setPosition(mFGSurfaceControl, 64, 64);
1293 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1294 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1295 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001296
1297 EXPECT_INITIAL_STATE("After restoring initial state");
1298 }
1299 sp<ScreenCapture> sc;
1300};
1301
1302TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1303 EXPECT_INITIAL_STATE("before anything");
1304
1305 // By default position can be updated even while
1306 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001307 asTransaction([&](Transaction& t) {
1308 t.setSize(mFGSurfaceControl, 32, 32);
1309 t.setPosition(mFGSurfaceControl, 100, 100);
1310 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001311
1312 {
1313 SCOPED_TRACE("After moving surface");
1314 ScreenCapture::captureScreen(&sc);
1315 // If we moved, the FG Surface should cover up what was previously BG
1316 // however if we didn't move the FG wouldn't be large enough now.
1317 sc->expectFGColor(163, 163);
1318 }
1319
1320 restoreInitialState();
1321
1322 // Now we repeat with setGeometryAppliesWithResize
1323 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001324 asTransaction([&](Transaction& t) {
1325 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1326 t.setSize(mFGSurfaceControl, 32, 32);
1327 t.setPosition(mFGSurfaceControl, 100, 100);
1328 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001329
1330 {
1331 SCOPED_TRACE("While resize is pending");
1332 ScreenCapture::captureScreen(&sc);
1333 // This time we shouldn't have moved, so the BG color
1334 // should still be visible.
1335 sc->expectBGColor(128, 128);
1336 }
1337
1338 completeFGResize();
1339
1340 {
1341 SCOPED_TRACE("After the resize");
1342 ScreenCapture::captureScreen(&sc);
1343 // But after the resize completes, we should move
1344 // and the FG should be visible here.
1345 sc->expectFGColor(128, 128);
1346 }
1347}
1348
1349class CropLatchingTest : public GeometryLatchingTest {
1350protected:
1351 void EXPECT_CROPPED_STATE(const char* trace) {
1352 SCOPED_TRACE(trace);
1353 ScreenCapture::captureScreen(&sc);
1354 // The edge should be moved back one pixel by our crop.
1355 sc->expectFGColor(126, 126);
1356 sc->expectBGColor(127, 127);
1357 sc->expectBGColor(128, 128);
1358 }
chaviw59f5c562017-06-28 16:39:06 -07001359
1360 void EXPECT_RESIZE_STATE(const char* trace) {
1361 SCOPED_TRACE(trace);
1362 ScreenCapture::captureScreen(&sc);
1363 // The FG is now resized too 128,128 at 64,64
1364 sc->expectFGColor(64, 64);
1365 sc->expectFGColor(191, 191);
1366 sc->expectBGColor(192, 192);
1367 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001368};
1369
1370TEST_F(CropLatchingTest, CropLatching) {
1371 EXPECT_INITIAL_STATE("before anything");
1372 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001373 asTransaction([&](Transaction& t) {
1374 t.setSize(mFGSurfaceControl, 128, 128);
1375 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1376 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001377
1378 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1379
1380 restoreInitialState();
1381
Robert Carr4cdc58f2017-08-23 14:22:20 -07001382 asTransaction([&](Transaction& t) {
1383 t.setSize(mFGSurfaceControl, 128, 128);
1384 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1385 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1386 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001387
1388 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1389
1390 completeFGResize();
1391
1392 EXPECT_CROPPED_STATE("after the resize finishes");
1393}
1394
1395TEST_F(CropLatchingTest, FinalCropLatching) {
1396 EXPECT_INITIAL_STATE("before anything");
1397 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001398 asTransaction([&](Transaction& t) {
1399 t.setSize(mFGSurfaceControl, 128, 128);
1400 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1401 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001402
1403 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1404
1405 restoreInitialState();
1406
Robert Carr4cdc58f2017-08-23 14:22:20 -07001407 asTransaction([&](Transaction& t) {
1408 t.setSize(mFGSurfaceControl, 128, 128);
1409 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1410 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1411 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001412
1413 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1414
1415 completeFGResize();
1416
1417 EXPECT_CROPPED_STATE("after the resize finishes");
1418}
1419
Robert Carr7bf247e2017-05-18 14:02:49 -07001420// In this test we ensure that setGeometryAppliesWithResize actually demands
1421// a buffer of the new size, and not just any size.
1422TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1423 EXPECT_INITIAL_STATE("before anything");
1424 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001425 asTransaction([&](Transaction& t) {
1426 t.setSize(mFGSurfaceControl, 128, 128);
1427 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1428 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001429
1430 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1431
1432 restoreInitialState();
1433
1434 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1435 // initiating the resize.
1436 lockAndFillFGBuffer();
1437
Robert Carr4cdc58f2017-08-23 14:22:20 -07001438 asTransaction([&](Transaction& t) {
1439 t.setSize(mFGSurfaceControl, 128, 128);
1440 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1441 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1442 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001443
1444 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1445
1446 // We now submit our old buffer, at the old size, and ensure it doesn't
1447 // trigger geometry latching.
1448 unlockFGBuffer();
1449
1450 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1451
1452 completeFGResize();
1453
1454 EXPECT_CROPPED_STATE("after the resize finishes");
1455}
1456
1457TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1458 EXPECT_INITIAL_STATE("before anything");
1459 // In this scenario, we attempt to set the final crop a second time while the resize
1460 // is still pending, and ensure we are successful. Success meaning the second crop
1461 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001462 asTransaction([&](Transaction& t) {
1463 t.setSize(mFGSurfaceControl, 128, 128);
1464 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1465 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1466 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001467
chaviw59f5c562017-06-28 16:39:06 -07001468 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1469
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001470 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001471
chaviw59f5c562017-06-28 16:39:06 -07001472 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001473
1474 completeFGResize();
1475
chaviw59f5c562017-06-28 16:39:06 -07001476 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001477}
1478
Pablo Ceballos05289c22016-04-14 15:49:55 -07001479TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1480 sp<ScreenCapture> sc;
1481 {
1482 SCOPED_TRACE("before anything");
1483 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001484 sc->expectBGColor(32, 32);
1485 sc->expectFGColor(96, 96);
1486 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001487 }
1488
1489 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001490 asTransaction([&](Transaction& t) {
1491 t.setAlpha(mFGSurfaceControl, 0.75);
1492 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001493 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001494 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001495
Robert Carr4cdc58f2017-08-23 14:22:20 -07001496 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001497 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001498 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001499 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001500 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001501
1502 {
1503 SCOPED_TRACE("before any trigger");
1504 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001505 sc->expectBGColor(32, 32);
1506 sc->expectFGColor(96, 96);
1507 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001508 }
1509
1510 // should trigger the first deferred transaction, but not the second one
1511 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1512 {
1513 SCOPED_TRACE("after first trigger");
1514 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001515 sc->expectBGColor(32, 32);
1516 sc->checkPixel(96, 96, 162, 63, 96);
1517 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001518 }
1519
1520 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001521 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001522
1523 // trigger the second deferred transaction
1524 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1525 {
1526 SCOPED_TRACE("after second trigger");
1527 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001528 sc->expectBGColor(32, 32);
1529 sc->expectBGColor(96, 96);
1530 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001531 }
1532}
1533
Robert Carrdb66e622017-04-10 16:55:57 -07001534TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1535 sp<ScreenCapture> sc;
1536 {
1537 SCOPED_TRACE("before adding relative surface");
1538 ScreenCapture::captureScreen(&sc);
1539 sc->expectBGColor(24, 24);
1540 sc->expectFGColor(75, 75);
1541 sc->expectBGColor(145, 145);
1542 }
1543
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001544 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1545 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001546 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1547 waitForPostedBuffers();
1548
1549 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001550 asTransaction([&](Transaction& t) {
1551 t.setPosition(relativeSurfaceControl, 64, 64);
1552 t.show(relativeSurfaceControl);
1553 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1554 });
Robert Carrdb66e622017-04-10 16:55:57 -07001555
1556 {
1557 SCOPED_TRACE("after adding relative surface");
1558 ScreenCapture::captureScreen(&sc);
1559 // our relative surface should be visible now.
1560 sc->checkPixel(75, 75, 255, 177, 177);
1561 }
1562
1563 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001564 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001565
1566 {
1567 SCOPED_TRACE("after set layer");
1568 ScreenCapture::captureScreen(&sc);
1569 // now the FG surface should be visible again.
1570 sc->expectFGColor(75, 75);
1571 }
1572}
1573
Robert Carre392b552017-09-19 12:16:05 -07001574TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1575 sp<ScreenCapture> sc;
1576
1577 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001578 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1579 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1580 sp<SurfaceControl> childBuffer =
1581 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1582 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001583 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1584
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001585 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001586
1587 {
1588 ScreenCapture::captureScreen(&sc);
1589 sc->expectChildColor(73, 73);
1590 sc->expectFGColor(74, 74);
1591 }
1592
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001593 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001594
1595 {
1596 ScreenCapture::captureScreen(&sc);
1597 sc->expectChildColor(73, 73);
1598 sc->expectChildColor(74, 74);
1599 }
1600}
1601
Robert Carr2c5f6d22017-09-26 12:30:35 -07001602TEST_F(LayerUpdateTest, MergingTransactions) {
1603 sp<ScreenCapture> sc;
1604 {
1605 SCOPED_TRACE("before move");
1606 ScreenCapture::captureScreen(&sc);
1607 sc->expectBGColor(0, 12);
1608 sc->expectFGColor(75, 75);
1609 sc->expectBGColor(145, 145);
1610 }
1611
1612 Transaction t1, t2;
1613 t1.setPosition(mFGSurfaceControl, 128, 128);
1614 t2.setPosition(mFGSurfaceControl, 0, 0);
1615 // We expect that the position update from t2 now
1616 // overwrites the position update from t1.
1617 t1.merge(std::move(t2));
1618 t1.apply();
1619
1620 {
1621 ScreenCapture::captureScreen(&sc);
1622 sc->expectFGColor(1, 1);
1623 }
1624}
1625
Robert Carr1f0a16a2016-10-24 16:27:39 -07001626class ChildLayerTest : public LayerUpdateTest {
1627protected:
1628 void SetUp() override {
1629 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001630 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1631 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001632 fillSurfaceRGBA8(mChild, 200, 200, 200);
1633
1634 {
1635 SCOPED_TRACE("before anything");
1636 ScreenCapture::captureScreen(&mCapture);
1637 mCapture->expectChildColor(64, 64);
1638 }
1639 }
1640 void TearDown() override {
1641 LayerUpdateTest::TearDown();
1642 mChild = 0;
1643 }
1644
1645 sp<SurfaceControl> mChild;
1646 sp<ScreenCapture> mCapture;
1647};
1648
1649TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001650 asTransaction([&](Transaction& t) {
1651 t.show(mChild);
1652 t.setPosition(mChild, 10, 10);
1653 t.setPosition(mFGSurfaceControl, 64, 64);
1654 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655
1656 {
1657 ScreenCapture::captureScreen(&mCapture);
1658 // Top left of foreground must now be visible
1659 mCapture->expectFGColor(64, 64);
1660 // But 10 pixels in we should see the child surface
1661 mCapture->expectChildColor(74, 74);
1662 // And 10 more pixels we should be back to the foreground surface
1663 mCapture->expectFGColor(84, 84);
1664 }
1665
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001666 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667
1668 {
1669 ScreenCapture::captureScreen(&mCapture);
1670 // Top left of foreground should now be at 0, 0
1671 mCapture->expectFGColor(0, 0);
1672 // But 10 pixels in we should see the child surface
1673 mCapture->expectChildColor(10, 10);
1674 // And 10 more pixels we should be back to the foreground surface
1675 mCapture->expectFGColor(20, 20);
1676 }
1677}
1678
Robert Carr41b08b52017-06-01 16:11:34 -07001679TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001680 asTransaction([&](Transaction& t) {
1681 t.show(mChild);
1682 t.setPosition(mChild, 0, 0);
1683 t.setPosition(mFGSurfaceControl, 0, 0);
1684 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1685 });
Robert Carr41b08b52017-06-01 16:11:34 -07001686
1687 {
1688 ScreenCapture::captureScreen(&mCapture);
1689 mCapture->expectChildColor(0, 0);
1690 mCapture->expectChildColor(4, 4);
1691 mCapture->expectBGColor(5, 5);
1692 }
1693}
1694
1695TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001696 asTransaction([&](Transaction& t) {
1697 t.show(mChild);
1698 t.setPosition(mChild, 0, 0);
1699 t.setPosition(mFGSurfaceControl, 0, 0);
1700 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1701 });
Robert Carr41b08b52017-06-01 16:11:34 -07001702
1703 {
1704 ScreenCapture::captureScreen(&mCapture);
1705 mCapture->expectChildColor(0, 0);
1706 mCapture->expectChildColor(4, 4);
1707 mCapture->expectBGColor(5, 5);
1708 }
1709}
1710
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001712 asTransaction([&](Transaction& t) {
1713 t.show(mChild);
1714 t.setPosition(mFGSurfaceControl, 0, 0);
1715 t.setPosition(mChild, 63, 63);
1716 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001717
1718 {
1719 ScreenCapture::captureScreen(&mCapture);
1720 mCapture->expectFGColor(0, 0);
1721 // Last pixel in foreground should now be the child.
1722 mCapture->expectChildColor(63, 63);
1723 // But the child should be constrained and the next pixel
1724 // must be the background
1725 mCapture->expectBGColor(64, 64);
1726 }
1727}
1728
1729TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001730 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001731
1732 // Find the boundary between the parent and child
1733 {
1734 ScreenCapture::captureScreen(&mCapture);
1735 mCapture->expectChildColor(9, 9);
1736 mCapture->expectFGColor(10, 10);
1737 }
1738
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001739 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740
1741 // The boundary should be twice as far from the origin now.
1742 // The pixels from the last test should all be child now
1743 {
1744 ScreenCapture::captureScreen(&mCapture);
1745 mCapture->expectChildColor(9, 9);
1746 mCapture->expectChildColor(10, 10);
1747 mCapture->expectChildColor(19, 19);
1748 mCapture->expectFGColor(20, 20);
1749 }
1750}
Robert Carr9524cb32017-02-13 11:32:32 -08001751
Robert Carr6452f122017-03-21 10:41:29 -07001752TEST_F(ChildLayerTest, ChildLayerAlpha) {
1753 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1754 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1755 fillSurfaceRGBA8(mChild, 0, 254, 0);
1756 waitForPostedBuffers();
1757
Robert Carr4cdc58f2017-08-23 14:22:20 -07001758 asTransaction([&](Transaction& t) {
1759 t.show(mChild);
1760 t.setPosition(mChild, 0, 0);
1761 t.setPosition(mFGSurfaceControl, 0, 0);
1762 });
Robert Carr6452f122017-03-21 10:41:29 -07001763
1764 {
1765 ScreenCapture::captureScreen(&mCapture);
1766 // Unblended child color
1767 mCapture->checkPixel(0, 0, 0, 254, 0);
1768 }
1769
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001770 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001771
1772 {
1773 ScreenCapture::captureScreen(&mCapture);
1774 // Child and BG blended.
1775 mCapture->checkPixel(0, 0, 127, 127, 0);
1776 }
1777
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001778 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001779
1780 {
1781 ScreenCapture::captureScreen(&mCapture);
1782 // Child and BG blended.
1783 mCapture->checkPixel(0, 0, 95, 64, 95);
1784 }
1785}
1786
Robert Carr9524cb32017-02-13 11:32:32 -08001787TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001788 asTransaction([&](Transaction& t) {
1789 t.show(mChild);
1790 t.setPosition(mChild, 10, 10);
1791 t.setPosition(mFGSurfaceControl, 64, 64);
1792 });
Robert Carr9524cb32017-02-13 11:32:32 -08001793
1794 {
1795 ScreenCapture::captureScreen(&mCapture);
1796 // Top left of foreground must now be visible
1797 mCapture->expectFGColor(64, 64);
1798 // But 10 pixels in we should see the child surface
1799 mCapture->expectChildColor(74, 74);
1800 // And 10 more pixels we should be back to the foreground surface
1801 mCapture->expectFGColor(84, 84);
1802 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001803
1804 asTransaction([&](Transaction& t) {
1805 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1806 });
1807
Robert Carr9524cb32017-02-13 11:32:32 -08001808 {
1809 ScreenCapture::captureScreen(&mCapture);
1810 mCapture->expectFGColor(64, 64);
1811 // In reparenting we should have exposed the entire foreground surface.
1812 mCapture->expectFGColor(74, 74);
1813 // And the child layer should now begin at 10, 10 (since the BG
1814 // layer is at (0, 0)).
1815 mCapture->expectBGColor(9, 9);
1816 mCapture->expectChildColor(10, 10);
1817 }
1818}
1819
chaviw161410b02017-07-27 10:46:08 -07001820TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001821 asTransaction([&](Transaction& t) {
1822 t.show(mChild);
1823 t.setPosition(mChild, 10, 10);
1824 t.setPosition(mFGSurfaceControl, 64, 64);
1825 });
Robert Carr9524cb32017-02-13 11:32:32 -08001826
1827 {
1828 ScreenCapture::captureScreen(&mCapture);
1829 // Top left of foreground must now be visible
1830 mCapture->expectFGColor(64, 64);
1831 // But 10 pixels in we should see the child surface
1832 mCapture->expectChildColor(74, 74);
1833 // And 10 more pixels we should be back to the foreground surface
1834 mCapture->expectFGColor(84, 84);
1835 }
1836
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001837 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001838
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001839 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001840
chaviw161410b02017-07-27 10:46:08 -07001841 // Since the child has the same client as the parent, it will not get
1842 // detached and will be hidden.
1843 {
1844 ScreenCapture::captureScreen(&mCapture);
1845 mCapture->expectFGColor(64, 64);
1846 mCapture->expectFGColor(74, 74);
1847 mCapture->expectFGColor(84, 84);
1848 }
1849}
1850
1851TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1852 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001853 sp<SurfaceControl> mChildNewClient =
1854 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1855 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001856
1857 ASSERT_TRUE(mChildNewClient != NULL);
1858 ASSERT_TRUE(mChildNewClient->isValid());
1859
1860 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1861
Robert Carr4cdc58f2017-08-23 14:22:20 -07001862 asTransaction([&](Transaction& t) {
1863 t.hide(mChild);
1864 t.show(mChildNewClient);
1865 t.setPosition(mChildNewClient, 10, 10);
1866 t.setPosition(mFGSurfaceControl, 64, 64);
1867 });
chaviw161410b02017-07-27 10:46:08 -07001868
1869 {
1870 ScreenCapture::captureScreen(&mCapture);
1871 // Top left of foreground must now be visible
1872 mCapture->expectFGColor(64, 64);
1873 // But 10 pixels in we should see the child surface
1874 mCapture->expectChildColor(74, 74);
1875 // And 10 more pixels we should be back to the foreground surface
1876 mCapture->expectFGColor(84, 84);
1877 }
1878
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001879 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001880
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001881 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001882
Robert Carr9524cb32017-02-13 11:32:32 -08001883 // Nothing should have changed.
1884 {
1885 ScreenCapture::captureScreen(&mCapture);
1886 mCapture->expectFGColor(64, 64);
1887 mCapture->expectChildColor(74, 74);
1888 mCapture->expectFGColor(84, 84);
1889 }
1890}
1891
Robert Carr9b429f42017-04-17 14:56:57 -07001892TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001893 asTransaction([&](Transaction& t) {
1894 t.show(mChild);
1895 t.setPosition(mChild, 0, 0);
1896 t.setPosition(mFGSurfaceControl, 0, 0);
1897 });
Robert Carr9b429f42017-04-17 14:56:57 -07001898
1899 {
1900 ScreenCapture::captureScreen(&mCapture);
1901 // We've positioned the child in the top left.
1902 mCapture->expectChildColor(0, 0);
1903 // But it's only 10x10.
1904 mCapture->expectFGColor(10, 10);
1905 }
1906
Robert Carr4cdc58f2017-08-23 14:22:20 -07001907 asTransaction([&](Transaction& t) {
1908 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1909 // We cause scaling by 2.
1910 t.setSize(mFGSurfaceControl, 128, 128);
1911 });
Robert Carr9b429f42017-04-17 14:56:57 -07001912
1913 {
1914 ScreenCapture::captureScreen(&mCapture);
1915 // We've positioned the child in the top left.
1916 mCapture->expectChildColor(0, 0);
1917 mCapture->expectChildColor(10, 10);
1918 mCapture->expectChildColor(19, 19);
1919 // And now it should be scaled all the way to 20x20
1920 mCapture->expectFGColor(20, 20);
1921 }
1922}
1923
Robert Carr1725eee2017-04-26 18:32:15 -07001924// Regression test for b/37673612
1925TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001926 asTransaction([&](Transaction& t) {
1927 t.show(mChild);
1928 t.setPosition(mChild, 0, 0);
1929 t.setPosition(mFGSurfaceControl, 0, 0);
1930 });
Robert Carr1725eee2017-04-26 18:32:15 -07001931
1932 {
1933 ScreenCapture::captureScreen(&mCapture);
1934 // We've positioned the child in the top left.
1935 mCapture->expectChildColor(0, 0);
1936 // But it's only 10x10.
1937 mCapture->expectFGColor(10, 10);
1938 }
Robert Carr1725eee2017-04-26 18:32:15 -07001939 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1940 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001941 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07001942 sp<Surface> s = mFGSurfaceControl->getSurface();
1943 auto anw = static_cast<ANativeWindow*>(s.get());
1944 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1945 native_window_set_buffers_dimensions(anw, 64, 128);
1946 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1947 waitForPostedBuffers();
1948
1949 {
1950 // The child should still be in the same place and not have any strange scaling as in
1951 // b/37673612.
1952 ScreenCapture::captureScreen(&mCapture);
1953 mCapture->expectChildColor(0, 0);
1954 mCapture->expectFGColor(10, 10);
1955 }
1956}
1957
Dan Stoza412903f2017-04-27 13:42:17 -07001958TEST_F(ChildLayerTest, Bug36858924) {
1959 // Destroy the child layer
1960 mChild.clear();
1961
1962 // Now recreate it as hidden
1963 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1964 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
1965 mFGSurfaceControl.get());
1966
1967 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07001968 asTransaction([&](Transaction& t) {
1969 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001970 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001971 t.show(mChild);
1972 });
Dan Stoza412903f2017-04-27 13:42:17 -07001973
1974 // Render the foreground surface a few times
1975 //
1976 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
1977 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
1978 // never acquire/release the first buffer
1979 ALOGI("Filling 1");
1980 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1981 ALOGI("Filling 2");
1982 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
1983 ALOGI("Filling 3");
1984 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
1985 ALOGI("Filling 4");
1986 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
1987}
1988
chaviwf1961f72017-09-18 16:41:07 -07001989TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001990 asTransaction([&](Transaction& t) {
1991 t.show(mChild);
1992 t.setPosition(mChild, 10, 10);
1993 t.setPosition(mFGSurfaceControl, 64, 64);
1994 });
chaviw06178942017-07-27 10:25:59 -07001995
1996 {
1997 ScreenCapture::captureScreen(&mCapture);
1998 // Top left of foreground must now be visible
1999 mCapture->expectFGColor(64, 64);
2000 // But 10 pixels in we should see the child surface
2001 mCapture->expectChildColor(74, 74);
2002 // And 10 more pixels we should be back to the foreground surface
2003 mCapture->expectFGColor(84, 84);
2004 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002005
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002006 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002007
chaviw06178942017-07-27 10:25:59 -07002008 {
2009 ScreenCapture::captureScreen(&mCapture);
2010 mCapture->expectFGColor(64, 64);
2011 // In reparenting we should have exposed the entire foreground surface.
2012 mCapture->expectFGColor(74, 74);
2013 // And the child layer should now begin at 10, 10 (since the BG
2014 // layer is at (0, 0)).
2015 mCapture->expectBGColor(9, 9);
2016 mCapture->expectChildColor(10, 10);
2017 }
2018}
2019
chaviwf1961f72017-09-18 16:41:07 -07002020TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002021 asTransaction([&](Transaction& t) {
2022 t.show(mChild);
2023 t.setPosition(mChild, 10, 10);
2024 t.setPosition(mFGSurfaceControl, 64, 64);
2025 });
chaviwf1961f72017-09-18 16:41:07 -07002026
2027 {
2028 ScreenCapture::captureScreen(&mCapture);
2029 // Top left of foreground must now be visible
2030 mCapture->expectFGColor(64, 64);
2031 // But 10 pixels in we should see the child surface
2032 mCapture->expectChildColor(74, 74);
2033 // And 10 more pixels we should be back to the foreground surface
2034 mCapture->expectFGColor(84, 84);
2035 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002036 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002037 {
2038 ScreenCapture::captureScreen(&mCapture);
2039 // Nothing should have changed.
2040 mCapture->expectFGColor(64, 64);
2041 mCapture->expectChildColor(74, 74);
2042 mCapture->expectFGColor(84, 84);
2043 }
2044}
2045
2046TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002047 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2048 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002049 ASSERT_TRUE(newSurface != NULL);
2050 ASSERT_TRUE(newSurface->isValid());
2051
2052 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002053 asTransaction([&](Transaction& t) {
2054 t.hide(mChild);
2055 t.show(newSurface);
2056 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002057 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002058 t.setPosition(mFGSurfaceControl, 64, 64);
2059 });
chaviwf1961f72017-09-18 16:41:07 -07002060
2061 {
2062 ScreenCapture::captureScreen(&mCapture);
2063 // Top left of foreground must now be visible
2064 mCapture->expectFGColor(64, 64);
2065 // At 10, 10 we should see the new surface
2066 mCapture->checkPixel(10, 10, 63, 195, 63);
2067 }
2068
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002069 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002070
2071 {
2072 ScreenCapture::captureScreen(&mCapture);
2073 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2074 // mFGSurface, putting it at 74, 74.
2075 mCapture->expectFGColor(64, 64);
2076 mCapture->checkPixel(74, 74, 63, 195, 63);
2077 mCapture->expectFGColor(84, 84);
2078 }
2079}
2080
chaviwc9674332017-08-28 12:32:18 -07002081TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002082 sp<SurfaceControl> grandchild =
2083 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2084 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002085 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2086
2087 {
2088 ScreenCapture::captureScreen(&mCapture);
2089 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2090 // which begins at 64, 64
2091 mCapture->checkPixel(64, 64, 50, 50, 50);
2092 }
2093}
2094
Robert Carr503c7042017-09-27 15:06:08 -07002095TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002096 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2097 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002098 fillSurfaceRGBA8(relative, 255, 255, 255);
2099
2100 Transaction t;
2101 t.setLayer(relative, INT32_MAX)
2102 .setRelativeLayer(mChild, relative->getHandle(), 1)
2103 .setPosition(mFGSurfaceControl, 0, 0)
2104 .apply(true);
2105
2106 // We expect that the child should have been elevated above our
2107 // INT_MAX layer even though it's not a child of it.
2108 {
2109 ScreenCapture::captureScreen(&mCapture);
2110 mCapture->expectChildColor(0, 0);
2111 mCapture->expectChildColor(9, 9);
2112 mCapture->checkPixel(10, 10, 255, 255, 255);
2113 }
2114}
2115
chaviw13fdc492017-06-27 12:40:18 -07002116class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002117protected:
chaviw13fdc492017-06-27 12:40:18 -07002118 void SetUp() override {
2119 LayerUpdateTest::SetUp();
2120
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002121 mLayerColorControl =
2122 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2123 PIXEL_FORMAT_RGBA_8888,
2124 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002125
2126 ASSERT_TRUE(mLayerColorControl != NULL);
2127 ASSERT_TRUE(mLayerColorControl->isValid());
2128
Robert Carr4cdc58f2017-08-23 14:22:20 -07002129 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002130 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002131 t.setPosition(mLayerColorControl, 140, 140);
2132 t.hide(mLayerColorControl);
2133 t.hide(mFGSurfaceControl);
2134 });
chaviw13fdc492017-06-27 12:40:18 -07002135 }
2136
2137 void TearDown() override {
2138 LayerUpdateTest::TearDown();
2139 mLayerColorControl = 0;
2140 }
2141
2142 sp<SurfaceControl> mLayerColorControl;
2143};
2144
2145TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2146 sp<ScreenCapture> sc;
2147
2148 {
2149 SCOPED_TRACE("before setColor");
2150 ScreenCapture::captureScreen(&sc);
2151 sc->expectBGColor(145, 145);
2152 }
2153
Robert Carr4cdc58f2017-08-23 14:22:20 -07002154 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002155 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002156 t.setColor(mLayerColorControl, color);
2157 t.show(mLayerColorControl);
2158 });
chaviw13fdc492017-06-27 12:40:18 -07002159
chaviw13fdc492017-06-27 12:40:18 -07002160 {
2161 // There should now be a color
2162 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002163
chaviw13fdc492017-06-27 12:40:18 -07002164 ScreenCapture::captureScreen(&sc);
2165 sc->checkPixel(145, 145, 43, 207, 131);
2166 }
2167}
2168
2169TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2170 sp<ScreenCapture> sc;
2171 {
2172 SCOPED_TRACE("before setColor");
2173 ScreenCapture::captureScreen(&sc);
2174 sc->expectBGColor(145, 145);
2175 }
2176
Robert Carr4cdc58f2017-08-23 14:22:20 -07002177 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002178 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002179 t.setColor(mLayerColorControl, color);
2180 t.setAlpha(mLayerColorControl, .75f);
2181 t.show(mLayerColorControl);
2182 });
2183
chaviw13fdc492017-06-27 12:40:18 -07002184 {
2185 // There should now be a color with .75 alpha
2186 SCOPED_TRACE("after setColor");
2187 ScreenCapture::captureScreen(&sc);
2188 sc->checkPixel(145, 145, 48, 171, 147);
2189 }
2190}
2191
2192TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2193 sp<ScreenCapture> sc;
2194 {
2195 SCOPED_TRACE("before setColor");
2196 ScreenCapture::captureScreen(&sc);
2197 sc->expectBGColor(145, 145);
2198 }
2199
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002200 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002201
chaviw13fdc492017-06-27 12:40:18 -07002202 {
2203 // There should now be set to 0,0,0 (black) as default.
2204 SCOPED_TRACE("after setColor");
2205 ScreenCapture::captureScreen(&sc);
2206 sc->checkPixel(145, 145, 0, 0, 0);
2207 }
2208}
2209
chaviwa76b2712017-09-20 12:02:26 -07002210class ScreenCaptureTest : public LayerUpdateTest {
2211protected:
2212 std::unique_ptr<CaptureLayer> mCapture;
2213};
2214
2215TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2216 auto bgHandle = mBGSurfaceControl->getHandle();
2217 CaptureLayer::captureScreen(&mCapture, bgHandle);
2218 mCapture->expectBGColor(0, 0);
2219 // Doesn't capture FG layer which is at 64, 64
2220 mCapture->expectBGColor(64, 64);
2221}
2222
2223TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2224 auto fgHandle = mFGSurfaceControl->getHandle();
2225
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002226 sp<SurfaceControl> child =
2227 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2228 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002229 fillSurfaceRGBA8(child, 200, 200, 200);
2230
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002231 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002232
2233 // Captures mFGSurfaceControl layer and its child.
2234 CaptureLayer::captureScreen(&mCapture, fgHandle);
2235 mCapture->expectFGColor(10, 10);
2236 mCapture->expectChildColor(0, 0);
2237}
2238
2239TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2240 auto fgHandle = mFGSurfaceControl->getHandle();
2241
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002242 sp<SurfaceControl> child =
2243 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2244 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002245 fillSurfaceRGBA8(child, 200, 200, 200);
2246
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002247 sp<SurfaceControl> grandchild =
2248 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2249 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002250
2251 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2252 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002253 .show(child)
2254 .setPosition(grandchild, 5, 5)
2255 .show(grandchild)
2256 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002257
2258 // Captures mFGSurfaceControl, its child, and the grandchild.
2259 CaptureLayer::captureScreen(&mCapture, fgHandle);
2260 mCapture->expectFGColor(10, 10);
2261 mCapture->expectChildColor(0, 0);
2262 mCapture->checkPixel(5, 5, 50, 50, 50);
2263}
2264
2265TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002266 sp<SurfaceControl> child =
2267 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2268 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002269 fillSurfaceRGBA8(child, 200, 200, 200);
2270 auto childHandle = child->getHandle();
2271
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002272 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002273
2274 // Captures only the child layer, and not the parent.
2275 CaptureLayer::captureScreen(&mCapture, childHandle);
2276 mCapture->expectChildColor(0, 0);
2277 mCapture->expectChildColor(9, 9);
2278}
2279
2280TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002281 sp<SurfaceControl> child =
2282 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2283 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002284 fillSurfaceRGBA8(child, 200, 200, 200);
2285 auto childHandle = child->getHandle();
2286
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002287 sp<SurfaceControl> grandchild =
2288 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2289 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002290 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2291
2292 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002293 .show(child)
2294 .setPosition(grandchild, 5, 5)
2295 .show(grandchild)
2296 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002297
2298 auto grandchildHandle = grandchild->getHandle();
2299
2300 // Captures only the grandchild.
2301 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2302 mCapture->checkPixel(0, 0, 50, 50, 50);
2303 mCapture->checkPixel(4, 4, 50, 50, 50);
2304}
2305
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002306} // namespace android