blob: ed1529b37bc545331d23655d414c61b8ada4059a [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:
Chia-I Wu718daf82017-10-20 11:57:17 -0700189 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
190 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700191 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700192 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700193 SurfaceComposerClient::Transaction().apply(true);
194
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000195 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700196 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000197 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, minLayerZ, maxLayerZ,
198 false));
199 *sc = new ScreenCapture(outBuffer);
200 }
201
202 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
203 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
204 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
205 SurfaceComposerClient::Transaction().apply(true);
206
207 sp<GraphicBuffer> outBuffer;
208 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
209 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700210 }
211
Robert Carr578038f2018-03-09 12:25:24 -0800212 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
213 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
214 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
215 SurfaceComposerClient::Transaction().apply(true);
216
217 sp<GraphicBuffer> outBuffer;
218 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
219 *sc = std::make_unique<ScreenCapture>(outBuffer);
220 }
221
Chia-I Wu718daf82017-10-20 11:57:17 -0700222 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000223 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
224 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700225 }
226
227 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000228 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700229 const bool leftBorder = rect.left > 0;
230 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000231 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
232 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700233
234 if (topBorder) {
235 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
236 if (leftBorder) {
237 top.left -= 1;
238 }
239 if (rightBorder) {
240 top.right += 1;
241 }
242 expectColor(top, color, tolerance);
243 }
244 if (leftBorder) {
245 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
246 expectColor(left, color, tolerance);
247 }
248 if (rightBorder) {
249 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
250 expectColor(right, color, tolerance);
251 }
252 if (bottomBorder) {
253 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
254 if (leftBorder) {
255 bottom.left -= 1;
256 }
257 if (rightBorder) {
258 bottom.right += 1;
259 }
260 expectColor(bottom, color, tolerance);
261 }
262 }
263
Chia-I Wu93853fe2017-11-02 08:30:27 -0700264 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
265 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
266 uint8_t tolerance = 0) {
267 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
268
269 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
270 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
271 // avoid checking borders due to unspecified filtering behavior
272 const int32_t offsetX = filtered ? 2 : 0;
273 const int32_t offsetY = filtered ? 2 : 0;
274 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
275 tolerance);
276 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
277 tolerance);
278 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
279 tolerance);
280 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
281 bottomRight, tolerance);
282 }
283
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700284 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000285 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
286 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700287 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
288 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700289 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
290 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700291 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700292 }
293 }
294
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700295 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700297 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700298
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700299 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700300
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000301 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
302 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700303 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700304
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000305 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700306
307private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000308 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800309 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700310};
311
Chia-I Wu718daf82017-10-20 11:57:17 -0700312class LayerTransactionTest : public ::testing::Test {
313protected:
314 void SetUp() override {
315 mClient = new SurfaceComposerClient;
316 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
317
318 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
319 }
320
Marissa Wall61c58622018-07-18 10:12:20 -0700321 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
322 uint32_t flags = 0) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700323 auto layer =
324 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
325 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
326
327 status_t error = Transaction()
328 .setLayerStack(layer, mDisplayLayerStack)
329 .setLayer(layer, mLayerZBase)
330 .apply();
331 if (error != NO_ERROR) {
332 ADD_FAILURE() << "failed to initialize SurfaceControl";
333 layer.clear();
334 }
335
336 return layer;
337 }
338
Marissa Wall61c58622018-07-18 10:12:20 -0700339 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700340 // wait for previous transactions (such as setSize) to complete
341 Transaction().apply(true);
342
343 ANativeWindow_Buffer buffer = {};
344 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
345
346 return buffer;
347 }
348
Marissa Wall61c58622018-07-18 10:12:20 -0700349 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700350 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
351
352 // wait for the newly posted buffer to be latched
353 waitForLayerBuffers();
354 }
355
Marissa Wall61c58622018-07-18 10:12:20 -0700356 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
357 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700358 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700359 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
360 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
361 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700362 }
363
Marissa Wall61c58622018-07-18 10:12:20 -0700364 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
365 int32_t bufferWidth, int32_t bufferHeight) {
366 sp<GraphicBuffer> buffer =
367 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
368 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
369 BufferUsage::COMPOSER_OVERLAY,
370 "test");
371 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
372 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
373 }
374
375 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
376 int32_t bufferWidth, int32_t bufferHeight) {
377 switch (mLayerType) {
378 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
379 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
380 break;
381 case ISurfaceComposerClient::eFXSurfaceBufferState:
382 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
383 break;
384 default:
385 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
386 }
387 }
388
389 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
390 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700391 const Color& topRight, const Color& bottomLeft,
392 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700393 switch (mLayerType) {
394 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
395 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
396 bottomLeft, bottomRight);
397 break;
398 case ISurfaceComposerClient::eFXSurfaceBufferState:
399 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
400 bottomLeft, bottomRight);
401 break;
402 default:
403 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
404 }
405 }
406
407 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
408 int32_t bufferHeight, const Color& topLeft,
409 const Color& topRight, const Color& bottomLeft,
410 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700411 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700412 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
413 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700414
Marissa Wall61c58622018-07-18 10:12:20 -0700415 const int32_t halfW = bufferWidth / 2;
416 const int32_t halfH = bufferHeight / 2;
417 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
418 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
419 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
420 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
421 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700422
Marissa Wall61c58622018-07-18 10:12:20 -0700423 postBufferQueueLayerBuffer(layer);
424 }
425
426 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
427 int32_t bufferHeight, const Color& topLeft,
428 const Color& topRight, const Color& bottomLeft,
429 const Color& bottomRight) {
430 sp<GraphicBuffer> buffer =
431 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
432 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
433 BufferUsage::COMPOSER_OVERLAY,
434 "test");
435
436 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
437
438 const int32_t halfW = bufferWidth / 2;
439 const int32_t halfH = bufferHeight / 2;
440 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
441 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
442 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
443 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
444
445 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700446 }
447
Chia-I Wu718daf82017-10-20 11:57:17 -0700448 sp<ScreenCapture> screenshot() {
449 sp<ScreenCapture> screenshot;
450 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
451 return screenshot;
452 }
453
454 sp<SurfaceComposerClient> mClient;
455
456 sp<IBinder> mDisplay;
457 uint32_t mDisplayWidth;
458 uint32_t mDisplayHeight;
459 uint32_t mDisplayLayerStack;
460
461 // leave room for ~256 layers
462 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
463
Marissa Wall61c58622018-07-18 10:12:20 -0700464 void setPositionWithResizeHelper(uint32_t layerType);
465 void setSizeBasicHelper(uint32_t layerType);
466 void setMatrixWithResizeHelper(uint32_t layerType);
467
Chia-I Wu718daf82017-10-20 11:57:17 -0700468private:
469 void SetUpDisplay() {
470 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
471 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
472
473 // get display width/height
474 DisplayInfo info;
475 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
476 mDisplayWidth = info.w;
477 mDisplayHeight = info.h;
478
479 // After a new buffer is queued, SurfaceFlinger is notified and will
480 // latch the new buffer on next vsync. Let's heuristically wait for 3
481 // vsyncs.
482 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
483
484 mDisplayLayerStack = 0;
485 // set layer stack (b/68888219)
486 Transaction t;
487 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
488 t.apply();
489 }
490
491 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
492
493 int32_t mBufferPostDelay;
494};
495
Marissa Wall61c58622018-07-18 10:12:20 -0700496class LayerTypeTransactionTest : public LayerTransactionTest,
497 public ::testing::WithParamInterface<uint32_t> {
498public:
499 LayerTypeTransactionTest() { mLayerType = GetParam(); }
500
501 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
502 uint32_t flags = 0) override {
503 // if the flags already have a layer type specified, return an error
504 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
505 return nullptr;
506 }
507 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
508 }
509
510 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
511 int32_t bufferHeight) {
512 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
513 bufferWidth, bufferHeight));
514 }
515
516 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
517 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
518 const Color& bottomLeft, const Color& bottomRight) {
519 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
520 bufferWidth, bufferHeight,
521 topLeft, topRight,
522 bottomLeft, bottomRight));
523 }
524
525protected:
526 uint32_t mLayerType;
527};
528
529INSTANTIATE_TEST_CASE_P(
530 LayerTypeTransactionTests, LayerTypeTransactionTest,
531 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
532 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
533
534TEST_P(LayerTypeTransactionTest, SetPositionBasic) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700535 sp<SurfaceControl> layer;
536 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700537 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700538
539 {
540 SCOPED_TRACE("default position");
541 auto shot = screenshot();
542 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
543 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
544 }
545
546 Transaction().setPosition(layer, 5, 10).apply();
547 {
548 SCOPED_TRACE("new position");
549 auto shot = screenshot();
550 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
551 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
552 }
553}
554
Marissa Wall61c58622018-07-18 10:12:20 -0700555TEST_P(LayerTypeTransactionTest, SetPositionRounding) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700556 sp<SurfaceControl> layer;
557 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700558 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700559
560 // GLES requires only 4 bits of subpixel precision during rasterization
561 // XXX GLES composition does not match HWC composition due to precision
562 // loss (b/69315223)
563 const float epsilon = 1.0f / 16.0f;
564 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
565 {
566 SCOPED_TRACE("rounding down");
567 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
568 }
569
570 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
571 {
572 SCOPED_TRACE("rounding up");
573 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
574 }
575}
576
Marissa Wall61c58622018-07-18 10:12:20 -0700577TEST_P(LayerTypeTransactionTest, SetPositionOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700578 sp<SurfaceControl> layer;
579 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700580 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700581
582 Transaction().setPosition(layer, -32, -32).apply();
583 {
584 SCOPED_TRACE("negative coordinates");
585 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
586 }
587
588 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
589 {
590 SCOPED_TRACE("positive coordinates");
591 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
592 }
593}
594
Marissa Wall61c58622018-07-18 10:12:20 -0700595TEST_P(LayerTypeTransactionTest, SetPositionPartiallyOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700596 sp<SurfaceControl> layer;
597 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700598 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700599
600 // partially out of bounds
601 Transaction().setPosition(layer, -30, -30).apply();
602 {
603 SCOPED_TRACE("negative coordinates");
604 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
605 }
606
607 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
608 {
609 SCOPED_TRACE("positive coordinates");
610 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
611 mDisplayHeight),
612 Color::RED);
613 }
614}
615
Marissa Wall61c58622018-07-18 10:12:20 -0700616void LayerTransactionTest::setPositionWithResizeHelper(uint32_t layerType) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700617 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700618 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
619 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700620
621 // setPosition is applied immediately by default, with or without resize
622 // pending
623 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
624 {
625 SCOPED_TRACE("resize pending");
626 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700627 Rect rect;
628 switch (layerType) {
629 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
630 rect = {5, 10, 37, 42};
631 break;
632 case ISurfaceComposerClient::eFXSurfaceBufferState:
633 rect = {5, 10, 69, 74};
634 break;
635 default:
636 ASSERT_FALSE(true) << "Unsupported layer type";
637 }
638
639 shot->expectColor(rect, Color::RED);
640 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700641 }
642
Marissa Wall61c58622018-07-18 10:12:20 -0700643 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700644 {
645 SCOPED_TRACE("resize applied");
646 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
647 }
648}
649
Marissa Wall61c58622018-07-18 10:12:20 -0700650TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
651 ASSERT_NO_FATAL_FAILURE(
652 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
653}
654
655TEST_F(LayerTransactionTest, SetPositionWithResize_BufferState) {
656 ASSERT_NO_FATAL_FAILURE(
657 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
658}
659
660TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700661 sp<SurfaceControl> layer;
662 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700663 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700664
665 // request setPosition to be applied with the next resize
666 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
667 {
668 SCOPED_TRACE("new position pending");
669 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
670 }
671
672 Transaction().setPosition(layer, 15, 20).apply();
673 {
674 SCOPED_TRACE("pending new position modified");
675 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
676 }
677
678 Transaction().setSize(layer, 64, 64).apply();
679 {
680 SCOPED_TRACE("resize pending");
681 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
682 }
683
684 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700685 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700686 {
687 SCOPED_TRACE("new position applied");
688 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
689 }
690}
691
Marissa Wall61c58622018-07-18 10:12:20 -0700692TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700693 sp<SurfaceControl> layer;
694 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700695 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700696
697 // setPosition is not immediate even with SCALE_TO_WINDOW override
698 Transaction()
699 .setPosition(layer, 5, 10)
700 .setSize(layer, 64, 64)
701 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
702 .setGeometryAppliesWithResize(layer)
703 .apply();
704 {
705 SCOPED_TRACE("new position pending");
706 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
707 }
708
Marissa Wall61c58622018-07-18 10:12:20 -0700709 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700710 {
711 SCOPED_TRACE("new position applied");
712 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
713 }
714}
715
Marissa Wall61c58622018-07-18 10:12:20 -0700716void LayerTransactionTest::setSizeBasicHelper(uint32_t layerType) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700717 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700718 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
719 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700720
721 Transaction().setSize(layer, 64, 64).apply();
722 {
723 SCOPED_TRACE("resize pending");
724 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700725 Rect rect;
726 switch (layerType) {
727 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
728 rect = {0, 0, 32, 32};
729 break;
730 case ISurfaceComposerClient::eFXSurfaceBufferState:
731 rect = {0, 0, 64, 64};
732 break;
733 default:
734 ASSERT_FALSE(true) << "Unsupported layer type";
735 }
736 shot->expectColor(rect, Color::RED);
737 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700738 }
739
Marissa Wall61c58622018-07-18 10:12:20 -0700740 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700741 {
742 SCOPED_TRACE("resize applied");
743 auto shot = screenshot();
744 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
745 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
746 }
747}
748
Marissa Wall61c58622018-07-18 10:12:20 -0700749TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
750 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue);
751}
752
753TEST_F(LayerTransactionTest, SetSizeBasic_BufferState) {
754 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState);
755}
756
757TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700758 // cannot test robustness against invalid sizes (zero or really huge)
759}
760
Marissa Wall61c58622018-07-18 10:12:20 -0700761TEST_P(LayerTypeTransactionTest, SetSizeWithScaleToWindow) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700762 sp<SurfaceControl> layer;
763 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700764 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700765
766 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
767 Transaction()
768 .setSize(layer, 64, 64)
769 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
770 .apply();
771 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
772}
773
Marissa Wall61c58622018-07-18 10:12:20 -0700774TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700775 sp<SurfaceControl> layerR;
776 sp<SurfaceControl> layerG;
777 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700778 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700779 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700780 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700781
782 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
783 {
784 SCOPED_TRACE("layerR");
785 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
786 }
787
788 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
789 {
790 SCOPED_TRACE("layerG");
791 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
792 }
793}
794
Marissa Wall61c58622018-07-18 10:12:20 -0700795TEST_P(LayerTypeTransactionTest, SetZNegative) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700796 sp<SurfaceControl> layerR;
797 sp<SurfaceControl> layerG;
798 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700799 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700800 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700801 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700802
803 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
804 {
805 SCOPED_TRACE("layerR");
806 sp<ScreenCapture> screenshot;
807 ScreenCapture::captureScreen(&screenshot, -2, -1);
808 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
809 }
810
811 Transaction().setLayer(layerR, -3).apply();
812 {
813 SCOPED_TRACE("layerG");
814 sp<ScreenCapture> screenshot;
815 ScreenCapture::captureScreen(&screenshot, -3, -1);
816 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
817 }
818}
819
Marissa Wall61c58622018-07-18 10:12:20 -0700820TEST_P(LayerTypeTransactionTest, SetRelativeZBasic) {
Chia-I Wu49313302017-10-31 10:14:40 -0700821 sp<SurfaceControl> layerR;
822 sp<SurfaceControl> layerG;
823 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700824 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700825 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700826 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700827
828 Transaction()
829 .setPosition(layerG, 16, 16)
830 .setRelativeLayer(layerG, layerR->getHandle(), 1)
831 .apply();
832 {
833 SCOPED_TRACE("layerG above");
834 auto shot = screenshot();
835 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
836 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
837 }
838
839 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
840 {
841 SCOPED_TRACE("layerG below");
842 auto shot = screenshot();
843 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
844 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
845 }
846}
847
Marissa Wall61c58622018-07-18 10:12:20 -0700848TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
Chia-I Wuec2d9852017-11-21 09:21:01 -0800849 sp<SurfaceControl> layerR;
850 sp<SurfaceControl> layerG;
851 sp<SurfaceControl> layerB;
852 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700853 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800854 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700855 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800856 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700857 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800858
859 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
860 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
861
862 sp<ScreenCapture> screenshot;
863 // only layerB is in this range
864 ScreenCapture::captureScreen(&screenshot, -2, -1);
865 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
866}
867
Marissa Wall61c58622018-07-18 10:12:20 -0700868TEST_P(LayerTypeTransactionTest, SetRelativeZGroup) {
Chia-I Wu49313302017-10-31 10:14:40 -0700869 sp<SurfaceControl> layerR;
870 sp<SurfaceControl> layerG;
871 sp<SurfaceControl> layerB;
872 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700873 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700874 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700875 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700876 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700877 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700878
879 // layerR = 0, layerG = layerR + 3, layerB = 2
880 Transaction()
881 .setPosition(layerG, 8, 8)
882 .setRelativeLayer(layerG, layerR->getHandle(), 3)
883 .setPosition(layerB, 16, 16)
884 .setLayer(layerB, mLayerZBase + 2)
885 .apply();
886 {
887 SCOPED_TRACE("(layerR < layerG) < layerB");
888 auto shot = screenshot();
889 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
890 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
891 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
892 }
893
894 // layerR = 4, layerG = layerR + 3, layerB = 2
895 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
896 {
897 SCOPED_TRACE("layerB < (layerR < layerG)");
898 auto shot = screenshot();
899 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
900 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
901 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
902 }
903
904 // layerR = 4, layerG = layerR - 3, layerB = 2
905 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
906 {
907 SCOPED_TRACE("layerB < (layerG < layerR)");
908 auto shot = screenshot();
909 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
910 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
911 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
912 }
913
914 // restore to absolute z
915 // layerR = 4, layerG = 0, layerB = 2
916 Transaction().setLayer(layerG, mLayerZBase).apply();
917 {
918 SCOPED_TRACE("layerG < layerB < layerR");
919 auto shot = screenshot();
920 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
921 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
922 }
923
924 // layerR should not affect layerG anymore
925 // layerR = 1, layerG = 0, layerB = 2
926 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
927 {
928 SCOPED_TRACE("layerG < layerR < layerB");
929 auto shot = screenshot();
930 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
931 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
932 }
933}
934
Marissa Wall61c58622018-07-18 10:12:20 -0700935TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700936 sp<SurfaceControl> layerR;
937 sp<SurfaceControl> layerG;
938
939 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700940 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700941 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700942 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700943
944 Transaction()
945 .setPosition(layerG, 16, 16)
946 .setRelativeLayer(layerG, layerR->getHandle(), 1)
947 .apply();
948
949 mClient->destroySurface(layerG->getHandle());
950 // layerG should have been removed
951 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
952}
953
Marissa Wall61c58622018-07-18 10:12:20 -0700954TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700955 sp<SurfaceControl> layer;
956 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700957 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700958
959 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
960 {
961 SCOPED_TRACE("layer hidden");
962 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
963 }
964
965 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
966 {
967 SCOPED_TRACE("layer shown");
968 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
969 }
970}
971
Marissa Wall61c58622018-07-18 10:12:20 -0700972TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700973 const Color translucentRed = {100, 0, 0, 100};
974 sp<SurfaceControl> layerR;
975 sp<SurfaceControl> layerG;
976 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700977 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700978 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700979 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700980
981 Transaction()
982 .setLayer(layerR, mLayerZBase + 1)
983 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
984 .apply();
985 {
986 SCOPED_TRACE("layerR opaque");
987 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
988 }
989
990 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
991 {
992 SCOPED_TRACE("layerR translucent");
993 const uint8_t g = uint8_t(255 - translucentRed.a);
994 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
995 }
996}
997
Marissa Wall61c58622018-07-18 10:12:20 -0700998TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700999 sp<SurfaceControl> layer;
1000 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001001 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001002
1003 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001004 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001005 Transaction()
1006 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1007 .apply(true);
1008 ASSERT_EQ(PERMISSION_DENIED,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001009 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -07001010 false));
1011
1012 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1013 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001014 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -07001015 false));
1016}
1017
Marissa Wall61c58622018-07-18 10:12:20 -07001018TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001019 const Rect top(0, 0, 32, 16);
1020 const Rect bottom(0, 16, 32, 32);
1021 sp<SurfaceControl> layer;
1022 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1023
1024 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001025 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1026 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1027 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001028 // setTransparentRegionHint always applies to the following buffer
1029 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001030 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001031 {
1032 SCOPED_TRACE("top transparent");
1033 auto shot = screenshot();
1034 shot->expectColor(top, Color::BLACK);
1035 shot->expectColor(bottom, Color::RED);
1036 }
1037
1038 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1039 {
1040 SCOPED_TRACE("transparent region hint pending");
1041 auto shot = screenshot();
1042 shot->expectColor(top, Color::BLACK);
1043 shot->expectColor(bottom, Color::RED);
1044 }
1045
Marissa Wall61c58622018-07-18 10:12:20 -07001046 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1047 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1048 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1049 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001050 {
1051 SCOPED_TRACE("bottom transparent");
1052 auto shot = screenshot();
1053 shot->expectColor(top, Color::RED);
1054 shot->expectColor(bottom, Color::BLACK);
1055 }
1056}
1057
Marissa Wall61c58622018-07-18 10:12:20 -07001058TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1059 const Rect top(0, 0, 32, 16);
1060 const Rect bottom(0, 16, 32, 32);
1061 sp<SurfaceControl> layer;
1062 ASSERT_NO_FATAL_FAILURE(
1063 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1064
1065 sp<GraphicBuffer> buffer =
1066 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1067 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1068 BufferUsage::COMPOSER_OVERLAY,
1069 "test");
1070
1071 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1072 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1073 Transaction()
1074 .setTransparentRegionHint(layer, Region(top))
1075 .setBuffer(layer, buffer)
1076 .setSize(layer, 32, 32)
1077 .apply();
1078 {
1079 SCOPED_TRACE("top transparent");
1080 auto shot = screenshot();
1081 shot->expectColor(top, Color::BLACK);
1082 shot->expectColor(bottom, Color::RED);
1083 }
1084
1085 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1086 {
1087 SCOPED_TRACE("transparent region hint intermediate");
1088 auto shot = screenshot();
1089 shot->expectColor(top, Color::BLACK);
1090 shot->expectColor(bottom, Color::BLACK);
1091 }
1092
1093 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1094 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1095 BufferUsage::COMPOSER_OVERLAY,
1096 "test");
1097
1098 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1099 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1100 Transaction().setBuffer(layer, buffer).setSize(layer, 32, 32).apply();
1101 {
1102 SCOPED_TRACE("bottom transparent");
1103 auto shot = screenshot();
1104 shot->expectColor(top, Color::RED);
1105 shot->expectColor(bottom, Color::BLACK);
1106 }
1107}
1108
1109TEST_P(LayerTypeTransactionTest, SetTransparentRegionHintOutOfBounds) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001110 sp<SurfaceControl> layerTransparent;
1111 sp<SurfaceControl> layerR;
1112 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1113 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1114
1115 // check that transparent region hint is bound by the layer size
1116 Transaction()
1117 .setTransparentRegionHint(layerTransparent,
1118 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1119 .setPosition(layerR, 16, 16)
1120 .setLayer(layerR, mLayerZBase + 1)
1121 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001122 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1123 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001124 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1125}
1126
Marissa Wall61c58622018-07-18 10:12:20 -07001127TEST_P(LayerTypeTransactionTest, SetAlphaBasic) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001128 sp<SurfaceControl> layer1;
1129 sp<SurfaceControl> layer2;
1130 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1131 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001132 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}, 32, 32));
1133 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001134
1135 Transaction()
1136 .setAlpha(layer1, 0.25f)
1137 .setAlpha(layer2, 0.75f)
1138 .setPosition(layer2, 16, 0)
1139 .setLayer(layer2, mLayerZBase + 1)
1140 .apply();
1141 {
1142 auto shot = screenshot();
1143 uint8_t r = 16; // 64 * 0.25f
1144 uint8_t g = 48; // 64 * 0.75f
1145 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1146 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1147
1148 r /= 4; // r * (1.0f - 0.75f)
1149 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1150 }
1151}
1152
Marissa Wall61c58622018-07-18 10:12:20 -07001153TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001154 const Color color = {64, 0, 0, 255};
1155 sp<SurfaceControl> layer;
1156 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001157 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001158
1159 Transaction().setAlpha(layer, 2.0f).apply();
1160 {
1161 SCOPED_TRACE("clamped to 1.0f");
1162 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1163 }
1164
1165 Transaction().setAlpha(layer, -1.0f).apply();
1166 {
1167 SCOPED_TRACE("clamped to 0.0f");
1168 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1169 }
1170}
1171
Chia-I Wue4ef6102017-11-01 15:16:35 -07001172TEST_F(LayerTransactionTest, SetColorBasic) {
1173 sp<SurfaceControl> bufferLayer;
1174 sp<SurfaceControl> colorLayer;
1175 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001176 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001177 ASSERT_NO_FATAL_FAILURE(
1178 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1179
1180 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1181 {
1182 SCOPED_TRACE("default color");
1183 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1184 }
1185
1186 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1187 const Color expected = {15, 51, 85, 255};
1188 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1189 // channel) should be less than one
1190 const uint8_t tolerance = 1;
1191 Transaction().setColor(colorLayer, color).apply();
1192 {
1193 SCOPED_TRACE("new color");
1194 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1195 }
1196}
1197
1198TEST_F(LayerTransactionTest, SetColorClamped) {
1199 sp<SurfaceControl> colorLayer;
1200 ASSERT_NO_FATAL_FAILURE(
1201 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1202
1203 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1204 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1205}
1206
1207TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1208 sp<SurfaceControl> bufferLayer;
1209 sp<SurfaceControl> colorLayer;
1210 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001211 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001212 ASSERT_NO_FATAL_FAILURE(
1213 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1214
1215 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1216 const float alpha = 0.25f;
1217 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1218 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1219 // channel) should be less than one
1220 const uint8_t tolerance = 1;
1221 Transaction()
1222 .setColor(colorLayer, color)
1223 .setAlpha(colorLayer, alpha)
1224 .setLayer(colorLayer, mLayerZBase + 1)
1225 .apply();
1226 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1227 tolerance);
1228}
1229
Adrian Roosb7a96502018-04-08 11:38:55 -07001230TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1231 sp<SurfaceControl> bufferLayer;
1232 sp<SurfaceControl> parentLayer;
1233 sp<SurfaceControl> colorLayer;
1234 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1235 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001236 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Adrian Roosb7a96502018-04-08 11:38:55 -07001237 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1238 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1239
1240 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1241 const float alpha = 0.25f;
1242 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1243 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1244 // channel) should be less than one
1245 const uint8_t tolerance = 1;
1246 Transaction()
1247 .reparent(colorLayer, parentLayer->getHandle())
1248 .setColor(colorLayer, color)
1249 .setAlpha(parentLayer, alpha)
1250 .setLayer(parentLayer, mLayerZBase + 1)
1251 .apply();
1252 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1253 tolerance);
1254}
1255
Marissa Wall61c58622018-07-18 10:12:20 -07001256TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001257 sp<SurfaceControl> bufferLayer;
1258 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001259 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001260
1261 // color is ignored
1262 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1263 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1264}
1265
Marissa Wall61c58622018-07-18 10:12:20 -07001266TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001267 sp<SurfaceControl> layer;
1268 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001269 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001270
1271 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1272 {
1273 SCOPED_TRACE("non-existing layer stack");
1274 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1275 }
1276
1277 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1278 {
1279 SCOPED_TRACE("original layer stack");
1280 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1281 }
1282}
1283
Marissa Wall61c58622018-07-18 10:12:20 -07001284TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001285 sp<SurfaceControl> layer;
1286 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1287 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001288 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001289
1290 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1291 {
1292 SCOPED_TRACE("IDENTITY");
1293 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1294 Color::WHITE);
1295 }
1296
1297 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1298 {
1299 SCOPED_TRACE("FLIP_H");
1300 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1301 Color::BLUE);
1302 }
1303
1304 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1305 {
1306 SCOPED_TRACE("FLIP_V");
1307 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1308 Color::GREEN);
1309 }
1310
1311 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1312 {
1313 SCOPED_TRACE("ROT_90");
1314 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1315 Color::GREEN);
1316 }
1317
1318 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1319 {
1320 SCOPED_TRACE("SCALE");
1321 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1322 Color::WHITE, true /* filtered */);
1323 }
1324}
1325
Marissa Wall61c58622018-07-18 10:12:20 -07001326TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001327 sp<SurfaceControl> layer;
1328 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1329 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001330 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001331
1332 const float rot = M_SQRT1_2; // 45 degrees
1333 const float trans = M_SQRT2 * 16.0f;
1334 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1335
1336 auto shot = screenshot();
1337 // check a 8x8 region inside each color
1338 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1339 const int32_t halfL = 4;
1340 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1341 };
1342 const int32_t unit = int32_t(trans / 2);
1343 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1344 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1345 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1346 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1347}
1348
Marissa Wall61c58622018-07-18 10:12:20 -07001349void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001350 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001351 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1352 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001353
1354 // setMatrix is applied after any pending resize, unlike setPosition
1355 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1356 {
1357 SCOPED_TRACE("resize pending");
1358 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001359 Rect rect;
1360 switch (layerType) {
1361 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1362 rect = {0, 0, 32, 32};
1363 break;
1364 case ISurfaceComposerClient::eFXSurfaceBufferState:
1365 rect = {0, 0, 128, 128};
1366 break;
1367 default:
1368 ASSERT_FALSE(true) << "Unsupported layer type";
1369 }
1370 shot->expectColor(rect, Color::RED);
1371 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001372 }
1373
Marissa Wall61c58622018-07-18 10:12:20 -07001374 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001375 {
1376 SCOPED_TRACE("resize applied");
1377 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1378 }
1379}
1380
Marissa Wall61c58622018-07-18 10:12:20 -07001381TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1382 ASSERT_NO_FATAL_FAILURE(
1383 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1384}
1385
1386TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1387 ASSERT_NO_FATAL_FAILURE(
1388 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1389}
1390
1391TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001392 sp<SurfaceControl> layer;
1393 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001394 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001395
1396 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1397 Transaction()
1398 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1399 .setSize(layer, 64, 64)
1400 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1401 .apply();
1402 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1403}
1404
Marissa Wall61c58622018-07-18 10:12:20 -07001405TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001406 sp<SurfaceControl> layer;
1407 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1408 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001409 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001410
1411 // XXX SCALE_CROP is not respected; calling setSize and
1412 // setOverrideScalingMode in separate transactions does not work
1413 // (b/69315456)
1414 Transaction()
1415 .setSize(layer, 64, 16)
1416 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1417 .apply();
1418 {
1419 SCOPED_TRACE("SCALE_TO_WINDOW");
1420 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1421 Color::WHITE, true /* filtered */);
1422 }
1423}
1424
Dan Stoza000dd012018-08-01 13:31:52 -07001425TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1426 sp<SurfaceControl> layer;
1427 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1428
1429 sp<IBinder> handle = layer->getHandle();
1430 ASSERT_TRUE(handle != nullptr);
1431
1432 FrameStats frameStats;
1433 mClient->getLayerFrameStats(handle, &frameStats);
1434
1435 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1436}
1437
Marissa Wall61c58622018-07-18 10:12:20 -07001438TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001439 sp<SurfaceControl> layer;
1440 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001441 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001442 const Rect crop(8, 8, 24, 24);
1443
Marissa Wallf58c14b2018-07-24 10:50:43 -07001444 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001445 auto shot = screenshot();
1446 shot->expectColor(crop, Color::RED);
1447 shot->expectBorder(crop, Color::BLACK);
1448}
1449
Marissa Wall61c58622018-07-18 10:12:20 -07001450TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1451 sp<SurfaceControl> layer;
1452 ASSERT_NO_FATAL_FAILURE(
1453 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1454 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1455 const Rect crop(8, 8, 24, 24);
1456
1457 Transaction().setCrop(layer, crop).apply();
1458 auto shot = screenshot();
1459 shot->expectColor(crop, Color::RED);
1460 shot->expectBorder(crop, Color::BLACK);
1461}
1462
1463TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001464 sp<SurfaceControl> layer;
1465 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001466 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001467
1468 {
1469 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001470 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001471 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1472 }
1473
1474 {
1475 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001476 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001477 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1478 }
1479}
1480
Marissa Wall61c58622018-07-18 10:12:20 -07001481TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1482 sp<SurfaceControl> layer;
1483 ASSERT_NO_FATAL_FAILURE(
1484 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1485 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1486
1487 {
1488 SCOPED_TRACE("empty rect");
1489 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1490 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1491 }
1492
1493 {
1494 SCOPED_TRACE("negative rect");
1495 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1496 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1497 }
1498}
1499
1500TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001501 sp<SurfaceControl> layer;
1502 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001503 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001504
Marissa Wallf58c14b2018-07-24 10:50:43 -07001505 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001506 auto shot = screenshot();
1507 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1508 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1509}
1510
Marissa Wall61c58622018-07-18 10:12:20 -07001511TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1512 sp<SurfaceControl> layer;
1513 ASSERT_NO_FATAL_FAILURE(
1514 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1515 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1516
1517 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1518 auto shot = screenshot();
1519 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1520 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1521}
1522
1523TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001524 sp<SurfaceControl> layer;
1525 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001526 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001527
1528 const Point position(32, 32);
1529 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001530 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001531 auto shot = screenshot();
1532 shot->expectColor(crop + position, Color::RED);
1533 shot->expectBorder(crop + position, Color::BLACK);
1534}
1535
Marissa Wall61c58622018-07-18 10:12:20 -07001536TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1537 sp<SurfaceControl> layer;
1538 ASSERT_NO_FATAL_FAILURE(
1539 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1540 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1541
1542 const Point position(32, 32);
1543 const Rect crop(8, 8, 24, 24);
1544 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1545 auto shot = screenshot();
1546 shot->expectColor(crop + position, Color::RED);
1547 shot->expectBorder(crop + position, Color::BLACK);
1548}
1549
1550TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001551 sp<SurfaceControl> layer;
1552 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001553 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001554
1555 // crop is affected by matrix
1556 Transaction()
1557 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001558 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001559 .apply();
1560 auto shot = screenshot();
1561 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1562 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1563}
1564
Marissa Wall61c58622018-07-18 10:12:20 -07001565TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1566 sp<SurfaceControl> layer;
1567 ASSERT_NO_FATAL_FAILURE(
1568 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1569 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1570
1571 // crop is affected by matrix
1572 Transaction()
1573 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1574 .setCrop(layer, Rect(8, 8, 24, 24))
1575 .apply();
1576 auto shot = screenshot();
1577 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1578 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1579}
1580
1581TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001582 sp<SurfaceControl> layer;
1583 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001584 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001585
Marissa Wallf58c14b2018-07-24 10:50:43 -07001586 // setCrop_legacy is applied immediately by default, with or without resize pending
1587 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001588 {
1589 SCOPED_TRACE("resize pending");
1590 auto shot = screenshot();
1591 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1592 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1593 }
1594
Marissa Wall61c58622018-07-18 10:12:20 -07001595 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001596 {
1597 SCOPED_TRACE("resize applied");
1598 auto shot = screenshot();
1599 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1600 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1601 }
1602}
1603
Marissa Wall61c58622018-07-18 10:12:20 -07001604TEST_F(LayerTransactionTest, SetCropWithResize_BufferState) {
1605 sp<SurfaceControl> layer;
1606 ASSERT_NO_FATAL_FAILURE(
1607 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1608 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1609
1610 // setCrop_legacy is applied immediately by default, with or without resize pending
1611 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1612 {
1613 SCOPED_TRACE("new buffer pending");
1614 auto shot = screenshot();
1615 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1616 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1617 }
1618
1619 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1620 {
1621 SCOPED_TRACE("new buffer");
1622 auto shot = screenshot();
1623 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1624 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1625 }
1626}
1627
1628TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001629 sp<SurfaceControl> layer;
1630 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001631 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001632
Marissa Wallf58c14b2018-07-24 10:50:43 -07001633 // request setCrop_legacy to be applied with the next resize
1634 Transaction()
1635 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1636 .setGeometryAppliesWithResize(layer)
1637 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001638 {
1639 SCOPED_TRACE("waiting for next resize");
1640 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1641 }
1642
Marissa Wallf58c14b2018-07-24 10:50:43 -07001643 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001644 {
1645 SCOPED_TRACE("pending crop modified");
1646 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1647 }
1648
1649 Transaction().setSize(layer, 16, 16).apply();
1650 {
1651 SCOPED_TRACE("resize pending");
1652 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1653 }
1654
1655 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001656 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001657 {
1658 SCOPED_TRACE("new crop applied");
1659 auto shot = screenshot();
1660 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1661 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1662 }
1663}
1664
Marissa Wall61c58622018-07-18 10:12:20 -07001665TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1666 sp<SurfaceControl> layer;
1667 ASSERT_NO_FATAL_FAILURE(
1668 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1669 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1670
1671 // request setCrop_legacy to be applied with the next resize
1672 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1673 {
1674 SCOPED_TRACE("set crop 1");
1675 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1676 }
1677
1678 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1679 {
1680 SCOPED_TRACE("set crop 2");
1681 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1682 }
1683
1684 Transaction().setSize(layer, 16, 16).apply();
1685 {
1686 SCOPED_TRACE("resize");
1687 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1688 }
1689
1690 // finally resize
1691 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1692 {
1693 SCOPED_TRACE("new buffer");
1694 auto shot = screenshot();
1695 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1696 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1697 }
1698}
1699
1700TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001701 sp<SurfaceControl> layer;
1702 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001703 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001704
Marissa Wallf58c14b2018-07-24 10:50:43 -07001705 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001706 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001707 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001708 .setSize(layer, 16, 16)
1709 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1710 .setGeometryAppliesWithResize(layer)
1711 .apply();
1712 {
1713 SCOPED_TRACE("new crop pending");
1714 auto shot = screenshot();
1715 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1716 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1717 }
1718
1719 // XXX crop is never latched without other geometry change (b/69315677)
1720 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001721 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001722 Transaction().setPosition(layer, 0, 0).apply();
1723 {
1724 SCOPED_TRACE("new crop applied");
1725 auto shot = screenshot();
1726 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1727 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1728 }
1729}
1730
Marissa Wall61c58622018-07-18 10:12:20 -07001731TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1732 sp<SurfaceControl> layer;
1733 ASSERT_NO_FATAL_FAILURE(
1734 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1735 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1736
1737 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1738 Transaction()
1739 .setCrop(layer, Rect(4, 4, 12, 12))
1740 .setSize(layer, 16, 16)
1741 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1742 .setGeometryAppliesWithResize(layer)
1743 .apply();
1744 {
1745 SCOPED_TRACE("new crop pending");
1746 auto shot = screenshot();
1747 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1748 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1749 }
1750
1751 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1752 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1753 Transaction().setPosition(layer, 0, 0).apply();
1754 {
1755 SCOPED_TRACE("new crop applied");
1756 auto shot = screenshot();
1757 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1758 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1759 }
1760}
1761
Marissa Wall61c58622018-07-18 10:12:20 -07001762TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1763 sp<SurfaceControl> layer;
1764 ASSERT_NO_FATAL_FAILURE(
1765 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1766
1767 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1768
1769 auto shot = screenshot();
1770 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1771 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1772}
1773
1774TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1775 sp<SurfaceControl> layer;
1776 ASSERT_NO_FATAL_FAILURE(
1777 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1778
1779 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1780
1781 {
1782 SCOPED_TRACE("set buffer 1");
1783 auto shot = screenshot();
1784 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1785 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1786 }
1787
1788 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1789
1790 {
1791 SCOPED_TRACE("set buffer 2");
1792 auto shot = screenshot();
1793 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1794 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1795 }
1796
1797 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1798
1799 {
1800 SCOPED_TRACE("set buffer 3");
1801 auto shot = screenshot();
1802 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1803 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1804 }
1805}
1806
1807TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1808 sp<SurfaceControl> layer1;
1809 ASSERT_NO_FATAL_FAILURE(
1810 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1811
1812 sp<SurfaceControl> layer2;
1813 ASSERT_NO_FATAL_FAILURE(
1814 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1815
1816 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1817
1818 {
1819 SCOPED_TRACE("set layer 1 buffer red");
1820 auto shot = screenshot();
1821 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1822 }
1823
1824 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1825
1826 {
1827 SCOPED_TRACE("set layer 2 buffer blue");
1828 auto shot = screenshot();
1829 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1830 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1831 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1832 }
1833
1834 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1835 {
1836 SCOPED_TRACE("set layer 1 buffer green");
1837 auto shot = screenshot();
1838 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1839 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1840 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1841 }
1842
1843 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1844
1845 {
1846 SCOPED_TRACE("set layer 2 buffer white");
1847 auto shot = screenshot();
1848 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1849 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1850 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1851 }
1852}
1853
1854TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1855 sp<SurfaceControl> layer;
1856 ASSERT_NO_FATAL_FAILURE(
1857 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1858
1859 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1860 Color::BLUE, Color::WHITE));
1861
1862 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1863
1864 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1865 Color::GREEN, true /* filtered */);
1866}
1867
1868TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1869 sp<SurfaceControl> layer;
1870 ASSERT_NO_FATAL_FAILURE(
1871 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1872
1873 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1874 Color::BLUE, Color::WHITE));
1875
1876 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1877
1878 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1879 Color::BLUE, true /* filtered */);
1880}
1881
1882TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
1883 sp<SurfaceControl> layer;
1884 ASSERT_NO_FATAL_FAILURE(
1885 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1886
1887 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1888 Color::BLUE, Color::WHITE));
1889
1890 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
1891
1892 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1893 Color::GREEN, true /* filtered */);
1894}
1895
1896TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
1897 sp<SurfaceControl> layer;
1898 ASSERT_NO_FATAL_FAILURE(
1899 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1900
1901 Transaction().setTransformToDisplayInverse(layer, false).apply();
1902
1903 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
1904
1905 Transaction().setTransformToDisplayInverse(layer, true).apply();
1906}
1907
1908TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
1909 sp<SurfaceControl> layer;
1910 ASSERT_NO_FATAL_FAILURE(
1911 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1912
1913 sp<GraphicBuffer> buffer =
1914 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1915 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1916 BufferUsage::COMPOSER_OVERLAY,
1917 "test");
1918 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1919
1920 sp<Fence> fence = new Fence(-1);
1921
1922 Transaction()
1923 .setBuffer(layer, buffer)
1924 .setAcquireFence(layer, fence)
1925 .setSize(layer, 32, 32)
1926 .apply();
1927
1928 auto shot = screenshot();
1929 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1930 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1931}
1932
1933TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
1934 sp<SurfaceControl> layer;
1935 ASSERT_NO_FATAL_FAILURE(
1936 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1937
1938 sp<GraphicBuffer> buffer =
1939 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1940 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1941 BufferUsage::COMPOSER_OVERLAY,
1942 "test");
1943 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1944
1945 Transaction()
1946 .setBuffer(layer, buffer)
1947 .setDataspace(layer, ui::Dataspace::UNKNOWN)
1948 .setSize(layer, 32, 32)
1949 .apply();
1950
1951 auto shot = screenshot();
1952 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1953 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1954}
1955
1956TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
1957 sp<SurfaceControl> layer;
1958 ASSERT_NO_FATAL_FAILURE(
1959 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1960
1961 sp<GraphicBuffer> buffer =
1962 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1963 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1964 BufferUsage::COMPOSER_OVERLAY,
1965 "test");
1966 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1967
1968 HdrMetadata hdrMetadata;
1969 hdrMetadata.validTypes = 0;
1970 Transaction()
1971 .setBuffer(layer, buffer)
1972 .setHdrMetadata(layer, hdrMetadata)
1973 .setSize(layer, 32, 32)
1974 .apply();
1975
1976 auto shot = screenshot();
1977 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1978 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1979}
1980
1981TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
1982 sp<SurfaceControl> layer;
1983 ASSERT_NO_FATAL_FAILURE(
1984 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1985
1986 sp<GraphicBuffer> buffer =
1987 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1988 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1989 BufferUsage::COMPOSER_OVERLAY,
1990 "test");
1991 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1992
1993 Region region;
1994 region.set(32, 32);
1995 Transaction()
1996 .setBuffer(layer, buffer)
1997 .setSurfaceDamageRegion(layer, region)
1998 .setSize(layer, 32, 32)
1999 .apply();
2000
2001 auto shot = screenshot();
2002 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2003 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2004}
2005
2006TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2007 sp<SurfaceControl> layer;
2008 ASSERT_NO_FATAL_FAILURE(
2009 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2010
2011 sp<GraphicBuffer> buffer =
2012 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2013 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2014 BufferUsage::COMPOSER_OVERLAY,
2015 "test");
2016 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2017
2018 Transaction()
2019 .setBuffer(layer, buffer)
2020 .setApi(layer, NATIVE_WINDOW_API_CPU)
2021 .setSize(layer, 32, 32)
2022 .apply();
2023
2024 auto shot = screenshot();
2025 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2026 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2027}
2028
2029TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2030 sp<SurfaceControl> layer;
2031 ASSERT_NO_FATAL_FAILURE(
2032 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2033
2034 // verify this doesn't cause a crash
2035 Transaction().setSidebandStream(layer, nullptr).apply();
2036}
2037
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002038class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002039protected:
2040 virtual void SetUp() {
2041 mComposerClient = new SurfaceComposerClient;
2042 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
2043
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002044 sp<IBinder> display(
2045 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002046 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002047 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002048
2049 ssize_t displayWidth = info.w;
2050 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002051
2052 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002053 mBGSurfaceControl =
2054 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
2055 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002056 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002057 ASSERT_TRUE(mBGSurfaceControl->isValid());
2058 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2059
2060 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002061 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
2062 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002063 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002064 ASSERT_TRUE(mFGSurfaceControl->isValid());
2065
2066 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2067
2068 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002069 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
2070 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002071 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002072 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2073
2074 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2075
Robert Carr4cdc58f2017-08-23 14:22:20 -07002076 asTransaction([&](Transaction& t) {
2077 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002078
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002079 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002080
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002081 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2082 .setPosition(mFGSurfaceControl, 64, 64)
2083 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002084
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002085 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2086 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2087 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002088 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002089 }
2090
2091 virtual void TearDown() {
2092 mComposerClient->dispose();
2093 mBGSurfaceControl = 0;
2094 mFGSurfaceControl = 0;
2095 mSyncSurfaceControl = 0;
2096 mComposerClient = 0;
2097 }
2098
2099 void waitForPostedBuffers() {
2100 // Since the sync surface is in synchronous mode (i.e. double buffered)
2101 // posting three buffers to it should ensure that at least two
2102 // SurfaceFlinger::handlePageFlip calls have been made, which should
2103 // guaranteed that a buffer posted to another Surface has been retired.
2104 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2105 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2106 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2107 }
2108
Robert Carr4cdc58f2017-08-23 14:22:20 -07002109 void asTransaction(const std::function<void(Transaction&)>& exec) {
2110 Transaction t;
2111 exec(t);
2112 t.apply(true);
2113 }
2114
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002115 sp<SurfaceComposerClient> mComposerClient;
2116 sp<SurfaceControl> mBGSurfaceControl;
2117 sp<SurfaceControl> mFGSurfaceControl;
2118
2119 // This surface is used to ensure that the buffers posted to
2120 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2121 sp<SurfaceControl> mSyncSurfaceControl;
2122};
2123
Robert Carr7f619b22017-11-06 12:56:35 -08002124TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
2125 sp<ScreenCapture> sc;
2126
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002127 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
2128 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002129 fillSurfaceRGBA8(relative, 10, 10, 10);
2130 waitForPostedBuffers();
2131
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002132 Transaction{}
2133 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002134 .setPosition(relative, 64, 64)
2135 .apply();
2136
2137 {
2138 // The relative should be on top of the FG control.
2139 ScreenCapture::captureScreen(&sc);
2140 sc->checkPixel(64, 64, 10, 10, 10);
2141 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002142 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002143
2144 {
2145 // Nothing should change at this point.
2146 ScreenCapture::captureScreen(&sc);
2147 sc->checkPixel(64, 64, 10, 10, 10);
2148 }
2149
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002150 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002151
2152 {
2153 // Ensure that the relative was actually hidden, rather than
2154 // being left in the detached but visible state.
2155 ScreenCapture::captureScreen(&sc);
2156 sc->expectFGColor(64, 64);
2157 }
2158}
2159
Robert Carr8d5227b2017-03-16 15:41:03 -07002160class GeometryLatchingTest : public LayerUpdateTest {
2161protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002162 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002163 SCOPED_TRACE(trace);
2164 ScreenCapture::captureScreen(&sc);
2165 // We find the leading edge of the FG surface.
2166 sc->expectFGColor(127, 127);
2167 sc->expectBGColor(128, 128);
2168 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002169
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002170 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002171
2172 void unlockFGBuffer() {
2173 sp<Surface> s = mFGSurfaceControl->getSurface();
2174 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2175 waitForPostedBuffers();
2176 }
2177
Robert Carr8d5227b2017-03-16 15:41:03 -07002178 void completeFGResize() {
2179 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2180 waitForPostedBuffers();
2181 }
2182 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002183 asTransaction([&](Transaction& t) {
2184 t.setSize(mFGSurfaceControl, 64, 64);
2185 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002186 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002187 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002188
2189 EXPECT_INITIAL_STATE("After restoring initial state");
2190 }
2191 sp<ScreenCapture> sc;
2192};
2193
Robert Carr8d5227b2017-03-16 15:41:03 -07002194class CropLatchingTest : public GeometryLatchingTest {
2195protected:
2196 void EXPECT_CROPPED_STATE(const char* trace) {
2197 SCOPED_TRACE(trace);
2198 ScreenCapture::captureScreen(&sc);
2199 // The edge should be moved back one pixel by our crop.
2200 sc->expectFGColor(126, 126);
2201 sc->expectBGColor(127, 127);
2202 sc->expectBGColor(128, 128);
2203 }
chaviw59f5c562017-06-28 16:39:06 -07002204
2205 void EXPECT_RESIZE_STATE(const char* trace) {
2206 SCOPED_TRACE(trace);
2207 ScreenCapture::captureScreen(&sc);
2208 // The FG is now resized too 128,128 at 64,64
2209 sc->expectFGColor(64, 64);
2210 sc->expectFGColor(191, 191);
2211 sc->expectBGColor(192, 192);
2212 }
Robert Carr8d5227b2017-03-16 15:41:03 -07002213};
2214
Pablo Ceballos05289c22016-04-14 15:49:55 -07002215TEST_F(LayerUpdateTest, DeferredTransactionTest) {
2216 sp<ScreenCapture> sc;
2217 {
2218 SCOPED_TRACE("before anything");
2219 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002220 sc->expectBGColor(32, 32);
2221 sc->expectFGColor(96, 96);
2222 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002223 }
2224
2225 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002226 asTransaction([&](Transaction& t) {
2227 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002228 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2229 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002230 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002231
Robert Carr4cdc58f2017-08-23 14:22:20 -07002232 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002233 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002234 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2235 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002236 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002237
2238 {
2239 SCOPED_TRACE("before any trigger");
2240 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002241 sc->expectBGColor(32, 32);
2242 sc->expectFGColor(96, 96);
2243 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002244 }
2245
2246 // should trigger the first deferred transaction, but not the second one
2247 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2248 {
2249 SCOPED_TRACE("after first trigger");
2250 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002251 sc->expectBGColor(32, 32);
2252 sc->checkPixel(96, 96, 162, 63, 96);
2253 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002254 }
2255
2256 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002257 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002258
2259 // trigger the second deferred transaction
2260 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2261 {
2262 SCOPED_TRACE("after second trigger");
2263 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002264 sc->expectBGColor(32, 32);
2265 sc->expectBGColor(96, 96);
2266 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002267 }
2268}
2269
Robert Carre392b552017-09-19 12:16:05 -07002270TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
2271 sp<ScreenCapture> sc;
2272
2273 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002274 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
2275 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2276 sp<SurfaceControl> childBuffer =
2277 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
2278 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002279 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
2280
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002281 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002282
2283 {
2284 ScreenCapture::captureScreen(&sc);
2285 sc->expectChildColor(73, 73);
2286 sc->expectFGColor(74, 74);
2287 }
2288
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002289 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002290
2291 {
2292 ScreenCapture::captureScreen(&sc);
2293 sc->expectChildColor(73, 73);
2294 sc->expectChildColor(74, 74);
2295 }
2296}
2297
Robert Carr2c5f6d22017-09-26 12:30:35 -07002298TEST_F(LayerUpdateTest, MergingTransactions) {
2299 sp<ScreenCapture> sc;
2300 {
2301 SCOPED_TRACE("before move");
2302 ScreenCapture::captureScreen(&sc);
2303 sc->expectBGColor(0, 12);
2304 sc->expectFGColor(75, 75);
2305 sc->expectBGColor(145, 145);
2306 }
2307
2308 Transaction t1, t2;
2309 t1.setPosition(mFGSurfaceControl, 128, 128);
2310 t2.setPosition(mFGSurfaceControl, 0, 0);
2311 // We expect that the position update from t2 now
2312 // overwrites the position update from t1.
2313 t1.merge(std::move(t2));
2314 t1.apply();
2315
2316 {
2317 ScreenCapture::captureScreen(&sc);
2318 sc->expectFGColor(1, 1);
2319 }
2320}
2321
Robert Carr1f0a16a2016-10-24 16:27:39 -07002322class ChildLayerTest : public LayerUpdateTest {
2323protected:
2324 void SetUp() override {
2325 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002326 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2327 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002328 fillSurfaceRGBA8(mChild, 200, 200, 200);
2329
2330 {
2331 SCOPED_TRACE("before anything");
2332 ScreenCapture::captureScreen(&mCapture);
2333 mCapture->expectChildColor(64, 64);
2334 }
2335 }
2336 void TearDown() override {
2337 LayerUpdateTest::TearDown();
2338 mChild = 0;
2339 }
2340
2341 sp<SurfaceControl> mChild;
2342 sp<ScreenCapture> mCapture;
2343};
2344
2345TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002346 asTransaction([&](Transaction& t) {
2347 t.show(mChild);
2348 t.setPosition(mChild, 10, 10);
2349 t.setPosition(mFGSurfaceControl, 64, 64);
2350 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002351
2352 {
2353 ScreenCapture::captureScreen(&mCapture);
2354 // Top left of foreground must now be visible
2355 mCapture->expectFGColor(64, 64);
2356 // But 10 pixels in we should see the child surface
2357 mCapture->expectChildColor(74, 74);
2358 // And 10 more pixels we should be back to the foreground surface
2359 mCapture->expectFGColor(84, 84);
2360 }
2361
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002362 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002363
2364 {
2365 ScreenCapture::captureScreen(&mCapture);
2366 // Top left of foreground should now be at 0, 0
2367 mCapture->expectFGColor(0, 0);
2368 // But 10 pixels in we should see the child surface
2369 mCapture->expectChildColor(10, 10);
2370 // And 10 more pixels we should be back to the foreground surface
2371 mCapture->expectFGColor(20, 20);
2372 }
2373}
2374
Robert Carr41b08b52017-06-01 16:11:34 -07002375TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002376 asTransaction([&](Transaction& t) {
2377 t.show(mChild);
2378 t.setPosition(mChild, 0, 0);
2379 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002380 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002381 });
Robert Carr41b08b52017-06-01 16:11:34 -07002382
2383 {
2384 ScreenCapture::captureScreen(&mCapture);
2385 mCapture->expectChildColor(0, 0);
2386 mCapture->expectChildColor(4, 4);
2387 mCapture->expectBGColor(5, 5);
2388 }
2389}
2390
Robert Carr1f0a16a2016-10-24 16:27:39 -07002391TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002392 asTransaction([&](Transaction& t) {
2393 t.show(mChild);
2394 t.setPosition(mFGSurfaceControl, 0, 0);
2395 t.setPosition(mChild, 63, 63);
2396 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002397
2398 {
2399 ScreenCapture::captureScreen(&mCapture);
2400 mCapture->expectFGColor(0, 0);
2401 // Last pixel in foreground should now be the child.
2402 mCapture->expectChildColor(63, 63);
2403 // But the child should be constrained and the next pixel
2404 // must be the background
2405 mCapture->expectBGColor(64, 64);
2406 }
2407}
2408
2409TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002410 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002411
2412 // Find the boundary between the parent and child
2413 {
2414 ScreenCapture::captureScreen(&mCapture);
2415 mCapture->expectChildColor(9, 9);
2416 mCapture->expectFGColor(10, 10);
2417 }
2418
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002419 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002420
2421 // The boundary should be twice as far from the origin now.
2422 // The pixels from the last test should all be child now
2423 {
2424 ScreenCapture::captureScreen(&mCapture);
2425 mCapture->expectChildColor(9, 9);
2426 mCapture->expectChildColor(10, 10);
2427 mCapture->expectChildColor(19, 19);
2428 mCapture->expectFGColor(20, 20);
2429 }
2430}
Robert Carr9524cb32017-02-13 11:32:32 -08002431
Robert Carr6452f122017-03-21 10:41:29 -07002432TEST_F(ChildLayerTest, ChildLayerAlpha) {
2433 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2434 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2435 fillSurfaceRGBA8(mChild, 0, 254, 0);
2436 waitForPostedBuffers();
2437
Robert Carr4cdc58f2017-08-23 14:22:20 -07002438 asTransaction([&](Transaction& t) {
2439 t.show(mChild);
2440 t.setPosition(mChild, 0, 0);
2441 t.setPosition(mFGSurfaceControl, 0, 0);
2442 });
Robert Carr6452f122017-03-21 10:41:29 -07002443
2444 {
2445 ScreenCapture::captureScreen(&mCapture);
2446 // Unblended child color
2447 mCapture->checkPixel(0, 0, 0, 254, 0);
2448 }
2449
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002450 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002451
2452 {
2453 ScreenCapture::captureScreen(&mCapture);
2454 // Child and BG blended.
2455 mCapture->checkPixel(0, 0, 127, 127, 0);
2456 }
2457
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002458 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002459
2460 {
2461 ScreenCapture::captureScreen(&mCapture);
2462 // Child and BG blended.
2463 mCapture->checkPixel(0, 0, 95, 64, 95);
2464 }
2465}
2466
Robert Carr9524cb32017-02-13 11:32:32 -08002467TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002468 asTransaction([&](Transaction& t) {
2469 t.show(mChild);
2470 t.setPosition(mChild, 10, 10);
2471 t.setPosition(mFGSurfaceControl, 64, 64);
2472 });
Robert Carr9524cb32017-02-13 11:32:32 -08002473
2474 {
2475 ScreenCapture::captureScreen(&mCapture);
2476 // Top left of foreground must now be visible
2477 mCapture->expectFGColor(64, 64);
2478 // But 10 pixels in we should see the child surface
2479 mCapture->expectChildColor(74, 74);
2480 // And 10 more pixels we should be back to the foreground surface
2481 mCapture->expectFGColor(84, 84);
2482 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002483
2484 asTransaction([&](Transaction& t) {
2485 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2486 });
2487
Robert Carr9524cb32017-02-13 11:32:32 -08002488 {
2489 ScreenCapture::captureScreen(&mCapture);
2490 mCapture->expectFGColor(64, 64);
2491 // In reparenting we should have exposed the entire foreground surface.
2492 mCapture->expectFGColor(74, 74);
2493 // And the child layer should now begin at 10, 10 (since the BG
2494 // layer is at (0, 0)).
2495 mCapture->expectBGColor(9, 9);
2496 mCapture->expectChildColor(10, 10);
2497 }
2498}
2499
chaviw161410b02017-07-27 10:46:08 -07002500TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002501 asTransaction([&](Transaction& t) {
2502 t.show(mChild);
2503 t.setPosition(mChild, 10, 10);
2504 t.setPosition(mFGSurfaceControl, 64, 64);
2505 });
Robert Carr9524cb32017-02-13 11:32:32 -08002506
2507 {
2508 ScreenCapture::captureScreen(&mCapture);
2509 // Top left of foreground must now be visible
2510 mCapture->expectFGColor(64, 64);
2511 // But 10 pixels in we should see the child surface
2512 mCapture->expectChildColor(74, 74);
2513 // And 10 more pixels we should be back to the foreground surface
2514 mCapture->expectFGColor(84, 84);
2515 }
2516
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002517 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002518
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002519 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002520
chaviw161410b02017-07-27 10:46:08 -07002521 // Since the child has the same client as the parent, it will not get
2522 // detached and will be hidden.
2523 {
2524 ScreenCapture::captureScreen(&mCapture);
2525 mCapture->expectFGColor(64, 64);
2526 mCapture->expectFGColor(74, 74);
2527 mCapture->expectFGColor(84, 84);
2528 }
2529}
2530
2531TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2532 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002533 sp<SurfaceControl> mChildNewClient =
2534 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2535 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002536
Peiyong Lin566a3b42018-01-09 18:22:43 -08002537 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002538 ASSERT_TRUE(mChildNewClient->isValid());
2539
2540 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2541
Robert Carr4cdc58f2017-08-23 14:22:20 -07002542 asTransaction([&](Transaction& t) {
2543 t.hide(mChild);
2544 t.show(mChildNewClient);
2545 t.setPosition(mChildNewClient, 10, 10);
2546 t.setPosition(mFGSurfaceControl, 64, 64);
2547 });
chaviw161410b02017-07-27 10:46:08 -07002548
2549 {
2550 ScreenCapture::captureScreen(&mCapture);
2551 // Top left of foreground must now be visible
2552 mCapture->expectFGColor(64, 64);
2553 // But 10 pixels in we should see the child surface
2554 mCapture->expectChildColor(74, 74);
2555 // And 10 more pixels we should be back to the foreground surface
2556 mCapture->expectFGColor(84, 84);
2557 }
2558
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002559 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002560
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002561 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002562
Robert Carr9524cb32017-02-13 11:32:32 -08002563 // Nothing should have changed.
2564 {
2565 ScreenCapture::captureScreen(&mCapture);
2566 mCapture->expectFGColor(64, 64);
2567 mCapture->expectChildColor(74, 74);
2568 mCapture->expectFGColor(84, 84);
2569 }
2570}
2571
Robert Carr9b429f42017-04-17 14:56:57 -07002572TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002573 asTransaction([&](Transaction& t) {
2574 t.show(mChild);
2575 t.setPosition(mChild, 0, 0);
2576 t.setPosition(mFGSurfaceControl, 0, 0);
2577 });
Robert Carr9b429f42017-04-17 14:56:57 -07002578
2579 {
2580 ScreenCapture::captureScreen(&mCapture);
2581 // We've positioned the child in the top left.
2582 mCapture->expectChildColor(0, 0);
2583 // But it's only 10x10.
2584 mCapture->expectFGColor(10, 10);
2585 }
2586
Robert Carr4cdc58f2017-08-23 14:22:20 -07002587 asTransaction([&](Transaction& t) {
2588 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2589 // We cause scaling by 2.
2590 t.setSize(mFGSurfaceControl, 128, 128);
2591 });
Robert Carr9b429f42017-04-17 14:56:57 -07002592
2593 {
2594 ScreenCapture::captureScreen(&mCapture);
2595 // We've positioned the child in the top left.
2596 mCapture->expectChildColor(0, 0);
2597 mCapture->expectChildColor(10, 10);
2598 mCapture->expectChildColor(19, 19);
2599 // And now it should be scaled all the way to 20x20
2600 mCapture->expectFGColor(20, 20);
2601 }
2602}
2603
Robert Carr1725eee2017-04-26 18:32:15 -07002604// Regression test for b/37673612
2605TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002606 asTransaction([&](Transaction& t) {
2607 t.show(mChild);
2608 t.setPosition(mChild, 0, 0);
2609 t.setPosition(mFGSurfaceControl, 0, 0);
2610 });
Robert Carr1725eee2017-04-26 18:32:15 -07002611
2612 {
2613 ScreenCapture::captureScreen(&mCapture);
2614 // We've positioned the child in the top left.
2615 mCapture->expectChildColor(0, 0);
2616 // But it's only 10x10.
2617 mCapture->expectFGColor(10, 10);
2618 }
Robert Carr1725eee2017-04-26 18:32:15 -07002619 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2620 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002621 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002622 sp<Surface> s = mFGSurfaceControl->getSurface();
2623 auto anw = static_cast<ANativeWindow*>(s.get());
2624 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2625 native_window_set_buffers_dimensions(anw, 64, 128);
2626 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2627 waitForPostedBuffers();
2628
2629 {
2630 // The child should still be in the same place and not have any strange scaling as in
2631 // b/37673612.
2632 ScreenCapture::captureScreen(&mCapture);
2633 mCapture->expectChildColor(0, 0);
2634 mCapture->expectFGColor(10, 10);
2635 }
2636}
2637
Dan Stoza412903f2017-04-27 13:42:17 -07002638TEST_F(ChildLayerTest, Bug36858924) {
2639 // Destroy the child layer
2640 mChild.clear();
2641
2642 // Now recreate it as hidden
2643 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2644 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2645 mFGSurfaceControl.get());
2646
2647 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002648 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002649 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
2650 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002651 t.show(mChild);
2652 });
Dan Stoza412903f2017-04-27 13:42:17 -07002653
2654 // Render the foreground surface a few times
2655 //
2656 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2657 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2658 // never acquire/release the first buffer
2659 ALOGI("Filling 1");
2660 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2661 ALOGI("Filling 2");
2662 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2663 ALOGI("Filling 3");
2664 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2665 ALOGI("Filling 4");
2666 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2667}
2668
chaviwf1961f72017-09-18 16:41:07 -07002669TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002670 asTransaction([&](Transaction& t) {
2671 t.show(mChild);
2672 t.setPosition(mChild, 10, 10);
2673 t.setPosition(mFGSurfaceControl, 64, 64);
2674 });
chaviw06178942017-07-27 10:25:59 -07002675
2676 {
2677 ScreenCapture::captureScreen(&mCapture);
2678 // Top left of foreground must now be visible
2679 mCapture->expectFGColor(64, 64);
2680 // But 10 pixels in we should see the child surface
2681 mCapture->expectChildColor(74, 74);
2682 // And 10 more pixels we should be back to the foreground surface
2683 mCapture->expectFGColor(84, 84);
2684 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002685
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002686 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002687
chaviw06178942017-07-27 10:25:59 -07002688 {
2689 ScreenCapture::captureScreen(&mCapture);
2690 mCapture->expectFGColor(64, 64);
2691 // In reparenting we should have exposed the entire foreground surface.
2692 mCapture->expectFGColor(74, 74);
2693 // And the child layer should now begin at 10, 10 (since the BG
2694 // layer is at (0, 0)).
2695 mCapture->expectBGColor(9, 9);
2696 mCapture->expectChildColor(10, 10);
2697 }
2698}
2699
chaviwf1961f72017-09-18 16:41:07 -07002700TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002701 asTransaction([&](Transaction& t) {
2702 t.show(mChild);
2703 t.setPosition(mChild, 10, 10);
2704 t.setPosition(mFGSurfaceControl, 64, 64);
2705 });
chaviwf1961f72017-09-18 16:41:07 -07002706
2707 {
2708 ScreenCapture::captureScreen(&mCapture);
2709 // Top left of foreground must now be visible
2710 mCapture->expectFGColor(64, 64);
2711 // But 10 pixels in we should see the child surface
2712 mCapture->expectChildColor(74, 74);
2713 // And 10 more pixels we should be back to the foreground surface
2714 mCapture->expectFGColor(84, 84);
2715 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002716 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002717 {
2718 ScreenCapture::captureScreen(&mCapture);
2719 // Nothing should have changed.
2720 mCapture->expectFGColor(64, 64);
2721 mCapture->expectChildColor(74, 74);
2722 mCapture->expectFGColor(84, 84);
2723 }
2724}
2725
2726TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002727 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2728 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002729 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002730 ASSERT_TRUE(newSurface->isValid());
2731
2732 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002733 asTransaction([&](Transaction& t) {
2734 t.hide(mChild);
2735 t.show(newSurface);
2736 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002737 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002738 t.setPosition(mFGSurfaceControl, 64, 64);
2739 });
chaviwf1961f72017-09-18 16:41:07 -07002740
2741 {
2742 ScreenCapture::captureScreen(&mCapture);
2743 // Top left of foreground must now be visible
2744 mCapture->expectFGColor(64, 64);
2745 // At 10, 10 we should see the new surface
2746 mCapture->checkPixel(10, 10, 63, 195, 63);
2747 }
2748
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002749 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002750
2751 {
2752 ScreenCapture::captureScreen(&mCapture);
2753 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2754 // mFGSurface, putting it at 74, 74.
2755 mCapture->expectFGColor(64, 64);
2756 mCapture->checkPixel(74, 74, 63, 195, 63);
2757 mCapture->expectFGColor(84, 84);
2758 }
2759}
2760
chaviwc9674332017-08-28 12:32:18 -07002761TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002762 sp<SurfaceControl> grandchild =
2763 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2764 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002765 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2766
2767 {
2768 ScreenCapture::captureScreen(&mCapture);
2769 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2770 // which begins at 64, 64
2771 mCapture->checkPixel(64, 64, 50, 50, 50);
2772 }
2773}
2774
Robert Carr503c7042017-09-27 15:06:08 -07002775TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002776 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2777 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002778 fillSurfaceRGBA8(relative, 255, 255, 255);
2779
2780 Transaction t;
2781 t.setLayer(relative, INT32_MAX)
2782 .setRelativeLayer(mChild, relative->getHandle(), 1)
2783 .setPosition(mFGSurfaceControl, 0, 0)
2784 .apply(true);
2785
2786 // We expect that the child should have been elevated above our
2787 // INT_MAX layer even though it's not a child of it.
2788 {
2789 ScreenCapture::captureScreen(&mCapture);
2790 mCapture->expectChildColor(0, 0);
2791 mCapture->expectChildColor(9, 9);
2792 mCapture->checkPixel(10, 10, 255, 255, 255);
2793 }
2794}
2795
chaviwa76b2712017-09-20 12:02:26 -07002796class ScreenCaptureTest : public LayerUpdateTest {
2797protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002798 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002799};
2800
2801TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2802 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002803 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002804 mCapture->expectBGColor(0, 0);
2805 // Doesn't capture FG layer which is at 64, 64
2806 mCapture->expectBGColor(64, 64);
2807}
2808
2809TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2810 auto fgHandle = mFGSurfaceControl->getHandle();
2811
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002812 sp<SurfaceControl> child =
2813 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2814 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002815 fillSurfaceRGBA8(child, 200, 200, 200);
2816
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002817 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002818
2819 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002820 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002821 mCapture->expectFGColor(10, 10);
2822 mCapture->expectChildColor(0, 0);
2823}
2824
Robert Carr578038f2018-03-09 12:25:24 -08002825TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2826 auto fgHandle = mFGSurfaceControl->getHandle();
2827
2828 sp<SurfaceControl> child =
2829 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2830 0, mFGSurfaceControl.get());
2831 fillSurfaceRGBA8(child, 200, 200, 200);
2832
2833 SurfaceComposerClient::Transaction().show(child).apply(true);
2834
2835 // Captures mFGSurfaceControl's child
2836 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2837 mCapture->checkPixel(10, 10, 0, 0, 0);
2838 mCapture->expectChildColor(0, 0);
2839}
2840
chaviw50da5042018-04-09 13:49:37 -07002841TEST_F(ScreenCaptureTest, CaptureTransparent) {
2842 sp<SurfaceControl> child =
2843 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2844 0, mFGSurfaceControl.get());
2845
2846 fillSurfaceRGBA8(child, 200, 200, 200);
2847
2848 SurfaceComposerClient::Transaction().show(child).apply(true);
2849
2850 auto childHandle = child->getHandle();
2851
2852 // Captures child
2853 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
2854 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
2855 // Area outside of child's bounds is transparent.
2856 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
2857}
2858
chaviw4b129c22018-04-09 16:19:43 -07002859TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
2860 auto fgHandle = mFGSurfaceControl->getHandle();
2861
2862 sp<SurfaceControl> child =
2863 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2864 0, mFGSurfaceControl.get());
2865 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 10,
2866 10, PIXEL_FORMAT_RGBA_8888, 0);
2867 fillSurfaceRGBA8(child, 200, 200, 200);
2868 fillSurfaceRGBA8(relative, 100, 100, 100);
2869
2870 SurfaceComposerClient::Transaction()
2871 .show(child)
2872 // Set relative layer above fg layer so should be shown above when computing all layers.
2873 .setRelativeLayer(relative, fgHandle, 1)
2874 .show(relative)
2875 .apply(true);
2876
2877 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
2878 ScreenCapture::captureLayers(&mCapture, fgHandle);
2879 mCapture->expectFGColor(10, 10);
2880 mCapture->expectChildColor(0, 0);
2881}
2882
2883TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
2884 auto fgHandle = mFGSurfaceControl->getHandle();
2885
2886 sp<SurfaceControl> child =
2887 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2888 0, mFGSurfaceControl.get());
2889 sp<SurfaceControl> relative =
2890 mComposerClient->createSurface(String8("Relative surface"), 10, 10,
2891 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2892 fillSurfaceRGBA8(child, 200, 200, 200);
2893 fillSurfaceRGBA8(relative, 100, 100, 100);
2894
2895 SurfaceComposerClient::Transaction()
2896 .show(child)
2897 // Set relative layer below fg layer but relative to child layer so it should be shown
2898 // above child layer.
2899 .setLayer(relative, -1)
2900 .setRelativeLayer(relative, child->getHandle(), 1)
2901 .show(relative)
2902 .apply(true);
2903
2904 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
2905 // relative value should be taken into account, placing it above child layer.
2906 ScreenCapture::captureLayers(&mCapture, fgHandle);
2907 mCapture->expectFGColor(10, 10);
2908 // Relative layer is showing on top of child layer
2909 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
2910}
Robert Carr578038f2018-03-09 12:25:24 -08002911
2912// In the following tests we verify successful skipping of a parent layer,
2913// so we use the same verification logic and only change how we mutate
2914// the parent layer to verify that various properties are ignored.
2915class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2916public:
2917 void SetUp() override {
2918 LayerUpdateTest::SetUp();
2919
2920 mChild =
2921 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2922 0, mFGSurfaceControl.get());
2923 fillSurfaceRGBA8(mChild, 200, 200, 200);
2924
2925 SurfaceComposerClient::Transaction().show(mChild).apply(true);
2926 }
2927
2928 void verify() {
2929 auto fgHandle = mFGSurfaceControl->getHandle();
2930 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2931 mCapture->checkPixel(10, 10, 0, 0, 0);
2932 mCapture->expectChildColor(0, 0);
2933 }
2934
2935 std::unique_ptr<ScreenCapture> mCapture;
2936 sp<SurfaceControl> mChild;
2937};
2938
2939TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2940
2941 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2942
2943 // Even though the parent is hidden we should still capture the child.
2944 verify();
2945}
2946
2947TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002948 SurfaceComposerClient::Transaction()
2949 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
2950 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08002951
2952 // Even though the parent is cropped out we should still capture the child.
2953 verify();
2954}
2955
2956TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2957
2958 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2959
2960 // We should not inherit the parent scaling.
2961 verify();
2962}
2963
Robert Carr15eae092018-03-23 13:43:53 -07002964TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
2965 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2966
2967 // Even though the parent is hidden we should still capture the child.
2968 verify();
2969
2970 // Verify everything was properly hidden when rendering the full-screen.
2971 screenshot()->expectBGColor(0,0);
2972}
2973
2974
chaviwa76b2712017-09-20 12:02:26 -07002975TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2976 auto fgHandle = mFGSurfaceControl->getHandle();
2977
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002978 sp<SurfaceControl> child =
2979 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2980 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002981 fillSurfaceRGBA8(child, 200, 200, 200);
2982
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002983 sp<SurfaceControl> grandchild =
2984 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2985 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002986
2987 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2988 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002989 .show(child)
2990 .setPosition(grandchild, 5, 5)
2991 .show(grandchild)
2992 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002993
2994 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002995 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002996 mCapture->expectFGColor(10, 10);
2997 mCapture->expectChildColor(0, 0);
2998 mCapture->checkPixel(5, 5, 50, 50, 50);
2999}
3000
3001TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003002 sp<SurfaceControl> child =
3003 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3004 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003005 fillSurfaceRGBA8(child, 200, 200, 200);
3006 auto childHandle = child->getHandle();
3007
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003008 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003009
3010 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003011 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07003012 mCapture->expectChildColor(0, 0);
3013 mCapture->expectChildColor(9, 9);
3014}
3015
3016TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003017 sp<SurfaceControl> child =
3018 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3019 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003020 fillSurfaceRGBA8(child, 200, 200, 200);
3021 auto childHandle = child->getHandle();
3022
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003023 sp<SurfaceControl> grandchild =
3024 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
3025 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003026 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3027
3028 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003029 .show(child)
3030 .setPosition(grandchild, 5, 5)
3031 .show(grandchild)
3032 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003033
3034 auto grandchildHandle = grandchild->getHandle();
3035
3036 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003037 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07003038 mCapture->checkPixel(0, 0, 50, 50, 50);
3039 mCapture->checkPixel(4, 4, 50, 50, 50);
3040}
3041
chaviw7206d492017-11-10 16:16:12 -08003042TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003043 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
3044 PIXEL_FORMAT_RGBA_8888, 0);
3045 sp<SurfaceControl> blueLayer =
3046 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
3047 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003048
Marissa Wall61c58622018-07-18 10:12:20 -07003049 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3050 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003051
3052 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003053 .setLayer(redLayer, INT32_MAX - 1)
3054 .show(redLayer)
3055 .show(blueLayer)
3056 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003057
3058 auto redLayerHandle = redLayer->getHandle();
3059
3060 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003061 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3062 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3063 // red area below the blue area
3064 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3065 // red area to the right of the blue area
3066 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003067
3068 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003069 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08003070 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
3071 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003072 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08003073 mCapture->checkPixel(30, 30, 0, 0, 0);
3074}
3075
3076TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003077 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
3078 PIXEL_FORMAT_RGBA_8888, 0);
3079 sp<SurfaceControl> blueLayer =
3080 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
3081 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003082
Marissa Wall61c58622018-07-18 10:12:20 -07003083 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3084 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003085
3086 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003087 .setLayer(redLayer, INT32_MAX - 1)
3088 .show(redLayer)
3089 .show(blueLayer)
3090 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003091
3092 auto redLayerHandle = redLayer->getHandle();
3093
3094 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003095 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3096 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3097 // red area below the blue area
3098 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3099 // red area to the right of the blue area
3100 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003101
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003102 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08003103 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003104 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
3105 // red area below the blue area
3106 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
3107 // red area to the right of the blue area
3108 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003109 mCapture->checkPixel(30, 30, 0, 0, 0);
3110}
3111
3112TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003113 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
3114 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08003115
Marissa Wall61c58622018-07-18 10:12:20 -07003116 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08003117
3118 auto redLayerHandle = redLayer->getHandle();
3119 mComposerClient->destroySurface(redLayerHandle);
3120 SurfaceComposerClient::Transaction().apply(true);
3121
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003122 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08003123
3124 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003125 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
3126 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08003127}
3128
chaviw8e3fe5d2018-02-22 10:55:42 -08003129
3130class DereferenceSurfaceControlTest : public LayerTransactionTest {
3131protected:
3132 void SetUp() override {
3133 LayerTransactionTest::SetUp();
3134 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003135 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003136 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003137 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003138 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
3139 {
3140 SCOPED_TRACE("before anything");
3141 auto shot = screenshot();
3142 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3143 }
3144 }
3145 void TearDown() override {
3146 LayerTransactionTest::TearDown();
3147 bgLayer = 0;
3148 fgLayer = 0;
3149 }
3150
3151 sp<SurfaceControl> bgLayer;
3152 sp<SurfaceControl> fgLayer;
3153};
3154
3155TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
3156 fgLayer = nullptr;
3157 {
3158 SCOPED_TRACE("after setting null");
3159 auto shot = screenshot();
3160 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
3161 }
3162}
3163
3164TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
3165 auto transaction = Transaction().show(fgLayer);
3166 fgLayer = nullptr;
3167 {
3168 SCOPED_TRACE("after setting null");
3169 auto shot = screenshot();
3170 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3171 }
3172}
3173
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003174} // namespace android