blob: d80c2134e462b229886bfdfaee6569aa5c9d552b [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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001148class LayerUpdateTest : public ::testing::Test {
1149protected:
1150 virtual void SetUp() {
1151 mComposerClient = new SurfaceComposerClient;
1152 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1153
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001154 sp<IBinder> display(
1155 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001156 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001157 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001158
1159 ssize_t displayWidth = info.w;
1160 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001161
1162 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001163 mBGSurfaceControl =
1164 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1165 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001166 ASSERT_TRUE(mBGSurfaceControl != NULL);
1167 ASSERT_TRUE(mBGSurfaceControl->isValid());
1168 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1169
1170 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001171 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1172 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001173 ASSERT_TRUE(mFGSurfaceControl != NULL);
1174 ASSERT_TRUE(mFGSurfaceControl->isValid());
1175
1176 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1177
1178 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001179 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1180 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001181 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1182 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1183
1184 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1185
Robert Carr4cdc58f2017-08-23 14:22:20 -07001186 asTransaction([&](Transaction& t) {
1187 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001188
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001189 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001190
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001191 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1192 .setPosition(mFGSurfaceControl, 64, 64)
1193 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001194
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001195 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1196 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1197 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001198 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001199 }
1200
1201 virtual void TearDown() {
1202 mComposerClient->dispose();
1203 mBGSurfaceControl = 0;
1204 mFGSurfaceControl = 0;
1205 mSyncSurfaceControl = 0;
1206 mComposerClient = 0;
1207 }
1208
1209 void waitForPostedBuffers() {
1210 // Since the sync surface is in synchronous mode (i.e. double buffered)
1211 // posting three buffers to it should ensure that at least two
1212 // SurfaceFlinger::handlePageFlip calls have been made, which should
1213 // guaranteed that a buffer posted to another Surface has been retired.
1214 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1215 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1216 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1217 }
1218
Robert Carr4cdc58f2017-08-23 14:22:20 -07001219 void asTransaction(const std::function<void(Transaction&)>& exec) {
1220 Transaction t;
1221 exec(t);
1222 t.apply(true);
1223 }
1224
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001225 sp<SurfaceComposerClient> mComposerClient;
1226 sp<SurfaceControl> mBGSurfaceControl;
1227 sp<SurfaceControl> mFGSurfaceControl;
1228
1229 // This surface is used to ensure that the buffers posted to
1230 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1231 sp<SurfaceControl> mSyncSurfaceControl;
1232};
1233
Robert Carr7f619b22017-11-06 12:56:35 -08001234TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1235 sp<ScreenCapture> sc;
1236
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001237 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1238 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001239 fillSurfaceRGBA8(relative, 10, 10, 10);
1240 waitForPostedBuffers();
1241
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001242 Transaction{}
1243 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001244 .setPosition(relative, 64, 64)
1245 .apply();
1246
1247 {
1248 // The relative should be on top of the FG control.
1249 ScreenCapture::captureScreen(&sc);
1250 sc->checkPixel(64, 64, 10, 10, 10);
1251 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001252 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001253
1254 {
1255 // Nothing should change at this point.
1256 ScreenCapture::captureScreen(&sc);
1257 sc->checkPixel(64, 64, 10, 10, 10);
1258 }
1259
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001260 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001261
1262 {
1263 // Ensure that the relative was actually hidden, rather than
1264 // being left in the detached but visible state.
1265 ScreenCapture::captureScreen(&sc);
1266 sc->expectFGColor(64, 64);
1267 }
1268}
1269
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001270TEST_F(LayerUpdateTest, LayerMoveWorks) {
1271 sp<ScreenCapture> sc;
1272 {
1273 SCOPED_TRACE("before move");
1274 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001275 sc->expectBGColor(0, 12);
1276 sc->expectFGColor(75, 75);
1277 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001278 }
1279
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001280 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001281
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001282 {
1283 // This should reflect the new position, but not the new color.
1284 SCOPED_TRACE("after move, before redraw");
1285 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001286 sc->expectBGColor(24, 24);
1287 sc->expectBGColor(75, 75);
1288 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001289 }
1290
1291 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1292 waitForPostedBuffers();
1293 {
1294 // This should reflect the new position and the new color.
1295 SCOPED_TRACE("after redraw");
1296 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001297 sc->expectBGColor(24, 24);
1298 sc->expectBGColor(75, 75);
1299 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001300 }
1301}
1302
1303TEST_F(LayerUpdateTest, LayerResizeWorks) {
1304 sp<ScreenCapture> sc;
1305 {
1306 SCOPED_TRACE("before resize");
1307 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001308 sc->expectBGColor(0, 12);
1309 sc->expectFGColor(75, 75);
1310 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001311 }
1312
Steve Block9d453682011-12-20 16:23:08 +00001313 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001314 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001315 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001316 {
1317 // This should not reflect the new size or color because SurfaceFlinger
1318 // has not yet received a buffer of the correct size.
1319 SCOPED_TRACE("after resize, before redraw");
1320 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001321 sc->expectBGColor(0, 12);
1322 sc->expectFGColor(75, 75);
1323 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001324 }
1325
Steve Block9d453682011-12-20 16:23:08 +00001326 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001327 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1328 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001329 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001330 {
1331 // This should reflect the new size and the new color.
1332 SCOPED_TRACE("after redraw");
1333 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001334 sc->expectBGColor(24, 24);
1335 sc->checkPixel(75, 75, 63, 195, 63);
1336 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001337 }
1338}
1339
Haixia Shid5750962015-07-27 16:50:49 -07001340TEST_F(LayerUpdateTest, LayerCropWorks) {
1341 sp<ScreenCapture> sc;
1342 {
1343 SCOPED_TRACE("before crop");
1344 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001345 sc->expectBGColor(24, 24);
1346 sc->expectFGColor(75, 75);
1347 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001348 }
1349
Robert Carr4cdc58f2017-08-23 14:22:20 -07001350 asTransaction([&](Transaction& t) {
1351 Rect cropRect(16, 16, 32, 32);
1352 t.setCrop(mFGSurfaceControl, cropRect);
1353 });
Haixia Shid5750962015-07-27 16:50:49 -07001354 {
1355 // This should crop the foreground surface.
1356 SCOPED_TRACE("after crop");
1357 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001358 sc->expectBGColor(24, 24);
1359 sc->expectBGColor(75, 75);
1360 sc->expectFGColor(95, 80);
1361 sc->expectFGColor(80, 95);
1362 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001363 }
1364}
1365
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001366TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1367 sp<ScreenCapture> sc;
1368 {
1369 SCOPED_TRACE("before crop");
1370 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001371 sc->expectBGColor(24, 24);
1372 sc->expectFGColor(75, 75);
1373 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001374 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001375 asTransaction([&](Transaction& t) {
1376 Rect cropRect(16, 16, 32, 32);
1377 t.setFinalCrop(mFGSurfaceControl, cropRect);
1378 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001379 {
1380 // This should crop the foreground surface.
1381 SCOPED_TRACE("after crop");
1382 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001383 sc->expectBGColor(24, 24);
1384 sc->expectBGColor(75, 75);
1385 sc->expectBGColor(95, 80);
1386 sc->expectBGColor(80, 95);
1387 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001388 }
1389}
1390
Haixia Shid5750962015-07-27 16:50:49 -07001391TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1392 sp<ScreenCapture> sc;
1393 {
1394 SCOPED_TRACE("before setLayer");
1395 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001396 sc->expectBGColor(24, 24);
1397 sc->expectFGColor(75, 75);
1398 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001399 }
1400
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001401 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001402
Haixia Shid5750962015-07-27 16:50:49 -07001403 {
1404 // This should hide the foreground surface beneath the background.
1405 SCOPED_TRACE("after setLayer");
1406 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001407 sc->expectBGColor(24, 24);
1408 sc->expectBGColor(75, 75);
1409 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001410 }
1411}
1412
1413TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1414 sp<ScreenCapture> sc;
1415 {
1416 SCOPED_TRACE("before hide");
1417 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001418 sc->expectBGColor(24, 24);
1419 sc->expectFGColor(75, 75);
1420 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001421 }
1422
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001423 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001424
Haixia Shid5750962015-07-27 16:50:49 -07001425 {
1426 // This should hide the foreground surface.
1427 SCOPED_TRACE("after hide, before show");
1428 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001429 sc->expectBGColor(24, 24);
1430 sc->expectBGColor(75, 75);
1431 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001432 }
1433
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001434 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001435
Haixia Shid5750962015-07-27 16:50:49 -07001436 {
1437 // This should show the foreground surface.
1438 SCOPED_TRACE("after show");
1439 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001440 sc->expectBGColor(24, 24);
1441 sc->expectFGColor(75, 75);
1442 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001443 }
1444}
1445
1446TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1447 sp<ScreenCapture> sc;
1448 {
1449 SCOPED_TRACE("before setAlpha");
1450 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001451 sc->expectBGColor(24, 24);
1452 sc->expectFGColor(75, 75);
1453 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001454 }
1455
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001456 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001457
Haixia Shid5750962015-07-27 16:50:49 -07001458 {
1459 // This should set foreground to be 75% opaque.
1460 SCOPED_TRACE("after setAlpha");
1461 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001462 sc->expectBGColor(24, 24);
1463 sc->checkPixel(75, 75, 162, 63, 96);
1464 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001465 }
1466}
1467
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001468TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1469 sp<ScreenCapture> sc;
1470 {
1471 SCOPED_TRACE("before setLayerStack");
1472 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001473 sc->expectBGColor(24, 24);
1474 sc->expectFGColor(75, 75);
1475 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001476 }
1477
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001478 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001479 {
1480 // This should hide the foreground surface since it goes to a different
1481 // layer stack.
1482 SCOPED_TRACE("after setLayerStack");
1483 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001484 sc->expectBGColor(24, 24);
1485 sc->expectBGColor(75, 75);
1486 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001487 }
1488}
1489
1490TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1491 sp<ScreenCapture> sc;
1492 {
1493 SCOPED_TRACE("before setFlags");
1494 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001495 sc->expectBGColor(24, 24);
1496 sc->expectFGColor(75, 75);
1497 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001498 }
1499
Robert Carr4cdc58f2017-08-23 14:22:20 -07001500 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001501 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001502 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001503 {
1504 // This should hide the foreground surface
1505 SCOPED_TRACE("after setFlags");
1506 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001507 sc->expectBGColor(24, 24);
1508 sc->expectBGColor(75, 75);
1509 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001510 }
1511}
1512
1513TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1514 sp<ScreenCapture> sc;
1515 {
1516 SCOPED_TRACE("before setMatrix");
1517 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001518 sc->expectBGColor(24, 24);
1519 sc->expectFGColor(91, 96);
1520 sc->expectFGColor(96, 101);
1521 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001522 }
1523
Robert Carr4cdc58f2017-08-23 14:22:20 -07001524 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001525 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001526 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001527 {
1528 SCOPED_TRACE("after setMatrix");
1529 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001530 sc->expectBGColor(24, 24);
1531 sc->expectFGColor(91, 96);
1532 sc->expectBGColor(96, 91);
1533 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001534 }
1535}
1536
Robert Carr8d5227b2017-03-16 15:41:03 -07001537class GeometryLatchingTest : public LayerUpdateTest {
1538protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001539 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001540 SCOPED_TRACE(trace);
1541 ScreenCapture::captureScreen(&sc);
1542 // We find the leading edge of the FG surface.
1543 sc->expectFGColor(127, 127);
1544 sc->expectBGColor(128, 128);
1545 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001546
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001547 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001548
1549 void unlockFGBuffer() {
1550 sp<Surface> s = mFGSurfaceControl->getSurface();
1551 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1552 waitForPostedBuffers();
1553 }
1554
Robert Carr8d5227b2017-03-16 15:41:03 -07001555 void completeFGResize() {
1556 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1557 waitForPostedBuffers();
1558 }
1559 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001560 asTransaction([&](Transaction& t) {
1561 t.setSize(mFGSurfaceControl, 64, 64);
1562 t.setPosition(mFGSurfaceControl, 64, 64);
1563 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1564 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1565 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001566
1567 EXPECT_INITIAL_STATE("After restoring initial state");
1568 }
1569 sp<ScreenCapture> sc;
1570};
1571
1572TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1573 EXPECT_INITIAL_STATE("before anything");
1574
1575 // By default position can be updated even while
1576 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001577 asTransaction([&](Transaction& t) {
1578 t.setSize(mFGSurfaceControl, 32, 32);
1579 t.setPosition(mFGSurfaceControl, 100, 100);
1580 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001581
1582 {
1583 SCOPED_TRACE("After moving surface");
1584 ScreenCapture::captureScreen(&sc);
1585 // If we moved, the FG Surface should cover up what was previously BG
1586 // however if we didn't move the FG wouldn't be large enough now.
1587 sc->expectFGColor(163, 163);
1588 }
1589
1590 restoreInitialState();
1591
1592 // Now we repeat with setGeometryAppliesWithResize
1593 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001594 asTransaction([&](Transaction& t) {
1595 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1596 t.setSize(mFGSurfaceControl, 32, 32);
1597 t.setPosition(mFGSurfaceControl, 100, 100);
1598 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001599
1600 {
1601 SCOPED_TRACE("While resize is pending");
1602 ScreenCapture::captureScreen(&sc);
1603 // This time we shouldn't have moved, so the BG color
1604 // should still be visible.
1605 sc->expectBGColor(128, 128);
1606 }
1607
1608 completeFGResize();
1609
1610 {
1611 SCOPED_TRACE("After the resize");
1612 ScreenCapture::captureScreen(&sc);
1613 // But after the resize completes, we should move
1614 // and the FG should be visible here.
1615 sc->expectFGColor(128, 128);
1616 }
1617}
1618
1619class CropLatchingTest : public GeometryLatchingTest {
1620protected:
1621 void EXPECT_CROPPED_STATE(const char* trace) {
1622 SCOPED_TRACE(trace);
1623 ScreenCapture::captureScreen(&sc);
1624 // The edge should be moved back one pixel by our crop.
1625 sc->expectFGColor(126, 126);
1626 sc->expectBGColor(127, 127);
1627 sc->expectBGColor(128, 128);
1628 }
chaviw59f5c562017-06-28 16:39:06 -07001629
1630 void EXPECT_RESIZE_STATE(const char* trace) {
1631 SCOPED_TRACE(trace);
1632 ScreenCapture::captureScreen(&sc);
1633 // The FG is now resized too 128,128 at 64,64
1634 sc->expectFGColor(64, 64);
1635 sc->expectFGColor(191, 191);
1636 sc->expectBGColor(192, 192);
1637 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001638};
1639
1640TEST_F(CropLatchingTest, CropLatching) {
1641 EXPECT_INITIAL_STATE("before anything");
1642 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001643 asTransaction([&](Transaction& t) {
1644 t.setSize(mFGSurfaceControl, 128, 128);
1645 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1646 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001647
1648 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1649
1650 restoreInitialState();
1651
Robert Carr4cdc58f2017-08-23 14:22:20 -07001652 asTransaction([&](Transaction& t) {
1653 t.setSize(mFGSurfaceControl, 128, 128);
1654 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1655 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1656 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001657
1658 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1659
1660 completeFGResize();
1661
1662 EXPECT_CROPPED_STATE("after the resize finishes");
1663}
1664
1665TEST_F(CropLatchingTest, FinalCropLatching) {
1666 EXPECT_INITIAL_STATE("before anything");
1667 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001668 asTransaction([&](Transaction& t) {
1669 t.setSize(mFGSurfaceControl, 128, 128);
1670 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1671 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001672
1673 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1674
1675 restoreInitialState();
1676
Robert Carr4cdc58f2017-08-23 14:22:20 -07001677 asTransaction([&](Transaction& t) {
1678 t.setSize(mFGSurfaceControl, 128, 128);
1679 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1680 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1681 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001682
1683 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1684
1685 completeFGResize();
1686
1687 EXPECT_CROPPED_STATE("after the resize finishes");
1688}
1689
Robert Carr7bf247e2017-05-18 14:02:49 -07001690// In this test we ensure that setGeometryAppliesWithResize actually demands
1691// a buffer of the new size, and not just any size.
1692TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1693 EXPECT_INITIAL_STATE("before anything");
1694 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001695 asTransaction([&](Transaction& t) {
1696 t.setSize(mFGSurfaceControl, 128, 128);
1697 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1698 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001699
1700 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1701
1702 restoreInitialState();
1703
1704 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1705 // initiating the resize.
1706 lockAndFillFGBuffer();
1707
Robert Carr4cdc58f2017-08-23 14:22:20 -07001708 asTransaction([&](Transaction& t) {
1709 t.setSize(mFGSurfaceControl, 128, 128);
1710 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1711 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1712 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001713
1714 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1715
1716 // We now submit our old buffer, at the old size, and ensure it doesn't
1717 // trigger geometry latching.
1718 unlockFGBuffer();
1719
1720 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1721
1722 completeFGResize();
1723
1724 EXPECT_CROPPED_STATE("after the resize finishes");
1725}
1726
1727TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1728 EXPECT_INITIAL_STATE("before anything");
1729 // In this scenario, we attempt to set the final crop a second time while the resize
1730 // is still pending, and ensure we are successful. Success meaning the second crop
1731 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001732 asTransaction([&](Transaction& t) {
1733 t.setSize(mFGSurfaceControl, 128, 128);
1734 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1735 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1736 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001737
chaviw59f5c562017-06-28 16:39:06 -07001738 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1739
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001740 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001741
chaviw59f5c562017-06-28 16:39:06 -07001742 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001743
1744 completeFGResize();
1745
chaviw59f5c562017-06-28 16:39:06 -07001746 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001747}
1748
Pablo Ceballos05289c22016-04-14 15:49:55 -07001749TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1750 sp<ScreenCapture> sc;
1751 {
1752 SCOPED_TRACE("before anything");
1753 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001754 sc->expectBGColor(32, 32);
1755 sc->expectFGColor(96, 96);
1756 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001757 }
1758
1759 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001760 asTransaction([&](Transaction& t) {
1761 t.setAlpha(mFGSurfaceControl, 0.75);
1762 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001763 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001764 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001765
Robert Carr4cdc58f2017-08-23 14:22:20 -07001766 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001767 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001768 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001769 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001770 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001771
1772 {
1773 SCOPED_TRACE("before any trigger");
1774 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001775 sc->expectBGColor(32, 32);
1776 sc->expectFGColor(96, 96);
1777 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001778 }
1779
1780 // should trigger the first deferred transaction, but not the second one
1781 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1782 {
1783 SCOPED_TRACE("after first trigger");
1784 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001785 sc->expectBGColor(32, 32);
1786 sc->checkPixel(96, 96, 162, 63, 96);
1787 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001788 }
1789
1790 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001791 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001792
1793 // trigger the second deferred transaction
1794 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1795 {
1796 SCOPED_TRACE("after second trigger");
1797 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001798 sc->expectBGColor(32, 32);
1799 sc->expectBGColor(96, 96);
1800 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001801 }
1802}
1803
Robert Carrdb66e622017-04-10 16:55:57 -07001804TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1805 sp<ScreenCapture> sc;
1806 {
1807 SCOPED_TRACE("before adding relative surface");
1808 ScreenCapture::captureScreen(&sc);
1809 sc->expectBGColor(24, 24);
1810 sc->expectFGColor(75, 75);
1811 sc->expectBGColor(145, 145);
1812 }
1813
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001814 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1815 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001816 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1817 waitForPostedBuffers();
1818
1819 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001820 asTransaction([&](Transaction& t) {
1821 t.setPosition(relativeSurfaceControl, 64, 64);
1822 t.show(relativeSurfaceControl);
1823 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1824 });
Robert Carrdb66e622017-04-10 16:55:57 -07001825
1826 {
1827 SCOPED_TRACE("after adding relative surface");
1828 ScreenCapture::captureScreen(&sc);
1829 // our relative surface should be visible now.
1830 sc->checkPixel(75, 75, 255, 177, 177);
1831 }
1832
1833 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001834 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001835
1836 {
1837 SCOPED_TRACE("after set layer");
1838 ScreenCapture::captureScreen(&sc);
1839 // now the FG surface should be visible again.
1840 sc->expectFGColor(75, 75);
1841 }
1842}
1843
Robert Carre392b552017-09-19 12:16:05 -07001844TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1845 sp<ScreenCapture> sc;
1846
1847 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001848 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1849 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1850 sp<SurfaceControl> childBuffer =
1851 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1852 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001853 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1854
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001855 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001856
1857 {
1858 ScreenCapture::captureScreen(&sc);
1859 sc->expectChildColor(73, 73);
1860 sc->expectFGColor(74, 74);
1861 }
1862
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001863 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001864
1865 {
1866 ScreenCapture::captureScreen(&sc);
1867 sc->expectChildColor(73, 73);
1868 sc->expectChildColor(74, 74);
1869 }
1870}
1871
Robert Carr2c5f6d22017-09-26 12:30:35 -07001872TEST_F(LayerUpdateTest, MergingTransactions) {
1873 sp<ScreenCapture> sc;
1874 {
1875 SCOPED_TRACE("before move");
1876 ScreenCapture::captureScreen(&sc);
1877 sc->expectBGColor(0, 12);
1878 sc->expectFGColor(75, 75);
1879 sc->expectBGColor(145, 145);
1880 }
1881
1882 Transaction t1, t2;
1883 t1.setPosition(mFGSurfaceControl, 128, 128);
1884 t2.setPosition(mFGSurfaceControl, 0, 0);
1885 // We expect that the position update from t2 now
1886 // overwrites the position update from t1.
1887 t1.merge(std::move(t2));
1888 t1.apply();
1889
1890 {
1891 ScreenCapture::captureScreen(&sc);
1892 sc->expectFGColor(1, 1);
1893 }
1894}
1895
Robert Carr1f0a16a2016-10-24 16:27:39 -07001896class ChildLayerTest : public LayerUpdateTest {
1897protected:
1898 void SetUp() override {
1899 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001900 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1901 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001902 fillSurfaceRGBA8(mChild, 200, 200, 200);
1903
1904 {
1905 SCOPED_TRACE("before anything");
1906 ScreenCapture::captureScreen(&mCapture);
1907 mCapture->expectChildColor(64, 64);
1908 }
1909 }
1910 void TearDown() override {
1911 LayerUpdateTest::TearDown();
1912 mChild = 0;
1913 }
1914
1915 sp<SurfaceControl> mChild;
1916 sp<ScreenCapture> mCapture;
1917};
1918
1919TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001920 asTransaction([&](Transaction& t) {
1921 t.show(mChild);
1922 t.setPosition(mChild, 10, 10);
1923 t.setPosition(mFGSurfaceControl, 64, 64);
1924 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001925
1926 {
1927 ScreenCapture::captureScreen(&mCapture);
1928 // Top left of foreground must now be visible
1929 mCapture->expectFGColor(64, 64);
1930 // But 10 pixels in we should see the child surface
1931 mCapture->expectChildColor(74, 74);
1932 // And 10 more pixels we should be back to the foreground surface
1933 mCapture->expectFGColor(84, 84);
1934 }
1935
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001936 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001937
1938 {
1939 ScreenCapture::captureScreen(&mCapture);
1940 // Top left of foreground should now be at 0, 0
1941 mCapture->expectFGColor(0, 0);
1942 // But 10 pixels in we should see the child surface
1943 mCapture->expectChildColor(10, 10);
1944 // And 10 more pixels we should be back to the foreground surface
1945 mCapture->expectFGColor(20, 20);
1946 }
1947}
1948
Robert Carr41b08b52017-06-01 16:11:34 -07001949TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001950 asTransaction([&](Transaction& t) {
1951 t.show(mChild);
1952 t.setPosition(mChild, 0, 0);
1953 t.setPosition(mFGSurfaceControl, 0, 0);
1954 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1955 });
Robert Carr41b08b52017-06-01 16:11:34 -07001956
1957 {
1958 ScreenCapture::captureScreen(&mCapture);
1959 mCapture->expectChildColor(0, 0);
1960 mCapture->expectChildColor(4, 4);
1961 mCapture->expectBGColor(5, 5);
1962 }
1963}
1964
1965TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001966 asTransaction([&](Transaction& t) {
1967 t.show(mChild);
1968 t.setPosition(mChild, 0, 0);
1969 t.setPosition(mFGSurfaceControl, 0, 0);
1970 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1971 });
Robert Carr41b08b52017-06-01 16:11:34 -07001972
1973 {
1974 ScreenCapture::captureScreen(&mCapture);
1975 mCapture->expectChildColor(0, 0);
1976 mCapture->expectChildColor(4, 4);
1977 mCapture->expectBGColor(5, 5);
1978 }
1979}
1980
Robert Carr1f0a16a2016-10-24 16:27:39 -07001981TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001982 asTransaction([&](Transaction& t) {
1983 t.show(mChild);
1984 t.setPosition(mFGSurfaceControl, 0, 0);
1985 t.setPosition(mChild, 63, 63);
1986 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001987
1988 {
1989 ScreenCapture::captureScreen(&mCapture);
1990 mCapture->expectFGColor(0, 0);
1991 // Last pixel in foreground should now be the child.
1992 mCapture->expectChildColor(63, 63);
1993 // But the child should be constrained and the next pixel
1994 // must be the background
1995 mCapture->expectBGColor(64, 64);
1996 }
1997}
1998
1999TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002000 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002001
2002 // Find the boundary between the parent and child
2003 {
2004 ScreenCapture::captureScreen(&mCapture);
2005 mCapture->expectChildColor(9, 9);
2006 mCapture->expectFGColor(10, 10);
2007 }
2008
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002009 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002010
2011 // The boundary should be twice as far from the origin now.
2012 // The pixels from the last test should all be child now
2013 {
2014 ScreenCapture::captureScreen(&mCapture);
2015 mCapture->expectChildColor(9, 9);
2016 mCapture->expectChildColor(10, 10);
2017 mCapture->expectChildColor(19, 19);
2018 mCapture->expectFGColor(20, 20);
2019 }
2020}
Robert Carr9524cb32017-02-13 11:32:32 -08002021
Robert Carr6452f122017-03-21 10:41:29 -07002022TEST_F(ChildLayerTest, ChildLayerAlpha) {
2023 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2024 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2025 fillSurfaceRGBA8(mChild, 0, 254, 0);
2026 waitForPostedBuffers();
2027
Robert Carr4cdc58f2017-08-23 14:22:20 -07002028 asTransaction([&](Transaction& t) {
2029 t.show(mChild);
2030 t.setPosition(mChild, 0, 0);
2031 t.setPosition(mFGSurfaceControl, 0, 0);
2032 });
Robert Carr6452f122017-03-21 10:41:29 -07002033
2034 {
2035 ScreenCapture::captureScreen(&mCapture);
2036 // Unblended child color
2037 mCapture->checkPixel(0, 0, 0, 254, 0);
2038 }
2039
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002040 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002041
2042 {
2043 ScreenCapture::captureScreen(&mCapture);
2044 // Child and BG blended.
2045 mCapture->checkPixel(0, 0, 127, 127, 0);
2046 }
2047
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002048 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002049
2050 {
2051 ScreenCapture::captureScreen(&mCapture);
2052 // Child and BG blended.
2053 mCapture->checkPixel(0, 0, 95, 64, 95);
2054 }
2055}
2056
Robert Carr9524cb32017-02-13 11:32:32 -08002057TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002058 asTransaction([&](Transaction& t) {
2059 t.show(mChild);
2060 t.setPosition(mChild, 10, 10);
2061 t.setPosition(mFGSurfaceControl, 64, 64);
2062 });
Robert Carr9524cb32017-02-13 11:32:32 -08002063
2064 {
2065 ScreenCapture::captureScreen(&mCapture);
2066 // Top left of foreground must now be visible
2067 mCapture->expectFGColor(64, 64);
2068 // But 10 pixels in we should see the child surface
2069 mCapture->expectChildColor(74, 74);
2070 // And 10 more pixels we should be back to the foreground surface
2071 mCapture->expectFGColor(84, 84);
2072 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002073
2074 asTransaction([&](Transaction& t) {
2075 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2076 });
2077
Robert Carr9524cb32017-02-13 11:32:32 -08002078 {
2079 ScreenCapture::captureScreen(&mCapture);
2080 mCapture->expectFGColor(64, 64);
2081 // In reparenting we should have exposed the entire foreground surface.
2082 mCapture->expectFGColor(74, 74);
2083 // And the child layer should now begin at 10, 10 (since the BG
2084 // layer is at (0, 0)).
2085 mCapture->expectBGColor(9, 9);
2086 mCapture->expectChildColor(10, 10);
2087 }
2088}
2089
chaviw161410b02017-07-27 10:46:08 -07002090TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002091 asTransaction([&](Transaction& t) {
2092 t.show(mChild);
2093 t.setPosition(mChild, 10, 10);
2094 t.setPosition(mFGSurfaceControl, 64, 64);
2095 });
Robert Carr9524cb32017-02-13 11:32:32 -08002096
2097 {
2098 ScreenCapture::captureScreen(&mCapture);
2099 // Top left of foreground must now be visible
2100 mCapture->expectFGColor(64, 64);
2101 // But 10 pixels in we should see the child surface
2102 mCapture->expectChildColor(74, 74);
2103 // And 10 more pixels we should be back to the foreground surface
2104 mCapture->expectFGColor(84, 84);
2105 }
2106
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002107 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002108
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002109 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002110
chaviw161410b02017-07-27 10:46:08 -07002111 // Since the child has the same client as the parent, it will not get
2112 // detached and will be hidden.
2113 {
2114 ScreenCapture::captureScreen(&mCapture);
2115 mCapture->expectFGColor(64, 64);
2116 mCapture->expectFGColor(74, 74);
2117 mCapture->expectFGColor(84, 84);
2118 }
2119}
2120
2121TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2122 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002123 sp<SurfaceControl> mChildNewClient =
2124 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2125 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002126
2127 ASSERT_TRUE(mChildNewClient != NULL);
2128 ASSERT_TRUE(mChildNewClient->isValid());
2129
2130 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2131
Robert Carr4cdc58f2017-08-23 14:22:20 -07002132 asTransaction([&](Transaction& t) {
2133 t.hide(mChild);
2134 t.show(mChildNewClient);
2135 t.setPosition(mChildNewClient, 10, 10);
2136 t.setPosition(mFGSurfaceControl, 64, 64);
2137 });
chaviw161410b02017-07-27 10:46:08 -07002138
2139 {
2140 ScreenCapture::captureScreen(&mCapture);
2141 // Top left of foreground must now be visible
2142 mCapture->expectFGColor(64, 64);
2143 // But 10 pixels in we should see the child surface
2144 mCapture->expectChildColor(74, 74);
2145 // And 10 more pixels we should be back to the foreground surface
2146 mCapture->expectFGColor(84, 84);
2147 }
2148
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002149 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002150
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002151 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002152
Robert Carr9524cb32017-02-13 11:32:32 -08002153 // Nothing should have changed.
2154 {
2155 ScreenCapture::captureScreen(&mCapture);
2156 mCapture->expectFGColor(64, 64);
2157 mCapture->expectChildColor(74, 74);
2158 mCapture->expectFGColor(84, 84);
2159 }
2160}
2161
Robert Carr9b429f42017-04-17 14:56:57 -07002162TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002163 asTransaction([&](Transaction& t) {
2164 t.show(mChild);
2165 t.setPosition(mChild, 0, 0);
2166 t.setPosition(mFGSurfaceControl, 0, 0);
2167 });
Robert Carr9b429f42017-04-17 14:56:57 -07002168
2169 {
2170 ScreenCapture::captureScreen(&mCapture);
2171 // We've positioned the child in the top left.
2172 mCapture->expectChildColor(0, 0);
2173 // But it's only 10x10.
2174 mCapture->expectFGColor(10, 10);
2175 }
2176
Robert Carr4cdc58f2017-08-23 14:22:20 -07002177 asTransaction([&](Transaction& t) {
2178 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2179 // We cause scaling by 2.
2180 t.setSize(mFGSurfaceControl, 128, 128);
2181 });
Robert Carr9b429f42017-04-17 14:56:57 -07002182
2183 {
2184 ScreenCapture::captureScreen(&mCapture);
2185 // We've positioned the child in the top left.
2186 mCapture->expectChildColor(0, 0);
2187 mCapture->expectChildColor(10, 10);
2188 mCapture->expectChildColor(19, 19);
2189 // And now it should be scaled all the way to 20x20
2190 mCapture->expectFGColor(20, 20);
2191 }
2192}
2193
Robert Carr1725eee2017-04-26 18:32:15 -07002194// Regression test for b/37673612
2195TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002196 asTransaction([&](Transaction& t) {
2197 t.show(mChild);
2198 t.setPosition(mChild, 0, 0);
2199 t.setPosition(mFGSurfaceControl, 0, 0);
2200 });
Robert Carr1725eee2017-04-26 18:32:15 -07002201
2202 {
2203 ScreenCapture::captureScreen(&mCapture);
2204 // We've positioned the child in the top left.
2205 mCapture->expectChildColor(0, 0);
2206 // But it's only 10x10.
2207 mCapture->expectFGColor(10, 10);
2208 }
Robert Carr1725eee2017-04-26 18:32:15 -07002209 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2210 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002211 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002212 sp<Surface> s = mFGSurfaceControl->getSurface();
2213 auto anw = static_cast<ANativeWindow*>(s.get());
2214 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2215 native_window_set_buffers_dimensions(anw, 64, 128);
2216 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2217 waitForPostedBuffers();
2218
2219 {
2220 // The child should still be in the same place and not have any strange scaling as in
2221 // b/37673612.
2222 ScreenCapture::captureScreen(&mCapture);
2223 mCapture->expectChildColor(0, 0);
2224 mCapture->expectFGColor(10, 10);
2225 }
2226}
2227
Dan Stoza412903f2017-04-27 13:42:17 -07002228TEST_F(ChildLayerTest, Bug36858924) {
2229 // Destroy the child layer
2230 mChild.clear();
2231
2232 // Now recreate it as hidden
2233 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2234 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2235 mFGSurfaceControl.get());
2236
2237 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002238 asTransaction([&](Transaction& t) {
2239 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002240 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002241 t.show(mChild);
2242 });
Dan Stoza412903f2017-04-27 13:42:17 -07002243
2244 // Render the foreground surface a few times
2245 //
2246 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2247 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2248 // never acquire/release the first buffer
2249 ALOGI("Filling 1");
2250 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2251 ALOGI("Filling 2");
2252 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2253 ALOGI("Filling 3");
2254 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2255 ALOGI("Filling 4");
2256 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2257}
2258
chaviwf1961f72017-09-18 16:41:07 -07002259TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002260 asTransaction([&](Transaction& t) {
2261 t.show(mChild);
2262 t.setPosition(mChild, 10, 10);
2263 t.setPosition(mFGSurfaceControl, 64, 64);
2264 });
chaviw06178942017-07-27 10:25:59 -07002265
2266 {
2267 ScreenCapture::captureScreen(&mCapture);
2268 // Top left of foreground must now be visible
2269 mCapture->expectFGColor(64, 64);
2270 // But 10 pixels in we should see the child surface
2271 mCapture->expectChildColor(74, 74);
2272 // And 10 more pixels we should be back to the foreground surface
2273 mCapture->expectFGColor(84, 84);
2274 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002275
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002276 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002277
chaviw06178942017-07-27 10:25:59 -07002278 {
2279 ScreenCapture::captureScreen(&mCapture);
2280 mCapture->expectFGColor(64, 64);
2281 // In reparenting we should have exposed the entire foreground surface.
2282 mCapture->expectFGColor(74, 74);
2283 // And the child layer should now begin at 10, 10 (since the BG
2284 // layer is at (0, 0)).
2285 mCapture->expectBGColor(9, 9);
2286 mCapture->expectChildColor(10, 10);
2287 }
2288}
2289
chaviwf1961f72017-09-18 16:41:07 -07002290TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002291 asTransaction([&](Transaction& t) {
2292 t.show(mChild);
2293 t.setPosition(mChild, 10, 10);
2294 t.setPosition(mFGSurfaceControl, 64, 64);
2295 });
chaviwf1961f72017-09-18 16:41:07 -07002296
2297 {
2298 ScreenCapture::captureScreen(&mCapture);
2299 // Top left of foreground must now be visible
2300 mCapture->expectFGColor(64, 64);
2301 // But 10 pixels in we should see the child surface
2302 mCapture->expectChildColor(74, 74);
2303 // And 10 more pixels we should be back to the foreground surface
2304 mCapture->expectFGColor(84, 84);
2305 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002306 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002307 {
2308 ScreenCapture::captureScreen(&mCapture);
2309 // Nothing should have changed.
2310 mCapture->expectFGColor(64, 64);
2311 mCapture->expectChildColor(74, 74);
2312 mCapture->expectFGColor(84, 84);
2313 }
2314}
2315
2316TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002317 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2318 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002319 ASSERT_TRUE(newSurface != NULL);
2320 ASSERT_TRUE(newSurface->isValid());
2321
2322 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002323 asTransaction([&](Transaction& t) {
2324 t.hide(mChild);
2325 t.show(newSurface);
2326 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002327 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002328 t.setPosition(mFGSurfaceControl, 64, 64);
2329 });
chaviwf1961f72017-09-18 16:41:07 -07002330
2331 {
2332 ScreenCapture::captureScreen(&mCapture);
2333 // Top left of foreground must now be visible
2334 mCapture->expectFGColor(64, 64);
2335 // At 10, 10 we should see the new surface
2336 mCapture->checkPixel(10, 10, 63, 195, 63);
2337 }
2338
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002339 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002340
2341 {
2342 ScreenCapture::captureScreen(&mCapture);
2343 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2344 // mFGSurface, putting it at 74, 74.
2345 mCapture->expectFGColor(64, 64);
2346 mCapture->checkPixel(74, 74, 63, 195, 63);
2347 mCapture->expectFGColor(84, 84);
2348 }
2349}
2350
chaviwc9674332017-08-28 12:32:18 -07002351TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002352 sp<SurfaceControl> grandchild =
2353 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2354 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002355 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2356
2357 {
2358 ScreenCapture::captureScreen(&mCapture);
2359 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2360 // which begins at 64, 64
2361 mCapture->checkPixel(64, 64, 50, 50, 50);
2362 }
2363}
2364
Robert Carr503c7042017-09-27 15:06:08 -07002365TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002366 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2367 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002368 fillSurfaceRGBA8(relative, 255, 255, 255);
2369
2370 Transaction t;
2371 t.setLayer(relative, INT32_MAX)
2372 .setRelativeLayer(mChild, relative->getHandle(), 1)
2373 .setPosition(mFGSurfaceControl, 0, 0)
2374 .apply(true);
2375
2376 // We expect that the child should have been elevated above our
2377 // INT_MAX layer even though it's not a child of it.
2378 {
2379 ScreenCapture::captureScreen(&mCapture);
2380 mCapture->expectChildColor(0, 0);
2381 mCapture->expectChildColor(9, 9);
2382 mCapture->checkPixel(10, 10, 255, 255, 255);
2383 }
2384}
2385
chaviw13fdc492017-06-27 12:40:18 -07002386class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002387protected:
chaviw13fdc492017-06-27 12:40:18 -07002388 void SetUp() override {
2389 LayerUpdateTest::SetUp();
2390
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002391 mLayerColorControl =
2392 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2393 PIXEL_FORMAT_RGBA_8888,
2394 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002395
2396 ASSERT_TRUE(mLayerColorControl != NULL);
2397 ASSERT_TRUE(mLayerColorControl->isValid());
2398
Robert Carr4cdc58f2017-08-23 14:22:20 -07002399 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002400 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002401 t.setPosition(mLayerColorControl, 140, 140);
2402 t.hide(mLayerColorControl);
2403 t.hide(mFGSurfaceControl);
2404 });
chaviw13fdc492017-06-27 12:40:18 -07002405 }
2406
2407 void TearDown() override {
2408 LayerUpdateTest::TearDown();
2409 mLayerColorControl = 0;
2410 }
2411
2412 sp<SurfaceControl> mLayerColorControl;
2413};
2414
2415TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2416 sp<ScreenCapture> sc;
2417
2418 {
2419 SCOPED_TRACE("before setColor");
2420 ScreenCapture::captureScreen(&sc);
2421 sc->expectBGColor(145, 145);
2422 }
2423
Robert Carr4cdc58f2017-08-23 14:22:20 -07002424 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002425 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002426 t.setColor(mLayerColorControl, color);
2427 t.show(mLayerColorControl);
2428 });
chaviw13fdc492017-06-27 12:40:18 -07002429
chaviw13fdc492017-06-27 12:40:18 -07002430 {
2431 // There should now be a color
2432 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002433
chaviw13fdc492017-06-27 12:40:18 -07002434 ScreenCapture::captureScreen(&sc);
2435 sc->checkPixel(145, 145, 43, 207, 131);
2436 }
2437}
2438
2439TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2440 sp<ScreenCapture> sc;
2441 {
2442 SCOPED_TRACE("before setColor");
2443 ScreenCapture::captureScreen(&sc);
2444 sc->expectBGColor(145, 145);
2445 }
2446
Robert Carr4cdc58f2017-08-23 14:22:20 -07002447 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002448 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002449 t.setColor(mLayerColorControl, color);
2450 t.setAlpha(mLayerColorControl, .75f);
2451 t.show(mLayerColorControl);
2452 });
2453
chaviw13fdc492017-06-27 12:40:18 -07002454 {
2455 // There should now be a color with .75 alpha
2456 SCOPED_TRACE("after setColor");
2457 ScreenCapture::captureScreen(&sc);
2458 sc->checkPixel(145, 145, 48, 171, 147);
2459 }
2460}
2461
2462TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2463 sp<ScreenCapture> sc;
2464 {
2465 SCOPED_TRACE("before setColor");
2466 ScreenCapture::captureScreen(&sc);
2467 sc->expectBGColor(145, 145);
2468 }
2469
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002470 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002471
chaviw13fdc492017-06-27 12:40:18 -07002472 {
2473 // There should now be set to 0,0,0 (black) as default.
2474 SCOPED_TRACE("after setColor");
2475 ScreenCapture::captureScreen(&sc);
2476 sc->checkPixel(145, 145, 0, 0, 0);
2477 }
2478}
2479
chaviwa76b2712017-09-20 12:02:26 -07002480class ScreenCaptureTest : public LayerUpdateTest {
2481protected:
2482 std::unique_ptr<CaptureLayer> mCapture;
2483};
2484
2485TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2486 auto bgHandle = mBGSurfaceControl->getHandle();
2487 CaptureLayer::captureScreen(&mCapture, bgHandle);
2488 mCapture->expectBGColor(0, 0);
2489 // Doesn't capture FG layer which is at 64, 64
2490 mCapture->expectBGColor(64, 64);
2491}
2492
2493TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2494 auto fgHandle = mFGSurfaceControl->getHandle();
2495
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002496 sp<SurfaceControl> child =
2497 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2498 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002499 fillSurfaceRGBA8(child, 200, 200, 200);
2500
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002501 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002502
2503 // Captures mFGSurfaceControl layer and its child.
2504 CaptureLayer::captureScreen(&mCapture, fgHandle);
2505 mCapture->expectFGColor(10, 10);
2506 mCapture->expectChildColor(0, 0);
2507}
2508
2509TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2510 auto fgHandle = mFGSurfaceControl->getHandle();
2511
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002512 sp<SurfaceControl> child =
2513 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2514 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002515 fillSurfaceRGBA8(child, 200, 200, 200);
2516
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002517 sp<SurfaceControl> grandchild =
2518 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2519 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002520
2521 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2522 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002523 .show(child)
2524 .setPosition(grandchild, 5, 5)
2525 .show(grandchild)
2526 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002527
2528 // Captures mFGSurfaceControl, its child, and the grandchild.
2529 CaptureLayer::captureScreen(&mCapture, fgHandle);
2530 mCapture->expectFGColor(10, 10);
2531 mCapture->expectChildColor(0, 0);
2532 mCapture->checkPixel(5, 5, 50, 50, 50);
2533}
2534
2535TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002536 sp<SurfaceControl> child =
2537 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2538 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002539 fillSurfaceRGBA8(child, 200, 200, 200);
2540 auto childHandle = child->getHandle();
2541
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002542 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002543
2544 // Captures only the child layer, and not the parent.
2545 CaptureLayer::captureScreen(&mCapture, childHandle);
2546 mCapture->expectChildColor(0, 0);
2547 mCapture->expectChildColor(9, 9);
2548}
2549
2550TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002551 sp<SurfaceControl> child =
2552 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2553 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002554 fillSurfaceRGBA8(child, 200, 200, 200);
2555 auto childHandle = child->getHandle();
2556
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002557 sp<SurfaceControl> grandchild =
2558 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2559 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002560 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2561
2562 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002563 .show(child)
2564 .setPosition(grandchild, 5, 5)
2565 .show(grandchild)
2566 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002567
2568 auto grandchildHandle = grandchild->getHandle();
2569
2570 // Captures only the grandchild.
2571 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2572 mCapture->checkPixel(0, 0, 50, 50, 50);
2573 mCapture->checkPixel(4, 4, 50, 50, 50);
2574}
2575
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002576} // namespace android