blob: 63f6a41346343a4038f52da25eea93b956000e91 [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 Wu93853fe2017-11-02 08:30:27 -070053 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070054 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070055 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070056};
57
58const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070059const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070060const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070061const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070062const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070063const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070064
65std::ostream& operator<<(std::ostream& os, const Color& color) {
66 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
67 return os;
68}
69
70// Fill a region with the specified color.
71void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
72 int32_t x = rect.left;
73 int32_t y = rect.top;
74 int32_t width = rect.right - rect.left;
75 int32_t height = rect.bottom - rect.top;
76
77 if (x < 0) {
78 width += x;
79 x = 0;
80 }
81 if (y < 0) {
82 height += y;
83 y = 0;
84 }
85 if (x + width > buffer.width) {
86 x = std::min(x, buffer.width);
87 width = buffer.width - x;
88 }
89 if (y + height > buffer.height) {
90 y = std::min(y, buffer.height);
91 height = buffer.height - y;
92 }
93
94 for (int32_t j = 0; j < height; j++) {
95 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
96 for (int32_t i = 0; i < width; i++) {
97 dst[0] = color.r;
98 dst[1] = color.g;
99 dst[2] = color.b;
100 dst[3] = color.a;
101 dst += 4;
102 }
103 }
104}
105
106// Check if a region has the specified color.
107void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect,
108 const Color& color, uint8_t tolerance) {
109 int32_t x = rect.left;
110 int32_t y = rect.top;
111 int32_t width = rect.right - rect.left;
112 int32_t height = rect.bottom - rect.top;
113
114 if (x + width > int32_t(buffer.width)) {
115 x = std::min(x, int32_t(buffer.width));
116 width = buffer.width - x;
117 }
118 if (y + height > int32_t(buffer.height)) {
119 y = std::min(y, int32_t(buffer.height));
120 height = buffer.height - y;
121 }
122
123 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
124 uint8_t tmp = a >= b ? a - b : b - a;
125 return tmp <= tolerance;
126 };
127 for (int32_t j = 0; j < height; j++) {
128 const uint8_t* src =
129 static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4;
130 for (int32_t i = 0; i < width; i++) {
131 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
132 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
133 << "pixel @ (" << x + i << ", " << y + j << "): "
134 << "expected (" << color << "), "
135 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
136 src += 4;
137 }
138 }
139}
140
141} // anonymous namespace
142
Robert Carr4cdc58f2017-08-23 14:22:20 -0700143using Transaction = SurfaceComposerClient::Transaction;
144
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700145// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700146static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
147 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800148 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700149 sp<Surface> s = sc->getSurface();
150 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800151 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
152 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700153 for (int y = 0; y < outBuffer.height; y++) {
154 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700155 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700156 pixel[0] = r;
157 pixel[1] = g;
158 pixel[2] = b;
159 pixel[3] = 255;
160 }
161 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700162 if (unlock) {
163 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
164 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700165}
166
167// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
168// individual pixel values for testing purposes.
169class ScreenCapture : public RefBase {
170public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700171 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
172 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700173 sp<IGraphicBufferProducer> producer;
174 sp<IGraphicBufferConsumer> consumer;
175 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -0700176 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700177 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700178 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700179 SurfaceComposerClient::Transaction().apply(true);
180
Chia-I Wu718daf82017-10-20 11:57:17 -0700181 ASSERT_EQ(NO_ERROR,
182 sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false));
Michael Lentine5a16a622015-05-21 13:48:24 -0700183 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700184 }
185
Chia-I Wu718daf82017-10-20 11:57:17 -0700186 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
187 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
188 expectBufferColor(mBuf, rect, color, tolerance);
189 }
190
191 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
192 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
193 const bool leftBorder = rect.left > 0;
194 const bool topBorder = rect.top > 0;
195 const bool rightBorder = rect.right < int32_t(mBuf.width);
196 const bool bottomBorder = rect.bottom < int32_t(mBuf.height);
197
198 if (topBorder) {
199 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
200 if (leftBorder) {
201 top.left -= 1;
202 }
203 if (rightBorder) {
204 top.right += 1;
205 }
206 expectColor(top, color, tolerance);
207 }
208 if (leftBorder) {
209 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
210 expectColor(left, color, tolerance);
211 }
212 if (rightBorder) {
213 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
214 expectColor(right, color, tolerance);
215 }
216 if (bottomBorder) {
217 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
218 if (leftBorder) {
219 bottom.left -= 1;
220 }
221 if (rightBorder) {
222 bottom.right += 1;
223 }
224 expectColor(bottom, color, tolerance);
225 }
226 }
227
Chia-I Wu93853fe2017-11-02 08:30:27 -0700228 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
229 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
230 uint8_t tolerance = 0) {
231 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
232
233 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
234 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
235 // avoid checking borders due to unspecified filtering behavior
236 const int32_t offsetX = filtered ? 2 : 0;
237 const int32_t offsetY = filtered ? 2 : 0;
238 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
239 tolerance);
240 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
241 tolerance);
242 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
243 tolerance);
244 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
245 bottomRight, tolerance);
246 }
247
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700248 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700249 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
250 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
251 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700252 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
253 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700254 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
255 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700256 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700257 }
258 }
259
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700260 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700261
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700262 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700263
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700264 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700265
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700266private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700267 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700268 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
269 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700270
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700271 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700272
273 sp<CpuConsumer> mCC;
274 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700275};
276
chaviwa76b2712017-09-20 12:02:26 -0700277class CaptureLayer {
278public:
279 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
280 sp<IGraphicBufferProducer> producer;
281 sp<IGraphicBufferConsumer> consumer;
282 BufferQueue::createBufferQueue(&producer, &consumer);
283 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
284 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700285 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700286 SurfaceComposerClient::Transaction().apply(true);
287 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
288 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
289 }
290
291 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
292 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
293 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
294 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
295 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
296 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700297 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700298 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
299 EXPECT_EQ(String8(), err) << err.string();
300 }
301 }
302
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700303 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700304
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700305 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700306
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700307 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700308
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700309 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700310 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
311 }
312
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700313 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700314
315private:
316 sp<CpuConsumer> mCC;
317 CpuConsumer::LockedBuffer mBuffer;
318};
319
Chia-I Wu718daf82017-10-20 11:57:17 -0700320class LayerTransactionTest : public ::testing::Test {
321protected:
322 void SetUp() override {
323 mClient = new SurfaceComposerClient;
324 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
325
326 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
327 }
328
329 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
330 uint32_t flags = 0) {
331 auto layer =
332 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
333 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
334
335 status_t error = Transaction()
336 .setLayerStack(layer, mDisplayLayerStack)
337 .setLayer(layer, mLayerZBase)
338 .apply();
339 if (error != NO_ERROR) {
340 ADD_FAILURE() << "failed to initialize SurfaceControl";
341 layer.clear();
342 }
343
344 return layer;
345 }
346
347 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
348 // wait for previous transactions (such as setSize) to complete
349 Transaction().apply(true);
350
351 ANativeWindow_Buffer buffer = {};
352 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
353
354 return buffer;
355 }
356
357 void postLayerBuffer(const sp<SurfaceControl>& layer) {
358 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
359
360 // wait for the newly posted buffer to be latched
361 waitForLayerBuffers();
362 }
363
364 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
365 ANativeWindow_Buffer buffer;
366 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
367 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
368 postLayerBuffer(layer);
369 }
370
Chia-I Wu93853fe2017-11-02 08:30:27 -0700371 void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft,
372 const Color& topRight, const Color& bottomLeft,
373 const Color& bottomRight) {
374 ANativeWindow_Buffer buffer;
375 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
376 ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0);
377
378 const int32_t halfW = buffer.width / 2;
379 const int32_t halfH = buffer.height / 2;
380 fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
381 fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight);
382 fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft);
383 fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight);
384
385 postLayerBuffer(layer);
386 }
387
Chia-I Wu718daf82017-10-20 11:57:17 -0700388 sp<ScreenCapture> screenshot() {
389 sp<ScreenCapture> screenshot;
390 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
391 return screenshot;
392 }
393
394 sp<SurfaceComposerClient> mClient;
395
396 sp<IBinder> mDisplay;
397 uint32_t mDisplayWidth;
398 uint32_t mDisplayHeight;
399 uint32_t mDisplayLayerStack;
400
401 // leave room for ~256 layers
402 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
403
404private:
405 void SetUpDisplay() {
406 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
407 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
408
409 // get display width/height
410 DisplayInfo info;
411 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
412 mDisplayWidth = info.w;
413 mDisplayHeight = info.h;
414
415 // After a new buffer is queued, SurfaceFlinger is notified and will
416 // latch the new buffer on next vsync. Let's heuristically wait for 3
417 // vsyncs.
418 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
419
420 mDisplayLayerStack = 0;
421 // set layer stack (b/68888219)
422 Transaction t;
423 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
424 t.apply();
425 }
426
427 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
428
429 int32_t mBufferPostDelay;
430};
431
432TEST_F(LayerTransactionTest, SetPositionBasic) {
433 sp<SurfaceControl> layer;
434 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
435 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
436
437 {
438 SCOPED_TRACE("default position");
439 auto shot = screenshot();
440 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
441 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
442 }
443
444 Transaction().setPosition(layer, 5, 10).apply();
445 {
446 SCOPED_TRACE("new position");
447 auto shot = screenshot();
448 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
449 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
450 }
451}
452
453TEST_F(LayerTransactionTest, SetPositionRounding) {
454 sp<SurfaceControl> layer;
455 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
456 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
457
458 // GLES requires only 4 bits of subpixel precision during rasterization
459 // XXX GLES composition does not match HWC composition due to precision
460 // loss (b/69315223)
461 const float epsilon = 1.0f / 16.0f;
462 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
463 {
464 SCOPED_TRACE("rounding down");
465 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
466 }
467
468 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
469 {
470 SCOPED_TRACE("rounding up");
471 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
472 }
473}
474
475TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
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 Transaction().setPosition(layer, -32, -32).apply();
481 {
482 SCOPED_TRACE("negative coordinates");
483 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
484 }
485
486 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
487 {
488 SCOPED_TRACE("positive coordinates");
489 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
490 }
491}
492
493TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
494 sp<SurfaceControl> layer;
495 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
496 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
497
498 // partially out of bounds
499 Transaction().setPosition(layer, -30, -30).apply();
500 {
501 SCOPED_TRACE("negative coordinates");
502 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
503 }
504
505 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
506 {
507 SCOPED_TRACE("positive coordinates");
508 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
509 mDisplayHeight),
510 Color::RED);
511 }
512}
513
514TEST_F(LayerTransactionTest, SetPositionWithResize) {
515 sp<SurfaceControl> layer;
516 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
517 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
518
519 // setPosition is applied immediately by default, with or without resize
520 // pending
521 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
522 {
523 SCOPED_TRACE("resize pending");
524 auto shot = screenshot();
525 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
526 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
527 }
528
529 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
530 {
531 SCOPED_TRACE("resize applied");
532 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
533 }
534}
535
536TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
537 sp<SurfaceControl> layer;
538 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
539 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
540
541 // request setPosition to be applied with the next resize
542 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
543 {
544 SCOPED_TRACE("new position pending");
545 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
546 }
547
548 Transaction().setPosition(layer, 15, 20).apply();
549 {
550 SCOPED_TRACE("pending new position modified");
551 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
552 }
553
554 Transaction().setSize(layer, 64, 64).apply();
555 {
556 SCOPED_TRACE("resize pending");
557 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
558 }
559
560 // finally resize and latch the buffer
561 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
562 {
563 SCOPED_TRACE("new position applied");
564 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
565 }
566}
567
568TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
569 sp<SurfaceControl> layer;
570 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
571 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
572
573 // setPosition is not immediate even with SCALE_TO_WINDOW override
574 Transaction()
575 .setPosition(layer, 5, 10)
576 .setSize(layer, 64, 64)
577 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
578 .setGeometryAppliesWithResize(layer)
579 .apply();
580 {
581 SCOPED_TRACE("new position pending");
582 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
583 }
584
585 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
586 {
587 SCOPED_TRACE("new position applied");
588 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
589 }
590}
591
Chia-I Wu0eaea312017-10-31 10:14:40 -0700592TEST_F(LayerTransactionTest, SetSizeBasic) {
593 sp<SurfaceControl> layer;
594 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
595 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
596
597 Transaction().setSize(layer, 64, 64).apply();
598 {
599 SCOPED_TRACE("resize pending");
600 auto shot = screenshot();
601 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
602 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
603 }
604
605 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
606 {
607 SCOPED_TRACE("resize applied");
608 auto shot = screenshot();
609 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
610 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
611 }
612}
613
614TEST_F(LayerTransactionTest, SetSizeInvalid) {
615 // cannot test robustness against invalid sizes (zero or really huge)
616}
617
618TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
619 sp<SurfaceControl> layer;
620 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
621 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
622
623 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
624 Transaction()
625 .setSize(layer, 64, 64)
626 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
627 .apply();
628 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
629}
630
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700631TEST_F(LayerTransactionTest, SetZBasic) {
632 sp<SurfaceControl> layerR;
633 sp<SurfaceControl> layerG;
634 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
635 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
636 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
637 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
638
639 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
640 {
641 SCOPED_TRACE("layerR");
642 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
643 }
644
645 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
646 {
647 SCOPED_TRACE("layerG");
648 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
649 }
650}
651
652TEST_F(LayerTransactionTest, SetZNegative) {
653 sp<SurfaceControl> layerR;
654 sp<SurfaceControl> layerG;
655 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
656 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
657 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
658 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
659
660 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
661 {
662 SCOPED_TRACE("layerR");
663 sp<ScreenCapture> screenshot;
664 ScreenCapture::captureScreen(&screenshot, -2, -1);
665 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
666 }
667
668 Transaction().setLayer(layerR, -3).apply();
669 {
670 SCOPED_TRACE("layerG");
671 sp<ScreenCapture> screenshot;
672 ScreenCapture::captureScreen(&screenshot, -3, -1);
673 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
674 }
675}
676
Chia-I Wu49313302017-10-31 10:14:40 -0700677TEST_F(LayerTransactionTest, SetRelativeZBasic) {
678 sp<SurfaceControl> layerR;
679 sp<SurfaceControl> layerG;
680 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
681 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
682 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
683 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
684
685 Transaction()
686 .setPosition(layerG, 16, 16)
687 .setRelativeLayer(layerG, layerR->getHandle(), 1)
688 .apply();
689 {
690 SCOPED_TRACE("layerG above");
691 auto shot = screenshot();
692 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
693 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
694 }
695
696 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
697 {
698 SCOPED_TRACE("layerG below");
699 auto shot = screenshot();
700 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
701 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
702 }
703}
704
705TEST_F(LayerTransactionTest, SetRelativeZGroup) {
706 sp<SurfaceControl> layerR;
707 sp<SurfaceControl> layerG;
708 sp<SurfaceControl> layerB;
709 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
710 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
711 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
712 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
713 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
714 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
715
716 // layerR = 0, layerG = layerR + 3, layerB = 2
717 Transaction()
718 .setPosition(layerG, 8, 8)
719 .setRelativeLayer(layerG, layerR->getHandle(), 3)
720 .setPosition(layerB, 16, 16)
721 .setLayer(layerB, mLayerZBase + 2)
722 .apply();
723 {
724 SCOPED_TRACE("(layerR < layerG) < layerB");
725 auto shot = screenshot();
726 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
727 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
728 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
729 }
730
731 // layerR = 4, layerG = layerR + 3, layerB = 2
732 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
733 {
734 SCOPED_TRACE("layerB < (layerR < layerG)");
735 auto shot = screenshot();
736 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
737 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
738 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
739 }
740
741 // layerR = 4, layerG = layerR - 3, layerB = 2
742 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
743 {
744 SCOPED_TRACE("layerB < (layerG < layerR)");
745 auto shot = screenshot();
746 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
747 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
748 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
749 }
750
751 // restore to absolute z
752 // layerR = 4, layerG = 0, layerB = 2
753 Transaction().setLayer(layerG, mLayerZBase).apply();
754 {
755 SCOPED_TRACE("layerG < layerB < layerR");
756 auto shot = screenshot();
757 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
758 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
759 }
760
761 // layerR should not affect layerG anymore
762 // layerR = 1, layerG = 0, layerB = 2
763 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
764 {
765 SCOPED_TRACE("layerG < layerR < layerB");
766 auto shot = screenshot();
767 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
768 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
769 }
770}
771
772TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
773 sp<SurfaceControl> layerR;
774 sp<SurfaceControl> layerG;
775
776 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
777 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
778 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
779 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
780
781 Transaction()
782 .setPosition(layerG, 16, 16)
783 .setRelativeLayer(layerG, layerR->getHandle(), 1)
784 .apply();
785
786 mClient->destroySurface(layerG->getHandle());
787 // layerG should have been removed
788 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
789}
790
Chia-I Wu57b27502017-10-31 10:14:40 -0700791TEST_F(LayerTransactionTest, SetFlagsHidden) {
792 sp<SurfaceControl> layer;
793 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
794 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
795
796 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
797 {
798 SCOPED_TRACE("layer hidden");
799 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
800 }
801
802 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
803 {
804 SCOPED_TRACE("layer shown");
805 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
806 }
807}
808
809TEST_F(LayerTransactionTest, SetFlagsOpaque) {
810 const Color translucentRed = {100, 0, 0, 100};
811 sp<SurfaceControl> layerR;
812 sp<SurfaceControl> layerG;
813 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
814 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
815 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
816 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
817
818 Transaction()
819 .setLayer(layerR, mLayerZBase + 1)
820 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
821 .apply();
822 {
823 SCOPED_TRACE("layerR opaque");
824 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
825 }
826
827 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
828 {
829 SCOPED_TRACE("layerR translucent");
830 const uint8_t g = uint8_t(255 - translucentRed.a);
831 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
832 }
833}
834
835TEST_F(LayerTransactionTest, SetFlagsSecure) {
836 sp<SurfaceControl> layer;
837 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
838 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
839
840 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
841 sp<IGraphicBufferProducer> producer;
842 sp<IGraphicBufferConsumer> consumer;
843 BufferQueue::createBufferQueue(&producer, &consumer);
844 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
845
846 Transaction()
847 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
848 .apply(true);
849 ASSERT_EQ(PERMISSION_DENIED,
850 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
851 false));
852
853 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
854 ASSERT_EQ(NO_ERROR,
855 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
856 false));
857}
858
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700859TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
860 const Rect top(0, 0, 32, 16);
861 const Rect bottom(0, 16, 32, 32);
862 sp<SurfaceControl> layer;
863 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
864
865 ANativeWindow_Buffer buffer;
866 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
867 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
868 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
869 // setTransparentRegionHint always applies to the following buffer
870 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
871 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
872 {
873 SCOPED_TRACE("top transparent");
874 auto shot = screenshot();
875 shot->expectColor(top, Color::BLACK);
876 shot->expectColor(bottom, Color::RED);
877 }
878
879 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
880 {
881 SCOPED_TRACE("transparent region hint pending");
882 auto shot = screenshot();
883 shot->expectColor(top, Color::BLACK);
884 shot->expectColor(bottom, Color::RED);
885 }
886
887 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
888 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
889 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
890 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
891 {
892 SCOPED_TRACE("bottom transparent");
893 auto shot = screenshot();
894 shot->expectColor(top, Color::RED);
895 shot->expectColor(bottom, Color::BLACK);
896 }
897}
898
899TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
900 sp<SurfaceControl> layerTransparent;
901 sp<SurfaceControl> layerR;
902 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
903 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
904
905 // check that transparent region hint is bound by the layer size
906 Transaction()
907 .setTransparentRegionHint(layerTransparent,
908 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
909 .setPosition(layerR, 16, 16)
910 .setLayer(layerR, mLayerZBase + 1)
911 .apply();
912 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
913 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
914 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
915}
916
Chia-I Wua8a515e2017-11-01 15:16:35 -0700917TEST_F(LayerTransactionTest, SetAlphaBasic) {
918 sp<SurfaceControl> layer1;
919 sp<SurfaceControl> layer2;
920 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
921 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
922 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
923 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
924
925 Transaction()
926 .setAlpha(layer1, 0.25f)
927 .setAlpha(layer2, 0.75f)
928 .setPosition(layer2, 16, 0)
929 .setLayer(layer2, mLayerZBase + 1)
930 .apply();
931 {
932 auto shot = screenshot();
933 uint8_t r = 16; // 64 * 0.25f
934 uint8_t g = 48; // 64 * 0.75f
935 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
936 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
937
938 r /= 4; // r * (1.0f - 0.75f)
939 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
940 }
941}
942
943TEST_F(LayerTransactionTest, SetAlphaClamped) {
944 const Color color = {64, 0, 0, 255};
945 sp<SurfaceControl> layer;
946 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
947 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
948
949 Transaction().setAlpha(layer, 2.0f).apply();
950 {
951 SCOPED_TRACE("clamped to 1.0f");
952 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
953 }
954
955 Transaction().setAlpha(layer, -1.0f).apply();
956 {
957 SCOPED_TRACE("clamped to 0.0f");
958 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
959 }
960}
961
Chia-I Wue4ef6102017-11-01 15:16:35 -0700962TEST_F(LayerTransactionTest, SetColorBasic) {
963 sp<SurfaceControl> bufferLayer;
964 sp<SurfaceControl> colorLayer;
965 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
966 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
967 ASSERT_NO_FATAL_FAILURE(
968 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
969
970 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
971 {
972 SCOPED_TRACE("default color");
973 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
974 }
975
976 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
977 const Color expected = {15, 51, 85, 255};
978 // this is handwavy, but the precison loss scaled by 255 (8-bit per
979 // channel) should be less than one
980 const uint8_t tolerance = 1;
981 Transaction().setColor(colorLayer, color).apply();
982 {
983 SCOPED_TRACE("new color");
984 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
985 }
986}
987
988TEST_F(LayerTransactionTest, SetColorClamped) {
989 sp<SurfaceControl> colorLayer;
990 ASSERT_NO_FATAL_FAILURE(
991 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
992
993 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
994 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
995}
996
997TEST_F(LayerTransactionTest, SetColorWithAlpha) {
998 sp<SurfaceControl> bufferLayer;
999 sp<SurfaceControl> colorLayer;
1000 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1001 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1002 ASSERT_NO_FATAL_FAILURE(
1003 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1004
1005 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1006 const float alpha = 0.25f;
1007 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1008 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1009 // channel) should be less than one
1010 const uint8_t tolerance = 1;
1011 Transaction()
1012 .setColor(colorLayer, color)
1013 .setAlpha(colorLayer, alpha)
1014 .setLayer(colorLayer, mLayerZBase + 1)
1015 .apply();
1016 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1017 tolerance);
1018}
1019
1020TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1021 sp<SurfaceControl> bufferLayer;
1022 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1023 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1024
1025 // color is ignored
1026 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1027 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1028}
1029
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001030TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1031 sp<SurfaceControl> layer;
1032 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1033 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1034
1035 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1036 {
1037 SCOPED_TRACE("non-existing layer stack");
1038 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1039 }
1040
1041 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1042 {
1043 SCOPED_TRACE("original layer stack");
1044 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1045 }
1046}
1047
Chia-I Wu93853fe2017-11-02 08:30:27 -07001048TEST_F(LayerTransactionTest, SetMatrixBasic) {
1049 sp<SurfaceControl> layer;
1050 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1051 ASSERT_NO_FATAL_FAILURE(
1052 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1053
1054 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1055 {
1056 SCOPED_TRACE("IDENTITY");
1057 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1058 Color::WHITE);
1059 }
1060
1061 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1062 {
1063 SCOPED_TRACE("FLIP_H");
1064 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1065 Color::BLUE);
1066 }
1067
1068 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1069 {
1070 SCOPED_TRACE("FLIP_V");
1071 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1072 Color::GREEN);
1073 }
1074
1075 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1076 {
1077 SCOPED_TRACE("ROT_90");
1078 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1079 Color::GREEN);
1080 }
1081
1082 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1083 {
1084 SCOPED_TRACE("SCALE");
1085 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1086 Color::WHITE, true /* filtered */);
1087 }
1088}
1089
1090TEST_F(LayerTransactionTest, SetMatrixRot45) {
1091 sp<SurfaceControl> layer;
1092 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1093 ASSERT_NO_FATAL_FAILURE(
1094 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1095
1096 const float rot = M_SQRT1_2; // 45 degrees
1097 const float trans = M_SQRT2 * 16.0f;
1098 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1099
1100 auto shot = screenshot();
1101 // check a 8x8 region inside each color
1102 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1103 const int32_t halfL = 4;
1104 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1105 };
1106 const int32_t unit = int32_t(trans / 2);
1107 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1108 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1109 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1110 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1111}
1112
1113TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1114 sp<SurfaceControl> layer;
1115 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1116 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1117
1118 // setMatrix is applied after any pending resize, unlike setPosition
1119 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1120 {
1121 SCOPED_TRACE("resize pending");
1122 auto shot = screenshot();
1123 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1124 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1125 }
1126
1127 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1128 {
1129 SCOPED_TRACE("resize applied");
1130 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1131 }
1132}
1133
1134TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1135 sp<SurfaceControl> layer;
1136 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1137 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1138
1139 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1140 Transaction()
1141 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1142 .setSize(layer, 64, 64)
1143 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1144 .apply();
1145 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1146}
1147
Chia-I Wua56b2042017-11-01 15:16:35 -07001148TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1149 sp<SurfaceControl> layer;
1150 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1151 ASSERT_NO_FATAL_FAILURE(
1152 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1153
1154 // XXX SCALE_CROP is not respected; calling setSize and
1155 // setOverrideScalingMode in separate transactions does not work
1156 // (b/69315456)
1157 Transaction()
1158 .setSize(layer, 64, 16)
1159 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1160 .apply();
1161 {
1162 SCOPED_TRACE("SCALE_TO_WINDOW");
1163 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1164 Color::WHITE, true /* filtered */);
1165 }
1166}
1167
Chia-I Wu04dcca82017-11-02 08:30:27 -07001168TEST_F(LayerTransactionTest, SetCropBasic) {
1169 sp<SurfaceControl> layer;
1170 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1171 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1172 const Rect crop(8, 8, 24, 24);
1173
1174 Transaction().setCrop(layer, crop).apply();
1175 auto shot = screenshot();
1176 shot->expectColor(crop, Color::RED);
1177 shot->expectBorder(crop, Color::BLACK);
1178}
1179
1180TEST_F(LayerTransactionTest, SetCropEmpty) {
1181 sp<SurfaceControl> layer;
1182 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1183 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1184
1185 {
1186 SCOPED_TRACE("empty rect");
1187 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1188 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1189 }
1190
1191 {
1192 SCOPED_TRACE("negative rect");
1193 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1194 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1195 }
1196}
1197
1198TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1199 sp<SurfaceControl> layer;
1200 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1201 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1202
1203 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1204 auto shot = screenshot();
1205 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1206 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1207}
1208
1209TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1210 sp<SurfaceControl> layer;
1211 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1212 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1213
1214 const Point position(32, 32);
1215 const Rect crop(8, 8, 24, 24);
1216 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1217 auto shot = screenshot();
1218 shot->expectColor(crop + position, Color::RED);
1219 shot->expectBorder(crop + position, Color::BLACK);
1220}
1221
1222TEST_F(LayerTransactionTest, SetCropWithScale) {
1223 sp<SurfaceControl> layer;
1224 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1225 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1226
1227 // crop is affected by matrix
1228 Transaction()
1229 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1230 .setCrop(layer, Rect(8, 8, 24, 24))
1231 .apply();
1232 auto shot = screenshot();
1233 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1234 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1235}
1236
1237TEST_F(LayerTransactionTest, SetCropWithResize) {
1238 sp<SurfaceControl> layer;
1239 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1240 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1241
1242 // setCrop is applied immediately by default, with or without resize pending
1243 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1244 {
1245 SCOPED_TRACE("resize pending");
1246 auto shot = screenshot();
1247 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1248 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1249 }
1250
1251 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1252 {
1253 SCOPED_TRACE("resize applied");
1254 auto shot = screenshot();
1255 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1256 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1257 }
1258}
1259
1260TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1261 sp<SurfaceControl> layer;
1262 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1263 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1264
1265 // request setCrop to be applied with the next resize
1266 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1267 {
1268 SCOPED_TRACE("waiting for next resize");
1269 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1270 }
1271
1272 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1273 {
1274 SCOPED_TRACE("pending crop modified");
1275 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1276 }
1277
1278 Transaction().setSize(layer, 16, 16).apply();
1279 {
1280 SCOPED_TRACE("resize pending");
1281 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1282 }
1283
1284 // finally resize
1285 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1286 {
1287 SCOPED_TRACE("new crop applied");
1288 auto shot = screenshot();
1289 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1290 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1291 }
1292}
1293
1294TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1295 sp<SurfaceControl> layer;
1296 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1297 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1298
1299 // setCrop is not immediate even with SCALE_TO_WINDOW override
1300 Transaction()
1301 .setCrop(layer, Rect(4, 4, 12, 12))
1302 .setSize(layer, 16, 16)
1303 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1304 .setGeometryAppliesWithResize(layer)
1305 .apply();
1306 {
1307 SCOPED_TRACE("new crop pending");
1308 auto shot = screenshot();
1309 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1310 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1311 }
1312
1313 // XXX crop is never latched without other geometry change (b/69315677)
1314 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1315 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1316 Transaction().setPosition(layer, 0, 0).apply();
1317 {
1318 SCOPED_TRACE("new crop applied");
1319 auto shot = screenshot();
1320 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1321 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1322 }
1323}
1324
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001325class LayerUpdateTest : public ::testing::Test {
1326protected:
1327 virtual void SetUp() {
1328 mComposerClient = new SurfaceComposerClient;
1329 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1330
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001331 sp<IBinder> display(
1332 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001333 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001334 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001335
1336 ssize_t displayWidth = info.w;
1337 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001338
1339 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001340 mBGSurfaceControl =
1341 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1342 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001343 ASSERT_TRUE(mBGSurfaceControl != NULL);
1344 ASSERT_TRUE(mBGSurfaceControl->isValid());
1345 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1346
1347 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001348 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1349 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001350 ASSERT_TRUE(mFGSurfaceControl != NULL);
1351 ASSERT_TRUE(mFGSurfaceControl->isValid());
1352
1353 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1354
1355 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001356 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1357 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001358 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1359 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1360
1361 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1362
Robert Carr4cdc58f2017-08-23 14:22:20 -07001363 asTransaction([&](Transaction& t) {
1364 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001365
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001366 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001367
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001368 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1369 .setPosition(mFGSurfaceControl, 64, 64)
1370 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001371
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001372 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1373 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1374 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001375 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001376 }
1377
1378 virtual void TearDown() {
1379 mComposerClient->dispose();
1380 mBGSurfaceControl = 0;
1381 mFGSurfaceControl = 0;
1382 mSyncSurfaceControl = 0;
1383 mComposerClient = 0;
1384 }
1385
1386 void waitForPostedBuffers() {
1387 // Since the sync surface is in synchronous mode (i.e. double buffered)
1388 // posting three buffers to it should ensure that at least two
1389 // SurfaceFlinger::handlePageFlip calls have been made, which should
1390 // guaranteed that a buffer posted to another Surface has been retired.
1391 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1392 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1393 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1394 }
1395
Robert Carr4cdc58f2017-08-23 14:22:20 -07001396 void asTransaction(const std::function<void(Transaction&)>& exec) {
1397 Transaction t;
1398 exec(t);
1399 t.apply(true);
1400 }
1401
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001402 sp<SurfaceComposerClient> mComposerClient;
1403 sp<SurfaceControl> mBGSurfaceControl;
1404 sp<SurfaceControl> mFGSurfaceControl;
1405
1406 // This surface is used to ensure that the buffers posted to
1407 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1408 sp<SurfaceControl> mSyncSurfaceControl;
1409};
1410
Robert Carr7f619b22017-11-06 12:56:35 -08001411TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1412 sp<ScreenCapture> sc;
1413
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001414 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1415 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001416 fillSurfaceRGBA8(relative, 10, 10, 10);
1417 waitForPostedBuffers();
1418
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001419 Transaction{}
1420 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001421 .setPosition(relative, 64, 64)
1422 .apply();
1423
1424 {
1425 // The relative should be on top of the FG control.
1426 ScreenCapture::captureScreen(&sc);
1427 sc->checkPixel(64, 64, 10, 10, 10);
1428 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001429 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001430
1431 {
1432 // Nothing should change at this point.
1433 ScreenCapture::captureScreen(&sc);
1434 sc->checkPixel(64, 64, 10, 10, 10);
1435 }
1436
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001437 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001438
1439 {
1440 // Ensure that the relative was actually hidden, rather than
1441 // being left in the detached but visible state.
1442 ScreenCapture::captureScreen(&sc);
1443 sc->expectFGColor(64, 64);
1444 }
1445}
1446
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001447TEST_F(LayerUpdateTest, LayerMoveWorks) {
1448 sp<ScreenCapture> sc;
1449 {
1450 SCOPED_TRACE("before move");
1451 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001452 sc->expectBGColor(0, 12);
1453 sc->expectFGColor(75, 75);
1454 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001455 }
1456
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001457 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001458
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001459 {
1460 // This should reflect the new position, but not the new color.
1461 SCOPED_TRACE("after move, before redraw");
1462 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001463 sc->expectBGColor(24, 24);
1464 sc->expectBGColor(75, 75);
1465 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001466 }
1467
1468 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1469 waitForPostedBuffers();
1470 {
1471 // This should reflect the new position and the new color.
1472 SCOPED_TRACE("after redraw");
1473 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001474 sc->expectBGColor(24, 24);
1475 sc->expectBGColor(75, 75);
1476 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001477 }
1478}
1479
1480TEST_F(LayerUpdateTest, LayerResizeWorks) {
1481 sp<ScreenCapture> sc;
1482 {
1483 SCOPED_TRACE("before resize");
1484 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001485 sc->expectBGColor(0, 12);
1486 sc->expectFGColor(75, 75);
1487 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001488 }
1489
Steve Block9d453682011-12-20 16:23:08 +00001490 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001491 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001492 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001493 {
1494 // This should not reflect the new size or color because SurfaceFlinger
1495 // has not yet received a buffer of the correct size.
1496 SCOPED_TRACE("after resize, before redraw");
1497 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001498 sc->expectBGColor(0, 12);
1499 sc->expectFGColor(75, 75);
1500 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001501 }
1502
Steve Block9d453682011-12-20 16:23:08 +00001503 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001504 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1505 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001506 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001507 {
1508 // This should reflect the new size and the new color.
1509 SCOPED_TRACE("after redraw");
1510 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001511 sc->expectBGColor(24, 24);
1512 sc->checkPixel(75, 75, 63, 195, 63);
1513 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001514 }
1515}
1516
Haixia Shid5750962015-07-27 16:50:49 -07001517TEST_F(LayerUpdateTest, LayerCropWorks) {
1518 sp<ScreenCapture> sc;
1519 {
1520 SCOPED_TRACE("before crop");
1521 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001522 sc->expectBGColor(24, 24);
1523 sc->expectFGColor(75, 75);
1524 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001525 }
1526
Robert Carr4cdc58f2017-08-23 14:22:20 -07001527 asTransaction([&](Transaction& t) {
1528 Rect cropRect(16, 16, 32, 32);
1529 t.setCrop(mFGSurfaceControl, cropRect);
1530 });
Haixia Shid5750962015-07-27 16:50:49 -07001531 {
1532 // This should crop the foreground surface.
1533 SCOPED_TRACE("after crop");
1534 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001535 sc->expectBGColor(24, 24);
1536 sc->expectBGColor(75, 75);
1537 sc->expectFGColor(95, 80);
1538 sc->expectFGColor(80, 95);
1539 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001540 }
1541}
1542
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001543TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1544 sp<ScreenCapture> sc;
1545 {
1546 SCOPED_TRACE("before crop");
1547 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001548 sc->expectBGColor(24, 24);
1549 sc->expectFGColor(75, 75);
1550 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001551 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001552 asTransaction([&](Transaction& t) {
1553 Rect cropRect(16, 16, 32, 32);
1554 t.setFinalCrop(mFGSurfaceControl, cropRect);
1555 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001556 {
1557 // This should crop the foreground surface.
1558 SCOPED_TRACE("after crop");
1559 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001560 sc->expectBGColor(24, 24);
1561 sc->expectBGColor(75, 75);
1562 sc->expectBGColor(95, 80);
1563 sc->expectBGColor(80, 95);
1564 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001565 }
1566}
1567
Haixia Shid5750962015-07-27 16:50:49 -07001568TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1569 sp<ScreenCapture> sc;
1570 {
1571 SCOPED_TRACE("before setLayer");
1572 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001573 sc->expectBGColor(24, 24);
1574 sc->expectFGColor(75, 75);
1575 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001576 }
1577
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001578 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001579
Haixia Shid5750962015-07-27 16:50:49 -07001580 {
1581 // This should hide the foreground surface beneath the background.
1582 SCOPED_TRACE("after setLayer");
1583 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001584 sc->expectBGColor(24, 24);
1585 sc->expectBGColor(75, 75);
1586 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001587 }
1588}
1589
1590TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1591 sp<ScreenCapture> sc;
1592 {
1593 SCOPED_TRACE("before hide");
1594 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001595 sc->expectBGColor(24, 24);
1596 sc->expectFGColor(75, 75);
1597 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001598 }
1599
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001600 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001601
Haixia Shid5750962015-07-27 16:50:49 -07001602 {
1603 // This should hide the foreground surface.
1604 SCOPED_TRACE("after hide, before show");
1605 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001606 sc->expectBGColor(24, 24);
1607 sc->expectBGColor(75, 75);
1608 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001609 }
1610
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001611 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001612
Haixia Shid5750962015-07-27 16:50:49 -07001613 {
1614 // This should show the foreground surface.
1615 SCOPED_TRACE("after show");
1616 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001617 sc->expectBGColor(24, 24);
1618 sc->expectFGColor(75, 75);
1619 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001620 }
1621}
1622
1623TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1624 sp<ScreenCapture> sc;
1625 {
1626 SCOPED_TRACE("before setAlpha");
1627 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001628 sc->expectBGColor(24, 24);
1629 sc->expectFGColor(75, 75);
1630 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001631 }
1632
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001633 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001634
Haixia Shid5750962015-07-27 16:50:49 -07001635 {
1636 // This should set foreground to be 75% opaque.
1637 SCOPED_TRACE("after setAlpha");
1638 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001639 sc->expectBGColor(24, 24);
1640 sc->checkPixel(75, 75, 162, 63, 96);
1641 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001642 }
1643}
1644
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001645TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1646 sp<ScreenCapture> sc;
1647 {
1648 SCOPED_TRACE("before setLayerStack");
1649 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001650 sc->expectBGColor(24, 24);
1651 sc->expectFGColor(75, 75);
1652 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001653 }
1654
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001655 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001656 {
1657 // This should hide the foreground surface since it goes to a different
1658 // layer stack.
1659 SCOPED_TRACE("after setLayerStack");
1660 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001661 sc->expectBGColor(24, 24);
1662 sc->expectBGColor(75, 75);
1663 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001664 }
1665}
1666
1667TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1668 sp<ScreenCapture> sc;
1669 {
1670 SCOPED_TRACE("before setFlags");
1671 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001672 sc->expectBGColor(24, 24);
1673 sc->expectFGColor(75, 75);
1674 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001675 }
1676
Robert Carr4cdc58f2017-08-23 14:22:20 -07001677 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001678 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001679 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001680 {
1681 // This should hide the foreground surface
1682 SCOPED_TRACE("after setFlags");
1683 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001684 sc->expectBGColor(24, 24);
1685 sc->expectBGColor(75, 75);
1686 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001687 }
1688}
1689
1690TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1691 sp<ScreenCapture> sc;
1692 {
1693 SCOPED_TRACE("before setMatrix");
1694 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001695 sc->expectBGColor(24, 24);
1696 sc->expectFGColor(91, 96);
1697 sc->expectFGColor(96, 101);
1698 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001699 }
1700
Robert Carr4cdc58f2017-08-23 14:22:20 -07001701 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001702 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001703 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001704 {
1705 SCOPED_TRACE("after setMatrix");
1706 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001707 sc->expectBGColor(24, 24);
1708 sc->expectFGColor(91, 96);
1709 sc->expectBGColor(96, 91);
1710 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001711 }
1712}
1713
Robert Carr8d5227b2017-03-16 15:41:03 -07001714class GeometryLatchingTest : public LayerUpdateTest {
1715protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001716 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001717 SCOPED_TRACE(trace);
1718 ScreenCapture::captureScreen(&sc);
1719 // We find the leading edge of the FG surface.
1720 sc->expectFGColor(127, 127);
1721 sc->expectBGColor(128, 128);
1722 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001723
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001724 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001725
1726 void unlockFGBuffer() {
1727 sp<Surface> s = mFGSurfaceControl->getSurface();
1728 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1729 waitForPostedBuffers();
1730 }
1731
Robert Carr8d5227b2017-03-16 15:41:03 -07001732 void completeFGResize() {
1733 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1734 waitForPostedBuffers();
1735 }
1736 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001737 asTransaction([&](Transaction& t) {
1738 t.setSize(mFGSurfaceControl, 64, 64);
1739 t.setPosition(mFGSurfaceControl, 64, 64);
1740 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1741 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1742 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001743
1744 EXPECT_INITIAL_STATE("After restoring initial state");
1745 }
1746 sp<ScreenCapture> sc;
1747};
1748
1749TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1750 EXPECT_INITIAL_STATE("before anything");
1751
1752 // By default position can be updated even while
1753 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001754 asTransaction([&](Transaction& t) {
1755 t.setSize(mFGSurfaceControl, 32, 32);
1756 t.setPosition(mFGSurfaceControl, 100, 100);
1757 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001758
1759 {
1760 SCOPED_TRACE("After moving surface");
1761 ScreenCapture::captureScreen(&sc);
1762 // If we moved, the FG Surface should cover up what was previously BG
1763 // however if we didn't move the FG wouldn't be large enough now.
1764 sc->expectFGColor(163, 163);
1765 }
1766
1767 restoreInitialState();
1768
1769 // Now we repeat with setGeometryAppliesWithResize
1770 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001771 asTransaction([&](Transaction& t) {
1772 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1773 t.setSize(mFGSurfaceControl, 32, 32);
1774 t.setPosition(mFGSurfaceControl, 100, 100);
1775 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001776
1777 {
1778 SCOPED_TRACE("While resize is pending");
1779 ScreenCapture::captureScreen(&sc);
1780 // This time we shouldn't have moved, so the BG color
1781 // should still be visible.
1782 sc->expectBGColor(128, 128);
1783 }
1784
1785 completeFGResize();
1786
1787 {
1788 SCOPED_TRACE("After the resize");
1789 ScreenCapture::captureScreen(&sc);
1790 // But after the resize completes, we should move
1791 // and the FG should be visible here.
1792 sc->expectFGColor(128, 128);
1793 }
1794}
1795
1796class CropLatchingTest : public GeometryLatchingTest {
1797protected:
1798 void EXPECT_CROPPED_STATE(const char* trace) {
1799 SCOPED_TRACE(trace);
1800 ScreenCapture::captureScreen(&sc);
1801 // The edge should be moved back one pixel by our crop.
1802 sc->expectFGColor(126, 126);
1803 sc->expectBGColor(127, 127);
1804 sc->expectBGColor(128, 128);
1805 }
chaviw59f5c562017-06-28 16:39:06 -07001806
1807 void EXPECT_RESIZE_STATE(const char* trace) {
1808 SCOPED_TRACE(trace);
1809 ScreenCapture::captureScreen(&sc);
1810 // The FG is now resized too 128,128 at 64,64
1811 sc->expectFGColor(64, 64);
1812 sc->expectFGColor(191, 191);
1813 sc->expectBGColor(192, 192);
1814 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001815};
1816
1817TEST_F(CropLatchingTest, CropLatching) {
1818 EXPECT_INITIAL_STATE("before anything");
1819 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001820 asTransaction([&](Transaction& t) {
1821 t.setSize(mFGSurfaceControl, 128, 128);
1822 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1823 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001824
1825 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1826
1827 restoreInitialState();
1828
Robert Carr4cdc58f2017-08-23 14:22:20 -07001829 asTransaction([&](Transaction& t) {
1830 t.setSize(mFGSurfaceControl, 128, 128);
1831 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1832 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1833 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001834
1835 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1836
1837 completeFGResize();
1838
1839 EXPECT_CROPPED_STATE("after the resize finishes");
1840}
1841
1842TEST_F(CropLatchingTest, FinalCropLatching) {
1843 EXPECT_INITIAL_STATE("before anything");
1844 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001845 asTransaction([&](Transaction& t) {
1846 t.setSize(mFGSurfaceControl, 128, 128);
1847 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1848 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001849
1850 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1851
1852 restoreInitialState();
1853
Robert Carr4cdc58f2017-08-23 14:22:20 -07001854 asTransaction([&](Transaction& t) {
1855 t.setSize(mFGSurfaceControl, 128, 128);
1856 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1857 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1858 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001859
1860 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1861
1862 completeFGResize();
1863
1864 EXPECT_CROPPED_STATE("after the resize finishes");
1865}
1866
Robert Carr7bf247e2017-05-18 14:02:49 -07001867// In this test we ensure that setGeometryAppliesWithResize actually demands
1868// a buffer of the new size, and not just any size.
1869TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1870 EXPECT_INITIAL_STATE("before anything");
1871 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001872 asTransaction([&](Transaction& t) {
1873 t.setSize(mFGSurfaceControl, 128, 128);
1874 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1875 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001876
1877 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1878
1879 restoreInitialState();
1880
1881 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1882 // initiating the resize.
1883 lockAndFillFGBuffer();
1884
Robert Carr4cdc58f2017-08-23 14:22:20 -07001885 asTransaction([&](Transaction& t) {
1886 t.setSize(mFGSurfaceControl, 128, 128);
1887 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1888 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1889 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001890
1891 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1892
1893 // We now submit our old buffer, at the old size, and ensure it doesn't
1894 // trigger geometry latching.
1895 unlockFGBuffer();
1896
1897 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1898
1899 completeFGResize();
1900
1901 EXPECT_CROPPED_STATE("after the resize finishes");
1902}
1903
1904TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1905 EXPECT_INITIAL_STATE("before anything");
1906 // In this scenario, we attempt to set the final crop a second time while the resize
1907 // is still pending, and ensure we are successful. Success meaning the second crop
1908 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001909 asTransaction([&](Transaction& t) {
1910 t.setSize(mFGSurfaceControl, 128, 128);
1911 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1912 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1913 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001914
chaviw59f5c562017-06-28 16:39:06 -07001915 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1916
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001917 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001918
chaviw59f5c562017-06-28 16:39:06 -07001919 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001920
1921 completeFGResize();
1922
chaviw59f5c562017-06-28 16:39:06 -07001923 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001924}
1925
Pablo Ceballos05289c22016-04-14 15:49:55 -07001926TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1927 sp<ScreenCapture> sc;
1928 {
1929 SCOPED_TRACE("before anything");
1930 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001931 sc->expectBGColor(32, 32);
1932 sc->expectFGColor(96, 96);
1933 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001934 }
1935
1936 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001937 asTransaction([&](Transaction& t) {
1938 t.setAlpha(mFGSurfaceControl, 0.75);
1939 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001940 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001941 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001942
Robert Carr4cdc58f2017-08-23 14:22:20 -07001943 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001944 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001945 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001946 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001947 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001948
1949 {
1950 SCOPED_TRACE("before any trigger");
1951 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001952 sc->expectBGColor(32, 32);
1953 sc->expectFGColor(96, 96);
1954 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001955 }
1956
1957 // should trigger the first deferred transaction, but not the second one
1958 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1959 {
1960 SCOPED_TRACE("after first trigger");
1961 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001962 sc->expectBGColor(32, 32);
1963 sc->checkPixel(96, 96, 162, 63, 96);
1964 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001965 }
1966
1967 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001968 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001969
1970 // trigger the second deferred transaction
1971 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1972 {
1973 SCOPED_TRACE("after second trigger");
1974 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001975 sc->expectBGColor(32, 32);
1976 sc->expectBGColor(96, 96);
1977 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001978 }
1979}
1980
Robert Carrdb66e622017-04-10 16:55:57 -07001981TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1982 sp<ScreenCapture> sc;
1983 {
1984 SCOPED_TRACE("before adding relative surface");
1985 ScreenCapture::captureScreen(&sc);
1986 sc->expectBGColor(24, 24);
1987 sc->expectFGColor(75, 75);
1988 sc->expectBGColor(145, 145);
1989 }
1990
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001991 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1992 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001993 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1994 waitForPostedBuffers();
1995
1996 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001997 asTransaction([&](Transaction& t) {
1998 t.setPosition(relativeSurfaceControl, 64, 64);
1999 t.show(relativeSurfaceControl);
2000 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
2001 });
Robert Carrdb66e622017-04-10 16:55:57 -07002002
2003 {
2004 SCOPED_TRACE("after adding relative surface");
2005 ScreenCapture::captureScreen(&sc);
2006 // our relative surface should be visible now.
2007 sc->checkPixel(75, 75, 255, 177, 177);
2008 }
2009
2010 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002011 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07002012
2013 {
2014 SCOPED_TRACE("after set layer");
2015 ScreenCapture::captureScreen(&sc);
2016 // now the FG surface should be visible again.
2017 sc->expectFGColor(75, 75);
2018 }
2019}
2020
Robert Carre392b552017-09-19 12:16:05 -07002021TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
2022 sp<ScreenCapture> sc;
2023
2024 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002025 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
2026 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2027 sp<SurfaceControl> childBuffer =
2028 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
2029 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002030 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
2031
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002032 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002033
2034 {
2035 ScreenCapture::captureScreen(&sc);
2036 sc->expectChildColor(73, 73);
2037 sc->expectFGColor(74, 74);
2038 }
2039
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002040 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002041
2042 {
2043 ScreenCapture::captureScreen(&sc);
2044 sc->expectChildColor(73, 73);
2045 sc->expectChildColor(74, 74);
2046 }
2047}
2048
Robert Carr2c5f6d22017-09-26 12:30:35 -07002049TEST_F(LayerUpdateTest, MergingTransactions) {
2050 sp<ScreenCapture> sc;
2051 {
2052 SCOPED_TRACE("before move");
2053 ScreenCapture::captureScreen(&sc);
2054 sc->expectBGColor(0, 12);
2055 sc->expectFGColor(75, 75);
2056 sc->expectBGColor(145, 145);
2057 }
2058
2059 Transaction t1, t2;
2060 t1.setPosition(mFGSurfaceControl, 128, 128);
2061 t2.setPosition(mFGSurfaceControl, 0, 0);
2062 // We expect that the position update from t2 now
2063 // overwrites the position update from t1.
2064 t1.merge(std::move(t2));
2065 t1.apply();
2066
2067 {
2068 ScreenCapture::captureScreen(&sc);
2069 sc->expectFGColor(1, 1);
2070 }
2071}
2072
Robert Carr1f0a16a2016-10-24 16:27:39 -07002073class ChildLayerTest : public LayerUpdateTest {
2074protected:
2075 void SetUp() override {
2076 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002077 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2078 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002079 fillSurfaceRGBA8(mChild, 200, 200, 200);
2080
2081 {
2082 SCOPED_TRACE("before anything");
2083 ScreenCapture::captureScreen(&mCapture);
2084 mCapture->expectChildColor(64, 64);
2085 }
2086 }
2087 void TearDown() override {
2088 LayerUpdateTest::TearDown();
2089 mChild = 0;
2090 }
2091
2092 sp<SurfaceControl> mChild;
2093 sp<ScreenCapture> mCapture;
2094};
2095
2096TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002097 asTransaction([&](Transaction& t) {
2098 t.show(mChild);
2099 t.setPosition(mChild, 10, 10);
2100 t.setPosition(mFGSurfaceControl, 64, 64);
2101 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002102
2103 {
2104 ScreenCapture::captureScreen(&mCapture);
2105 // Top left of foreground must now be visible
2106 mCapture->expectFGColor(64, 64);
2107 // But 10 pixels in we should see the child surface
2108 mCapture->expectChildColor(74, 74);
2109 // And 10 more pixels we should be back to the foreground surface
2110 mCapture->expectFGColor(84, 84);
2111 }
2112
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002113 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002114
2115 {
2116 ScreenCapture::captureScreen(&mCapture);
2117 // Top left of foreground should now be at 0, 0
2118 mCapture->expectFGColor(0, 0);
2119 // But 10 pixels in we should see the child surface
2120 mCapture->expectChildColor(10, 10);
2121 // And 10 more pixels we should be back to the foreground surface
2122 mCapture->expectFGColor(20, 20);
2123 }
2124}
2125
Robert Carr41b08b52017-06-01 16:11:34 -07002126TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002127 asTransaction([&](Transaction& t) {
2128 t.show(mChild);
2129 t.setPosition(mChild, 0, 0);
2130 t.setPosition(mFGSurfaceControl, 0, 0);
2131 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2132 });
Robert Carr41b08b52017-06-01 16:11:34 -07002133
2134 {
2135 ScreenCapture::captureScreen(&mCapture);
2136 mCapture->expectChildColor(0, 0);
2137 mCapture->expectChildColor(4, 4);
2138 mCapture->expectBGColor(5, 5);
2139 }
2140}
2141
2142TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002143 asTransaction([&](Transaction& t) {
2144 t.show(mChild);
2145 t.setPosition(mChild, 0, 0);
2146 t.setPosition(mFGSurfaceControl, 0, 0);
2147 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2148 });
Robert Carr41b08b52017-06-01 16:11:34 -07002149
2150 {
2151 ScreenCapture::captureScreen(&mCapture);
2152 mCapture->expectChildColor(0, 0);
2153 mCapture->expectChildColor(4, 4);
2154 mCapture->expectBGColor(5, 5);
2155 }
2156}
2157
Robert Carr1f0a16a2016-10-24 16:27:39 -07002158TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002159 asTransaction([&](Transaction& t) {
2160 t.show(mChild);
2161 t.setPosition(mFGSurfaceControl, 0, 0);
2162 t.setPosition(mChild, 63, 63);
2163 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002164
2165 {
2166 ScreenCapture::captureScreen(&mCapture);
2167 mCapture->expectFGColor(0, 0);
2168 // Last pixel in foreground should now be the child.
2169 mCapture->expectChildColor(63, 63);
2170 // But the child should be constrained and the next pixel
2171 // must be the background
2172 mCapture->expectBGColor(64, 64);
2173 }
2174}
2175
2176TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002177 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002178
2179 // Find the boundary between the parent and child
2180 {
2181 ScreenCapture::captureScreen(&mCapture);
2182 mCapture->expectChildColor(9, 9);
2183 mCapture->expectFGColor(10, 10);
2184 }
2185
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002186 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002187
2188 // The boundary should be twice as far from the origin now.
2189 // The pixels from the last test should all be child now
2190 {
2191 ScreenCapture::captureScreen(&mCapture);
2192 mCapture->expectChildColor(9, 9);
2193 mCapture->expectChildColor(10, 10);
2194 mCapture->expectChildColor(19, 19);
2195 mCapture->expectFGColor(20, 20);
2196 }
2197}
Robert Carr9524cb32017-02-13 11:32:32 -08002198
Robert Carr6452f122017-03-21 10:41:29 -07002199TEST_F(ChildLayerTest, ChildLayerAlpha) {
2200 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2201 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2202 fillSurfaceRGBA8(mChild, 0, 254, 0);
2203 waitForPostedBuffers();
2204
Robert Carr4cdc58f2017-08-23 14:22:20 -07002205 asTransaction([&](Transaction& t) {
2206 t.show(mChild);
2207 t.setPosition(mChild, 0, 0);
2208 t.setPosition(mFGSurfaceControl, 0, 0);
2209 });
Robert Carr6452f122017-03-21 10:41:29 -07002210
2211 {
2212 ScreenCapture::captureScreen(&mCapture);
2213 // Unblended child color
2214 mCapture->checkPixel(0, 0, 0, 254, 0);
2215 }
2216
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002217 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002218
2219 {
2220 ScreenCapture::captureScreen(&mCapture);
2221 // Child and BG blended.
2222 mCapture->checkPixel(0, 0, 127, 127, 0);
2223 }
2224
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002225 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002226
2227 {
2228 ScreenCapture::captureScreen(&mCapture);
2229 // Child and BG blended.
2230 mCapture->checkPixel(0, 0, 95, 64, 95);
2231 }
2232}
2233
Robert Carr9524cb32017-02-13 11:32:32 -08002234TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002235 asTransaction([&](Transaction& t) {
2236 t.show(mChild);
2237 t.setPosition(mChild, 10, 10);
2238 t.setPosition(mFGSurfaceControl, 64, 64);
2239 });
Robert Carr9524cb32017-02-13 11:32:32 -08002240
2241 {
2242 ScreenCapture::captureScreen(&mCapture);
2243 // Top left of foreground must now be visible
2244 mCapture->expectFGColor(64, 64);
2245 // But 10 pixels in we should see the child surface
2246 mCapture->expectChildColor(74, 74);
2247 // And 10 more pixels we should be back to the foreground surface
2248 mCapture->expectFGColor(84, 84);
2249 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002250
2251 asTransaction([&](Transaction& t) {
2252 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2253 });
2254
Robert Carr9524cb32017-02-13 11:32:32 -08002255 {
2256 ScreenCapture::captureScreen(&mCapture);
2257 mCapture->expectFGColor(64, 64);
2258 // In reparenting we should have exposed the entire foreground surface.
2259 mCapture->expectFGColor(74, 74);
2260 // And the child layer should now begin at 10, 10 (since the BG
2261 // layer is at (0, 0)).
2262 mCapture->expectBGColor(9, 9);
2263 mCapture->expectChildColor(10, 10);
2264 }
2265}
2266
chaviw161410b02017-07-27 10:46:08 -07002267TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002268 asTransaction([&](Transaction& t) {
2269 t.show(mChild);
2270 t.setPosition(mChild, 10, 10);
2271 t.setPosition(mFGSurfaceControl, 64, 64);
2272 });
Robert Carr9524cb32017-02-13 11:32:32 -08002273
2274 {
2275 ScreenCapture::captureScreen(&mCapture);
2276 // Top left of foreground must now be visible
2277 mCapture->expectFGColor(64, 64);
2278 // But 10 pixels in we should see the child surface
2279 mCapture->expectChildColor(74, 74);
2280 // And 10 more pixels we should be back to the foreground surface
2281 mCapture->expectFGColor(84, 84);
2282 }
2283
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002284 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002285
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002286 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002287
chaviw161410b02017-07-27 10:46:08 -07002288 // Since the child has the same client as the parent, it will not get
2289 // detached and will be hidden.
2290 {
2291 ScreenCapture::captureScreen(&mCapture);
2292 mCapture->expectFGColor(64, 64);
2293 mCapture->expectFGColor(74, 74);
2294 mCapture->expectFGColor(84, 84);
2295 }
2296}
2297
2298TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2299 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002300 sp<SurfaceControl> mChildNewClient =
2301 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2302 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002303
2304 ASSERT_TRUE(mChildNewClient != NULL);
2305 ASSERT_TRUE(mChildNewClient->isValid());
2306
2307 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2308
Robert Carr4cdc58f2017-08-23 14:22:20 -07002309 asTransaction([&](Transaction& t) {
2310 t.hide(mChild);
2311 t.show(mChildNewClient);
2312 t.setPosition(mChildNewClient, 10, 10);
2313 t.setPosition(mFGSurfaceControl, 64, 64);
2314 });
chaviw161410b02017-07-27 10:46:08 -07002315
2316 {
2317 ScreenCapture::captureScreen(&mCapture);
2318 // Top left of foreground must now be visible
2319 mCapture->expectFGColor(64, 64);
2320 // But 10 pixels in we should see the child surface
2321 mCapture->expectChildColor(74, 74);
2322 // And 10 more pixels we should be back to the foreground surface
2323 mCapture->expectFGColor(84, 84);
2324 }
2325
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002326 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002327
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002328 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002329
Robert Carr9524cb32017-02-13 11:32:32 -08002330 // Nothing should have changed.
2331 {
2332 ScreenCapture::captureScreen(&mCapture);
2333 mCapture->expectFGColor(64, 64);
2334 mCapture->expectChildColor(74, 74);
2335 mCapture->expectFGColor(84, 84);
2336 }
2337}
2338
Robert Carr9b429f42017-04-17 14:56:57 -07002339TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002340 asTransaction([&](Transaction& t) {
2341 t.show(mChild);
2342 t.setPosition(mChild, 0, 0);
2343 t.setPosition(mFGSurfaceControl, 0, 0);
2344 });
Robert Carr9b429f42017-04-17 14:56:57 -07002345
2346 {
2347 ScreenCapture::captureScreen(&mCapture);
2348 // We've positioned the child in the top left.
2349 mCapture->expectChildColor(0, 0);
2350 // But it's only 10x10.
2351 mCapture->expectFGColor(10, 10);
2352 }
2353
Robert Carr4cdc58f2017-08-23 14:22:20 -07002354 asTransaction([&](Transaction& t) {
2355 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2356 // We cause scaling by 2.
2357 t.setSize(mFGSurfaceControl, 128, 128);
2358 });
Robert Carr9b429f42017-04-17 14:56:57 -07002359
2360 {
2361 ScreenCapture::captureScreen(&mCapture);
2362 // We've positioned the child in the top left.
2363 mCapture->expectChildColor(0, 0);
2364 mCapture->expectChildColor(10, 10);
2365 mCapture->expectChildColor(19, 19);
2366 // And now it should be scaled all the way to 20x20
2367 mCapture->expectFGColor(20, 20);
2368 }
2369}
2370
Robert Carr1725eee2017-04-26 18:32:15 -07002371// Regression test for b/37673612
2372TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002373 asTransaction([&](Transaction& t) {
2374 t.show(mChild);
2375 t.setPosition(mChild, 0, 0);
2376 t.setPosition(mFGSurfaceControl, 0, 0);
2377 });
Robert Carr1725eee2017-04-26 18:32:15 -07002378
2379 {
2380 ScreenCapture::captureScreen(&mCapture);
2381 // We've positioned the child in the top left.
2382 mCapture->expectChildColor(0, 0);
2383 // But it's only 10x10.
2384 mCapture->expectFGColor(10, 10);
2385 }
Robert Carr1725eee2017-04-26 18:32:15 -07002386 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2387 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002388 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002389 sp<Surface> s = mFGSurfaceControl->getSurface();
2390 auto anw = static_cast<ANativeWindow*>(s.get());
2391 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2392 native_window_set_buffers_dimensions(anw, 64, 128);
2393 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2394 waitForPostedBuffers();
2395
2396 {
2397 // The child should still be in the same place and not have any strange scaling as in
2398 // b/37673612.
2399 ScreenCapture::captureScreen(&mCapture);
2400 mCapture->expectChildColor(0, 0);
2401 mCapture->expectFGColor(10, 10);
2402 }
2403}
2404
Dan Stoza412903f2017-04-27 13:42:17 -07002405TEST_F(ChildLayerTest, Bug36858924) {
2406 // Destroy the child layer
2407 mChild.clear();
2408
2409 // Now recreate it as hidden
2410 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2411 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2412 mFGSurfaceControl.get());
2413
2414 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002415 asTransaction([&](Transaction& t) {
2416 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002417 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002418 t.show(mChild);
2419 });
Dan Stoza412903f2017-04-27 13:42:17 -07002420
2421 // Render the foreground surface a few times
2422 //
2423 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2424 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2425 // never acquire/release the first buffer
2426 ALOGI("Filling 1");
2427 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2428 ALOGI("Filling 2");
2429 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2430 ALOGI("Filling 3");
2431 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2432 ALOGI("Filling 4");
2433 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2434}
2435
chaviwf1961f72017-09-18 16:41:07 -07002436TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002437 asTransaction([&](Transaction& t) {
2438 t.show(mChild);
2439 t.setPosition(mChild, 10, 10);
2440 t.setPosition(mFGSurfaceControl, 64, 64);
2441 });
chaviw06178942017-07-27 10:25:59 -07002442
2443 {
2444 ScreenCapture::captureScreen(&mCapture);
2445 // Top left of foreground must now be visible
2446 mCapture->expectFGColor(64, 64);
2447 // But 10 pixels in we should see the child surface
2448 mCapture->expectChildColor(74, 74);
2449 // And 10 more pixels we should be back to the foreground surface
2450 mCapture->expectFGColor(84, 84);
2451 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002452
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002453 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002454
chaviw06178942017-07-27 10:25:59 -07002455 {
2456 ScreenCapture::captureScreen(&mCapture);
2457 mCapture->expectFGColor(64, 64);
2458 // In reparenting we should have exposed the entire foreground surface.
2459 mCapture->expectFGColor(74, 74);
2460 // And the child layer should now begin at 10, 10 (since the BG
2461 // layer is at (0, 0)).
2462 mCapture->expectBGColor(9, 9);
2463 mCapture->expectChildColor(10, 10);
2464 }
2465}
2466
chaviwf1961f72017-09-18 16:41:07 -07002467TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002468 asTransaction([&](Transaction& t) {
2469 t.show(mChild);
2470 t.setPosition(mChild, 10, 10);
2471 t.setPosition(mFGSurfaceControl, 64, 64);
2472 });
chaviwf1961f72017-09-18 16:41:07 -07002473
2474 {
2475 ScreenCapture::captureScreen(&mCapture);
2476 // Top left of foreground must now be visible
2477 mCapture->expectFGColor(64, 64);
2478 // But 10 pixels in we should see the child surface
2479 mCapture->expectChildColor(74, 74);
2480 // And 10 more pixels we should be back to the foreground surface
2481 mCapture->expectFGColor(84, 84);
2482 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002483 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002484 {
2485 ScreenCapture::captureScreen(&mCapture);
2486 // Nothing should have changed.
2487 mCapture->expectFGColor(64, 64);
2488 mCapture->expectChildColor(74, 74);
2489 mCapture->expectFGColor(84, 84);
2490 }
2491}
2492
2493TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002494 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2495 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002496 ASSERT_TRUE(newSurface != NULL);
2497 ASSERT_TRUE(newSurface->isValid());
2498
2499 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002500 asTransaction([&](Transaction& t) {
2501 t.hide(mChild);
2502 t.show(newSurface);
2503 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002504 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002505 t.setPosition(mFGSurfaceControl, 64, 64);
2506 });
chaviwf1961f72017-09-18 16:41:07 -07002507
2508 {
2509 ScreenCapture::captureScreen(&mCapture);
2510 // Top left of foreground must now be visible
2511 mCapture->expectFGColor(64, 64);
2512 // At 10, 10 we should see the new surface
2513 mCapture->checkPixel(10, 10, 63, 195, 63);
2514 }
2515
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002516 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002517
2518 {
2519 ScreenCapture::captureScreen(&mCapture);
2520 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2521 // mFGSurface, putting it at 74, 74.
2522 mCapture->expectFGColor(64, 64);
2523 mCapture->checkPixel(74, 74, 63, 195, 63);
2524 mCapture->expectFGColor(84, 84);
2525 }
2526}
2527
chaviwc9674332017-08-28 12:32:18 -07002528TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002529 sp<SurfaceControl> grandchild =
2530 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2531 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002532 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2533
2534 {
2535 ScreenCapture::captureScreen(&mCapture);
2536 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2537 // which begins at 64, 64
2538 mCapture->checkPixel(64, 64, 50, 50, 50);
2539 }
2540}
2541
Robert Carr503c7042017-09-27 15:06:08 -07002542TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002543 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2544 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002545 fillSurfaceRGBA8(relative, 255, 255, 255);
2546
2547 Transaction t;
2548 t.setLayer(relative, INT32_MAX)
2549 .setRelativeLayer(mChild, relative->getHandle(), 1)
2550 .setPosition(mFGSurfaceControl, 0, 0)
2551 .apply(true);
2552
2553 // We expect that the child should have been elevated above our
2554 // INT_MAX layer even though it's not a child of it.
2555 {
2556 ScreenCapture::captureScreen(&mCapture);
2557 mCapture->expectChildColor(0, 0);
2558 mCapture->expectChildColor(9, 9);
2559 mCapture->checkPixel(10, 10, 255, 255, 255);
2560 }
2561}
2562
chaviw13fdc492017-06-27 12:40:18 -07002563class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002564protected:
chaviw13fdc492017-06-27 12:40:18 -07002565 void SetUp() override {
2566 LayerUpdateTest::SetUp();
2567
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002568 mLayerColorControl =
2569 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2570 PIXEL_FORMAT_RGBA_8888,
2571 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002572
2573 ASSERT_TRUE(mLayerColorControl != NULL);
2574 ASSERT_TRUE(mLayerColorControl->isValid());
2575
Robert Carr4cdc58f2017-08-23 14:22:20 -07002576 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002577 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002578 t.setPosition(mLayerColorControl, 140, 140);
2579 t.hide(mLayerColorControl);
2580 t.hide(mFGSurfaceControl);
2581 });
chaviw13fdc492017-06-27 12:40:18 -07002582 }
2583
2584 void TearDown() override {
2585 LayerUpdateTest::TearDown();
2586 mLayerColorControl = 0;
2587 }
2588
2589 sp<SurfaceControl> mLayerColorControl;
2590};
2591
2592TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2593 sp<ScreenCapture> sc;
2594
2595 {
2596 SCOPED_TRACE("before setColor");
2597 ScreenCapture::captureScreen(&sc);
2598 sc->expectBGColor(145, 145);
2599 }
2600
Robert Carr4cdc58f2017-08-23 14:22:20 -07002601 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002602 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002603 t.setColor(mLayerColorControl, color);
2604 t.show(mLayerColorControl);
2605 });
chaviw13fdc492017-06-27 12:40:18 -07002606
chaviw13fdc492017-06-27 12:40:18 -07002607 {
2608 // There should now be a color
2609 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002610
chaviw13fdc492017-06-27 12:40:18 -07002611 ScreenCapture::captureScreen(&sc);
2612 sc->checkPixel(145, 145, 43, 207, 131);
2613 }
2614}
2615
2616TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2617 sp<ScreenCapture> sc;
2618 {
2619 SCOPED_TRACE("before setColor");
2620 ScreenCapture::captureScreen(&sc);
2621 sc->expectBGColor(145, 145);
2622 }
2623
Robert Carr4cdc58f2017-08-23 14:22:20 -07002624 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002625 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002626 t.setColor(mLayerColorControl, color);
2627 t.setAlpha(mLayerColorControl, .75f);
2628 t.show(mLayerColorControl);
2629 });
2630
chaviw13fdc492017-06-27 12:40:18 -07002631 {
2632 // There should now be a color with .75 alpha
2633 SCOPED_TRACE("after setColor");
2634 ScreenCapture::captureScreen(&sc);
2635 sc->checkPixel(145, 145, 48, 171, 147);
2636 }
2637}
2638
2639TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2640 sp<ScreenCapture> sc;
2641 {
2642 SCOPED_TRACE("before setColor");
2643 ScreenCapture::captureScreen(&sc);
2644 sc->expectBGColor(145, 145);
2645 }
2646
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002647 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002648
chaviw13fdc492017-06-27 12:40:18 -07002649 {
2650 // There should now be set to 0,0,0 (black) as default.
2651 SCOPED_TRACE("after setColor");
2652 ScreenCapture::captureScreen(&sc);
2653 sc->checkPixel(145, 145, 0, 0, 0);
2654 }
2655}
2656
chaviwa76b2712017-09-20 12:02:26 -07002657class ScreenCaptureTest : public LayerUpdateTest {
2658protected:
2659 std::unique_ptr<CaptureLayer> mCapture;
2660};
2661
2662TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2663 auto bgHandle = mBGSurfaceControl->getHandle();
2664 CaptureLayer::captureScreen(&mCapture, bgHandle);
2665 mCapture->expectBGColor(0, 0);
2666 // Doesn't capture FG layer which is at 64, 64
2667 mCapture->expectBGColor(64, 64);
2668}
2669
2670TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2671 auto fgHandle = mFGSurfaceControl->getHandle();
2672
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002673 sp<SurfaceControl> child =
2674 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2675 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002676 fillSurfaceRGBA8(child, 200, 200, 200);
2677
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002678 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002679
2680 // Captures mFGSurfaceControl layer and its child.
2681 CaptureLayer::captureScreen(&mCapture, fgHandle);
2682 mCapture->expectFGColor(10, 10);
2683 mCapture->expectChildColor(0, 0);
2684}
2685
2686TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2687 auto fgHandle = mFGSurfaceControl->getHandle();
2688
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002689 sp<SurfaceControl> child =
2690 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2691 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002692 fillSurfaceRGBA8(child, 200, 200, 200);
2693
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002694 sp<SurfaceControl> grandchild =
2695 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2696 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002697
2698 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2699 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002700 .show(child)
2701 .setPosition(grandchild, 5, 5)
2702 .show(grandchild)
2703 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002704
2705 // Captures mFGSurfaceControl, its child, and the grandchild.
2706 CaptureLayer::captureScreen(&mCapture, fgHandle);
2707 mCapture->expectFGColor(10, 10);
2708 mCapture->expectChildColor(0, 0);
2709 mCapture->checkPixel(5, 5, 50, 50, 50);
2710}
2711
2712TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002713 sp<SurfaceControl> child =
2714 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2715 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002716 fillSurfaceRGBA8(child, 200, 200, 200);
2717 auto childHandle = child->getHandle();
2718
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002719 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002720
2721 // Captures only the child layer, and not the parent.
2722 CaptureLayer::captureScreen(&mCapture, childHandle);
2723 mCapture->expectChildColor(0, 0);
2724 mCapture->expectChildColor(9, 9);
2725}
2726
2727TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002728 sp<SurfaceControl> child =
2729 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2730 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002731 fillSurfaceRGBA8(child, 200, 200, 200);
2732 auto childHandle = child->getHandle();
2733
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002734 sp<SurfaceControl> grandchild =
2735 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2736 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002737 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2738
2739 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002740 .show(child)
2741 .setPosition(grandchild, 5, 5)
2742 .show(grandchild)
2743 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002744
2745 auto grandchildHandle = grandchild->getHandle();
2746
2747 // Captures only the grandchild.
2748 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2749 mCapture->checkPixel(0, 0, 50, 50, 50);
2750 mCapture->checkPixel(4, 4, 50, 50, 50);
2751}
2752
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002753} // namespace android