blob: 4ab4fec267a8de59264bccee868584d9719e0a51 [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
1762TEST_F(LayerTransactionTest, SetFinalCropBasic_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001763 sp<SurfaceControl> layer;
1764 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001765 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001766 const Rect crop(8, 8, 24, 24);
1767
1768 // same as in SetCropBasic
Marissa Wallf58c14b2018-07-24 10:50:43 -07001769 Transaction().setFinalCrop_legacy(layer, crop).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001770 auto shot = screenshot();
1771 shot->expectColor(crop, Color::RED);
1772 shot->expectBorder(crop, Color::BLACK);
1773}
1774
Marissa Wall61c58622018-07-18 10:12:20 -07001775TEST_F(LayerTransactionTest, SetFinalCropEmpty_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001776 sp<SurfaceControl> layer;
1777 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001778 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001779
1780 // same as in SetCropEmpty
1781 {
1782 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001783 Transaction().setFinalCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001784 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1785 }
1786
1787 {
1788 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001789 Transaction().setFinalCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001790 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1791 }
1792}
1793
Marissa Wall61c58622018-07-18 10:12:20 -07001794TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001795 sp<SurfaceControl> layer;
1796 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001797 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001798
1799 // same as in SetCropOutOfBounds
Marissa Wallf58c14b2018-07-24 10:50:43 -07001800 Transaction().setFinalCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001801 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
Marissa Wall61c58622018-07-18 10:12:20 -07001806TEST_F(LayerTransactionTest, SetFinalCropWithTranslation_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001807 sp<SurfaceControl> layer;
1808 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001809 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001810
1811 // final crop is applied post-translation
Marissa Wallf58c14b2018-07-24 10:50:43 -07001812 Transaction().setPosition(layer, 16, 16).setFinalCrop_legacy(layer, Rect(8, 8, 24, 24)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001813 auto shot = screenshot();
1814 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1815 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1816}
1817
Marissa Wall61c58622018-07-18 10:12:20 -07001818TEST_F(LayerTransactionTest, SetFinalCropWithScale_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001819 sp<SurfaceControl> layer;
1820 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001821 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001822
1823 // final crop is not affected by matrix
1824 Transaction()
1825 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001826 .setFinalCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wucdd71a52017-11-02 08:30:27 -07001827 .apply();
1828 auto shot = screenshot();
1829 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1830 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1831}
1832
Marissa Wall61c58622018-07-18 10:12:20 -07001833TEST_F(LayerTransactionTest, SetFinalCropWithResize_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001834 sp<SurfaceControl> layer;
1835 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001836 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001837
1838 // same as in SetCropWithResize
Marissa Wallf58c14b2018-07-24 10:50:43 -07001839 Transaction().setFinalCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001840 {
1841 SCOPED_TRACE("resize pending");
1842 auto shot = screenshot();
1843 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1844 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1845 }
1846
Marissa Wall61c58622018-07-18 10:12:20 -07001847 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001848 {
1849 SCOPED_TRACE("resize applied");
1850 auto shot = screenshot();
1851 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1852 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1853 }
1854}
1855
Marissa Wall61c58622018-07-18 10:12:20 -07001856TEST_F(LayerTransactionTest, SetFinalCropWithNextResize_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001857 sp<SurfaceControl> layer;
1858 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001859 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001860
1861 // same as in SetCropWithNextResize
1862 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001863 .setFinalCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wucdd71a52017-11-02 08:30:27 -07001864 .setGeometryAppliesWithResize(layer)
1865 .apply();
1866 {
1867 SCOPED_TRACE("waiting for next resize");
1868 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1869 }
1870
Marissa Wallf58c14b2018-07-24 10:50:43 -07001871 Transaction().setFinalCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001872 {
1873 SCOPED_TRACE("pending final crop modified");
1874 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1875 }
1876
1877 Transaction().setSize(layer, 16, 16).apply();
1878 {
1879 SCOPED_TRACE("resize pending");
1880 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1881 }
1882
1883 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001884 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001885 {
1886 SCOPED_TRACE("new final crop applied");
1887 auto shot = screenshot();
1888 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1889 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1890 }
1891}
1892
Marissa Wall61c58622018-07-18 10:12:20 -07001893TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wucdd71a52017-11-02 08:30:27 -07001894 sp<SurfaceControl> layer;
1895 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001896 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001897
1898 // same as in SetCropWithNextResizeScaleToWindow
1899 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001900 .setFinalCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wucdd71a52017-11-02 08:30:27 -07001901 .setSize(layer, 16, 16)
1902 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1903 .setGeometryAppliesWithResize(layer)
1904 .apply();
1905 {
1906 SCOPED_TRACE("new final crop pending");
1907 auto shot = screenshot();
1908 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1909 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1910 }
1911
1912 // XXX final crop is never latched without other geometry change (b/69315677)
1913 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001914 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wucdd71a52017-11-02 08:30:27 -07001915 Transaction().setPosition(layer, 0, 0).apply();
1916 {
1917 SCOPED_TRACE("new final crop applied");
1918 auto shot = screenshot();
1919 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1920 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1921 }
1922}
1923
Marissa Wall61c58622018-07-18 10:12:20 -07001924TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1925 sp<SurfaceControl> layer;
1926 ASSERT_NO_FATAL_FAILURE(
1927 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1928
1929 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1930
1931 auto shot = screenshot();
1932 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1933 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1934}
1935
1936TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1937 sp<SurfaceControl> layer;
1938 ASSERT_NO_FATAL_FAILURE(
1939 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1940
1941 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1942
1943 {
1944 SCOPED_TRACE("set buffer 1");
1945 auto shot = screenshot();
1946 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1947 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1948 }
1949
1950 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1951
1952 {
1953 SCOPED_TRACE("set buffer 2");
1954 auto shot = screenshot();
1955 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1956 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1957 }
1958
1959 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1960
1961 {
1962 SCOPED_TRACE("set buffer 3");
1963 auto shot = screenshot();
1964 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1965 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1966 }
1967}
1968
1969TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1970 sp<SurfaceControl> layer1;
1971 ASSERT_NO_FATAL_FAILURE(
1972 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1973
1974 sp<SurfaceControl> layer2;
1975 ASSERT_NO_FATAL_FAILURE(
1976 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1977
1978 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1979
1980 {
1981 SCOPED_TRACE("set layer 1 buffer red");
1982 auto shot = screenshot();
1983 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1984 }
1985
1986 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1987
1988 {
1989 SCOPED_TRACE("set layer 2 buffer blue");
1990 auto shot = screenshot();
1991 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1992 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1993 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1994 }
1995
1996 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1997 {
1998 SCOPED_TRACE("set layer 1 buffer green");
1999 auto shot = screenshot();
2000 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2001 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2002 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2003 }
2004
2005 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2006
2007 {
2008 SCOPED_TRACE("set layer 2 buffer white");
2009 auto shot = screenshot();
2010 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2011 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2012 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2013 }
2014}
2015
2016TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
2017 sp<SurfaceControl> layer;
2018 ASSERT_NO_FATAL_FAILURE(
2019 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2020
2021 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2022 Color::BLUE, Color::WHITE));
2023
2024 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
2025
2026 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2027 Color::GREEN, true /* filtered */);
2028}
2029
2030TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
2031 sp<SurfaceControl> layer;
2032 ASSERT_NO_FATAL_FAILURE(
2033 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2034
2035 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2036 Color::BLUE, Color::WHITE));
2037
2038 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
2039
2040 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2041 Color::BLUE, true /* filtered */);
2042}
2043
2044TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
2045 sp<SurfaceControl> layer;
2046 ASSERT_NO_FATAL_FAILURE(
2047 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2048
2049 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2050 Color::BLUE, Color::WHITE));
2051
2052 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
2053
2054 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2055 Color::GREEN, true /* filtered */);
2056}
2057
2058TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2059 sp<SurfaceControl> layer;
2060 ASSERT_NO_FATAL_FAILURE(
2061 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2062
2063 Transaction().setTransformToDisplayInverse(layer, false).apply();
2064
2065 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2066
2067 Transaction().setTransformToDisplayInverse(layer, true).apply();
2068}
2069
2070TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
2071 sp<SurfaceControl> layer;
2072 ASSERT_NO_FATAL_FAILURE(
2073 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2074
2075 sp<GraphicBuffer> buffer =
2076 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2077 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2078 BufferUsage::COMPOSER_OVERLAY,
2079 "test");
2080 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2081
2082 sp<Fence> fence = new Fence(-1);
2083
2084 Transaction()
2085 .setBuffer(layer, buffer)
2086 .setAcquireFence(layer, fence)
2087 .setSize(layer, 32, 32)
2088 .apply();
2089
2090 auto shot = screenshot();
2091 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2092 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2093}
2094
2095TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2096 sp<SurfaceControl> layer;
2097 ASSERT_NO_FATAL_FAILURE(
2098 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2099
2100 sp<GraphicBuffer> buffer =
2101 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2102 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2103 BufferUsage::COMPOSER_OVERLAY,
2104 "test");
2105 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2106
2107 Transaction()
2108 .setBuffer(layer, buffer)
2109 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2110 .setSize(layer, 32, 32)
2111 .apply();
2112
2113 auto shot = screenshot();
2114 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2115 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2116}
2117
2118TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2119 sp<SurfaceControl> layer;
2120 ASSERT_NO_FATAL_FAILURE(
2121 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2122
2123 sp<GraphicBuffer> buffer =
2124 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2125 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2126 BufferUsage::COMPOSER_OVERLAY,
2127 "test");
2128 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2129
2130 HdrMetadata hdrMetadata;
2131 hdrMetadata.validTypes = 0;
2132 Transaction()
2133 .setBuffer(layer, buffer)
2134 .setHdrMetadata(layer, hdrMetadata)
2135 .setSize(layer, 32, 32)
2136 .apply();
2137
2138 auto shot = screenshot();
2139 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2140 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2141}
2142
2143TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2144 sp<SurfaceControl> layer;
2145 ASSERT_NO_FATAL_FAILURE(
2146 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2147
2148 sp<GraphicBuffer> buffer =
2149 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2150 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2151 BufferUsage::COMPOSER_OVERLAY,
2152 "test");
2153 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2154
2155 Region region;
2156 region.set(32, 32);
2157 Transaction()
2158 .setBuffer(layer, buffer)
2159 .setSurfaceDamageRegion(layer, region)
2160 .setSize(layer, 32, 32)
2161 .apply();
2162
2163 auto shot = screenshot();
2164 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2165 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2166}
2167
2168TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2169 sp<SurfaceControl> layer;
2170 ASSERT_NO_FATAL_FAILURE(
2171 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2172
2173 sp<GraphicBuffer> buffer =
2174 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2175 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2176 BufferUsage::COMPOSER_OVERLAY,
2177 "test");
2178 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2179
2180 Transaction()
2181 .setBuffer(layer, buffer)
2182 .setApi(layer, NATIVE_WINDOW_API_CPU)
2183 .setSize(layer, 32, 32)
2184 .apply();
2185
2186 auto shot = screenshot();
2187 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2188 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2189}
2190
2191TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2192 sp<SurfaceControl> layer;
2193 ASSERT_NO_FATAL_FAILURE(
2194 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2195
2196 // verify this doesn't cause a crash
2197 Transaction().setSidebandStream(layer, nullptr).apply();
2198}
2199
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002200class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002201protected:
2202 virtual void SetUp() {
2203 mComposerClient = new SurfaceComposerClient;
2204 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
2205
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002206 sp<IBinder> display(
2207 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002208 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002209 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002210
2211 ssize_t displayWidth = info.w;
2212 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002213
2214 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002215 mBGSurfaceControl =
2216 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
2217 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002218 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002219 ASSERT_TRUE(mBGSurfaceControl->isValid());
2220 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2221
2222 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002223 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
2224 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002225 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002226 ASSERT_TRUE(mFGSurfaceControl->isValid());
2227
2228 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2229
2230 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002231 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
2232 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002233 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002234 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2235
2236 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2237
Robert Carr4cdc58f2017-08-23 14:22:20 -07002238 asTransaction([&](Transaction& t) {
2239 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002240
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002241 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002242
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002243 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2244 .setPosition(mFGSurfaceControl, 64, 64)
2245 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002246
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002247 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2248 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2249 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002250 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002251 }
2252
2253 virtual void TearDown() {
2254 mComposerClient->dispose();
2255 mBGSurfaceControl = 0;
2256 mFGSurfaceControl = 0;
2257 mSyncSurfaceControl = 0;
2258 mComposerClient = 0;
2259 }
2260
2261 void waitForPostedBuffers() {
2262 // Since the sync surface is in synchronous mode (i.e. double buffered)
2263 // posting three buffers to it should ensure that at least two
2264 // SurfaceFlinger::handlePageFlip calls have been made, which should
2265 // guaranteed that a buffer posted to another Surface has been retired.
2266 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2267 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2268 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2269 }
2270
Robert Carr4cdc58f2017-08-23 14:22:20 -07002271 void asTransaction(const std::function<void(Transaction&)>& exec) {
2272 Transaction t;
2273 exec(t);
2274 t.apply(true);
2275 }
2276
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002277 sp<SurfaceComposerClient> mComposerClient;
2278 sp<SurfaceControl> mBGSurfaceControl;
2279 sp<SurfaceControl> mFGSurfaceControl;
2280
2281 // This surface is used to ensure that the buffers posted to
2282 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2283 sp<SurfaceControl> mSyncSurfaceControl;
2284};
2285
Robert Carr7f619b22017-11-06 12:56:35 -08002286TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
2287 sp<ScreenCapture> sc;
2288
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002289 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
2290 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002291 fillSurfaceRGBA8(relative, 10, 10, 10);
2292 waitForPostedBuffers();
2293
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002294 Transaction{}
2295 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002296 .setPosition(relative, 64, 64)
2297 .apply();
2298
2299 {
2300 // The relative should be on top of the FG control.
2301 ScreenCapture::captureScreen(&sc);
2302 sc->checkPixel(64, 64, 10, 10, 10);
2303 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002304 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002305
2306 {
2307 // Nothing should change at this point.
2308 ScreenCapture::captureScreen(&sc);
2309 sc->checkPixel(64, 64, 10, 10, 10);
2310 }
2311
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002312 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002313
2314 {
2315 // Ensure that the relative was actually hidden, rather than
2316 // being left in the detached but visible state.
2317 ScreenCapture::captureScreen(&sc);
2318 sc->expectFGColor(64, 64);
2319 }
2320}
2321
Robert Carr8d5227b2017-03-16 15:41:03 -07002322class GeometryLatchingTest : public LayerUpdateTest {
2323protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002324 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002325 SCOPED_TRACE(trace);
2326 ScreenCapture::captureScreen(&sc);
2327 // We find the leading edge of the FG surface.
2328 sc->expectFGColor(127, 127);
2329 sc->expectBGColor(128, 128);
2330 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002331
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002332 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002333
2334 void unlockFGBuffer() {
2335 sp<Surface> s = mFGSurfaceControl->getSurface();
2336 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2337 waitForPostedBuffers();
2338 }
2339
Robert Carr8d5227b2017-03-16 15:41:03 -07002340 void completeFGResize() {
2341 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2342 waitForPostedBuffers();
2343 }
2344 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002345 asTransaction([&](Transaction& t) {
2346 t.setSize(mFGSurfaceControl, 64, 64);
2347 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002348 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
2349 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(0, 0, -1, -1));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002350 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002351
2352 EXPECT_INITIAL_STATE("After restoring initial state");
2353 }
2354 sp<ScreenCapture> sc;
2355};
2356
Robert Carr8d5227b2017-03-16 15:41:03 -07002357class CropLatchingTest : public GeometryLatchingTest {
2358protected:
2359 void EXPECT_CROPPED_STATE(const char* trace) {
2360 SCOPED_TRACE(trace);
2361 ScreenCapture::captureScreen(&sc);
2362 // The edge should be moved back one pixel by our crop.
2363 sc->expectFGColor(126, 126);
2364 sc->expectBGColor(127, 127);
2365 sc->expectBGColor(128, 128);
2366 }
chaviw59f5c562017-06-28 16:39:06 -07002367
2368 void EXPECT_RESIZE_STATE(const char* trace) {
2369 SCOPED_TRACE(trace);
2370 ScreenCapture::captureScreen(&sc);
2371 // The FG is now resized too 128,128 at 64,64
2372 sc->expectFGColor(64, 64);
2373 sc->expectFGColor(191, 191);
2374 sc->expectBGColor(192, 192);
2375 }
Robert Carr8d5227b2017-03-16 15:41:03 -07002376};
2377
Robert Carr7bf247e2017-05-18 14:02:49 -07002378// In this test we ensure that setGeometryAppliesWithResize actually demands
2379// a buffer of the new size, and not just any size.
2380TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
2381 EXPECT_INITIAL_STATE("before anything");
2382 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002383 asTransaction([&](Transaction& t) {
2384 t.setSize(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002385 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(64, 64, 127, 127));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002386 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002387
2388 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
2389
2390 restoreInitialState();
2391
2392 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
2393 // initiating the resize.
2394 lockAndFillFGBuffer();
2395
Robert Carr4cdc58f2017-08-23 14:22:20 -07002396 asTransaction([&](Transaction& t) {
2397 t.setSize(mFGSurfaceControl, 128, 128);
2398 t.setGeometryAppliesWithResize(mFGSurfaceControl);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002399 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(64, 64, 127, 127));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002400 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002401
2402 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
2403
2404 // We now submit our old buffer, at the old size, and ensure it doesn't
2405 // trigger geometry latching.
2406 unlockFGBuffer();
2407
2408 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
2409
2410 completeFGResize();
2411
2412 EXPECT_CROPPED_STATE("after the resize finishes");
2413}
2414
Pablo Ceballos05289c22016-04-14 15:49:55 -07002415TEST_F(LayerUpdateTest, DeferredTransactionTest) {
2416 sp<ScreenCapture> sc;
2417 {
2418 SCOPED_TRACE("before anything");
2419 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002420 sc->expectBGColor(32, 32);
2421 sc->expectFGColor(96, 96);
2422 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002423 }
2424
2425 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002426 asTransaction([&](Transaction& t) {
2427 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002428 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2429 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002430 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002431
Robert Carr4cdc58f2017-08-23 14:22:20 -07002432 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002433 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002434 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2435 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002436 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002437
2438 {
2439 SCOPED_TRACE("before any trigger");
2440 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002441 sc->expectBGColor(32, 32);
2442 sc->expectFGColor(96, 96);
2443 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002444 }
2445
2446 // should trigger the first deferred transaction, but not the second one
2447 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2448 {
2449 SCOPED_TRACE("after first trigger");
2450 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002451 sc->expectBGColor(32, 32);
2452 sc->checkPixel(96, 96, 162, 63, 96);
2453 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002454 }
2455
2456 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002457 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002458
2459 // trigger the second deferred transaction
2460 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2461 {
2462 SCOPED_TRACE("after second trigger");
2463 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002464 sc->expectBGColor(32, 32);
2465 sc->expectBGColor(96, 96);
2466 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002467 }
2468}
2469
Robert Carre392b552017-09-19 12:16:05 -07002470TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
2471 sp<ScreenCapture> sc;
2472
2473 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002474 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
2475 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2476 sp<SurfaceControl> childBuffer =
2477 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
2478 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002479 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
2480
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002481 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002482
2483 {
2484 ScreenCapture::captureScreen(&sc);
2485 sc->expectChildColor(73, 73);
2486 sc->expectFGColor(74, 74);
2487 }
2488
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002489 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002490
2491 {
2492 ScreenCapture::captureScreen(&sc);
2493 sc->expectChildColor(73, 73);
2494 sc->expectChildColor(74, 74);
2495 }
2496}
2497
Robert Carr2c5f6d22017-09-26 12:30:35 -07002498TEST_F(LayerUpdateTest, MergingTransactions) {
2499 sp<ScreenCapture> sc;
2500 {
2501 SCOPED_TRACE("before move");
2502 ScreenCapture::captureScreen(&sc);
2503 sc->expectBGColor(0, 12);
2504 sc->expectFGColor(75, 75);
2505 sc->expectBGColor(145, 145);
2506 }
2507
2508 Transaction t1, t2;
2509 t1.setPosition(mFGSurfaceControl, 128, 128);
2510 t2.setPosition(mFGSurfaceControl, 0, 0);
2511 // We expect that the position update from t2 now
2512 // overwrites the position update from t1.
2513 t1.merge(std::move(t2));
2514 t1.apply();
2515
2516 {
2517 ScreenCapture::captureScreen(&sc);
2518 sc->expectFGColor(1, 1);
2519 }
2520}
2521
Robert Carr1f0a16a2016-10-24 16:27:39 -07002522class ChildLayerTest : public LayerUpdateTest {
2523protected:
2524 void SetUp() override {
2525 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002526 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2527 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002528 fillSurfaceRGBA8(mChild, 200, 200, 200);
2529
2530 {
2531 SCOPED_TRACE("before anything");
2532 ScreenCapture::captureScreen(&mCapture);
2533 mCapture->expectChildColor(64, 64);
2534 }
2535 }
2536 void TearDown() override {
2537 LayerUpdateTest::TearDown();
2538 mChild = 0;
2539 }
2540
2541 sp<SurfaceControl> mChild;
2542 sp<ScreenCapture> mCapture;
2543};
2544
2545TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002546 asTransaction([&](Transaction& t) {
2547 t.show(mChild);
2548 t.setPosition(mChild, 10, 10);
2549 t.setPosition(mFGSurfaceControl, 64, 64);
2550 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002551
2552 {
2553 ScreenCapture::captureScreen(&mCapture);
2554 // Top left of foreground must now be visible
2555 mCapture->expectFGColor(64, 64);
2556 // But 10 pixels in we should see the child surface
2557 mCapture->expectChildColor(74, 74);
2558 // And 10 more pixels we should be back to the foreground surface
2559 mCapture->expectFGColor(84, 84);
2560 }
2561
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002562 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002563
2564 {
2565 ScreenCapture::captureScreen(&mCapture);
2566 // Top left of foreground should now be at 0, 0
2567 mCapture->expectFGColor(0, 0);
2568 // But 10 pixels in we should see the child surface
2569 mCapture->expectChildColor(10, 10);
2570 // And 10 more pixels we should be back to the foreground surface
2571 mCapture->expectFGColor(20, 20);
2572 }
2573}
2574
Robert Carr41b08b52017-06-01 16:11:34 -07002575TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002576 asTransaction([&](Transaction& t) {
2577 t.show(mChild);
2578 t.setPosition(mChild, 0, 0);
2579 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002580 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002581 });
Robert Carr41b08b52017-06-01 16:11:34 -07002582
2583 {
2584 ScreenCapture::captureScreen(&mCapture);
2585 mCapture->expectChildColor(0, 0);
2586 mCapture->expectChildColor(4, 4);
2587 mCapture->expectBGColor(5, 5);
2588 }
2589}
2590
2591TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002592 asTransaction([&](Transaction& t) {
2593 t.show(mChild);
2594 t.setPosition(mChild, 0, 0);
2595 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002596 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002597 });
Robert Carr41b08b52017-06-01 16:11:34 -07002598
2599 {
2600 ScreenCapture::captureScreen(&mCapture);
2601 mCapture->expectChildColor(0, 0);
2602 mCapture->expectChildColor(4, 4);
2603 mCapture->expectBGColor(5, 5);
2604 }
2605}
2606
Robert Carr1f0a16a2016-10-24 16:27:39 -07002607TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002608 asTransaction([&](Transaction& t) {
2609 t.show(mChild);
2610 t.setPosition(mFGSurfaceControl, 0, 0);
2611 t.setPosition(mChild, 63, 63);
2612 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002613
2614 {
2615 ScreenCapture::captureScreen(&mCapture);
2616 mCapture->expectFGColor(0, 0);
2617 // Last pixel in foreground should now be the child.
2618 mCapture->expectChildColor(63, 63);
2619 // But the child should be constrained and the next pixel
2620 // must be the background
2621 mCapture->expectBGColor(64, 64);
2622 }
2623}
2624
2625TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002626 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002627
2628 // Find the boundary between the parent and child
2629 {
2630 ScreenCapture::captureScreen(&mCapture);
2631 mCapture->expectChildColor(9, 9);
2632 mCapture->expectFGColor(10, 10);
2633 }
2634
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002635 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002636
2637 // The boundary should be twice as far from the origin now.
2638 // The pixels from the last test should all be child now
2639 {
2640 ScreenCapture::captureScreen(&mCapture);
2641 mCapture->expectChildColor(9, 9);
2642 mCapture->expectChildColor(10, 10);
2643 mCapture->expectChildColor(19, 19);
2644 mCapture->expectFGColor(20, 20);
2645 }
2646}
Robert Carr9524cb32017-02-13 11:32:32 -08002647
Robert Carr6452f122017-03-21 10:41:29 -07002648TEST_F(ChildLayerTest, ChildLayerAlpha) {
2649 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2650 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2651 fillSurfaceRGBA8(mChild, 0, 254, 0);
2652 waitForPostedBuffers();
2653
Robert Carr4cdc58f2017-08-23 14:22:20 -07002654 asTransaction([&](Transaction& t) {
2655 t.show(mChild);
2656 t.setPosition(mChild, 0, 0);
2657 t.setPosition(mFGSurfaceControl, 0, 0);
2658 });
Robert Carr6452f122017-03-21 10:41:29 -07002659
2660 {
2661 ScreenCapture::captureScreen(&mCapture);
2662 // Unblended child color
2663 mCapture->checkPixel(0, 0, 0, 254, 0);
2664 }
2665
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002666 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002667
2668 {
2669 ScreenCapture::captureScreen(&mCapture);
2670 // Child and BG blended.
2671 mCapture->checkPixel(0, 0, 127, 127, 0);
2672 }
2673
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002674 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002675
2676 {
2677 ScreenCapture::captureScreen(&mCapture);
2678 // Child and BG blended.
2679 mCapture->checkPixel(0, 0, 95, 64, 95);
2680 }
2681}
2682
Robert Carr9524cb32017-02-13 11:32:32 -08002683TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002684 asTransaction([&](Transaction& t) {
2685 t.show(mChild);
2686 t.setPosition(mChild, 10, 10);
2687 t.setPosition(mFGSurfaceControl, 64, 64);
2688 });
Robert Carr9524cb32017-02-13 11:32:32 -08002689
2690 {
2691 ScreenCapture::captureScreen(&mCapture);
2692 // Top left of foreground must now be visible
2693 mCapture->expectFGColor(64, 64);
2694 // But 10 pixels in we should see the child surface
2695 mCapture->expectChildColor(74, 74);
2696 // And 10 more pixels we should be back to the foreground surface
2697 mCapture->expectFGColor(84, 84);
2698 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002699
2700 asTransaction([&](Transaction& t) {
2701 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2702 });
2703
Robert Carr9524cb32017-02-13 11:32:32 -08002704 {
2705 ScreenCapture::captureScreen(&mCapture);
2706 mCapture->expectFGColor(64, 64);
2707 // In reparenting we should have exposed the entire foreground surface.
2708 mCapture->expectFGColor(74, 74);
2709 // And the child layer should now begin at 10, 10 (since the BG
2710 // layer is at (0, 0)).
2711 mCapture->expectBGColor(9, 9);
2712 mCapture->expectChildColor(10, 10);
2713 }
2714}
2715
chaviw161410b02017-07-27 10:46:08 -07002716TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002717 asTransaction([&](Transaction& t) {
2718 t.show(mChild);
2719 t.setPosition(mChild, 10, 10);
2720 t.setPosition(mFGSurfaceControl, 64, 64);
2721 });
Robert Carr9524cb32017-02-13 11:32:32 -08002722
2723 {
2724 ScreenCapture::captureScreen(&mCapture);
2725 // Top left of foreground must now be visible
2726 mCapture->expectFGColor(64, 64);
2727 // But 10 pixels in we should see the child surface
2728 mCapture->expectChildColor(74, 74);
2729 // And 10 more pixels we should be back to the foreground surface
2730 mCapture->expectFGColor(84, 84);
2731 }
2732
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002733 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002734
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002735 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002736
chaviw161410b02017-07-27 10:46:08 -07002737 // Since the child has the same client as the parent, it will not get
2738 // detached and will be hidden.
2739 {
2740 ScreenCapture::captureScreen(&mCapture);
2741 mCapture->expectFGColor(64, 64);
2742 mCapture->expectFGColor(74, 74);
2743 mCapture->expectFGColor(84, 84);
2744 }
2745}
2746
2747TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2748 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002749 sp<SurfaceControl> mChildNewClient =
2750 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2751 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002752
Peiyong Lin566a3b42018-01-09 18:22:43 -08002753 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002754 ASSERT_TRUE(mChildNewClient->isValid());
2755
2756 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2757
Robert Carr4cdc58f2017-08-23 14:22:20 -07002758 asTransaction([&](Transaction& t) {
2759 t.hide(mChild);
2760 t.show(mChildNewClient);
2761 t.setPosition(mChildNewClient, 10, 10);
2762 t.setPosition(mFGSurfaceControl, 64, 64);
2763 });
chaviw161410b02017-07-27 10:46:08 -07002764
2765 {
2766 ScreenCapture::captureScreen(&mCapture);
2767 // Top left of foreground must now be visible
2768 mCapture->expectFGColor(64, 64);
2769 // But 10 pixels in we should see the child surface
2770 mCapture->expectChildColor(74, 74);
2771 // And 10 more pixels we should be back to the foreground surface
2772 mCapture->expectFGColor(84, 84);
2773 }
2774
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002775 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002776
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002777 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002778
Robert Carr9524cb32017-02-13 11:32:32 -08002779 // Nothing should have changed.
2780 {
2781 ScreenCapture::captureScreen(&mCapture);
2782 mCapture->expectFGColor(64, 64);
2783 mCapture->expectChildColor(74, 74);
2784 mCapture->expectFGColor(84, 84);
2785 }
2786}
2787
Robert Carr9b429f42017-04-17 14:56:57 -07002788TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002789 asTransaction([&](Transaction& t) {
2790 t.show(mChild);
2791 t.setPosition(mChild, 0, 0);
2792 t.setPosition(mFGSurfaceControl, 0, 0);
2793 });
Robert Carr9b429f42017-04-17 14:56:57 -07002794
2795 {
2796 ScreenCapture::captureScreen(&mCapture);
2797 // We've positioned the child in the top left.
2798 mCapture->expectChildColor(0, 0);
2799 // But it's only 10x10.
2800 mCapture->expectFGColor(10, 10);
2801 }
2802
Robert Carr4cdc58f2017-08-23 14:22:20 -07002803 asTransaction([&](Transaction& t) {
2804 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2805 // We cause scaling by 2.
2806 t.setSize(mFGSurfaceControl, 128, 128);
2807 });
Robert Carr9b429f42017-04-17 14:56:57 -07002808
2809 {
2810 ScreenCapture::captureScreen(&mCapture);
2811 // We've positioned the child in the top left.
2812 mCapture->expectChildColor(0, 0);
2813 mCapture->expectChildColor(10, 10);
2814 mCapture->expectChildColor(19, 19);
2815 // And now it should be scaled all the way to 20x20
2816 mCapture->expectFGColor(20, 20);
2817 }
2818}
2819
Robert Carr1725eee2017-04-26 18:32:15 -07002820// Regression test for b/37673612
2821TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002822 asTransaction([&](Transaction& t) {
2823 t.show(mChild);
2824 t.setPosition(mChild, 0, 0);
2825 t.setPosition(mFGSurfaceControl, 0, 0);
2826 });
Robert Carr1725eee2017-04-26 18:32:15 -07002827
2828 {
2829 ScreenCapture::captureScreen(&mCapture);
2830 // We've positioned the child in the top left.
2831 mCapture->expectChildColor(0, 0);
2832 // But it's only 10x10.
2833 mCapture->expectFGColor(10, 10);
2834 }
Robert Carr1725eee2017-04-26 18:32:15 -07002835 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2836 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002837 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002838 sp<Surface> s = mFGSurfaceControl->getSurface();
2839 auto anw = static_cast<ANativeWindow*>(s.get());
2840 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2841 native_window_set_buffers_dimensions(anw, 64, 128);
2842 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2843 waitForPostedBuffers();
2844
2845 {
2846 // The child should still be in the same place and not have any strange scaling as in
2847 // b/37673612.
2848 ScreenCapture::captureScreen(&mCapture);
2849 mCapture->expectChildColor(0, 0);
2850 mCapture->expectFGColor(10, 10);
2851 }
2852}
2853
Dan Stoza412903f2017-04-27 13:42:17 -07002854TEST_F(ChildLayerTest, Bug36858924) {
2855 // Destroy the child layer
2856 mChild.clear();
2857
2858 // Now recreate it as hidden
2859 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2860 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2861 mFGSurfaceControl.get());
2862
2863 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002864 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002865 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
2866 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002867 t.show(mChild);
2868 });
Dan Stoza412903f2017-04-27 13:42:17 -07002869
2870 // Render the foreground surface a few times
2871 //
2872 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2873 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2874 // never acquire/release the first buffer
2875 ALOGI("Filling 1");
2876 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2877 ALOGI("Filling 2");
2878 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2879 ALOGI("Filling 3");
2880 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2881 ALOGI("Filling 4");
2882 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2883}
2884
chaviwf1961f72017-09-18 16:41:07 -07002885TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002886 asTransaction([&](Transaction& t) {
2887 t.show(mChild);
2888 t.setPosition(mChild, 10, 10);
2889 t.setPosition(mFGSurfaceControl, 64, 64);
2890 });
chaviw06178942017-07-27 10:25:59 -07002891
2892 {
2893 ScreenCapture::captureScreen(&mCapture);
2894 // Top left of foreground must now be visible
2895 mCapture->expectFGColor(64, 64);
2896 // But 10 pixels in we should see the child surface
2897 mCapture->expectChildColor(74, 74);
2898 // And 10 more pixels we should be back to the foreground surface
2899 mCapture->expectFGColor(84, 84);
2900 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002901
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002902 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002903
chaviw06178942017-07-27 10:25:59 -07002904 {
2905 ScreenCapture::captureScreen(&mCapture);
2906 mCapture->expectFGColor(64, 64);
2907 // In reparenting we should have exposed the entire foreground surface.
2908 mCapture->expectFGColor(74, 74);
2909 // And the child layer should now begin at 10, 10 (since the BG
2910 // layer is at (0, 0)).
2911 mCapture->expectBGColor(9, 9);
2912 mCapture->expectChildColor(10, 10);
2913 }
2914}
2915
chaviwf1961f72017-09-18 16:41:07 -07002916TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002917 asTransaction([&](Transaction& t) {
2918 t.show(mChild);
2919 t.setPosition(mChild, 10, 10);
2920 t.setPosition(mFGSurfaceControl, 64, 64);
2921 });
chaviwf1961f72017-09-18 16:41:07 -07002922
2923 {
2924 ScreenCapture::captureScreen(&mCapture);
2925 // Top left of foreground must now be visible
2926 mCapture->expectFGColor(64, 64);
2927 // But 10 pixels in we should see the child surface
2928 mCapture->expectChildColor(74, 74);
2929 // And 10 more pixels we should be back to the foreground surface
2930 mCapture->expectFGColor(84, 84);
2931 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002932 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002933 {
2934 ScreenCapture::captureScreen(&mCapture);
2935 // Nothing should have changed.
2936 mCapture->expectFGColor(64, 64);
2937 mCapture->expectChildColor(74, 74);
2938 mCapture->expectFGColor(84, 84);
2939 }
2940}
2941
2942TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002943 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2944 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002945 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002946 ASSERT_TRUE(newSurface->isValid());
2947
2948 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002949 asTransaction([&](Transaction& t) {
2950 t.hide(mChild);
2951 t.show(newSurface);
2952 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002953 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002954 t.setPosition(mFGSurfaceControl, 64, 64);
2955 });
chaviwf1961f72017-09-18 16:41:07 -07002956
2957 {
2958 ScreenCapture::captureScreen(&mCapture);
2959 // Top left of foreground must now be visible
2960 mCapture->expectFGColor(64, 64);
2961 // At 10, 10 we should see the new surface
2962 mCapture->checkPixel(10, 10, 63, 195, 63);
2963 }
2964
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002965 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002966
2967 {
2968 ScreenCapture::captureScreen(&mCapture);
2969 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2970 // mFGSurface, putting it at 74, 74.
2971 mCapture->expectFGColor(64, 64);
2972 mCapture->checkPixel(74, 74, 63, 195, 63);
2973 mCapture->expectFGColor(84, 84);
2974 }
2975}
2976
chaviwc9674332017-08-28 12:32:18 -07002977TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002978 sp<SurfaceControl> grandchild =
2979 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2980 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002981 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2982
2983 {
2984 ScreenCapture::captureScreen(&mCapture);
2985 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2986 // which begins at 64, 64
2987 mCapture->checkPixel(64, 64, 50, 50, 50);
2988 }
2989}
2990
Robert Carr503c7042017-09-27 15:06:08 -07002991TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002992 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2993 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002994 fillSurfaceRGBA8(relative, 255, 255, 255);
2995
2996 Transaction t;
2997 t.setLayer(relative, INT32_MAX)
2998 .setRelativeLayer(mChild, relative->getHandle(), 1)
2999 .setPosition(mFGSurfaceControl, 0, 0)
3000 .apply(true);
3001
3002 // We expect that the child should have been elevated above our
3003 // INT_MAX layer even though it's not a child of it.
3004 {
3005 ScreenCapture::captureScreen(&mCapture);
3006 mCapture->expectChildColor(0, 0);
3007 mCapture->expectChildColor(9, 9);
3008 mCapture->checkPixel(10, 10, 255, 255, 255);
3009 }
3010}
3011
chaviwa76b2712017-09-20 12:02:26 -07003012class ScreenCaptureTest : public LayerUpdateTest {
3013protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003014 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07003015};
3016
3017TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
3018 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003019 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003020 mCapture->expectBGColor(0, 0);
3021 // Doesn't capture FG layer which is at 64, 64
3022 mCapture->expectBGColor(64, 64);
3023}
3024
3025TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
3026 auto fgHandle = mFGSurfaceControl->getHandle();
3027
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003028 sp<SurfaceControl> child =
3029 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3030 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003031 fillSurfaceRGBA8(child, 200, 200, 200);
3032
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003033 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003034
3035 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003036 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003037 mCapture->expectFGColor(10, 10);
3038 mCapture->expectChildColor(0, 0);
3039}
3040
Robert Carr578038f2018-03-09 12:25:24 -08003041TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
3042 auto fgHandle = mFGSurfaceControl->getHandle();
3043
3044 sp<SurfaceControl> child =
3045 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3046 0, mFGSurfaceControl.get());
3047 fillSurfaceRGBA8(child, 200, 200, 200);
3048
3049 SurfaceComposerClient::Transaction().show(child).apply(true);
3050
3051 // Captures mFGSurfaceControl's child
3052 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3053 mCapture->checkPixel(10, 10, 0, 0, 0);
3054 mCapture->expectChildColor(0, 0);
3055}
3056
chaviw50da5042018-04-09 13:49:37 -07003057TEST_F(ScreenCaptureTest, CaptureTransparent) {
3058 sp<SurfaceControl> child =
3059 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3060 0, mFGSurfaceControl.get());
3061
3062 fillSurfaceRGBA8(child, 200, 200, 200);
3063
3064 SurfaceComposerClient::Transaction().show(child).apply(true);
3065
3066 auto childHandle = child->getHandle();
3067
3068 // Captures child
3069 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
3070 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
3071 // Area outside of child's bounds is transparent.
3072 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
3073}
3074
chaviw4b129c22018-04-09 16:19:43 -07003075TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
3076 auto fgHandle = mFGSurfaceControl->getHandle();
3077
3078 sp<SurfaceControl> child =
3079 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3080 0, mFGSurfaceControl.get());
3081 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 10,
3082 10, PIXEL_FORMAT_RGBA_8888, 0);
3083 fillSurfaceRGBA8(child, 200, 200, 200);
3084 fillSurfaceRGBA8(relative, 100, 100, 100);
3085
3086 SurfaceComposerClient::Transaction()
3087 .show(child)
3088 // Set relative layer above fg layer so should be shown above when computing all layers.
3089 .setRelativeLayer(relative, fgHandle, 1)
3090 .show(relative)
3091 .apply(true);
3092
3093 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
3094 ScreenCapture::captureLayers(&mCapture, fgHandle);
3095 mCapture->expectFGColor(10, 10);
3096 mCapture->expectChildColor(0, 0);
3097}
3098
3099TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
3100 auto fgHandle = mFGSurfaceControl->getHandle();
3101
3102 sp<SurfaceControl> child =
3103 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3104 0, mFGSurfaceControl.get());
3105 sp<SurfaceControl> relative =
3106 mComposerClient->createSurface(String8("Relative surface"), 10, 10,
3107 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3108 fillSurfaceRGBA8(child, 200, 200, 200);
3109 fillSurfaceRGBA8(relative, 100, 100, 100);
3110
3111 SurfaceComposerClient::Transaction()
3112 .show(child)
3113 // Set relative layer below fg layer but relative to child layer so it should be shown
3114 // above child layer.
3115 .setLayer(relative, -1)
3116 .setRelativeLayer(relative, child->getHandle(), 1)
3117 .show(relative)
3118 .apply(true);
3119
3120 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
3121 // relative value should be taken into account, placing it above child layer.
3122 ScreenCapture::captureLayers(&mCapture, fgHandle);
3123 mCapture->expectFGColor(10, 10);
3124 // Relative layer is showing on top of child layer
3125 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
3126}
Robert Carr578038f2018-03-09 12:25:24 -08003127
3128// In the following tests we verify successful skipping of a parent layer,
3129// so we use the same verification logic and only change how we mutate
3130// the parent layer to verify that various properties are ignored.
3131class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
3132public:
3133 void SetUp() override {
3134 LayerUpdateTest::SetUp();
3135
3136 mChild =
3137 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3138 0, mFGSurfaceControl.get());
3139 fillSurfaceRGBA8(mChild, 200, 200, 200);
3140
3141 SurfaceComposerClient::Transaction().show(mChild).apply(true);
3142 }
3143
3144 void verify() {
3145 auto fgHandle = mFGSurfaceControl->getHandle();
3146 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3147 mCapture->checkPixel(10, 10, 0, 0, 0);
3148 mCapture->expectChildColor(0, 0);
3149 }
3150
3151 std::unique_ptr<ScreenCapture> mCapture;
3152 sp<SurfaceControl> mChild;
3153};
3154
3155TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
3156
3157 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3158
3159 // Even though the parent is hidden we should still capture the child.
3160 verify();
3161}
3162
3163TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003164 SurfaceComposerClient::Transaction()
3165 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
3166 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08003167
3168 // Even though the parent is cropped out we should still capture the child.
3169 verify();
3170}
3171
3172TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
3173
3174 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
3175
3176 // We should not inherit the parent scaling.
3177 verify();
3178}
3179
Robert Carr15eae092018-03-23 13:43:53 -07003180TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
3181 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3182
3183 // Even though the parent is hidden we should still capture the child.
3184 verify();
3185
3186 // Verify everything was properly hidden when rendering the full-screen.
3187 screenshot()->expectBGColor(0,0);
3188}
3189
3190
chaviwa76b2712017-09-20 12:02:26 -07003191TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
3192 auto fgHandle = mFGSurfaceControl->getHandle();
3193
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003194 sp<SurfaceControl> child =
3195 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3196 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003197 fillSurfaceRGBA8(child, 200, 200, 200);
3198
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003199 sp<SurfaceControl> grandchild =
3200 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
3201 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003202
3203 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3204 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003205 .show(child)
3206 .setPosition(grandchild, 5, 5)
3207 .show(grandchild)
3208 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003209
3210 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003211 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003212 mCapture->expectFGColor(10, 10);
3213 mCapture->expectChildColor(0, 0);
3214 mCapture->checkPixel(5, 5, 50, 50, 50);
3215}
3216
3217TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003218 sp<SurfaceControl> child =
3219 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3220 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003221 fillSurfaceRGBA8(child, 200, 200, 200);
3222 auto childHandle = child->getHandle();
3223
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003224 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003225
3226 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003227 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07003228 mCapture->expectChildColor(0, 0);
3229 mCapture->expectChildColor(9, 9);
3230}
3231
3232TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003233 sp<SurfaceControl> child =
3234 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
3235 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003236 fillSurfaceRGBA8(child, 200, 200, 200);
3237 auto childHandle = child->getHandle();
3238
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003239 sp<SurfaceControl> grandchild =
3240 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
3241 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003242 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3243
3244 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003245 .show(child)
3246 .setPosition(grandchild, 5, 5)
3247 .show(grandchild)
3248 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003249
3250 auto grandchildHandle = grandchild->getHandle();
3251
3252 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003253 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07003254 mCapture->checkPixel(0, 0, 50, 50, 50);
3255 mCapture->checkPixel(4, 4, 50, 50, 50);
3256}
3257
chaviw7206d492017-11-10 16:16:12 -08003258TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003259 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
3260 PIXEL_FORMAT_RGBA_8888, 0);
3261 sp<SurfaceControl> blueLayer =
3262 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
3263 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003264
Marissa Wall61c58622018-07-18 10:12:20 -07003265 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3266 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003267
3268 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003269 .setLayer(redLayer, INT32_MAX - 1)
3270 .show(redLayer)
3271 .show(blueLayer)
3272 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003273
3274 auto redLayerHandle = redLayer->getHandle();
3275
3276 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003277 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3278 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3279 // red area below the blue area
3280 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3281 // red area to the right of the blue area
3282 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003283
3284 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003285 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08003286 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
3287 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003288 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08003289 mCapture->checkPixel(30, 30, 0, 0, 0);
3290}
3291
3292TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003293 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
3294 PIXEL_FORMAT_RGBA_8888, 0);
3295 sp<SurfaceControl> blueLayer =
3296 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
3297 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003298
Marissa Wall61c58622018-07-18 10:12:20 -07003299 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3300 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003301
3302 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003303 .setLayer(redLayer, INT32_MAX - 1)
3304 .show(redLayer)
3305 .show(blueLayer)
3306 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003307
3308 auto redLayerHandle = redLayer->getHandle();
3309
3310 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003311 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3312 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3313 // red area below the blue area
3314 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3315 // red area to the right of the blue area
3316 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003317
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003318 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08003319 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003320 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
3321 // red area below the blue area
3322 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
3323 // red area to the right of the blue area
3324 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003325 mCapture->checkPixel(30, 30, 0, 0, 0);
3326}
3327
3328TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003329 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
3330 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08003331
Marissa Wall61c58622018-07-18 10:12:20 -07003332 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08003333
3334 auto redLayerHandle = redLayer->getHandle();
3335 mComposerClient->destroySurface(redLayerHandle);
3336 SurfaceComposerClient::Transaction().apply(true);
3337
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003338 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08003339
3340 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003341 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
3342 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08003343}
3344
chaviw8e3fe5d2018-02-22 10:55:42 -08003345
3346class DereferenceSurfaceControlTest : public LayerTransactionTest {
3347protected:
3348 void SetUp() override {
3349 LayerTransactionTest::SetUp();
3350 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003351 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003352 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003353 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003354 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
3355 {
3356 SCOPED_TRACE("before anything");
3357 auto shot = screenshot();
3358 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3359 }
3360 }
3361 void TearDown() override {
3362 LayerTransactionTest::TearDown();
3363 bgLayer = 0;
3364 fgLayer = 0;
3365 }
3366
3367 sp<SurfaceControl> bgLayer;
3368 sp<SurfaceControl> fgLayer;
3369};
3370
3371TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
3372 fgLayer = nullptr;
3373 {
3374 SCOPED_TRACE("after setting null");
3375 auto shot = screenshot();
3376 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
3377 }
3378}
3379
3380TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
3381 auto transaction = Transaction().show(fgLayer);
3382 fgLayer = nullptr;
3383 {
3384 SCOPED_TRACE("after setting null");
3385 auto shot = screenshot();
3386 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3387 }
3388}
3389
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003390} // namespace android