blob: fd219913df38ca1c8e48b8c40736dc04f31d47d3 [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
1011TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1012 sp<SurfaceControl> bufferLayer;
1013 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1014 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1015
1016 // color is ignored
1017 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1018 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1019}
1020
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001021TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1022 sp<SurfaceControl> layer;
1023 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1024 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1025
1026 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1027 {
1028 SCOPED_TRACE("non-existing layer stack");
1029 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1030 }
1031
1032 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1033 {
1034 SCOPED_TRACE("original layer stack");
1035 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1036 }
1037}
1038
Chia-I Wu93853fe2017-11-02 08:30:27 -07001039TEST_F(LayerTransactionTest, SetMatrixBasic) {
1040 sp<SurfaceControl> layer;
1041 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1042 ASSERT_NO_FATAL_FAILURE(
1043 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1044
1045 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1046 {
1047 SCOPED_TRACE("IDENTITY");
1048 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1049 Color::WHITE);
1050 }
1051
1052 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1053 {
1054 SCOPED_TRACE("FLIP_H");
1055 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1056 Color::BLUE);
1057 }
1058
1059 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1060 {
1061 SCOPED_TRACE("FLIP_V");
1062 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1063 Color::GREEN);
1064 }
1065
1066 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1067 {
1068 SCOPED_TRACE("ROT_90");
1069 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1070 Color::GREEN);
1071 }
1072
1073 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1074 {
1075 SCOPED_TRACE("SCALE");
1076 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1077 Color::WHITE, true /* filtered */);
1078 }
1079}
1080
1081TEST_F(LayerTransactionTest, SetMatrixRot45) {
1082 sp<SurfaceControl> layer;
1083 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1084 ASSERT_NO_FATAL_FAILURE(
1085 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1086
1087 const float rot = M_SQRT1_2; // 45 degrees
1088 const float trans = M_SQRT2 * 16.0f;
1089 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1090
1091 auto shot = screenshot();
1092 // check a 8x8 region inside each color
1093 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1094 const int32_t halfL = 4;
1095 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1096 };
1097 const int32_t unit = int32_t(trans / 2);
1098 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1099 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1100 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1101 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1102}
1103
1104TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1105 sp<SurfaceControl> layer;
1106 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1107 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1108
1109 // setMatrix is applied after any pending resize, unlike setPosition
1110 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1111 {
1112 SCOPED_TRACE("resize pending");
1113 auto shot = screenshot();
1114 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1115 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1116 }
1117
1118 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1119 {
1120 SCOPED_TRACE("resize applied");
1121 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1122 }
1123}
1124
1125TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1126 sp<SurfaceControl> layer;
1127 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1128 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1129
1130 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1131 Transaction()
1132 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1133 .setSize(layer, 64, 64)
1134 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1135 .apply();
1136 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1137}
1138
Chia-I Wua56b2042017-11-01 15:16:35 -07001139TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1140 sp<SurfaceControl> layer;
1141 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1142 ASSERT_NO_FATAL_FAILURE(
1143 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1144
1145 // XXX SCALE_CROP is not respected; calling setSize and
1146 // setOverrideScalingMode in separate transactions does not work
1147 // (b/69315456)
1148 Transaction()
1149 .setSize(layer, 64, 16)
1150 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1151 .apply();
1152 {
1153 SCOPED_TRACE("SCALE_TO_WINDOW");
1154 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1155 Color::WHITE, true /* filtered */);
1156 }
1157}
1158
Chia-I Wu04dcca82017-11-02 08:30:27 -07001159TEST_F(LayerTransactionTest, SetCropBasic) {
1160 sp<SurfaceControl> layer;
1161 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1162 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1163 const Rect crop(8, 8, 24, 24);
1164
1165 Transaction().setCrop(layer, crop).apply();
1166 auto shot = screenshot();
1167 shot->expectColor(crop, Color::RED);
1168 shot->expectBorder(crop, Color::BLACK);
1169}
1170
1171TEST_F(LayerTransactionTest, SetCropEmpty) {
1172 sp<SurfaceControl> layer;
1173 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1174 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1175
1176 {
1177 SCOPED_TRACE("empty rect");
1178 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1179 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1180 }
1181
1182 {
1183 SCOPED_TRACE("negative rect");
1184 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1185 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1186 }
1187}
1188
1189TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1190 sp<SurfaceControl> layer;
1191 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1192 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1193
1194 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1195 auto shot = screenshot();
1196 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1197 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1198}
1199
1200TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1201 sp<SurfaceControl> layer;
1202 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1203 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1204
1205 const Point position(32, 32);
1206 const Rect crop(8, 8, 24, 24);
1207 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1208 auto shot = screenshot();
1209 shot->expectColor(crop + position, Color::RED);
1210 shot->expectBorder(crop + position, Color::BLACK);
1211}
1212
1213TEST_F(LayerTransactionTest, SetCropWithScale) {
1214 sp<SurfaceControl> layer;
1215 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1216 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1217
1218 // crop is affected by matrix
1219 Transaction()
1220 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1221 .setCrop(layer, Rect(8, 8, 24, 24))
1222 .apply();
1223 auto shot = screenshot();
1224 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1225 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1226}
1227
1228TEST_F(LayerTransactionTest, SetCropWithResize) {
1229 sp<SurfaceControl> layer;
1230 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1231 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1232
1233 // setCrop is applied immediately by default, with or without resize pending
1234 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1235 {
1236 SCOPED_TRACE("resize pending");
1237 auto shot = screenshot();
1238 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1239 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1240 }
1241
1242 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1243 {
1244 SCOPED_TRACE("resize applied");
1245 auto shot = screenshot();
1246 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1247 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1248 }
1249}
1250
1251TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1252 sp<SurfaceControl> layer;
1253 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1254 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1255
1256 // request setCrop to be applied with the next resize
1257 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1258 {
1259 SCOPED_TRACE("waiting for next resize");
1260 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1261 }
1262
1263 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1264 {
1265 SCOPED_TRACE("pending crop modified");
1266 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1267 }
1268
1269 Transaction().setSize(layer, 16, 16).apply();
1270 {
1271 SCOPED_TRACE("resize pending");
1272 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1273 }
1274
1275 // finally resize
1276 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1277 {
1278 SCOPED_TRACE("new crop applied");
1279 auto shot = screenshot();
1280 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1281 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1282 }
1283}
1284
1285TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1286 sp<SurfaceControl> layer;
1287 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1288 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1289
1290 // setCrop is not immediate even with SCALE_TO_WINDOW override
1291 Transaction()
1292 .setCrop(layer, Rect(4, 4, 12, 12))
1293 .setSize(layer, 16, 16)
1294 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1295 .setGeometryAppliesWithResize(layer)
1296 .apply();
1297 {
1298 SCOPED_TRACE("new crop pending");
1299 auto shot = screenshot();
1300 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1301 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1302 }
1303
1304 // XXX crop is never latched without other geometry change (b/69315677)
1305 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1306 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1307 Transaction().setPosition(layer, 0, 0).apply();
1308 {
1309 SCOPED_TRACE("new crop applied");
1310 auto shot = screenshot();
1311 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1312 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1313 }
1314}
1315
Chia-I Wucdd71a52017-11-02 08:30:27 -07001316TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1317 sp<SurfaceControl> layer;
1318 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1319 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1320 const Rect crop(8, 8, 24, 24);
1321
1322 // same as in SetCropBasic
1323 Transaction().setFinalCrop(layer, crop).apply();
1324 auto shot = screenshot();
1325 shot->expectColor(crop, Color::RED);
1326 shot->expectBorder(crop, Color::BLACK);
1327}
1328
1329TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1330 sp<SurfaceControl> layer;
1331 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1332 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1333
1334 // same as in SetCropEmpty
1335 {
1336 SCOPED_TRACE("empty rect");
1337 Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1338 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1339 }
1340
1341 {
1342 SCOPED_TRACE("negative rect");
1343 Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1344 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1345 }
1346}
1347
1348TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1349 sp<SurfaceControl> layer;
1350 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1351 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1352
1353 // same as in SetCropOutOfBounds
1354 Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1355 auto shot = screenshot();
1356 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1357 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1358}
1359
1360TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1361 sp<SurfaceControl> layer;
1362 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1363 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1364
1365 // final crop is applied post-translation
1366 Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1367 auto shot = screenshot();
1368 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1369 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1370}
1371
1372TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1373 sp<SurfaceControl> layer;
1374 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1375 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1376
1377 // final crop is not affected by matrix
1378 Transaction()
1379 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1380 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1381 .apply();
1382 auto shot = screenshot();
1383 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1384 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1385}
1386
1387TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1388 sp<SurfaceControl> layer;
1389 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1390 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1391
1392 // same as in SetCropWithResize
1393 Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1394 {
1395 SCOPED_TRACE("resize pending");
1396 auto shot = screenshot();
1397 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1398 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1399 }
1400
1401 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1402 {
1403 SCOPED_TRACE("resize applied");
1404 auto shot = screenshot();
1405 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1406 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1407 }
1408}
1409
1410TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1411 sp<SurfaceControl> layer;
1412 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1413 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1414
1415 // same as in SetCropWithNextResize
1416 Transaction()
1417 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1418 .setGeometryAppliesWithResize(layer)
1419 .apply();
1420 {
1421 SCOPED_TRACE("waiting for next resize");
1422 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1423 }
1424
1425 Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1426 {
1427 SCOPED_TRACE("pending final crop modified");
1428 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1429 }
1430
1431 Transaction().setSize(layer, 16, 16).apply();
1432 {
1433 SCOPED_TRACE("resize pending");
1434 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1435 }
1436
1437 // finally resize
1438 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1439 {
1440 SCOPED_TRACE("new final crop applied");
1441 auto shot = screenshot();
1442 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1443 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1444 }
1445}
1446
1447TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1448 sp<SurfaceControl> layer;
1449 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1450 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1451
1452 // same as in SetCropWithNextResizeScaleToWindow
1453 Transaction()
1454 .setFinalCrop(layer, Rect(4, 4, 12, 12))
1455 .setSize(layer, 16, 16)
1456 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1457 .setGeometryAppliesWithResize(layer)
1458 .apply();
1459 {
1460 SCOPED_TRACE("new final crop pending");
1461 auto shot = screenshot();
1462 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1463 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1464 }
1465
1466 // XXX final crop is never latched without other geometry change (b/69315677)
1467 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1468 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1469 Transaction().setPosition(layer, 0, 0).apply();
1470 {
1471 SCOPED_TRACE("new final crop applied");
1472 auto shot = screenshot();
1473 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1474 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1475 }
1476}
1477
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001478class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001479protected:
1480 virtual void SetUp() {
1481 mComposerClient = new SurfaceComposerClient;
1482 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1483
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001484 sp<IBinder> display(
1485 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001486 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001487 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001488
1489 ssize_t displayWidth = info.w;
1490 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001491
1492 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001493 mBGSurfaceControl =
1494 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1495 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001496 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001497 ASSERT_TRUE(mBGSurfaceControl->isValid());
1498 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1499
1500 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001501 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1502 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001503 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001504 ASSERT_TRUE(mFGSurfaceControl->isValid());
1505
1506 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1507
1508 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001509 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1510 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001511 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001512 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1513
1514 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1515
Robert Carr4cdc58f2017-08-23 14:22:20 -07001516 asTransaction([&](Transaction& t) {
1517 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001518
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001519 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001520
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001521 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1522 .setPosition(mFGSurfaceControl, 64, 64)
1523 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001524
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001525 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1526 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1527 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001528 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001529 }
1530
1531 virtual void TearDown() {
1532 mComposerClient->dispose();
1533 mBGSurfaceControl = 0;
1534 mFGSurfaceControl = 0;
1535 mSyncSurfaceControl = 0;
1536 mComposerClient = 0;
1537 }
1538
1539 void waitForPostedBuffers() {
1540 // Since the sync surface is in synchronous mode (i.e. double buffered)
1541 // posting three buffers to it should ensure that at least two
1542 // SurfaceFlinger::handlePageFlip calls have been made, which should
1543 // guaranteed that a buffer posted to another Surface has been retired.
1544 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1545 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1546 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1547 }
1548
Robert Carr4cdc58f2017-08-23 14:22:20 -07001549 void asTransaction(const std::function<void(Transaction&)>& exec) {
1550 Transaction t;
1551 exec(t);
1552 t.apply(true);
1553 }
1554
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001555 sp<SurfaceComposerClient> mComposerClient;
1556 sp<SurfaceControl> mBGSurfaceControl;
1557 sp<SurfaceControl> mFGSurfaceControl;
1558
1559 // This surface is used to ensure that the buffers posted to
1560 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1561 sp<SurfaceControl> mSyncSurfaceControl;
1562};
1563
Robert Carr7f619b22017-11-06 12:56:35 -08001564TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1565 sp<ScreenCapture> sc;
1566
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001567 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1568 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001569 fillSurfaceRGBA8(relative, 10, 10, 10);
1570 waitForPostedBuffers();
1571
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001572 Transaction{}
1573 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001574 .setPosition(relative, 64, 64)
1575 .apply();
1576
1577 {
1578 // The relative should be on top of the FG control.
1579 ScreenCapture::captureScreen(&sc);
1580 sc->checkPixel(64, 64, 10, 10, 10);
1581 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001582 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001583
1584 {
1585 // Nothing should change at this point.
1586 ScreenCapture::captureScreen(&sc);
1587 sc->checkPixel(64, 64, 10, 10, 10);
1588 }
1589
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001590 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001591
1592 {
1593 // Ensure that the relative was actually hidden, rather than
1594 // being left in the detached but visible state.
1595 ScreenCapture::captureScreen(&sc);
1596 sc->expectFGColor(64, 64);
1597 }
1598}
1599
Robert Carr8d5227b2017-03-16 15:41:03 -07001600class GeometryLatchingTest : public LayerUpdateTest {
1601protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001602 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001603 SCOPED_TRACE(trace);
1604 ScreenCapture::captureScreen(&sc);
1605 // We find the leading edge of the FG surface.
1606 sc->expectFGColor(127, 127);
1607 sc->expectBGColor(128, 128);
1608 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001609
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001610 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001611
1612 void unlockFGBuffer() {
1613 sp<Surface> s = mFGSurfaceControl->getSurface();
1614 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1615 waitForPostedBuffers();
1616 }
1617
Robert Carr8d5227b2017-03-16 15:41:03 -07001618 void completeFGResize() {
1619 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1620 waitForPostedBuffers();
1621 }
1622 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001623 asTransaction([&](Transaction& t) {
1624 t.setSize(mFGSurfaceControl, 64, 64);
1625 t.setPosition(mFGSurfaceControl, 64, 64);
1626 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1627 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1628 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001629
1630 EXPECT_INITIAL_STATE("After restoring initial state");
1631 }
1632 sp<ScreenCapture> sc;
1633};
1634
Robert Carr8d5227b2017-03-16 15:41:03 -07001635class CropLatchingTest : public GeometryLatchingTest {
1636protected:
1637 void EXPECT_CROPPED_STATE(const char* trace) {
1638 SCOPED_TRACE(trace);
1639 ScreenCapture::captureScreen(&sc);
1640 // The edge should be moved back one pixel by our crop.
1641 sc->expectFGColor(126, 126);
1642 sc->expectBGColor(127, 127);
1643 sc->expectBGColor(128, 128);
1644 }
chaviw59f5c562017-06-28 16:39:06 -07001645
1646 void EXPECT_RESIZE_STATE(const char* trace) {
1647 SCOPED_TRACE(trace);
1648 ScreenCapture::captureScreen(&sc);
1649 // The FG is now resized too 128,128 at 64,64
1650 sc->expectFGColor(64, 64);
1651 sc->expectFGColor(191, 191);
1652 sc->expectBGColor(192, 192);
1653 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001654};
1655
Robert Carr7bf247e2017-05-18 14:02:49 -07001656// In this test we ensure that setGeometryAppliesWithResize actually demands
1657// a buffer of the new size, and not just any size.
1658TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1659 EXPECT_INITIAL_STATE("before anything");
1660 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001661 asTransaction([&](Transaction& t) {
1662 t.setSize(mFGSurfaceControl, 128, 128);
1663 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1664 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001665
1666 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1667
1668 restoreInitialState();
1669
1670 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1671 // initiating the resize.
1672 lockAndFillFGBuffer();
1673
Robert Carr4cdc58f2017-08-23 14:22:20 -07001674 asTransaction([&](Transaction& t) {
1675 t.setSize(mFGSurfaceControl, 128, 128);
1676 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1677 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1678 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001679
1680 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1681
1682 // We now submit our old buffer, at the old size, and ensure it doesn't
1683 // trigger geometry latching.
1684 unlockFGBuffer();
1685
1686 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1687
1688 completeFGResize();
1689
1690 EXPECT_CROPPED_STATE("after the resize finishes");
1691}
1692
Pablo Ceballos05289c22016-04-14 15:49:55 -07001693TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1694 sp<ScreenCapture> sc;
1695 {
1696 SCOPED_TRACE("before anything");
1697 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001698 sc->expectBGColor(32, 32);
1699 sc->expectFGColor(96, 96);
1700 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001701 }
1702
1703 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001704 asTransaction([&](Transaction& t) {
1705 t.setAlpha(mFGSurfaceControl, 0.75);
1706 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001707 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001708 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001709
Robert Carr4cdc58f2017-08-23 14:22:20 -07001710 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001711 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001712 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001713 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001714 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001715
1716 {
1717 SCOPED_TRACE("before any trigger");
1718 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001719 sc->expectBGColor(32, 32);
1720 sc->expectFGColor(96, 96);
1721 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001722 }
1723
1724 // should trigger the first deferred transaction, but not the second one
1725 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1726 {
1727 SCOPED_TRACE("after first trigger");
1728 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001729 sc->expectBGColor(32, 32);
1730 sc->checkPixel(96, 96, 162, 63, 96);
1731 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001732 }
1733
1734 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001735 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001736
1737 // trigger the second deferred transaction
1738 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1739 {
1740 SCOPED_TRACE("after second trigger");
1741 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001742 sc->expectBGColor(32, 32);
1743 sc->expectBGColor(96, 96);
1744 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001745 }
1746}
1747
Robert Carre392b552017-09-19 12:16:05 -07001748TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1749 sp<ScreenCapture> sc;
1750
1751 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001752 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1753 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1754 sp<SurfaceControl> childBuffer =
1755 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1756 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001757 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1758
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001759 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001760
1761 {
1762 ScreenCapture::captureScreen(&sc);
1763 sc->expectChildColor(73, 73);
1764 sc->expectFGColor(74, 74);
1765 }
1766
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001767 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001768
1769 {
1770 ScreenCapture::captureScreen(&sc);
1771 sc->expectChildColor(73, 73);
1772 sc->expectChildColor(74, 74);
1773 }
1774}
1775
Robert Carr2c5f6d22017-09-26 12:30:35 -07001776TEST_F(LayerUpdateTest, MergingTransactions) {
1777 sp<ScreenCapture> sc;
1778 {
1779 SCOPED_TRACE("before move");
1780 ScreenCapture::captureScreen(&sc);
1781 sc->expectBGColor(0, 12);
1782 sc->expectFGColor(75, 75);
1783 sc->expectBGColor(145, 145);
1784 }
1785
1786 Transaction t1, t2;
1787 t1.setPosition(mFGSurfaceControl, 128, 128);
1788 t2.setPosition(mFGSurfaceControl, 0, 0);
1789 // We expect that the position update from t2 now
1790 // overwrites the position update from t1.
1791 t1.merge(std::move(t2));
1792 t1.apply();
1793
1794 {
1795 ScreenCapture::captureScreen(&sc);
1796 sc->expectFGColor(1, 1);
1797 }
1798}
1799
Robert Carr1f0a16a2016-10-24 16:27:39 -07001800class ChildLayerTest : public LayerUpdateTest {
1801protected:
1802 void SetUp() override {
1803 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001804 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1805 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001806 fillSurfaceRGBA8(mChild, 200, 200, 200);
1807
1808 {
1809 SCOPED_TRACE("before anything");
1810 ScreenCapture::captureScreen(&mCapture);
1811 mCapture->expectChildColor(64, 64);
1812 }
1813 }
1814 void TearDown() override {
1815 LayerUpdateTest::TearDown();
1816 mChild = 0;
1817 }
1818
1819 sp<SurfaceControl> mChild;
1820 sp<ScreenCapture> mCapture;
1821};
1822
1823TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001824 asTransaction([&](Transaction& t) {
1825 t.show(mChild);
1826 t.setPosition(mChild, 10, 10);
1827 t.setPosition(mFGSurfaceControl, 64, 64);
1828 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001829
1830 {
1831 ScreenCapture::captureScreen(&mCapture);
1832 // Top left of foreground must now be visible
1833 mCapture->expectFGColor(64, 64);
1834 // But 10 pixels in we should see the child surface
1835 mCapture->expectChildColor(74, 74);
1836 // And 10 more pixels we should be back to the foreground surface
1837 mCapture->expectFGColor(84, 84);
1838 }
1839
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001840 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841
1842 {
1843 ScreenCapture::captureScreen(&mCapture);
1844 // Top left of foreground should now be at 0, 0
1845 mCapture->expectFGColor(0, 0);
1846 // But 10 pixels in we should see the child surface
1847 mCapture->expectChildColor(10, 10);
1848 // And 10 more pixels we should be back to the foreground surface
1849 mCapture->expectFGColor(20, 20);
1850 }
1851}
1852
Robert Carr41b08b52017-06-01 16:11:34 -07001853TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001854 asTransaction([&](Transaction& t) {
1855 t.show(mChild);
1856 t.setPosition(mChild, 0, 0);
1857 t.setPosition(mFGSurfaceControl, 0, 0);
1858 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1859 });
Robert Carr41b08b52017-06-01 16:11:34 -07001860
1861 {
1862 ScreenCapture::captureScreen(&mCapture);
1863 mCapture->expectChildColor(0, 0);
1864 mCapture->expectChildColor(4, 4);
1865 mCapture->expectBGColor(5, 5);
1866 }
1867}
1868
1869TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001870 asTransaction([&](Transaction& t) {
1871 t.show(mChild);
1872 t.setPosition(mChild, 0, 0);
1873 t.setPosition(mFGSurfaceControl, 0, 0);
1874 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1875 });
Robert Carr41b08b52017-06-01 16:11:34 -07001876
1877 {
1878 ScreenCapture::captureScreen(&mCapture);
1879 mCapture->expectChildColor(0, 0);
1880 mCapture->expectChildColor(4, 4);
1881 mCapture->expectBGColor(5, 5);
1882 }
1883}
1884
Robert Carr1f0a16a2016-10-24 16:27:39 -07001885TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001886 asTransaction([&](Transaction& t) {
1887 t.show(mChild);
1888 t.setPosition(mFGSurfaceControl, 0, 0);
1889 t.setPosition(mChild, 63, 63);
1890 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001891
1892 {
1893 ScreenCapture::captureScreen(&mCapture);
1894 mCapture->expectFGColor(0, 0);
1895 // Last pixel in foreground should now be the child.
1896 mCapture->expectChildColor(63, 63);
1897 // But the child should be constrained and the next pixel
1898 // must be the background
1899 mCapture->expectBGColor(64, 64);
1900 }
1901}
1902
1903TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001904 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001905
1906 // Find the boundary between the parent and child
1907 {
1908 ScreenCapture::captureScreen(&mCapture);
1909 mCapture->expectChildColor(9, 9);
1910 mCapture->expectFGColor(10, 10);
1911 }
1912
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001913 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914
1915 // The boundary should be twice as far from the origin now.
1916 // The pixels from the last test should all be child now
1917 {
1918 ScreenCapture::captureScreen(&mCapture);
1919 mCapture->expectChildColor(9, 9);
1920 mCapture->expectChildColor(10, 10);
1921 mCapture->expectChildColor(19, 19);
1922 mCapture->expectFGColor(20, 20);
1923 }
1924}
Robert Carr9524cb32017-02-13 11:32:32 -08001925
Robert Carr6452f122017-03-21 10:41:29 -07001926TEST_F(ChildLayerTest, ChildLayerAlpha) {
1927 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1928 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1929 fillSurfaceRGBA8(mChild, 0, 254, 0);
1930 waitForPostedBuffers();
1931
Robert Carr4cdc58f2017-08-23 14:22:20 -07001932 asTransaction([&](Transaction& t) {
1933 t.show(mChild);
1934 t.setPosition(mChild, 0, 0);
1935 t.setPosition(mFGSurfaceControl, 0, 0);
1936 });
Robert Carr6452f122017-03-21 10:41:29 -07001937
1938 {
1939 ScreenCapture::captureScreen(&mCapture);
1940 // Unblended child color
1941 mCapture->checkPixel(0, 0, 0, 254, 0);
1942 }
1943
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001944 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001945
1946 {
1947 ScreenCapture::captureScreen(&mCapture);
1948 // Child and BG blended.
1949 mCapture->checkPixel(0, 0, 127, 127, 0);
1950 }
1951
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001952 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001953
1954 {
1955 ScreenCapture::captureScreen(&mCapture);
1956 // Child and BG blended.
1957 mCapture->checkPixel(0, 0, 95, 64, 95);
1958 }
1959}
1960
Robert Carr9524cb32017-02-13 11:32:32 -08001961TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001962 asTransaction([&](Transaction& t) {
1963 t.show(mChild);
1964 t.setPosition(mChild, 10, 10);
1965 t.setPosition(mFGSurfaceControl, 64, 64);
1966 });
Robert Carr9524cb32017-02-13 11:32:32 -08001967
1968 {
1969 ScreenCapture::captureScreen(&mCapture);
1970 // Top left of foreground must now be visible
1971 mCapture->expectFGColor(64, 64);
1972 // But 10 pixels in we should see the child surface
1973 mCapture->expectChildColor(74, 74);
1974 // And 10 more pixels we should be back to the foreground surface
1975 mCapture->expectFGColor(84, 84);
1976 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001977
1978 asTransaction([&](Transaction& t) {
1979 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1980 });
1981
Robert Carr9524cb32017-02-13 11:32:32 -08001982 {
1983 ScreenCapture::captureScreen(&mCapture);
1984 mCapture->expectFGColor(64, 64);
1985 // In reparenting we should have exposed the entire foreground surface.
1986 mCapture->expectFGColor(74, 74);
1987 // And the child layer should now begin at 10, 10 (since the BG
1988 // layer is at (0, 0)).
1989 mCapture->expectBGColor(9, 9);
1990 mCapture->expectChildColor(10, 10);
1991 }
1992}
1993
chaviw161410b02017-07-27 10:46:08 -07001994TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001995 asTransaction([&](Transaction& t) {
1996 t.show(mChild);
1997 t.setPosition(mChild, 10, 10);
1998 t.setPosition(mFGSurfaceControl, 64, 64);
1999 });
Robert Carr9524cb32017-02-13 11:32:32 -08002000
2001 {
2002 ScreenCapture::captureScreen(&mCapture);
2003 // Top left of foreground must now be visible
2004 mCapture->expectFGColor(64, 64);
2005 // But 10 pixels in we should see the child surface
2006 mCapture->expectChildColor(74, 74);
2007 // And 10 more pixels we should be back to the foreground surface
2008 mCapture->expectFGColor(84, 84);
2009 }
2010
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002011 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002012
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002013 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002014
chaviw161410b02017-07-27 10:46:08 -07002015 // Since the child has the same client as the parent, it will not get
2016 // detached and will be hidden.
2017 {
2018 ScreenCapture::captureScreen(&mCapture);
2019 mCapture->expectFGColor(64, 64);
2020 mCapture->expectFGColor(74, 74);
2021 mCapture->expectFGColor(84, 84);
2022 }
2023}
2024
2025TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2026 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002027 sp<SurfaceControl> mChildNewClient =
2028 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2029 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002030
Peiyong Lin566a3b42018-01-09 18:22:43 -08002031 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002032 ASSERT_TRUE(mChildNewClient->isValid());
2033
2034 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2035
Robert Carr4cdc58f2017-08-23 14:22:20 -07002036 asTransaction([&](Transaction& t) {
2037 t.hide(mChild);
2038 t.show(mChildNewClient);
2039 t.setPosition(mChildNewClient, 10, 10);
2040 t.setPosition(mFGSurfaceControl, 64, 64);
2041 });
chaviw161410b02017-07-27 10:46:08 -07002042
2043 {
2044 ScreenCapture::captureScreen(&mCapture);
2045 // Top left of foreground must now be visible
2046 mCapture->expectFGColor(64, 64);
2047 // But 10 pixels in we should see the child surface
2048 mCapture->expectChildColor(74, 74);
2049 // And 10 more pixels we should be back to the foreground surface
2050 mCapture->expectFGColor(84, 84);
2051 }
2052
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002053 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002054
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002055 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002056
Robert Carr9524cb32017-02-13 11:32:32 -08002057 // Nothing should have changed.
2058 {
2059 ScreenCapture::captureScreen(&mCapture);
2060 mCapture->expectFGColor(64, 64);
2061 mCapture->expectChildColor(74, 74);
2062 mCapture->expectFGColor(84, 84);
2063 }
2064}
2065
Robert Carr9b429f42017-04-17 14:56:57 -07002066TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002067 asTransaction([&](Transaction& t) {
2068 t.show(mChild);
2069 t.setPosition(mChild, 0, 0);
2070 t.setPosition(mFGSurfaceControl, 0, 0);
2071 });
Robert Carr9b429f42017-04-17 14:56:57 -07002072
2073 {
2074 ScreenCapture::captureScreen(&mCapture);
2075 // We've positioned the child in the top left.
2076 mCapture->expectChildColor(0, 0);
2077 // But it's only 10x10.
2078 mCapture->expectFGColor(10, 10);
2079 }
2080
Robert Carr4cdc58f2017-08-23 14:22:20 -07002081 asTransaction([&](Transaction& t) {
2082 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2083 // We cause scaling by 2.
2084 t.setSize(mFGSurfaceControl, 128, 128);
2085 });
Robert Carr9b429f42017-04-17 14:56:57 -07002086
2087 {
2088 ScreenCapture::captureScreen(&mCapture);
2089 // We've positioned the child in the top left.
2090 mCapture->expectChildColor(0, 0);
2091 mCapture->expectChildColor(10, 10);
2092 mCapture->expectChildColor(19, 19);
2093 // And now it should be scaled all the way to 20x20
2094 mCapture->expectFGColor(20, 20);
2095 }
2096}
2097
Robert Carr1725eee2017-04-26 18:32:15 -07002098// Regression test for b/37673612
2099TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002100 asTransaction([&](Transaction& t) {
2101 t.show(mChild);
2102 t.setPosition(mChild, 0, 0);
2103 t.setPosition(mFGSurfaceControl, 0, 0);
2104 });
Robert Carr1725eee2017-04-26 18:32:15 -07002105
2106 {
2107 ScreenCapture::captureScreen(&mCapture);
2108 // We've positioned the child in the top left.
2109 mCapture->expectChildColor(0, 0);
2110 // But it's only 10x10.
2111 mCapture->expectFGColor(10, 10);
2112 }
Robert Carr1725eee2017-04-26 18:32:15 -07002113 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2114 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002115 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002116 sp<Surface> s = mFGSurfaceControl->getSurface();
2117 auto anw = static_cast<ANativeWindow*>(s.get());
2118 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2119 native_window_set_buffers_dimensions(anw, 64, 128);
2120 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2121 waitForPostedBuffers();
2122
2123 {
2124 // The child should still be in the same place and not have any strange scaling as in
2125 // b/37673612.
2126 ScreenCapture::captureScreen(&mCapture);
2127 mCapture->expectChildColor(0, 0);
2128 mCapture->expectFGColor(10, 10);
2129 }
2130}
2131
Dan Stoza412903f2017-04-27 13:42:17 -07002132TEST_F(ChildLayerTest, Bug36858924) {
2133 // Destroy the child layer
2134 mChild.clear();
2135
2136 // Now recreate it as hidden
2137 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2138 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2139 mFGSurfaceControl.get());
2140
2141 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002142 asTransaction([&](Transaction& t) {
2143 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002144 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002145 t.show(mChild);
2146 });
Dan Stoza412903f2017-04-27 13:42:17 -07002147
2148 // Render the foreground surface a few times
2149 //
2150 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2151 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2152 // never acquire/release the first buffer
2153 ALOGI("Filling 1");
2154 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2155 ALOGI("Filling 2");
2156 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2157 ALOGI("Filling 3");
2158 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2159 ALOGI("Filling 4");
2160 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2161}
2162
chaviwf1961f72017-09-18 16:41:07 -07002163TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002164 asTransaction([&](Transaction& t) {
2165 t.show(mChild);
2166 t.setPosition(mChild, 10, 10);
2167 t.setPosition(mFGSurfaceControl, 64, 64);
2168 });
chaviw06178942017-07-27 10:25:59 -07002169
2170 {
2171 ScreenCapture::captureScreen(&mCapture);
2172 // Top left of foreground must now be visible
2173 mCapture->expectFGColor(64, 64);
2174 // But 10 pixels in we should see the child surface
2175 mCapture->expectChildColor(74, 74);
2176 // And 10 more pixels we should be back to the foreground surface
2177 mCapture->expectFGColor(84, 84);
2178 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002179
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002180 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002181
chaviw06178942017-07-27 10:25:59 -07002182 {
2183 ScreenCapture::captureScreen(&mCapture);
2184 mCapture->expectFGColor(64, 64);
2185 // In reparenting we should have exposed the entire foreground surface.
2186 mCapture->expectFGColor(74, 74);
2187 // And the child layer should now begin at 10, 10 (since the BG
2188 // layer is at (0, 0)).
2189 mCapture->expectBGColor(9, 9);
2190 mCapture->expectChildColor(10, 10);
2191 }
2192}
2193
chaviwf1961f72017-09-18 16:41:07 -07002194TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002195 asTransaction([&](Transaction& t) {
2196 t.show(mChild);
2197 t.setPosition(mChild, 10, 10);
2198 t.setPosition(mFGSurfaceControl, 64, 64);
2199 });
chaviwf1961f72017-09-18 16:41:07 -07002200
2201 {
2202 ScreenCapture::captureScreen(&mCapture);
2203 // Top left of foreground must now be visible
2204 mCapture->expectFGColor(64, 64);
2205 // But 10 pixels in we should see the child surface
2206 mCapture->expectChildColor(74, 74);
2207 // And 10 more pixels we should be back to the foreground surface
2208 mCapture->expectFGColor(84, 84);
2209 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002210 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002211 {
2212 ScreenCapture::captureScreen(&mCapture);
2213 // Nothing should have changed.
2214 mCapture->expectFGColor(64, 64);
2215 mCapture->expectChildColor(74, 74);
2216 mCapture->expectFGColor(84, 84);
2217 }
2218}
2219
2220TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002221 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2222 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002223 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002224 ASSERT_TRUE(newSurface->isValid());
2225
2226 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002227 asTransaction([&](Transaction& t) {
2228 t.hide(mChild);
2229 t.show(newSurface);
2230 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002231 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002232 t.setPosition(mFGSurfaceControl, 64, 64);
2233 });
chaviwf1961f72017-09-18 16:41:07 -07002234
2235 {
2236 ScreenCapture::captureScreen(&mCapture);
2237 // Top left of foreground must now be visible
2238 mCapture->expectFGColor(64, 64);
2239 // At 10, 10 we should see the new surface
2240 mCapture->checkPixel(10, 10, 63, 195, 63);
2241 }
2242
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002243 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002244
2245 {
2246 ScreenCapture::captureScreen(&mCapture);
2247 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2248 // mFGSurface, putting it at 74, 74.
2249 mCapture->expectFGColor(64, 64);
2250 mCapture->checkPixel(74, 74, 63, 195, 63);
2251 mCapture->expectFGColor(84, 84);
2252 }
2253}
2254
chaviwc9674332017-08-28 12:32:18 -07002255TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002256 sp<SurfaceControl> grandchild =
2257 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2258 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002259 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2260
2261 {
2262 ScreenCapture::captureScreen(&mCapture);
2263 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2264 // which begins at 64, 64
2265 mCapture->checkPixel(64, 64, 50, 50, 50);
2266 }
2267}
2268
Robert Carr503c7042017-09-27 15:06:08 -07002269TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002270 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2271 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002272 fillSurfaceRGBA8(relative, 255, 255, 255);
2273
2274 Transaction t;
2275 t.setLayer(relative, INT32_MAX)
2276 .setRelativeLayer(mChild, relative->getHandle(), 1)
2277 .setPosition(mFGSurfaceControl, 0, 0)
2278 .apply(true);
2279
2280 // We expect that the child should have been elevated above our
2281 // INT_MAX layer even though it's not a child of it.
2282 {
2283 ScreenCapture::captureScreen(&mCapture);
2284 mCapture->expectChildColor(0, 0);
2285 mCapture->expectChildColor(9, 9);
2286 mCapture->checkPixel(10, 10, 255, 255, 255);
2287 }
2288}
2289
chaviwa76b2712017-09-20 12:02:26 -07002290class ScreenCaptureTest : public LayerUpdateTest {
2291protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002292 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002293};
2294
2295TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2296 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002297 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002298 mCapture->expectBGColor(0, 0);
2299 // Doesn't capture FG layer which is at 64, 64
2300 mCapture->expectBGColor(64, 64);
2301}
2302
2303TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2304 auto fgHandle = mFGSurfaceControl->getHandle();
2305
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002306 sp<SurfaceControl> child =
2307 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2308 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002309 fillSurfaceRGBA8(child, 200, 200, 200);
2310
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002311 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002312
2313 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002314 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002315 mCapture->expectFGColor(10, 10);
2316 mCapture->expectChildColor(0, 0);
2317}
2318
Robert Carr578038f2018-03-09 12:25:24 -08002319TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2320 auto fgHandle = mFGSurfaceControl->getHandle();
2321
2322 sp<SurfaceControl> child =
2323 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2324 0, mFGSurfaceControl.get());
2325 fillSurfaceRGBA8(child, 200, 200, 200);
2326
2327 SurfaceComposerClient::Transaction().show(child).apply(true);
2328
2329 // Captures mFGSurfaceControl's child
2330 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2331 mCapture->checkPixel(10, 10, 0, 0, 0);
2332 mCapture->expectChildColor(0, 0);
2333}
2334
2335
2336// In the following tests we verify successful skipping of a parent layer,
2337// so we use the same verification logic and only change how we mutate
2338// the parent layer to verify that various properties are ignored.
2339class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2340public:
2341 void SetUp() override {
2342 LayerUpdateTest::SetUp();
2343
2344 mChild =
2345 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2346 0, mFGSurfaceControl.get());
2347 fillSurfaceRGBA8(mChild, 200, 200, 200);
2348
2349 SurfaceComposerClient::Transaction().show(mChild).apply(true);
2350 }
2351
2352 void verify() {
2353 auto fgHandle = mFGSurfaceControl->getHandle();
2354 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2355 mCapture->checkPixel(10, 10, 0, 0, 0);
2356 mCapture->expectChildColor(0, 0);
2357 }
2358
2359 std::unique_ptr<ScreenCapture> mCapture;
2360 sp<SurfaceControl> mChild;
2361};
2362
2363TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2364
2365 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2366
2367 // Even though the parent is hidden we should still capture the child.
2368 verify();
2369}
2370
2371TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
2372
2373 SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true);
2374
2375 // Even though the parent is cropped out we should still capture the child.
2376 verify();
2377}
2378
2379TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2380
2381 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2382
2383 // We should not inherit the parent scaling.
2384 verify();
2385}
2386
chaviwa76b2712017-09-20 12:02:26 -07002387TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2388 auto fgHandle = mFGSurfaceControl->getHandle();
2389
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002390 sp<SurfaceControl> child =
2391 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2392 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002393 fillSurfaceRGBA8(child, 200, 200, 200);
2394
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002395 sp<SurfaceControl> grandchild =
2396 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2397 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002398
2399 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2400 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002401 .show(child)
2402 .setPosition(grandchild, 5, 5)
2403 .show(grandchild)
2404 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002405
2406 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002407 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002408 mCapture->expectFGColor(10, 10);
2409 mCapture->expectChildColor(0, 0);
2410 mCapture->checkPixel(5, 5, 50, 50, 50);
2411}
2412
2413TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002414 sp<SurfaceControl> child =
2415 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2416 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002417 fillSurfaceRGBA8(child, 200, 200, 200);
2418 auto childHandle = child->getHandle();
2419
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002420 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002421
2422 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002423 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07002424 mCapture->expectChildColor(0, 0);
2425 mCapture->expectChildColor(9, 9);
2426}
2427
2428TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002429 sp<SurfaceControl> child =
2430 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2431 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002432 fillSurfaceRGBA8(child, 200, 200, 200);
2433 auto childHandle = child->getHandle();
2434
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002435 sp<SurfaceControl> grandchild =
2436 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2437 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002438 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2439
2440 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002441 .show(child)
2442 .setPosition(grandchild, 5, 5)
2443 .show(grandchild)
2444 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002445
2446 auto grandchildHandle = grandchild->getHandle();
2447
2448 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002449 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07002450 mCapture->checkPixel(0, 0, 50, 50, 50);
2451 mCapture->checkPixel(4, 4, 50, 50, 50);
2452}
2453
chaviw7206d492017-11-10 16:16:12 -08002454TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002455 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2456 PIXEL_FORMAT_RGBA_8888, 0);
2457 sp<SurfaceControl> blueLayer =
2458 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2459 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002460
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002461 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2462 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002463
2464 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002465 .setLayer(redLayer, INT32_MAX - 1)
2466 .show(redLayer)
2467 .show(blueLayer)
2468 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002469
2470 auto redLayerHandle = redLayer->getHandle();
2471
2472 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002473 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2474 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2475 // red area below the blue area
2476 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2477 // red area to the right of the blue area
2478 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002479
2480 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002481 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08002482 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2483 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002484 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08002485 mCapture->checkPixel(30, 30, 0, 0, 0);
2486}
2487
2488TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002489 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2490 PIXEL_FORMAT_RGBA_8888, 0);
2491 sp<SurfaceControl> blueLayer =
2492 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2493 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002494
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002495 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2496 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002497
2498 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002499 .setLayer(redLayer, INT32_MAX - 1)
2500 .show(redLayer)
2501 .show(blueLayer)
2502 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002503
2504 auto redLayerHandle = redLayer->getHandle();
2505
2506 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002507 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2508 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2509 // red area below the blue area
2510 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2511 // red area to the right of the blue area
2512 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002513
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002514 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08002515 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002516 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
2517 // red area below the blue area
2518 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
2519 // red area to the right of the blue area
2520 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002521 mCapture->checkPixel(30, 30, 0, 0, 0);
2522}
2523
2524TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002525 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2526 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08002527
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002528 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
chaviw7206d492017-11-10 16:16:12 -08002529
2530 auto redLayerHandle = redLayer->getHandle();
2531 mComposerClient->destroySurface(redLayerHandle);
2532 SurfaceComposerClient::Transaction().apply(true);
2533
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002534 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08002535
2536 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002537 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2538 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08002539}
2540
chaviw8e3fe5d2018-02-22 10:55:42 -08002541
2542class DereferenceSurfaceControlTest : public LayerTransactionTest {
2543protected:
2544 void SetUp() override {
2545 LayerTransactionTest::SetUp();
2546 bgLayer = createLayer("BG layer", 20, 20);
2547 fillLayerColor(bgLayer, Color::RED);
2548 fgLayer = createLayer("FG layer", 20, 20);
2549 fillLayerColor(fgLayer, Color::BLUE);
2550 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
2551 {
2552 SCOPED_TRACE("before anything");
2553 auto shot = screenshot();
2554 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2555 }
2556 }
2557 void TearDown() override {
2558 LayerTransactionTest::TearDown();
2559 bgLayer = 0;
2560 fgLayer = 0;
2561 }
2562
2563 sp<SurfaceControl> bgLayer;
2564 sp<SurfaceControl> fgLayer;
2565};
2566
2567TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
2568 fgLayer = nullptr;
2569 {
2570 SCOPED_TRACE("after setting null");
2571 auto shot = screenshot();
2572 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
2573 }
2574}
2575
2576TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
2577 auto transaction = Transaction().show(fgLayer);
2578 fgLayer = nullptr;
2579 {
2580 SCOPED_TRACE("after setting null");
2581 auto shot = screenshot();
2582 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2583 }
2584}
2585
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002586} // namespace android