blob: 176c691c6a87a0de81e4e4c15b046c0a5baf9a88 [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();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800151 ASSERT_TRUE(s != nullptr);
152 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800153 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
Robert Carr578038f2018-03-09 12:25:24 -0800195 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
196 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
197 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
198 SurfaceComposerClient::Transaction().apply(true);
199
200 sp<GraphicBuffer> outBuffer;
201 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
202 *sc = std::make_unique<ScreenCapture>(outBuffer);
203 }
204
Chia-I Wu718daf82017-10-20 11:57:17 -0700205 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000206 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
207 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700208 }
209
210 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000211 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700212 const bool leftBorder = rect.left > 0;
213 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000214 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
215 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700216
217 if (topBorder) {
218 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
219 if (leftBorder) {
220 top.left -= 1;
221 }
222 if (rightBorder) {
223 top.right += 1;
224 }
225 expectColor(top, color, tolerance);
226 }
227 if (leftBorder) {
228 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
229 expectColor(left, color, tolerance);
230 }
231 if (rightBorder) {
232 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
233 expectColor(right, color, tolerance);
234 }
235 if (bottomBorder) {
236 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
237 if (leftBorder) {
238 bottom.left -= 1;
239 }
240 if (rightBorder) {
241 bottom.right += 1;
242 }
243 expectColor(bottom, color, tolerance);
244 }
245 }
246
Chia-I Wu93853fe2017-11-02 08:30:27 -0700247 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
248 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
249 uint8_t tolerance = 0) {
250 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
251
252 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
253 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
254 // avoid checking borders due to unspecified filtering behavior
255 const int32_t offsetX = filtered ? 2 : 0;
256 const int32_t offsetY = filtered ? 2 : 0;
257 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
258 tolerance);
259 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
260 tolerance);
261 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
262 tolerance);
263 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
264 bottomRight, tolerance);
265 }
266
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700267 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000268 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
269 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700270 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
271 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700272 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
273 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700274 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700275 }
276 }
277
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700278 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700279
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700280 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700281
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700282 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700283
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000284 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
285 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700286 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700287
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000288 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700289
290private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000291 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800292 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700293};
294
Chia-I Wu718daf82017-10-20 11:57:17 -0700295class LayerTransactionTest : public ::testing::Test {
296protected:
297 void SetUp() override {
298 mClient = new SurfaceComposerClient;
299 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
300
301 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
302 }
303
304 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
305 uint32_t flags = 0) {
306 auto layer =
307 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
308 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
309
310 status_t error = Transaction()
311 .setLayerStack(layer, mDisplayLayerStack)
312 .setLayer(layer, mLayerZBase)
313 .apply();
314 if (error != NO_ERROR) {
315 ADD_FAILURE() << "failed to initialize SurfaceControl";
316 layer.clear();
317 }
318
319 return layer;
320 }
321
322 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
323 // wait for previous transactions (such as setSize) to complete
324 Transaction().apply(true);
325
326 ANativeWindow_Buffer buffer = {};
327 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
328
329 return buffer;
330 }
331
332 void postLayerBuffer(const sp<SurfaceControl>& layer) {
333 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
334
335 // wait for the newly posted buffer to be latched
336 waitForLayerBuffers();
337 }
338
339 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
340 ANativeWindow_Buffer buffer;
341 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
342 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
343 postLayerBuffer(layer);
344 }
345
Chia-I Wu93853fe2017-11-02 08:30:27 -0700346 void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft,
347 const Color& topRight, const Color& bottomLeft,
348 const Color& bottomRight) {
349 ANativeWindow_Buffer buffer;
350 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
351 ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0);
352
353 const int32_t halfW = buffer.width / 2;
354 const int32_t halfH = buffer.height / 2;
355 fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
356 fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight);
357 fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft);
358 fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight);
359
360 postLayerBuffer(layer);
361 }
362
Chia-I Wu718daf82017-10-20 11:57:17 -0700363 sp<ScreenCapture> screenshot() {
364 sp<ScreenCapture> screenshot;
365 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
366 return screenshot;
367 }
368
369 sp<SurfaceComposerClient> mClient;
370
371 sp<IBinder> mDisplay;
372 uint32_t mDisplayWidth;
373 uint32_t mDisplayHeight;
374 uint32_t mDisplayLayerStack;
375
376 // leave room for ~256 layers
377 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
378
379private:
380 void SetUpDisplay() {
381 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
382 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
383
384 // get display width/height
385 DisplayInfo info;
386 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
387 mDisplayWidth = info.w;
388 mDisplayHeight = info.h;
389
390 // After a new buffer is queued, SurfaceFlinger is notified and will
391 // latch the new buffer on next vsync. Let's heuristically wait for 3
392 // vsyncs.
393 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
394
395 mDisplayLayerStack = 0;
396 // set layer stack (b/68888219)
397 Transaction t;
398 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
399 t.apply();
400 }
401
402 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
403
404 int32_t mBufferPostDelay;
405};
406
407TEST_F(LayerTransactionTest, SetPositionBasic) {
408 sp<SurfaceControl> layer;
409 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
410 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
411
412 {
413 SCOPED_TRACE("default position");
414 auto shot = screenshot();
415 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
416 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
417 }
418
419 Transaction().setPosition(layer, 5, 10).apply();
420 {
421 SCOPED_TRACE("new position");
422 auto shot = screenshot();
423 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
424 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
425 }
426}
427
428TEST_F(LayerTransactionTest, SetPositionRounding) {
429 sp<SurfaceControl> layer;
430 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
431 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
432
433 // GLES requires only 4 bits of subpixel precision during rasterization
434 // XXX GLES composition does not match HWC composition due to precision
435 // loss (b/69315223)
436 const float epsilon = 1.0f / 16.0f;
437 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
438 {
439 SCOPED_TRACE("rounding down");
440 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
441 }
442
443 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
444 {
445 SCOPED_TRACE("rounding up");
446 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
447 }
448}
449
450TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
451 sp<SurfaceControl> layer;
452 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
453 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
454
455 Transaction().setPosition(layer, -32, -32).apply();
456 {
457 SCOPED_TRACE("negative coordinates");
458 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
459 }
460
461 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
462 {
463 SCOPED_TRACE("positive coordinates");
464 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
465 }
466}
467
468TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
469 sp<SurfaceControl> layer;
470 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
471 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
472
473 // partially out of bounds
474 Transaction().setPosition(layer, -30, -30).apply();
475 {
476 SCOPED_TRACE("negative coordinates");
477 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
478 }
479
480 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
481 {
482 SCOPED_TRACE("positive coordinates");
483 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
484 mDisplayHeight),
485 Color::RED);
486 }
487}
488
489TEST_F(LayerTransactionTest, SetPositionWithResize) {
490 sp<SurfaceControl> layer;
491 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
492 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
493
494 // setPosition is applied immediately by default, with or without resize
495 // pending
496 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
497 {
498 SCOPED_TRACE("resize pending");
499 auto shot = screenshot();
500 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
501 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
502 }
503
504 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
505 {
506 SCOPED_TRACE("resize applied");
507 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
508 }
509}
510
511TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
512 sp<SurfaceControl> layer;
513 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
514 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
515
516 // request setPosition to be applied with the next resize
517 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
518 {
519 SCOPED_TRACE("new position pending");
520 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
521 }
522
523 Transaction().setPosition(layer, 15, 20).apply();
524 {
525 SCOPED_TRACE("pending new position modified");
526 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
527 }
528
529 Transaction().setSize(layer, 64, 64).apply();
530 {
531 SCOPED_TRACE("resize pending");
532 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
533 }
534
535 // finally resize and latch the buffer
536 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
537 {
538 SCOPED_TRACE("new position applied");
539 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
540 }
541}
542
543TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
544 sp<SurfaceControl> layer;
545 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
546 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
547
548 // setPosition is not immediate even with SCALE_TO_WINDOW override
549 Transaction()
550 .setPosition(layer, 5, 10)
551 .setSize(layer, 64, 64)
552 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
553 .setGeometryAppliesWithResize(layer)
554 .apply();
555 {
556 SCOPED_TRACE("new position pending");
557 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
558 }
559
560 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
561 {
562 SCOPED_TRACE("new position applied");
563 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
564 }
565}
566
Chia-I Wu0eaea312017-10-31 10:14:40 -0700567TEST_F(LayerTransactionTest, SetSizeBasic) {
568 sp<SurfaceControl> layer;
569 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
570 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
571
572 Transaction().setSize(layer, 64, 64).apply();
573 {
574 SCOPED_TRACE("resize pending");
575 auto shot = screenshot();
576 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
577 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
578 }
579
580 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
581 {
582 SCOPED_TRACE("resize applied");
583 auto shot = screenshot();
584 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
585 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
586 }
587}
588
589TEST_F(LayerTransactionTest, SetSizeInvalid) {
590 // cannot test robustness against invalid sizes (zero or really huge)
591}
592
593TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
594 sp<SurfaceControl> layer;
595 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
596 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
597
598 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
599 Transaction()
600 .setSize(layer, 64, 64)
601 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
602 .apply();
603 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
604}
605
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700606TEST_F(LayerTransactionTest, SetZBasic) {
607 sp<SurfaceControl> layerR;
608 sp<SurfaceControl> layerG;
609 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
610 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
611 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
612 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
613
614 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
615 {
616 SCOPED_TRACE("layerR");
617 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
618 }
619
620 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
621 {
622 SCOPED_TRACE("layerG");
623 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
624 }
625}
626
627TEST_F(LayerTransactionTest, SetZNegative) {
628 sp<SurfaceControl> layerR;
629 sp<SurfaceControl> layerG;
630 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
631 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
632 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
633 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
634
635 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
636 {
637 SCOPED_TRACE("layerR");
638 sp<ScreenCapture> screenshot;
639 ScreenCapture::captureScreen(&screenshot, -2, -1);
640 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
641 }
642
643 Transaction().setLayer(layerR, -3).apply();
644 {
645 SCOPED_TRACE("layerG");
646 sp<ScreenCapture> screenshot;
647 ScreenCapture::captureScreen(&screenshot, -3, -1);
648 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
649 }
650}
651
Chia-I Wu49313302017-10-31 10:14:40 -0700652TEST_F(LayerTransactionTest, SetRelativeZBasic) {
653 sp<SurfaceControl> layerR;
654 sp<SurfaceControl> layerG;
655 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
656 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
657 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
658 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
659
660 Transaction()
661 .setPosition(layerG, 16, 16)
662 .setRelativeLayer(layerG, layerR->getHandle(), 1)
663 .apply();
664 {
665 SCOPED_TRACE("layerG above");
666 auto shot = screenshot();
667 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
668 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
669 }
670
671 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
672 {
673 SCOPED_TRACE("layerG below");
674 auto shot = screenshot();
675 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
676 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
677 }
678}
679
Chia-I Wuec2d9852017-11-21 09:21:01 -0800680TEST_F(LayerTransactionTest, SetRelativeZNegative) {
681 sp<SurfaceControl> layerR;
682 sp<SurfaceControl> layerG;
683 sp<SurfaceControl> layerB;
684 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
685 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
686 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
687 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
688 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
689 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
690
691 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
692 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
693
694 sp<ScreenCapture> screenshot;
695 // only layerB is in this range
696 ScreenCapture::captureScreen(&screenshot, -2, -1);
697 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
698}
699
Chia-I Wu49313302017-10-31 10:14:40 -0700700TEST_F(LayerTransactionTest, SetRelativeZGroup) {
701 sp<SurfaceControl> layerR;
702 sp<SurfaceControl> layerG;
703 sp<SurfaceControl> layerB;
704 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
705 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
706 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
707 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
708 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
709 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
710
711 // layerR = 0, layerG = layerR + 3, layerB = 2
712 Transaction()
713 .setPosition(layerG, 8, 8)
714 .setRelativeLayer(layerG, layerR->getHandle(), 3)
715 .setPosition(layerB, 16, 16)
716 .setLayer(layerB, mLayerZBase + 2)
717 .apply();
718 {
719 SCOPED_TRACE("(layerR < layerG) < layerB");
720 auto shot = screenshot();
721 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
722 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
723 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
724 }
725
726 // layerR = 4, layerG = layerR + 3, layerB = 2
727 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
728 {
729 SCOPED_TRACE("layerB < (layerR < layerG)");
730 auto shot = screenshot();
731 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
732 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
733 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
734 }
735
736 // layerR = 4, layerG = layerR - 3, layerB = 2
737 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
738 {
739 SCOPED_TRACE("layerB < (layerG < layerR)");
740 auto shot = screenshot();
741 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
742 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
743 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
744 }
745
746 // restore to absolute z
747 // layerR = 4, layerG = 0, layerB = 2
748 Transaction().setLayer(layerG, mLayerZBase).apply();
749 {
750 SCOPED_TRACE("layerG < layerB < layerR");
751 auto shot = screenshot();
752 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
753 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
754 }
755
756 // layerR should not affect layerG anymore
757 // layerR = 1, layerG = 0, layerB = 2
758 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
759 {
760 SCOPED_TRACE("layerG < layerR < layerB");
761 auto shot = screenshot();
762 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
763 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
764 }
765}
766
767TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
768 sp<SurfaceControl> layerR;
769 sp<SurfaceControl> layerG;
770
771 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
772 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
773 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
774 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
775
776 Transaction()
777 .setPosition(layerG, 16, 16)
778 .setRelativeLayer(layerG, layerR->getHandle(), 1)
779 .apply();
780
781 mClient->destroySurface(layerG->getHandle());
782 // layerG should have been removed
783 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
784}
785
Chia-I Wu57b27502017-10-31 10:14:40 -0700786TEST_F(LayerTransactionTest, SetFlagsHidden) {
787 sp<SurfaceControl> layer;
788 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
789 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
790
791 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
792 {
793 SCOPED_TRACE("layer hidden");
794 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
795 }
796
797 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
798 {
799 SCOPED_TRACE("layer shown");
800 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
801 }
802}
803
804TEST_F(LayerTransactionTest, SetFlagsOpaque) {
805 const Color translucentRed = {100, 0, 0, 100};
806 sp<SurfaceControl> layerR;
807 sp<SurfaceControl> layerG;
808 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
809 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
810 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
811 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
812
813 Transaction()
814 .setLayer(layerR, mLayerZBase + 1)
815 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
816 .apply();
817 {
818 SCOPED_TRACE("layerR opaque");
819 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
820 }
821
822 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
823 {
824 SCOPED_TRACE("layerR translucent");
825 const uint8_t g = uint8_t(255 - translucentRed.a);
826 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
827 }
828}
829
830TEST_F(LayerTransactionTest, SetFlagsSecure) {
831 sp<SurfaceControl> layer;
832 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
833 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
834
835 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000836 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -0700837 Transaction()
838 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
839 .apply(true);
840 ASSERT_EQ(PERMISSION_DENIED,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000841 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700842 false));
843
844 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
845 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000846 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700847 false));
848}
849
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700850TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
851 const Rect top(0, 0, 32, 16);
852 const Rect bottom(0, 16, 32, 32);
853 sp<SurfaceControl> layer;
854 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
855
856 ANativeWindow_Buffer buffer;
857 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
858 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
859 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
860 // setTransparentRegionHint always applies to the following buffer
861 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
862 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
863 {
864 SCOPED_TRACE("top transparent");
865 auto shot = screenshot();
866 shot->expectColor(top, Color::BLACK);
867 shot->expectColor(bottom, Color::RED);
868 }
869
870 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
871 {
872 SCOPED_TRACE("transparent region hint pending");
873 auto shot = screenshot();
874 shot->expectColor(top, Color::BLACK);
875 shot->expectColor(bottom, Color::RED);
876 }
877
878 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
879 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
880 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
881 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
882 {
883 SCOPED_TRACE("bottom transparent");
884 auto shot = screenshot();
885 shot->expectColor(top, Color::RED);
886 shot->expectColor(bottom, Color::BLACK);
887 }
888}
889
890TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
891 sp<SurfaceControl> layerTransparent;
892 sp<SurfaceControl> layerR;
893 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
894 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
895
896 // check that transparent region hint is bound by the layer size
897 Transaction()
898 .setTransparentRegionHint(layerTransparent,
899 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
900 .setPosition(layerR, 16, 16)
901 .setLayer(layerR, mLayerZBase + 1)
902 .apply();
903 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
904 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
905 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
906}
907
Chia-I Wua8a515e2017-11-01 15:16:35 -0700908TEST_F(LayerTransactionTest, SetAlphaBasic) {
909 sp<SurfaceControl> layer1;
910 sp<SurfaceControl> layer2;
911 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
912 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
913 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
914 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
915
916 Transaction()
917 .setAlpha(layer1, 0.25f)
918 .setAlpha(layer2, 0.75f)
919 .setPosition(layer2, 16, 0)
920 .setLayer(layer2, mLayerZBase + 1)
921 .apply();
922 {
923 auto shot = screenshot();
924 uint8_t r = 16; // 64 * 0.25f
925 uint8_t g = 48; // 64 * 0.75f
926 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
927 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
928
929 r /= 4; // r * (1.0f - 0.75f)
930 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
931 }
932}
933
934TEST_F(LayerTransactionTest, SetAlphaClamped) {
935 const Color color = {64, 0, 0, 255};
936 sp<SurfaceControl> layer;
937 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
938 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
939
940 Transaction().setAlpha(layer, 2.0f).apply();
941 {
942 SCOPED_TRACE("clamped to 1.0f");
943 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
944 }
945
946 Transaction().setAlpha(layer, -1.0f).apply();
947 {
948 SCOPED_TRACE("clamped to 0.0f");
949 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
950 }
951}
952
Chia-I Wue4ef6102017-11-01 15:16:35 -0700953TEST_F(LayerTransactionTest, SetColorBasic) {
954 sp<SurfaceControl> bufferLayer;
955 sp<SurfaceControl> colorLayer;
956 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
957 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
958 ASSERT_NO_FATAL_FAILURE(
959 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
960
961 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
962 {
963 SCOPED_TRACE("default color");
964 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
965 }
966
967 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
968 const Color expected = {15, 51, 85, 255};
969 // this is handwavy, but the precison loss scaled by 255 (8-bit per
970 // channel) should be less than one
971 const uint8_t tolerance = 1;
972 Transaction().setColor(colorLayer, color).apply();
973 {
974 SCOPED_TRACE("new color");
975 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
976 }
977}
978
979TEST_F(LayerTransactionTest, SetColorClamped) {
980 sp<SurfaceControl> colorLayer;
981 ASSERT_NO_FATAL_FAILURE(
982 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
983
984 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
985 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
986}
987
988TEST_F(LayerTransactionTest, SetColorWithAlpha) {
989 sp<SurfaceControl> bufferLayer;
990 sp<SurfaceControl> colorLayer;
991 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
992 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
993 ASSERT_NO_FATAL_FAILURE(
994 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
995
996 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
997 const float alpha = 0.25f;
998 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
999 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1000 // channel) should be less than one
1001 const uint8_t tolerance = 1;
1002 Transaction()
1003 .setColor(colorLayer, color)
1004 .setAlpha(colorLayer, alpha)
1005 .setLayer(colorLayer, mLayerZBase + 1)
1006 .apply();
1007 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1008 tolerance);
1009}
1010
Adrian Roosb7a96502018-04-08 11:38:55 -07001011TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1012 sp<SurfaceControl> bufferLayer;
1013 sp<SurfaceControl> parentLayer;
1014 sp<SurfaceControl> colorLayer;
1015 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1016 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
1017 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1018 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1019 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1020
1021 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1022 const float alpha = 0.25f;
1023 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1024 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1025 // channel) should be less than one
1026 const uint8_t tolerance = 1;
1027 Transaction()
1028 .reparent(colorLayer, parentLayer->getHandle())
1029 .setColor(colorLayer, color)
1030 .setAlpha(parentLayer, alpha)
1031 .setLayer(parentLayer, mLayerZBase + 1)
1032 .apply();
1033 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1034 tolerance);
1035}
1036
Chia-I Wue4ef6102017-11-01 15:16:35 -07001037TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1038 sp<SurfaceControl> bufferLayer;
1039 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1040 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1041
1042 // color is ignored
1043 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1044 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1045}
1046
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001047TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1048 sp<SurfaceControl> layer;
1049 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1050 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1051
1052 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1053 {
1054 SCOPED_TRACE("non-existing layer stack");
1055 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1056 }
1057
1058 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1059 {
1060 SCOPED_TRACE("original layer stack");
1061 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1062 }
1063}
1064
Chia-I Wu93853fe2017-11-02 08:30:27 -07001065TEST_F(LayerTransactionTest, SetMatrixBasic) {
1066 sp<SurfaceControl> layer;
1067 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1068 ASSERT_NO_FATAL_FAILURE(
1069 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1070
1071 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1072 {
1073 SCOPED_TRACE("IDENTITY");
1074 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1075 Color::WHITE);
1076 }
1077
1078 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1079 {
1080 SCOPED_TRACE("FLIP_H");
1081 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1082 Color::BLUE);
1083 }
1084
1085 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1086 {
1087 SCOPED_TRACE("FLIP_V");
1088 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1089 Color::GREEN);
1090 }
1091
1092 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1093 {
1094 SCOPED_TRACE("ROT_90");
1095 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1096 Color::GREEN);
1097 }
1098
1099 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1100 {
1101 SCOPED_TRACE("SCALE");
1102 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1103 Color::WHITE, true /* filtered */);
1104 }
1105}
1106
1107TEST_F(LayerTransactionTest, SetMatrixRot45) {
1108 sp<SurfaceControl> layer;
1109 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1110 ASSERT_NO_FATAL_FAILURE(
1111 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1112
1113 const float rot = M_SQRT1_2; // 45 degrees
1114 const float trans = M_SQRT2 * 16.0f;
1115 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1116
1117 auto shot = screenshot();
1118 // check a 8x8 region inside each color
1119 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1120 const int32_t halfL = 4;
1121 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1122 };
1123 const int32_t unit = int32_t(trans / 2);
1124 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1125 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1126 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1127 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1128}
1129
1130TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1131 sp<SurfaceControl> layer;
1132 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1133 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1134
1135 // setMatrix is applied after any pending resize, unlike setPosition
1136 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1137 {
1138 SCOPED_TRACE("resize pending");
1139 auto shot = screenshot();
1140 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1141 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1142 }
1143
1144 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1145 {
1146 SCOPED_TRACE("resize applied");
1147 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1148 }
1149}
1150
1151TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1152 sp<SurfaceControl> layer;
1153 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1154 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1155
1156 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1157 Transaction()
1158 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1159 .setSize(layer, 64, 64)
1160 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1161 .apply();
1162 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1163}
1164
Chia-I Wua56b2042017-11-01 15:16:35 -07001165TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1166 sp<SurfaceControl> layer;
1167 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1168 ASSERT_NO_FATAL_FAILURE(
1169 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1170
1171 // XXX SCALE_CROP is not respected; calling setSize and
1172 // setOverrideScalingMode in separate transactions does not work
1173 // (b/69315456)
1174 Transaction()
1175 .setSize(layer, 64, 16)
1176 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1177 .apply();
1178 {
1179 SCOPED_TRACE("SCALE_TO_WINDOW");
1180 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1181 Color::WHITE, true /* filtered */);
1182 }
1183}
1184
Chia-I Wu04dcca82017-11-02 08:30:27 -07001185TEST_F(LayerTransactionTest, SetCropBasic) {
1186 sp<SurfaceControl> layer;
1187 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1188 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1189 const Rect crop(8, 8, 24, 24);
1190
1191 Transaction().setCrop(layer, crop).apply();
1192 auto shot = screenshot();
1193 shot->expectColor(crop, Color::RED);
1194 shot->expectBorder(crop, Color::BLACK);
1195}
1196
1197TEST_F(LayerTransactionTest, SetCropEmpty) {
1198 sp<SurfaceControl> layer;
1199 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1200 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1201
1202 {
1203 SCOPED_TRACE("empty rect");
1204 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1205 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1206 }
1207
1208 {
1209 SCOPED_TRACE("negative rect");
1210 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1211 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1212 }
1213}
1214
1215TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1216 sp<SurfaceControl> layer;
1217 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1218 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1219
1220 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1221 auto shot = screenshot();
1222 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1223 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1224}
1225
1226TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1227 sp<SurfaceControl> layer;
1228 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1229 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1230
1231 const Point position(32, 32);
1232 const Rect crop(8, 8, 24, 24);
1233 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1234 auto shot = screenshot();
1235 shot->expectColor(crop + position, Color::RED);
1236 shot->expectBorder(crop + position, Color::BLACK);
1237}
1238
1239TEST_F(LayerTransactionTest, SetCropWithScale) {
1240 sp<SurfaceControl> layer;
1241 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1242 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1243
1244 // crop is affected by matrix
1245 Transaction()
1246 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1247 .setCrop(layer, Rect(8, 8, 24, 24))
1248 .apply();
1249 auto shot = screenshot();
1250 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1251 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1252}
1253
1254TEST_F(LayerTransactionTest, SetCropWithResize) {
1255 sp<SurfaceControl> layer;
1256 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1257 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1258
1259 // setCrop is applied immediately by default, with or without resize pending
1260 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1261 {
1262 SCOPED_TRACE("resize pending");
1263 auto shot = screenshot();
1264 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1265 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1266 }
1267
1268 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1269 {
1270 SCOPED_TRACE("resize applied");
1271 auto shot = screenshot();
1272 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1273 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1274 }
1275}
1276
1277TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1278 sp<SurfaceControl> layer;
1279 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1280 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1281
1282 // request setCrop to be applied with the next resize
1283 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1284 {
1285 SCOPED_TRACE("waiting for next resize");
1286 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1287 }
1288
1289 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1290 {
1291 SCOPED_TRACE("pending crop modified");
1292 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1293 }
1294
1295 Transaction().setSize(layer, 16, 16).apply();
1296 {
1297 SCOPED_TRACE("resize pending");
1298 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1299 }
1300
1301 // finally resize
1302 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1303 {
1304 SCOPED_TRACE("new crop applied");
1305 auto shot = screenshot();
1306 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1307 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1308 }
1309}
1310
1311TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1312 sp<SurfaceControl> layer;
1313 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1314 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1315
1316 // setCrop is not immediate even with SCALE_TO_WINDOW override
1317 Transaction()
1318 .setCrop(layer, Rect(4, 4, 12, 12))
1319 .setSize(layer, 16, 16)
1320 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1321 .setGeometryAppliesWithResize(layer)
1322 .apply();
1323 {
1324 SCOPED_TRACE("new crop pending");
1325 auto shot = screenshot();
1326 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1327 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1328 }
1329
1330 // XXX crop is never latched without other geometry change (b/69315677)
1331 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1332 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1333 Transaction().setPosition(layer, 0, 0).apply();
1334 {
1335 SCOPED_TRACE("new crop applied");
1336 auto shot = screenshot();
1337 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1338 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1339 }
1340}
1341
Chia-I Wucdd71a52017-11-02 08:30:27 -07001342TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1343 sp<SurfaceControl> layer;
1344 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1345 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1346 const Rect crop(8, 8, 24, 24);
1347
1348 // same as in SetCropBasic
1349 Transaction().setFinalCrop(layer, crop).apply();
1350 auto shot = screenshot();
1351 shot->expectColor(crop, Color::RED);
1352 shot->expectBorder(crop, Color::BLACK);
1353}
1354
1355TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1356 sp<SurfaceControl> layer;
1357 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1358 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1359
1360 // same as in SetCropEmpty
1361 {
1362 SCOPED_TRACE("empty rect");
1363 Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1364 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1365 }
1366
1367 {
1368 SCOPED_TRACE("negative rect");
1369 Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1370 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1371 }
1372}
1373
1374TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1375 sp<SurfaceControl> layer;
1376 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1377 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1378
1379 // same as in SetCropOutOfBounds
1380 Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1381 auto shot = screenshot();
1382 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1383 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1384}
1385
1386TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1387 sp<SurfaceControl> layer;
1388 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1389 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1390
1391 // final crop is applied post-translation
1392 Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1393 auto shot = screenshot();
1394 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1395 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1396}
1397
1398TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1399 sp<SurfaceControl> layer;
1400 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1401 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1402
1403 // final crop is not affected by matrix
1404 Transaction()
1405 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1406 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1407 .apply();
1408 auto shot = screenshot();
1409 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1410 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1411}
1412
1413TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1414 sp<SurfaceControl> layer;
1415 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1416 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1417
1418 // same as in SetCropWithResize
1419 Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1420 {
1421 SCOPED_TRACE("resize pending");
1422 auto shot = screenshot();
1423 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1424 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1425 }
1426
1427 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1428 {
1429 SCOPED_TRACE("resize applied");
1430 auto shot = screenshot();
1431 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1432 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1433 }
1434}
1435
1436TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1437 sp<SurfaceControl> layer;
1438 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1439 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1440
1441 // same as in SetCropWithNextResize
1442 Transaction()
1443 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1444 .setGeometryAppliesWithResize(layer)
1445 .apply();
1446 {
1447 SCOPED_TRACE("waiting for next resize");
1448 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1449 }
1450
1451 Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1452 {
1453 SCOPED_TRACE("pending final crop modified");
1454 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1455 }
1456
1457 Transaction().setSize(layer, 16, 16).apply();
1458 {
1459 SCOPED_TRACE("resize pending");
1460 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1461 }
1462
1463 // finally resize
1464 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1465 {
1466 SCOPED_TRACE("new final crop applied");
1467 auto shot = screenshot();
1468 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1469 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1470 }
1471}
1472
1473TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1474 sp<SurfaceControl> layer;
1475 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1476 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1477
1478 // same as in SetCropWithNextResizeScaleToWindow
1479 Transaction()
1480 .setFinalCrop(layer, Rect(4, 4, 12, 12))
1481 .setSize(layer, 16, 16)
1482 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1483 .setGeometryAppliesWithResize(layer)
1484 .apply();
1485 {
1486 SCOPED_TRACE("new final crop pending");
1487 auto shot = screenshot();
1488 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1489 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1490 }
1491
1492 // XXX final crop is never latched without other geometry change (b/69315677)
1493 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1494 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1495 Transaction().setPosition(layer, 0, 0).apply();
1496 {
1497 SCOPED_TRACE("new final crop applied");
1498 auto shot = screenshot();
1499 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1500 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1501 }
1502}
1503
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001504class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001505protected:
1506 virtual void SetUp() {
1507 mComposerClient = new SurfaceComposerClient;
1508 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1509
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001510 sp<IBinder> display(
1511 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001512 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001513 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001514
1515 ssize_t displayWidth = info.w;
1516 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001517
1518 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001519 mBGSurfaceControl =
1520 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1521 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001522 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001523 ASSERT_TRUE(mBGSurfaceControl->isValid());
1524 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1525
1526 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001527 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1528 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001529 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001530 ASSERT_TRUE(mFGSurfaceControl->isValid());
1531
1532 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1533
1534 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001535 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1536 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001537 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001538 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1539
1540 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1541
Robert Carr4cdc58f2017-08-23 14:22:20 -07001542 asTransaction([&](Transaction& t) {
1543 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001544
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001545 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001546
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001547 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1548 .setPosition(mFGSurfaceControl, 64, 64)
1549 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001550
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001551 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1552 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1553 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001554 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001555 }
1556
1557 virtual void TearDown() {
1558 mComposerClient->dispose();
1559 mBGSurfaceControl = 0;
1560 mFGSurfaceControl = 0;
1561 mSyncSurfaceControl = 0;
1562 mComposerClient = 0;
1563 }
1564
1565 void waitForPostedBuffers() {
1566 // Since the sync surface is in synchronous mode (i.e. double buffered)
1567 // posting three buffers to it should ensure that at least two
1568 // SurfaceFlinger::handlePageFlip calls have been made, which should
1569 // guaranteed that a buffer posted to another Surface has been retired.
1570 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1571 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1572 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1573 }
1574
Robert Carr4cdc58f2017-08-23 14:22:20 -07001575 void asTransaction(const std::function<void(Transaction&)>& exec) {
1576 Transaction t;
1577 exec(t);
1578 t.apply(true);
1579 }
1580
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001581 sp<SurfaceComposerClient> mComposerClient;
1582 sp<SurfaceControl> mBGSurfaceControl;
1583 sp<SurfaceControl> mFGSurfaceControl;
1584
1585 // This surface is used to ensure that the buffers posted to
1586 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1587 sp<SurfaceControl> mSyncSurfaceControl;
1588};
1589
Robert Carr7f619b22017-11-06 12:56:35 -08001590TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1591 sp<ScreenCapture> sc;
1592
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001593 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1594 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001595 fillSurfaceRGBA8(relative, 10, 10, 10);
1596 waitForPostedBuffers();
1597
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001598 Transaction{}
1599 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001600 .setPosition(relative, 64, 64)
1601 .apply();
1602
1603 {
1604 // The relative should be on top of the FG control.
1605 ScreenCapture::captureScreen(&sc);
1606 sc->checkPixel(64, 64, 10, 10, 10);
1607 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001608 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001609
1610 {
1611 // Nothing should change at this point.
1612 ScreenCapture::captureScreen(&sc);
1613 sc->checkPixel(64, 64, 10, 10, 10);
1614 }
1615
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001616 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001617
1618 {
1619 // Ensure that the relative was actually hidden, rather than
1620 // being left in the detached but visible state.
1621 ScreenCapture::captureScreen(&sc);
1622 sc->expectFGColor(64, 64);
1623 }
1624}
1625
Robert Carr8d5227b2017-03-16 15:41:03 -07001626class GeometryLatchingTest : public LayerUpdateTest {
1627protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001628 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001629 SCOPED_TRACE(trace);
1630 ScreenCapture::captureScreen(&sc);
1631 // We find the leading edge of the FG surface.
1632 sc->expectFGColor(127, 127);
1633 sc->expectBGColor(128, 128);
1634 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001635
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001636 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001637
1638 void unlockFGBuffer() {
1639 sp<Surface> s = mFGSurfaceControl->getSurface();
1640 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1641 waitForPostedBuffers();
1642 }
1643
Robert Carr8d5227b2017-03-16 15:41:03 -07001644 void completeFGResize() {
1645 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1646 waitForPostedBuffers();
1647 }
1648 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001649 asTransaction([&](Transaction& t) {
1650 t.setSize(mFGSurfaceControl, 64, 64);
1651 t.setPosition(mFGSurfaceControl, 64, 64);
1652 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1653 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1654 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001655
1656 EXPECT_INITIAL_STATE("After restoring initial state");
1657 }
1658 sp<ScreenCapture> sc;
1659};
1660
Robert Carr8d5227b2017-03-16 15:41:03 -07001661class CropLatchingTest : public GeometryLatchingTest {
1662protected:
1663 void EXPECT_CROPPED_STATE(const char* trace) {
1664 SCOPED_TRACE(trace);
1665 ScreenCapture::captureScreen(&sc);
1666 // The edge should be moved back one pixel by our crop.
1667 sc->expectFGColor(126, 126);
1668 sc->expectBGColor(127, 127);
1669 sc->expectBGColor(128, 128);
1670 }
chaviw59f5c562017-06-28 16:39:06 -07001671
1672 void EXPECT_RESIZE_STATE(const char* trace) {
1673 SCOPED_TRACE(trace);
1674 ScreenCapture::captureScreen(&sc);
1675 // The FG is now resized too 128,128 at 64,64
1676 sc->expectFGColor(64, 64);
1677 sc->expectFGColor(191, 191);
1678 sc->expectBGColor(192, 192);
1679 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001680};
1681
Robert Carr7bf247e2017-05-18 14:02:49 -07001682// In this test we ensure that setGeometryAppliesWithResize actually demands
1683// a buffer of the new size, and not just any size.
1684TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1685 EXPECT_INITIAL_STATE("before anything");
1686 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001687 asTransaction([&](Transaction& t) {
1688 t.setSize(mFGSurfaceControl, 128, 128);
1689 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1690 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001691
1692 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1693
1694 restoreInitialState();
1695
1696 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1697 // initiating the resize.
1698 lockAndFillFGBuffer();
1699
Robert Carr4cdc58f2017-08-23 14:22:20 -07001700 asTransaction([&](Transaction& t) {
1701 t.setSize(mFGSurfaceControl, 128, 128);
1702 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1703 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1704 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001705
1706 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1707
1708 // We now submit our old buffer, at the old size, and ensure it doesn't
1709 // trigger geometry latching.
1710 unlockFGBuffer();
1711
1712 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1713
1714 completeFGResize();
1715
1716 EXPECT_CROPPED_STATE("after the resize finishes");
1717}
1718
Pablo Ceballos05289c22016-04-14 15:49:55 -07001719TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1720 sp<ScreenCapture> sc;
1721 {
1722 SCOPED_TRACE("before anything");
1723 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001724 sc->expectBGColor(32, 32);
1725 sc->expectFGColor(96, 96);
1726 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001727 }
1728
1729 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001730 asTransaction([&](Transaction& t) {
1731 t.setAlpha(mFGSurfaceControl, 0.75);
1732 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001733 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001734 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001735
Robert Carr4cdc58f2017-08-23 14:22:20 -07001736 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001737 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001738 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001739 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001740 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001741
1742 {
1743 SCOPED_TRACE("before any trigger");
1744 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001745 sc->expectBGColor(32, 32);
1746 sc->expectFGColor(96, 96);
1747 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001748 }
1749
1750 // should trigger the first deferred transaction, but not the second one
1751 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1752 {
1753 SCOPED_TRACE("after first trigger");
1754 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001755 sc->expectBGColor(32, 32);
1756 sc->checkPixel(96, 96, 162, 63, 96);
1757 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001758 }
1759
1760 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001761 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001762
1763 // trigger the second deferred transaction
1764 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1765 {
1766 SCOPED_TRACE("after second trigger");
1767 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001768 sc->expectBGColor(32, 32);
1769 sc->expectBGColor(96, 96);
1770 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001771 }
1772}
1773
Robert Carre392b552017-09-19 12:16:05 -07001774TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1775 sp<ScreenCapture> sc;
1776
1777 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001778 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1779 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1780 sp<SurfaceControl> childBuffer =
1781 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1782 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001783 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1784
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001785 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001786
1787 {
1788 ScreenCapture::captureScreen(&sc);
1789 sc->expectChildColor(73, 73);
1790 sc->expectFGColor(74, 74);
1791 }
1792
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001793 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001794
1795 {
1796 ScreenCapture::captureScreen(&sc);
1797 sc->expectChildColor(73, 73);
1798 sc->expectChildColor(74, 74);
1799 }
1800}
1801
Robert Carr2c5f6d22017-09-26 12:30:35 -07001802TEST_F(LayerUpdateTest, MergingTransactions) {
1803 sp<ScreenCapture> sc;
1804 {
1805 SCOPED_TRACE("before move");
1806 ScreenCapture::captureScreen(&sc);
1807 sc->expectBGColor(0, 12);
1808 sc->expectFGColor(75, 75);
1809 sc->expectBGColor(145, 145);
1810 }
1811
1812 Transaction t1, t2;
1813 t1.setPosition(mFGSurfaceControl, 128, 128);
1814 t2.setPosition(mFGSurfaceControl, 0, 0);
1815 // We expect that the position update from t2 now
1816 // overwrites the position update from t1.
1817 t1.merge(std::move(t2));
1818 t1.apply();
1819
1820 {
1821 ScreenCapture::captureScreen(&sc);
1822 sc->expectFGColor(1, 1);
1823 }
1824}
1825
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826class ChildLayerTest : public LayerUpdateTest {
1827protected:
1828 void SetUp() override {
1829 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001830 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1831 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001832 fillSurfaceRGBA8(mChild, 200, 200, 200);
1833
1834 {
1835 SCOPED_TRACE("before anything");
1836 ScreenCapture::captureScreen(&mCapture);
1837 mCapture->expectChildColor(64, 64);
1838 }
1839 }
1840 void TearDown() override {
1841 LayerUpdateTest::TearDown();
1842 mChild = 0;
1843 }
1844
1845 sp<SurfaceControl> mChild;
1846 sp<ScreenCapture> mCapture;
1847};
1848
1849TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001850 asTransaction([&](Transaction& t) {
1851 t.show(mChild);
1852 t.setPosition(mChild, 10, 10);
1853 t.setPosition(mFGSurfaceControl, 64, 64);
1854 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001855
1856 {
1857 ScreenCapture::captureScreen(&mCapture);
1858 // Top left of foreground must now be visible
1859 mCapture->expectFGColor(64, 64);
1860 // But 10 pixels in we should see the child surface
1861 mCapture->expectChildColor(74, 74);
1862 // And 10 more pixels we should be back to the foreground surface
1863 mCapture->expectFGColor(84, 84);
1864 }
1865
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001866 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867
1868 {
1869 ScreenCapture::captureScreen(&mCapture);
1870 // Top left of foreground should now be at 0, 0
1871 mCapture->expectFGColor(0, 0);
1872 // But 10 pixels in we should see the child surface
1873 mCapture->expectChildColor(10, 10);
1874 // And 10 more pixels we should be back to the foreground surface
1875 mCapture->expectFGColor(20, 20);
1876 }
1877}
1878
Robert Carr41b08b52017-06-01 16:11:34 -07001879TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001880 asTransaction([&](Transaction& t) {
1881 t.show(mChild);
1882 t.setPosition(mChild, 0, 0);
1883 t.setPosition(mFGSurfaceControl, 0, 0);
1884 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1885 });
Robert Carr41b08b52017-06-01 16:11:34 -07001886
1887 {
1888 ScreenCapture::captureScreen(&mCapture);
1889 mCapture->expectChildColor(0, 0);
1890 mCapture->expectChildColor(4, 4);
1891 mCapture->expectBGColor(5, 5);
1892 }
1893}
1894
1895TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001896 asTransaction([&](Transaction& t) {
1897 t.show(mChild);
1898 t.setPosition(mChild, 0, 0);
1899 t.setPosition(mFGSurfaceControl, 0, 0);
1900 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1901 });
Robert Carr41b08b52017-06-01 16:11:34 -07001902
1903 {
1904 ScreenCapture::captureScreen(&mCapture);
1905 mCapture->expectChildColor(0, 0);
1906 mCapture->expectChildColor(4, 4);
1907 mCapture->expectBGColor(5, 5);
1908 }
1909}
1910
Robert Carr1f0a16a2016-10-24 16:27:39 -07001911TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001912 asTransaction([&](Transaction& t) {
1913 t.show(mChild);
1914 t.setPosition(mFGSurfaceControl, 0, 0);
1915 t.setPosition(mChild, 63, 63);
1916 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001917
1918 {
1919 ScreenCapture::captureScreen(&mCapture);
1920 mCapture->expectFGColor(0, 0);
1921 // Last pixel in foreground should now be the child.
1922 mCapture->expectChildColor(63, 63);
1923 // But the child should be constrained and the next pixel
1924 // must be the background
1925 mCapture->expectBGColor(64, 64);
1926 }
1927}
1928
1929TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001930 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001931
1932 // Find the boundary between the parent and child
1933 {
1934 ScreenCapture::captureScreen(&mCapture);
1935 mCapture->expectChildColor(9, 9);
1936 mCapture->expectFGColor(10, 10);
1937 }
1938
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001939 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001940
1941 // The boundary should be twice as far from the origin now.
1942 // The pixels from the last test should all be child now
1943 {
1944 ScreenCapture::captureScreen(&mCapture);
1945 mCapture->expectChildColor(9, 9);
1946 mCapture->expectChildColor(10, 10);
1947 mCapture->expectChildColor(19, 19);
1948 mCapture->expectFGColor(20, 20);
1949 }
1950}
Robert Carr9524cb32017-02-13 11:32:32 -08001951
Robert Carr6452f122017-03-21 10:41:29 -07001952TEST_F(ChildLayerTest, ChildLayerAlpha) {
1953 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1954 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1955 fillSurfaceRGBA8(mChild, 0, 254, 0);
1956 waitForPostedBuffers();
1957
Robert Carr4cdc58f2017-08-23 14:22:20 -07001958 asTransaction([&](Transaction& t) {
1959 t.show(mChild);
1960 t.setPosition(mChild, 0, 0);
1961 t.setPosition(mFGSurfaceControl, 0, 0);
1962 });
Robert Carr6452f122017-03-21 10:41:29 -07001963
1964 {
1965 ScreenCapture::captureScreen(&mCapture);
1966 // Unblended child color
1967 mCapture->checkPixel(0, 0, 0, 254, 0);
1968 }
1969
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001970 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001971
1972 {
1973 ScreenCapture::captureScreen(&mCapture);
1974 // Child and BG blended.
1975 mCapture->checkPixel(0, 0, 127, 127, 0);
1976 }
1977
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001978 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001979
1980 {
1981 ScreenCapture::captureScreen(&mCapture);
1982 // Child and BG blended.
1983 mCapture->checkPixel(0, 0, 95, 64, 95);
1984 }
1985}
1986
Robert Carr9524cb32017-02-13 11:32:32 -08001987TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001988 asTransaction([&](Transaction& t) {
1989 t.show(mChild);
1990 t.setPosition(mChild, 10, 10);
1991 t.setPosition(mFGSurfaceControl, 64, 64);
1992 });
Robert Carr9524cb32017-02-13 11:32:32 -08001993
1994 {
1995 ScreenCapture::captureScreen(&mCapture);
1996 // Top left of foreground must now be visible
1997 mCapture->expectFGColor(64, 64);
1998 // But 10 pixels in we should see the child surface
1999 mCapture->expectChildColor(74, 74);
2000 // And 10 more pixels we should be back to the foreground surface
2001 mCapture->expectFGColor(84, 84);
2002 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002003
2004 asTransaction([&](Transaction& t) {
2005 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2006 });
2007
Robert Carr9524cb32017-02-13 11:32:32 -08002008 {
2009 ScreenCapture::captureScreen(&mCapture);
2010 mCapture->expectFGColor(64, 64);
2011 // In reparenting we should have exposed the entire foreground surface.
2012 mCapture->expectFGColor(74, 74);
2013 // And the child layer should now begin at 10, 10 (since the BG
2014 // layer is at (0, 0)).
2015 mCapture->expectBGColor(9, 9);
2016 mCapture->expectChildColor(10, 10);
2017 }
2018}
2019
chaviw161410b02017-07-27 10:46:08 -07002020TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002021 asTransaction([&](Transaction& t) {
2022 t.show(mChild);
2023 t.setPosition(mChild, 10, 10);
2024 t.setPosition(mFGSurfaceControl, 64, 64);
2025 });
Robert Carr9524cb32017-02-13 11:32:32 -08002026
2027 {
2028 ScreenCapture::captureScreen(&mCapture);
2029 // Top left of foreground must now be visible
2030 mCapture->expectFGColor(64, 64);
2031 // But 10 pixels in we should see the child surface
2032 mCapture->expectChildColor(74, 74);
2033 // And 10 more pixels we should be back to the foreground surface
2034 mCapture->expectFGColor(84, 84);
2035 }
2036
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002037 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002038
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002039 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002040
chaviw161410b02017-07-27 10:46:08 -07002041 // Since the child has the same client as the parent, it will not get
2042 // detached and will be hidden.
2043 {
2044 ScreenCapture::captureScreen(&mCapture);
2045 mCapture->expectFGColor(64, 64);
2046 mCapture->expectFGColor(74, 74);
2047 mCapture->expectFGColor(84, 84);
2048 }
2049}
2050
2051TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2052 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002053 sp<SurfaceControl> mChildNewClient =
2054 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2055 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002056
Peiyong Lin566a3b42018-01-09 18:22:43 -08002057 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002058 ASSERT_TRUE(mChildNewClient->isValid());
2059
2060 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2061
Robert Carr4cdc58f2017-08-23 14:22:20 -07002062 asTransaction([&](Transaction& t) {
2063 t.hide(mChild);
2064 t.show(mChildNewClient);
2065 t.setPosition(mChildNewClient, 10, 10);
2066 t.setPosition(mFGSurfaceControl, 64, 64);
2067 });
chaviw161410b02017-07-27 10:46:08 -07002068
2069 {
2070 ScreenCapture::captureScreen(&mCapture);
2071 // Top left of foreground must now be visible
2072 mCapture->expectFGColor(64, 64);
2073 // But 10 pixels in we should see the child surface
2074 mCapture->expectChildColor(74, 74);
2075 // And 10 more pixels we should be back to the foreground surface
2076 mCapture->expectFGColor(84, 84);
2077 }
2078
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002079 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002080
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002081 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002082
Robert Carr9524cb32017-02-13 11:32:32 -08002083 // Nothing should have changed.
2084 {
2085 ScreenCapture::captureScreen(&mCapture);
2086 mCapture->expectFGColor(64, 64);
2087 mCapture->expectChildColor(74, 74);
2088 mCapture->expectFGColor(84, 84);
2089 }
2090}
2091
Robert Carr9b429f42017-04-17 14:56:57 -07002092TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002093 asTransaction([&](Transaction& t) {
2094 t.show(mChild);
2095 t.setPosition(mChild, 0, 0);
2096 t.setPosition(mFGSurfaceControl, 0, 0);
2097 });
Robert Carr9b429f42017-04-17 14:56:57 -07002098
2099 {
2100 ScreenCapture::captureScreen(&mCapture);
2101 // We've positioned the child in the top left.
2102 mCapture->expectChildColor(0, 0);
2103 // But it's only 10x10.
2104 mCapture->expectFGColor(10, 10);
2105 }
2106
Robert Carr4cdc58f2017-08-23 14:22:20 -07002107 asTransaction([&](Transaction& t) {
2108 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2109 // We cause scaling by 2.
2110 t.setSize(mFGSurfaceControl, 128, 128);
2111 });
Robert Carr9b429f42017-04-17 14:56:57 -07002112
2113 {
2114 ScreenCapture::captureScreen(&mCapture);
2115 // We've positioned the child in the top left.
2116 mCapture->expectChildColor(0, 0);
2117 mCapture->expectChildColor(10, 10);
2118 mCapture->expectChildColor(19, 19);
2119 // And now it should be scaled all the way to 20x20
2120 mCapture->expectFGColor(20, 20);
2121 }
2122}
2123
Robert Carr1725eee2017-04-26 18:32:15 -07002124// Regression test for b/37673612
2125TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002126 asTransaction([&](Transaction& t) {
2127 t.show(mChild);
2128 t.setPosition(mChild, 0, 0);
2129 t.setPosition(mFGSurfaceControl, 0, 0);
2130 });
Robert Carr1725eee2017-04-26 18:32:15 -07002131
2132 {
2133 ScreenCapture::captureScreen(&mCapture);
2134 // We've positioned the child in the top left.
2135 mCapture->expectChildColor(0, 0);
2136 // But it's only 10x10.
2137 mCapture->expectFGColor(10, 10);
2138 }
Robert Carr1725eee2017-04-26 18:32:15 -07002139 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2140 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002141 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002142 sp<Surface> s = mFGSurfaceControl->getSurface();
2143 auto anw = static_cast<ANativeWindow*>(s.get());
2144 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2145 native_window_set_buffers_dimensions(anw, 64, 128);
2146 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2147 waitForPostedBuffers();
2148
2149 {
2150 // The child should still be in the same place and not have any strange scaling as in
2151 // b/37673612.
2152 ScreenCapture::captureScreen(&mCapture);
2153 mCapture->expectChildColor(0, 0);
2154 mCapture->expectFGColor(10, 10);
2155 }
2156}
2157
Dan Stoza412903f2017-04-27 13:42:17 -07002158TEST_F(ChildLayerTest, Bug36858924) {
2159 // Destroy the child layer
2160 mChild.clear();
2161
2162 // Now recreate it as hidden
2163 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2164 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2165 mFGSurfaceControl.get());
2166
2167 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002168 asTransaction([&](Transaction& t) {
2169 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002170 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002171 t.show(mChild);
2172 });
Dan Stoza412903f2017-04-27 13:42:17 -07002173
2174 // Render the foreground surface a few times
2175 //
2176 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2177 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2178 // never acquire/release the first buffer
2179 ALOGI("Filling 1");
2180 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2181 ALOGI("Filling 2");
2182 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2183 ALOGI("Filling 3");
2184 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2185 ALOGI("Filling 4");
2186 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2187}
2188
chaviwf1961f72017-09-18 16:41:07 -07002189TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002190 asTransaction([&](Transaction& t) {
2191 t.show(mChild);
2192 t.setPosition(mChild, 10, 10);
2193 t.setPosition(mFGSurfaceControl, 64, 64);
2194 });
chaviw06178942017-07-27 10:25:59 -07002195
2196 {
2197 ScreenCapture::captureScreen(&mCapture);
2198 // Top left of foreground must now be visible
2199 mCapture->expectFGColor(64, 64);
2200 // But 10 pixels in we should see the child surface
2201 mCapture->expectChildColor(74, 74);
2202 // And 10 more pixels we should be back to the foreground surface
2203 mCapture->expectFGColor(84, 84);
2204 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002205
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002206 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002207
chaviw06178942017-07-27 10:25:59 -07002208 {
2209 ScreenCapture::captureScreen(&mCapture);
2210 mCapture->expectFGColor(64, 64);
2211 // In reparenting we should have exposed the entire foreground surface.
2212 mCapture->expectFGColor(74, 74);
2213 // And the child layer should now begin at 10, 10 (since the BG
2214 // layer is at (0, 0)).
2215 mCapture->expectBGColor(9, 9);
2216 mCapture->expectChildColor(10, 10);
2217 }
2218}
2219
chaviwf1961f72017-09-18 16:41:07 -07002220TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002221 asTransaction([&](Transaction& t) {
2222 t.show(mChild);
2223 t.setPosition(mChild, 10, 10);
2224 t.setPosition(mFGSurfaceControl, 64, 64);
2225 });
chaviwf1961f72017-09-18 16:41:07 -07002226
2227 {
2228 ScreenCapture::captureScreen(&mCapture);
2229 // Top left of foreground must now be visible
2230 mCapture->expectFGColor(64, 64);
2231 // But 10 pixels in we should see the child surface
2232 mCapture->expectChildColor(74, 74);
2233 // And 10 more pixels we should be back to the foreground surface
2234 mCapture->expectFGColor(84, 84);
2235 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002236 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002237 {
2238 ScreenCapture::captureScreen(&mCapture);
2239 // Nothing should have changed.
2240 mCapture->expectFGColor(64, 64);
2241 mCapture->expectChildColor(74, 74);
2242 mCapture->expectFGColor(84, 84);
2243 }
2244}
2245
2246TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002247 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2248 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002249 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002250 ASSERT_TRUE(newSurface->isValid());
2251
2252 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002253 asTransaction([&](Transaction& t) {
2254 t.hide(mChild);
2255 t.show(newSurface);
2256 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002257 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002258 t.setPosition(mFGSurfaceControl, 64, 64);
2259 });
chaviwf1961f72017-09-18 16:41:07 -07002260
2261 {
2262 ScreenCapture::captureScreen(&mCapture);
2263 // Top left of foreground must now be visible
2264 mCapture->expectFGColor(64, 64);
2265 // At 10, 10 we should see the new surface
2266 mCapture->checkPixel(10, 10, 63, 195, 63);
2267 }
2268
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002269 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002270
2271 {
2272 ScreenCapture::captureScreen(&mCapture);
2273 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2274 // mFGSurface, putting it at 74, 74.
2275 mCapture->expectFGColor(64, 64);
2276 mCapture->checkPixel(74, 74, 63, 195, 63);
2277 mCapture->expectFGColor(84, 84);
2278 }
2279}
2280
chaviwc9674332017-08-28 12:32:18 -07002281TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002282 sp<SurfaceControl> grandchild =
2283 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2284 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002285 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2286
2287 {
2288 ScreenCapture::captureScreen(&mCapture);
2289 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2290 // which begins at 64, 64
2291 mCapture->checkPixel(64, 64, 50, 50, 50);
2292 }
2293}
2294
Robert Carr503c7042017-09-27 15:06:08 -07002295TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002296 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2297 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002298 fillSurfaceRGBA8(relative, 255, 255, 255);
2299
2300 Transaction t;
2301 t.setLayer(relative, INT32_MAX)
2302 .setRelativeLayer(mChild, relative->getHandle(), 1)
2303 .setPosition(mFGSurfaceControl, 0, 0)
2304 .apply(true);
2305
2306 // We expect that the child should have been elevated above our
2307 // INT_MAX layer even though it's not a child of it.
2308 {
2309 ScreenCapture::captureScreen(&mCapture);
2310 mCapture->expectChildColor(0, 0);
2311 mCapture->expectChildColor(9, 9);
2312 mCapture->checkPixel(10, 10, 255, 255, 255);
2313 }
2314}
2315
chaviwa76b2712017-09-20 12:02:26 -07002316class ScreenCaptureTest : public LayerUpdateTest {
2317protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002318 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002319};
2320
2321TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2322 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002323 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002324 mCapture->expectBGColor(0, 0);
2325 // Doesn't capture FG layer which is at 64, 64
2326 mCapture->expectBGColor(64, 64);
2327}
2328
2329TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2330 auto fgHandle = mFGSurfaceControl->getHandle();
2331
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002332 sp<SurfaceControl> child =
2333 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2334 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002335 fillSurfaceRGBA8(child, 200, 200, 200);
2336
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002337 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002338
2339 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002340 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002341 mCapture->expectFGColor(10, 10);
2342 mCapture->expectChildColor(0, 0);
2343}
2344
Robert Carr578038f2018-03-09 12:25:24 -08002345TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2346 auto fgHandle = mFGSurfaceControl->getHandle();
2347
2348 sp<SurfaceControl> child =
2349 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2350 0, mFGSurfaceControl.get());
2351 fillSurfaceRGBA8(child, 200, 200, 200);
2352
2353 SurfaceComposerClient::Transaction().show(child).apply(true);
2354
2355 // Captures mFGSurfaceControl's child
2356 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2357 mCapture->checkPixel(10, 10, 0, 0, 0);
2358 mCapture->expectChildColor(0, 0);
2359}
2360
2361
2362// In the following tests we verify successful skipping of a parent layer,
2363// so we use the same verification logic and only change how we mutate
2364// the parent layer to verify that various properties are ignored.
2365class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2366public:
2367 void SetUp() override {
2368 LayerUpdateTest::SetUp();
2369
2370 mChild =
2371 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2372 0, mFGSurfaceControl.get());
2373 fillSurfaceRGBA8(mChild, 200, 200, 200);
2374
2375 SurfaceComposerClient::Transaction().show(mChild).apply(true);
2376 }
2377
2378 void verify() {
2379 auto fgHandle = mFGSurfaceControl->getHandle();
2380 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2381 mCapture->checkPixel(10, 10, 0, 0, 0);
2382 mCapture->expectChildColor(0, 0);
2383 }
2384
2385 std::unique_ptr<ScreenCapture> mCapture;
2386 sp<SurfaceControl> mChild;
2387};
2388
2389TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2390
2391 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2392
2393 // Even though the parent is hidden we should still capture the child.
2394 verify();
2395}
2396
2397TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
2398
2399 SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true);
2400
2401 // Even though the parent is cropped out we should still capture the child.
2402 verify();
2403}
2404
2405TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2406
2407 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2408
2409 // We should not inherit the parent scaling.
2410 verify();
2411}
2412
Robert Carr15eae092018-03-23 13:43:53 -07002413TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
2414 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2415
2416 // Even though the parent is hidden we should still capture the child.
2417 verify();
2418
2419 // Verify everything was properly hidden when rendering the full-screen.
2420 screenshot()->expectBGColor(0,0);
2421}
2422
2423
chaviwa76b2712017-09-20 12:02:26 -07002424TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2425 auto fgHandle = mFGSurfaceControl->getHandle();
2426
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002427 sp<SurfaceControl> child =
2428 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2429 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002430 fillSurfaceRGBA8(child, 200, 200, 200);
2431
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002432 sp<SurfaceControl> grandchild =
2433 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2434 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002435
2436 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2437 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002438 .show(child)
2439 .setPosition(grandchild, 5, 5)
2440 .show(grandchild)
2441 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002442
2443 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002444 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002445 mCapture->expectFGColor(10, 10);
2446 mCapture->expectChildColor(0, 0);
2447 mCapture->checkPixel(5, 5, 50, 50, 50);
2448}
2449
2450TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002451 sp<SurfaceControl> child =
2452 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2453 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002454 fillSurfaceRGBA8(child, 200, 200, 200);
2455 auto childHandle = child->getHandle();
2456
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002457 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002458
2459 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002460 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07002461 mCapture->expectChildColor(0, 0);
2462 mCapture->expectChildColor(9, 9);
2463}
2464
2465TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002466 sp<SurfaceControl> child =
2467 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2468 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002469 fillSurfaceRGBA8(child, 200, 200, 200);
2470 auto childHandle = child->getHandle();
2471
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002472 sp<SurfaceControl> grandchild =
2473 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2474 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002475 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2476
2477 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002478 .show(child)
2479 .setPosition(grandchild, 5, 5)
2480 .show(grandchild)
2481 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002482
2483 auto grandchildHandle = grandchild->getHandle();
2484
2485 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002486 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07002487 mCapture->checkPixel(0, 0, 50, 50, 50);
2488 mCapture->checkPixel(4, 4, 50, 50, 50);
2489}
2490
chaviw7206d492017-11-10 16:16:12 -08002491TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002492 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2493 PIXEL_FORMAT_RGBA_8888, 0);
2494 sp<SurfaceControl> blueLayer =
2495 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2496 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002497
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002498 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2499 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002500
2501 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002502 .setLayer(redLayer, INT32_MAX - 1)
2503 .show(redLayer)
2504 .show(blueLayer)
2505 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002506
2507 auto redLayerHandle = redLayer->getHandle();
2508
2509 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002510 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2511 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2512 // red area below the blue area
2513 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2514 // red area to the right of the blue area
2515 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002516
2517 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002518 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08002519 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2520 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002521 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08002522 mCapture->checkPixel(30, 30, 0, 0, 0);
2523}
2524
2525TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002526 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2527 PIXEL_FORMAT_RGBA_8888, 0);
2528 sp<SurfaceControl> blueLayer =
2529 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2530 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002531
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002532 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2533 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002534
2535 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002536 .setLayer(redLayer, INT32_MAX - 1)
2537 .show(redLayer)
2538 .show(blueLayer)
2539 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002540
2541 auto redLayerHandle = redLayer->getHandle();
2542
2543 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002544 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2545 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2546 // red area below the blue area
2547 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2548 // red area to the right of the blue area
2549 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002550
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002551 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08002552 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002553 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
2554 // red area below the blue area
2555 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
2556 // red area to the right of the blue area
2557 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002558 mCapture->checkPixel(30, 30, 0, 0, 0);
2559}
2560
2561TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002562 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2563 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08002564
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002565 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
chaviw7206d492017-11-10 16:16:12 -08002566
2567 auto redLayerHandle = redLayer->getHandle();
2568 mComposerClient->destroySurface(redLayerHandle);
2569 SurfaceComposerClient::Transaction().apply(true);
2570
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002571 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08002572
2573 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002574 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2575 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08002576}
2577
chaviw8e3fe5d2018-02-22 10:55:42 -08002578
2579class DereferenceSurfaceControlTest : public LayerTransactionTest {
2580protected:
2581 void SetUp() override {
2582 LayerTransactionTest::SetUp();
2583 bgLayer = createLayer("BG layer", 20, 20);
2584 fillLayerColor(bgLayer, Color::RED);
2585 fgLayer = createLayer("FG layer", 20, 20);
2586 fillLayerColor(fgLayer, Color::BLUE);
2587 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
2588 {
2589 SCOPED_TRACE("before anything");
2590 auto shot = screenshot();
2591 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2592 }
2593 }
2594 void TearDown() override {
2595 LayerTransactionTest::TearDown();
2596 bgLayer = 0;
2597 fgLayer = 0;
2598 }
2599
2600 sp<SurfaceControl> bgLayer;
2601 sp<SurfaceControl> fgLayer;
2602};
2603
2604TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
2605 fgLayer = nullptr;
2606 {
2607 SCOPED_TRACE("after setting null");
2608 auto shot = screenshot();
2609 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
2610 }
2611}
2612
2613TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
2614 auto transaction = Transaction().show(fgLayer);
2615 fgLayer = nullptr;
2616 {
2617 SCOPED_TRACE("after setting null");
2618 auto shot = screenshot();
2619 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2620 }
2621}
2622
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002623} // namespace android