blob: 3dedf24a171e519a4ca4aeea96dbcc2df33b7a86 [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>
Marissa Wallfda30bb2018-10-12 11:34:28 -070018#include <chrono>
19#include <cinttypes>
Chia-I Wu718daf82017-10-20 11:57:17 -070020#include <functional>
21#include <limits>
22#include <ostream>
Marissa Wallfda30bb2018-10-12 11:34:28 -070023#include <thread>
Chia-I Wu718daf82017-10-20 11:57:17 -070024
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070025#include <gtest/gtest.h>
26
Michael Lentine5a16a622015-05-21 13:48:24 -070027#include <android/native_window.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070030#include <gui/LayerState.h>
31
Mathias Agopian90ac7992012-02-25 18:48:35 -080032#include <gui/Surface.h>
33#include <gui/SurfaceComposerClient.h>
34#include <private/gui/ComposerService.h>
35
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070036#include <ui/ColorSpace.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070038#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070039#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070041#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070042#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070043
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070044namespace android {
45
Chia-I Wu718daf82017-10-20 11:57:17 -070046namespace {
47
48struct Color {
49 uint8_t r;
50 uint8_t g;
51 uint8_t b;
52 uint8_t a;
53
54 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070055 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070056 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070057 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070058 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070059 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070060};
61
62const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070063const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070064const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070065const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070066const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070067const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070068
Marissa Wall61c58622018-07-18 10:12:20 -070069using android::hardware::graphics::common::V1_1::BufferUsage;
Marissa Wallfda30bb2018-10-12 11:34:28 -070070using namespace std::chrono_literals;
Marissa Wall61c58622018-07-18 10:12:20 -070071
Chia-I Wu718daf82017-10-20 11:57:17 -070072std::ostream& operator<<(std::ostream& os, const Color& color) {
73 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
74 return os;
75}
76
77// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070078void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
79 const Color& color) {
80 Rect r(0, 0, buffer.width, buffer.height);
81 if (!r.intersect(rect, &r)) {
82 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070083 }
84
Marissa Wall61c58622018-07-18 10:12:20 -070085 int32_t width = r.right - r.left;
86 int32_t height = r.bottom - r.top;
87
88 for (int32_t row = 0; row < height; row++) {
89 uint8_t* dst =
90 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
91 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070092 dst[0] = color.r;
93 dst[1] = color.g;
94 dst[2] = color.b;
95 dst[3] = color.a;
96 dst += 4;
97 }
98 }
99}
100
Marissa Wall61c58622018-07-18 10:12:20 -0700101// Fill a region with the specified color.
102void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
103 Rect r(0, 0, buffer->width, buffer->height);
104 if (!r.intersect(rect, &r)) {
105 return;
106 }
107
108 int32_t width = r.right - r.left;
109 int32_t height = r.bottom - r.top;
110
111 uint8_t* pixels;
112 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
113 reinterpret_cast<void**>(&pixels));
114
115 for (int32_t row = 0; row < height; row++) {
116 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
117 for (int32_t column = 0; column < width; column++) {
118 dst[0] = color.r;
119 dst[1] = color.g;
120 dst[2] = color.b;
121 dst[3] = color.a;
122 dst += 4;
123 }
124 }
125 buffer->unlock();
126}
127
Chia-I Wu718daf82017-10-20 11:57:17 -0700128// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000129void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700130 const Color& color, uint8_t tolerance) {
131 int32_t x = rect.left;
132 int32_t y = rect.top;
133 int32_t width = rect.right - rect.left;
134 int32_t height = rect.bottom - rect.top;
135
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000136 int32_t bufferWidth = int32_t(outBuffer->getWidth());
137 int32_t bufferHeight = int32_t(outBuffer->getHeight());
138 if (x + width > bufferWidth) {
139 x = std::min(x, bufferWidth);
140 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700141 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000142 if (y + height > bufferHeight) {
143 y = std::min(y, bufferHeight);
144 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700145 }
146
147 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
148 uint8_t tmp = a >= b ? a - b : b - a;
149 return tmp <= tolerance;
150 };
151 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000152 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700153 for (int32_t i = 0; i < width; i++) {
154 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
155 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
156 << "pixel @ (" << x + i << ", " << y + j << "): "
157 << "expected (" << color << "), "
158 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
159 src += 4;
160 }
161 }
162}
163
164} // anonymous namespace
165
Robert Carr4cdc58f2017-08-23 14:22:20 -0700166using Transaction = SurfaceComposerClient::Transaction;
167
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700168// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700169static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
170 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800171 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700172 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800173 ASSERT_TRUE(s != nullptr);
174 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800175 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700176 for (int y = 0; y < outBuffer.height; y++) {
177 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700178 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700179 pixel[0] = r;
180 pixel[1] = g;
181 pixel[2] = b;
182 pixel[3] = 255;
183 }
184 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700185 if (unlock) {
186 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
187 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700188}
189
190// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
191// individual pixel values for testing purposes.
192class ScreenCapture : public RefBase {
193public:
chaviw0e3479f2018-09-10 16:49:30 -0700194 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700195 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700196 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700197 SurfaceComposerClient::Transaction().apply(true);
198
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000199 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700200 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700201 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
202 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000203 }
204
205 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
206 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
207 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
208 SurfaceComposerClient::Transaction().apply(true);
209
210 sp<GraphicBuffer> outBuffer;
211 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
212 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700213 }
214
Robert Carr578038f2018-03-09 12:25:24 -0800215 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
216 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
217 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
218 SurfaceComposerClient::Transaction().apply(true);
219
220 sp<GraphicBuffer> outBuffer;
221 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
222 *sc = std::make_unique<ScreenCapture>(outBuffer);
223 }
224
Chia-I Wu718daf82017-10-20 11:57:17 -0700225 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000226 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
227 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700228 }
229
230 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000231 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700232 const bool leftBorder = rect.left > 0;
233 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000234 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
235 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700236
237 if (topBorder) {
238 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
239 if (leftBorder) {
240 top.left -= 1;
241 }
242 if (rightBorder) {
243 top.right += 1;
244 }
245 expectColor(top, color, tolerance);
246 }
247 if (leftBorder) {
248 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
249 expectColor(left, color, tolerance);
250 }
251 if (rightBorder) {
252 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
253 expectColor(right, color, tolerance);
254 }
255 if (bottomBorder) {
256 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
257 if (leftBorder) {
258 bottom.left -= 1;
259 }
260 if (rightBorder) {
261 bottom.right += 1;
262 }
263 expectColor(bottom, color, tolerance);
264 }
265 }
266
Chia-I Wu93853fe2017-11-02 08:30:27 -0700267 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
268 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
269 uint8_t tolerance = 0) {
270 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
271
272 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
273 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
274 // avoid checking borders due to unspecified filtering behavior
275 const int32_t offsetX = filtered ? 2 : 0;
276 const int32_t offsetY = filtered ? 2 : 0;
277 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
278 tolerance);
279 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
280 tolerance);
281 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
282 tolerance);
283 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
284 bottomRight, tolerance);
285 }
286
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700287 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000288 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
289 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700290 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
291 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700292 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
293 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700294 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700295 }
296 }
297
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700298 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700299
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700300 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700301
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700302 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700303
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000304 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
305 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700306 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700307
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000308 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700309
310private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000311 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800312 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700313};
314
Chia-I Wu718daf82017-10-20 11:57:17 -0700315class LayerTransactionTest : public ::testing::Test {
316protected:
317 void SetUp() override {
318 mClient = new SurfaceComposerClient;
319 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
320
321 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700322
323 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
324 sp<IBinder> binder = sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
Ady Abraham37965d42018-11-01 13:43:32 -0700325 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
Chia-I Wu718daf82017-10-20 11:57:17 -0700326 }
327
chaviw0e3479f2018-09-10 16:49:30 -0700328 virtual void TearDown() {
329 mBlackBgSurface = 0;
330 mClient->dispose();
331 mClient = 0;
332 }
333
Marissa Wallfda30bb2018-10-12 11:34:28 -0700334 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
335 const char* name, uint32_t width, uint32_t height,
Marissa Wall61c58622018-07-18 10:12:20 -0700336 uint32_t flags = 0) {
Vishnu Nair88a11f22018-11-28 18:30:57 -0800337 auto layer = createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags);
Chia-I Wu718daf82017-10-20 11:57:17 -0700338
Vishnu Nair60356342018-11-13 13:00:45 -0800339 Transaction t;
340 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -0800341
342 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700343 if (error != NO_ERROR) {
344 ADD_FAILURE() << "failed to initialize SurfaceControl";
345 layer.clear();
346 }
347
348 return layer;
349 }
350
Vishnu Nair88a11f22018-11-28 18:30:57 -0800351 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
352 const char* name, uint32_t width, uint32_t height,
353 PixelFormat format, uint32_t flags,
354 SurfaceControl* parent = nullptr) {
355 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
356 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
357 return layer;
358 }
359
Marissa Wallfda30bb2018-10-12 11:34:28 -0700360 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
361 uint32_t flags = 0) {
362 return createLayer(mClient, name, width, height, flags);
363 }
364
Marissa Wall61c58622018-07-18 10:12:20 -0700365 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700366 // wait for previous transactions (such as setSize) to complete
367 Transaction().apply(true);
368
369 ANativeWindow_Buffer buffer = {};
370 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
371
372 return buffer;
373 }
374
Marissa Wall61c58622018-07-18 10:12:20 -0700375 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700376 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
377
378 // wait for the newly posted buffer to be latched
379 waitForLayerBuffers();
380 }
381
Marissa Wall61c58622018-07-18 10:12:20 -0700382 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
383 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700384 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700385 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
386 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
387 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700388 }
389
Marissa Wall61c58622018-07-18 10:12:20 -0700390 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
391 int32_t bufferWidth, int32_t bufferHeight) {
392 sp<GraphicBuffer> buffer =
393 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
394 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
395 BufferUsage::COMPOSER_OVERLAY,
396 "test");
397 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
Marissa Wall861616d2018-10-22 12:52:23 -0700398 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -0700399 }
400
401 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
402 int32_t bufferWidth, int32_t bufferHeight) {
403 switch (mLayerType) {
404 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
405 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
406 break;
407 case ISurfaceComposerClient::eFXSurfaceBufferState:
408 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
409 break;
410 default:
411 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
412 }
413 }
414
415 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
416 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700417 const Color& topRight, const Color& bottomLeft,
418 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700419 switch (mLayerType) {
420 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
421 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
422 bottomLeft, bottomRight);
423 break;
424 case ISurfaceComposerClient::eFXSurfaceBufferState:
425 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
426 bottomLeft, bottomRight);
427 break;
428 default:
429 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
430 }
431 }
432
433 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
434 int32_t bufferHeight, const Color& topLeft,
435 const Color& topRight, const Color& bottomLeft,
436 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700437 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700438 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
439 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700440
Marissa Wall61c58622018-07-18 10:12:20 -0700441 const int32_t halfW = bufferWidth / 2;
442 const int32_t halfH = bufferHeight / 2;
443 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
444 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
445 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
446 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
447 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700448
Marissa Wall61c58622018-07-18 10:12:20 -0700449 postBufferQueueLayerBuffer(layer);
450 }
451
452 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
453 int32_t bufferHeight, const Color& topLeft,
454 const Color& topRight, const Color& bottomLeft,
455 const Color& bottomRight) {
456 sp<GraphicBuffer> buffer =
457 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
458 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
459 BufferUsage::COMPOSER_OVERLAY,
460 "test");
461
462 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
463
464 const int32_t halfW = bufferWidth / 2;
465 const int32_t halfH = bufferHeight / 2;
466 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
467 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
468 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
469 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
470
471 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700472 }
473
chaviw0e3479f2018-09-10 16:49:30 -0700474 std::unique_ptr<ScreenCapture> screenshot() {
475 std::unique_ptr<ScreenCapture> screenshot;
476 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700477 return screenshot;
478 }
479
480 sp<SurfaceComposerClient> mClient;
481
482 sp<IBinder> mDisplay;
483 uint32_t mDisplayWidth;
484 uint32_t mDisplayHeight;
485 uint32_t mDisplayLayerStack;
Marissa Wall861616d2018-10-22 12:52:23 -0700486 Rect mDisplayRect = Rect::INVALID_RECT;
Chia-I Wu718daf82017-10-20 11:57:17 -0700487
488 // leave room for ~256 layers
489 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
490
Marissa Wall861616d2018-10-22 12:52:23 -0700491 void setRelativeZBasicHelper(uint32_t layerType);
492 void setRelativeZGroupHelper(uint32_t layerType);
493 void setAlphaBasicHelper(uint32_t layerType);
Marissa Wall61c58622018-07-18 10:12:20 -0700494
chaviw0e3479f2018-09-10 16:49:30 -0700495 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700496 bool mColorManagementUsed;
497
Chia-I Wu718daf82017-10-20 11:57:17 -0700498private:
499 void SetUpDisplay() {
500 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
501 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
502
503 // get display width/height
504 DisplayInfo info;
505 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
506 mDisplayWidth = info.w;
507 mDisplayHeight = info.h;
Marissa Wall861616d2018-10-22 12:52:23 -0700508 mDisplayRect =
509 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
Chia-I Wu718daf82017-10-20 11:57:17 -0700510
511 // After a new buffer is queued, SurfaceFlinger is notified and will
512 // latch the new buffer on next vsync. Let's heuristically wait for 3
513 // vsyncs.
514 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
515
516 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700517
Vishnu Nair88a11f22018-11-28 18:30:57 -0800518 mBlackBgSurface =
519 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
520 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700521
Chia-I Wu718daf82017-10-20 11:57:17 -0700522 // set layer stack (b/68888219)
523 Transaction t;
524 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800525 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700526 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
527 t.setColor(mBlackBgSurface, half3{0, 0, 0});
528 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700529 t.apply();
530 }
531
chaviw0e3479f2018-09-10 16:49:30 -0700532 void waitForLayerBuffers() {
533 // Request an empty transaction to get applied synchronously to ensure the buffer is
534 // latched.
535 Transaction().apply(true);
536 usleep(mBufferPostDelay);
537 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700538
539 int32_t mBufferPostDelay;
540};
541
Marissa Wall61c58622018-07-18 10:12:20 -0700542class LayerTypeTransactionTest : public LayerTransactionTest,
543 public ::testing::WithParamInterface<uint32_t> {
544public:
545 LayerTypeTransactionTest() { mLayerType = GetParam(); }
546
547 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
548 uint32_t flags = 0) override {
549 // if the flags already have a layer type specified, return an error
550 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
551 return nullptr;
552 }
553 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
554 }
555
556 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
557 int32_t bufferHeight) {
558 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
559 bufferWidth, bufferHeight));
560 }
561
562 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
563 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
564 const Color& bottomLeft, const Color& bottomRight) {
565 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
566 bufferWidth, bufferHeight,
567 topLeft, topRight,
568 bottomLeft, bottomRight));
569 }
570
571protected:
572 uint32_t mLayerType;
573};
574
575INSTANTIATE_TEST_CASE_P(
576 LayerTypeTransactionTests, LayerTypeTransactionTest,
577 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
578 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
579
Marissa Wall861616d2018-10-22 12:52:23 -0700580TEST_F(LayerTransactionTest, SetPositionBasic_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700581 sp<SurfaceControl> layer;
582 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700583 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700584
585 {
586 SCOPED_TRACE("default position");
Marissa Wall861616d2018-10-22 12:52:23 -0700587 const Rect rect(0, 0, 32, 32);
Chia-I Wu718daf82017-10-20 11:57:17 -0700588 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700589 shot->expectColor(rect, Color::RED);
590 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700591 }
592
593 Transaction().setPosition(layer, 5, 10).apply();
594 {
595 SCOPED_TRACE("new position");
Marissa Wall861616d2018-10-22 12:52:23 -0700596 const Rect rect(5, 10, 37, 42);
Chia-I Wu718daf82017-10-20 11:57:17 -0700597 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700598 shot->expectColor(rect, Color::RED);
599 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700600 }
601}
602
Marissa Wall861616d2018-10-22 12:52:23 -0700603TEST_F(LayerTransactionTest, SetPositionRounding_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700604 sp<SurfaceControl> layer;
605 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700606 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700607
608 // GLES requires only 4 bits of subpixel precision during rasterization
609 // XXX GLES composition does not match HWC composition due to precision
610 // loss (b/69315223)
611 const float epsilon = 1.0f / 16.0f;
612 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
613 {
614 SCOPED_TRACE("rounding down");
615 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
616 }
617
618 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
619 {
620 SCOPED_TRACE("rounding up");
621 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
622 }
623}
624
Marissa Wall861616d2018-10-22 12:52:23 -0700625TEST_F(LayerTransactionTest, SetPositionOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700626 sp<SurfaceControl> layer;
627 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700628 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700629
630 Transaction().setPosition(layer, -32, -32).apply();
631 {
632 SCOPED_TRACE("negative coordinates");
Marissa Wall861616d2018-10-22 12:52:23 -0700633 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700634 }
635
636 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
637 {
638 SCOPED_TRACE("positive coordinates");
Marissa Wall861616d2018-10-22 12:52:23 -0700639 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700640 }
641}
642
Marissa Wall861616d2018-10-22 12:52:23 -0700643TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700644 sp<SurfaceControl> layer;
645 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700646 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700647
648 // partially out of bounds
649 Transaction().setPosition(layer, -30, -30).apply();
650 {
651 SCOPED_TRACE("negative coordinates");
652 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
653 }
654
655 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
656 {
657 SCOPED_TRACE("positive coordinates");
658 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
659 mDisplayHeight),
660 Color::RED);
661 }
662}
663
Marissa Wall861616d2018-10-22 12:52:23 -0700664TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700665 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700666 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
667 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700668
669 // setPosition is applied immediately by default, with or without resize
670 // pending
671 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
672 {
673 SCOPED_TRACE("resize pending");
674 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700675 const Rect rect(5, 10, 37, 42);
Marissa Wall61c58622018-07-18 10:12:20 -0700676 shot->expectColor(rect, Color::RED);
677 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700678 }
679
Marissa Wall861616d2018-10-22 12:52:23 -0700680 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700681 {
682 SCOPED_TRACE("resize applied");
683 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
684 }
685}
686
Marissa Wall61c58622018-07-18 10:12:20 -0700687TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700688 sp<SurfaceControl> layer;
689 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700690 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700691
692 // request setPosition to be applied with the next resize
693 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
694 {
695 SCOPED_TRACE("new position pending");
696 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
697 }
698
699 Transaction().setPosition(layer, 15, 20).apply();
700 {
701 SCOPED_TRACE("pending new position modified");
702 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
703 }
704
705 Transaction().setSize(layer, 64, 64).apply();
706 {
707 SCOPED_TRACE("resize pending");
708 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
709 }
710
711 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700712 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700713 {
714 SCOPED_TRACE("new position applied");
715 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
716 }
717}
718
Marissa Wall61c58622018-07-18 10:12:20 -0700719TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700720 sp<SurfaceControl> layer;
721 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700722 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700723
724 // setPosition is not immediate even with SCALE_TO_WINDOW override
725 Transaction()
726 .setPosition(layer, 5, 10)
727 .setSize(layer, 64, 64)
728 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
729 .setGeometryAppliesWithResize(layer)
730 .apply();
731 {
732 SCOPED_TRACE("new position pending");
733 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
734 }
735
Marissa Wall61c58622018-07-18 10:12:20 -0700736 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700737 {
738 SCOPED_TRACE("new position applied");
739 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
740 }
741}
742
Marissa Wall861616d2018-10-22 12:52:23 -0700743TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700744 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700745 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
746 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700747
748 Transaction().setSize(layer, 64, 64).apply();
749 {
750 SCOPED_TRACE("resize pending");
751 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700752 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700753 shot->expectColor(rect, Color::RED);
754 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700755 }
756
Marissa Wall861616d2018-10-22 12:52:23 -0700757 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700758 {
759 SCOPED_TRACE("resize applied");
760 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700761 const Rect rect(0, 0, 64, 64);
762 shot->expectColor(rect, Color::RED);
763 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700764 }
765}
766
Marissa Wall61c58622018-07-18 10:12:20 -0700767TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700768 // cannot test robustness against invalid sizes (zero or really huge)
769}
770
Marissa Wall861616d2018-10-22 12:52:23 -0700771TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700772 sp<SurfaceControl> layer;
773 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700774 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700775
776 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
777 Transaction()
778 .setSize(layer, 64, 64)
779 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
780 .apply();
781 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
782}
783
Marissa Wall61c58622018-07-18 10:12:20 -0700784TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700785 sp<SurfaceControl> layerR;
786 sp<SurfaceControl> layerG;
787 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700788 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700789 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700790 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700791
792 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
793 {
794 SCOPED_TRACE("layerR");
795 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
796 }
797
798 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
799 {
800 SCOPED_TRACE("layerG");
801 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
802 }
803}
804
Marissa Wall61c58622018-07-18 10:12:20 -0700805TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700806 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800807 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700808 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800809 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700810 sp<SurfaceControl> layerR;
811 sp<SurfaceControl> layerG;
812 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700813 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700814 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700815 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700816
chaviw0e3479f2018-09-10 16:49:30 -0700817 Transaction()
818 .reparent(layerR, parent->getHandle())
819 .reparent(layerG, parent->getHandle())
820 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700821 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
822 {
823 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700824 auto shot = screenshot();
825 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700826 }
827
828 Transaction().setLayer(layerR, -3).apply();
829 {
830 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700831 auto shot = screenshot();
832 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700833 }
834}
835
Marissa Wall861616d2018-10-22 12:52:23 -0700836void LayerTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700837 sp<SurfaceControl> layerR;
838 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700839 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
840 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
841 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
842 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700843
Marissa Wall861616d2018-10-22 12:52:23 -0700844 switch (layerType) {
845 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
846 Transaction()
847 .setPosition(layerG, 16, 16)
848 .setRelativeLayer(layerG, layerR->getHandle(), 1)
849 .apply();
850 break;
851 case ISurfaceComposerClient::eFXSurfaceBufferState:
852 Transaction()
853 .setFrame(layerR, Rect(0, 0, 32, 32))
854 .setFrame(layerG, Rect(16, 16, 48, 48))
855 .setRelativeLayer(layerG, layerR->getHandle(), 1)
856 .apply();
857 break;
858 default:
859 ASSERT_FALSE(true) << "Unsupported layer type";
860 }
Chia-I Wu49313302017-10-31 10:14:40 -0700861 {
862 SCOPED_TRACE("layerG above");
863 auto shot = screenshot();
864 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
865 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
866 }
867
868 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
869 {
870 SCOPED_TRACE("layerG below");
871 auto shot = screenshot();
872 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
873 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
874 }
875}
876
Marissa Wall861616d2018-10-22 12:52:23 -0700877TEST_F(LayerTransactionTest, SetRelativeZBasic_BufferQueue) {
878 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
879}
880
881TEST_F(LayerTransactionTest, SetRelativeZBasic_BufferState) {
882 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
883}
884
Marissa Wall61c58622018-07-18 10:12:20 -0700885TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700886 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800887 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700888 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800889 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800890 sp<SurfaceControl> layerR;
891 sp<SurfaceControl> layerG;
892 sp<SurfaceControl> layerB;
893 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700894 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800895 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700896 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800897 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700898 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800899
chaviw0e3479f2018-09-10 16:49:30 -0700900 Transaction()
901 .reparent(layerB, parent->getHandle())
902 .apply();
903
Chia-I Wuec2d9852017-11-21 09:21:01 -0800904 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
905 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
906
chaviw0e3479f2018-09-10 16:49:30 -0700907 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800908 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700909 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -0700910 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800911 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
912}
913
Marissa Wall861616d2018-10-22 12:52:23 -0700914void LayerTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700915 sp<SurfaceControl> layerR;
916 sp<SurfaceControl> layerG;
917 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -0700918 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
919 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
920 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
921 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
922 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
923 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700924
925 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -0700926 switch (layerType) {
927 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
928 Transaction()
929 .setPosition(layerG, 8, 8)
930 .setRelativeLayer(layerG, layerR->getHandle(), 3)
931 .setPosition(layerB, 16, 16)
932 .setLayer(layerB, mLayerZBase + 2)
933 .apply();
934 break;
935 case ISurfaceComposerClient::eFXSurfaceBufferState:
936 Transaction()
937 .setFrame(layerR, Rect(0, 0, 32, 32))
938 .setFrame(layerG, Rect(8, 8, 40, 40))
939 .setRelativeLayer(layerG, layerR->getHandle(), 3)
940 .setFrame(layerB, Rect(16, 16, 48, 48))
941 .setLayer(layerB, mLayerZBase + 2)
942 .apply();
943 break;
944 default:
945 ASSERT_FALSE(true) << "Unsupported layer type";
946 }
947
Chia-I Wu49313302017-10-31 10:14:40 -0700948 {
949 SCOPED_TRACE("(layerR < layerG) < layerB");
950 auto shot = screenshot();
951 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
952 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
953 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
954 }
955
956 // layerR = 4, layerG = layerR + 3, layerB = 2
957 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
958 {
959 SCOPED_TRACE("layerB < (layerR < layerG)");
960 auto shot = screenshot();
961 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
962 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
963 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
964 }
965
966 // layerR = 4, layerG = layerR - 3, layerB = 2
967 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
968 {
969 SCOPED_TRACE("layerB < (layerG < layerR)");
970 auto shot = screenshot();
971 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
972 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
973 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
974 }
975
976 // restore to absolute z
977 // layerR = 4, layerG = 0, layerB = 2
978 Transaction().setLayer(layerG, mLayerZBase).apply();
979 {
980 SCOPED_TRACE("layerG < layerB < layerR");
981 auto shot = screenshot();
982 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
983 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
984 }
985
986 // layerR should not affect layerG anymore
987 // layerR = 1, layerG = 0, layerB = 2
988 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
989 {
990 SCOPED_TRACE("layerG < layerR < layerB");
991 auto shot = screenshot();
992 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
993 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
994 }
995}
996
Marissa Wall861616d2018-10-22 12:52:23 -0700997TEST_F(LayerTransactionTest, SetRelativeZGroup_BufferQueue) {
998 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
999}
1000
1001TEST_F(LayerTransactionTest, SetRelativeZGroup_BufferState) {
1002 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1003}
1004
Marissa Wall61c58622018-07-18 10:12:20 -07001005TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -07001006 sp<SurfaceControl> layerR;
1007 sp<SurfaceControl> layerG;
1008
1009 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001010 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001011 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001012 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001013
1014 Transaction()
1015 .setPosition(layerG, 16, 16)
1016 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1017 .apply();
1018
1019 mClient->destroySurface(layerG->getHandle());
1020 // layerG should have been removed
1021 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1022}
1023
Marissa Wall61c58622018-07-18 10:12:20 -07001024TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001025 sp<SurfaceControl> layer;
1026 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001027 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001028
1029 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1030 {
1031 SCOPED_TRACE("layer hidden");
Marissa Wall861616d2018-10-22 12:52:23 -07001032 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -07001033 }
1034
1035 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1036 {
1037 SCOPED_TRACE("layer shown");
1038 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1039 }
1040}
1041
Marissa Wall61c58622018-07-18 10:12:20 -07001042TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001043 const Color translucentRed = {100, 0, 0, 100};
1044 sp<SurfaceControl> layerR;
1045 sp<SurfaceControl> layerG;
1046 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001047 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001048 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001049 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001050
1051 Transaction()
1052 .setLayer(layerR, mLayerZBase + 1)
1053 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1054 .apply();
1055 {
1056 SCOPED_TRACE("layerR opaque");
1057 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1058 }
1059
1060 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1061 {
1062 SCOPED_TRACE("layerR translucent");
1063 const uint8_t g = uint8_t(255 - translucentRed.a);
1064 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1065 }
1066}
1067
Marissa Wall61c58622018-07-18 10:12:20 -07001068TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001069 sp<SurfaceControl> layer;
1070 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001071 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001072
1073 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001074 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001075 Transaction()
1076 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1077 .apply(true);
1078 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001079 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001080
1081 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1082 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001083 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001084}
1085
Marissa Wall61c58622018-07-18 10:12:20 -07001086TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001087 const Rect top(0, 0, 32, 16);
1088 const Rect bottom(0, 16, 32, 32);
1089 sp<SurfaceControl> layer;
1090 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1091
1092 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001093 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1094 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1095 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001096 // setTransparentRegionHint always applies to the following buffer
1097 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001098 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001099 {
1100 SCOPED_TRACE("top transparent");
1101 auto shot = screenshot();
1102 shot->expectColor(top, Color::BLACK);
1103 shot->expectColor(bottom, Color::RED);
1104 }
1105
1106 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1107 {
1108 SCOPED_TRACE("transparent region hint pending");
1109 auto shot = screenshot();
1110 shot->expectColor(top, Color::BLACK);
1111 shot->expectColor(bottom, Color::RED);
1112 }
1113
Marissa Wall61c58622018-07-18 10:12:20 -07001114 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1115 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1116 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1117 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001118 {
1119 SCOPED_TRACE("bottom transparent");
1120 auto shot = screenshot();
1121 shot->expectColor(top, Color::RED);
1122 shot->expectColor(bottom, Color::BLACK);
1123 }
1124}
1125
Marissa Wall61c58622018-07-18 10:12:20 -07001126TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1127 const Rect top(0, 0, 32, 16);
1128 const Rect bottom(0, 16, 32, 32);
1129 sp<SurfaceControl> layer;
1130 ASSERT_NO_FATAL_FAILURE(
1131 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1132
1133 sp<GraphicBuffer> buffer =
1134 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1135 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1136 BufferUsage::COMPOSER_OVERLAY,
1137 "test");
1138
1139 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1140 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1141 Transaction()
1142 .setTransparentRegionHint(layer, Region(top))
1143 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001144 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001145 .apply();
1146 {
1147 SCOPED_TRACE("top transparent");
1148 auto shot = screenshot();
1149 shot->expectColor(top, Color::BLACK);
1150 shot->expectColor(bottom, Color::RED);
1151 }
1152
1153 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1154 {
1155 SCOPED_TRACE("transparent region hint intermediate");
1156 auto shot = screenshot();
1157 shot->expectColor(top, Color::BLACK);
1158 shot->expectColor(bottom, Color::BLACK);
1159 }
1160
1161 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1162 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1163 BufferUsage::COMPOSER_OVERLAY,
1164 "test");
1165
1166 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1167 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001168 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001169 {
1170 SCOPED_TRACE("bottom transparent");
1171 auto shot = screenshot();
1172 shot->expectColor(top, Color::RED);
1173 shot->expectColor(bottom, Color::BLACK);
1174 }
1175}
1176
Marissa Wall861616d2018-10-22 12:52:23 -07001177TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001178 sp<SurfaceControl> layerTransparent;
1179 sp<SurfaceControl> layerR;
1180 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1181 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1182
1183 // check that transparent region hint is bound by the layer size
1184 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001185 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001186 .setPosition(layerR, 16, 16)
1187 .setLayer(layerR, mLayerZBase + 1)
1188 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001189 ASSERT_NO_FATAL_FAILURE(
1190 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1191 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001192 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1193}
1194
Marissa Wall861616d2018-10-22 12:52:23 -07001195TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
1196 sp<SurfaceControl> layerTransparent;
1197 sp<SurfaceControl> layerR;
1198 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1199 ASSERT_NO_FATAL_FAILURE(
1200 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1201
1202 // check that transparent region hint is bound by the layer size
1203 Transaction()
1204 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1205 .setFrame(layerR, Rect(16, 16, 48, 48))
1206 .setLayer(layerR, mLayerZBase + 1)
1207 .apply();
1208 ASSERT_NO_FATAL_FAILURE(
1209 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1210 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
1211 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1212}
1213
1214void LayerTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001215 sp<SurfaceControl> layer1;
1216 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001217 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1218 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1219 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1220 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001221
Marissa Wall861616d2018-10-22 12:52:23 -07001222 switch (layerType) {
1223 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1224 Transaction()
1225 .setAlpha(layer1, 0.25f)
1226 .setAlpha(layer2, 0.75f)
1227 .setPosition(layer2, 16, 0)
1228 .setLayer(layer2, mLayerZBase + 1)
1229 .apply();
1230 break;
1231 case ISurfaceComposerClient::eFXSurfaceBufferState:
1232 Transaction()
1233 .setAlpha(layer1, 0.25f)
1234 .setAlpha(layer2, 0.75f)
1235 .setFrame(layer1, Rect(0, 0, 32, 32))
1236 .setFrame(layer2, Rect(16, 0, 48, 32))
1237 .setLayer(layer2, mLayerZBase + 1)
1238 .apply();
1239 break;
1240 default:
1241 ASSERT_FALSE(true) << "Unsupported layer type";
1242 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001243 {
1244 auto shot = screenshot();
1245 uint8_t r = 16; // 64 * 0.25f
1246 uint8_t g = 48; // 64 * 0.75f
1247 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1248 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1249
1250 r /= 4; // r * (1.0f - 0.75f)
1251 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1252 }
1253}
1254
Marissa Wall861616d2018-10-22 12:52:23 -07001255TEST_F(LayerTransactionTest, SetAlphaBasic_BufferQueue) {
1256 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1257}
1258
1259TEST_F(LayerTransactionTest, SetAlphaBasic_BufferState) {
1260 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1261}
1262
Marissa Wall61c58622018-07-18 10:12:20 -07001263TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001264 const Color color = {64, 0, 0, 255};
1265 sp<SurfaceControl> layer;
1266 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001267 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001268
1269 Transaction().setAlpha(layer, 2.0f).apply();
1270 {
1271 SCOPED_TRACE("clamped to 1.0f");
1272 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1273 }
1274
1275 Transaction().setAlpha(layer, -1.0f).apply();
1276 {
1277 SCOPED_TRACE("clamped to 0.0f");
1278 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1279 }
1280}
1281
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001282TEST_P(LayerTypeTransactionTest, SetCornerRadius) {
1283 sp<SurfaceControl> layer;
1284 const uint8_t size = 64;
1285 const uint8_t testArea = 4;
1286 const float cornerRadius = 16.0f;
1287 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1288 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1289
1290 Transaction()
1291 .setCornerRadius(layer, cornerRadius)
1292 .apply();
1293 {
1294 auto shot = screenshot();
1295 // Transparent corners
1296 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
1297 shot->expectColor(Rect(0, size - testArea, testArea, testArea), Color::BLACK);
1298 shot->expectColor(Rect(size - testArea, 0, testArea, testArea), Color::BLACK);
1299 shot->expectColor(Rect(size - testArea, size - testArea, testArea, testArea),
1300 Color::BLACK);
1301 }
1302}
1303
Chia-I Wue4ef6102017-11-01 15:16:35 -07001304TEST_F(LayerTransactionTest, SetColorBasic) {
1305 sp<SurfaceControl> bufferLayer;
1306 sp<SurfaceControl> colorLayer;
1307 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001308 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001309 ASSERT_NO_FATAL_FAILURE(colorLayer =
1310 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1311 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001312
Vishnu Nair88a11f22018-11-28 18:30:57 -08001313 Transaction()
1314 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1315 .setLayer(colorLayer, mLayerZBase + 1)
1316 .apply();
1317
Chia-I Wue4ef6102017-11-01 15:16:35 -07001318 {
1319 SCOPED_TRACE("default color");
1320 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1321 }
1322
1323 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1324 const Color expected = {15, 51, 85, 255};
1325 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1326 // channel) should be less than one
1327 const uint8_t tolerance = 1;
1328 Transaction().setColor(colorLayer, color).apply();
1329 {
1330 SCOPED_TRACE("new color");
1331 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1332 }
1333}
1334
1335TEST_F(LayerTransactionTest, SetColorClamped) {
1336 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001337 ASSERT_NO_FATAL_FAILURE(colorLayer =
1338 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1339 ISurfaceComposerClient::eFXSurfaceColor));
1340 Transaction()
1341 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1342 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1343 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001344
Chia-I Wue4ef6102017-11-01 15:16:35 -07001345 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1346}
1347
1348TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1349 sp<SurfaceControl> bufferLayer;
1350 sp<SurfaceControl> colorLayer;
1351 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001352 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001353 ASSERT_NO_FATAL_FAILURE(colorLayer =
1354 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1355 ISurfaceComposerClient::eFXSurfaceColor));
1356 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001357
1358 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1359 const float alpha = 0.25f;
1360 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1361 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1362 // channel) should be less than one
1363 const uint8_t tolerance = 1;
1364 Transaction()
1365 .setColor(colorLayer, color)
1366 .setAlpha(colorLayer, alpha)
1367 .setLayer(colorLayer, mLayerZBase + 1)
1368 .apply();
1369 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1370 tolerance);
1371}
1372
Adrian Roosb7a96502018-04-08 11:38:55 -07001373TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1374 sp<SurfaceControl> bufferLayer;
1375 sp<SurfaceControl> parentLayer;
1376 sp<SurfaceControl> colorLayer;
1377 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1378 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001379 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001380 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1381 0 /* buffer height */,
1382 ISurfaceComposerClient::eFXSurfaceColor));
1383 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001384 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1385 const float alpha = 0.25f;
1386 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1387 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1388 // channel) should be less than one
1389 const uint8_t tolerance = 1;
1390 Transaction()
1391 .reparent(colorLayer, parentLayer->getHandle())
1392 .setColor(colorLayer, color)
1393 .setAlpha(parentLayer, alpha)
1394 .setLayer(parentLayer, mLayerZBase + 1)
1395 .apply();
1396 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1397 tolerance);
1398}
1399
Marissa Wall61c58622018-07-18 10:12:20 -07001400TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001401 sp<SurfaceControl> bufferLayer;
1402 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001403 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001404
1405 // color is ignored
1406 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1407 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1408}
1409
Marissa Wall61c58622018-07-18 10:12:20 -07001410TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001411 sp<SurfaceControl> layer;
1412 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001413 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001414
1415 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1416 {
1417 SCOPED_TRACE("non-existing layer stack");
Marissa Wall861616d2018-10-22 12:52:23 -07001418 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001419 }
1420
1421 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1422 {
1423 SCOPED_TRACE("original layer stack");
1424 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1425 }
1426}
1427
Marissa Wall861616d2018-10-22 12:52:23 -07001428TEST_F(LayerTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001429 sp<SurfaceControl> layer;
1430 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001431 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1432 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001433
1434 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1435 {
1436 SCOPED_TRACE("IDENTITY");
1437 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1438 Color::WHITE);
1439 }
1440
1441 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1442 {
1443 SCOPED_TRACE("FLIP_H");
1444 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1445 Color::BLUE);
1446 }
1447
1448 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1449 {
1450 SCOPED_TRACE("FLIP_V");
1451 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1452 Color::GREEN);
1453 }
1454
1455 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1456 {
1457 SCOPED_TRACE("ROT_90");
1458 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1459 Color::GREEN);
1460 }
1461
1462 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1463 {
1464 SCOPED_TRACE("SCALE");
1465 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1466 Color::WHITE, true /* filtered */);
1467 }
1468}
1469
Marissa Wall861616d2018-10-22 12:52:23 -07001470TEST_F(LayerTransactionTest, SetMatrixBasic_BufferState) {
1471 sp<SurfaceControl> layer;
1472 ASSERT_NO_FATAL_FAILURE(
1473 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1474 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1475 Color::BLUE, Color::WHITE));
1476
1477 Transaction()
1478 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1479 .setFrame(layer, Rect(0, 0, 32, 32))
1480 .apply();
1481 {
1482 SCOPED_TRACE("IDENTITY");
1483 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1484 Color::WHITE);
1485 }
1486
1487 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1488 {
1489 SCOPED_TRACE("FLIP_H");
1490 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1491 Color::WHITE);
1492 }
1493
1494 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1495 {
1496 SCOPED_TRACE("FLIP_V");
1497 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1498 Color::WHITE);
1499 }
1500
1501 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1502 {
1503 SCOPED_TRACE("ROT_90");
1504 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1505 Color::WHITE);
1506 }
1507
1508 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1509 {
1510 SCOPED_TRACE("SCALE");
1511 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1512 Color::WHITE);
1513 }
1514}
1515
1516TEST_F(LayerTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001517 sp<SurfaceControl> layer;
1518 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001519 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1520 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001521
1522 const float rot = M_SQRT1_2; // 45 degrees
1523 const float trans = M_SQRT2 * 16.0f;
1524 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1525
1526 auto shot = screenshot();
1527 // check a 8x8 region inside each color
1528 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1529 const int32_t halfL = 4;
1530 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1531 };
1532 const int32_t unit = int32_t(trans / 2);
1533 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1534 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1535 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1536 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1537}
1538
Marissa Wall861616d2018-10-22 12:52:23 -07001539TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001540 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001541 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1542 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001543
1544 // setMatrix is applied after any pending resize, unlike setPosition
1545 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1546 {
1547 SCOPED_TRACE("resize pending");
1548 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001549 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001550 shot->expectColor(rect, Color::RED);
1551 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001552 }
1553
Marissa Wall861616d2018-10-22 12:52:23 -07001554 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001555 {
1556 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001557 const Rect rect(0, 0, 128, 128);
1558 screenshot()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001559 }
1560}
1561
Marissa Wall861616d2018-10-22 12:52:23 -07001562TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001563 sp<SurfaceControl> layer;
1564 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001565 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001566
1567 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1568 Transaction()
1569 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1570 .setSize(layer, 64, 64)
1571 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1572 .apply();
1573 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1574}
1575
Marissa Wall861616d2018-10-22 12:52:23 -07001576TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001577 sp<SurfaceControl> layer;
1578 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001579 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1580 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001581
1582 // XXX SCALE_CROP is not respected; calling setSize and
1583 // setOverrideScalingMode in separate transactions does not work
1584 // (b/69315456)
1585 Transaction()
1586 .setSize(layer, 64, 16)
1587 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1588 .apply();
1589 {
1590 SCOPED_TRACE("SCALE_TO_WINDOW");
1591 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1592 Color::WHITE, true /* filtered */);
1593 }
1594}
1595
Dan Stoza000dd012018-08-01 13:31:52 -07001596TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1597 sp<SurfaceControl> layer;
1598 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1599
1600 sp<IBinder> handle = layer->getHandle();
1601 ASSERT_TRUE(handle != nullptr);
1602
1603 FrameStats frameStats;
1604 mClient->getLayerFrameStats(handle, &frameStats);
1605
1606 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1607}
1608
Marissa Wall61c58622018-07-18 10:12:20 -07001609TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001610 sp<SurfaceControl> layer;
1611 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001612 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001613 const Rect crop(8, 8, 24, 24);
1614
Marissa Wallf58c14b2018-07-24 10:50:43 -07001615 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001616 auto shot = screenshot();
1617 shot->expectColor(crop, Color::RED);
1618 shot->expectBorder(crop, Color::BLACK);
1619}
1620
Marissa Wall61c58622018-07-18 10:12:20 -07001621TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1622 sp<SurfaceControl> layer;
1623 ASSERT_NO_FATAL_FAILURE(
1624 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1625 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1626 const Rect crop(8, 8, 24, 24);
1627
1628 Transaction().setCrop(layer, crop).apply();
1629 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001630 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1631 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001632}
1633
1634TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001635 sp<SurfaceControl> layer;
1636 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001637 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001638
1639 {
1640 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001641 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001642 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1643 }
1644
1645 {
1646 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001647 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001648 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1649 }
1650}
1651
Marissa Wall61c58622018-07-18 10:12:20 -07001652TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1653 sp<SurfaceControl> layer;
1654 ASSERT_NO_FATAL_FAILURE(
1655 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1656 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1657
1658 {
1659 SCOPED_TRACE("empty rect");
1660 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1661 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1662 }
1663
1664 {
1665 SCOPED_TRACE("negative rect");
1666 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1667 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1668 }
1669}
1670
1671TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001672 sp<SurfaceControl> layer;
1673 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001674 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001675
Marissa Wallf58c14b2018-07-24 10:50:43 -07001676 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001677 auto shot = screenshot();
1678 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1679 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1680}
1681
Marissa Wall861616d2018-10-22 12:52:23 -07001682// TODO (marissaw): change Layer to make crop to be in bounds instead of passing a bad crop to hwc
1683// TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1684// sp<SurfaceControl> layer;
1685// ASSERT_NO_FATAL_FAILURE(
1686// layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1687// ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1688//
1689// Transaction()
1690// .setCrop(layer, Rect(-128, -64, 128, 64))
1691// .setFrame(layer, Rect(0, 0, 32, 32))
1692// .apply();
1693// auto shot = screenshot();
1694// shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1695// shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1696//}
1697//
Marissa Wall61c58622018-07-18 10:12:20 -07001698TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001699 sp<SurfaceControl> layer;
1700 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001701 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001702
1703 const Point position(32, 32);
1704 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001705 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001706 auto shot = screenshot();
1707 shot->expectColor(crop + position, Color::RED);
1708 shot->expectBorder(crop + position, Color::BLACK);
1709}
1710
Marissa Wall61c58622018-07-18 10:12:20 -07001711TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1712 sp<SurfaceControl> layer;
1713 ASSERT_NO_FATAL_FAILURE(
1714 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1715 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1716
Marissa Wall861616d2018-10-22 12:52:23 -07001717 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001718 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001719 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001720 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001721 shot->expectColor(frame, Color::RED);
1722 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001723}
1724
1725TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001726 sp<SurfaceControl> layer;
1727 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001728 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001729
Marissa Wall861616d2018-10-22 12:52:23 -07001730 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001731 Transaction()
1732 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001733 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001734 .apply();
1735 auto shot = screenshot();
1736 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1737 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1738}
1739
Marissa Wall61c58622018-07-18 10:12:20 -07001740TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001741 sp<SurfaceControl> layer;
1742 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001743 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001744
Marissa Wallf58c14b2018-07-24 10:50:43 -07001745 // setCrop_legacy is applied immediately by default, with or without resize pending
1746 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001747 {
1748 SCOPED_TRACE("resize pending");
1749 auto shot = screenshot();
1750 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1751 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1752 }
1753
Marissa Wall61c58622018-07-18 10:12:20 -07001754 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001755 {
1756 SCOPED_TRACE("resize applied");
1757 auto shot = screenshot();
1758 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1759 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1760 }
1761}
1762
Marissa Wall61c58622018-07-18 10:12:20 -07001763TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001764 sp<SurfaceControl> layer;
1765 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001766 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001767
Marissa Wallf58c14b2018-07-24 10:50:43 -07001768 // request setCrop_legacy to be applied with the next resize
1769 Transaction()
1770 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1771 .setGeometryAppliesWithResize(layer)
1772 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001773 {
1774 SCOPED_TRACE("waiting for next resize");
1775 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1776 }
1777
Marissa Wallf58c14b2018-07-24 10:50:43 -07001778 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001779 {
1780 SCOPED_TRACE("pending crop modified");
1781 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1782 }
1783
1784 Transaction().setSize(layer, 16, 16).apply();
1785 {
1786 SCOPED_TRACE("resize pending");
1787 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1788 }
1789
1790 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001791 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001792 {
1793 SCOPED_TRACE("new crop applied");
1794 auto shot = screenshot();
1795 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1796 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1797 }
1798}
1799
Marissa Wall61c58622018-07-18 10:12:20 -07001800TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001801 sp<SurfaceControl> layer;
1802 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001803 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001804
Marissa Wallf58c14b2018-07-24 10:50:43 -07001805 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001806 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001807 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001808 .setSize(layer, 16, 16)
1809 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1810 .setGeometryAppliesWithResize(layer)
1811 .apply();
1812 {
1813 SCOPED_TRACE("new crop pending");
1814 auto shot = screenshot();
1815 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1816 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1817 }
1818
1819 // XXX crop is never latched without other geometry change (b/69315677)
1820 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001821 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001822 Transaction().setPosition(layer, 0, 0).apply();
1823 {
1824 SCOPED_TRACE("new crop applied");
1825 auto shot = screenshot();
1826 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1827 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1828 }
1829}
1830
Marissa Wall861616d2018-10-22 12:52:23 -07001831TEST_F(LayerTransactionTest, SetFrameBasic_BufferState) {
1832 sp<SurfaceControl> layer;
1833 ASSERT_NO_FATAL_FAILURE(
1834 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1835 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1836 const Rect frame(8, 8, 24, 24);
1837
1838 Transaction().setFrame(layer, frame).apply();
1839 auto shot = screenshot();
1840 shot->expectColor(frame, Color::RED);
1841 shot->expectBorder(frame, Color::BLACK);
1842}
1843
1844TEST_F(LayerTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001845 sp<SurfaceControl> layer;
1846 ASSERT_NO_FATAL_FAILURE(
1847 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1848 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1849
Marissa Wall61c58622018-07-18 10:12:20 -07001850 {
Marissa Wall861616d2018-10-22 12:52:23 -07001851 SCOPED_TRACE("empty rect");
1852 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
1853 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001854 }
1855
Marissa Wall61c58622018-07-18 10:12:20 -07001856 {
Marissa Wall861616d2018-10-22 12:52:23 -07001857 SCOPED_TRACE("negative rect");
1858 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
1859 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001860 }
1861}
1862
Marissa Wall861616d2018-10-22 12:52:23 -07001863TEST_F(LayerTransactionTest, SetFrameDefaultParentless_BufferState) {
1864 sp<SurfaceControl> layer;
1865 ASSERT_NO_FATAL_FAILURE(
1866 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1867 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
1868
1869 // A parentless layer will default to a frame with the same size as the buffer
1870 auto shot = screenshot();
1871 shot->expectColor(Rect(0, 0, 10, 10), Color::RED);
1872 shot->expectBorder(Rect(0, 0, 10, 10), Color::BLACK);
1873}
1874
1875TEST_F(LayerTransactionTest, SetFrameDefaultBSParent_BufferState) {
1876 sp<SurfaceControl> parent, child;
1877 ASSERT_NO_FATAL_FAILURE(
1878 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1879 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
1880 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
1881
1882 ASSERT_NO_FATAL_FAILURE(
1883 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1884 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1885
1886 Transaction().reparent(child, parent->getHandle()).apply();
1887
1888 // A layer will default to the frame of its parent
1889 auto shot = screenshot();
1890 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1891 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1892}
1893
1894TEST_F(LayerTransactionTest, SetFrameDefaultBQParent_BufferState) {
1895 sp<SurfaceControl> parent, child;
1896 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
1897 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
1898
1899 ASSERT_NO_FATAL_FAILURE(
1900 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1901 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1902
1903 Transaction().reparent(child, parent->getHandle()).apply();
1904
1905 // A layer will default to the frame of its parent
1906 auto shot = screenshot();
1907 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1908 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1909}
1910
1911TEST_F(LayerTransactionTest, SetFrameUpdate_BufferState) {
1912 sp<SurfaceControl> layer;
1913 ASSERT_NO_FATAL_FAILURE(
1914 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1915 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1916 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
1917
1918 std::this_thread::sleep_for(500ms);
1919
1920 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
1921
1922 auto shot = screenshot();
1923 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1924 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1925}
1926
1927TEST_F(LayerTransactionTest, SetFrameOutsideBounds_BufferState) {
1928 sp<SurfaceControl> parent, child;
1929 ASSERT_NO_FATAL_FAILURE(
1930 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1931 ASSERT_NO_FATAL_FAILURE(
1932 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1933 Transaction().reparent(child, parent->getHandle()).apply();
1934
1935 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
1936 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
1937
1938 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1939 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
1940
1941 auto shot = screenshot();
1942 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
1943 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
1944 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1945}
1946
Marissa Wall61c58622018-07-18 10:12:20 -07001947TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1948 sp<SurfaceControl> layer;
1949 ASSERT_NO_FATAL_FAILURE(
1950 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1951
1952 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1953
1954 auto shot = screenshot();
1955 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1956 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1957}
1958
1959TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1960 sp<SurfaceControl> layer;
1961 ASSERT_NO_FATAL_FAILURE(
1962 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1963
1964 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1965
1966 {
1967 SCOPED_TRACE("set buffer 1");
1968 auto shot = screenshot();
1969 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1970 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1971 }
1972
1973 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1974
1975 {
1976 SCOPED_TRACE("set buffer 2");
1977 auto shot = screenshot();
1978 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1979 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1980 }
1981
1982 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1983
1984 {
1985 SCOPED_TRACE("set buffer 3");
1986 auto shot = screenshot();
1987 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1988 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1989 }
1990}
1991
1992TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1993 sp<SurfaceControl> layer1;
1994 ASSERT_NO_FATAL_FAILURE(
1995 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1996
1997 sp<SurfaceControl> layer2;
1998 ASSERT_NO_FATAL_FAILURE(
1999 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2000
2001 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2002
Marissa Wall861616d2018-10-22 12:52:23 -07002003 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002004 {
2005 SCOPED_TRACE("set layer 1 buffer red");
2006 auto shot = screenshot();
2007 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2008 }
2009
2010 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2011
Marissa Wall861616d2018-10-22 12:52:23 -07002012 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002013 {
2014 SCOPED_TRACE("set layer 2 buffer blue");
2015 auto shot = screenshot();
2016 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2017 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2018 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2019 }
2020
2021 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2022 {
2023 SCOPED_TRACE("set layer 1 buffer green");
2024 auto shot = screenshot();
2025 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2026 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2027 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2028 }
2029
2030 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2031
2032 {
2033 SCOPED_TRACE("set layer 2 buffer white");
2034 auto shot = screenshot();
2035 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2036 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2037 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2038 }
2039}
2040
2041TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
2042 sp<SurfaceControl> layer;
2043 ASSERT_NO_FATAL_FAILURE(
2044 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2045
2046 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2047 Color::BLUE, Color::WHITE));
2048
Marissa Wall861616d2018-10-22 12:52:23 -07002049 Transaction()
2050 .setFrame(layer, Rect(0, 0, 32, 32))
2051 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2052 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002053
2054 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2055 Color::GREEN, true /* filtered */);
2056}
2057
2058TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
2059 sp<SurfaceControl> layer;
2060 ASSERT_NO_FATAL_FAILURE(
2061 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2062
2063 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2064 Color::BLUE, Color::WHITE));
2065
Marissa Wall861616d2018-10-22 12:52:23 -07002066 Transaction()
2067 .setFrame(layer, Rect(0, 0, 32, 32))
2068 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2069 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002070
2071 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2072 Color::BLUE, true /* filtered */);
2073}
2074
2075TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
2076 sp<SurfaceControl> layer;
2077 ASSERT_NO_FATAL_FAILURE(
2078 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2079
2080 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2081 Color::BLUE, Color::WHITE));
2082
Marissa Wall861616d2018-10-22 12:52:23 -07002083 Transaction()
2084 .setFrame(layer, Rect(0, 0, 32, 32))
2085 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2086 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002087
2088 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2089 Color::GREEN, true /* filtered */);
2090}
2091
2092TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2093 sp<SurfaceControl> layer;
2094 ASSERT_NO_FATAL_FAILURE(
2095 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2096
2097 Transaction().setTransformToDisplayInverse(layer, false).apply();
2098
2099 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2100
2101 Transaction().setTransformToDisplayInverse(layer, true).apply();
2102}
2103
2104TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
2105 sp<SurfaceControl> layer;
2106 ASSERT_NO_FATAL_FAILURE(
2107 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2108
2109 sp<GraphicBuffer> buffer =
2110 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2111 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2112 BufferUsage::COMPOSER_OVERLAY,
2113 "test");
2114 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2115
Marissa Wallfda30bb2018-10-12 11:34:28 -07002116 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002117
2118 Transaction()
2119 .setBuffer(layer, buffer)
2120 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002121 .apply();
2122
2123 auto shot = screenshot();
2124 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2125 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2126}
2127
2128TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2129 sp<SurfaceControl> layer;
2130 ASSERT_NO_FATAL_FAILURE(
2131 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2132
2133 sp<GraphicBuffer> buffer =
2134 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2135 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2136 BufferUsage::COMPOSER_OVERLAY,
2137 "test");
2138 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2139
2140 Transaction()
2141 .setBuffer(layer, buffer)
2142 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002143 .apply();
2144
2145 auto shot = screenshot();
2146 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2147 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2148}
2149
2150TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2151 sp<SurfaceControl> layer;
2152 ASSERT_NO_FATAL_FAILURE(
2153 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2154
2155 sp<GraphicBuffer> buffer =
2156 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2157 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2158 BufferUsage::COMPOSER_OVERLAY,
2159 "test");
2160 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2161
2162 HdrMetadata hdrMetadata;
2163 hdrMetadata.validTypes = 0;
2164 Transaction()
2165 .setBuffer(layer, buffer)
2166 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002167 .apply();
2168
2169 auto shot = screenshot();
2170 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2171 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2172}
2173
2174TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2175 sp<SurfaceControl> layer;
2176 ASSERT_NO_FATAL_FAILURE(
2177 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2178
2179 sp<GraphicBuffer> buffer =
2180 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2181 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2182 BufferUsage::COMPOSER_OVERLAY,
2183 "test");
2184 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2185
2186 Region region;
2187 region.set(32, 32);
2188 Transaction()
2189 .setBuffer(layer, buffer)
2190 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002191 .apply();
2192
2193 auto shot = screenshot();
2194 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2195 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2196}
2197
2198TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2199 sp<SurfaceControl> layer;
2200 ASSERT_NO_FATAL_FAILURE(
2201 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2202
2203 sp<GraphicBuffer> buffer =
2204 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2205 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2206 BufferUsage::COMPOSER_OVERLAY,
2207 "test");
2208 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2209
2210 Transaction()
2211 .setBuffer(layer, buffer)
2212 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002213 .apply();
2214
2215 auto shot = screenshot();
2216 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2217 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2218}
2219
2220TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2221 sp<SurfaceControl> layer;
2222 ASSERT_NO_FATAL_FAILURE(
2223 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2224
2225 // verify this doesn't cause a crash
2226 Transaction().setSidebandStream(layer, nullptr).apply();
2227}
2228
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002229class ColorTransformHelper {
2230public:
2231 static void DegammaColorSingle(half& s) {
2232 if (s <= 0.03928f)
2233 s = s / 12.92f;
2234 else
2235 s = pow((s + 0.055f) / 1.055f, 2.4f);
2236 }
2237
2238 static void DegammaColor(half3& color) {
2239 DegammaColorSingle(color.r);
2240 DegammaColorSingle(color.g);
2241 DegammaColorSingle(color.b);
2242 }
2243
2244 static void GammaColorSingle(half& s) {
2245 if (s <= 0.0031308f) {
2246 s = s * 12.92f;
2247 } else {
2248 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2249 }
2250 }
2251
2252 static void GammaColor(half3& color) {
2253 GammaColorSingle(color.r);
2254 GammaColorSingle(color.g);
2255 GammaColorSingle(color.b);
2256 }
2257
2258 static void applyMatrix(half3& color, const mat3& mat) {
2259 half3 ret = half3(0);
2260
2261 for (int i = 0; i < 3; i++) {
2262 for (int j = 0; j < 3; j++) {
2263 ret[i] = ret[i] + color[j] * mat[j][i];
2264 }
2265 }
2266 color = ret;
2267 }
2268};
2269
Peiyong Lind3788632018-09-18 16:01:31 -07002270TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2271 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002272 ASSERT_NO_FATAL_FAILURE(colorLayer =
2273 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2274 ISurfaceComposerClient::eFXSurfaceColor));
2275 Transaction()
2276 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2277 .setLayer(colorLayer, mLayerZBase + 1)
2278 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002279 {
2280 SCOPED_TRACE("default color");
2281 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2282 }
2283
2284 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002285 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002286 mat3 matrix;
2287 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2288 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2289 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002290
2291 // degamma before applying the matrix
2292 if (mColorManagementUsed) {
2293 ColorTransformHelper::DegammaColor(expected);
2294 }
2295
2296 ColorTransformHelper::applyMatrix(expected, matrix);
2297
2298 if (mColorManagementUsed) {
2299 ColorTransformHelper::GammaColor(expected);
2300 }
2301
2302 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2303 uint8_t(expected.b * 255), 255};
2304
2305 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2306 // channel) should be less than one
2307 const uint8_t tolerance = 1;
2308
Peiyong Lind3788632018-09-18 16:01:31 -07002309 Transaction().setColor(colorLayer, color)
2310 .setColorTransform(colorLayer, matrix, vec3()).apply();
2311 {
2312 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002313 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002314 }
2315}
2316
Marissa Wallfda30bb2018-10-12 11:34:28 -07002317class ExpectedResult {
2318public:
2319 enum Transaction {
2320 NOT_PRESENTED = 0,
2321 PRESENTED,
2322 };
2323
2324 enum Buffer {
2325 NOT_ACQUIRED = 0,
2326 ACQUIRED,
2327 };
2328
2329 enum PreviousBuffer {
2330 NOT_RELEASED = 0,
2331 RELEASED,
2332 };
2333
2334 void reset() {
2335 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2336 mExpectedSurfaceResults.clear();
2337 }
2338
2339 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
2340 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2341 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2342 mTransactionResult = transactionResult;
2343 mExpectedSurfaceResults.emplace(std::piecewise_construct,
2344 std::forward_as_tuple(layer->getHandle()),
2345 std::forward_as_tuple(bufferResult, previousBufferResult));
2346 }
2347
2348 void addSurfaces(ExpectedResult::Transaction transactionResult,
2349 const std::vector<sp<SurfaceControl>>& layers,
2350 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2351 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2352 for (const auto& layer : layers) {
2353 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2354 }
2355 }
2356
2357 void verifyTransactionStats(const TransactionStats& transactionStats) const {
2358 const auto& [latchTime, presentTime, surfaceStats] = transactionStats;
2359 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2360 ASSERT_GE(latchTime, 0) << "bad latch time";
2361 ASSERT_GE(presentTime, 0) << "bad present time";
2362 } else {
2363 ASSERT_EQ(presentTime, -1) << "transaction shouldn't have been presented";
2364 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2365 }
2366
2367 ASSERT_EQ(surfaceStats.size(), mExpectedSurfaceResults.size())
2368 << "wrong number of surfaces";
2369
2370 for (const auto& stats : surfaceStats) {
2371 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2372 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2373 << "unexpected surface control";
2374 expectedSurfaceResult->second.verifySurfaceStats(stats, latchTime);
2375 }
2376 }
2377
2378private:
2379 class ExpectedSurfaceResult {
2380 public:
2381 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2382 ExpectedResult::PreviousBuffer previousBufferResult)
2383 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2384
2385 void verifySurfaceStats(const SurfaceStats& surfaceStats, nsecs_t latchTime) const {
2386 const auto& [surfaceControl, acquireTime, releasePreviousBuffer] = surfaceStats;
2387
2388 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2389 << "bad acquire time";
2390 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
2391 ASSERT_EQ(releasePreviousBuffer,
2392 mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED)
2393 << "bad previous buffer released";
2394 }
2395
2396 private:
2397 ExpectedResult::Buffer mBufferResult;
2398 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2399 };
2400
2401 struct IBinderHash {
2402 std::size_t operator()(const sp<IBinder>& strongPointer) const {
2403 return std::hash<IBinder*>{}(strongPointer.get());
2404 }
2405 };
2406 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2407 std::unordered_map<sp<IBinder>, ExpectedSurfaceResult, IBinderHash> mExpectedSurfaceResults;
2408};
2409
2410class CallbackHelper {
2411public:
2412 static void function(void* callbackContext, const TransactionStats& transactionStats) {
2413 if (!callbackContext) {
2414 ALOGE("failed to get callback context");
2415 }
2416 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2417 std::lock_guard lock(helper->mMutex);
2418 helper->mTransactionStatsQueue.push(transactionStats);
2419 helper->mConditionVariable.notify_all();
2420 }
2421
2422 void getTransactionStats(TransactionStats* outStats) {
2423 std::unique_lock lock(mMutex);
2424
2425 if (mTransactionStatsQueue.empty()) {
2426 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2427 std::cv_status::timeout)
2428 << "did not receive callback";
2429 }
2430
2431 *outStats = std::move(mTransactionStatsQueue.front());
2432 mTransactionStatsQueue.pop();
2433 }
2434
2435 void verifyFinalState() {
2436 // Wait to see if there are extra callbacks
2437 std::this_thread::sleep_for(500ms);
2438
2439 std::lock_guard lock(mMutex);
2440 EXPECT_EQ(mTransactionStatsQueue.size(), 0) << "extra callbacks received";
2441 mTransactionStatsQueue = {};
2442 }
2443
2444 void* getContext() { return static_cast<void*>(this); }
2445
2446 std::mutex mMutex;
2447 std::condition_variable mConditionVariable;
2448 std::queue<TransactionStats> mTransactionStatsQueue;
2449};
2450
2451class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002452public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002453 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002454 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002455 }
2456
Marissa Wall861616d2018-10-22 12:52:23 -07002457 static void fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2458 const sp<SurfaceControl>& layer = nullptr) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002459 if (layer) {
2460 sp<GraphicBuffer> buffer =
Marissa Wall861616d2018-10-22 12:52:23 -07002461 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002462 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2463 BufferUsage::COMPOSER_OVERLAY |
2464 BufferUsage::GPU_TEXTURE,
2465 "test");
Marissa Wall861616d2018-10-22 12:52:23 -07002466 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002467
2468 sp<Fence> fence = new Fence(-1);
2469
Marissa Wall861616d2018-10-22 12:52:23 -07002470 transaction.setBuffer(layer, buffer).setAcquireFence(layer, fence);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002471 }
2472
2473 transaction.addTransactionCompletedCallback(callbackHelper->function,
2474 callbackHelper->getContext());
2475 }
2476
Marissa Wall861616d2018-10-22 12:52:23 -07002477 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2478 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002479 TransactionStats transactionStats;
2480 ASSERT_NO_FATAL_FAILURE(helper.getTransactionStats(&transactionStats));
2481 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyTransactionStats(transactionStats));
2482
2483 if (finalState) {
2484 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2485 }
2486 }
2487
Marissa Wall861616d2018-10-22 12:52:23 -07002488 static void waitForCallbacks(CallbackHelper& helper,
2489 const std::vector<ExpectedResult>& expectedResults,
2490 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002491 for (const auto& expectedResult : expectedResults) {
2492 waitForCallback(helper, expectedResult);
2493 }
2494 if (finalState) {
2495 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2496 }
2497 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002498};
2499
2500TEST_F(LayerCallbackTest, Basic) {
2501 sp<SurfaceControl> layer;
2502 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2503
2504 Transaction transaction;
2505 CallbackHelper callback;
2506 fillTransaction(transaction, &callback, layer);
2507
2508 transaction.apply();
2509
2510 ExpectedResult expected;
2511 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2512 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2513}
2514
2515TEST_F(LayerCallbackTest, NoBuffer) {
2516 sp<SurfaceControl> layer;
2517 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2518
2519 Transaction transaction;
2520 CallbackHelper callback;
2521 fillTransaction(transaction, &callback);
2522
Marissa Wall861616d2018-10-22 12:52:23 -07002523 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002524
2525 ExpectedResult expected;
2526 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2527 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2528}
2529
2530TEST_F(LayerCallbackTest, NoStateChange) {
2531 Transaction transaction;
2532 CallbackHelper callback;
2533 fillTransaction(transaction, &callback);
2534
2535 transaction.apply();
2536
2537 ExpectedResult expected;
2538 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2539}
2540
2541TEST_F(LayerCallbackTest, OffScreen) {
2542 sp<SurfaceControl> layer;
2543 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2544
2545 Transaction transaction;
2546 CallbackHelper callback;
2547 fillTransaction(transaction, &callback, layer);
2548
Marissa Wall861616d2018-10-22 12:52:23 -07002549 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002550
2551 ExpectedResult expected;
2552 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2553 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2554}
2555
2556TEST_F(LayerCallbackTest, Merge) {
2557 sp<SurfaceControl> layer1, layer2;
2558 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2559 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2560
2561 Transaction transaction1, transaction2;
2562 CallbackHelper callback1, callback2;
2563 fillTransaction(transaction1, &callback1, layer1);
2564 fillTransaction(transaction2, &callback2, layer2);
2565
Marissa Wall861616d2018-10-22 12:52:23 -07002566 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2567 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002568
2569 ExpectedResult expected;
2570 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2571 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2572 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2573}
2574
2575TEST_F(LayerCallbackTest, Merge_SameCallback) {
2576 sp<SurfaceControl> layer1, layer2;
2577 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2578 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2579
2580 Transaction transaction1, transaction2;
2581 CallbackHelper callback;
2582 fillTransaction(transaction1, &callback, layer1);
2583 fillTransaction(transaction2, &callback, layer2);
2584
2585 transaction2.merge(std::move(transaction1)).apply();
2586
2587 ExpectedResult expected;
2588 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2589 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2590 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2591}
2592
2593TEST_F(LayerCallbackTest, Merge_SameLayer) {
2594 sp<SurfaceControl> layer;
2595 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2596
2597 Transaction transaction1, transaction2;
2598 CallbackHelper callback1, callback2;
2599 fillTransaction(transaction1, &callback1, layer);
2600 fillTransaction(transaction2, &callback2, layer);
2601
2602 transaction2.merge(std::move(transaction1)).apply();
2603
2604 ExpectedResult expected;
2605 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2606 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2607 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2608}
2609
2610TEST_F(LayerCallbackTest, Merge_SingleBuffer) {
2611 sp<SurfaceControl> layer1, layer2;
2612 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2613 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2614
2615 Transaction transaction1, transaction2;
2616 CallbackHelper callback1, callback2;
2617 fillTransaction(transaction1, &callback1, layer1);
2618 fillTransaction(transaction2, &callback2);
2619
Marissa Wall861616d2018-10-22 12:52:23 -07002620 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2621 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002622
2623 ExpectedResult expected;
2624 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2625 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2626 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2627}
2628
2629TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2630 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2631 client2(new SurfaceComposerClient);
2632
2633 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2634 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2635
2636 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002637 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002638 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002639 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002640 ISurfaceComposerClient::eFXSurfaceBufferState));
2641
2642 Transaction transaction1, transaction2;
2643 CallbackHelper callback1, callback2;
2644 fillTransaction(transaction1, &callback1, layer1);
2645 fillTransaction(transaction2, &callback2, layer2);
2646
Marissa Wall861616d2018-10-22 12:52:23 -07002647 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2648 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002649
2650 ExpectedResult expected;
2651 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2652 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2653 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2654}
2655
2656TEST_F(LayerCallbackTest, MultipleTransactions) {
2657 sp<SurfaceControl> layer;
2658 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2659
2660 Transaction transaction;
2661 CallbackHelper callback;
2662 for (size_t i = 0; i < 10; i++) {
2663 fillTransaction(transaction, &callback, layer);
2664
2665 transaction.apply();
2666
2667 ExpectedResult expected;
2668 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2669 ExpectedResult::Buffer::NOT_ACQUIRED,
2670 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2671 : ExpectedResult::PreviousBuffer::RELEASED);
2672 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2673 }
2674 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2675}
2676
2677TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
2678 sp<SurfaceControl> layer;
2679 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2680
2681 Transaction transaction;
2682 CallbackHelper callback;
2683 for (size_t i = 0; i < 10; i++) {
2684 ExpectedResult expected;
2685
2686 if (i == 0) {
2687 fillTransaction(transaction, &callback, layer);
2688 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2689 } else {
2690 fillTransaction(transaction, &callback);
2691 }
2692
2693 transaction.apply();
2694
2695 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2696 }
2697 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2698}
2699
2700TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
2701 sp<SurfaceControl> layer;
2702 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2703
2704 Transaction transaction;
2705 CallbackHelper callback;
2706 for (size_t i = 0; i < 10; i++) {
2707 if (i == 0) {
2708 fillTransaction(transaction, &callback, layer);
2709 } else {
2710 fillTransaction(transaction, &callback);
2711 }
2712
Marissa Wall861616d2018-10-22 12:52:23 -07002713 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002714
2715 ExpectedResult expected;
2716 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
2717 : ExpectedResult::Transaction::NOT_PRESENTED,
2718 layer);
2719 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
2720 }
2721 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2722}
2723
2724TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
2725 sp<SurfaceControl> layer1, layer2;
2726 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2727 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2728
2729 Transaction transaction1, transaction2;
2730 CallbackHelper callback1, callback2;
2731 for (size_t i = 0; i < 10; i++) {
2732 fillTransaction(transaction1, &callback1, layer1);
2733 fillTransaction(transaction2, &callback2, layer2);
2734
Marissa Wall861616d2018-10-22 12:52:23 -07002735 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2736 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002737
2738 ExpectedResult expected;
2739 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2740 ExpectedResult::Buffer::NOT_ACQUIRED,
2741 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2742 : ExpectedResult::PreviousBuffer::RELEASED);
2743 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2744 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2745 }
2746 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2747 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2748}
2749
2750TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
2751 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2752 client2(new SurfaceComposerClient);
2753 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2754 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2755
2756 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002757 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002758 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002759 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002760 ISurfaceComposerClient::eFXSurfaceBufferState));
2761
2762 Transaction transaction1, transaction2;
2763 CallbackHelper callback1, callback2;
2764 for (size_t i = 0; i < 10; i++) {
2765 fillTransaction(transaction1, &callback1, layer1);
2766 fillTransaction(transaction2, &callback2, layer2);
2767
Marissa Wall861616d2018-10-22 12:52:23 -07002768 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2769 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002770
2771 ExpectedResult expected;
2772 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2773 ExpectedResult::Buffer::NOT_ACQUIRED,
2774 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2775 : ExpectedResult::PreviousBuffer::RELEASED);
2776 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2777 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2778 }
2779 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2780 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2781}
2782
2783TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
2784 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2785 client2(new SurfaceComposerClient);
2786 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2787 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2788
2789 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002790 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002791 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002792 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002793 ISurfaceComposerClient::eFXSurfaceBufferState));
2794
2795 Transaction transaction1, transaction2;
2796 CallbackHelper callback1, callback2;
2797
2798 // Normal call to set up test
2799 fillTransaction(transaction1, &callback1, layer1);
2800 fillTransaction(transaction2, &callback2, layer2);
2801
Marissa Wall861616d2018-10-22 12:52:23 -07002802 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2803 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002804
2805 ExpectedResult expected;
2806 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2807 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2808 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2809 expected.reset();
2810
2811 // Test
2812 fillTransaction(transaction1, &callback1);
2813 fillTransaction(transaction2, &callback2);
2814
2815 transaction2.merge(std::move(transaction1)).apply();
2816
2817 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2818 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2819}
2820
2821TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
2822 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2823 client2(new SurfaceComposerClient);
2824
2825 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2826 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2827
2828 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002829 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002830 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002831 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002832 ISurfaceComposerClient::eFXSurfaceBufferState));
2833
2834 Transaction transaction1, transaction2;
2835 CallbackHelper callback1, callback2;
2836
2837 // Normal call to set up test
2838 fillTransaction(transaction1, &callback1, layer1);
2839 fillTransaction(transaction2, &callback2, layer2);
2840
Marissa Wall861616d2018-10-22 12:52:23 -07002841 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2842 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002843
2844 ExpectedResult expected;
2845 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2846 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2847 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2848 expected.reset();
2849
2850 // Test
2851 fillTransaction(transaction1, &callback1);
2852 fillTransaction(transaction2, &callback2);
2853
Marissa Wall861616d2018-10-22 12:52:23 -07002854 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002855
2856 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2);
2857 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2858 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2859}
2860
2861TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
2862 sp<SurfaceControl> layer;
2863 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2864
2865 Transaction transaction;
2866 CallbackHelper callback;
2867 std::vector<ExpectedResult> expectedResults(50);
2868 ExpectedResult::PreviousBuffer previousBufferResult =
2869 ExpectedResult::PreviousBuffer::NOT_RELEASED;
2870 for (auto& expected : expectedResults) {
2871 expected.reset();
2872 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2873 ExpectedResult::Buffer::NOT_ACQUIRED, previousBufferResult);
2874 previousBufferResult = ExpectedResult::PreviousBuffer::RELEASED;
2875
2876 fillTransaction(transaction, &callback, layer);
2877
2878 transaction.apply();
2879 std::this_thread::sleep_for(200ms);
2880 }
2881 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2882}
2883
2884TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
2885 sp<SurfaceControl> layer;
2886 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2887
2888 Transaction transaction;
2889 CallbackHelper callback;
2890 std::vector<ExpectedResult> expectedResults(50);
2891 bool first = true;
2892 for (auto& expected : expectedResults) {
2893 expected.reset();
2894
2895 if (first) {
2896 fillTransaction(transaction, &callback, layer);
2897 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2898 first = false;
2899 } else {
2900 fillTransaction(transaction, &callback);
2901 }
2902
2903 transaction.apply();
2904 std::this_thread::sleep_for(200ms);
2905 }
2906 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2907}
2908
2909TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
2910 sp<SurfaceControl> layer;
2911 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2912
2913 // Normal call to set up test
2914 Transaction transaction;
2915 CallbackHelper callback;
2916 fillTransaction(transaction, &callback, layer);
2917
Marissa Wall861616d2018-10-22 12:52:23 -07002918 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002919
2920 ExpectedResult expectedResult;
2921 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2922 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
2923
2924 // Test
2925 std::vector<ExpectedResult> expectedResults(50);
2926 for (auto& expected : expectedResults) {
2927 expected.reset();
2928 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2929
2930 fillTransaction(transaction, &callback);
2931
Marissa Wall861616d2018-10-22 12:52:23 -07002932 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002933
2934 std::this_thread::sleep_for(200ms);
2935 }
2936 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2937}
2938
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002939class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002940protected:
2941 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002942 LayerTransactionTest::SetUp();
2943 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002944
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002945 sp<IBinder> display(
2946 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002947 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002948 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002949
2950 ssize_t displayWidth = info.w;
2951 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002952
2953 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002954 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2955 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002956 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002957 ASSERT_TRUE(mBGSurfaceControl->isValid());
2958 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2959
2960 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002961 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2962
Peiyong Lin566a3b42018-01-09 18:22:43 -08002963 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002964 ASSERT_TRUE(mFGSurfaceControl->isValid());
2965
2966 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2967
2968 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002969 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002970 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002971 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2972
2973 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2974
Robert Carr4cdc58f2017-08-23 14:22:20 -07002975 asTransaction([&](Transaction& t) {
2976 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002977
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002978 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002979
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002980 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2981 .setPosition(mFGSurfaceControl, 64, 64)
2982 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002983
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002984 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2985 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2986 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002987 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002988 }
2989
2990 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002991 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002992 mBGSurfaceControl = 0;
2993 mFGSurfaceControl = 0;
2994 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002995 }
2996
2997 void waitForPostedBuffers() {
2998 // Since the sync surface is in synchronous mode (i.e. double buffered)
2999 // posting three buffers to it should ensure that at least two
3000 // SurfaceFlinger::handlePageFlip calls have been made, which should
3001 // guaranteed that a buffer posted to another Surface has been retired.
3002 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3003 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3004 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3005 }
3006
Robert Carr4cdc58f2017-08-23 14:22:20 -07003007 void asTransaction(const std::function<void(Transaction&)>& exec) {
3008 Transaction t;
3009 exec(t);
3010 t.apply(true);
3011 }
3012
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003013 sp<SurfaceControl> mBGSurfaceControl;
3014 sp<SurfaceControl> mFGSurfaceControl;
3015
3016 // This surface is used to ensure that the buffers posted to
3017 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3018 sp<SurfaceControl> mSyncSurfaceControl;
3019};
3020
Robert Carr7f619b22017-11-06 12:56:35 -08003021TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003022
chaviw0e3479f2018-09-10 16:49:30 -07003023 std::unique_ptr<ScreenCapture> sc;
3024
3025 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003026 fillSurfaceRGBA8(relative, 10, 10, 10);
3027 waitForPostedBuffers();
3028
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003029 Transaction{}
3030 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003031 .setPosition(relative, 64, 64)
3032 .apply();
3033
3034 {
3035 // The relative should be on top of the FG control.
3036 ScreenCapture::captureScreen(&sc);
3037 sc->checkPixel(64, 64, 10, 10, 10);
3038 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003039 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003040
3041 {
3042 // Nothing should change at this point.
3043 ScreenCapture::captureScreen(&sc);
3044 sc->checkPixel(64, 64, 10, 10, 10);
3045 }
3046
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003047 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003048
3049 {
3050 // Ensure that the relative was actually hidden, rather than
3051 // being left in the detached but visible state.
3052 ScreenCapture::captureScreen(&sc);
3053 sc->expectFGColor(64, 64);
3054 }
3055}
3056
Robert Carr8d5227b2017-03-16 15:41:03 -07003057class GeometryLatchingTest : public LayerUpdateTest {
3058protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003059 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003060 SCOPED_TRACE(trace);
3061 ScreenCapture::captureScreen(&sc);
3062 // We find the leading edge of the FG surface.
3063 sc->expectFGColor(127, 127);
3064 sc->expectBGColor(128, 128);
3065 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003066
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003067 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07003068
3069 void unlockFGBuffer() {
3070 sp<Surface> s = mFGSurfaceControl->getSurface();
3071 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3072 waitForPostedBuffers();
3073 }
3074
Robert Carr8d5227b2017-03-16 15:41:03 -07003075 void completeFGResize() {
3076 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3077 waitForPostedBuffers();
3078 }
3079 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003080 asTransaction([&](Transaction& t) {
3081 t.setSize(mFGSurfaceControl, 64, 64);
3082 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003083 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003084 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003085
3086 EXPECT_INITIAL_STATE("After restoring initial state");
3087 }
chaviw0e3479f2018-09-10 16:49:30 -07003088 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003089};
3090
Robert Carr8d5227b2017-03-16 15:41:03 -07003091class CropLatchingTest : public GeometryLatchingTest {
3092protected:
3093 void EXPECT_CROPPED_STATE(const char* trace) {
3094 SCOPED_TRACE(trace);
3095 ScreenCapture::captureScreen(&sc);
3096 // The edge should be moved back one pixel by our crop.
3097 sc->expectFGColor(126, 126);
3098 sc->expectBGColor(127, 127);
3099 sc->expectBGColor(128, 128);
3100 }
chaviw59f5c562017-06-28 16:39:06 -07003101
3102 void EXPECT_RESIZE_STATE(const char* trace) {
3103 SCOPED_TRACE(trace);
3104 ScreenCapture::captureScreen(&sc);
3105 // The FG is now resized too 128,128 at 64,64
3106 sc->expectFGColor(64, 64);
3107 sc->expectFGColor(191, 191);
3108 sc->expectBGColor(192, 192);
3109 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003110};
3111
Pablo Ceballos05289c22016-04-14 15:49:55 -07003112TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003113 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003114 {
3115 SCOPED_TRACE("before anything");
3116 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003117 sc->expectBGColor(32, 32);
3118 sc->expectFGColor(96, 96);
3119 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003120 }
3121
3122 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003123 asTransaction([&](Transaction& t) {
3124 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003125 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3126 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003127 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003128
Robert Carr4cdc58f2017-08-23 14:22:20 -07003129 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003130 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003131 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3132 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003133 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003134
3135 {
3136 SCOPED_TRACE("before any trigger");
3137 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003138 sc->expectBGColor(32, 32);
3139 sc->expectFGColor(96, 96);
3140 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003141 }
3142
3143 // should trigger the first deferred transaction, but not the second one
3144 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3145 {
3146 SCOPED_TRACE("after first trigger");
3147 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003148 sc->expectBGColor(32, 32);
3149 sc->checkPixel(96, 96, 162, 63, 96);
3150 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003151 }
3152
3153 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003154 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003155
3156 // trigger the second deferred transaction
3157 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3158 {
3159 SCOPED_TRACE("after second trigger");
3160 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003161 sc->expectBGColor(32, 32);
3162 sc->expectBGColor(96, 96);
3163 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003164 }
3165}
3166
Robert Carre392b552017-09-19 12:16:05 -07003167TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003168 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003169
3170 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003171 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3172 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3173 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3174 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003175 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003176 SurfaceComposerClient::Transaction{}
3177 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3178 .show(childNoBuffer)
3179 .show(childBuffer)
3180 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003181 {
3182 ScreenCapture::captureScreen(&sc);
3183 sc->expectChildColor(73, 73);
3184 sc->expectFGColor(74, 74);
3185 }
Vishnu Nair60356342018-11-13 13:00:45 -08003186 SurfaceComposerClient::Transaction{}
3187 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3188 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003189 {
3190 ScreenCapture::captureScreen(&sc);
3191 sc->expectChildColor(73, 73);
3192 sc->expectChildColor(74, 74);
3193 }
3194}
3195
Robert Carr2c5f6d22017-09-26 12:30:35 -07003196TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003197 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003198 {
3199 SCOPED_TRACE("before move");
3200 ScreenCapture::captureScreen(&sc);
3201 sc->expectBGColor(0, 12);
3202 sc->expectFGColor(75, 75);
3203 sc->expectBGColor(145, 145);
3204 }
3205
3206 Transaction t1, t2;
3207 t1.setPosition(mFGSurfaceControl, 128, 128);
3208 t2.setPosition(mFGSurfaceControl, 0, 0);
3209 // We expect that the position update from t2 now
3210 // overwrites the position update from t1.
3211 t1.merge(std::move(t2));
3212 t1.apply();
3213
3214 {
3215 ScreenCapture::captureScreen(&sc);
3216 sc->expectFGColor(1, 1);
3217 }
3218}
3219
Robert Carr1f0a16a2016-10-24 16:27:39 -07003220class ChildLayerTest : public LayerUpdateTest {
3221protected:
3222 void SetUp() override {
3223 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003224 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3225 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003226 fillSurfaceRGBA8(mChild, 200, 200, 200);
3227
3228 {
3229 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003230 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003231 mCapture->expectChildColor(64, 64);
3232 }
3233 }
3234 void TearDown() override {
3235 LayerUpdateTest::TearDown();
3236 mChild = 0;
3237 }
3238
3239 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003240 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003241};
3242
3243TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003244 asTransaction([&](Transaction& t) {
3245 t.show(mChild);
3246 t.setPosition(mChild, 10, 10);
3247 t.setPosition(mFGSurfaceControl, 64, 64);
3248 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003249
3250 {
chaviw0e3479f2018-09-10 16:49:30 -07003251 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003252 // Top left of foreground must now be visible
3253 mCapture->expectFGColor(64, 64);
3254 // But 10 pixels in we should see the child surface
3255 mCapture->expectChildColor(74, 74);
3256 // And 10 more pixels we should be back to the foreground surface
3257 mCapture->expectFGColor(84, 84);
3258 }
3259
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003260 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003261
3262 {
chaviw0e3479f2018-09-10 16:49:30 -07003263 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003264 // Top left of foreground should now be at 0, 0
3265 mCapture->expectFGColor(0, 0);
3266 // But 10 pixels in we should see the child surface
3267 mCapture->expectChildColor(10, 10);
3268 // And 10 more pixels we should be back to the foreground surface
3269 mCapture->expectFGColor(20, 20);
3270 }
3271}
3272
Robert Carr41b08b52017-06-01 16:11:34 -07003273TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003274 asTransaction([&](Transaction& t) {
3275 t.show(mChild);
3276 t.setPosition(mChild, 0, 0);
3277 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003278 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003279 });
Robert Carr41b08b52017-06-01 16:11:34 -07003280
3281 {
chaviw0e3479f2018-09-10 16:49:30 -07003282 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003283 mCapture->expectChildColor(0, 0);
3284 mCapture->expectChildColor(4, 4);
3285 mCapture->expectBGColor(5, 5);
3286 }
3287}
3288
Robert Carr1f0a16a2016-10-24 16:27:39 -07003289TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003290 asTransaction([&](Transaction& t) {
3291 t.show(mChild);
3292 t.setPosition(mFGSurfaceControl, 0, 0);
3293 t.setPosition(mChild, 63, 63);
3294 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003295
3296 {
chaviw0e3479f2018-09-10 16:49:30 -07003297 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003298 mCapture->expectFGColor(0, 0);
3299 // Last pixel in foreground should now be the child.
3300 mCapture->expectChildColor(63, 63);
3301 // But the child should be constrained and the next pixel
3302 // must be the background
3303 mCapture->expectBGColor(64, 64);
3304 }
3305}
3306
3307TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003308 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003309
3310 // Find the boundary between the parent and child
3311 {
chaviw0e3479f2018-09-10 16:49:30 -07003312 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003313 mCapture->expectChildColor(9, 9);
3314 mCapture->expectFGColor(10, 10);
3315 }
3316
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003317 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003318
3319 // The boundary should be twice as far from the origin now.
3320 // The pixels from the last test should all be child now
3321 {
chaviw0e3479f2018-09-10 16:49:30 -07003322 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003323 mCapture->expectChildColor(9, 9);
3324 mCapture->expectChildColor(10, 10);
3325 mCapture->expectChildColor(19, 19);
3326 mCapture->expectFGColor(20, 20);
3327 }
3328}
Robert Carr9524cb32017-02-13 11:32:32 -08003329
Robert Carr6452f122017-03-21 10:41:29 -07003330TEST_F(ChildLayerTest, ChildLayerAlpha) {
3331 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3332 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3333 fillSurfaceRGBA8(mChild, 0, 254, 0);
3334 waitForPostedBuffers();
3335
Robert Carr4cdc58f2017-08-23 14:22:20 -07003336 asTransaction([&](Transaction& t) {
3337 t.show(mChild);
3338 t.setPosition(mChild, 0, 0);
3339 t.setPosition(mFGSurfaceControl, 0, 0);
3340 });
Robert Carr6452f122017-03-21 10:41:29 -07003341
3342 {
chaviw0e3479f2018-09-10 16:49:30 -07003343 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003344 // Unblended child color
3345 mCapture->checkPixel(0, 0, 0, 254, 0);
3346 }
3347
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003348 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003349
3350 {
chaviw0e3479f2018-09-10 16:49:30 -07003351 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003352 // Child and BG blended.
3353 mCapture->checkPixel(0, 0, 127, 127, 0);
3354 }
3355
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003356 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003357
3358 {
chaviw0e3479f2018-09-10 16:49:30 -07003359 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003360 // Child and BG blended.
3361 mCapture->checkPixel(0, 0, 95, 64, 95);
3362 }
3363}
3364
Robert Carr9524cb32017-02-13 11:32:32 -08003365TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003366 asTransaction([&](Transaction& t) {
3367 t.show(mChild);
3368 t.setPosition(mChild, 10, 10);
3369 t.setPosition(mFGSurfaceControl, 64, 64);
3370 });
Robert Carr9524cb32017-02-13 11:32:32 -08003371
3372 {
chaviw0e3479f2018-09-10 16:49:30 -07003373 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003374 // Top left of foreground must now be visible
3375 mCapture->expectFGColor(64, 64);
3376 // But 10 pixels in we should see the child surface
3377 mCapture->expectChildColor(74, 74);
3378 // And 10 more pixels we should be back to the foreground surface
3379 mCapture->expectFGColor(84, 84);
3380 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003381
3382 asTransaction([&](Transaction& t) {
3383 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
3384 });
3385
Robert Carr9524cb32017-02-13 11:32:32 -08003386 {
chaviw0e3479f2018-09-10 16:49:30 -07003387 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003388 mCapture->expectFGColor(64, 64);
3389 // In reparenting we should have exposed the entire foreground surface.
3390 mCapture->expectFGColor(74, 74);
3391 // And the child layer should now begin at 10, 10 (since the BG
3392 // layer is at (0, 0)).
3393 mCapture->expectBGColor(9, 9);
3394 mCapture->expectChildColor(10, 10);
3395 }
3396}
3397
Robert Carr2e102c92018-10-23 12:11:15 -07003398TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
3399 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003400 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07003401 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
3402
3403 {
3404 SCOPED_TRACE("Grandchild visible");
3405 ScreenCapture::captureScreen(&mCapture);
3406 mCapture->checkPixel(64, 64, 111, 111, 111);
3407 }
3408
3409 mChild->clear();
3410
3411 {
3412 SCOPED_TRACE("After destroying child");
3413 ScreenCapture::captureScreen(&mCapture);
3414 mCapture->expectFGColor(64, 64);
3415 }
3416
3417 asTransaction([&](Transaction& t) {
3418 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
3419 });
3420
3421 {
3422 SCOPED_TRACE("After reparenting grandchild");
3423 ScreenCapture::captureScreen(&mCapture);
3424 mCapture->checkPixel(64, 64, 111, 111, 111);
3425 }
3426}
3427
chaviw161410b02017-07-27 10:46:08 -07003428TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003429 asTransaction([&](Transaction& t) {
3430 t.show(mChild);
3431 t.setPosition(mChild, 10, 10);
3432 t.setPosition(mFGSurfaceControl, 64, 64);
3433 });
Robert Carr9524cb32017-02-13 11:32:32 -08003434
3435 {
chaviw0e3479f2018-09-10 16:49:30 -07003436 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003437 // Top left of foreground must now be visible
3438 mCapture->expectFGColor(64, 64);
3439 // But 10 pixels in we should see the child surface
3440 mCapture->expectChildColor(74, 74);
3441 // And 10 more pixels we should be back to the foreground surface
3442 mCapture->expectFGColor(84, 84);
3443 }
3444
chaviw0e3479f2018-09-10 16:49:30 -07003445
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003446 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08003447
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003448 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08003449
chaviw161410b02017-07-27 10:46:08 -07003450 // Since the child has the same client as the parent, it will not get
3451 // detached and will be hidden.
3452 {
chaviw0e3479f2018-09-10 16:49:30 -07003453 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003454 mCapture->expectFGColor(64, 64);
3455 mCapture->expectFGColor(74, 74);
3456 mCapture->expectFGColor(84, 84);
3457 }
3458}
3459
3460TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
3461 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003462 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003463 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
3464 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07003465
chaviw161410b02017-07-27 10:46:08 -07003466 ASSERT_TRUE(mChildNewClient->isValid());
3467
3468 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
3469
Robert Carr4cdc58f2017-08-23 14:22:20 -07003470 asTransaction([&](Transaction& t) {
3471 t.hide(mChild);
3472 t.show(mChildNewClient);
3473 t.setPosition(mChildNewClient, 10, 10);
3474 t.setPosition(mFGSurfaceControl, 64, 64);
3475 });
chaviw161410b02017-07-27 10:46:08 -07003476
3477 {
chaviw0e3479f2018-09-10 16:49:30 -07003478 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003479 // Top left of foreground must now be visible
3480 mCapture->expectFGColor(64, 64);
3481 // But 10 pixels in we should see the child surface
3482 mCapture->expectChildColor(74, 74);
3483 // And 10 more pixels we should be back to the foreground surface
3484 mCapture->expectFGColor(84, 84);
3485 }
3486
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003487 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07003488
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003489 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07003490
Robert Carr9524cb32017-02-13 11:32:32 -08003491 // Nothing should have changed.
3492 {
chaviw0e3479f2018-09-10 16:49:30 -07003493 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003494 mCapture->expectFGColor(64, 64);
3495 mCapture->expectChildColor(74, 74);
3496 mCapture->expectFGColor(84, 84);
3497 }
3498}
3499
chaviw5aedec92018-10-22 10:40:38 -07003500TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
3501 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
3502 sp<SurfaceControl> childNewClient =
3503 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
3504 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3505
3506 ASSERT_TRUE(childNewClient != nullptr);
3507 ASSERT_TRUE(childNewClient->isValid());
3508
3509 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
3510
3511 Transaction()
3512 .hide(mChild)
3513 .show(childNewClient)
3514 .setPosition(childNewClient, 10, 10)
3515 .setPosition(mFGSurfaceControl, 64, 64)
3516 .apply();
3517
3518 {
3519 mCapture = screenshot();
3520 // Top left of foreground must now be visible
3521 mCapture->expectFGColor(64, 64);
3522 // But 10 pixels in we should see the child surface
3523 mCapture->expectChildColor(74, 74);
3524 // And 10 more pixels we should be back to the foreground surface
3525 mCapture->expectFGColor(84, 84);
3526 }
3527
3528 Transaction().detachChildren(mFGSurfaceControl).apply();
3529 Transaction().hide(childNewClient).apply();
3530
3531 // Nothing should have changed.
3532 {
3533 mCapture = screenshot();
3534 mCapture->expectFGColor(64, 64);
3535 mCapture->expectChildColor(74, 74);
3536 mCapture->expectFGColor(84, 84);
3537 }
3538
3539 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
3540 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
3541 32);
3542 Transaction()
3543 .setLayer(newParentSurface, INT32_MAX - 1)
3544 .show(newParentSurface)
3545 .setPosition(newParentSurface, 20, 20)
3546 .reparent(childNewClient, newParentSurface->getHandle())
3547 .apply();
3548 {
3549 mCapture = screenshot();
3550 // Child is now hidden.
3551 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
3552 }
3553}
3554
Robert Carr9b429f42017-04-17 14:56:57 -07003555TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003556 asTransaction([&](Transaction& t) {
3557 t.show(mChild);
3558 t.setPosition(mChild, 0, 0);
3559 t.setPosition(mFGSurfaceControl, 0, 0);
3560 });
Robert Carr9b429f42017-04-17 14:56:57 -07003561
3562 {
chaviw0e3479f2018-09-10 16:49:30 -07003563 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003564 // We've positioned the child in the top left.
3565 mCapture->expectChildColor(0, 0);
3566 // But it's only 10x10.
3567 mCapture->expectFGColor(10, 10);
3568 }
3569
Robert Carr4cdc58f2017-08-23 14:22:20 -07003570 asTransaction([&](Transaction& t) {
3571 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3572 // We cause scaling by 2.
3573 t.setSize(mFGSurfaceControl, 128, 128);
3574 });
Robert Carr9b429f42017-04-17 14:56:57 -07003575
3576 {
chaviw0e3479f2018-09-10 16:49:30 -07003577 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003578 // We've positioned the child in the top left.
3579 mCapture->expectChildColor(0, 0);
3580 mCapture->expectChildColor(10, 10);
3581 mCapture->expectChildColor(19, 19);
3582 // And now it should be scaled all the way to 20x20
3583 mCapture->expectFGColor(20, 20);
3584 }
3585}
3586
Robert Carr1725eee2017-04-26 18:32:15 -07003587// Regression test for b/37673612
3588TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003589 asTransaction([&](Transaction& t) {
3590 t.show(mChild);
3591 t.setPosition(mChild, 0, 0);
3592 t.setPosition(mFGSurfaceControl, 0, 0);
3593 });
Robert Carr1725eee2017-04-26 18:32:15 -07003594
3595 {
chaviw0e3479f2018-09-10 16:49:30 -07003596 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003597 // We've positioned the child in the top left.
3598 mCapture->expectChildColor(0, 0);
3599 // But it's only 10x10.
3600 mCapture->expectFGColor(10, 10);
3601 }
Robert Carr1725eee2017-04-26 18:32:15 -07003602 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
3603 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003604 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07003605 sp<Surface> s = mFGSurfaceControl->getSurface();
3606 auto anw = static_cast<ANativeWindow*>(s.get());
3607 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
3608 native_window_set_buffers_dimensions(anw, 64, 128);
3609 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3610 waitForPostedBuffers();
3611
3612 {
3613 // The child should still be in the same place and not have any strange scaling as in
3614 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07003615 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003616 mCapture->expectChildColor(0, 0);
3617 mCapture->expectFGColor(10, 10);
3618 }
3619}
3620
Dan Stoza412903f2017-04-27 13:42:17 -07003621TEST_F(ChildLayerTest, Bug36858924) {
3622 // Destroy the child layer
3623 mChild.clear();
3624
3625 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08003626 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
3627 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07003628
3629 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07003630 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003631 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
3632 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003633 t.show(mChild);
3634 });
Dan Stoza412903f2017-04-27 13:42:17 -07003635
3636 // Render the foreground surface a few times
3637 //
3638 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
3639 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
3640 // never acquire/release the first buffer
3641 ALOGI("Filling 1");
3642 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3643 ALOGI("Filling 2");
3644 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
3645 ALOGI("Filling 3");
3646 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
3647 ALOGI("Filling 4");
3648 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3649}
3650
chaviwf1961f72017-09-18 16:41:07 -07003651TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003652 asTransaction([&](Transaction& t) {
3653 t.show(mChild);
3654 t.setPosition(mChild, 10, 10);
3655 t.setPosition(mFGSurfaceControl, 64, 64);
3656 });
chaviw06178942017-07-27 10:25:59 -07003657
3658 {
chaviw0e3479f2018-09-10 16:49:30 -07003659 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003660 // Top left of foreground must now be visible
3661 mCapture->expectFGColor(64, 64);
3662 // But 10 pixels in we should see the child surface
3663 mCapture->expectChildColor(74, 74);
3664 // And 10 more pixels we should be back to the foreground surface
3665 mCapture->expectFGColor(84, 84);
3666 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003667
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003668 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07003669
chaviw06178942017-07-27 10:25:59 -07003670 {
chaviw0e3479f2018-09-10 16:49:30 -07003671 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003672 mCapture->expectFGColor(64, 64);
3673 // In reparenting we should have exposed the entire foreground surface.
3674 mCapture->expectFGColor(74, 74);
3675 // And the child layer should now begin at 10, 10 (since the BG
3676 // layer is at (0, 0)).
3677 mCapture->expectBGColor(9, 9);
3678 mCapture->expectChildColor(10, 10);
3679 }
3680}
3681
chaviwf1961f72017-09-18 16:41:07 -07003682TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003683 asTransaction([&](Transaction& t) {
3684 t.show(mChild);
3685 t.setPosition(mChild, 10, 10);
3686 t.setPosition(mFGSurfaceControl, 64, 64);
3687 });
chaviwf1961f72017-09-18 16:41:07 -07003688
3689 {
chaviw0e3479f2018-09-10 16:49:30 -07003690 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003691 // Top left of foreground must now be visible
3692 mCapture->expectFGColor(64, 64);
3693 // But 10 pixels in we should see the child surface
3694 mCapture->expectChildColor(74, 74);
3695 // And 10 more pixels we should be back to the foreground surface
3696 mCapture->expectFGColor(84, 84);
3697 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003698 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07003699 {
chaviw0e3479f2018-09-10 16:49:30 -07003700 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003701 // Nothing should have changed.
3702 mCapture->expectFGColor(64, 64);
3703 mCapture->expectChildColor(74, 74);
3704 mCapture->expectFGColor(84, 84);
3705 }
3706}
3707
3708TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07003709 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003710 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07003711 ASSERT_TRUE(newSurface->isValid());
3712
3713 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003714 asTransaction([&](Transaction& t) {
3715 t.hide(mChild);
3716 t.show(newSurface);
3717 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003718 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003719 t.setPosition(mFGSurfaceControl, 64, 64);
3720 });
chaviwf1961f72017-09-18 16:41:07 -07003721
3722 {
chaviw0e3479f2018-09-10 16:49:30 -07003723 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003724 // Top left of foreground must now be visible
3725 mCapture->expectFGColor(64, 64);
3726 // At 10, 10 we should see the new surface
3727 mCapture->checkPixel(10, 10, 63, 195, 63);
3728 }
3729
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003730 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07003731
3732 {
chaviw0e3479f2018-09-10 16:49:30 -07003733 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003734 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
3735 // mFGSurface, putting it at 74, 74.
3736 mCapture->expectFGColor(64, 64);
3737 mCapture->checkPixel(74, 74, 63, 195, 63);
3738 mCapture->expectFGColor(84, 84);
3739 }
3740}
3741
chaviwc9674332017-08-28 12:32:18 -07003742TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003743 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
3744 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07003745 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3746
3747 {
chaviw0e3479f2018-09-10 16:49:30 -07003748 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07003749 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
3750 // which begins at 64, 64
3751 mCapture->checkPixel(64, 64, 50, 50, 50);
3752 }
3753}
3754
Robert Carr503c7042017-09-27 15:06:08 -07003755TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003756 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07003757 fillSurfaceRGBA8(relative, 255, 255, 255);
3758
3759 Transaction t;
3760 t.setLayer(relative, INT32_MAX)
3761 .setRelativeLayer(mChild, relative->getHandle(), 1)
3762 .setPosition(mFGSurfaceControl, 0, 0)
3763 .apply(true);
3764
3765 // We expect that the child should have been elevated above our
3766 // INT_MAX layer even though it's not a child of it.
3767 {
chaviw0e3479f2018-09-10 16:49:30 -07003768 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07003769 mCapture->expectChildColor(0, 0);
3770 mCapture->expectChildColor(9, 9);
3771 mCapture->checkPixel(10, 10, 255, 255, 255);
3772 }
3773}
Vishnu Nair60356342018-11-13 13:00:45 -08003774class BoundlessLayerTest : public LayerUpdateTest {
3775protected:
3776 std::unique_ptr<ScreenCapture> mCapture;
3777};
3778
3779// Verify setting a size on a buffer layer has no effect.
3780TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
3781 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003782 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
3783 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003784 ASSERT_TRUE(bufferLayer->isValid());
3785 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
3786 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
3787 {
3788 mCapture = screenshot();
3789 // Top left of background must now be visible
3790 mCapture->expectBGColor(0, 0);
3791 // Foreground Surface bounds must be color layer
3792 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
3793 // Buffer layer should not extend past buffer bounds
3794 mCapture->expectFGColor(95, 95);
3795 }
3796}
3797
3798// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
3799// which will crop the color layer.
3800TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
3801 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003802 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3803 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003804 ASSERT_TRUE(colorLayer->isValid());
3805 asTransaction([&](Transaction& t) {
3806 t.setColor(colorLayer, half3{0, 0, 0});
3807 t.show(colorLayer);
3808 });
3809 {
3810 mCapture = screenshot();
3811 // Top left of background must now be visible
3812 mCapture->expectBGColor(0, 0);
3813 // Foreground Surface bounds must be color layer
3814 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3815 // Color layer should not extend past foreground bounds
3816 mCapture->expectBGColor(129, 129);
3817 }
3818}
3819
3820// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
3821// a crop which will be used to crop the color layer.
3822TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003823 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3824 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003825 ASSERT_TRUE(cropLayer->isValid());
3826 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003827 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3828 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003829 ASSERT_TRUE(colorLayer->isValid());
3830 asTransaction([&](Transaction& t) {
3831 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
3832 t.setColor(colorLayer, half3{0, 0, 0});
3833 t.show(cropLayer);
3834 t.show(colorLayer);
3835 });
3836 {
3837 mCapture = screenshot();
3838 // Top left of background must now be visible
3839 mCapture->expectBGColor(0, 0);
3840 // Top left of foreground must now be visible
3841 mCapture->expectFGColor(64, 64);
3842 // 5 pixels from the foreground we should see the child surface
3843 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
3844 // 10 pixels from the foreground we should be back to the foreground surface
3845 mCapture->expectFGColor(74, 74);
3846 }
3847}
3848
3849// Verify for boundless layer with no children, their transforms have no effect.
3850TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
3851 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003852 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3853 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003854 ASSERT_TRUE(colorLayer->isValid());
3855 asTransaction([&](Transaction& t) {
3856 t.setPosition(colorLayer, 320, 320);
3857 t.setMatrix(colorLayer, 2, 0, 0, 2);
3858 t.setColor(colorLayer, half3{0, 0, 0});
3859 t.show(colorLayer);
3860 });
3861 {
3862 mCapture = screenshot();
3863 // Top left of background must now be visible
3864 mCapture->expectBGColor(0, 0);
3865 // Foreground Surface bounds must be color layer
3866 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3867 // Color layer should not extend past foreground bounds
3868 mCapture->expectBGColor(129, 129);
3869 }
3870}
3871
3872// Verify for boundless layer with children, their transforms have an effect.
3873TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
3874 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003875 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3876 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003877 ASSERT_TRUE(boundlessLayerRightShift->isValid());
3878 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003879 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3880 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003881 ASSERT_TRUE(boundlessLayerDownShift->isValid());
3882 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003883 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3884 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003885 ASSERT_TRUE(colorLayer->isValid());
3886 asTransaction([&](Transaction& t) {
3887 t.setPosition(boundlessLayerRightShift, 32, 0);
3888 t.show(boundlessLayerRightShift);
3889 t.setPosition(boundlessLayerDownShift, 0, 32);
3890 t.show(boundlessLayerDownShift);
3891 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3892 t.setColor(colorLayer, half3{0, 0, 0});
3893 t.show(colorLayer);
3894 });
3895 {
3896 mCapture = screenshot();
3897 // Top left of background must now be visible
3898 mCapture->expectBGColor(0, 0);
3899 // Top left of foreground must now be visible
3900 mCapture->expectFGColor(64, 64);
3901 // Foreground Surface bounds must be color layer
3902 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
3903 // Color layer should not extend past foreground bounds
3904 mCapture->expectBGColor(129, 129);
3905 }
3906}
3907
3908// Verify child layers do not get clipped if they temporarily move into the negative
3909// coordinate space as the result of an intermediate transformation.
3910TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
3911 sp<SurfaceControl> boundlessLayer =
3912 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3913 0 /* flags */, mFGSurfaceControl.get());
3914 ASSERT_TRUE(boundlessLayer != nullptr);
3915 ASSERT_TRUE(boundlessLayer->isValid());
3916 sp<SurfaceControl> colorLayer =
3917 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3918 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
3919 ASSERT_TRUE(colorLayer != nullptr);
3920 ASSERT_TRUE(colorLayer->isValid());
3921 asTransaction([&](Transaction& t) {
3922 // shift child layer off bounds. If this layer was not boundless, we will
3923 // expect the child layer to be cropped.
3924 t.setPosition(boundlessLayer, 32, 32);
3925 t.show(boundlessLayer);
3926 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3927 // undo shift by parent
3928 t.setPosition(colorLayer, -32, -32);
3929 t.setColor(colorLayer, half3{0, 0, 0});
3930 t.show(colorLayer);
3931 });
3932 {
3933 mCapture = screenshot();
3934 // Top left of background must now be visible
3935 mCapture->expectBGColor(0, 0);
3936 // Foreground Surface bounds must be color layer
3937 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3938 // Color layer should not extend past foreground bounds
3939 mCapture->expectBGColor(129, 129);
3940 }
3941}
3942
3943// Verify for boundless root layers with children, their transforms have an effect.
3944TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003945 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
3946 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08003947 ASSERT_TRUE(rootBoundlessLayer->isValid());
3948 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003949 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3950 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
3951
Vishnu Nair60356342018-11-13 13:00:45 -08003952 ASSERT_TRUE(colorLayer->isValid());
3953 asTransaction([&](Transaction& t) {
3954 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
3955 t.setPosition(rootBoundlessLayer, 32, 32);
3956 t.show(rootBoundlessLayer);
3957 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3958 t.setColor(colorLayer, half3{0, 0, 0});
3959 t.show(colorLayer);
3960 t.hide(mFGSurfaceControl);
3961 });
3962 {
3963 mCapture = screenshot();
3964 // Top left of background must now be visible
3965 mCapture->expectBGColor(0, 0);
3966 // Top left of foreground must now be visible
3967 mCapture->expectBGColor(31, 31);
3968 // Foreground Surface bounds must be color layer
3969 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
3970 // Color layer should not extend past foreground bounds
3971 mCapture->expectBGColor(97, 97);
3972 }
3973}
Robert Carr503c7042017-09-27 15:06:08 -07003974
chaviwa76b2712017-09-20 12:02:26 -07003975class ScreenCaptureTest : public LayerUpdateTest {
3976protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003977 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07003978};
3979
3980TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
3981 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003982 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003983 mCapture->expectBGColor(0, 0);
3984 // Doesn't capture FG layer which is at 64, 64
3985 mCapture->expectBGColor(64, 64);
3986}
3987
3988TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
3989 auto fgHandle = mFGSurfaceControl->getHandle();
3990
Vishnu Nair88a11f22018-11-28 18:30:57 -08003991 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3992 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003993 fillSurfaceRGBA8(child, 200, 200, 200);
3994
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003995 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003996
3997 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003998 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003999 mCapture->expectFGColor(10, 10);
4000 mCapture->expectChildColor(0, 0);
4001}
4002
Robert Carr578038f2018-03-09 12:25:24 -08004003TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4004 auto fgHandle = mFGSurfaceControl->getHandle();
4005
Vishnu Nair88a11f22018-11-28 18:30:57 -08004006 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4007 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004008 fillSurfaceRGBA8(child, 200, 200, 200);
4009
4010 SurfaceComposerClient::Transaction().show(child).apply(true);
4011
4012 // Captures mFGSurfaceControl's child
4013 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4014 mCapture->checkPixel(10, 10, 0, 0, 0);
4015 mCapture->expectChildColor(0, 0);
4016}
4017
chaviw50da5042018-04-09 13:49:37 -07004018TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004019 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4020 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07004021
4022 fillSurfaceRGBA8(child, 200, 200, 200);
4023
4024 SurfaceComposerClient::Transaction().show(child).apply(true);
4025
4026 auto childHandle = child->getHandle();
4027
4028 // Captures child
4029 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
4030 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
4031 // Area outside of child's bounds is transparent.
4032 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
4033}
4034
chaviw4b129c22018-04-09 16:19:43 -07004035TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
4036 auto fgHandle = mFGSurfaceControl->getHandle();
4037
Vishnu Nair88a11f22018-11-28 18:30:57 -08004038 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4039 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4040 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07004041 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07004042 fillSurfaceRGBA8(child, 200, 200, 200);
4043 fillSurfaceRGBA8(relative, 100, 100, 100);
4044
4045 SurfaceComposerClient::Transaction()
4046 .show(child)
4047 // Set relative layer above fg layer so should be shown above when computing all layers.
4048 .setRelativeLayer(relative, fgHandle, 1)
4049 .show(relative)
4050 .apply(true);
4051
4052 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
4053 ScreenCapture::captureLayers(&mCapture, fgHandle);
4054 mCapture->expectFGColor(10, 10);
4055 mCapture->expectChildColor(0, 0);
4056}
4057
4058TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
4059 auto fgHandle = mFGSurfaceControl->getHandle();
4060
Vishnu Nair88a11f22018-11-28 18:30:57 -08004061 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4062 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4063 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
4064 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07004065 fillSurfaceRGBA8(child, 200, 200, 200);
4066 fillSurfaceRGBA8(relative, 100, 100, 100);
4067
4068 SurfaceComposerClient::Transaction()
4069 .show(child)
4070 // Set relative layer below fg layer but relative to child layer so it should be shown
4071 // above child layer.
4072 .setLayer(relative, -1)
4073 .setRelativeLayer(relative, child->getHandle(), 1)
4074 .show(relative)
4075 .apply(true);
4076
4077 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
4078 // relative value should be taken into account, placing it above child layer.
4079 ScreenCapture::captureLayers(&mCapture, fgHandle);
4080 mCapture->expectFGColor(10, 10);
4081 // Relative layer is showing on top of child layer
4082 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
4083}
Robert Carr578038f2018-03-09 12:25:24 -08004084
4085// In the following tests we verify successful skipping of a parent layer,
4086// so we use the same verification logic and only change how we mutate
4087// the parent layer to verify that various properties are ignored.
4088class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
4089public:
4090 void SetUp() override {
4091 LayerUpdateTest::SetUp();
4092
Vishnu Nair88a11f22018-11-28 18:30:57 -08004093 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4094 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004095 fillSurfaceRGBA8(mChild, 200, 200, 200);
4096
4097 SurfaceComposerClient::Transaction().show(mChild).apply(true);
4098 }
4099
4100 void verify() {
4101 auto fgHandle = mFGSurfaceControl->getHandle();
4102 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4103 mCapture->checkPixel(10, 10, 0, 0, 0);
4104 mCapture->expectChildColor(0, 0);
4105 }
4106
4107 std::unique_ptr<ScreenCapture> mCapture;
4108 sp<SurfaceControl> mChild;
4109};
4110
4111TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4112
4113 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4114
4115 // Even though the parent is hidden we should still capture the child.
4116 verify();
4117}
4118
4119TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004120 SurfaceComposerClient::Transaction()
4121 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
4122 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08004123
4124 // Even though the parent is cropped out we should still capture the child.
4125 verify();
4126}
4127
4128TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4129
4130 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4131
4132 // We should not inherit the parent scaling.
4133 verify();
4134}
4135
Robert Carr15eae092018-03-23 13:43:53 -07004136TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4137 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4138
4139 // Even though the parent is hidden we should still capture the child.
4140 verify();
4141
4142 // Verify everything was properly hidden when rendering the full-screen.
4143 screenshot()->expectBGColor(0,0);
4144}
4145
4146
chaviwa76b2712017-09-20 12:02:26 -07004147TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4148 auto fgHandle = mFGSurfaceControl->getHandle();
4149
Vishnu Nair88a11f22018-11-28 18:30:57 -08004150 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4151 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004152 fillSurfaceRGBA8(child, 200, 200, 200);
4153
Vishnu Nair88a11f22018-11-28 18:30:57 -08004154 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4155 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004156
4157 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4158 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004159 .show(child)
4160 .setPosition(grandchild, 5, 5)
4161 .show(grandchild)
4162 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004163
4164 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004165 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004166 mCapture->expectFGColor(10, 10);
4167 mCapture->expectChildColor(0, 0);
4168 mCapture->checkPixel(5, 5, 50, 50, 50);
4169}
4170
4171TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004172 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4173 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004174 fillSurfaceRGBA8(child, 200, 200, 200);
4175 auto childHandle = child->getHandle();
4176
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004177 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004178
4179 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004180 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004181 mCapture->expectChildColor(0, 0);
4182 mCapture->expectChildColor(9, 9);
4183}
4184
4185TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004186 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4187 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004188 fillSurfaceRGBA8(child, 200, 200, 200);
4189 auto childHandle = child->getHandle();
4190
Vishnu Nair88a11f22018-11-28 18:30:57 -08004191 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4192 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004193 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4194
4195 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004196 .show(child)
4197 .setPosition(grandchild, 5, 5)
4198 .show(grandchild)
4199 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004200
4201 auto grandchildHandle = grandchild->getHandle();
4202
4203 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004204 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004205 mCapture->checkPixel(0, 0, 50, 50, 50);
4206 mCapture->checkPixel(4, 4, 50, 50, 50);
4207}
4208
chaviw7206d492017-11-10 16:16:12 -08004209TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004210 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004211 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4212 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004213
Marissa Wall61c58622018-07-18 10:12:20 -07004214 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4215 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004216
4217 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004218 .setLayer(redLayer, INT32_MAX - 1)
4219 .show(redLayer)
4220 .show(blueLayer)
4221 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004222
4223 auto redLayerHandle = redLayer->getHandle();
4224
4225 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004226 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4227 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4228 // red area below the blue area
4229 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4230 // red area to the right of the blue area
4231 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004232
Marissa Wall861616d2018-10-22 12:52:23 -07004233 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004234 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004235 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4236 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004237 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004238 mCapture->checkPixel(30, 30, 0, 0, 0);
4239}
4240
4241TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004242 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004243 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4244 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004245
Marissa Wall61c58622018-07-18 10:12:20 -07004246 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4247 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004248
4249 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004250 .setLayer(redLayer, INT32_MAX - 1)
4251 .show(redLayer)
4252 .show(blueLayer)
4253 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004254
4255 auto redLayerHandle = redLayer->getHandle();
4256
4257 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004258 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4259 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4260 // red area below the blue area
4261 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4262 // red area to the right of the blue area
4263 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004264
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004265 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004266 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004267 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4268 // red area below the blue area
4269 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4270 // red area to the right of the blue area
4271 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004272 mCapture->checkPixel(30, 30, 0, 0, 0);
4273}
4274
4275TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004276 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004277
Marissa Wall61c58622018-07-18 10:12:20 -07004278 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004279
4280 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07004281 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08004282 SurfaceComposerClient::Transaction().apply(true);
4283
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004284 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004285
4286 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004287 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4288 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004289}
4290
chaviw8e3fe5d2018-02-22 10:55:42 -08004291
4292class DereferenceSurfaceControlTest : public LayerTransactionTest {
4293protected:
4294 void SetUp() override {
4295 LayerTransactionTest::SetUp();
4296 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004297 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004298 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004299 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004300 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4301 {
4302 SCOPED_TRACE("before anything");
4303 auto shot = screenshot();
4304 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4305 }
4306 }
4307 void TearDown() override {
4308 LayerTransactionTest::TearDown();
4309 bgLayer = 0;
4310 fgLayer = 0;
4311 }
4312
4313 sp<SurfaceControl> bgLayer;
4314 sp<SurfaceControl> fgLayer;
4315};
4316
4317TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4318 fgLayer = nullptr;
4319 {
4320 SCOPED_TRACE("after setting null");
4321 auto shot = screenshot();
4322 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4323 }
4324}
4325
4326TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4327 auto transaction = Transaction().show(fgLayer);
4328 fgLayer = nullptr;
4329 {
4330 SCOPED_TRACE("after setting null");
4331 auto shot = screenshot();
4332 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4333 }
4334}
4335
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004336} // namespace android