blob: aed1f789af9917a390a547c28e20324515abe056 [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
Marissa Wall61c58622018-07-18 10:12:20 -070065using android::hardware::graphics::common::V1_1::BufferUsage;
66
Chia-I Wu718daf82017-10-20 11:57:17 -070067std::ostream& operator<<(std::ostream& os, const Color& color) {
68 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
69 return os;
70}
71
72// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070073void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
74 const Color& color) {
75 Rect r(0, 0, buffer.width, buffer.height);
76 if (!r.intersect(rect, &r)) {
77 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070078 }
79
Marissa Wall61c58622018-07-18 10:12:20 -070080 int32_t width = r.right - r.left;
81 int32_t height = r.bottom - r.top;
82
83 for (int32_t row = 0; row < height; row++) {
84 uint8_t* dst =
85 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
86 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070087 dst[0] = color.r;
88 dst[1] = color.g;
89 dst[2] = color.b;
90 dst[3] = color.a;
91 dst += 4;
92 }
93 }
94}
95
Marissa Wall61c58622018-07-18 10:12:20 -070096// Fill a region with the specified color.
97void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
98 Rect r(0, 0, buffer->width, buffer->height);
99 if (!r.intersect(rect, &r)) {
100 return;
101 }
102
103 int32_t width = r.right - r.left;
104 int32_t height = r.bottom - r.top;
105
106 uint8_t* pixels;
107 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
108 reinterpret_cast<void**>(&pixels));
109
110 for (int32_t row = 0; row < height; row++) {
111 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
112 for (int32_t column = 0; column < width; column++) {
113 dst[0] = color.r;
114 dst[1] = color.g;
115 dst[2] = color.b;
116 dst[3] = color.a;
117 dst += 4;
118 }
119 }
120 buffer->unlock();
121}
122
Chia-I Wu718daf82017-10-20 11:57:17 -0700123// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000124void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700125 const Color& color, uint8_t tolerance) {
126 int32_t x = rect.left;
127 int32_t y = rect.top;
128 int32_t width = rect.right - rect.left;
129 int32_t height = rect.bottom - rect.top;
130
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000131 int32_t bufferWidth = int32_t(outBuffer->getWidth());
132 int32_t bufferHeight = int32_t(outBuffer->getHeight());
133 if (x + width > bufferWidth) {
134 x = std::min(x, bufferWidth);
135 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700136 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000137 if (y + height > bufferHeight) {
138 y = std::min(y, bufferHeight);
139 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700140 }
141
142 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
143 uint8_t tmp = a >= b ? a - b : b - a;
144 return tmp <= tolerance;
145 };
146 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000147 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700148 for (int32_t i = 0; i < width; i++) {
149 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
150 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
151 << "pixel @ (" << x + i << ", " << y + j << "): "
152 << "expected (" << color << "), "
153 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
154 src += 4;
155 }
156 }
157}
158
159} // anonymous namespace
160
Robert Carr4cdc58f2017-08-23 14:22:20 -0700161using Transaction = SurfaceComposerClient::Transaction;
162
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700163// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700164static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
165 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800166 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700167 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800168 ASSERT_TRUE(s != nullptr);
169 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800170 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700171 for (int y = 0; y < outBuffer.height; y++) {
172 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700173 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700174 pixel[0] = r;
175 pixel[1] = g;
176 pixel[2] = b;
177 pixel[3] = 255;
178 }
179 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700180 if (unlock) {
181 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
182 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700183}
184
185// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
186// individual pixel values for testing purposes.
187class ScreenCapture : public RefBase {
188public:
chaviw0e3479f2018-09-10 16:49:30 -0700189 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700190 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700191 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700192 SurfaceComposerClient::Transaction().apply(true);
193
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000194 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700195 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700196 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
197 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000198 }
199
200 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
201 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
202 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
203 SurfaceComposerClient::Transaction().apply(true);
204
205 sp<GraphicBuffer> outBuffer;
206 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
207 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700208 }
209
Robert Carr578038f2018-03-09 12:25:24 -0800210 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
211 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
212 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
213 SurfaceComposerClient::Transaction().apply(true);
214
215 sp<GraphicBuffer> outBuffer;
216 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
217 *sc = std::make_unique<ScreenCapture>(outBuffer);
218 }
219
Chia-I Wu718daf82017-10-20 11:57:17 -0700220 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000221 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
222 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700223 }
224
225 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000226 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700227 const bool leftBorder = rect.left > 0;
228 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000229 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
230 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700231
232 if (topBorder) {
233 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
234 if (leftBorder) {
235 top.left -= 1;
236 }
237 if (rightBorder) {
238 top.right += 1;
239 }
240 expectColor(top, color, tolerance);
241 }
242 if (leftBorder) {
243 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
244 expectColor(left, color, tolerance);
245 }
246 if (rightBorder) {
247 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
248 expectColor(right, color, tolerance);
249 }
250 if (bottomBorder) {
251 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
252 if (leftBorder) {
253 bottom.left -= 1;
254 }
255 if (rightBorder) {
256 bottom.right += 1;
257 }
258 expectColor(bottom, color, tolerance);
259 }
260 }
261
Chia-I Wu93853fe2017-11-02 08:30:27 -0700262 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
263 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
264 uint8_t tolerance = 0) {
265 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
266
267 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
268 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
269 // avoid checking borders due to unspecified filtering behavior
270 const int32_t offsetX = filtered ? 2 : 0;
271 const int32_t offsetY = filtered ? 2 : 0;
272 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
273 tolerance);
274 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
275 tolerance);
276 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
277 tolerance);
278 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
279 bottomRight, tolerance);
280 }
281
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700282 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000283 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
284 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700285 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
286 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700287 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
288 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700289 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700290 }
291 }
292
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700293 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700294
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700295 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700297 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700298
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000299 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
300 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700301 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700302
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000303 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700304
305private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000306 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800307 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700308};
309
Chia-I Wu718daf82017-10-20 11:57:17 -0700310class LayerTransactionTest : public ::testing::Test {
311protected:
312 void SetUp() override {
313 mClient = new SurfaceComposerClient;
314 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
315
316 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
317 }
318
chaviw0e3479f2018-09-10 16:49:30 -0700319 virtual void TearDown() {
320 mBlackBgSurface = 0;
321 mClient->dispose();
322 mClient = 0;
323 }
324
Marissa Wall61c58622018-07-18 10:12:20 -0700325 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
326 uint32_t flags = 0) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700327 auto layer =
328 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
329 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
330
Vishnu Nair60356342018-11-13 13:00:45 -0800331 Transaction t;
332 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
333 // If we are creating a color layer, set its crop since its size will be ignored.
334 if (flags == ISurfaceComposerClient::eFXSurfaceColor) {
335 t.setCrop_legacy(layer, Rect(0, 0, width, height));
336 }
337
338 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700339 if (error != NO_ERROR) {
340 ADD_FAILURE() << "failed to initialize SurfaceControl";
341 layer.clear();
342 }
343
344 return layer;
345 }
346
Marissa Wall61c58622018-07-18 10:12:20 -0700347 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700348 // wait for previous transactions (such as setSize) to complete
349 Transaction().apply(true);
350
351 ANativeWindow_Buffer buffer = {};
352 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
353
354 return buffer;
355 }
356
Marissa Wall61c58622018-07-18 10:12:20 -0700357 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700358 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
359
360 // wait for the newly posted buffer to be latched
361 waitForLayerBuffers();
362 }
363
Marissa Wall61c58622018-07-18 10:12:20 -0700364 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
365 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700366 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700367 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
368 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
369 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700370 }
371
Marissa Wall61c58622018-07-18 10:12:20 -0700372 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
373 int32_t bufferWidth, int32_t bufferHeight) {
374 sp<GraphicBuffer> buffer =
375 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
376 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
377 BufferUsage::COMPOSER_OVERLAY,
378 "test");
379 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
380 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
381 }
382
383 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
384 int32_t bufferWidth, int32_t bufferHeight) {
385 switch (mLayerType) {
386 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
387 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
388 break;
389 case ISurfaceComposerClient::eFXSurfaceBufferState:
390 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
391 break;
392 default:
393 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
394 }
395 }
396
397 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
398 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700399 const Color& topRight, const Color& bottomLeft,
400 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700401 switch (mLayerType) {
402 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
403 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
404 bottomLeft, bottomRight);
405 break;
406 case ISurfaceComposerClient::eFXSurfaceBufferState:
407 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
408 bottomLeft, bottomRight);
409 break;
410 default:
411 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
412 }
413 }
414
415 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
416 int32_t bufferHeight, const Color& topLeft,
417 const Color& topRight, const Color& bottomLeft,
418 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700419 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700420 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
421 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700422
Marissa Wall61c58622018-07-18 10:12:20 -0700423 const int32_t halfW = bufferWidth / 2;
424 const int32_t halfH = bufferHeight / 2;
425 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
426 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
427 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
428 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
429 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700430
Marissa Wall61c58622018-07-18 10:12:20 -0700431 postBufferQueueLayerBuffer(layer);
432 }
433
434 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
435 int32_t bufferHeight, const Color& topLeft,
436 const Color& topRight, const Color& bottomLeft,
437 const Color& bottomRight) {
438 sp<GraphicBuffer> buffer =
439 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
440 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
441 BufferUsage::COMPOSER_OVERLAY,
442 "test");
443
444 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
445
446 const int32_t halfW = bufferWidth / 2;
447 const int32_t halfH = bufferHeight / 2;
448 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
449 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
450 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
451 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
452
453 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700454 }
455
chaviw0e3479f2018-09-10 16:49:30 -0700456 std::unique_ptr<ScreenCapture> screenshot() {
457 std::unique_ptr<ScreenCapture> screenshot;
458 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700459 return screenshot;
460 }
461
462 sp<SurfaceComposerClient> mClient;
463
464 sp<IBinder> mDisplay;
465 uint32_t mDisplayWidth;
466 uint32_t mDisplayHeight;
467 uint32_t mDisplayLayerStack;
468
469 // leave room for ~256 layers
470 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
471
Marissa Wall61c58622018-07-18 10:12:20 -0700472 void setPositionWithResizeHelper(uint32_t layerType);
473 void setSizeBasicHelper(uint32_t layerType);
474 void setMatrixWithResizeHelper(uint32_t layerType);
475
chaviw0e3479f2018-09-10 16:49:30 -0700476 sp<SurfaceControl> mBlackBgSurface;
Chia-I Wu718daf82017-10-20 11:57:17 -0700477private:
478 void SetUpDisplay() {
479 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
480 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
481
482 // get display width/height
483 DisplayInfo info;
484 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
485 mDisplayWidth = info.w;
486 mDisplayHeight = info.h;
487
488 // After a new buffer is queued, SurfaceFlinger is notified and will
489 // latch the new buffer on next vsync. Let's heuristically wait for 3
490 // vsyncs.
491 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
492
493 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700494
495 mBlackBgSurface = mClient->createSurface(String8("BaseSurface"), mDisplayWidth,
496 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
497 ISurfaceComposerClient::eFXSurfaceColor);
498
Chia-I Wu718daf82017-10-20 11:57:17 -0700499 // set layer stack (b/68888219)
500 Transaction t;
501 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800502 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700503 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
504 t.setColor(mBlackBgSurface, half3{0, 0, 0});
505 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700506 t.apply();
507 }
508
chaviw0e3479f2018-09-10 16:49:30 -0700509 void waitForLayerBuffers() {
510 // Request an empty transaction to get applied synchronously to ensure the buffer is
511 // latched.
512 Transaction().apply(true);
513 usleep(mBufferPostDelay);
514 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700515
516 int32_t mBufferPostDelay;
517};
518
Marissa Wall61c58622018-07-18 10:12:20 -0700519class LayerTypeTransactionTest : public LayerTransactionTest,
520 public ::testing::WithParamInterface<uint32_t> {
521public:
522 LayerTypeTransactionTest() { mLayerType = GetParam(); }
523
524 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
525 uint32_t flags = 0) override {
526 // if the flags already have a layer type specified, return an error
527 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
528 return nullptr;
529 }
530 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
531 }
532
533 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
534 int32_t bufferHeight) {
535 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
536 bufferWidth, bufferHeight));
537 }
538
539 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
540 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
541 const Color& bottomLeft, const Color& bottomRight) {
542 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
543 bufferWidth, bufferHeight,
544 topLeft, topRight,
545 bottomLeft, bottomRight));
546 }
547
548protected:
549 uint32_t mLayerType;
550};
551
552INSTANTIATE_TEST_CASE_P(
553 LayerTypeTransactionTests, LayerTypeTransactionTest,
554 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
555 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
556
557TEST_P(LayerTypeTransactionTest, SetPositionBasic) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700558 sp<SurfaceControl> layer;
559 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700560 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700561
562 {
563 SCOPED_TRACE("default position");
564 auto shot = screenshot();
565 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
566 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
567 }
568
569 Transaction().setPosition(layer, 5, 10).apply();
570 {
571 SCOPED_TRACE("new position");
572 auto shot = screenshot();
573 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
574 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
575 }
576}
577
Marissa Wall61c58622018-07-18 10:12:20 -0700578TEST_P(LayerTypeTransactionTest, SetPositionRounding) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700579 sp<SurfaceControl> layer;
580 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700581 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700582
583 // GLES requires only 4 bits of subpixel precision during rasterization
584 // XXX GLES composition does not match HWC composition due to precision
585 // loss (b/69315223)
586 const float epsilon = 1.0f / 16.0f;
587 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
588 {
589 SCOPED_TRACE("rounding down");
590 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
591 }
592
593 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
594 {
595 SCOPED_TRACE("rounding up");
596 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
597 }
598}
599
Marissa Wall61c58622018-07-18 10:12:20 -0700600TEST_P(LayerTypeTransactionTest, SetPositionOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700601 sp<SurfaceControl> layer;
602 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700603 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700604
605 Transaction().setPosition(layer, -32, -32).apply();
606 {
607 SCOPED_TRACE("negative coordinates");
608 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
609 }
610
611 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
612 {
613 SCOPED_TRACE("positive coordinates");
614 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
615 }
616}
617
Marissa Wall61c58622018-07-18 10:12:20 -0700618TEST_P(LayerTypeTransactionTest, SetPositionPartiallyOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700619 sp<SurfaceControl> layer;
620 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700621 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700622
623 // partially out of bounds
624 Transaction().setPosition(layer, -30, -30).apply();
625 {
626 SCOPED_TRACE("negative coordinates");
627 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
628 }
629
630 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
631 {
632 SCOPED_TRACE("positive coordinates");
633 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
634 mDisplayHeight),
635 Color::RED);
636 }
637}
638
Marissa Wall61c58622018-07-18 10:12:20 -0700639void LayerTransactionTest::setPositionWithResizeHelper(uint32_t layerType) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700640 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700641 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
642 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700643
644 // setPosition is applied immediately by default, with or without resize
645 // pending
646 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
647 {
648 SCOPED_TRACE("resize pending");
649 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700650 Rect rect;
651 switch (layerType) {
652 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
653 rect = {5, 10, 37, 42};
654 break;
655 case ISurfaceComposerClient::eFXSurfaceBufferState:
656 rect = {5, 10, 69, 74};
657 break;
658 default:
659 ASSERT_FALSE(true) << "Unsupported layer type";
660 }
661
662 shot->expectColor(rect, Color::RED);
663 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700664 }
665
Marissa Wall61c58622018-07-18 10:12:20 -0700666 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700667 {
668 SCOPED_TRACE("resize applied");
669 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
670 }
671}
672
Marissa Wall61c58622018-07-18 10:12:20 -0700673TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
674 ASSERT_NO_FATAL_FAILURE(
675 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
676}
677
678TEST_F(LayerTransactionTest, SetPositionWithResize_BufferState) {
679 ASSERT_NO_FATAL_FAILURE(
680 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
681}
682
683TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700684 sp<SurfaceControl> layer;
685 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700686 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700687
688 // request setPosition to be applied with the next resize
689 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
690 {
691 SCOPED_TRACE("new position pending");
692 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
693 }
694
695 Transaction().setPosition(layer, 15, 20).apply();
696 {
697 SCOPED_TRACE("pending new position modified");
698 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
699 }
700
701 Transaction().setSize(layer, 64, 64).apply();
702 {
703 SCOPED_TRACE("resize pending");
704 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
705 }
706
707 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700708 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700709 {
710 SCOPED_TRACE("new position applied");
711 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
712 }
713}
714
Marissa Wall61c58622018-07-18 10:12:20 -0700715TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700716 sp<SurfaceControl> layer;
717 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700718 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700719
720 // setPosition is not immediate even with SCALE_TO_WINDOW override
721 Transaction()
722 .setPosition(layer, 5, 10)
723 .setSize(layer, 64, 64)
724 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
725 .setGeometryAppliesWithResize(layer)
726 .apply();
727 {
728 SCOPED_TRACE("new position pending");
729 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
730 }
731
Marissa Wall61c58622018-07-18 10:12:20 -0700732 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700733 {
734 SCOPED_TRACE("new position applied");
735 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
736 }
737}
738
Marissa Wall61c58622018-07-18 10:12:20 -0700739void LayerTransactionTest::setSizeBasicHelper(uint32_t layerType) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700740 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700741 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
742 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700743
744 Transaction().setSize(layer, 64, 64).apply();
745 {
746 SCOPED_TRACE("resize pending");
747 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700748 Rect rect;
749 switch (layerType) {
750 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
751 rect = {0, 0, 32, 32};
752 break;
753 case ISurfaceComposerClient::eFXSurfaceBufferState:
754 rect = {0, 0, 64, 64};
755 break;
756 default:
757 ASSERT_FALSE(true) << "Unsupported layer type";
758 }
759 shot->expectColor(rect, Color::RED);
760 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700761 }
762
Marissa Wall61c58622018-07-18 10:12:20 -0700763 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700764 {
765 SCOPED_TRACE("resize applied");
766 auto shot = screenshot();
767 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
768 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
769 }
770}
771
Marissa Wall61c58622018-07-18 10:12:20 -0700772TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
773 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue);
774}
775
776TEST_F(LayerTransactionTest, SetSizeBasic_BufferState) {
777 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState);
778}
779
780TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700781 // cannot test robustness against invalid sizes (zero or really huge)
782}
783
Marissa Wall61c58622018-07-18 10:12:20 -0700784TEST_P(LayerTypeTransactionTest, SetSizeWithScaleToWindow) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700785 sp<SurfaceControl> layer;
786 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700787 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700788
789 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
790 Transaction()
791 .setSize(layer, 64, 64)
792 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
793 .apply();
794 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
795}
796
Marissa Wall61c58622018-07-18 10:12:20 -0700797TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700798 sp<SurfaceControl> layerR;
799 sp<SurfaceControl> layerG;
800 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700801 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700802 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700803 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700804
805 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
806 {
807 SCOPED_TRACE("layerR");
808 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
809 }
810
811 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
812 {
813 SCOPED_TRACE("layerG");
814 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
815 }
816}
817
Marissa Wall61c58622018-07-18 10:12:20 -0700818TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700819 sp<SurfaceControl> parent =
820 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
821 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700822 sp<SurfaceControl> layerR;
823 sp<SurfaceControl> layerG;
824 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700825 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700826 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700827 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700828
chaviw0e3479f2018-09-10 16:49:30 -0700829 Transaction()
830 .reparent(layerR, parent->getHandle())
831 .reparent(layerG, parent->getHandle())
832 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700833 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
834 {
835 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700836 auto shot = screenshot();
837 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700838 }
839
840 Transaction().setLayer(layerR, -3).apply();
841 {
842 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700843 auto shot = screenshot();
844 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700845 }
846}
847
Marissa Wall61c58622018-07-18 10:12:20 -0700848TEST_P(LayerTypeTransactionTest, SetRelativeZBasic) {
Chia-I Wu49313302017-10-31 10:14:40 -0700849 sp<SurfaceControl> layerR;
850 sp<SurfaceControl> layerG;
851 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700852 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700853 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700854 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700855
856 Transaction()
857 .setPosition(layerG, 16, 16)
858 .setRelativeLayer(layerG, layerR->getHandle(), 1)
859 .apply();
860 {
861 SCOPED_TRACE("layerG above");
862 auto shot = screenshot();
863 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
864 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
865 }
866
867 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
868 {
869 SCOPED_TRACE("layerG below");
870 auto shot = screenshot();
871 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
872 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
873 }
874}
875
Marissa Wall61c58622018-07-18 10:12:20 -0700876TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700877 sp<SurfaceControl> parent =
878 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
879 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800880 sp<SurfaceControl> layerR;
881 sp<SurfaceControl> layerG;
882 sp<SurfaceControl> layerB;
883 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700884 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800885 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700886 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800887 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700888 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800889
chaviw0e3479f2018-09-10 16:49:30 -0700890 Transaction()
891 .reparent(layerB, parent->getHandle())
892 .apply();
893
Chia-I Wuec2d9852017-11-21 09:21:01 -0800894 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
895 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
896
chaviw0e3479f2018-09-10 16:49:30 -0700897 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800898 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700899 sp<IBinder> parentHandle = parent->getHandle();
900 ScreenCapture::captureLayers(&screenshot, parentHandle);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800901 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
902}
903
Marissa Wall61c58622018-07-18 10:12:20 -0700904TEST_P(LayerTypeTransactionTest, SetRelativeZGroup) {
Chia-I Wu49313302017-10-31 10:14:40 -0700905 sp<SurfaceControl> layerR;
906 sp<SurfaceControl> layerG;
907 sp<SurfaceControl> layerB;
908 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700909 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700910 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700911 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700912 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700913 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700914
915 // layerR = 0, layerG = layerR + 3, layerB = 2
916 Transaction()
917 .setPosition(layerG, 8, 8)
918 .setRelativeLayer(layerG, layerR->getHandle(), 3)
919 .setPosition(layerB, 16, 16)
920 .setLayer(layerB, mLayerZBase + 2)
921 .apply();
922 {
923 SCOPED_TRACE("(layerR < layerG) < layerB");
924 auto shot = screenshot();
925 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
926 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
927 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
928 }
929
930 // layerR = 4, layerG = layerR + 3, layerB = 2
931 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
932 {
933 SCOPED_TRACE("layerB < (layerR < layerG)");
934 auto shot = screenshot();
935 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
936 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
937 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
938 }
939
940 // layerR = 4, layerG = layerR - 3, layerB = 2
941 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
942 {
943 SCOPED_TRACE("layerB < (layerG < layerR)");
944 auto shot = screenshot();
945 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
946 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
947 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
948 }
949
950 // restore to absolute z
951 // layerR = 4, layerG = 0, layerB = 2
952 Transaction().setLayer(layerG, mLayerZBase).apply();
953 {
954 SCOPED_TRACE("layerG < layerB < layerR");
955 auto shot = screenshot();
956 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
957 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
958 }
959
960 // layerR should not affect layerG anymore
961 // layerR = 1, layerG = 0, layerB = 2
962 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
963 {
964 SCOPED_TRACE("layerG < layerR < layerB");
965 auto shot = screenshot();
966 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
967 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
968 }
969}
970
Marissa Wall61c58622018-07-18 10:12:20 -0700971TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700972 sp<SurfaceControl> layerR;
973 sp<SurfaceControl> layerG;
974
975 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700976 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700977 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700978 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700979
980 Transaction()
981 .setPosition(layerG, 16, 16)
982 .setRelativeLayer(layerG, layerR->getHandle(), 1)
983 .apply();
984
985 mClient->destroySurface(layerG->getHandle());
986 // layerG should have been removed
987 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
988}
989
Marissa Wall61c58622018-07-18 10:12:20 -0700990TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700991 sp<SurfaceControl> layer;
992 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700993 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700994
995 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
996 {
997 SCOPED_TRACE("layer hidden");
998 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
999 }
1000
1001 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1002 {
1003 SCOPED_TRACE("layer shown");
1004 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1005 }
1006}
1007
Marissa Wall61c58622018-07-18 10:12:20 -07001008TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001009 const Color translucentRed = {100, 0, 0, 100};
1010 sp<SurfaceControl> layerR;
1011 sp<SurfaceControl> layerG;
1012 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001013 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001014 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001015 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001016
1017 Transaction()
1018 .setLayer(layerR, mLayerZBase + 1)
1019 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1020 .apply();
1021 {
1022 SCOPED_TRACE("layerR opaque");
1023 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1024 }
1025
1026 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1027 {
1028 SCOPED_TRACE("layerR translucent");
1029 const uint8_t g = uint8_t(255 - translucentRed.a);
1030 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1031 }
1032}
1033
Marissa Wall61c58622018-07-18 10:12:20 -07001034TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001035 sp<SurfaceControl> layer;
1036 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001037 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001038
1039 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001040 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001041 Transaction()
1042 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1043 .apply(true);
1044 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001045 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001046
1047 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1048 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001049 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001050}
1051
Marissa Wall61c58622018-07-18 10:12:20 -07001052TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001053 const Rect top(0, 0, 32, 16);
1054 const Rect bottom(0, 16, 32, 32);
1055 sp<SurfaceControl> layer;
1056 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1057
1058 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001059 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1060 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1061 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001062 // setTransparentRegionHint always applies to the following buffer
1063 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001064 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001065 {
1066 SCOPED_TRACE("top transparent");
1067 auto shot = screenshot();
1068 shot->expectColor(top, Color::BLACK);
1069 shot->expectColor(bottom, Color::RED);
1070 }
1071
1072 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1073 {
1074 SCOPED_TRACE("transparent region hint pending");
1075 auto shot = screenshot();
1076 shot->expectColor(top, Color::BLACK);
1077 shot->expectColor(bottom, Color::RED);
1078 }
1079
Marissa Wall61c58622018-07-18 10:12:20 -07001080 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1081 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1082 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1083 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001084 {
1085 SCOPED_TRACE("bottom transparent");
1086 auto shot = screenshot();
1087 shot->expectColor(top, Color::RED);
1088 shot->expectColor(bottom, Color::BLACK);
1089 }
1090}
1091
Marissa Wall61c58622018-07-18 10:12:20 -07001092TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1093 const Rect top(0, 0, 32, 16);
1094 const Rect bottom(0, 16, 32, 32);
1095 sp<SurfaceControl> layer;
1096 ASSERT_NO_FATAL_FAILURE(
1097 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1098
1099 sp<GraphicBuffer> buffer =
1100 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1101 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1102 BufferUsage::COMPOSER_OVERLAY,
1103 "test");
1104
1105 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1106 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1107 Transaction()
1108 .setTransparentRegionHint(layer, Region(top))
1109 .setBuffer(layer, buffer)
1110 .setSize(layer, 32, 32)
1111 .apply();
1112 {
1113 SCOPED_TRACE("top transparent");
1114 auto shot = screenshot();
1115 shot->expectColor(top, Color::BLACK);
1116 shot->expectColor(bottom, Color::RED);
1117 }
1118
1119 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1120 {
1121 SCOPED_TRACE("transparent region hint intermediate");
1122 auto shot = screenshot();
1123 shot->expectColor(top, Color::BLACK);
1124 shot->expectColor(bottom, Color::BLACK);
1125 }
1126
1127 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1128 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1129 BufferUsage::COMPOSER_OVERLAY,
1130 "test");
1131
1132 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1133 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1134 Transaction().setBuffer(layer, buffer).setSize(layer, 32, 32).apply();
1135 {
1136 SCOPED_TRACE("bottom transparent");
1137 auto shot = screenshot();
1138 shot->expectColor(top, Color::RED);
1139 shot->expectColor(bottom, Color::BLACK);
1140 }
1141}
1142
1143TEST_P(LayerTypeTransactionTest, SetTransparentRegionHintOutOfBounds) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001144 sp<SurfaceControl> layerTransparent;
1145 sp<SurfaceControl> layerR;
1146 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1147 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1148
1149 // check that transparent region hint is bound by the layer size
1150 Transaction()
1151 .setTransparentRegionHint(layerTransparent,
1152 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1153 .setPosition(layerR, 16, 16)
1154 .setLayer(layerR, mLayerZBase + 1)
1155 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001156 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1157 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001158 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1159}
1160
Marissa Wall61c58622018-07-18 10:12:20 -07001161TEST_P(LayerTypeTransactionTest, SetAlphaBasic) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001162 sp<SurfaceControl> layer1;
1163 sp<SurfaceControl> layer2;
1164 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1165 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001166 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}, 32, 32));
1167 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001168
1169 Transaction()
1170 .setAlpha(layer1, 0.25f)
1171 .setAlpha(layer2, 0.75f)
1172 .setPosition(layer2, 16, 0)
1173 .setLayer(layer2, mLayerZBase + 1)
1174 .apply();
1175 {
1176 auto shot = screenshot();
1177 uint8_t r = 16; // 64 * 0.25f
1178 uint8_t g = 48; // 64 * 0.75f
1179 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1180 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1181
1182 r /= 4; // r * (1.0f - 0.75f)
1183 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1184 }
1185}
1186
Marissa Wall61c58622018-07-18 10:12:20 -07001187TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001188 const Color color = {64, 0, 0, 255};
1189 sp<SurfaceControl> layer;
1190 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001191 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001192
1193 Transaction().setAlpha(layer, 2.0f).apply();
1194 {
1195 SCOPED_TRACE("clamped to 1.0f");
1196 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1197 }
1198
1199 Transaction().setAlpha(layer, -1.0f).apply();
1200 {
1201 SCOPED_TRACE("clamped to 0.0f");
1202 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1203 }
1204}
1205
Chia-I Wue4ef6102017-11-01 15:16:35 -07001206TEST_F(LayerTransactionTest, SetColorBasic) {
1207 sp<SurfaceControl> bufferLayer;
1208 sp<SurfaceControl> colorLayer;
1209 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001210 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001211 ASSERT_NO_FATAL_FAILURE(
1212 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1213
1214 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1215 {
1216 SCOPED_TRACE("default color");
1217 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1218 }
1219
1220 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1221 const Color expected = {15, 51, 85, 255};
1222 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1223 // channel) should be less than one
1224 const uint8_t tolerance = 1;
1225 Transaction().setColor(colorLayer, color).apply();
1226 {
1227 SCOPED_TRACE("new color");
1228 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1229 }
1230}
1231
1232TEST_F(LayerTransactionTest, SetColorClamped) {
1233 sp<SurfaceControl> colorLayer;
1234 ASSERT_NO_FATAL_FAILURE(
1235 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1236
1237 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1238 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1239}
1240
1241TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1242 sp<SurfaceControl> bufferLayer;
1243 sp<SurfaceControl> colorLayer;
1244 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001245 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001246 ASSERT_NO_FATAL_FAILURE(
1247 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1248
1249 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1250 const float alpha = 0.25f;
1251 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1252 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1253 // channel) should be less than one
1254 const uint8_t tolerance = 1;
1255 Transaction()
1256 .setColor(colorLayer, color)
1257 .setAlpha(colorLayer, alpha)
1258 .setLayer(colorLayer, mLayerZBase + 1)
1259 .apply();
1260 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1261 tolerance);
1262}
1263
Adrian Roosb7a96502018-04-08 11:38:55 -07001264TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1265 sp<SurfaceControl> bufferLayer;
1266 sp<SurfaceControl> parentLayer;
1267 sp<SurfaceControl> colorLayer;
1268 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1269 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001270 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Adrian Roosb7a96502018-04-08 11:38:55 -07001271 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1272 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1273
1274 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1275 const float alpha = 0.25f;
1276 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1277 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1278 // channel) should be less than one
1279 const uint8_t tolerance = 1;
1280 Transaction()
1281 .reparent(colorLayer, parentLayer->getHandle())
1282 .setColor(colorLayer, color)
1283 .setAlpha(parentLayer, alpha)
1284 .setLayer(parentLayer, mLayerZBase + 1)
1285 .apply();
1286 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1287 tolerance);
1288}
1289
Marissa Wall61c58622018-07-18 10:12:20 -07001290TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001291 sp<SurfaceControl> bufferLayer;
1292 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001293 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001294
1295 // color is ignored
1296 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1297 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1298}
1299
Marissa Wall61c58622018-07-18 10:12:20 -07001300TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001301 sp<SurfaceControl> layer;
1302 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001303 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001304
1305 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1306 {
1307 SCOPED_TRACE("non-existing layer stack");
1308 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1309 }
1310
1311 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1312 {
1313 SCOPED_TRACE("original layer stack");
1314 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1315 }
1316}
1317
Marissa Wall61c58622018-07-18 10:12:20 -07001318TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001319 sp<SurfaceControl> layer;
1320 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1321 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001322 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001323
1324 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1325 {
1326 SCOPED_TRACE("IDENTITY");
1327 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1328 Color::WHITE);
1329 }
1330
1331 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1332 {
1333 SCOPED_TRACE("FLIP_H");
1334 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1335 Color::BLUE);
1336 }
1337
1338 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1339 {
1340 SCOPED_TRACE("FLIP_V");
1341 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1342 Color::GREEN);
1343 }
1344
1345 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1346 {
1347 SCOPED_TRACE("ROT_90");
1348 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1349 Color::GREEN);
1350 }
1351
1352 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1353 {
1354 SCOPED_TRACE("SCALE");
1355 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1356 Color::WHITE, true /* filtered */);
1357 }
1358}
1359
Marissa Wall61c58622018-07-18 10:12:20 -07001360TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001361 sp<SurfaceControl> layer;
1362 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1363 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001364 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001365
1366 const float rot = M_SQRT1_2; // 45 degrees
1367 const float trans = M_SQRT2 * 16.0f;
1368 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1369
1370 auto shot = screenshot();
1371 // check a 8x8 region inside each color
1372 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1373 const int32_t halfL = 4;
1374 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1375 };
1376 const int32_t unit = int32_t(trans / 2);
1377 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1378 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1379 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1380 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1381}
1382
Marissa Wall61c58622018-07-18 10:12:20 -07001383void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001384 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001385 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1386 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001387
1388 // setMatrix is applied after any pending resize, unlike setPosition
1389 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1390 {
1391 SCOPED_TRACE("resize pending");
1392 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001393 Rect rect;
1394 switch (layerType) {
1395 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1396 rect = {0, 0, 32, 32};
1397 break;
1398 case ISurfaceComposerClient::eFXSurfaceBufferState:
1399 rect = {0, 0, 128, 128};
1400 break;
1401 default:
1402 ASSERT_FALSE(true) << "Unsupported layer type";
1403 }
1404 shot->expectColor(rect, Color::RED);
1405 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001406 }
1407
Marissa Wall61c58622018-07-18 10:12:20 -07001408 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001409 {
1410 SCOPED_TRACE("resize applied");
1411 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1412 }
1413}
1414
Marissa Wall61c58622018-07-18 10:12:20 -07001415TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1416 ASSERT_NO_FATAL_FAILURE(
1417 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1418}
1419
1420TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1421 ASSERT_NO_FATAL_FAILURE(
1422 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1423}
1424
1425TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001426 sp<SurfaceControl> layer;
1427 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001428 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001429
1430 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1431 Transaction()
1432 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1433 .setSize(layer, 64, 64)
1434 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1435 .apply();
1436 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1437}
1438
Marissa Wall61c58622018-07-18 10:12:20 -07001439TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001440 sp<SurfaceControl> layer;
1441 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1442 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001443 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001444
1445 // XXX SCALE_CROP is not respected; calling setSize and
1446 // setOverrideScalingMode in separate transactions does not work
1447 // (b/69315456)
1448 Transaction()
1449 .setSize(layer, 64, 16)
1450 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1451 .apply();
1452 {
1453 SCOPED_TRACE("SCALE_TO_WINDOW");
1454 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1455 Color::WHITE, true /* filtered */);
1456 }
1457}
1458
Dan Stoza000dd012018-08-01 13:31:52 -07001459TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1460 sp<SurfaceControl> layer;
1461 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1462
1463 sp<IBinder> handle = layer->getHandle();
1464 ASSERT_TRUE(handle != nullptr);
1465
1466 FrameStats frameStats;
1467 mClient->getLayerFrameStats(handle, &frameStats);
1468
1469 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1470}
1471
Marissa Wall61c58622018-07-18 10:12:20 -07001472TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001473 sp<SurfaceControl> layer;
1474 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001475 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001476 const Rect crop(8, 8, 24, 24);
1477
Marissa Wallf58c14b2018-07-24 10:50:43 -07001478 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001479 auto shot = screenshot();
1480 shot->expectColor(crop, Color::RED);
1481 shot->expectBorder(crop, Color::BLACK);
1482}
1483
Marissa Wall61c58622018-07-18 10:12:20 -07001484TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1485 sp<SurfaceControl> layer;
1486 ASSERT_NO_FATAL_FAILURE(
1487 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1488 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1489 const Rect crop(8, 8, 24, 24);
1490
1491 Transaction().setCrop(layer, crop).apply();
1492 auto shot = screenshot();
1493 shot->expectColor(crop, Color::RED);
1494 shot->expectBorder(crop, Color::BLACK);
1495}
1496
1497TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001498 sp<SurfaceControl> layer;
1499 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001500 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001501
1502 {
1503 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001504 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001505 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1506 }
1507
1508 {
1509 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001510 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001511 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1512 }
1513}
1514
Marissa Wall61c58622018-07-18 10:12:20 -07001515TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1516 sp<SurfaceControl> layer;
1517 ASSERT_NO_FATAL_FAILURE(
1518 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1519 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1520
1521 {
1522 SCOPED_TRACE("empty rect");
1523 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1524 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1525 }
1526
1527 {
1528 SCOPED_TRACE("negative rect");
1529 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1530 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1531 }
1532}
1533
1534TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001535 sp<SurfaceControl> layer;
1536 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001537 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001538
Marissa Wallf58c14b2018-07-24 10:50:43 -07001539 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001540 auto shot = screenshot();
1541 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1542 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1543}
1544
Marissa Wall61c58622018-07-18 10:12:20 -07001545TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1546 sp<SurfaceControl> layer;
1547 ASSERT_NO_FATAL_FAILURE(
1548 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1549 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1550
1551 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1552 auto shot = screenshot();
1553 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1554 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1555}
1556
1557TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001558 sp<SurfaceControl> layer;
1559 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001560 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001561
1562 const Point position(32, 32);
1563 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001564 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001565 auto shot = screenshot();
1566 shot->expectColor(crop + position, Color::RED);
1567 shot->expectBorder(crop + position, Color::BLACK);
1568}
1569
Marissa Wall61c58622018-07-18 10:12:20 -07001570TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1571 sp<SurfaceControl> layer;
1572 ASSERT_NO_FATAL_FAILURE(
1573 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1574 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1575
1576 const Point position(32, 32);
1577 const Rect crop(8, 8, 24, 24);
1578 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1579 auto shot = screenshot();
1580 shot->expectColor(crop + position, Color::RED);
1581 shot->expectBorder(crop + position, Color::BLACK);
1582}
1583
1584TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001585 sp<SurfaceControl> layer;
1586 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001587 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001588
1589 // crop is affected by matrix
1590 Transaction()
1591 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001592 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001593 .apply();
1594 auto shot = screenshot();
1595 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1596 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1597}
1598
Marissa Wall61c58622018-07-18 10:12:20 -07001599TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1600 sp<SurfaceControl> layer;
1601 ASSERT_NO_FATAL_FAILURE(
1602 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1603 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1604
1605 // crop is affected by matrix
1606 Transaction()
1607 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1608 .setCrop(layer, Rect(8, 8, 24, 24))
1609 .apply();
1610 auto shot = screenshot();
1611 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1612 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1613}
1614
1615TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001616 sp<SurfaceControl> layer;
1617 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001618 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001619
Marissa Wallf58c14b2018-07-24 10:50:43 -07001620 // setCrop_legacy is applied immediately by default, with or without resize pending
1621 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001622 {
1623 SCOPED_TRACE("resize pending");
1624 auto shot = screenshot();
1625 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1626 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1627 }
1628
Marissa Wall61c58622018-07-18 10:12:20 -07001629 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001630 {
1631 SCOPED_TRACE("resize applied");
1632 auto shot = screenshot();
1633 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1634 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1635 }
1636}
1637
Marissa Wall61c58622018-07-18 10:12:20 -07001638TEST_F(LayerTransactionTest, SetCropWithResize_BufferState) {
1639 sp<SurfaceControl> layer;
1640 ASSERT_NO_FATAL_FAILURE(
1641 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1642 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1643
1644 // setCrop_legacy is applied immediately by default, with or without resize pending
1645 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1646 {
1647 SCOPED_TRACE("new buffer pending");
1648 auto shot = screenshot();
1649 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1650 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1651 }
1652
1653 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1654 {
1655 SCOPED_TRACE("new buffer");
1656 auto shot = screenshot();
1657 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1658 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1659 }
1660}
1661
1662TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001663 sp<SurfaceControl> layer;
1664 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001665 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001666
Marissa Wallf58c14b2018-07-24 10:50:43 -07001667 // request setCrop_legacy to be applied with the next resize
1668 Transaction()
1669 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1670 .setGeometryAppliesWithResize(layer)
1671 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001672 {
1673 SCOPED_TRACE("waiting for next resize");
1674 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1675 }
1676
Marissa Wallf58c14b2018-07-24 10:50:43 -07001677 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001678 {
1679 SCOPED_TRACE("pending crop modified");
1680 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1681 }
1682
1683 Transaction().setSize(layer, 16, 16).apply();
1684 {
1685 SCOPED_TRACE("resize pending");
1686 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1687 }
1688
1689 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001690 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001691 {
1692 SCOPED_TRACE("new crop applied");
1693 auto shot = screenshot();
1694 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1695 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1696 }
1697}
1698
Marissa Wall61c58622018-07-18 10:12:20 -07001699TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1700 sp<SurfaceControl> layer;
1701 ASSERT_NO_FATAL_FAILURE(
1702 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1703 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1704
1705 // request setCrop_legacy to be applied with the next resize
1706 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1707 {
1708 SCOPED_TRACE("set crop 1");
1709 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1710 }
1711
1712 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1713 {
1714 SCOPED_TRACE("set crop 2");
1715 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1716 }
1717
1718 Transaction().setSize(layer, 16, 16).apply();
1719 {
1720 SCOPED_TRACE("resize");
1721 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1722 }
1723
1724 // finally resize
1725 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1726 {
1727 SCOPED_TRACE("new buffer");
1728 auto shot = screenshot();
1729 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1730 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1731 }
1732}
1733
1734TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001735 sp<SurfaceControl> layer;
1736 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001737 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001738
Marissa Wallf58c14b2018-07-24 10:50:43 -07001739 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001740 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001741 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001742 .setSize(layer, 16, 16)
1743 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1744 .setGeometryAppliesWithResize(layer)
1745 .apply();
1746 {
1747 SCOPED_TRACE("new crop pending");
1748 auto shot = screenshot();
1749 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1750 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1751 }
1752
1753 // XXX crop is never latched without other geometry change (b/69315677)
1754 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001755 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001756 Transaction().setPosition(layer, 0, 0).apply();
1757 {
1758 SCOPED_TRACE("new crop applied");
1759 auto shot = screenshot();
1760 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1761 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1762 }
1763}
1764
Marissa Wall61c58622018-07-18 10:12:20 -07001765TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1766 sp<SurfaceControl> layer;
1767 ASSERT_NO_FATAL_FAILURE(
1768 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1769 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1770
1771 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1772 Transaction()
1773 .setCrop(layer, Rect(4, 4, 12, 12))
1774 .setSize(layer, 16, 16)
1775 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1776 .setGeometryAppliesWithResize(layer)
1777 .apply();
1778 {
1779 SCOPED_TRACE("new crop pending");
1780 auto shot = screenshot();
1781 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1782 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1783 }
1784
1785 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1786 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1787 Transaction().setPosition(layer, 0, 0).apply();
1788 {
1789 SCOPED_TRACE("new crop applied");
1790 auto shot = screenshot();
1791 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1792 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1793 }
1794}
1795
Marissa Wall61c58622018-07-18 10:12:20 -07001796TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1797 sp<SurfaceControl> layer;
1798 ASSERT_NO_FATAL_FAILURE(
1799 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1800
1801 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1802
1803 auto shot = screenshot();
1804 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1805 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1806}
1807
1808TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1809 sp<SurfaceControl> layer;
1810 ASSERT_NO_FATAL_FAILURE(
1811 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1812
1813 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1814
1815 {
1816 SCOPED_TRACE("set buffer 1");
1817 auto shot = screenshot();
1818 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1819 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1820 }
1821
1822 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1823
1824 {
1825 SCOPED_TRACE("set buffer 2");
1826 auto shot = screenshot();
1827 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1828 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1829 }
1830
1831 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1832
1833 {
1834 SCOPED_TRACE("set buffer 3");
1835 auto shot = screenshot();
1836 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1837 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1838 }
1839}
1840
1841TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1842 sp<SurfaceControl> layer1;
1843 ASSERT_NO_FATAL_FAILURE(
1844 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1845
1846 sp<SurfaceControl> layer2;
1847 ASSERT_NO_FATAL_FAILURE(
1848 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1849
1850 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1851
1852 {
1853 SCOPED_TRACE("set layer 1 buffer red");
1854 auto shot = screenshot();
1855 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1856 }
1857
1858 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1859
1860 {
1861 SCOPED_TRACE("set layer 2 buffer blue");
1862 auto shot = screenshot();
1863 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1864 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1865 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1866 }
1867
1868 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1869 {
1870 SCOPED_TRACE("set layer 1 buffer green");
1871 auto shot = screenshot();
1872 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1873 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1874 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1875 }
1876
1877 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1878
1879 {
1880 SCOPED_TRACE("set layer 2 buffer white");
1881 auto shot = screenshot();
1882 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1883 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1884 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1885 }
1886}
1887
1888TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1889 sp<SurfaceControl> layer;
1890 ASSERT_NO_FATAL_FAILURE(
1891 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1892
1893 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1894 Color::BLUE, Color::WHITE));
1895
1896 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1897
1898 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1899 Color::GREEN, true /* filtered */);
1900}
1901
1902TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1903 sp<SurfaceControl> layer;
1904 ASSERT_NO_FATAL_FAILURE(
1905 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1906
1907 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1908 Color::BLUE, Color::WHITE));
1909
1910 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1911
1912 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1913 Color::BLUE, true /* filtered */);
1914}
1915
1916TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
1917 sp<SurfaceControl> layer;
1918 ASSERT_NO_FATAL_FAILURE(
1919 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1920
1921 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1922 Color::BLUE, Color::WHITE));
1923
1924 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
1925
1926 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1927 Color::GREEN, true /* filtered */);
1928}
1929
1930TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
1931 sp<SurfaceControl> layer;
1932 ASSERT_NO_FATAL_FAILURE(
1933 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1934
1935 Transaction().setTransformToDisplayInverse(layer, false).apply();
1936
1937 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
1938
1939 Transaction().setTransformToDisplayInverse(layer, true).apply();
1940}
1941
1942TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
1943 sp<SurfaceControl> layer;
1944 ASSERT_NO_FATAL_FAILURE(
1945 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1946
1947 sp<GraphicBuffer> buffer =
1948 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1949 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1950 BufferUsage::COMPOSER_OVERLAY,
1951 "test");
1952 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1953
1954 sp<Fence> fence = new Fence(-1);
1955
1956 Transaction()
1957 .setBuffer(layer, buffer)
1958 .setAcquireFence(layer, fence)
1959 .setSize(layer, 32, 32)
1960 .apply();
1961
1962 auto shot = screenshot();
1963 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1964 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1965}
1966
1967TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
1968 sp<SurfaceControl> layer;
1969 ASSERT_NO_FATAL_FAILURE(
1970 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1971
1972 sp<GraphicBuffer> buffer =
1973 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1974 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1975 BufferUsage::COMPOSER_OVERLAY,
1976 "test");
1977 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1978
1979 Transaction()
1980 .setBuffer(layer, buffer)
1981 .setDataspace(layer, ui::Dataspace::UNKNOWN)
1982 .setSize(layer, 32, 32)
1983 .apply();
1984
1985 auto shot = screenshot();
1986 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1987 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1988}
1989
1990TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
1991 sp<SurfaceControl> layer;
1992 ASSERT_NO_FATAL_FAILURE(
1993 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1994
1995 sp<GraphicBuffer> buffer =
1996 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1997 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1998 BufferUsage::COMPOSER_OVERLAY,
1999 "test");
2000 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2001
2002 HdrMetadata hdrMetadata;
2003 hdrMetadata.validTypes = 0;
2004 Transaction()
2005 .setBuffer(layer, buffer)
2006 .setHdrMetadata(layer, hdrMetadata)
2007 .setSize(layer, 32, 32)
2008 .apply();
2009
2010 auto shot = screenshot();
2011 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2012 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2013}
2014
2015TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2016 sp<SurfaceControl> layer;
2017 ASSERT_NO_FATAL_FAILURE(
2018 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2019
2020 sp<GraphicBuffer> buffer =
2021 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2022 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2023 BufferUsage::COMPOSER_OVERLAY,
2024 "test");
2025 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2026
2027 Region region;
2028 region.set(32, 32);
2029 Transaction()
2030 .setBuffer(layer, buffer)
2031 .setSurfaceDamageRegion(layer, region)
2032 .setSize(layer, 32, 32)
2033 .apply();
2034
2035 auto shot = screenshot();
2036 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2037 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2038}
2039
2040TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2041 sp<SurfaceControl> layer;
2042 ASSERT_NO_FATAL_FAILURE(
2043 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2044
2045 sp<GraphicBuffer> buffer =
2046 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2047 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2048 BufferUsage::COMPOSER_OVERLAY,
2049 "test");
2050 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2051
2052 Transaction()
2053 .setBuffer(layer, buffer)
2054 .setApi(layer, NATIVE_WINDOW_API_CPU)
2055 .setSize(layer, 32, 32)
2056 .apply();
2057
2058 auto shot = screenshot();
2059 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2060 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2061}
2062
2063TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2064 sp<SurfaceControl> layer;
2065 ASSERT_NO_FATAL_FAILURE(
2066 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2067
2068 // verify this doesn't cause a crash
2069 Transaction().setSidebandStream(layer, nullptr).apply();
2070}
2071
Peiyong Lind3788632018-09-18 16:01:31 -07002072TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2073 sp<SurfaceControl> colorLayer;
2074 ASSERT_NO_FATAL_FAILURE(
2075 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
2076
2077 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
2078 {
2079 SCOPED_TRACE("default color");
2080 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2081 }
2082
2083 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2084 const Color expected = {90, 90, 90, 255};
2085 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2086 // channel) should be less than one
2087 const uint8_t tolerance = 1;
2088 mat3 matrix;
2089 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2090 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2091 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2092 Transaction().setColor(colorLayer, color)
2093 .setColorTransform(colorLayer, matrix, vec3()).apply();
2094 {
2095 SCOPED_TRACE("new color");
2096 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
2097 }
2098}
2099
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002100class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002101protected:
2102 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002103 LayerTransactionTest::SetUp();
2104 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002105
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002106 sp<IBinder> display(
2107 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002108 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002109 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002110
2111 ssize_t displayWidth = info.w;
2112 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002113
2114 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002115 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2116 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002117 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002118 ASSERT_TRUE(mBGSurfaceControl->isValid());
2119 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2120
2121 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002122 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2123
Peiyong Lin566a3b42018-01-09 18:22:43 -08002124 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002125 ASSERT_TRUE(mFGSurfaceControl->isValid());
2126
2127 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2128
2129 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002130 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002131 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002132 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2133
2134 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2135
Robert Carr4cdc58f2017-08-23 14:22:20 -07002136 asTransaction([&](Transaction& t) {
2137 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002138
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002139 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002140
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002141 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2142 .setPosition(mFGSurfaceControl, 64, 64)
2143 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002144
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002145 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2146 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2147 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002148 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002149 }
2150
2151 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002152 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002153 mBGSurfaceControl = 0;
2154 mFGSurfaceControl = 0;
2155 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002156 }
2157
2158 void waitForPostedBuffers() {
2159 // Since the sync surface is in synchronous mode (i.e. double buffered)
2160 // posting three buffers to it should ensure that at least two
2161 // SurfaceFlinger::handlePageFlip calls have been made, which should
2162 // guaranteed that a buffer posted to another Surface has been retired.
2163 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2164 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2165 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2166 }
2167
Robert Carr4cdc58f2017-08-23 14:22:20 -07002168 void asTransaction(const std::function<void(Transaction&)>& exec) {
2169 Transaction t;
2170 exec(t);
2171 t.apply(true);
2172 }
2173
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002174 sp<SurfaceControl> mBGSurfaceControl;
2175 sp<SurfaceControl> mFGSurfaceControl;
2176
2177 // This surface is used to ensure that the buffers posted to
2178 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2179 sp<SurfaceControl> mSyncSurfaceControl;
2180};
2181
Robert Carr7f619b22017-11-06 12:56:35 -08002182TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08002183
chaviw0e3479f2018-09-10 16:49:30 -07002184 std::unique_ptr<ScreenCapture> sc;
2185
2186 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002187 fillSurfaceRGBA8(relative, 10, 10, 10);
2188 waitForPostedBuffers();
2189
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002190 Transaction{}
2191 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002192 .setPosition(relative, 64, 64)
2193 .apply();
2194
2195 {
2196 // The relative should be on top of the FG control.
2197 ScreenCapture::captureScreen(&sc);
2198 sc->checkPixel(64, 64, 10, 10, 10);
2199 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002200 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002201
2202 {
2203 // Nothing should change at this point.
2204 ScreenCapture::captureScreen(&sc);
2205 sc->checkPixel(64, 64, 10, 10, 10);
2206 }
2207
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002208 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002209
2210 {
2211 // Ensure that the relative was actually hidden, rather than
2212 // being left in the detached but visible state.
2213 ScreenCapture::captureScreen(&sc);
2214 sc->expectFGColor(64, 64);
2215 }
2216}
2217
Robert Carr8d5227b2017-03-16 15:41:03 -07002218class GeometryLatchingTest : public LayerUpdateTest {
2219protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002220 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002221 SCOPED_TRACE(trace);
2222 ScreenCapture::captureScreen(&sc);
2223 // We find the leading edge of the FG surface.
2224 sc->expectFGColor(127, 127);
2225 sc->expectBGColor(128, 128);
2226 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002227
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002228 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002229
2230 void unlockFGBuffer() {
2231 sp<Surface> s = mFGSurfaceControl->getSurface();
2232 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2233 waitForPostedBuffers();
2234 }
2235
Robert Carr8d5227b2017-03-16 15:41:03 -07002236 void completeFGResize() {
2237 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2238 waitForPostedBuffers();
2239 }
2240 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002241 asTransaction([&](Transaction& t) {
2242 t.setSize(mFGSurfaceControl, 64, 64);
2243 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002244 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002245 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002246
2247 EXPECT_INITIAL_STATE("After restoring initial state");
2248 }
chaviw0e3479f2018-09-10 16:49:30 -07002249 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07002250};
2251
Robert Carr8d5227b2017-03-16 15:41:03 -07002252class CropLatchingTest : public GeometryLatchingTest {
2253protected:
2254 void EXPECT_CROPPED_STATE(const char* trace) {
2255 SCOPED_TRACE(trace);
2256 ScreenCapture::captureScreen(&sc);
2257 // The edge should be moved back one pixel by our crop.
2258 sc->expectFGColor(126, 126);
2259 sc->expectBGColor(127, 127);
2260 sc->expectBGColor(128, 128);
2261 }
chaviw59f5c562017-06-28 16:39:06 -07002262
2263 void EXPECT_RESIZE_STATE(const char* trace) {
2264 SCOPED_TRACE(trace);
2265 ScreenCapture::captureScreen(&sc);
2266 // The FG is now resized too 128,128 at 64,64
2267 sc->expectFGColor(64, 64);
2268 sc->expectFGColor(191, 191);
2269 sc->expectBGColor(192, 192);
2270 }
Robert Carr8d5227b2017-03-16 15:41:03 -07002271};
2272
Pablo Ceballos05289c22016-04-14 15:49:55 -07002273TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07002274 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07002275 {
2276 SCOPED_TRACE("before anything");
2277 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002278 sc->expectBGColor(32, 32);
2279 sc->expectFGColor(96, 96);
2280 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002281 }
2282
2283 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002284 asTransaction([&](Transaction& t) {
2285 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002286 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2287 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002288 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002289
Robert Carr4cdc58f2017-08-23 14:22:20 -07002290 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002291 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002292 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2293 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002294 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002295
2296 {
2297 SCOPED_TRACE("before any trigger");
2298 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002299 sc->expectBGColor(32, 32);
2300 sc->expectFGColor(96, 96);
2301 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002302 }
2303
2304 // should trigger the first deferred transaction, but not the second one
2305 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2306 {
2307 SCOPED_TRACE("after first trigger");
2308 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002309 sc->expectBGColor(32, 32);
2310 sc->checkPixel(96, 96, 162, 63, 96);
2311 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002312 }
2313
2314 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002315 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002316
2317 // trigger the second deferred transaction
2318 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2319 {
2320 SCOPED_TRACE("after second trigger");
2321 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002322 sc->expectBGColor(32, 32);
2323 sc->expectBGColor(96, 96);
2324 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002325 }
2326}
2327
Robert Carre392b552017-09-19 12:16:05 -07002328TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07002329 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07002330
2331 sp<SurfaceControl> childNoBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07002332 mClient->createSurface(String8("Bufferless child"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002333 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2334 sp<SurfaceControl> childBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07002335 mClient->createSurface(String8("Buffered child"), 20, 20,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002336 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002337 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08002338 SurfaceComposerClient::Transaction{}
2339 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
2340 .show(childNoBuffer)
2341 .show(childBuffer)
2342 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002343 {
2344 ScreenCapture::captureScreen(&sc);
2345 sc->expectChildColor(73, 73);
2346 sc->expectFGColor(74, 74);
2347 }
Vishnu Nair60356342018-11-13 13:00:45 -08002348 SurfaceComposerClient::Transaction{}
2349 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
2350 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002351 {
2352 ScreenCapture::captureScreen(&sc);
2353 sc->expectChildColor(73, 73);
2354 sc->expectChildColor(74, 74);
2355 }
2356}
2357
Robert Carr2c5f6d22017-09-26 12:30:35 -07002358TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07002359 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07002360 {
2361 SCOPED_TRACE("before move");
2362 ScreenCapture::captureScreen(&sc);
2363 sc->expectBGColor(0, 12);
2364 sc->expectFGColor(75, 75);
2365 sc->expectBGColor(145, 145);
2366 }
2367
2368 Transaction t1, t2;
2369 t1.setPosition(mFGSurfaceControl, 128, 128);
2370 t2.setPosition(mFGSurfaceControl, 0, 0);
2371 // We expect that the position update from t2 now
2372 // overwrites the position update from t1.
2373 t1.merge(std::move(t2));
2374 t1.apply();
2375
2376 {
2377 ScreenCapture::captureScreen(&sc);
2378 sc->expectFGColor(1, 1);
2379 }
2380}
2381
Robert Carr1f0a16a2016-10-24 16:27:39 -07002382class ChildLayerTest : public LayerUpdateTest {
2383protected:
2384 void SetUp() override {
2385 LayerUpdateTest::SetUp();
chaviw0e3479f2018-09-10 16:49:30 -07002386 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002387 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002388 fillSurfaceRGBA8(mChild, 200, 200, 200);
2389
2390 {
2391 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07002392 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002393 mCapture->expectChildColor(64, 64);
2394 }
2395 }
2396 void TearDown() override {
2397 LayerUpdateTest::TearDown();
2398 mChild = 0;
2399 }
2400
2401 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07002402 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002403};
2404
2405TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002406 asTransaction([&](Transaction& t) {
2407 t.show(mChild);
2408 t.setPosition(mChild, 10, 10);
2409 t.setPosition(mFGSurfaceControl, 64, 64);
2410 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002411
2412 {
chaviw0e3479f2018-09-10 16:49:30 -07002413 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002414 // Top left of foreground must now be visible
2415 mCapture->expectFGColor(64, 64);
2416 // But 10 pixels in we should see the child surface
2417 mCapture->expectChildColor(74, 74);
2418 // And 10 more pixels we should be back to the foreground surface
2419 mCapture->expectFGColor(84, 84);
2420 }
2421
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002422 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002423
2424 {
chaviw0e3479f2018-09-10 16:49:30 -07002425 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002426 // Top left of foreground should now be at 0, 0
2427 mCapture->expectFGColor(0, 0);
2428 // But 10 pixels in we should see the child surface
2429 mCapture->expectChildColor(10, 10);
2430 // And 10 more pixels we should be back to the foreground surface
2431 mCapture->expectFGColor(20, 20);
2432 }
2433}
2434
Robert Carr41b08b52017-06-01 16:11:34 -07002435TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002436 asTransaction([&](Transaction& t) {
2437 t.show(mChild);
2438 t.setPosition(mChild, 0, 0);
2439 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002440 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002441 });
Robert Carr41b08b52017-06-01 16:11:34 -07002442
2443 {
chaviw0e3479f2018-09-10 16:49:30 -07002444 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07002445 mCapture->expectChildColor(0, 0);
2446 mCapture->expectChildColor(4, 4);
2447 mCapture->expectBGColor(5, 5);
2448 }
2449}
2450
Robert Carr1f0a16a2016-10-24 16:27:39 -07002451TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002452 asTransaction([&](Transaction& t) {
2453 t.show(mChild);
2454 t.setPosition(mFGSurfaceControl, 0, 0);
2455 t.setPosition(mChild, 63, 63);
2456 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002457
2458 {
chaviw0e3479f2018-09-10 16:49:30 -07002459 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002460 mCapture->expectFGColor(0, 0);
2461 // Last pixel in foreground should now be the child.
2462 mCapture->expectChildColor(63, 63);
2463 // But the child should be constrained and the next pixel
2464 // must be the background
2465 mCapture->expectBGColor(64, 64);
2466 }
2467}
2468
2469TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002470 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002471
2472 // Find the boundary between the parent and child
2473 {
chaviw0e3479f2018-09-10 16:49:30 -07002474 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002475 mCapture->expectChildColor(9, 9);
2476 mCapture->expectFGColor(10, 10);
2477 }
2478
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002479 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002480
2481 // The boundary should be twice as far from the origin now.
2482 // The pixels from the last test should all be child now
2483 {
chaviw0e3479f2018-09-10 16:49:30 -07002484 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002485 mCapture->expectChildColor(9, 9);
2486 mCapture->expectChildColor(10, 10);
2487 mCapture->expectChildColor(19, 19);
2488 mCapture->expectFGColor(20, 20);
2489 }
2490}
Robert Carr9524cb32017-02-13 11:32:32 -08002491
Robert Carr6452f122017-03-21 10:41:29 -07002492TEST_F(ChildLayerTest, ChildLayerAlpha) {
2493 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2494 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2495 fillSurfaceRGBA8(mChild, 0, 254, 0);
2496 waitForPostedBuffers();
2497
Robert Carr4cdc58f2017-08-23 14:22:20 -07002498 asTransaction([&](Transaction& t) {
2499 t.show(mChild);
2500 t.setPosition(mChild, 0, 0);
2501 t.setPosition(mFGSurfaceControl, 0, 0);
2502 });
Robert Carr6452f122017-03-21 10:41:29 -07002503
2504 {
chaviw0e3479f2018-09-10 16:49:30 -07002505 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002506 // Unblended child color
2507 mCapture->checkPixel(0, 0, 0, 254, 0);
2508 }
2509
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002510 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002511
2512 {
chaviw0e3479f2018-09-10 16:49:30 -07002513 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002514 // Child and BG blended.
2515 mCapture->checkPixel(0, 0, 127, 127, 0);
2516 }
2517
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002518 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002519
2520 {
chaviw0e3479f2018-09-10 16:49:30 -07002521 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002522 // Child and BG blended.
2523 mCapture->checkPixel(0, 0, 95, 64, 95);
2524 }
2525}
2526
Robert Carr9524cb32017-02-13 11:32:32 -08002527TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002528 asTransaction([&](Transaction& t) {
2529 t.show(mChild);
2530 t.setPosition(mChild, 10, 10);
2531 t.setPosition(mFGSurfaceControl, 64, 64);
2532 });
Robert Carr9524cb32017-02-13 11:32:32 -08002533
2534 {
chaviw0e3479f2018-09-10 16:49:30 -07002535 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002536 // Top left of foreground must now be visible
2537 mCapture->expectFGColor(64, 64);
2538 // But 10 pixels in we should see the child surface
2539 mCapture->expectChildColor(74, 74);
2540 // And 10 more pixels we should be back to the foreground surface
2541 mCapture->expectFGColor(84, 84);
2542 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002543
2544 asTransaction([&](Transaction& t) {
2545 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2546 });
2547
Robert Carr9524cb32017-02-13 11:32:32 -08002548 {
chaviw0e3479f2018-09-10 16:49:30 -07002549 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002550 mCapture->expectFGColor(64, 64);
2551 // In reparenting we should have exposed the entire foreground surface.
2552 mCapture->expectFGColor(74, 74);
2553 // And the child layer should now begin at 10, 10 (since the BG
2554 // layer is at (0, 0)).
2555 mCapture->expectBGColor(9, 9);
2556 mCapture->expectChildColor(10, 10);
2557 }
2558}
2559
chaviw161410b02017-07-27 10:46:08 -07002560TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002561 asTransaction([&](Transaction& t) {
2562 t.show(mChild);
2563 t.setPosition(mChild, 10, 10);
2564 t.setPosition(mFGSurfaceControl, 64, 64);
2565 });
Robert Carr9524cb32017-02-13 11:32:32 -08002566
2567 {
chaviw0e3479f2018-09-10 16:49:30 -07002568 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002569 // Top left of foreground must now be visible
2570 mCapture->expectFGColor(64, 64);
2571 // But 10 pixels in we should see the child surface
2572 mCapture->expectChildColor(74, 74);
2573 // And 10 more pixels we should be back to the foreground surface
2574 mCapture->expectFGColor(84, 84);
2575 }
2576
chaviw0e3479f2018-09-10 16:49:30 -07002577
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002578 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002579
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002580 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002581
chaviw161410b02017-07-27 10:46:08 -07002582 // Since the child has the same client as the parent, it will not get
2583 // detached and will be hidden.
2584 {
chaviw0e3479f2018-09-10 16:49:30 -07002585 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07002586 mCapture->expectFGColor(64, 64);
2587 mCapture->expectFGColor(74, 74);
2588 mCapture->expectFGColor(84, 84);
2589 }
2590}
2591
2592TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2593 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002594 sp<SurfaceControl> mChildNewClient =
2595 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2596 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002597
Peiyong Lin566a3b42018-01-09 18:22:43 -08002598 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002599 ASSERT_TRUE(mChildNewClient->isValid());
2600
2601 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2602
Robert Carr4cdc58f2017-08-23 14:22:20 -07002603 asTransaction([&](Transaction& t) {
2604 t.hide(mChild);
2605 t.show(mChildNewClient);
2606 t.setPosition(mChildNewClient, 10, 10);
2607 t.setPosition(mFGSurfaceControl, 64, 64);
2608 });
chaviw161410b02017-07-27 10:46:08 -07002609
2610 {
chaviw0e3479f2018-09-10 16:49:30 -07002611 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07002612 // Top left of foreground must now be visible
2613 mCapture->expectFGColor(64, 64);
2614 // But 10 pixels in we should see the child surface
2615 mCapture->expectChildColor(74, 74);
2616 // And 10 more pixels we should be back to the foreground surface
2617 mCapture->expectFGColor(84, 84);
2618 }
2619
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002620 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002621
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002622 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002623
Robert Carr9524cb32017-02-13 11:32:32 -08002624 // Nothing should have changed.
2625 {
chaviw0e3479f2018-09-10 16:49:30 -07002626 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002627 mCapture->expectFGColor(64, 64);
2628 mCapture->expectChildColor(74, 74);
2629 mCapture->expectFGColor(84, 84);
2630 }
2631}
2632
Robert Carr9b429f42017-04-17 14:56:57 -07002633TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002634 asTransaction([&](Transaction& t) {
2635 t.show(mChild);
2636 t.setPosition(mChild, 0, 0);
2637 t.setPosition(mFGSurfaceControl, 0, 0);
2638 });
Robert Carr9b429f42017-04-17 14:56:57 -07002639
2640 {
chaviw0e3479f2018-09-10 16:49:30 -07002641 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07002642 // We've positioned the child in the top left.
2643 mCapture->expectChildColor(0, 0);
2644 // But it's only 10x10.
2645 mCapture->expectFGColor(10, 10);
2646 }
2647
Robert Carr4cdc58f2017-08-23 14:22:20 -07002648 asTransaction([&](Transaction& t) {
2649 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2650 // We cause scaling by 2.
2651 t.setSize(mFGSurfaceControl, 128, 128);
2652 });
Robert Carr9b429f42017-04-17 14:56:57 -07002653
2654 {
chaviw0e3479f2018-09-10 16:49:30 -07002655 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07002656 // We've positioned the child in the top left.
2657 mCapture->expectChildColor(0, 0);
2658 mCapture->expectChildColor(10, 10);
2659 mCapture->expectChildColor(19, 19);
2660 // And now it should be scaled all the way to 20x20
2661 mCapture->expectFGColor(20, 20);
2662 }
2663}
2664
Robert Carr1725eee2017-04-26 18:32:15 -07002665// Regression test for b/37673612
2666TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002667 asTransaction([&](Transaction& t) {
2668 t.show(mChild);
2669 t.setPosition(mChild, 0, 0);
2670 t.setPosition(mFGSurfaceControl, 0, 0);
2671 });
Robert Carr1725eee2017-04-26 18:32:15 -07002672
2673 {
chaviw0e3479f2018-09-10 16:49:30 -07002674 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07002675 // We've positioned the child in the top left.
2676 mCapture->expectChildColor(0, 0);
2677 // But it's only 10x10.
2678 mCapture->expectFGColor(10, 10);
2679 }
Robert Carr1725eee2017-04-26 18:32:15 -07002680 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2681 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002682 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002683 sp<Surface> s = mFGSurfaceControl->getSurface();
2684 auto anw = static_cast<ANativeWindow*>(s.get());
2685 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2686 native_window_set_buffers_dimensions(anw, 64, 128);
2687 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2688 waitForPostedBuffers();
2689
2690 {
2691 // The child should still be in the same place and not have any strange scaling as in
2692 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07002693 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07002694 mCapture->expectChildColor(0, 0);
2695 mCapture->expectFGColor(10, 10);
2696 }
2697}
2698
Dan Stoza412903f2017-04-27 13:42:17 -07002699TEST_F(ChildLayerTest, Bug36858924) {
2700 // Destroy the child layer
2701 mChild.clear();
2702
2703 // Now recreate it as hidden
chaviw0e3479f2018-09-10 16:49:30 -07002704 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Dan Stoza412903f2017-04-27 13:42:17 -07002705 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2706 mFGSurfaceControl.get());
2707
2708 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002709 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002710 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
2711 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002712 t.show(mChild);
2713 });
Dan Stoza412903f2017-04-27 13:42:17 -07002714
2715 // Render the foreground surface a few times
2716 //
2717 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2718 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2719 // never acquire/release the first buffer
2720 ALOGI("Filling 1");
2721 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2722 ALOGI("Filling 2");
2723 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2724 ALOGI("Filling 3");
2725 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2726 ALOGI("Filling 4");
2727 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2728}
2729
chaviwf1961f72017-09-18 16:41:07 -07002730TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002731 asTransaction([&](Transaction& t) {
2732 t.show(mChild);
2733 t.setPosition(mChild, 10, 10);
2734 t.setPosition(mFGSurfaceControl, 64, 64);
2735 });
chaviw06178942017-07-27 10:25:59 -07002736
2737 {
chaviw0e3479f2018-09-10 16:49:30 -07002738 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07002739 // Top left of foreground must now be visible
2740 mCapture->expectFGColor(64, 64);
2741 // But 10 pixels in we should see the child surface
2742 mCapture->expectChildColor(74, 74);
2743 // And 10 more pixels we should be back to the foreground surface
2744 mCapture->expectFGColor(84, 84);
2745 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002746
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002747 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002748
chaviw06178942017-07-27 10:25:59 -07002749 {
chaviw0e3479f2018-09-10 16:49:30 -07002750 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07002751 mCapture->expectFGColor(64, 64);
2752 // In reparenting we should have exposed the entire foreground surface.
2753 mCapture->expectFGColor(74, 74);
2754 // And the child layer should now begin at 10, 10 (since the BG
2755 // layer is at (0, 0)).
2756 mCapture->expectBGColor(9, 9);
2757 mCapture->expectChildColor(10, 10);
2758 }
2759}
2760
chaviwf1961f72017-09-18 16:41:07 -07002761TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002762 asTransaction([&](Transaction& t) {
2763 t.show(mChild);
2764 t.setPosition(mChild, 10, 10);
2765 t.setPosition(mFGSurfaceControl, 64, 64);
2766 });
chaviwf1961f72017-09-18 16:41:07 -07002767
2768 {
chaviw0e3479f2018-09-10 16:49:30 -07002769 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002770 // Top left of foreground must now be visible
2771 mCapture->expectFGColor(64, 64);
2772 // But 10 pixels in we should see the child surface
2773 mCapture->expectChildColor(74, 74);
2774 // And 10 more pixels we should be back to the foreground surface
2775 mCapture->expectFGColor(84, 84);
2776 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002777 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002778 {
chaviw0e3479f2018-09-10 16:49:30 -07002779 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002780 // Nothing should have changed.
2781 mCapture->expectFGColor(64, 64);
2782 mCapture->expectChildColor(74, 74);
2783 mCapture->expectFGColor(84, 84);
2784 }
2785}
2786
2787TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07002788 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002789 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002790 ASSERT_TRUE(newSurface->isValid());
2791
2792 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002793 asTransaction([&](Transaction& t) {
2794 t.hide(mChild);
2795 t.show(newSurface);
2796 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002797 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002798 t.setPosition(mFGSurfaceControl, 64, 64);
2799 });
chaviwf1961f72017-09-18 16:41:07 -07002800
2801 {
chaviw0e3479f2018-09-10 16:49:30 -07002802 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002803 // Top left of foreground must now be visible
2804 mCapture->expectFGColor(64, 64);
2805 // At 10, 10 we should see the new surface
2806 mCapture->checkPixel(10, 10, 63, 195, 63);
2807 }
2808
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002809 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002810
2811 {
chaviw0e3479f2018-09-10 16:49:30 -07002812 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002813 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2814 // mFGSurface, putting it at 74, 74.
2815 mCapture->expectFGColor(64, 64);
2816 mCapture->checkPixel(74, 74, 63, 195, 63);
2817 mCapture->expectFGColor(84, 84);
2818 }
2819}
2820
chaviwc9674332017-08-28 12:32:18 -07002821TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002822 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07002823 mClient->createSurface(String8("Grandchild surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002824 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002825 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2826
2827 {
chaviw0e3479f2018-09-10 16:49:30 -07002828 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07002829 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2830 // which begins at 64, 64
2831 mCapture->checkPixel(64, 64, 50, 50, 50);
2832 }
2833}
2834
Robert Carr503c7042017-09-27 15:06:08 -07002835TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07002836 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002837 fillSurfaceRGBA8(relative, 255, 255, 255);
2838
2839 Transaction t;
2840 t.setLayer(relative, INT32_MAX)
2841 .setRelativeLayer(mChild, relative->getHandle(), 1)
2842 .setPosition(mFGSurfaceControl, 0, 0)
2843 .apply(true);
2844
2845 // We expect that the child should have been elevated above our
2846 // INT_MAX layer even though it's not a child of it.
2847 {
chaviw0e3479f2018-09-10 16:49:30 -07002848 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07002849 mCapture->expectChildColor(0, 0);
2850 mCapture->expectChildColor(9, 9);
2851 mCapture->checkPixel(10, 10, 255, 255, 255);
2852 }
2853}
Vishnu Nair60356342018-11-13 13:00:45 -08002854class BoundlessLayerTest : public LayerUpdateTest {
2855protected:
2856 std::unique_ptr<ScreenCapture> mCapture;
2857};
2858
2859// Verify setting a size on a buffer layer has no effect.
2860TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
2861 sp<SurfaceControl> bufferLayer =
2862 mClient->createSurface(String8("BufferLayer"), 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
2863 mFGSurfaceControl.get());
2864 ASSERT_TRUE(bufferLayer != nullptr);
2865 ASSERT_TRUE(bufferLayer->isValid());
2866 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
2867 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
2868 {
2869 mCapture = screenshot();
2870 // Top left of background must now be visible
2871 mCapture->expectBGColor(0, 0);
2872 // Foreground Surface bounds must be color layer
2873 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
2874 // Buffer layer should not extend past buffer bounds
2875 mCapture->expectFGColor(95, 95);
2876 }
2877}
2878
2879// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
2880// which will crop the color layer.
2881TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
2882 sp<SurfaceControl> colorLayer =
2883 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
2884 ISurfaceComposerClient::eFXSurfaceColor,
2885 mFGSurfaceControl.get());
2886 ASSERT_TRUE(colorLayer != nullptr);
2887 ASSERT_TRUE(colorLayer->isValid());
2888 asTransaction([&](Transaction& t) {
2889 t.setColor(colorLayer, half3{0, 0, 0});
2890 t.show(colorLayer);
2891 });
2892 {
2893 mCapture = screenshot();
2894 // Top left of background must now be visible
2895 mCapture->expectBGColor(0, 0);
2896 // Foreground Surface bounds must be color layer
2897 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
2898 // Color layer should not extend past foreground bounds
2899 mCapture->expectBGColor(129, 129);
2900 }
2901}
2902
2903// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
2904// a crop which will be used to crop the color layer.
2905TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
2906 sp<SurfaceControl> cropLayer =
2907 mClient->createSurface(String8("CropLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
2908 0 /* flags */, mFGSurfaceControl.get());
2909 ASSERT_TRUE(cropLayer != nullptr);
2910 ASSERT_TRUE(cropLayer->isValid());
2911 sp<SurfaceControl> colorLayer =
2912 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
2913 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
2914 ASSERT_TRUE(colorLayer != nullptr);
2915 ASSERT_TRUE(colorLayer->isValid());
2916 asTransaction([&](Transaction& t) {
2917 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
2918 t.setColor(colorLayer, half3{0, 0, 0});
2919 t.show(cropLayer);
2920 t.show(colorLayer);
2921 });
2922 {
2923 mCapture = screenshot();
2924 // Top left of background must now be visible
2925 mCapture->expectBGColor(0, 0);
2926 // Top left of foreground must now be visible
2927 mCapture->expectFGColor(64, 64);
2928 // 5 pixels from the foreground we should see the child surface
2929 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
2930 // 10 pixels from the foreground we should be back to the foreground surface
2931 mCapture->expectFGColor(74, 74);
2932 }
2933}
2934
2935// Verify for boundless layer with no children, their transforms have no effect.
2936TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
2937 sp<SurfaceControl> colorLayer =
2938 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
2939 ISurfaceComposerClient::eFXSurfaceColor,
2940 mFGSurfaceControl.get());
2941 ASSERT_TRUE(colorLayer != nullptr);
2942 ASSERT_TRUE(colorLayer->isValid());
2943 asTransaction([&](Transaction& t) {
2944 t.setPosition(colorLayer, 320, 320);
2945 t.setMatrix(colorLayer, 2, 0, 0, 2);
2946 t.setColor(colorLayer, half3{0, 0, 0});
2947 t.show(colorLayer);
2948 });
2949 {
2950 mCapture = screenshot();
2951 // Top left of background must now be visible
2952 mCapture->expectBGColor(0, 0);
2953 // Foreground Surface bounds must be color layer
2954 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
2955 // Color layer should not extend past foreground bounds
2956 mCapture->expectBGColor(129, 129);
2957 }
2958}
2959
2960// Verify for boundless layer with children, their transforms have an effect.
2961TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
2962 sp<SurfaceControl> boundlessLayerRightShift =
2963 mClient->createSurface(String8("BoundlessLayerRightShift"), 0, 0,
2964 PIXEL_FORMAT_RGBA_8888, 0 /* flags */, mFGSurfaceControl.get());
2965 ASSERT_TRUE(boundlessLayerRightShift != nullptr);
2966 ASSERT_TRUE(boundlessLayerRightShift->isValid());
2967 sp<SurfaceControl> boundlessLayerDownShift =
2968 mClient->createSurface(String8("BoundlessLayerLeftShift"), 0, 0, PIXEL_FORMAT_RGBA_8888,
2969 0 /* flags */, boundlessLayerRightShift.get());
2970 ASSERT_TRUE(boundlessLayerDownShift != nullptr);
2971 ASSERT_TRUE(boundlessLayerDownShift->isValid());
2972 sp<SurfaceControl> colorLayer =
2973 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
2974 ISurfaceComposerClient::eFXSurfaceColor,
2975 boundlessLayerDownShift.get());
2976 ASSERT_TRUE(colorLayer != nullptr);
2977 ASSERT_TRUE(colorLayer->isValid());
2978 asTransaction([&](Transaction& t) {
2979 t.setPosition(boundlessLayerRightShift, 32, 0);
2980 t.show(boundlessLayerRightShift);
2981 t.setPosition(boundlessLayerDownShift, 0, 32);
2982 t.show(boundlessLayerDownShift);
2983 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
2984 t.setColor(colorLayer, half3{0, 0, 0});
2985 t.show(colorLayer);
2986 });
2987 {
2988 mCapture = screenshot();
2989 // Top left of background must now be visible
2990 mCapture->expectBGColor(0, 0);
2991 // Top left of foreground must now be visible
2992 mCapture->expectFGColor(64, 64);
2993 // Foreground Surface bounds must be color layer
2994 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
2995 // Color layer should not extend past foreground bounds
2996 mCapture->expectBGColor(129, 129);
2997 }
2998}
2999
3000// Verify child layers do not get clipped if they temporarily move into the negative
3001// coordinate space as the result of an intermediate transformation.
3002TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
3003 sp<SurfaceControl> boundlessLayer =
3004 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3005 0 /* flags */, mFGSurfaceControl.get());
3006 ASSERT_TRUE(boundlessLayer != nullptr);
3007 ASSERT_TRUE(boundlessLayer->isValid());
3008 sp<SurfaceControl> colorLayer =
3009 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3010 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
3011 ASSERT_TRUE(colorLayer != nullptr);
3012 ASSERT_TRUE(colorLayer->isValid());
3013 asTransaction([&](Transaction& t) {
3014 // shift child layer off bounds. If this layer was not boundless, we will
3015 // expect the child layer to be cropped.
3016 t.setPosition(boundlessLayer, 32, 32);
3017 t.show(boundlessLayer);
3018 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3019 // undo shift by parent
3020 t.setPosition(colorLayer, -32, -32);
3021 t.setColor(colorLayer, half3{0, 0, 0});
3022 t.show(colorLayer);
3023 });
3024 {
3025 mCapture = screenshot();
3026 // Top left of background must now be visible
3027 mCapture->expectBGColor(0, 0);
3028 // Foreground Surface bounds must be color layer
3029 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3030 // Color layer should not extend past foreground bounds
3031 mCapture->expectBGColor(129, 129);
3032 }
3033}
3034
3035// Verify for boundless root layers with children, their transforms have an effect.
3036TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
3037 sp<SurfaceControl> rootBoundlessLayer =
3038 mClient->createSurface(String8("RootBoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3039 0 /* flags */);
3040 ASSERT_TRUE(rootBoundlessLayer != nullptr);
3041 ASSERT_TRUE(rootBoundlessLayer->isValid());
3042 sp<SurfaceControl> colorLayer =
3043 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3044 ISurfaceComposerClient::eFXSurfaceColor,
3045 rootBoundlessLayer.get());
3046 ASSERT_TRUE(colorLayer != nullptr);
3047 ASSERT_TRUE(colorLayer->isValid());
3048 asTransaction([&](Transaction& t) {
3049 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
3050 t.setPosition(rootBoundlessLayer, 32, 32);
3051 t.show(rootBoundlessLayer);
3052 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3053 t.setColor(colorLayer, half3{0, 0, 0});
3054 t.show(colorLayer);
3055 t.hide(mFGSurfaceControl);
3056 });
3057 {
3058 mCapture = screenshot();
3059 // Top left of background must now be visible
3060 mCapture->expectBGColor(0, 0);
3061 // Top left of foreground must now be visible
3062 mCapture->expectBGColor(31, 31);
3063 // Foreground Surface bounds must be color layer
3064 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
3065 // Color layer should not extend past foreground bounds
3066 mCapture->expectBGColor(97, 97);
3067 }
3068}
Robert Carr503c7042017-09-27 15:06:08 -07003069
chaviwa76b2712017-09-20 12:02:26 -07003070class ScreenCaptureTest : public LayerUpdateTest {
3071protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003072 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07003073};
3074
3075TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
3076 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003077 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003078 mCapture->expectBGColor(0, 0);
3079 // Doesn't capture FG layer which is at 64, 64
3080 mCapture->expectBGColor(64, 64);
3081}
3082
3083TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
3084 auto fgHandle = mFGSurfaceControl->getHandle();
3085
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003086 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003087 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003088 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003089 fillSurfaceRGBA8(child, 200, 200, 200);
3090
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003091 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003092
3093 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003094 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003095 mCapture->expectFGColor(10, 10);
3096 mCapture->expectChildColor(0, 0);
3097}
3098
Robert Carr578038f2018-03-09 12:25:24 -08003099TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
3100 auto fgHandle = mFGSurfaceControl->getHandle();
3101
3102 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003103 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08003104 0, mFGSurfaceControl.get());
3105 fillSurfaceRGBA8(child, 200, 200, 200);
3106
3107 SurfaceComposerClient::Transaction().show(child).apply(true);
3108
3109 // Captures mFGSurfaceControl's child
3110 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3111 mCapture->checkPixel(10, 10, 0, 0, 0);
3112 mCapture->expectChildColor(0, 0);
3113}
3114
chaviw50da5042018-04-09 13:49:37 -07003115TEST_F(ScreenCaptureTest, CaptureTransparent) {
3116 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003117 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw50da5042018-04-09 13:49:37 -07003118 0, mFGSurfaceControl.get());
3119
3120 fillSurfaceRGBA8(child, 200, 200, 200);
3121
3122 SurfaceComposerClient::Transaction().show(child).apply(true);
3123
3124 auto childHandle = child->getHandle();
3125
3126 // Captures child
3127 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
3128 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
3129 // Area outside of child's bounds is transparent.
3130 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
3131}
3132
chaviw4b129c22018-04-09 16:19:43 -07003133TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
3134 auto fgHandle = mFGSurfaceControl->getHandle();
3135
3136 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003137 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07003138 0, mFGSurfaceControl.get());
chaviw0e3479f2018-09-10 16:49:30 -07003139 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07003140 fillSurfaceRGBA8(child, 200, 200, 200);
3141 fillSurfaceRGBA8(relative, 100, 100, 100);
3142
3143 SurfaceComposerClient::Transaction()
3144 .show(child)
3145 // Set relative layer above fg layer so should be shown above when computing all layers.
3146 .setRelativeLayer(relative, fgHandle, 1)
3147 .show(relative)
3148 .apply(true);
3149
3150 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
3151 ScreenCapture::captureLayers(&mCapture, fgHandle);
3152 mCapture->expectFGColor(10, 10);
3153 mCapture->expectChildColor(0, 0);
3154}
3155
3156TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
3157 auto fgHandle = mFGSurfaceControl->getHandle();
3158
3159 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003160 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07003161 0, mFGSurfaceControl.get());
3162 sp<SurfaceControl> relative =
chaviw0e3479f2018-09-10 16:49:30 -07003163 mClient->createSurface(String8("Relative surface"), 10, 10,
chaviw4b129c22018-04-09 16:19:43 -07003164 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3165 fillSurfaceRGBA8(child, 200, 200, 200);
3166 fillSurfaceRGBA8(relative, 100, 100, 100);
3167
3168 SurfaceComposerClient::Transaction()
3169 .show(child)
3170 // Set relative layer below fg layer but relative to child layer so it should be shown
3171 // above child layer.
3172 .setLayer(relative, -1)
3173 .setRelativeLayer(relative, child->getHandle(), 1)
3174 .show(relative)
3175 .apply(true);
3176
3177 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
3178 // relative value should be taken into account, placing it above child layer.
3179 ScreenCapture::captureLayers(&mCapture, fgHandle);
3180 mCapture->expectFGColor(10, 10);
3181 // Relative layer is showing on top of child layer
3182 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
3183}
Robert Carr578038f2018-03-09 12:25:24 -08003184
3185// In the following tests we verify successful skipping of a parent layer,
3186// so we use the same verification logic and only change how we mutate
3187// the parent layer to verify that various properties are ignored.
3188class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
3189public:
3190 void SetUp() override {
3191 LayerUpdateTest::SetUp();
3192
3193 mChild =
chaviw0e3479f2018-09-10 16:49:30 -07003194 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08003195 0, mFGSurfaceControl.get());
3196 fillSurfaceRGBA8(mChild, 200, 200, 200);
3197
3198 SurfaceComposerClient::Transaction().show(mChild).apply(true);
3199 }
3200
3201 void verify() {
3202 auto fgHandle = mFGSurfaceControl->getHandle();
3203 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3204 mCapture->checkPixel(10, 10, 0, 0, 0);
3205 mCapture->expectChildColor(0, 0);
3206 }
3207
3208 std::unique_ptr<ScreenCapture> mCapture;
3209 sp<SurfaceControl> mChild;
3210};
3211
3212TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
3213
3214 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3215
3216 // Even though the parent is hidden we should still capture the child.
3217 verify();
3218}
3219
3220TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003221 SurfaceComposerClient::Transaction()
3222 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
3223 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08003224
3225 // Even though the parent is cropped out we should still capture the child.
3226 verify();
3227}
3228
3229TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
3230
3231 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
3232
3233 // We should not inherit the parent scaling.
3234 verify();
3235}
3236
Robert Carr15eae092018-03-23 13:43:53 -07003237TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
3238 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3239
3240 // Even though the parent is hidden we should still capture the child.
3241 verify();
3242
3243 // Verify everything was properly hidden when rendering the full-screen.
3244 screenshot()->expectBGColor(0,0);
3245}
3246
3247
chaviwa76b2712017-09-20 12:02:26 -07003248TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
3249 auto fgHandle = mFGSurfaceControl->getHandle();
3250
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003251 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003252 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003253 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003254 fillSurfaceRGBA8(child, 200, 200, 200);
3255
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003256 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003257 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003258 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003259
3260 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3261 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003262 .show(child)
3263 .setPosition(grandchild, 5, 5)
3264 .show(grandchild)
3265 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003266
3267 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003268 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003269 mCapture->expectFGColor(10, 10);
3270 mCapture->expectChildColor(0, 0);
3271 mCapture->checkPixel(5, 5, 50, 50, 50);
3272}
3273
3274TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003275 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003276 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003277 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003278 fillSurfaceRGBA8(child, 200, 200, 200);
3279 auto childHandle = child->getHandle();
3280
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003281 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003282
3283 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003284 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07003285 mCapture->expectChildColor(0, 0);
3286 mCapture->expectChildColor(9, 9);
3287}
3288
3289TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003290 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003291 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003292 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003293 fillSurfaceRGBA8(child, 200, 200, 200);
3294 auto childHandle = child->getHandle();
3295
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003296 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003297 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003298 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003299 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3300
3301 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003302 .show(child)
3303 .setPosition(grandchild, 5, 5)
3304 .show(grandchild)
3305 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003306
3307 auto grandchildHandle = grandchild->getHandle();
3308
3309 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003310 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07003311 mCapture->checkPixel(0, 0, 50, 50, 50);
3312 mCapture->checkPixel(4, 4, 50, 50, 50);
3313}
3314
chaviw7206d492017-11-10 16:16:12 -08003315TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07003316 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003317 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07003318 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003319 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003320
Marissa Wall61c58622018-07-18 10:12:20 -07003321 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3322 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003323
3324 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003325 .setLayer(redLayer, INT32_MAX - 1)
3326 .show(redLayer)
3327 .show(blueLayer)
3328 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003329
3330 auto redLayerHandle = redLayer->getHandle();
3331
3332 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003333 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3334 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3335 // red area below the blue area
3336 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3337 // red area to the right of the blue area
3338 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003339
3340 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003341 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08003342 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
3343 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003344 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08003345 mCapture->checkPixel(30, 30, 0, 0, 0);
3346}
3347
3348TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07003349 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003350 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07003351 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003352 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003353
Marissa Wall61c58622018-07-18 10:12:20 -07003354 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3355 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003356
3357 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003358 .setLayer(redLayer, INT32_MAX - 1)
3359 .show(redLayer)
3360 .show(blueLayer)
3361 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003362
3363 auto redLayerHandle = redLayer->getHandle();
3364
3365 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003366 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3367 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3368 // red area below the blue area
3369 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3370 // red area to the right of the blue area
3371 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003372
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003373 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08003374 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003375 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
3376 // red area below the blue area
3377 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
3378 // red area to the right of the blue area
3379 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003380 mCapture->checkPixel(30, 30, 0, 0, 0);
3381}
3382
3383TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003384 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08003385
Marissa Wall61c58622018-07-18 10:12:20 -07003386 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08003387
3388 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07003389 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08003390 SurfaceComposerClient::Transaction().apply(true);
3391
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003392 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08003393
3394 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003395 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
3396 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08003397}
3398
chaviw8e3fe5d2018-02-22 10:55:42 -08003399
3400class DereferenceSurfaceControlTest : public LayerTransactionTest {
3401protected:
3402 void SetUp() override {
3403 LayerTransactionTest::SetUp();
3404 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003405 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003406 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003407 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003408 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
3409 {
3410 SCOPED_TRACE("before anything");
3411 auto shot = screenshot();
3412 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3413 }
3414 }
3415 void TearDown() override {
3416 LayerTransactionTest::TearDown();
3417 bgLayer = 0;
3418 fgLayer = 0;
3419 }
3420
3421 sp<SurfaceControl> bgLayer;
3422 sp<SurfaceControl> fgLayer;
3423};
3424
3425TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
3426 fgLayer = nullptr;
3427 {
3428 SCOPED_TRACE("after setting null");
3429 auto shot = screenshot();
3430 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
3431 }
3432}
3433
3434TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
3435 auto transaction = Transaction().show(fgLayer);
3436 fgLayer = nullptr;
3437 {
3438 SCOPED_TRACE("after setting null");
3439 auto shot = screenshot();
3440 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3441 }
3442}
3443
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003444} // namespace android