blob: ff81dc9461452453ebf05313d83ffcab52562e03 [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.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000107void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700108 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
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000114 int32_t bufferWidth = int32_t(outBuffer->getWidth());
115 int32_t bufferHeight = int32_t(outBuffer->getHeight());
116 if (x + width > bufferWidth) {
117 x = std::min(x, bufferWidth);
118 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700119 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000120 if (y + height > bufferHeight) {
121 y = std::min(y, bufferHeight);
122 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700123 }
124
125 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
126 uint8_t tmp = a >= b ? a - b : b - a;
127 return tmp <= tolerance;
128 };
129 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000130 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700131 for (int32_t i = 0; i < width; i++) {
132 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
133 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
134 << "pixel @ (" << x + i << ", " << y + j << "): "
135 << "expected (" << color << "), "
136 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
137 src += 4;
138 }
139 }
140}
141
142} // anonymous namespace
143
Robert Carr4cdc58f2017-08-23 14:22:20 -0700144using Transaction = SurfaceComposerClient::Transaction;
145
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700146// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700147static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
148 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800149 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700150 sp<Surface> s = sc->getSurface();
151 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800152 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
153 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700154 for (int y = 0; y < outBuffer.height; y++) {
155 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700156 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700157 pixel[0] = r;
158 pixel[1] = g;
159 pixel[2] = b;
160 pixel[3] = 255;
161 }
162 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700163 if (unlock) {
164 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
165 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700166}
167
168// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
169// individual pixel values for testing purposes.
170class ScreenCapture : public RefBase {
171public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700172 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
173 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700174 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700175 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700176 SurfaceComposerClient::Transaction().apply(true);
177
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000178 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700179 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000180 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, minLayerZ, maxLayerZ,
181 false));
182 *sc = new ScreenCapture(outBuffer);
183 }
184
185 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
186 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
187 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
188 SurfaceComposerClient::Transaction().apply(true);
189
190 sp<GraphicBuffer> outBuffer;
191 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
192 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700193 }
194
Chia-I Wu718daf82017-10-20 11:57:17 -0700195 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000196 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
197 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700198 }
199
200 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000201 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700202 const bool leftBorder = rect.left > 0;
203 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000204 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
205 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700206
207 if (topBorder) {
208 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
209 if (leftBorder) {
210 top.left -= 1;
211 }
212 if (rightBorder) {
213 top.right += 1;
214 }
215 expectColor(top, color, tolerance);
216 }
217 if (leftBorder) {
218 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
219 expectColor(left, color, tolerance);
220 }
221 if (rightBorder) {
222 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
223 expectColor(right, color, tolerance);
224 }
225 if (bottomBorder) {
226 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
227 if (leftBorder) {
228 bottom.left -= 1;
229 }
230 if (rightBorder) {
231 bottom.right += 1;
232 }
233 expectColor(bottom, color, tolerance);
234 }
235 }
236
Chia-I Wu93853fe2017-11-02 08:30:27 -0700237 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
238 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
239 uint8_t tolerance = 0) {
240 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
241
242 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
243 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
244 // avoid checking borders due to unspecified filtering behavior
245 const int32_t offsetX = filtered ? 2 : 0;
246 const int32_t offsetY = filtered ? 2 : 0;
247 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
248 tolerance);
249 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
250 tolerance);
251 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
252 tolerance);
253 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
254 bottomRight, tolerance);
255 }
256
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700257 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000258 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
259 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700260 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
261 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700262 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
263 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700264 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700265 }
266 }
267
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700268 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700269
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700270 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700271
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700272 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700273
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000274 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
275 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700276 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700277
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000278 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700279
280private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000281 sp<GraphicBuffer> mOutBuffer;
282 uint8_t* mPixels = NULL;
chaviwa76b2712017-09-20 12:02:26 -0700283};
284
Chia-I Wu718daf82017-10-20 11:57:17 -0700285class LayerTransactionTest : public ::testing::Test {
286protected:
287 void SetUp() override {
288 mClient = new SurfaceComposerClient;
289 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
290
291 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
292 }
293
294 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
295 uint32_t flags = 0) {
296 auto layer =
297 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
298 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
299
300 status_t error = Transaction()
301 .setLayerStack(layer, mDisplayLayerStack)
302 .setLayer(layer, mLayerZBase)
303 .apply();
304 if (error != NO_ERROR) {
305 ADD_FAILURE() << "failed to initialize SurfaceControl";
306 layer.clear();
307 }
308
309 return layer;
310 }
311
312 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
313 // wait for previous transactions (such as setSize) to complete
314 Transaction().apply(true);
315
316 ANativeWindow_Buffer buffer = {};
317 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
318
319 return buffer;
320 }
321
322 void postLayerBuffer(const sp<SurfaceControl>& layer) {
323 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
324
325 // wait for the newly posted buffer to be latched
326 waitForLayerBuffers();
327 }
328
329 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
330 ANativeWindow_Buffer buffer;
331 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
332 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
333 postLayerBuffer(layer);
334 }
335
Chia-I Wu93853fe2017-11-02 08:30:27 -0700336 void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft,
337 const Color& topRight, const Color& bottomLeft,
338 const Color& bottomRight) {
339 ANativeWindow_Buffer buffer;
340 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
341 ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0);
342
343 const int32_t halfW = buffer.width / 2;
344 const int32_t halfH = buffer.height / 2;
345 fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
346 fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight);
347 fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft);
348 fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight);
349
350 postLayerBuffer(layer);
351 }
352
Chia-I Wu718daf82017-10-20 11:57:17 -0700353 sp<ScreenCapture> screenshot() {
354 sp<ScreenCapture> screenshot;
355 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
356 return screenshot;
357 }
358
359 sp<SurfaceComposerClient> mClient;
360
361 sp<IBinder> mDisplay;
362 uint32_t mDisplayWidth;
363 uint32_t mDisplayHeight;
364 uint32_t mDisplayLayerStack;
365
366 // leave room for ~256 layers
367 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
368
369private:
370 void SetUpDisplay() {
371 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
372 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
373
374 // get display width/height
375 DisplayInfo info;
376 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
377 mDisplayWidth = info.w;
378 mDisplayHeight = info.h;
379
380 // After a new buffer is queued, SurfaceFlinger is notified and will
381 // latch the new buffer on next vsync. Let's heuristically wait for 3
382 // vsyncs.
383 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
384
385 mDisplayLayerStack = 0;
386 // set layer stack (b/68888219)
387 Transaction t;
388 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
389 t.apply();
390 }
391
392 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
393
394 int32_t mBufferPostDelay;
395};
396
397TEST_F(LayerTransactionTest, SetPositionBasic) {
398 sp<SurfaceControl> layer;
399 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
400 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
401
402 {
403 SCOPED_TRACE("default position");
404 auto shot = screenshot();
405 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
406 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
407 }
408
409 Transaction().setPosition(layer, 5, 10).apply();
410 {
411 SCOPED_TRACE("new position");
412 auto shot = screenshot();
413 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
414 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
415 }
416}
417
418TEST_F(LayerTransactionTest, SetPositionRounding) {
419 sp<SurfaceControl> layer;
420 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
421 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
422
423 // GLES requires only 4 bits of subpixel precision during rasterization
424 // XXX GLES composition does not match HWC composition due to precision
425 // loss (b/69315223)
426 const float epsilon = 1.0f / 16.0f;
427 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
428 {
429 SCOPED_TRACE("rounding down");
430 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
431 }
432
433 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
434 {
435 SCOPED_TRACE("rounding up");
436 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
437 }
438}
439
440TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
441 sp<SurfaceControl> layer;
442 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
443 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
444
445 Transaction().setPosition(layer, -32, -32).apply();
446 {
447 SCOPED_TRACE("negative coordinates");
448 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
449 }
450
451 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
452 {
453 SCOPED_TRACE("positive coordinates");
454 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
455 }
456}
457
458TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
459 sp<SurfaceControl> layer;
460 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
461 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
462
463 // partially out of bounds
464 Transaction().setPosition(layer, -30, -30).apply();
465 {
466 SCOPED_TRACE("negative coordinates");
467 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
468 }
469
470 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
471 {
472 SCOPED_TRACE("positive coordinates");
473 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
474 mDisplayHeight),
475 Color::RED);
476 }
477}
478
479TEST_F(LayerTransactionTest, SetPositionWithResize) {
480 sp<SurfaceControl> layer;
481 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
482 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
483
484 // setPosition is applied immediately by default, with or without resize
485 // pending
486 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
487 {
488 SCOPED_TRACE("resize pending");
489 auto shot = screenshot();
490 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
491 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
492 }
493
494 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
495 {
496 SCOPED_TRACE("resize applied");
497 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
498 }
499}
500
501TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
502 sp<SurfaceControl> layer;
503 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
504 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
505
506 // request setPosition to be applied with the next resize
507 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
508 {
509 SCOPED_TRACE("new position pending");
510 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
511 }
512
513 Transaction().setPosition(layer, 15, 20).apply();
514 {
515 SCOPED_TRACE("pending new position modified");
516 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
517 }
518
519 Transaction().setSize(layer, 64, 64).apply();
520 {
521 SCOPED_TRACE("resize pending");
522 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
523 }
524
525 // finally resize and latch the buffer
526 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
527 {
528 SCOPED_TRACE("new position applied");
529 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
530 }
531}
532
533TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
534 sp<SurfaceControl> layer;
535 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
536 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
537
538 // setPosition is not immediate even with SCALE_TO_WINDOW override
539 Transaction()
540 .setPosition(layer, 5, 10)
541 .setSize(layer, 64, 64)
542 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
543 .setGeometryAppliesWithResize(layer)
544 .apply();
545 {
546 SCOPED_TRACE("new position pending");
547 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
548 }
549
550 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
551 {
552 SCOPED_TRACE("new position applied");
553 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
554 }
555}
556
Chia-I Wu0eaea312017-10-31 10:14:40 -0700557TEST_F(LayerTransactionTest, SetSizeBasic) {
558 sp<SurfaceControl> layer;
559 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
560 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
561
562 Transaction().setSize(layer, 64, 64).apply();
563 {
564 SCOPED_TRACE("resize pending");
565 auto shot = screenshot();
566 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
567 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
568 }
569
570 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
571 {
572 SCOPED_TRACE("resize applied");
573 auto shot = screenshot();
574 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
575 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
576 }
577}
578
579TEST_F(LayerTransactionTest, SetSizeInvalid) {
580 // cannot test robustness against invalid sizes (zero or really huge)
581}
582
583TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
584 sp<SurfaceControl> layer;
585 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
586 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
587
588 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
589 Transaction()
590 .setSize(layer, 64, 64)
591 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
592 .apply();
593 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
594}
595
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700596TEST_F(LayerTransactionTest, SetZBasic) {
597 sp<SurfaceControl> layerR;
598 sp<SurfaceControl> layerG;
599 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
600 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
601 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
602 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
603
604 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
605 {
606 SCOPED_TRACE("layerR");
607 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
608 }
609
610 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
611 {
612 SCOPED_TRACE("layerG");
613 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
614 }
615}
616
617TEST_F(LayerTransactionTest, SetZNegative) {
618 sp<SurfaceControl> layerR;
619 sp<SurfaceControl> layerG;
620 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
621 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
622 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
623 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
624
625 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
626 {
627 SCOPED_TRACE("layerR");
628 sp<ScreenCapture> screenshot;
629 ScreenCapture::captureScreen(&screenshot, -2, -1);
630 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
631 }
632
633 Transaction().setLayer(layerR, -3).apply();
634 {
635 SCOPED_TRACE("layerG");
636 sp<ScreenCapture> screenshot;
637 ScreenCapture::captureScreen(&screenshot, -3, -1);
638 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
639 }
640}
641
Chia-I Wu49313302017-10-31 10:14:40 -0700642TEST_F(LayerTransactionTest, SetRelativeZBasic) {
643 sp<SurfaceControl> layerR;
644 sp<SurfaceControl> layerG;
645 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
646 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
647 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
648 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
649
650 Transaction()
651 .setPosition(layerG, 16, 16)
652 .setRelativeLayer(layerG, layerR->getHandle(), 1)
653 .apply();
654 {
655 SCOPED_TRACE("layerG above");
656 auto shot = screenshot();
657 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
658 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
659 }
660
661 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
662 {
663 SCOPED_TRACE("layerG below");
664 auto shot = screenshot();
665 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
666 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
667 }
668}
669
Chia-I Wuec2d9852017-11-21 09:21:01 -0800670TEST_F(LayerTransactionTest, SetRelativeZNegative) {
671 sp<SurfaceControl> layerR;
672 sp<SurfaceControl> layerG;
673 sp<SurfaceControl> layerB;
674 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
675 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
676 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
677 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
678 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
679 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
680
681 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
682 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
683
684 sp<ScreenCapture> screenshot;
685 // only layerB is in this range
686 ScreenCapture::captureScreen(&screenshot, -2, -1);
687 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
688}
689
Chia-I Wu49313302017-10-31 10:14:40 -0700690TEST_F(LayerTransactionTest, SetRelativeZGroup) {
691 sp<SurfaceControl> layerR;
692 sp<SurfaceControl> layerG;
693 sp<SurfaceControl> layerB;
694 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
695 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
696 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
697 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
698 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
699 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
700
701 // layerR = 0, layerG = layerR + 3, layerB = 2
702 Transaction()
703 .setPosition(layerG, 8, 8)
704 .setRelativeLayer(layerG, layerR->getHandle(), 3)
705 .setPosition(layerB, 16, 16)
706 .setLayer(layerB, mLayerZBase + 2)
707 .apply();
708 {
709 SCOPED_TRACE("(layerR < layerG) < layerB");
710 auto shot = screenshot();
711 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
712 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
713 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
714 }
715
716 // layerR = 4, layerG = layerR + 3, layerB = 2
717 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
718 {
719 SCOPED_TRACE("layerB < (layerR < layerG)");
720 auto shot = screenshot();
721 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
722 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
723 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
724 }
725
726 // layerR = 4, layerG = layerR - 3, layerB = 2
727 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
728 {
729 SCOPED_TRACE("layerB < (layerG < layerR)");
730 auto shot = screenshot();
731 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
732 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
733 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
734 }
735
736 // restore to absolute z
737 // layerR = 4, layerG = 0, layerB = 2
738 Transaction().setLayer(layerG, mLayerZBase).apply();
739 {
740 SCOPED_TRACE("layerG < layerB < layerR");
741 auto shot = screenshot();
742 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
743 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
744 }
745
746 // layerR should not affect layerG anymore
747 // layerR = 1, layerG = 0, layerB = 2
748 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
749 {
750 SCOPED_TRACE("layerG < layerR < layerB");
751 auto shot = screenshot();
752 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
753 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
754 }
755}
756
757TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
758 sp<SurfaceControl> layerR;
759 sp<SurfaceControl> layerG;
760
761 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
762 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
763 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
764 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
765
766 Transaction()
767 .setPosition(layerG, 16, 16)
768 .setRelativeLayer(layerG, layerR->getHandle(), 1)
769 .apply();
770
771 mClient->destroySurface(layerG->getHandle());
772 // layerG should have been removed
773 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
774}
775
Chia-I Wu57b27502017-10-31 10:14:40 -0700776TEST_F(LayerTransactionTest, SetFlagsHidden) {
777 sp<SurfaceControl> layer;
778 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
779 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
780
781 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
782 {
783 SCOPED_TRACE("layer hidden");
784 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
785 }
786
787 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
788 {
789 SCOPED_TRACE("layer shown");
790 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
791 }
792}
793
794TEST_F(LayerTransactionTest, SetFlagsOpaque) {
795 const Color translucentRed = {100, 0, 0, 100};
796 sp<SurfaceControl> layerR;
797 sp<SurfaceControl> layerG;
798 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
799 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
800 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
801 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
802
803 Transaction()
804 .setLayer(layerR, mLayerZBase + 1)
805 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
806 .apply();
807 {
808 SCOPED_TRACE("layerR opaque");
809 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
810 }
811
812 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
813 {
814 SCOPED_TRACE("layerR translucent");
815 const uint8_t g = uint8_t(255 - translucentRed.a);
816 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
817 }
818}
819
820TEST_F(LayerTransactionTest, SetFlagsSecure) {
821 sp<SurfaceControl> layer;
822 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
823 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
824
825 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000826 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -0700827 Transaction()
828 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
829 .apply(true);
830 ASSERT_EQ(PERMISSION_DENIED,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000831 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700832 false));
833
834 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
835 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000836 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700837 false));
838}
839
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700840TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
841 const Rect top(0, 0, 32, 16);
842 const Rect bottom(0, 16, 32, 32);
843 sp<SurfaceControl> layer;
844 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
845
846 ANativeWindow_Buffer buffer;
847 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
848 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
849 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
850 // setTransparentRegionHint always applies to the following buffer
851 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
852 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
853 {
854 SCOPED_TRACE("top transparent");
855 auto shot = screenshot();
856 shot->expectColor(top, Color::BLACK);
857 shot->expectColor(bottom, Color::RED);
858 }
859
860 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
861 {
862 SCOPED_TRACE("transparent region hint pending");
863 auto shot = screenshot();
864 shot->expectColor(top, Color::BLACK);
865 shot->expectColor(bottom, Color::RED);
866 }
867
868 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
869 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
870 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
871 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
872 {
873 SCOPED_TRACE("bottom transparent");
874 auto shot = screenshot();
875 shot->expectColor(top, Color::RED);
876 shot->expectColor(bottom, Color::BLACK);
877 }
878}
879
880TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
881 sp<SurfaceControl> layerTransparent;
882 sp<SurfaceControl> layerR;
883 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
884 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
885
886 // check that transparent region hint is bound by the layer size
887 Transaction()
888 .setTransparentRegionHint(layerTransparent,
889 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
890 .setPosition(layerR, 16, 16)
891 .setLayer(layerR, mLayerZBase + 1)
892 .apply();
893 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
894 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
895 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
896}
897
Chia-I Wua8a515e2017-11-01 15:16:35 -0700898TEST_F(LayerTransactionTest, SetAlphaBasic) {
899 sp<SurfaceControl> layer1;
900 sp<SurfaceControl> layer2;
901 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
902 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
903 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
904 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
905
906 Transaction()
907 .setAlpha(layer1, 0.25f)
908 .setAlpha(layer2, 0.75f)
909 .setPosition(layer2, 16, 0)
910 .setLayer(layer2, mLayerZBase + 1)
911 .apply();
912 {
913 auto shot = screenshot();
914 uint8_t r = 16; // 64 * 0.25f
915 uint8_t g = 48; // 64 * 0.75f
916 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
917 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
918
919 r /= 4; // r * (1.0f - 0.75f)
920 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
921 }
922}
923
924TEST_F(LayerTransactionTest, SetAlphaClamped) {
925 const Color color = {64, 0, 0, 255};
926 sp<SurfaceControl> layer;
927 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
928 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
929
930 Transaction().setAlpha(layer, 2.0f).apply();
931 {
932 SCOPED_TRACE("clamped to 1.0f");
933 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
934 }
935
936 Transaction().setAlpha(layer, -1.0f).apply();
937 {
938 SCOPED_TRACE("clamped to 0.0f");
939 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
940 }
941}
942
Chia-I Wue4ef6102017-11-01 15:16:35 -0700943TEST_F(LayerTransactionTest, SetColorBasic) {
944 sp<SurfaceControl> bufferLayer;
945 sp<SurfaceControl> colorLayer;
946 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
947 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
948 ASSERT_NO_FATAL_FAILURE(
949 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
950
951 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
952 {
953 SCOPED_TRACE("default color");
954 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
955 }
956
957 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
958 const Color expected = {15, 51, 85, 255};
959 // this is handwavy, but the precison loss scaled by 255 (8-bit per
960 // channel) should be less than one
961 const uint8_t tolerance = 1;
962 Transaction().setColor(colorLayer, color).apply();
963 {
964 SCOPED_TRACE("new color");
965 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
966 }
967}
968
969TEST_F(LayerTransactionTest, SetColorClamped) {
970 sp<SurfaceControl> colorLayer;
971 ASSERT_NO_FATAL_FAILURE(
972 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
973
974 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
975 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
976}
977
978TEST_F(LayerTransactionTest, SetColorWithAlpha) {
979 sp<SurfaceControl> bufferLayer;
980 sp<SurfaceControl> colorLayer;
981 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
982 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
983 ASSERT_NO_FATAL_FAILURE(
984 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
985
986 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
987 const float alpha = 0.25f;
988 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
989 // this is handwavy, but the precison loss scaled by 255 (8-bit per
990 // channel) should be less than one
991 const uint8_t tolerance = 1;
992 Transaction()
993 .setColor(colorLayer, color)
994 .setAlpha(colorLayer, alpha)
995 .setLayer(colorLayer, mLayerZBase + 1)
996 .apply();
997 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
998 tolerance);
999}
1000
1001TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1002 sp<SurfaceControl> bufferLayer;
1003 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1004 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1005
1006 // color is ignored
1007 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1008 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1009}
1010
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001011TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1012 sp<SurfaceControl> layer;
1013 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1014 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1015
1016 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1017 {
1018 SCOPED_TRACE("non-existing layer stack");
1019 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1020 }
1021
1022 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1023 {
1024 SCOPED_TRACE("original layer stack");
1025 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1026 }
1027}
1028
Chia-I Wu93853fe2017-11-02 08:30:27 -07001029TEST_F(LayerTransactionTest, SetMatrixBasic) {
1030 sp<SurfaceControl> layer;
1031 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1032 ASSERT_NO_FATAL_FAILURE(
1033 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1034
1035 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1036 {
1037 SCOPED_TRACE("IDENTITY");
1038 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1039 Color::WHITE);
1040 }
1041
1042 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1043 {
1044 SCOPED_TRACE("FLIP_H");
1045 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1046 Color::BLUE);
1047 }
1048
1049 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1050 {
1051 SCOPED_TRACE("FLIP_V");
1052 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1053 Color::GREEN);
1054 }
1055
1056 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1057 {
1058 SCOPED_TRACE("ROT_90");
1059 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1060 Color::GREEN);
1061 }
1062
1063 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1064 {
1065 SCOPED_TRACE("SCALE");
1066 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1067 Color::WHITE, true /* filtered */);
1068 }
1069}
1070
1071TEST_F(LayerTransactionTest, SetMatrixRot45) {
1072 sp<SurfaceControl> layer;
1073 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1074 ASSERT_NO_FATAL_FAILURE(
1075 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1076
1077 const float rot = M_SQRT1_2; // 45 degrees
1078 const float trans = M_SQRT2 * 16.0f;
1079 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1080
1081 auto shot = screenshot();
1082 // check a 8x8 region inside each color
1083 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1084 const int32_t halfL = 4;
1085 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1086 };
1087 const int32_t unit = int32_t(trans / 2);
1088 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1089 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1090 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1091 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1092}
1093
1094TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1095 sp<SurfaceControl> layer;
1096 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1097 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1098
1099 // setMatrix is applied after any pending resize, unlike setPosition
1100 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1101 {
1102 SCOPED_TRACE("resize pending");
1103 auto shot = screenshot();
1104 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1105 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1106 }
1107
1108 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1109 {
1110 SCOPED_TRACE("resize applied");
1111 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1112 }
1113}
1114
1115TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1116 sp<SurfaceControl> layer;
1117 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1118 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1119
1120 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1121 Transaction()
1122 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1123 .setSize(layer, 64, 64)
1124 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1125 .apply();
1126 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1127}
1128
Chia-I Wua56b2042017-11-01 15:16:35 -07001129TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1130 sp<SurfaceControl> layer;
1131 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1132 ASSERT_NO_FATAL_FAILURE(
1133 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1134
1135 // XXX SCALE_CROP is not respected; calling setSize and
1136 // setOverrideScalingMode in separate transactions does not work
1137 // (b/69315456)
1138 Transaction()
1139 .setSize(layer, 64, 16)
1140 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1141 .apply();
1142 {
1143 SCOPED_TRACE("SCALE_TO_WINDOW");
1144 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1145 Color::WHITE, true /* filtered */);
1146 }
1147}
1148
Chia-I Wu04dcca82017-11-02 08:30:27 -07001149TEST_F(LayerTransactionTest, SetCropBasic) {
1150 sp<SurfaceControl> layer;
1151 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1152 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1153 const Rect crop(8, 8, 24, 24);
1154
1155 Transaction().setCrop(layer, crop).apply();
1156 auto shot = screenshot();
1157 shot->expectColor(crop, Color::RED);
1158 shot->expectBorder(crop, Color::BLACK);
1159}
1160
1161TEST_F(LayerTransactionTest, SetCropEmpty) {
1162 sp<SurfaceControl> layer;
1163 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1164 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1165
1166 {
1167 SCOPED_TRACE("empty rect");
1168 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1169 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1170 }
1171
1172 {
1173 SCOPED_TRACE("negative rect");
1174 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1175 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1176 }
1177}
1178
1179TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1180 sp<SurfaceControl> layer;
1181 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1182 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1183
1184 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1185 auto shot = screenshot();
1186 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1187 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1188}
1189
1190TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1191 sp<SurfaceControl> layer;
1192 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1193 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1194
1195 const Point position(32, 32);
1196 const Rect crop(8, 8, 24, 24);
1197 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1198 auto shot = screenshot();
1199 shot->expectColor(crop + position, Color::RED);
1200 shot->expectBorder(crop + position, Color::BLACK);
1201}
1202
1203TEST_F(LayerTransactionTest, SetCropWithScale) {
1204 sp<SurfaceControl> layer;
1205 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1206 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1207
1208 // crop is affected by matrix
1209 Transaction()
1210 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1211 .setCrop(layer, Rect(8, 8, 24, 24))
1212 .apply();
1213 auto shot = screenshot();
1214 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1215 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1216}
1217
1218TEST_F(LayerTransactionTest, SetCropWithResize) {
1219 sp<SurfaceControl> layer;
1220 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1221 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1222
1223 // setCrop is applied immediately by default, with or without resize pending
1224 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1225 {
1226 SCOPED_TRACE("resize pending");
1227 auto shot = screenshot();
1228 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1229 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1230 }
1231
1232 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1233 {
1234 SCOPED_TRACE("resize applied");
1235 auto shot = screenshot();
1236 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1237 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1238 }
1239}
1240
1241TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1242 sp<SurfaceControl> layer;
1243 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1244 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1245
1246 // request setCrop to be applied with the next resize
1247 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1248 {
1249 SCOPED_TRACE("waiting for next resize");
1250 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1251 }
1252
1253 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1254 {
1255 SCOPED_TRACE("pending crop modified");
1256 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1257 }
1258
1259 Transaction().setSize(layer, 16, 16).apply();
1260 {
1261 SCOPED_TRACE("resize pending");
1262 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1263 }
1264
1265 // finally resize
1266 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1267 {
1268 SCOPED_TRACE("new crop applied");
1269 auto shot = screenshot();
1270 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1271 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1272 }
1273}
1274
1275TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1276 sp<SurfaceControl> layer;
1277 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1278 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1279
1280 // setCrop is not immediate even with SCALE_TO_WINDOW override
1281 Transaction()
1282 .setCrop(layer, Rect(4, 4, 12, 12))
1283 .setSize(layer, 16, 16)
1284 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1285 .setGeometryAppliesWithResize(layer)
1286 .apply();
1287 {
1288 SCOPED_TRACE("new crop pending");
1289 auto shot = screenshot();
1290 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1291 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1292 }
1293
1294 // XXX crop is never latched without other geometry change (b/69315677)
1295 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1296 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1297 Transaction().setPosition(layer, 0, 0).apply();
1298 {
1299 SCOPED_TRACE("new crop applied");
1300 auto shot = screenshot();
1301 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1302 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1303 }
1304}
1305
Chia-I Wucdd71a52017-11-02 08:30:27 -07001306TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1307 sp<SurfaceControl> layer;
1308 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1309 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1310 const Rect crop(8, 8, 24, 24);
1311
1312 // same as in SetCropBasic
1313 Transaction().setFinalCrop(layer, crop).apply();
1314 auto shot = screenshot();
1315 shot->expectColor(crop, Color::RED);
1316 shot->expectBorder(crop, Color::BLACK);
1317}
1318
1319TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1320 sp<SurfaceControl> layer;
1321 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1322 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1323
1324 // same as in SetCropEmpty
1325 {
1326 SCOPED_TRACE("empty rect");
1327 Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1328 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1329 }
1330
1331 {
1332 SCOPED_TRACE("negative rect");
1333 Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1334 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1335 }
1336}
1337
1338TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1339 sp<SurfaceControl> layer;
1340 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1341 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1342
1343 // same as in SetCropOutOfBounds
1344 Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1345 auto shot = screenshot();
1346 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1347 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1348}
1349
1350TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1351 sp<SurfaceControl> layer;
1352 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1353 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1354
1355 // final crop is applied post-translation
1356 Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1357 auto shot = screenshot();
1358 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1359 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1360}
1361
1362TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1363 sp<SurfaceControl> layer;
1364 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1365 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1366
1367 // final crop is not affected by matrix
1368 Transaction()
1369 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1370 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1371 .apply();
1372 auto shot = screenshot();
1373 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1374 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1375}
1376
1377TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1378 sp<SurfaceControl> layer;
1379 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1380 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1381
1382 // same as in SetCropWithResize
1383 Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1384 {
1385 SCOPED_TRACE("resize pending");
1386 auto shot = screenshot();
1387 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1388 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1389 }
1390
1391 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1392 {
1393 SCOPED_TRACE("resize applied");
1394 auto shot = screenshot();
1395 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1396 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1397 }
1398}
1399
1400TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1401 sp<SurfaceControl> layer;
1402 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1403 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1404
1405 // same as in SetCropWithNextResize
1406 Transaction()
1407 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1408 .setGeometryAppliesWithResize(layer)
1409 .apply();
1410 {
1411 SCOPED_TRACE("waiting for next resize");
1412 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1413 }
1414
1415 Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1416 {
1417 SCOPED_TRACE("pending final crop modified");
1418 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1419 }
1420
1421 Transaction().setSize(layer, 16, 16).apply();
1422 {
1423 SCOPED_TRACE("resize pending");
1424 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1425 }
1426
1427 // finally resize
1428 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1429 {
1430 SCOPED_TRACE("new final crop applied");
1431 auto shot = screenshot();
1432 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1433 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1434 }
1435}
1436
1437TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1438 sp<SurfaceControl> layer;
1439 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1440 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1441
1442 // same as in SetCropWithNextResizeScaleToWindow
1443 Transaction()
1444 .setFinalCrop(layer, Rect(4, 4, 12, 12))
1445 .setSize(layer, 16, 16)
1446 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1447 .setGeometryAppliesWithResize(layer)
1448 .apply();
1449 {
1450 SCOPED_TRACE("new final crop pending");
1451 auto shot = screenshot();
1452 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1453 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1454 }
1455
1456 // XXX final crop is never latched without other geometry change (b/69315677)
1457 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1458 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1459 Transaction().setPosition(layer, 0, 0).apply();
1460 {
1461 SCOPED_TRACE("new final crop applied");
1462 auto shot = screenshot();
1463 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1464 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1465 }
1466}
1467
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001468class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001469protected:
1470 virtual void SetUp() {
1471 mComposerClient = new SurfaceComposerClient;
1472 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1473
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001474 sp<IBinder> display(
1475 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001476 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001477 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001478
1479 ssize_t displayWidth = info.w;
1480 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001481
1482 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001483 mBGSurfaceControl =
1484 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1485 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001486 ASSERT_TRUE(mBGSurfaceControl != NULL);
1487 ASSERT_TRUE(mBGSurfaceControl->isValid());
1488 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1489
1490 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001491 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1492 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001493 ASSERT_TRUE(mFGSurfaceControl != NULL);
1494 ASSERT_TRUE(mFGSurfaceControl->isValid());
1495
1496 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1497
1498 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001499 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1500 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001501 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1502 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1503
1504 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1505
Robert Carr4cdc58f2017-08-23 14:22:20 -07001506 asTransaction([&](Transaction& t) {
1507 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001508
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001509 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001510
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001511 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1512 .setPosition(mFGSurfaceControl, 64, 64)
1513 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001514
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001515 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1516 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1517 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001518 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001519 }
1520
1521 virtual void TearDown() {
1522 mComposerClient->dispose();
1523 mBGSurfaceControl = 0;
1524 mFGSurfaceControl = 0;
1525 mSyncSurfaceControl = 0;
1526 mComposerClient = 0;
1527 }
1528
1529 void waitForPostedBuffers() {
1530 // Since the sync surface is in synchronous mode (i.e. double buffered)
1531 // posting three buffers to it should ensure that at least two
1532 // SurfaceFlinger::handlePageFlip calls have been made, which should
1533 // guaranteed that a buffer posted to another Surface has been retired.
1534 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1535 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1536 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1537 }
1538
Robert Carr4cdc58f2017-08-23 14:22:20 -07001539 void asTransaction(const std::function<void(Transaction&)>& exec) {
1540 Transaction t;
1541 exec(t);
1542 t.apply(true);
1543 }
1544
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001545 sp<SurfaceComposerClient> mComposerClient;
1546 sp<SurfaceControl> mBGSurfaceControl;
1547 sp<SurfaceControl> mFGSurfaceControl;
1548
1549 // This surface is used to ensure that the buffers posted to
1550 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1551 sp<SurfaceControl> mSyncSurfaceControl;
1552};
1553
Robert Carr7f619b22017-11-06 12:56:35 -08001554TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1555 sp<ScreenCapture> sc;
1556
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001557 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1558 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001559 fillSurfaceRGBA8(relative, 10, 10, 10);
1560 waitForPostedBuffers();
1561
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001562 Transaction{}
1563 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001564 .setPosition(relative, 64, 64)
1565 .apply();
1566
1567 {
1568 // The relative should be on top of the FG control.
1569 ScreenCapture::captureScreen(&sc);
1570 sc->checkPixel(64, 64, 10, 10, 10);
1571 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001572 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001573
1574 {
1575 // Nothing should change at this point.
1576 ScreenCapture::captureScreen(&sc);
1577 sc->checkPixel(64, 64, 10, 10, 10);
1578 }
1579
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001580 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001581
1582 {
1583 // Ensure that the relative was actually hidden, rather than
1584 // being left in the detached but visible state.
1585 ScreenCapture::captureScreen(&sc);
1586 sc->expectFGColor(64, 64);
1587 }
1588}
1589
Robert Carr8d5227b2017-03-16 15:41:03 -07001590class GeometryLatchingTest : public LayerUpdateTest {
1591protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001592 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001593 SCOPED_TRACE(trace);
1594 ScreenCapture::captureScreen(&sc);
1595 // We find the leading edge of the FG surface.
1596 sc->expectFGColor(127, 127);
1597 sc->expectBGColor(128, 128);
1598 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001599
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001600 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001601
1602 void unlockFGBuffer() {
1603 sp<Surface> s = mFGSurfaceControl->getSurface();
1604 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1605 waitForPostedBuffers();
1606 }
1607
Robert Carr8d5227b2017-03-16 15:41:03 -07001608 void completeFGResize() {
1609 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1610 waitForPostedBuffers();
1611 }
1612 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001613 asTransaction([&](Transaction& t) {
1614 t.setSize(mFGSurfaceControl, 64, 64);
1615 t.setPosition(mFGSurfaceControl, 64, 64);
1616 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1617 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1618 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001619
1620 EXPECT_INITIAL_STATE("After restoring initial state");
1621 }
1622 sp<ScreenCapture> sc;
1623};
1624
Robert Carr8d5227b2017-03-16 15:41:03 -07001625class CropLatchingTest : public GeometryLatchingTest {
1626protected:
1627 void EXPECT_CROPPED_STATE(const char* trace) {
1628 SCOPED_TRACE(trace);
1629 ScreenCapture::captureScreen(&sc);
1630 // The edge should be moved back one pixel by our crop.
1631 sc->expectFGColor(126, 126);
1632 sc->expectBGColor(127, 127);
1633 sc->expectBGColor(128, 128);
1634 }
chaviw59f5c562017-06-28 16:39:06 -07001635
1636 void EXPECT_RESIZE_STATE(const char* trace) {
1637 SCOPED_TRACE(trace);
1638 ScreenCapture::captureScreen(&sc);
1639 // The FG is now resized too 128,128 at 64,64
1640 sc->expectFGColor(64, 64);
1641 sc->expectFGColor(191, 191);
1642 sc->expectBGColor(192, 192);
1643 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001644};
1645
Robert Carr7bf247e2017-05-18 14:02:49 -07001646// In this test we ensure that setGeometryAppliesWithResize actually demands
1647// a buffer of the new size, and not just any size.
1648TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1649 EXPECT_INITIAL_STATE("before anything");
1650 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001651 asTransaction([&](Transaction& t) {
1652 t.setSize(mFGSurfaceControl, 128, 128);
1653 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1654 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001655
1656 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1657
1658 restoreInitialState();
1659
1660 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1661 // initiating the resize.
1662 lockAndFillFGBuffer();
1663
Robert Carr4cdc58f2017-08-23 14:22:20 -07001664 asTransaction([&](Transaction& t) {
1665 t.setSize(mFGSurfaceControl, 128, 128);
1666 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1667 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1668 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001669
1670 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1671
1672 // We now submit our old buffer, at the old size, and ensure it doesn't
1673 // trigger geometry latching.
1674 unlockFGBuffer();
1675
1676 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1677
1678 completeFGResize();
1679
1680 EXPECT_CROPPED_STATE("after the resize finishes");
1681}
1682
Pablo Ceballos05289c22016-04-14 15:49:55 -07001683TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1684 sp<ScreenCapture> sc;
1685 {
1686 SCOPED_TRACE("before anything");
1687 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001688 sc->expectBGColor(32, 32);
1689 sc->expectFGColor(96, 96);
1690 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001691 }
1692
1693 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001694 asTransaction([&](Transaction& t) {
1695 t.setAlpha(mFGSurfaceControl, 0.75);
1696 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001697 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001698 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001699
Robert Carr4cdc58f2017-08-23 14:22:20 -07001700 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001701 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001702 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001703 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001704 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001705
1706 {
1707 SCOPED_TRACE("before any trigger");
1708 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001709 sc->expectBGColor(32, 32);
1710 sc->expectFGColor(96, 96);
1711 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001712 }
1713
1714 // should trigger the first deferred transaction, but not the second one
1715 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1716 {
1717 SCOPED_TRACE("after first trigger");
1718 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001719 sc->expectBGColor(32, 32);
1720 sc->checkPixel(96, 96, 162, 63, 96);
1721 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001722 }
1723
1724 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001725 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001726
1727 // trigger the second deferred transaction
1728 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1729 {
1730 SCOPED_TRACE("after second trigger");
1731 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001732 sc->expectBGColor(32, 32);
1733 sc->expectBGColor(96, 96);
1734 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001735 }
1736}
1737
Robert Carre392b552017-09-19 12:16:05 -07001738TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1739 sp<ScreenCapture> sc;
1740
1741 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001742 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1743 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1744 sp<SurfaceControl> childBuffer =
1745 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1746 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001747 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1748
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001749 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001750
1751 {
1752 ScreenCapture::captureScreen(&sc);
1753 sc->expectChildColor(73, 73);
1754 sc->expectFGColor(74, 74);
1755 }
1756
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001757 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001758
1759 {
1760 ScreenCapture::captureScreen(&sc);
1761 sc->expectChildColor(73, 73);
1762 sc->expectChildColor(74, 74);
1763 }
1764}
1765
Robert Carr2c5f6d22017-09-26 12:30:35 -07001766TEST_F(LayerUpdateTest, MergingTransactions) {
1767 sp<ScreenCapture> sc;
1768 {
1769 SCOPED_TRACE("before move");
1770 ScreenCapture::captureScreen(&sc);
1771 sc->expectBGColor(0, 12);
1772 sc->expectFGColor(75, 75);
1773 sc->expectBGColor(145, 145);
1774 }
1775
1776 Transaction t1, t2;
1777 t1.setPosition(mFGSurfaceControl, 128, 128);
1778 t2.setPosition(mFGSurfaceControl, 0, 0);
1779 // We expect that the position update from t2 now
1780 // overwrites the position update from t1.
1781 t1.merge(std::move(t2));
1782 t1.apply();
1783
1784 {
1785 ScreenCapture::captureScreen(&sc);
1786 sc->expectFGColor(1, 1);
1787 }
1788}
1789
Robert Carr1f0a16a2016-10-24 16:27:39 -07001790class ChildLayerTest : public LayerUpdateTest {
1791protected:
1792 void SetUp() override {
1793 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001794 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1795 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001796 fillSurfaceRGBA8(mChild, 200, 200, 200);
1797
1798 {
1799 SCOPED_TRACE("before anything");
1800 ScreenCapture::captureScreen(&mCapture);
1801 mCapture->expectChildColor(64, 64);
1802 }
1803 }
1804 void TearDown() override {
1805 LayerUpdateTest::TearDown();
1806 mChild = 0;
1807 }
1808
1809 sp<SurfaceControl> mChild;
1810 sp<ScreenCapture> mCapture;
1811};
1812
1813TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001814 asTransaction([&](Transaction& t) {
1815 t.show(mChild);
1816 t.setPosition(mChild, 10, 10);
1817 t.setPosition(mFGSurfaceControl, 64, 64);
1818 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001819
1820 {
1821 ScreenCapture::captureScreen(&mCapture);
1822 // Top left of foreground must now be visible
1823 mCapture->expectFGColor(64, 64);
1824 // But 10 pixels in we should see the child surface
1825 mCapture->expectChildColor(74, 74);
1826 // And 10 more pixels we should be back to the foreground surface
1827 mCapture->expectFGColor(84, 84);
1828 }
1829
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001830 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001831
1832 {
1833 ScreenCapture::captureScreen(&mCapture);
1834 // Top left of foreground should now be at 0, 0
1835 mCapture->expectFGColor(0, 0);
1836 // But 10 pixels in we should see the child surface
1837 mCapture->expectChildColor(10, 10);
1838 // And 10 more pixels we should be back to the foreground surface
1839 mCapture->expectFGColor(20, 20);
1840 }
1841}
1842
Robert Carr41b08b52017-06-01 16:11:34 -07001843TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001844 asTransaction([&](Transaction& t) {
1845 t.show(mChild);
1846 t.setPosition(mChild, 0, 0);
1847 t.setPosition(mFGSurfaceControl, 0, 0);
1848 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1849 });
Robert Carr41b08b52017-06-01 16:11:34 -07001850
1851 {
1852 ScreenCapture::captureScreen(&mCapture);
1853 mCapture->expectChildColor(0, 0);
1854 mCapture->expectChildColor(4, 4);
1855 mCapture->expectBGColor(5, 5);
1856 }
1857}
1858
1859TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001860 asTransaction([&](Transaction& t) {
1861 t.show(mChild);
1862 t.setPosition(mChild, 0, 0);
1863 t.setPosition(mFGSurfaceControl, 0, 0);
1864 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1865 });
Robert Carr41b08b52017-06-01 16:11:34 -07001866
1867 {
1868 ScreenCapture::captureScreen(&mCapture);
1869 mCapture->expectChildColor(0, 0);
1870 mCapture->expectChildColor(4, 4);
1871 mCapture->expectBGColor(5, 5);
1872 }
1873}
1874
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001876 asTransaction([&](Transaction& t) {
1877 t.show(mChild);
1878 t.setPosition(mFGSurfaceControl, 0, 0);
1879 t.setPosition(mChild, 63, 63);
1880 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001881
1882 {
1883 ScreenCapture::captureScreen(&mCapture);
1884 mCapture->expectFGColor(0, 0);
1885 // Last pixel in foreground should now be the child.
1886 mCapture->expectChildColor(63, 63);
1887 // But the child should be constrained and the next pixel
1888 // must be the background
1889 mCapture->expectBGColor(64, 64);
1890 }
1891}
1892
1893TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001894 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001895
1896 // Find the boundary between the parent and child
1897 {
1898 ScreenCapture::captureScreen(&mCapture);
1899 mCapture->expectChildColor(9, 9);
1900 mCapture->expectFGColor(10, 10);
1901 }
1902
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001903 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001904
1905 // The boundary should be twice as far from the origin now.
1906 // The pixels from the last test should all be child now
1907 {
1908 ScreenCapture::captureScreen(&mCapture);
1909 mCapture->expectChildColor(9, 9);
1910 mCapture->expectChildColor(10, 10);
1911 mCapture->expectChildColor(19, 19);
1912 mCapture->expectFGColor(20, 20);
1913 }
1914}
Robert Carr9524cb32017-02-13 11:32:32 -08001915
Robert Carr6452f122017-03-21 10:41:29 -07001916TEST_F(ChildLayerTest, ChildLayerAlpha) {
1917 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1918 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1919 fillSurfaceRGBA8(mChild, 0, 254, 0);
1920 waitForPostedBuffers();
1921
Robert Carr4cdc58f2017-08-23 14:22:20 -07001922 asTransaction([&](Transaction& t) {
1923 t.show(mChild);
1924 t.setPosition(mChild, 0, 0);
1925 t.setPosition(mFGSurfaceControl, 0, 0);
1926 });
Robert Carr6452f122017-03-21 10:41:29 -07001927
1928 {
1929 ScreenCapture::captureScreen(&mCapture);
1930 // Unblended child color
1931 mCapture->checkPixel(0, 0, 0, 254, 0);
1932 }
1933
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001934 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001935
1936 {
1937 ScreenCapture::captureScreen(&mCapture);
1938 // Child and BG blended.
1939 mCapture->checkPixel(0, 0, 127, 127, 0);
1940 }
1941
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001942 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001943
1944 {
1945 ScreenCapture::captureScreen(&mCapture);
1946 // Child and BG blended.
1947 mCapture->checkPixel(0, 0, 95, 64, 95);
1948 }
1949}
1950
Robert Carr9524cb32017-02-13 11:32:32 -08001951TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001952 asTransaction([&](Transaction& t) {
1953 t.show(mChild);
1954 t.setPosition(mChild, 10, 10);
1955 t.setPosition(mFGSurfaceControl, 64, 64);
1956 });
Robert Carr9524cb32017-02-13 11:32:32 -08001957
1958 {
1959 ScreenCapture::captureScreen(&mCapture);
1960 // Top left of foreground must now be visible
1961 mCapture->expectFGColor(64, 64);
1962 // But 10 pixels in we should see the child surface
1963 mCapture->expectChildColor(74, 74);
1964 // And 10 more pixels we should be back to the foreground surface
1965 mCapture->expectFGColor(84, 84);
1966 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001967
1968 asTransaction([&](Transaction& t) {
1969 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1970 });
1971
Robert Carr9524cb32017-02-13 11:32:32 -08001972 {
1973 ScreenCapture::captureScreen(&mCapture);
1974 mCapture->expectFGColor(64, 64);
1975 // In reparenting we should have exposed the entire foreground surface.
1976 mCapture->expectFGColor(74, 74);
1977 // And the child layer should now begin at 10, 10 (since the BG
1978 // layer is at (0, 0)).
1979 mCapture->expectBGColor(9, 9);
1980 mCapture->expectChildColor(10, 10);
1981 }
1982}
1983
chaviw161410b02017-07-27 10:46:08 -07001984TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001985 asTransaction([&](Transaction& t) {
1986 t.show(mChild);
1987 t.setPosition(mChild, 10, 10);
1988 t.setPosition(mFGSurfaceControl, 64, 64);
1989 });
Robert Carr9524cb32017-02-13 11:32:32 -08001990
1991 {
1992 ScreenCapture::captureScreen(&mCapture);
1993 // Top left of foreground must now be visible
1994 mCapture->expectFGColor(64, 64);
1995 // But 10 pixels in we should see the child surface
1996 mCapture->expectChildColor(74, 74);
1997 // And 10 more pixels we should be back to the foreground surface
1998 mCapture->expectFGColor(84, 84);
1999 }
2000
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002001 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002002
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002003 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002004
chaviw161410b02017-07-27 10:46:08 -07002005 // Since the child has the same client as the parent, it will not get
2006 // detached and will be hidden.
2007 {
2008 ScreenCapture::captureScreen(&mCapture);
2009 mCapture->expectFGColor(64, 64);
2010 mCapture->expectFGColor(74, 74);
2011 mCapture->expectFGColor(84, 84);
2012 }
2013}
2014
2015TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2016 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002017 sp<SurfaceControl> mChildNewClient =
2018 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2019 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002020
2021 ASSERT_TRUE(mChildNewClient != NULL);
2022 ASSERT_TRUE(mChildNewClient->isValid());
2023
2024 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2025
Robert Carr4cdc58f2017-08-23 14:22:20 -07002026 asTransaction([&](Transaction& t) {
2027 t.hide(mChild);
2028 t.show(mChildNewClient);
2029 t.setPosition(mChildNewClient, 10, 10);
2030 t.setPosition(mFGSurfaceControl, 64, 64);
2031 });
chaviw161410b02017-07-27 10:46:08 -07002032
2033 {
2034 ScreenCapture::captureScreen(&mCapture);
2035 // Top left of foreground must now be visible
2036 mCapture->expectFGColor(64, 64);
2037 // But 10 pixels in we should see the child surface
2038 mCapture->expectChildColor(74, 74);
2039 // And 10 more pixels we should be back to the foreground surface
2040 mCapture->expectFGColor(84, 84);
2041 }
2042
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002043 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002044
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002045 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002046
Robert Carr9524cb32017-02-13 11:32:32 -08002047 // Nothing should have changed.
2048 {
2049 ScreenCapture::captureScreen(&mCapture);
2050 mCapture->expectFGColor(64, 64);
2051 mCapture->expectChildColor(74, 74);
2052 mCapture->expectFGColor(84, 84);
2053 }
2054}
2055
Robert Carr9b429f42017-04-17 14:56:57 -07002056TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002057 asTransaction([&](Transaction& t) {
2058 t.show(mChild);
2059 t.setPosition(mChild, 0, 0);
2060 t.setPosition(mFGSurfaceControl, 0, 0);
2061 });
Robert Carr9b429f42017-04-17 14:56:57 -07002062
2063 {
2064 ScreenCapture::captureScreen(&mCapture);
2065 // We've positioned the child in the top left.
2066 mCapture->expectChildColor(0, 0);
2067 // But it's only 10x10.
2068 mCapture->expectFGColor(10, 10);
2069 }
2070
Robert Carr4cdc58f2017-08-23 14:22:20 -07002071 asTransaction([&](Transaction& t) {
2072 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2073 // We cause scaling by 2.
2074 t.setSize(mFGSurfaceControl, 128, 128);
2075 });
Robert Carr9b429f42017-04-17 14:56:57 -07002076
2077 {
2078 ScreenCapture::captureScreen(&mCapture);
2079 // We've positioned the child in the top left.
2080 mCapture->expectChildColor(0, 0);
2081 mCapture->expectChildColor(10, 10);
2082 mCapture->expectChildColor(19, 19);
2083 // And now it should be scaled all the way to 20x20
2084 mCapture->expectFGColor(20, 20);
2085 }
2086}
2087
Robert Carr1725eee2017-04-26 18:32:15 -07002088// Regression test for b/37673612
2089TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002090 asTransaction([&](Transaction& t) {
2091 t.show(mChild);
2092 t.setPosition(mChild, 0, 0);
2093 t.setPosition(mFGSurfaceControl, 0, 0);
2094 });
Robert Carr1725eee2017-04-26 18:32:15 -07002095
2096 {
2097 ScreenCapture::captureScreen(&mCapture);
2098 // We've positioned the child in the top left.
2099 mCapture->expectChildColor(0, 0);
2100 // But it's only 10x10.
2101 mCapture->expectFGColor(10, 10);
2102 }
Robert Carr1725eee2017-04-26 18:32:15 -07002103 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2104 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002105 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002106 sp<Surface> s = mFGSurfaceControl->getSurface();
2107 auto anw = static_cast<ANativeWindow*>(s.get());
2108 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2109 native_window_set_buffers_dimensions(anw, 64, 128);
2110 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2111 waitForPostedBuffers();
2112
2113 {
2114 // The child should still be in the same place and not have any strange scaling as in
2115 // b/37673612.
2116 ScreenCapture::captureScreen(&mCapture);
2117 mCapture->expectChildColor(0, 0);
2118 mCapture->expectFGColor(10, 10);
2119 }
2120}
2121
Dan Stoza412903f2017-04-27 13:42:17 -07002122TEST_F(ChildLayerTest, Bug36858924) {
2123 // Destroy the child layer
2124 mChild.clear();
2125
2126 // Now recreate it as hidden
2127 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2128 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2129 mFGSurfaceControl.get());
2130
2131 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002132 asTransaction([&](Transaction& t) {
2133 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002134 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002135 t.show(mChild);
2136 });
Dan Stoza412903f2017-04-27 13:42:17 -07002137
2138 // Render the foreground surface a few times
2139 //
2140 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2141 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2142 // never acquire/release the first buffer
2143 ALOGI("Filling 1");
2144 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2145 ALOGI("Filling 2");
2146 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2147 ALOGI("Filling 3");
2148 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2149 ALOGI("Filling 4");
2150 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2151}
2152
chaviwf1961f72017-09-18 16:41:07 -07002153TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002154 asTransaction([&](Transaction& t) {
2155 t.show(mChild);
2156 t.setPosition(mChild, 10, 10);
2157 t.setPosition(mFGSurfaceControl, 64, 64);
2158 });
chaviw06178942017-07-27 10:25:59 -07002159
2160 {
2161 ScreenCapture::captureScreen(&mCapture);
2162 // Top left of foreground must now be visible
2163 mCapture->expectFGColor(64, 64);
2164 // But 10 pixels in we should see the child surface
2165 mCapture->expectChildColor(74, 74);
2166 // And 10 more pixels we should be back to the foreground surface
2167 mCapture->expectFGColor(84, 84);
2168 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002169
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002170 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002171
chaviw06178942017-07-27 10:25:59 -07002172 {
2173 ScreenCapture::captureScreen(&mCapture);
2174 mCapture->expectFGColor(64, 64);
2175 // In reparenting we should have exposed the entire foreground surface.
2176 mCapture->expectFGColor(74, 74);
2177 // And the child layer should now begin at 10, 10 (since the BG
2178 // layer is at (0, 0)).
2179 mCapture->expectBGColor(9, 9);
2180 mCapture->expectChildColor(10, 10);
2181 }
2182}
2183
chaviwf1961f72017-09-18 16:41:07 -07002184TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002185 asTransaction([&](Transaction& t) {
2186 t.show(mChild);
2187 t.setPosition(mChild, 10, 10);
2188 t.setPosition(mFGSurfaceControl, 64, 64);
2189 });
chaviwf1961f72017-09-18 16:41:07 -07002190
2191 {
2192 ScreenCapture::captureScreen(&mCapture);
2193 // Top left of foreground must now be visible
2194 mCapture->expectFGColor(64, 64);
2195 // But 10 pixels in we should see the child surface
2196 mCapture->expectChildColor(74, 74);
2197 // And 10 more pixels we should be back to the foreground surface
2198 mCapture->expectFGColor(84, 84);
2199 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002200 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002201 {
2202 ScreenCapture::captureScreen(&mCapture);
2203 // Nothing should have changed.
2204 mCapture->expectFGColor(64, 64);
2205 mCapture->expectChildColor(74, 74);
2206 mCapture->expectFGColor(84, 84);
2207 }
2208}
2209
2210TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002211 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2212 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002213 ASSERT_TRUE(newSurface != NULL);
2214 ASSERT_TRUE(newSurface->isValid());
2215
2216 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002217 asTransaction([&](Transaction& t) {
2218 t.hide(mChild);
2219 t.show(newSurface);
2220 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002221 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002222 t.setPosition(mFGSurfaceControl, 64, 64);
2223 });
chaviwf1961f72017-09-18 16:41:07 -07002224
2225 {
2226 ScreenCapture::captureScreen(&mCapture);
2227 // Top left of foreground must now be visible
2228 mCapture->expectFGColor(64, 64);
2229 // At 10, 10 we should see the new surface
2230 mCapture->checkPixel(10, 10, 63, 195, 63);
2231 }
2232
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002233 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002234
2235 {
2236 ScreenCapture::captureScreen(&mCapture);
2237 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2238 // mFGSurface, putting it at 74, 74.
2239 mCapture->expectFGColor(64, 64);
2240 mCapture->checkPixel(74, 74, 63, 195, 63);
2241 mCapture->expectFGColor(84, 84);
2242 }
2243}
2244
chaviwc9674332017-08-28 12:32:18 -07002245TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002246 sp<SurfaceControl> grandchild =
2247 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2248 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002249 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2250
2251 {
2252 ScreenCapture::captureScreen(&mCapture);
2253 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2254 // which begins at 64, 64
2255 mCapture->checkPixel(64, 64, 50, 50, 50);
2256 }
2257}
2258
Robert Carr503c7042017-09-27 15:06:08 -07002259TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002260 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2261 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002262 fillSurfaceRGBA8(relative, 255, 255, 255);
2263
2264 Transaction t;
2265 t.setLayer(relative, INT32_MAX)
2266 .setRelativeLayer(mChild, relative->getHandle(), 1)
2267 .setPosition(mFGSurfaceControl, 0, 0)
2268 .apply(true);
2269
2270 // We expect that the child should have been elevated above our
2271 // INT_MAX layer even though it's not a child of it.
2272 {
2273 ScreenCapture::captureScreen(&mCapture);
2274 mCapture->expectChildColor(0, 0);
2275 mCapture->expectChildColor(9, 9);
2276 mCapture->checkPixel(10, 10, 255, 255, 255);
2277 }
2278}
2279
chaviwa76b2712017-09-20 12:02:26 -07002280class ScreenCaptureTest : public LayerUpdateTest {
2281protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002282 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002283};
2284
2285TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2286 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002287 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002288 mCapture->expectBGColor(0, 0);
2289 // Doesn't capture FG layer which is at 64, 64
2290 mCapture->expectBGColor(64, 64);
2291}
2292
2293TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2294 auto fgHandle = mFGSurfaceControl->getHandle();
2295
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002296 sp<SurfaceControl> child =
2297 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2298 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002299 fillSurfaceRGBA8(child, 200, 200, 200);
2300
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002301 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002302
2303 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002304 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002305 mCapture->expectFGColor(10, 10);
2306 mCapture->expectChildColor(0, 0);
2307}
2308
2309TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2310 auto fgHandle = mFGSurfaceControl->getHandle();
2311
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002312 sp<SurfaceControl> child =
2313 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2314 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002315 fillSurfaceRGBA8(child, 200, 200, 200);
2316
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002317 sp<SurfaceControl> grandchild =
2318 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2319 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002320
2321 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2322 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002323 .show(child)
2324 .setPosition(grandchild, 5, 5)
2325 .show(grandchild)
2326 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002327
2328 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002329 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002330 mCapture->expectFGColor(10, 10);
2331 mCapture->expectChildColor(0, 0);
2332 mCapture->checkPixel(5, 5, 50, 50, 50);
2333}
2334
2335TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002336 sp<SurfaceControl> child =
2337 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2338 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002339 fillSurfaceRGBA8(child, 200, 200, 200);
2340 auto childHandle = child->getHandle();
2341
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002342 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002343
2344 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002345 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07002346 mCapture->expectChildColor(0, 0);
2347 mCapture->expectChildColor(9, 9);
2348}
2349
2350TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002351 sp<SurfaceControl> child =
2352 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2353 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002354 fillSurfaceRGBA8(child, 200, 200, 200);
2355 auto childHandle = child->getHandle();
2356
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002357 sp<SurfaceControl> grandchild =
2358 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2359 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002360 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2361
2362 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002363 .show(child)
2364 .setPosition(grandchild, 5, 5)
2365 .show(grandchild)
2366 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002367
2368 auto grandchildHandle = grandchild->getHandle();
2369
2370 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002371 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07002372 mCapture->checkPixel(0, 0, 50, 50, 50);
2373 mCapture->checkPixel(4, 4, 50, 50, 50);
2374}
2375
chaviw7206d492017-11-10 16:16:12 -08002376TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002377 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2378 PIXEL_FORMAT_RGBA_8888, 0);
2379 sp<SurfaceControl> blueLayer =
2380 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2381 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002382
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002383 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2384 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002385
2386 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002387 .setLayer(redLayer, INT32_MAX - 1)
2388 .show(redLayer)
2389 .show(blueLayer)
2390 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002391
2392 auto redLayerHandle = redLayer->getHandle();
2393
2394 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002395 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2396 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2397 // red area below the blue area
2398 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2399 // red area to the right of the blue area
2400 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002401
2402 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002403 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08002404 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2405 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002406 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08002407 mCapture->checkPixel(30, 30, 0, 0, 0);
2408}
2409
2410TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002411 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2412 PIXEL_FORMAT_RGBA_8888, 0);
2413 sp<SurfaceControl> blueLayer =
2414 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2415 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002416
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002417 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2418 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002419
2420 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002421 .setLayer(redLayer, INT32_MAX - 1)
2422 .show(redLayer)
2423 .show(blueLayer)
2424 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002425
2426 auto redLayerHandle = redLayer->getHandle();
2427
2428 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002429 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2430 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2431 // red area below the blue area
2432 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2433 // red area to the right of the blue area
2434 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002435
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002436 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08002437 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002438 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
2439 // red area below the blue area
2440 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
2441 // red area to the right of the blue area
2442 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002443 mCapture->checkPixel(30, 30, 0, 0, 0);
2444}
2445
2446TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002447 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2448 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08002449
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002450 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
chaviw7206d492017-11-10 16:16:12 -08002451
2452 auto redLayerHandle = redLayer->getHandle();
2453 mComposerClient->destroySurface(redLayerHandle);
2454 SurfaceComposerClient::Transaction().apply(true);
2455
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002456 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08002457
2458 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002459 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2460 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08002461}
2462
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002463} // namespace android