blob: 037d32faff269ff984d5bf60270767a75ce1c686 [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,
chaviwf66724d2018-11-28 16:35:21 -0800336 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
337 auto layer =
338 createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
Chia-I Wu718daf82017-10-20 11:57:17 -0700339
Vishnu Nair60356342018-11-13 13:00:45 -0800340 Transaction t;
341 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -0800342
343 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700344 if (error != NO_ERROR) {
345 ADD_FAILURE() << "failed to initialize SurfaceControl";
346 layer.clear();
347 }
348
349 return layer;
350 }
351
Vishnu Nair88a11f22018-11-28 18:30:57 -0800352 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
353 const char* name, uint32_t width, uint32_t height,
354 PixelFormat format, uint32_t flags,
355 SurfaceControl* parent = nullptr) {
356 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
357 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
358 return layer;
359 }
360
Marissa Wallfda30bb2018-10-12 11:34:28 -0700361 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800362 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
363 return createLayer(mClient, name, width, height, flags, parent);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700364 }
365
Marissa Wall61c58622018-07-18 10:12:20 -0700366 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700367 // wait for previous transactions (such as setSize) to complete
368 Transaction().apply(true);
369
370 ANativeWindow_Buffer buffer = {};
371 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
372
373 return buffer;
374 }
375
Marissa Wall61c58622018-07-18 10:12:20 -0700376 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700377 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
378
379 // wait for the newly posted buffer to be latched
380 waitForLayerBuffers();
381 }
382
Marissa Wall61c58622018-07-18 10:12:20 -0700383 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
384 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700385 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700386 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
387 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
388 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700389 }
390
Marissa Wall61c58622018-07-18 10:12:20 -0700391 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
392 int32_t bufferWidth, int32_t bufferHeight) {
393 sp<GraphicBuffer> buffer =
394 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
395 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
396 BufferUsage::COMPOSER_OVERLAY,
397 "test");
398 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
Marissa Wall861616d2018-10-22 12:52:23 -0700399 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -0700400 }
401
402 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
403 int32_t bufferWidth, int32_t bufferHeight) {
404 switch (mLayerType) {
405 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
406 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
407 break;
408 case ISurfaceComposerClient::eFXSurfaceBufferState:
409 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
410 break;
411 default:
412 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
413 }
414 }
415
416 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
417 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700418 const Color& topRight, const Color& bottomLeft,
419 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700420 switch (mLayerType) {
421 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
422 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
423 bottomLeft, bottomRight);
424 break;
425 case ISurfaceComposerClient::eFXSurfaceBufferState:
426 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
427 bottomLeft, bottomRight);
428 break;
429 default:
430 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
431 }
432 }
433
434 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
435 int32_t bufferHeight, const Color& topLeft,
436 const Color& topRight, const Color& bottomLeft,
437 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700438 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700439 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
440 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700441
Marissa Wall61c58622018-07-18 10:12:20 -0700442 const int32_t halfW = bufferWidth / 2;
443 const int32_t halfH = bufferHeight / 2;
444 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
445 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
446 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
447 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
448 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700449
Marissa Wall61c58622018-07-18 10:12:20 -0700450 postBufferQueueLayerBuffer(layer);
451 }
452
453 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
454 int32_t bufferHeight, const Color& topLeft,
455 const Color& topRight, const Color& bottomLeft,
456 const Color& bottomRight) {
457 sp<GraphicBuffer> buffer =
458 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
459 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
460 BufferUsage::COMPOSER_OVERLAY,
461 "test");
462
463 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
464
465 const int32_t halfW = bufferWidth / 2;
466 const int32_t halfH = bufferHeight / 2;
467 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
468 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
469 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
470 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
471
472 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700473 }
474
chaviw0e3479f2018-09-10 16:49:30 -0700475 std::unique_ptr<ScreenCapture> screenshot() {
476 std::unique_ptr<ScreenCapture> screenshot;
477 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700478 return screenshot;
479 }
480
481 sp<SurfaceComposerClient> mClient;
482
483 sp<IBinder> mDisplay;
484 uint32_t mDisplayWidth;
485 uint32_t mDisplayHeight;
486 uint32_t mDisplayLayerStack;
Marissa Wall861616d2018-10-22 12:52:23 -0700487 Rect mDisplayRect = Rect::INVALID_RECT;
Chia-I Wu718daf82017-10-20 11:57:17 -0700488
489 // leave room for ~256 layers
490 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
491
Marissa Wall861616d2018-10-22 12:52:23 -0700492 void setRelativeZBasicHelper(uint32_t layerType);
493 void setRelativeZGroupHelper(uint32_t layerType);
494 void setAlphaBasicHelper(uint32_t layerType);
Marissa Wall61c58622018-07-18 10:12:20 -0700495
chaviw0e3479f2018-09-10 16:49:30 -0700496 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700497 bool mColorManagementUsed;
498
Chia-I Wu718daf82017-10-20 11:57:17 -0700499private:
500 void SetUpDisplay() {
501 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
502 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
503
504 // get display width/height
505 DisplayInfo info;
506 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
507 mDisplayWidth = info.w;
508 mDisplayHeight = info.h;
Marissa Wall861616d2018-10-22 12:52:23 -0700509 mDisplayRect =
510 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
Chia-I Wu718daf82017-10-20 11:57:17 -0700511
512 // After a new buffer is queued, SurfaceFlinger is notified and will
513 // latch the new buffer on next vsync. Let's heuristically wait for 3
514 // vsyncs.
515 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
516
517 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700518
Vishnu Nair88a11f22018-11-28 18:30:57 -0800519 mBlackBgSurface =
520 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
521 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700522
Chia-I Wu718daf82017-10-20 11:57:17 -0700523 // set layer stack (b/68888219)
524 Transaction t;
525 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800526 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700527 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
528 t.setColor(mBlackBgSurface, half3{0, 0, 0});
529 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700530 t.apply();
531 }
532
chaviw0e3479f2018-09-10 16:49:30 -0700533 void waitForLayerBuffers() {
534 // Request an empty transaction to get applied synchronously to ensure the buffer is
535 // latched.
536 Transaction().apply(true);
537 usleep(mBufferPostDelay);
538 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700539
540 int32_t mBufferPostDelay;
541};
542
Marissa Wall61c58622018-07-18 10:12:20 -0700543class LayerTypeTransactionTest : public LayerTransactionTest,
544 public ::testing::WithParamInterface<uint32_t> {
545public:
546 LayerTypeTransactionTest() { mLayerType = GetParam(); }
547
548 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800549 uint32_t flags = 0, SurfaceControl* parent = nullptr) override {
Marissa Wall61c58622018-07-18 10:12:20 -0700550 // if the flags already have a layer type specified, return an error
551 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
552 return nullptr;
553 }
chaviwf66724d2018-11-28 16:35:21 -0800554 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
Marissa Wall61c58622018-07-18 10:12:20 -0700555 }
556
557 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
558 int32_t bufferHeight) {
559 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
560 bufferWidth, bufferHeight));
561 }
562
563 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
564 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
565 const Color& bottomLeft, const Color& bottomRight) {
566 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
567 bufferWidth, bufferHeight,
568 topLeft, topRight,
569 bottomLeft, bottomRight));
570 }
571
572protected:
573 uint32_t mLayerType;
574};
575
576INSTANTIATE_TEST_CASE_P(
577 LayerTypeTransactionTests, LayerTypeTransactionTest,
578 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
579 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
580
Marissa Wall861616d2018-10-22 12:52:23 -0700581TEST_F(LayerTransactionTest, SetPositionBasic_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700582 sp<SurfaceControl> layer;
583 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700584 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700585
586 {
587 SCOPED_TRACE("default position");
Marissa Wall861616d2018-10-22 12:52:23 -0700588 const Rect rect(0, 0, 32, 32);
Chia-I Wu718daf82017-10-20 11:57:17 -0700589 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700590 shot->expectColor(rect, Color::RED);
591 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700592 }
593
594 Transaction().setPosition(layer, 5, 10).apply();
595 {
596 SCOPED_TRACE("new position");
Marissa Wall861616d2018-10-22 12:52:23 -0700597 const Rect rect(5, 10, 37, 42);
Chia-I Wu718daf82017-10-20 11:57:17 -0700598 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700599 shot->expectColor(rect, Color::RED);
600 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700601 }
602}
603
Marissa Wall861616d2018-10-22 12:52:23 -0700604TEST_F(LayerTransactionTest, SetPositionRounding_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700605 sp<SurfaceControl> layer;
606 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700607 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700608
609 // GLES requires only 4 bits of subpixel precision during rasterization
610 // XXX GLES composition does not match HWC composition due to precision
611 // loss (b/69315223)
612 const float epsilon = 1.0f / 16.0f;
613 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
614 {
615 SCOPED_TRACE("rounding down");
616 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
617 }
618
619 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
620 {
621 SCOPED_TRACE("rounding up");
622 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
623 }
624}
625
Marissa Wall861616d2018-10-22 12:52:23 -0700626TEST_F(LayerTransactionTest, SetPositionOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700627 sp<SurfaceControl> layer;
628 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700629 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700630
631 Transaction().setPosition(layer, -32, -32).apply();
632 {
633 SCOPED_TRACE("negative coordinates");
Marissa Wall861616d2018-10-22 12:52:23 -0700634 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700635 }
636
637 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
638 {
639 SCOPED_TRACE("positive coordinates");
Marissa Wall861616d2018-10-22 12:52:23 -0700640 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700641 }
642}
643
Marissa Wall861616d2018-10-22 12:52:23 -0700644TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700645 sp<SurfaceControl> layer;
646 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700647 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700648
649 // partially out of bounds
650 Transaction().setPosition(layer, -30, -30).apply();
651 {
652 SCOPED_TRACE("negative coordinates");
653 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
654 }
655
656 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
657 {
658 SCOPED_TRACE("positive coordinates");
659 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
660 mDisplayHeight),
661 Color::RED);
662 }
663}
664
Marissa Wall861616d2018-10-22 12:52:23 -0700665TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700666 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700667 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
668 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700669
670 // setPosition is applied immediately by default, with or without resize
671 // pending
672 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
673 {
674 SCOPED_TRACE("resize pending");
675 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700676 const Rect rect(5, 10, 37, 42);
Marissa Wall61c58622018-07-18 10:12:20 -0700677 shot->expectColor(rect, Color::RED);
678 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700679 }
680
Marissa Wall861616d2018-10-22 12:52:23 -0700681 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700682 {
683 SCOPED_TRACE("resize applied");
684 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
685 }
686}
687
Marissa Wall61c58622018-07-18 10:12:20 -0700688TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700689 sp<SurfaceControl> layer;
690 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700691 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700692
693 // request setPosition to be applied with the next resize
694 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
695 {
696 SCOPED_TRACE("new position pending");
697 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
698 }
699
700 Transaction().setPosition(layer, 15, 20).apply();
701 {
702 SCOPED_TRACE("pending new position modified");
703 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
704 }
705
706 Transaction().setSize(layer, 64, 64).apply();
707 {
708 SCOPED_TRACE("resize pending");
709 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
710 }
711
712 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700713 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700714 {
715 SCOPED_TRACE("new position applied");
716 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
717 }
718}
719
Marissa Wall61c58622018-07-18 10:12:20 -0700720TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700721 sp<SurfaceControl> layer;
722 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700723 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700724
725 // setPosition is not immediate even with SCALE_TO_WINDOW override
726 Transaction()
727 .setPosition(layer, 5, 10)
728 .setSize(layer, 64, 64)
729 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
730 .setGeometryAppliesWithResize(layer)
731 .apply();
732 {
733 SCOPED_TRACE("new position pending");
734 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
735 }
736
Marissa Wall61c58622018-07-18 10:12:20 -0700737 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700738 {
739 SCOPED_TRACE("new position applied");
740 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
741 }
742}
743
Marissa Wall861616d2018-10-22 12:52:23 -0700744TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700745 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700746 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
747 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700748
749 Transaction().setSize(layer, 64, 64).apply();
750 {
751 SCOPED_TRACE("resize pending");
752 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700753 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700754 shot->expectColor(rect, Color::RED);
755 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700756 }
757
Marissa Wall861616d2018-10-22 12:52:23 -0700758 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700759 {
760 SCOPED_TRACE("resize applied");
761 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700762 const Rect rect(0, 0, 64, 64);
763 shot->expectColor(rect, Color::RED);
764 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700765 }
766}
767
Marissa Wall61c58622018-07-18 10:12:20 -0700768TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700769 // cannot test robustness against invalid sizes (zero or really huge)
770}
771
Marissa Wall861616d2018-10-22 12:52:23 -0700772TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700773 sp<SurfaceControl> layer;
774 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700775 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700776
777 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
778 Transaction()
779 .setSize(layer, 64, 64)
780 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
781 .apply();
782 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
783}
784
Marissa Wall61c58622018-07-18 10:12:20 -0700785TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700786 sp<SurfaceControl> layerR;
787 sp<SurfaceControl> layerG;
788 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700789 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700790 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700791 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700792
793 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
794 {
795 SCOPED_TRACE("layerR");
796 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
797 }
798
799 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
800 {
801 SCOPED_TRACE("layerG");
802 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
803 }
804}
805
Marissa Wall61c58622018-07-18 10:12:20 -0700806TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700807 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800808 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700809 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800810 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700811 sp<SurfaceControl> layerR;
812 sp<SurfaceControl> layerG;
813 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700814 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700815 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700816 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700817
chaviw0e3479f2018-09-10 16:49:30 -0700818 Transaction()
819 .reparent(layerR, parent->getHandle())
820 .reparent(layerG, parent->getHandle())
821 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700822 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
823 {
824 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700825 auto shot = screenshot();
826 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700827 }
828
829 Transaction().setLayer(layerR, -3).apply();
830 {
831 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700832 auto shot = screenshot();
833 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700834 }
835}
836
Marissa Wall861616d2018-10-22 12:52:23 -0700837void LayerTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700838 sp<SurfaceControl> layerR;
839 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700840 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
841 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
842 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
843 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700844
Marissa Wall861616d2018-10-22 12:52:23 -0700845 switch (layerType) {
846 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
847 Transaction()
848 .setPosition(layerG, 16, 16)
849 .setRelativeLayer(layerG, layerR->getHandle(), 1)
850 .apply();
851 break;
852 case ISurfaceComposerClient::eFXSurfaceBufferState:
853 Transaction()
854 .setFrame(layerR, Rect(0, 0, 32, 32))
855 .setFrame(layerG, Rect(16, 16, 48, 48))
856 .setRelativeLayer(layerG, layerR->getHandle(), 1)
857 .apply();
858 break;
859 default:
860 ASSERT_FALSE(true) << "Unsupported layer type";
861 }
Chia-I Wu49313302017-10-31 10:14:40 -0700862 {
863 SCOPED_TRACE("layerG above");
864 auto shot = screenshot();
865 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
866 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
867 }
868
869 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
870 {
871 SCOPED_TRACE("layerG below");
872 auto shot = screenshot();
873 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
874 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
875 }
876}
877
Marissa Wall861616d2018-10-22 12:52:23 -0700878TEST_F(LayerTransactionTest, SetRelativeZBasic_BufferQueue) {
879 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
880}
881
882TEST_F(LayerTransactionTest, SetRelativeZBasic_BufferState) {
883 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
884}
885
Marissa Wall61c58622018-07-18 10:12:20 -0700886TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700887 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800888 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700889 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800890 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800891 sp<SurfaceControl> layerR;
892 sp<SurfaceControl> layerG;
893 sp<SurfaceControl> layerB;
894 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700895 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800896 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700897 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800898 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700899 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800900
chaviw0e3479f2018-09-10 16:49:30 -0700901 Transaction()
902 .reparent(layerB, parent->getHandle())
903 .apply();
904
Chia-I Wuec2d9852017-11-21 09:21:01 -0800905 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
906 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
907
chaviw0e3479f2018-09-10 16:49:30 -0700908 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800909 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700910 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -0700911 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800912 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
913}
914
Marissa Wall861616d2018-10-22 12:52:23 -0700915void LayerTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700916 sp<SurfaceControl> layerR;
917 sp<SurfaceControl> layerG;
918 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -0700919 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
920 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
921 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
922 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
923 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
924 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700925
926 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -0700927 switch (layerType) {
928 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
929 Transaction()
930 .setPosition(layerG, 8, 8)
931 .setRelativeLayer(layerG, layerR->getHandle(), 3)
932 .setPosition(layerB, 16, 16)
933 .setLayer(layerB, mLayerZBase + 2)
934 .apply();
935 break;
936 case ISurfaceComposerClient::eFXSurfaceBufferState:
937 Transaction()
938 .setFrame(layerR, Rect(0, 0, 32, 32))
939 .setFrame(layerG, Rect(8, 8, 40, 40))
940 .setRelativeLayer(layerG, layerR->getHandle(), 3)
941 .setFrame(layerB, Rect(16, 16, 48, 48))
942 .setLayer(layerB, mLayerZBase + 2)
943 .apply();
944 break;
945 default:
946 ASSERT_FALSE(true) << "Unsupported layer type";
947 }
948
Chia-I Wu49313302017-10-31 10:14:40 -0700949 {
950 SCOPED_TRACE("(layerR < layerG) < layerB");
951 auto shot = screenshot();
952 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
953 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
954 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
955 }
956
957 // layerR = 4, layerG = layerR + 3, layerB = 2
958 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
959 {
960 SCOPED_TRACE("layerB < (layerR < layerG)");
961 auto shot = screenshot();
962 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
963 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
964 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
965 }
966
967 // layerR = 4, layerG = layerR - 3, layerB = 2
968 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
969 {
970 SCOPED_TRACE("layerB < (layerG < layerR)");
971 auto shot = screenshot();
972 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
973 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
974 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
975 }
976
977 // restore to absolute z
978 // layerR = 4, layerG = 0, layerB = 2
979 Transaction().setLayer(layerG, mLayerZBase).apply();
980 {
981 SCOPED_TRACE("layerG < layerB < layerR");
982 auto shot = screenshot();
983 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
984 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
985 }
986
987 // layerR should not affect layerG anymore
988 // layerR = 1, layerG = 0, layerB = 2
989 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
990 {
991 SCOPED_TRACE("layerG < layerR < layerB");
992 auto shot = screenshot();
993 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
994 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
995 }
996}
997
Marissa Wall861616d2018-10-22 12:52:23 -0700998TEST_F(LayerTransactionTest, SetRelativeZGroup_BufferQueue) {
999 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1000}
1001
1002TEST_F(LayerTransactionTest, SetRelativeZGroup_BufferState) {
1003 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1004}
1005
Marissa Wall61c58622018-07-18 10:12:20 -07001006TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -07001007 sp<SurfaceControl> layerR;
1008 sp<SurfaceControl> layerG;
1009
1010 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001011 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001012 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001013 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001014
1015 Transaction()
1016 .setPosition(layerG, 16, 16)
1017 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1018 .apply();
1019
1020 mClient->destroySurface(layerG->getHandle());
1021 // layerG should have been removed
1022 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1023}
1024
Marissa Wall61c58622018-07-18 10:12:20 -07001025TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001026 sp<SurfaceControl> layer;
1027 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001028 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001029
1030 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1031 {
1032 SCOPED_TRACE("layer hidden");
Marissa Wall861616d2018-10-22 12:52:23 -07001033 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -07001034 }
1035
1036 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1037 {
1038 SCOPED_TRACE("layer shown");
1039 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1040 }
1041}
1042
Marissa Wall61c58622018-07-18 10:12:20 -07001043TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001044 const Color translucentRed = {100, 0, 0, 100};
1045 sp<SurfaceControl> layerR;
1046 sp<SurfaceControl> layerG;
1047 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001048 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001049 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001050 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001051
1052 Transaction()
1053 .setLayer(layerR, mLayerZBase + 1)
1054 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1055 .apply();
1056 {
1057 SCOPED_TRACE("layerR opaque");
1058 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1059 }
1060
1061 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1062 {
1063 SCOPED_TRACE("layerR translucent");
1064 const uint8_t g = uint8_t(255 - translucentRed.a);
1065 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1066 }
1067}
1068
Marissa Wall61c58622018-07-18 10:12:20 -07001069TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001070 sp<SurfaceControl> layer;
1071 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001072 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001073
1074 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001075 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001076 Transaction()
1077 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1078 .apply(true);
1079 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001080 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001081
1082 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1083 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001084 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001085}
1086
Marissa Wall61c58622018-07-18 10:12:20 -07001087TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001088 const Rect top(0, 0, 32, 16);
1089 const Rect bottom(0, 16, 32, 32);
1090 sp<SurfaceControl> layer;
1091 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1092
1093 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001094 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1095 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1096 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001097 // setTransparentRegionHint always applies to the following buffer
1098 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001099 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001100 {
1101 SCOPED_TRACE("top transparent");
1102 auto shot = screenshot();
1103 shot->expectColor(top, Color::BLACK);
1104 shot->expectColor(bottom, Color::RED);
1105 }
1106
1107 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1108 {
1109 SCOPED_TRACE("transparent region hint pending");
1110 auto shot = screenshot();
1111 shot->expectColor(top, Color::BLACK);
1112 shot->expectColor(bottom, Color::RED);
1113 }
1114
Marissa Wall61c58622018-07-18 10:12:20 -07001115 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1116 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1117 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1118 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001119 {
1120 SCOPED_TRACE("bottom transparent");
1121 auto shot = screenshot();
1122 shot->expectColor(top, Color::RED);
1123 shot->expectColor(bottom, Color::BLACK);
1124 }
1125}
1126
Marissa Wall61c58622018-07-18 10:12:20 -07001127TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1128 const Rect top(0, 0, 32, 16);
1129 const Rect bottom(0, 16, 32, 32);
1130 sp<SurfaceControl> layer;
1131 ASSERT_NO_FATAL_FAILURE(
1132 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1133
1134 sp<GraphicBuffer> buffer =
1135 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1136 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1137 BufferUsage::COMPOSER_OVERLAY,
1138 "test");
1139
1140 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1141 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1142 Transaction()
1143 .setTransparentRegionHint(layer, Region(top))
1144 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001145 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001146 .apply();
1147 {
1148 SCOPED_TRACE("top transparent");
1149 auto shot = screenshot();
1150 shot->expectColor(top, Color::BLACK);
1151 shot->expectColor(bottom, Color::RED);
1152 }
1153
1154 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1155 {
1156 SCOPED_TRACE("transparent region hint intermediate");
1157 auto shot = screenshot();
1158 shot->expectColor(top, Color::BLACK);
1159 shot->expectColor(bottom, Color::BLACK);
1160 }
1161
1162 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1163 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1164 BufferUsage::COMPOSER_OVERLAY,
1165 "test");
1166
1167 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1168 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001169 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001170 {
1171 SCOPED_TRACE("bottom transparent");
1172 auto shot = screenshot();
1173 shot->expectColor(top, Color::RED);
1174 shot->expectColor(bottom, Color::BLACK);
1175 }
1176}
1177
Marissa Wall861616d2018-10-22 12:52:23 -07001178TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001179 sp<SurfaceControl> layerTransparent;
1180 sp<SurfaceControl> layerR;
1181 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1182 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1183
1184 // check that transparent region hint is bound by the layer size
1185 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001186 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001187 .setPosition(layerR, 16, 16)
1188 .setLayer(layerR, mLayerZBase + 1)
1189 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001190 ASSERT_NO_FATAL_FAILURE(
1191 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1192 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001193 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1194}
1195
Marissa Wall861616d2018-10-22 12:52:23 -07001196TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
1197 sp<SurfaceControl> layerTransparent;
1198 sp<SurfaceControl> layerR;
1199 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1200 ASSERT_NO_FATAL_FAILURE(
1201 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1202
1203 // check that transparent region hint is bound by the layer size
1204 Transaction()
1205 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1206 .setFrame(layerR, Rect(16, 16, 48, 48))
1207 .setLayer(layerR, mLayerZBase + 1)
1208 .apply();
1209 ASSERT_NO_FATAL_FAILURE(
1210 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1211 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
1212 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1213}
1214
1215void LayerTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001216 sp<SurfaceControl> layer1;
1217 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001218 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1219 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1220 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1221 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001222
Marissa Wall861616d2018-10-22 12:52:23 -07001223 switch (layerType) {
1224 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1225 Transaction()
1226 .setAlpha(layer1, 0.25f)
1227 .setAlpha(layer2, 0.75f)
1228 .setPosition(layer2, 16, 0)
1229 .setLayer(layer2, mLayerZBase + 1)
1230 .apply();
1231 break;
1232 case ISurfaceComposerClient::eFXSurfaceBufferState:
1233 Transaction()
1234 .setAlpha(layer1, 0.25f)
1235 .setAlpha(layer2, 0.75f)
1236 .setFrame(layer1, Rect(0, 0, 32, 32))
1237 .setFrame(layer2, Rect(16, 0, 48, 32))
1238 .setLayer(layer2, mLayerZBase + 1)
1239 .apply();
1240 break;
1241 default:
1242 ASSERT_FALSE(true) << "Unsupported layer type";
1243 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001244 {
1245 auto shot = screenshot();
1246 uint8_t r = 16; // 64 * 0.25f
1247 uint8_t g = 48; // 64 * 0.75f
1248 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1249 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1250
1251 r /= 4; // r * (1.0f - 0.75f)
1252 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1253 }
1254}
1255
Marissa Wall861616d2018-10-22 12:52:23 -07001256TEST_F(LayerTransactionTest, SetAlphaBasic_BufferQueue) {
1257 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1258}
1259
1260TEST_F(LayerTransactionTest, SetAlphaBasic_BufferState) {
1261 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1262}
1263
Marissa Wall61c58622018-07-18 10:12:20 -07001264TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001265 const Color color = {64, 0, 0, 255};
1266 sp<SurfaceControl> layer;
1267 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001268 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001269
1270 Transaction().setAlpha(layer, 2.0f).apply();
1271 {
1272 SCOPED_TRACE("clamped to 1.0f");
1273 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1274 }
1275
1276 Transaction().setAlpha(layer, -1.0f).apply();
1277 {
1278 SCOPED_TRACE("clamped to 0.0f");
1279 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1280 }
1281}
1282
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001283TEST_P(LayerTypeTransactionTest, SetCornerRadius) {
1284 sp<SurfaceControl> layer;
1285 const uint8_t size = 64;
1286 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001287 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001288 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1289 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1290
1291 Transaction()
1292 .setCornerRadius(layer, cornerRadius)
1293 .apply();
1294 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001295 const uint8_t bottom = size - 1;
1296 const uint8_t right = size - 1;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001297 auto shot = screenshot();
1298 // Transparent corners
1299 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001300 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1301 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1302 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1303 }
1304}
1305
1306TEST_P(LayerTypeTransactionTest, SetCornerRadiusChildCrop) {
1307 sp<SurfaceControl> parent;
1308 sp<SurfaceControl> child;
1309 const uint8_t size = 64;
1310 const uint8_t testArea = 4;
1311 const float cornerRadius = 20.0f;
1312 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1313 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1314 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1315 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1316
1317 Transaction()
1318 .setCornerRadius(parent, cornerRadius)
1319 .reparent(child, parent->getHandle())
1320 .setPosition(child, 0, size / 2)
1321 .apply();
1322 {
1323 const uint8_t bottom = size - 1;
1324 const uint8_t right = size - 1;
1325 auto shot = screenshot();
1326 // Top edge of child should not have rounded corners because it's translated in the parent
1327 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1328 Color::GREEN);
1329 // But bottom edges should have been clipped according to parent bounds
1330 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1331 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001332 }
1333}
1334
Chia-I Wue4ef6102017-11-01 15:16:35 -07001335TEST_F(LayerTransactionTest, SetColorBasic) {
1336 sp<SurfaceControl> bufferLayer;
1337 sp<SurfaceControl> colorLayer;
1338 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001339 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001340 ASSERT_NO_FATAL_FAILURE(colorLayer =
1341 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1342 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001343
Vishnu Nair88a11f22018-11-28 18:30:57 -08001344 Transaction()
1345 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1346 .setLayer(colorLayer, mLayerZBase + 1)
1347 .apply();
1348
Chia-I Wue4ef6102017-11-01 15:16:35 -07001349 {
1350 SCOPED_TRACE("default color");
1351 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1352 }
1353
1354 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1355 const Color expected = {15, 51, 85, 255};
1356 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1357 // channel) should be less than one
1358 const uint8_t tolerance = 1;
1359 Transaction().setColor(colorLayer, color).apply();
1360 {
1361 SCOPED_TRACE("new color");
1362 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1363 }
1364}
1365
1366TEST_F(LayerTransactionTest, SetColorClamped) {
1367 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001368 ASSERT_NO_FATAL_FAILURE(colorLayer =
1369 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1370 ISurfaceComposerClient::eFXSurfaceColor));
1371 Transaction()
1372 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1373 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1374 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001375
Chia-I Wue4ef6102017-11-01 15:16:35 -07001376 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1377}
1378
1379TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1380 sp<SurfaceControl> bufferLayer;
1381 sp<SurfaceControl> colorLayer;
1382 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001383 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001384 ASSERT_NO_FATAL_FAILURE(colorLayer =
1385 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1386 ISurfaceComposerClient::eFXSurfaceColor));
1387 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001388
1389 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1390 const float alpha = 0.25f;
1391 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1392 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1393 // channel) should be less than one
1394 const uint8_t tolerance = 1;
1395 Transaction()
1396 .setColor(colorLayer, color)
1397 .setAlpha(colorLayer, alpha)
1398 .setLayer(colorLayer, mLayerZBase + 1)
1399 .apply();
1400 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1401 tolerance);
1402}
1403
Adrian Roosb7a96502018-04-08 11:38:55 -07001404TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1405 sp<SurfaceControl> bufferLayer;
1406 sp<SurfaceControl> parentLayer;
1407 sp<SurfaceControl> colorLayer;
1408 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1409 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001410 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001411 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1412 0 /* buffer height */,
1413 ISurfaceComposerClient::eFXSurfaceColor));
1414 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001415 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1416 const float alpha = 0.25f;
1417 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1418 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1419 // channel) should be less than one
1420 const uint8_t tolerance = 1;
1421 Transaction()
1422 .reparent(colorLayer, parentLayer->getHandle())
1423 .setColor(colorLayer, color)
1424 .setAlpha(parentLayer, alpha)
1425 .setLayer(parentLayer, mLayerZBase + 1)
1426 .apply();
1427 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1428 tolerance);
1429}
1430
Marissa Wall61c58622018-07-18 10:12:20 -07001431TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001432 sp<SurfaceControl> bufferLayer;
1433 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001434 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001435
1436 // color is ignored
1437 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1438 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1439}
1440
Marissa Wall61c58622018-07-18 10:12:20 -07001441TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001442 sp<SurfaceControl> layer;
1443 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001444 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001445
1446 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1447 {
1448 SCOPED_TRACE("non-existing layer stack");
Marissa Wall861616d2018-10-22 12:52:23 -07001449 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001450 }
1451
1452 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1453 {
1454 SCOPED_TRACE("original layer stack");
1455 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1456 }
1457}
1458
Marissa Wall861616d2018-10-22 12:52:23 -07001459TEST_F(LayerTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001460 sp<SurfaceControl> layer;
1461 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001462 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1463 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001464
1465 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1466 {
1467 SCOPED_TRACE("IDENTITY");
1468 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1469 Color::WHITE);
1470 }
1471
1472 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1473 {
1474 SCOPED_TRACE("FLIP_H");
1475 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1476 Color::BLUE);
1477 }
1478
1479 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1480 {
1481 SCOPED_TRACE("FLIP_V");
1482 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1483 Color::GREEN);
1484 }
1485
1486 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1487 {
1488 SCOPED_TRACE("ROT_90");
1489 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1490 Color::GREEN);
1491 }
1492
1493 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1494 {
1495 SCOPED_TRACE("SCALE");
1496 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1497 Color::WHITE, true /* filtered */);
1498 }
1499}
1500
Marissa Wall861616d2018-10-22 12:52:23 -07001501TEST_F(LayerTransactionTest, SetMatrixBasic_BufferState) {
1502 sp<SurfaceControl> layer;
1503 ASSERT_NO_FATAL_FAILURE(
1504 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1505 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1506 Color::BLUE, Color::WHITE));
1507
1508 Transaction()
1509 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1510 .setFrame(layer, Rect(0, 0, 32, 32))
1511 .apply();
1512 {
1513 SCOPED_TRACE("IDENTITY");
1514 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1515 Color::WHITE);
1516 }
1517
1518 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1519 {
1520 SCOPED_TRACE("FLIP_H");
1521 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1522 Color::WHITE);
1523 }
1524
1525 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1526 {
1527 SCOPED_TRACE("FLIP_V");
1528 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1529 Color::WHITE);
1530 }
1531
1532 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1533 {
1534 SCOPED_TRACE("ROT_90");
1535 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1536 Color::WHITE);
1537 }
1538
1539 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1540 {
1541 SCOPED_TRACE("SCALE");
1542 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1543 Color::WHITE);
1544 }
1545}
1546
1547TEST_F(LayerTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001548 sp<SurfaceControl> layer;
1549 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001550 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1551 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001552
1553 const float rot = M_SQRT1_2; // 45 degrees
1554 const float trans = M_SQRT2 * 16.0f;
1555 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1556
1557 auto shot = screenshot();
1558 // check a 8x8 region inside each color
1559 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1560 const int32_t halfL = 4;
1561 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1562 };
1563 const int32_t unit = int32_t(trans / 2);
1564 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1565 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1566 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1567 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1568}
1569
Marissa Wall861616d2018-10-22 12:52:23 -07001570TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001571 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001572 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1573 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001574
1575 // setMatrix is applied after any pending resize, unlike setPosition
1576 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1577 {
1578 SCOPED_TRACE("resize pending");
1579 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001580 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001581 shot->expectColor(rect, Color::RED);
1582 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001583 }
1584
Marissa Wall861616d2018-10-22 12:52:23 -07001585 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001586 {
1587 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001588 const Rect rect(0, 0, 128, 128);
1589 screenshot()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001590 }
1591}
1592
Marissa Wall861616d2018-10-22 12:52:23 -07001593TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001594 sp<SurfaceControl> layer;
1595 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001596 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001597
1598 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1599 Transaction()
1600 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1601 .setSize(layer, 64, 64)
1602 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1603 .apply();
1604 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1605}
1606
Marissa Wall861616d2018-10-22 12:52:23 -07001607TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001608 sp<SurfaceControl> layer;
1609 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001610 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1611 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001612
1613 // XXX SCALE_CROP is not respected; calling setSize and
1614 // setOverrideScalingMode in separate transactions does not work
1615 // (b/69315456)
1616 Transaction()
1617 .setSize(layer, 64, 16)
1618 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1619 .apply();
1620 {
1621 SCOPED_TRACE("SCALE_TO_WINDOW");
1622 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1623 Color::WHITE, true /* filtered */);
1624 }
1625}
1626
Dan Stoza000dd012018-08-01 13:31:52 -07001627TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1628 sp<SurfaceControl> layer;
1629 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1630
1631 sp<IBinder> handle = layer->getHandle();
1632 ASSERT_TRUE(handle != nullptr);
1633
1634 FrameStats frameStats;
1635 mClient->getLayerFrameStats(handle, &frameStats);
1636
1637 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1638}
1639
Marissa Wall61c58622018-07-18 10:12:20 -07001640TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001641 sp<SurfaceControl> layer;
1642 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001643 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001644 const Rect crop(8, 8, 24, 24);
1645
Marissa Wallf58c14b2018-07-24 10:50:43 -07001646 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001647 auto shot = screenshot();
1648 shot->expectColor(crop, Color::RED);
1649 shot->expectBorder(crop, Color::BLACK);
1650}
1651
Marissa Wall61c58622018-07-18 10:12:20 -07001652TEST_F(LayerTransactionTest, SetCropBasic_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 const Rect crop(8, 8, 24, 24);
1658
1659 Transaction().setCrop(layer, crop).apply();
1660 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001661 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1662 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001663}
1664
1665TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001666 sp<SurfaceControl> layer;
1667 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001668 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001669
1670 {
1671 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001672 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001673 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1674 }
1675
1676 {
1677 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001678 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001679 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1680 }
1681}
1682
Marissa Wall61c58622018-07-18 10:12:20 -07001683TEST_F(LayerTransactionTest, SetCropEmpty_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 {
1690 SCOPED_TRACE("empty rect");
1691 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1692 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1693 }
1694
1695 {
1696 SCOPED_TRACE("negative rect");
1697 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1698 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1699 }
1700}
1701
1702TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001703 sp<SurfaceControl> layer;
1704 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001705 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001706
Marissa Wallf58c14b2018-07-24 10:50:43 -07001707 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001708 auto shot = screenshot();
1709 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1710 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1711}
1712
Valerie Hau0bc09152018-12-20 07:42:47 -08001713TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1714 sp<SurfaceControl> layer;
1715 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", mDisplayWidth, mDisplayHeight / 2,
1716 ISurfaceComposerClient::eFXSurfaceBufferState));
1717 sp<GraphicBuffer> buffer =
1718 new GraphicBuffer(mDisplayWidth, mDisplayHeight / 2, PIXEL_FORMAT_RGBA_8888, 1,
1719 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1720 BufferUsage::COMPOSER_OVERLAY,
1721 "test");
1722 fillGraphicBufferColor(buffer, Rect(0, 0, mDisplayWidth, mDisplayHeight / 4), Color::BLUE);
1723 fillGraphicBufferColor(buffer, Rect(0, mDisplayHeight / 4, mDisplayWidth, mDisplayHeight / 2),
1724 Color::RED);
1725
1726 Transaction().setBuffer(layer, buffer).apply();
1727
1728 // Partially out of bounds in the negative (upper left) direction
1729 Transaction().setCrop(layer, Rect(-128, -128, mDisplayWidth, mDisplayHeight / 4)).apply();
1730 {
1731 SCOPED_TRACE("out of bounds, negative (upper left) direction");
1732 auto shot = screenshot();
1733 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLUE);
1734 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLACK);
1735 }
1736
1737 // Partially out of bounds in the positive (lower right) direction
1738 Transaction()
1739 .setCrop(layer, Rect(0, mDisplayHeight / 4, mDisplayWidth + 1, mDisplayHeight))
1740 .apply();
1741 {
1742 SCOPED_TRACE("out of bounds, positive (lower right) direction");
1743 auto shot = screenshot();
1744 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::RED);
1745 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLACK);
1746 }
1747
1748 // Fully out of buffer space bounds
1749 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
1750 {
1751 SCOPED_TRACE("Fully out of bounds");
1752 auto shot = screenshot();
1753 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 4), Color::BLUE);
1754 shot->expectColor(Rect(0, mDisplayHeight / 4, mDisplayWidth, mDisplayHeight / 2),
1755 Color::RED);
1756 }
1757}
1758
Marissa Wall61c58622018-07-18 10:12:20 -07001759TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001760 sp<SurfaceControl> layer;
1761 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001762 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001763
1764 const Point position(32, 32);
1765 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001766 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001767 auto shot = screenshot();
1768 shot->expectColor(crop + position, Color::RED);
1769 shot->expectBorder(crop + position, Color::BLACK);
1770}
1771
Marissa Wall61c58622018-07-18 10:12:20 -07001772TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1773 sp<SurfaceControl> layer;
1774 ASSERT_NO_FATAL_FAILURE(
1775 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1776 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1777
Marissa Wall861616d2018-10-22 12:52:23 -07001778 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001779 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001780 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001781 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001782 shot->expectColor(frame, Color::RED);
1783 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001784}
1785
1786TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001787 sp<SurfaceControl> layer;
1788 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001789 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001790
Marissa Wall861616d2018-10-22 12:52:23 -07001791 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001792 Transaction()
1793 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001794 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001795 .apply();
1796 auto shot = screenshot();
1797 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1798 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1799}
1800
Marissa Wall61c58622018-07-18 10:12:20 -07001801TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001802 sp<SurfaceControl> layer;
1803 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001804 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001805
Marissa Wallf58c14b2018-07-24 10:50:43 -07001806 // setCrop_legacy is applied immediately by default, with or without resize pending
1807 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001808 {
1809 SCOPED_TRACE("resize pending");
1810 auto shot = screenshot();
1811 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1812 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1813 }
1814
Marissa Wall61c58622018-07-18 10:12:20 -07001815 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001816 {
1817 SCOPED_TRACE("resize applied");
1818 auto shot = screenshot();
1819 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1820 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1821 }
1822}
1823
Marissa Wall61c58622018-07-18 10:12:20 -07001824TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001825 sp<SurfaceControl> layer;
1826 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001827 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001828
Marissa Wallf58c14b2018-07-24 10:50:43 -07001829 // request setCrop_legacy to be applied with the next resize
1830 Transaction()
1831 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1832 .setGeometryAppliesWithResize(layer)
1833 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001834 {
1835 SCOPED_TRACE("waiting for next resize");
1836 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1837 }
1838
Marissa Wallf58c14b2018-07-24 10:50:43 -07001839 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001840 {
1841 SCOPED_TRACE("pending crop modified");
1842 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1843 }
1844
1845 Transaction().setSize(layer, 16, 16).apply();
1846 {
1847 SCOPED_TRACE("resize pending");
1848 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1849 }
1850
1851 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001852 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001853 {
1854 SCOPED_TRACE("new crop applied");
1855 auto shot = screenshot();
1856 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1857 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1858 }
1859}
1860
Marissa Wall61c58622018-07-18 10:12:20 -07001861TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001862 sp<SurfaceControl> layer;
1863 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001864 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001865
Marissa Wallf58c14b2018-07-24 10:50:43 -07001866 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001867 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001868 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001869 .setSize(layer, 16, 16)
1870 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1871 .setGeometryAppliesWithResize(layer)
1872 .apply();
1873 {
1874 SCOPED_TRACE("new crop pending");
1875 auto shot = screenshot();
1876 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1877 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1878 }
1879
1880 // XXX crop is never latched without other geometry change (b/69315677)
1881 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001882 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001883 Transaction().setPosition(layer, 0, 0).apply();
1884 {
1885 SCOPED_TRACE("new crop applied");
1886 auto shot = screenshot();
1887 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1888 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1889 }
1890}
1891
Marissa Wall861616d2018-10-22 12:52:23 -07001892TEST_F(LayerTransactionTest, SetFrameBasic_BufferState) {
1893 sp<SurfaceControl> layer;
1894 ASSERT_NO_FATAL_FAILURE(
1895 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1896 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1897 const Rect frame(8, 8, 24, 24);
1898
1899 Transaction().setFrame(layer, frame).apply();
1900 auto shot = screenshot();
1901 shot->expectColor(frame, Color::RED);
1902 shot->expectBorder(frame, Color::BLACK);
1903}
1904
1905TEST_F(LayerTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001906 sp<SurfaceControl> layer;
1907 ASSERT_NO_FATAL_FAILURE(
1908 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1909 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1910
Marissa Wall61c58622018-07-18 10:12:20 -07001911 {
Marissa Wall861616d2018-10-22 12:52:23 -07001912 SCOPED_TRACE("empty rect");
1913 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
1914 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001915 }
1916
Marissa Wall61c58622018-07-18 10:12:20 -07001917 {
Marissa Wall861616d2018-10-22 12:52:23 -07001918 SCOPED_TRACE("negative rect");
1919 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
1920 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001921 }
1922}
1923
Marissa Wall861616d2018-10-22 12:52:23 -07001924TEST_F(LayerTransactionTest, SetFrameDefaultParentless_BufferState) {
1925 sp<SurfaceControl> layer;
1926 ASSERT_NO_FATAL_FAILURE(
1927 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1928 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
1929
1930 // A parentless layer will default to a frame with the same size as the buffer
1931 auto shot = screenshot();
1932 shot->expectColor(Rect(0, 0, 10, 10), Color::RED);
1933 shot->expectBorder(Rect(0, 0, 10, 10), Color::BLACK);
1934}
1935
1936TEST_F(LayerTransactionTest, SetFrameDefaultBSParent_BufferState) {
1937 sp<SurfaceControl> parent, child;
1938 ASSERT_NO_FATAL_FAILURE(
1939 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1940 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
1941 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
1942
1943 ASSERT_NO_FATAL_FAILURE(
1944 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1945 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1946
1947 Transaction().reparent(child, parent->getHandle()).apply();
1948
1949 // A layer will default to the frame of its parent
1950 auto shot = screenshot();
1951 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1952 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1953}
1954
1955TEST_F(LayerTransactionTest, SetFrameDefaultBQParent_BufferState) {
1956 sp<SurfaceControl> parent, child;
1957 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
1958 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
1959
1960 ASSERT_NO_FATAL_FAILURE(
1961 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1962 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1963
1964 Transaction().reparent(child, parent->getHandle()).apply();
1965
1966 // A layer will default to the frame of its parent
1967 auto shot = screenshot();
1968 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1969 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1970}
1971
1972TEST_F(LayerTransactionTest, SetFrameUpdate_BufferState) {
1973 sp<SurfaceControl> layer;
1974 ASSERT_NO_FATAL_FAILURE(
1975 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1976 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1977 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
1978
1979 std::this_thread::sleep_for(500ms);
1980
1981 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
1982
1983 auto shot = screenshot();
1984 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1985 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1986}
1987
1988TEST_F(LayerTransactionTest, SetFrameOutsideBounds_BufferState) {
1989 sp<SurfaceControl> parent, child;
1990 ASSERT_NO_FATAL_FAILURE(
1991 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1992 ASSERT_NO_FATAL_FAILURE(
1993 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1994 Transaction().reparent(child, parent->getHandle()).apply();
1995
1996 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
1997 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
1998
1999 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2000 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2001
2002 auto shot = screenshot();
2003 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2004 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2005 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2006}
2007
Marissa Wall61c58622018-07-18 10:12:20 -07002008TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
2009 sp<SurfaceControl> layer;
2010 ASSERT_NO_FATAL_FAILURE(
2011 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2012
2013 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2014
2015 auto shot = screenshot();
2016 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2017 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2018}
2019
2020TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
2021 sp<SurfaceControl> layer;
2022 ASSERT_NO_FATAL_FAILURE(
2023 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2024
2025 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2026
2027 {
2028 SCOPED_TRACE("set buffer 1");
2029 auto shot = screenshot();
2030 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2031 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2032 }
2033
2034 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2035
2036 {
2037 SCOPED_TRACE("set buffer 2");
2038 auto shot = screenshot();
2039 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2040 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2041 }
2042
2043 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2044
2045 {
2046 SCOPED_TRACE("set buffer 3");
2047 auto shot = screenshot();
2048 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2049 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2050 }
2051}
2052
2053TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
2054 sp<SurfaceControl> layer1;
2055 ASSERT_NO_FATAL_FAILURE(
2056 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2057
2058 sp<SurfaceControl> layer2;
2059 ASSERT_NO_FATAL_FAILURE(
2060 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2061
2062 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2063
Marissa Wall861616d2018-10-22 12:52:23 -07002064 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002065 {
2066 SCOPED_TRACE("set layer 1 buffer red");
2067 auto shot = screenshot();
2068 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2069 }
2070
2071 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2072
Marissa Wall861616d2018-10-22 12:52:23 -07002073 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002074 {
2075 SCOPED_TRACE("set layer 2 buffer blue");
2076 auto shot = screenshot();
2077 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2078 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2079 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2080 }
2081
2082 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2083 {
2084 SCOPED_TRACE("set layer 1 buffer green");
2085 auto shot = screenshot();
2086 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2087 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2088 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2089 }
2090
2091 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2092
2093 {
2094 SCOPED_TRACE("set layer 2 buffer white");
2095 auto shot = screenshot();
2096 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2097 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2098 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2099 }
2100}
2101
2102TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
2103 sp<SurfaceControl> layer;
2104 ASSERT_NO_FATAL_FAILURE(
2105 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2106
2107 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2108 Color::BLUE, Color::WHITE));
2109
Marissa Wall861616d2018-10-22 12:52:23 -07002110 Transaction()
2111 .setFrame(layer, Rect(0, 0, 32, 32))
2112 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2113 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002114
2115 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2116 Color::GREEN, true /* filtered */);
2117}
2118
2119TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
2120 sp<SurfaceControl> layer;
2121 ASSERT_NO_FATAL_FAILURE(
2122 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2123
2124 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2125 Color::BLUE, Color::WHITE));
2126
Marissa Wall861616d2018-10-22 12:52:23 -07002127 Transaction()
2128 .setFrame(layer, Rect(0, 0, 32, 32))
2129 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2130 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002131
2132 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2133 Color::BLUE, true /* filtered */);
2134}
2135
2136TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
2137 sp<SurfaceControl> layer;
2138 ASSERT_NO_FATAL_FAILURE(
2139 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2140
2141 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2142 Color::BLUE, Color::WHITE));
2143
Marissa Wall861616d2018-10-22 12:52:23 -07002144 Transaction()
2145 .setFrame(layer, Rect(0, 0, 32, 32))
2146 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2147 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002148
2149 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2150 Color::GREEN, true /* filtered */);
2151}
2152
2153TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2154 sp<SurfaceControl> layer;
2155 ASSERT_NO_FATAL_FAILURE(
2156 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2157
2158 Transaction().setTransformToDisplayInverse(layer, false).apply();
2159
2160 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2161
2162 Transaction().setTransformToDisplayInverse(layer, true).apply();
2163}
2164
2165TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
2166 sp<SurfaceControl> layer;
2167 ASSERT_NO_FATAL_FAILURE(
2168 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2169
2170 sp<GraphicBuffer> buffer =
2171 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2172 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2173 BufferUsage::COMPOSER_OVERLAY,
2174 "test");
2175 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2176
Marissa Wallfda30bb2018-10-12 11:34:28 -07002177 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002178
2179 Transaction()
2180 .setBuffer(layer, buffer)
2181 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002182 .apply();
2183
2184 auto shot = screenshot();
2185 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2186 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2187}
2188
2189TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2190 sp<SurfaceControl> layer;
2191 ASSERT_NO_FATAL_FAILURE(
2192 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2193
2194 sp<GraphicBuffer> buffer =
2195 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2196 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2197 BufferUsage::COMPOSER_OVERLAY,
2198 "test");
2199 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2200
2201 Transaction()
2202 .setBuffer(layer, buffer)
2203 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002204 .apply();
2205
2206 auto shot = screenshot();
2207 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2208 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2209}
2210
2211TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2212 sp<SurfaceControl> layer;
2213 ASSERT_NO_FATAL_FAILURE(
2214 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2215
2216 sp<GraphicBuffer> buffer =
2217 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2218 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2219 BufferUsage::COMPOSER_OVERLAY,
2220 "test");
2221 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2222
2223 HdrMetadata hdrMetadata;
2224 hdrMetadata.validTypes = 0;
2225 Transaction()
2226 .setBuffer(layer, buffer)
2227 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002228 .apply();
2229
2230 auto shot = screenshot();
2231 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2232 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2233}
2234
2235TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2236 sp<SurfaceControl> layer;
2237 ASSERT_NO_FATAL_FAILURE(
2238 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2239
2240 sp<GraphicBuffer> buffer =
2241 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2242 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2243 BufferUsage::COMPOSER_OVERLAY,
2244 "test");
2245 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2246
2247 Region region;
2248 region.set(32, 32);
2249 Transaction()
2250 .setBuffer(layer, buffer)
2251 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002252 .apply();
2253
2254 auto shot = screenshot();
2255 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2256 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2257}
2258
2259TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2260 sp<SurfaceControl> layer;
2261 ASSERT_NO_FATAL_FAILURE(
2262 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2263
2264 sp<GraphicBuffer> buffer =
2265 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2266 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2267 BufferUsage::COMPOSER_OVERLAY,
2268 "test");
2269 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2270
2271 Transaction()
2272 .setBuffer(layer, buffer)
2273 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002274 .apply();
2275
2276 auto shot = screenshot();
2277 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2278 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2279}
2280
2281TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2282 sp<SurfaceControl> layer;
2283 ASSERT_NO_FATAL_FAILURE(
2284 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2285
2286 // verify this doesn't cause a crash
2287 Transaction().setSidebandStream(layer, nullptr).apply();
2288}
2289
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002290class ColorTransformHelper {
2291public:
2292 static void DegammaColorSingle(half& s) {
2293 if (s <= 0.03928f)
2294 s = s / 12.92f;
2295 else
2296 s = pow((s + 0.055f) / 1.055f, 2.4f);
2297 }
2298
2299 static void DegammaColor(half3& color) {
2300 DegammaColorSingle(color.r);
2301 DegammaColorSingle(color.g);
2302 DegammaColorSingle(color.b);
2303 }
2304
2305 static void GammaColorSingle(half& s) {
2306 if (s <= 0.0031308f) {
2307 s = s * 12.92f;
2308 } else {
2309 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2310 }
2311 }
2312
2313 static void GammaColor(half3& color) {
2314 GammaColorSingle(color.r);
2315 GammaColorSingle(color.g);
2316 GammaColorSingle(color.b);
2317 }
2318
2319 static void applyMatrix(half3& color, const mat3& mat) {
2320 half3 ret = half3(0);
2321
2322 for (int i = 0; i < 3; i++) {
2323 for (int j = 0; j < 3; j++) {
2324 ret[i] = ret[i] + color[j] * mat[j][i];
2325 }
2326 }
2327 color = ret;
2328 }
2329};
2330
Peiyong Lind3788632018-09-18 16:01:31 -07002331TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2332 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002333 ASSERT_NO_FATAL_FAILURE(colorLayer =
2334 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2335 ISurfaceComposerClient::eFXSurfaceColor));
2336 Transaction()
2337 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2338 .setLayer(colorLayer, mLayerZBase + 1)
2339 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002340 {
2341 SCOPED_TRACE("default color");
2342 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2343 }
2344
2345 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002346 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002347 mat3 matrix;
2348 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2349 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2350 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002351
2352 // degamma before applying the matrix
2353 if (mColorManagementUsed) {
2354 ColorTransformHelper::DegammaColor(expected);
2355 }
2356
2357 ColorTransformHelper::applyMatrix(expected, matrix);
2358
2359 if (mColorManagementUsed) {
2360 ColorTransformHelper::GammaColor(expected);
2361 }
2362
2363 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2364 uint8_t(expected.b * 255), 255};
2365
2366 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2367 // channel) should be less than one
2368 const uint8_t tolerance = 1;
2369
Peiyong Lind3788632018-09-18 16:01:31 -07002370 Transaction().setColor(colorLayer, color)
2371 .setColorTransform(colorLayer, matrix, vec3()).apply();
2372 {
2373 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002374 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002375 }
2376}
2377
chaviwf66724d2018-11-28 16:35:21 -08002378TEST_F(LayerTransactionTest, SetColorTransformOnParent) {
2379 sp<SurfaceControl> parentLayer;
2380 sp<SurfaceControl> colorLayer;
2381 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2382 0 /* buffer height */,
2383 ISurfaceComposerClient::eFXSurfaceContainer));
2384 ASSERT_NO_FATAL_FAILURE(
2385 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2386 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2387
2388 Transaction()
2389 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2390 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2391 .setLayer(parentLayer, mLayerZBase + 1)
2392 .apply();
2393 {
2394 SCOPED_TRACE("default color");
2395 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2396 }
2397
2398 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2399 half3 expected = color;
2400 mat3 matrix;
2401 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2402 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2403 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2404
2405 // degamma before applying the matrix
2406 if (mColorManagementUsed) {
2407 ColorTransformHelper::DegammaColor(expected);
2408 }
2409
2410 ColorTransformHelper::applyMatrix(expected, matrix);
2411
2412 if (mColorManagementUsed) {
2413 ColorTransformHelper::GammaColor(expected);
2414 }
2415
2416 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2417 uint8_t(expected.b * 255), 255};
2418
2419 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2420 // channel) should be less than one
2421 const uint8_t tolerance = 1;
2422
2423 Transaction()
2424 .setColor(colorLayer, color)
2425 .setColorTransform(parentLayer, matrix, vec3())
2426 .apply();
2427 {
2428 SCOPED_TRACE("new color");
2429 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2430 }
2431}
2432
2433TEST_F(LayerTransactionTest, SetColorTransformOnChildAndParent) {
2434 sp<SurfaceControl> parentLayer;
2435 sp<SurfaceControl> colorLayer;
2436 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2437 0 /* buffer height */,
2438 ISurfaceComposerClient::eFXSurfaceContainer));
2439 ASSERT_NO_FATAL_FAILURE(
2440 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2441 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2442
2443 Transaction()
2444 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2445 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2446 .setLayer(parentLayer, mLayerZBase + 1)
2447 .apply();
2448 {
2449 SCOPED_TRACE("default color");
2450 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2451 }
2452
2453 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2454 half3 expected = color;
2455 mat3 matrixChild;
2456 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2457 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2458 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2459 mat3 matrixParent;
2460 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2461 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2462 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2463
2464 // degamma before applying the matrix
2465 if (mColorManagementUsed) {
2466 ColorTransformHelper::DegammaColor(expected);
2467 }
2468
2469 ColorTransformHelper::applyMatrix(expected, matrixChild);
2470 ColorTransformHelper::applyMatrix(expected, matrixParent);
2471
2472 if (mColorManagementUsed) {
2473 ColorTransformHelper::GammaColor(expected);
2474 }
2475
2476 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2477 uint8_t(expected.b * 255), 255};
2478
2479 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2480 // channel) should be less than one
2481 const uint8_t tolerance = 1;
2482
2483 Transaction()
2484 .setColor(colorLayer, color)
2485 .setColorTransform(parentLayer, matrixParent, vec3())
2486 .setColorTransform(colorLayer, matrixChild, vec3())
2487 .apply();
2488 {
2489 SCOPED_TRACE("new color");
2490 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2491 }
2492}
2493
Marissa Wallfda30bb2018-10-12 11:34:28 -07002494class ExpectedResult {
2495public:
2496 enum Transaction {
2497 NOT_PRESENTED = 0,
2498 PRESENTED,
2499 };
2500
2501 enum Buffer {
2502 NOT_ACQUIRED = 0,
2503 ACQUIRED,
2504 };
2505
2506 enum PreviousBuffer {
2507 NOT_RELEASED = 0,
2508 RELEASED,
2509 };
2510
2511 void reset() {
2512 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2513 mExpectedSurfaceResults.clear();
2514 }
2515
2516 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
2517 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2518 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2519 mTransactionResult = transactionResult;
2520 mExpectedSurfaceResults.emplace(std::piecewise_construct,
2521 std::forward_as_tuple(layer->getHandle()),
2522 std::forward_as_tuple(bufferResult, previousBufferResult));
2523 }
2524
2525 void addSurfaces(ExpectedResult::Transaction transactionResult,
2526 const std::vector<sp<SurfaceControl>>& layers,
2527 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2528 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2529 for (const auto& layer : layers) {
2530 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2531 }
2532 }
2533
2534 void verifyTransactionStats(const TransactionStats& transactionStats) const {
Valerie Hau63258a12018-12-14 14:31:48 -08002535 const auto& [latchTime, presentFence, surfaceStats] = transactionStats;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002536 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2537 ASSERT_GE(latchTime, 0) << "bad latch time";
Valerie Hau63258a12018-12-14 14:31:48 -08002538 ASSERT_NE(presentFence, nullptr);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002539 } else {
Valerie Hau63258a12018-12-14 14:31:48 -08002540 ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
Marissa Wallfda30bb2018-10-12 11:34:28 -07002541 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2542 }
2543
2544 ASSERT_EQ(surfaceStats.size(), mExpectedSurfaceResults.size())
2545 << "wrong number of surfaces";
2546
2547 for (const auto& stats : surfaceStats) {
2548 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2549 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2550 << "unexpected surface control";
2551 expectedSurfaceResult->second.verifySurfaceStats(stats, latchTime);
2552 }
2553 }
2554
2555private:
2556 class ExpectedSurfaceResult {
2557 public:
2558 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2559 ExpectedResult::PreviousBuffer previousBufferResult)
2560 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2561
2562 void verifySurfaceStats(const SurfaceStats& surfaceStats, nsecs_t latchTime) const {
2563 const auto& [surfaceControl, acquireTime, releasePreviousBuffer] = surfaceStats;
2564
2565 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2566 << "bad acquire time";
2567 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
2568 ASSERT_EQ(releasePreviousBuffer,
2569 mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED)
2570 << "bad previous buffer released";
2571 }
2572
2573 private:
2574 ExpectedResult::Buffer mBufferResult;
2575 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2576 };
2577
2578 struct IBinderHash {
2579 std::size_t operator()(const sp<IBinder>& strongPointer) const {
2580 return std::hash<IBinder*>{}(strongPointer.get());
2581 }
2582 };
2583 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2584 std::unordered_map<sp<IBinder>, ExpectedSurfaceResult, IBinderHash> mExpectedSurfaceResults;
2585};
2586
2587class CallbackHelper {
2588public:
2589 static void function(void* callbackContext, const TransactionStats& transactionStats) {
2590 if (!callbackContext) {
2591 ALOGE("failed to get callback context");
2592 }
2593 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2594 std::lock_guard lock(helper->mMutex);
2595 helper->mTransactionStatsQueue.push(transactionStats);
2596 helper->mConditionVariable.notify_all();
2597 }
2598
2599 void getTransactionStats(TransactionStats* outStats) {
2600 std::unique_lock lock(mMutex);
2601
2602 if (mTransactionStatsQueue.empty()) {
2603 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2604 std::cv_status::timeout)
2605 << "did not receive callback";
2606 }
2607
2608 *outStats = std::move(mTransactionStatsQueue.front());
2609 mTransactionStatsQueue.pop();
2610 }
2611
2612 void verifyFinalState() {
2613 // Wait to see if there are extra callbacks
2614 std::this_thread::sleep_for(500ms);
2615
2616 std::lock_guard lock(mMutex);
2617 EXPECT_EQ(mTransactionStatsQueue.size(), 0) << "extra callbacks received";
2618 mTransactionStatsQueue = {};
2619 }
2620
2621 void* getContext() { return static_cast<void*>(this); }
2622
2623 std::mutex mMutex;
2624 std::condition_variable mConditionVariable;
2625 std::queue<TransactionStats> mTransactionStatsQueue;
2626};
2627
2628class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002629public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002630 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002631 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002632 }
2633
Marissa Wall861616d2018-10-22 12:52:23 -07002634 static void fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2635 const sp<SurfaceControl>& layer = nullptr) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002636 if (layer) {
2637 sp<GraphicBuffer> buffer =
Marissa Wall861616d2018-10-22 12:52:23 -07002638 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002639 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2640 BufferUsage::COMPOSER_OVERLAY |
2641 BufferUsage::GPU_TEXTURE,
2642 "test");
Marissa Wall861616d2018-10-22 12:52:23 -07002643 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002644
2645 sp<Fence> fence = new Fence(-1);
2646
Marissa Wall861616d2018-10-22 12:52:23 -07002647 transaction.setBuffer(layer, buffer).setAcquireFence(layer, fence);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002648 }
2649
2650 transaction.addTransactionCompletedCallback(callbackHelper->function,
2651 callbackHelper->getContext());
2652 }
2653
Marissa Wall861616d2018-10-22 12:52:23 -07002654 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2655 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002656 TransactionStats transactionStats;
2657 ASSERT_NO_FATAL_FAILURE(helper.getTransactionStats(&transactionStats));
2658 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyTransactionStats(transactionStats));
2659
2660 if (finalState) {
2661 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2662 }
2663 }
2664
Marissa Wall861616d2018-10-22 12:52:23 -07002665 static void waitForCallbacks(CallbackHelper& helper,
2666 const std::vector<ExpectedResult>& expectedResults,
2667 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002668 for (const auto& expectedResult : expectedResults) {
2669 waitForCallback(helper, expectedResult);
2670 }
2671 if (finalState) {
2672 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2673 }
2674 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002675};
2676
2677TEST_F(LayerCallbackTest, Basic) {
2678 sp<SurfaceControl> layer;
2679 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2680
2681 Transaction transaction;
2682 CallbackHelper callback;
2683 fillTransaction(transaction, &callback, layer);
2684
2685 transaction.apply();
2686
2687 ExpectedResult expected;
2688 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2689 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2690}
2691
2692TEST_F(LayerCallbackTest, NoBuffer) {
2693 sp<SurfaceControl> layer;
2694 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2695
2696 Transaction transaction;
2697 CallbackHelper callback;
2698 fillTransaction(transaction, &callback);
2699
Marissa Wall861616d2018-10-22 12:52:23 -07002700 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002701
2702 ExpectedResult expected;
2703 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2704 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2705}
2706
2707TEST_F(LayerCallbackTest, NoStateChange) {
2708 Transaction transaction;
2709 CallbackHelper callback;
2710 fillTransaction(transaction, &callback);
2711
2712 transaction.apply();
2713
2714 ExpectedResult expected;
2715 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2716}
2717
2718TEST_F(LayerCallbackTest, OffScreen) {
2719 sp<SurfaceControl> layer;
2720 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2721
2722 Transaction transaction;
2723 CallbackHelper callback;
2724 fillTransaction(transaction, &callback, layer);
2725
Marissa Wall861616d2018-10-22 12:52:23 -07002726 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002727
2728 ExpectedResult expected;
2729 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2730 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2731}
2732
2733TEST_F(LayerCallbackTest, Merge) {
2734 sp<SurfaceControl> layer1, layer2;
2735 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2736 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2737
2738 Transaction transaction1, transaction2;
2739 CallbackHelper callback1, callback2;
2740 fillTransaction(transaction1, &callback1, layer1);
2741 fillTransaction(transaction2, &callback2, layer2);
2742
Marissa Wall861616d2018-10-22 12:52:23 -07002743 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2744 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002745
2746 ExpectedResult expected;
2747 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2748 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2749 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2750}
2751
2752TEST_F(LayerCallbackTest, Merge_SameCallback) {
2753 sp<SurfaceControl> layer1, layer2;
2754 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2755 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2756
2757 Transaction transaction1, transaction2;
2758 CallbackHelper callback;
2759 fillTransaction(transaction1, &callback, layer1);
2760 fillTransaction(transaction2, &callback, layer2);
2761
2762 transaction2.merge(std::move(transaction1)).apply();
2763
2764 ExpectedResult expected;
2765 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2766 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2767 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2768}
2769
2770TEST_F(LayerCallbackTest, Merge_SameLayer) {
2771 sp<SurfaceControl> layer;
2772 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2773
2774 Transaction transaction1, transaction2;
2775 CallbackHelper callback1, callback2;
2776 fillTransaction(transaction1, &callback1, layer);
2777 fillTransaction(transaction2, &callback2, layer);
2778
2779 transaction2.merge(std::move(transaction1)).apply();
2780
2781 ExpectedResult expected;
2782 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2783 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2784 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2785}
2786
2787TEST_F(LayerCallbackTest, Merge_SingleBuffer) {
2788 sp<SurfaceControl> layer1, layer2;
2789 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2790 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2791
2792 Transaction transaction1, transaction2;
2793 CallbackHelper callback1, callback2;
2794 fillTransaction(transaction1, &callback1, layer1);
2795 fillTransaction(transaction2, &callback2);
2796
Marissa Wall861616d2018-10-22 12:52:23 -07002797 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2798 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002799
2800 ExpectedResult expected;
2801 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2802 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2803 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2804}
2805
2806TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2807 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2808 client2(new SurfaceComposerClient);
2809
2810 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2811 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2812
2813 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002814 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002815 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002816 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002817 ISurfaceComposerClient::eFXSurfaceBufferState));
2818
2819 Transaction transaction1, transaction2;
2820 CallbackHelper callback1, callback2;
2821 fillTransaction(transaction1, &callback1, layer1);
2822 fillTransaction(transaction2, &callback2, layer2);
2823
Marissa Wall861616d2018-10-22 12:52:23 -07002824 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2825 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002826
2827 ExpectedResult expected;
2828 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2829 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2830 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2831}
2832
2833TEST_F(LayerCallbackTest, MultipleTransactions) {
2834 sp<SurfaceControl> layer;
2835 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2836
2837 Transaction transaction;
2838 CallbackHelper callback;
2839 for (size_t i = 0; i < 10; i++) {
2840 fillTransaction(transaction, &callback, layer);
2841
2842 transaction.apply();
2843
2844 ExpectedResult expected;
2845 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2846 ExpectedResult::Buffer::NOT_ACQUIRED,
2847 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2848 : ExpectedResult::PreviousBuffer::RELEASED);
2849 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2850 }
2851 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2852}
2853
2854TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
2855 sp<SurfaceControl> layer;
2856 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2857
2858 Transaction transaction;
2859 CallbackHelper callback;
2860 for (size_t i = 0; i < 10; i++) {
2861 ExpectedResult expected;
2862
2863 if (i == 0) {
2864 fillTransaction(transaction, &callback, layer);
2865 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2866 } else {
2867 fillTransaction(transaction, &callback);
2868 }
2869
2870 transaction.apply();
2871
2872 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2873 }
2874 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2875}
2876
2877TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
2878 sp<SurfaceControl> layer;
2879 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2880
2881 Transaction transaction;
2882 CallbackHelper callback;
2883 for (size_t i = 0; i < 10; i++) {
2884 if (i == 0) {
2885 fillTransaction(transaction, &callback, layer);
2886 } else {
2887 fillTransaction(transaction, &callback);
2888 }
2889
Marissa Wall861616d2018-10-22 12:52:23 -07002890 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002891
2892 ExpectedResult expected;
2893 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
2894 : ExpectedResult::Transaction::NOT_PRESENTED,
2895 layer);
2896 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
2897 }
2898 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2899}
2900
2901TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
2902 sp<SurfaceControl> layer1, layer2;
2903 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2904 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2905
2906 Transaction transaction1, transaction2;
2907 CallbackHelper callback1, callback2;
2908 for (size_t i = 0; i < 10; i++) {
2909 fillTransaction(transaction1, &callback1, layer1);
2910 fillTransaction(transaction2, &callback2, layer2);
2911
Marissa Wall861616d2018-10-22 12:52:23 -07002912 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2913 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002914
2915 ExpectedResult expected;
2916 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2917 ExpectedResult::Buffer::NOT_ACQUIRED,
2918 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2919 : ExpectedResult::PreviousBuffer::RELEASED);
2920 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2921 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2922 }
2923 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2924 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2925}
2926
2927TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
2928 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2929 client2(new SurfaceComposerClient);
2930 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2931 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2932
2933 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002934 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002935 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002936 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002937 ISurfaceComposerClient::eFXSurfaceBufferState));
2938
2939 Transaction transaction1, transaction2;
2940 CallbackHelper callback1, callback2;
2941 for (size_t i = 0; i < 10; i++) {
2942 fillTransaction(transaction1, &callback1, layer1);
2943 fillTransaction(transaction2, &callback2, layer2);
2944
Marissa Wall861616d2018-10-22 12:52:23 -07002945 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2946 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002947
2948 ExpectedResult expected;
2949 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2950 ExpectedResult::Buffer::NOT_ACQUIRED,
2951 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2952 : ExpectedResult::PreviousBuffer::RELEASED);
2953 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2954 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2955 }
2956 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2957 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2958}
2959
2960TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
2961 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2962 client2(new SurfaceComposerClient);
2963 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2964 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2965
2966 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002967 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002968 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002969 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002970 ISurfaceComposerClient::eFXSurfaceBufferState));
2971
2972 Transaction transaction1, transaction2;
2973 CallbackHelper callback1, callback2;
2974
2975 // Normal call to set up test
2976 fillTransaction(transaction1, &callback1, layer1);
2977 fillTransaction(transaction2, &callback2, layer2);
2978
Marissa Wall861616d2018-10-22 12:52:23 -07002979 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2980 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002981
2982 ExpectedResult expected;
2983 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2984 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2985 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2986 expected.reset();
2987
2988 // Test
2989 fillTransaction(transaction1, &callback1);
2990 fillTransaction(transaction2, &callback2);
2991
2992 transaction2.merge(std::move(transaction1)).apply();
2993
2994 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2995 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2996}
2997
2998TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
2999 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3000 client2(new SurfaceComposerClient);
3001
3002 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3003 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3004
3005 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003006 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003007 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003008 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003009 ISurfaceComposerClient::eFXSurfaceBufferState));
3010
3011 Transaction transaction1, transaction2;
3012 CallbackHelper callback1, callback2;
3013
3014 // Normal call to set up test
3015 fillTransaction(transaction1, &callback1, layer1);
3016 fillTransaction(transaction2, &callback2, layer2);
3017
Marissa Wall861616d2018-10-22 12:52:23 -07003018 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3019 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003020
3021 ExpectedResult expected;
3022 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3023 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3024 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3025 expected.reset();
3026
3027 // Test
3028 fillTransaction(transaction1, &callback1);
3029 fillTransaction(transaction2, &callback2);
3030
Marissa Wall861616d2018-10-22 12:52:23 -07003031 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003032
3033 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2);
3034 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3035 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3036}
3037
3038TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3039 sp<SurfaceControl> layer;
3040 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3041
3042 Transaction transaction;
3043 CallbackHelper callback;
3044 std::vector<ExpectedResult> expectedResults(50);
3045 ExpectedResult::PreviousBuffer previousBufferResult =
3046 ExpectedResult::PreviousBuffer::NOT_RELEASED;
3047 for (auto& expected : expectedResults) {
3048 expected.reset();
3049 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3050 ExpectedResult::Buffer::NOT_ACQUIRED, previousBufferResult);
3051 previousBufferResult = ExpectedResult::PreviousBuffer::RELEASED;
3052
3053 fillTransaction(transaction, &callback, layer);
3054
3055 transaction.apply();
3056 std::this_thread::sleep_for(200ms);
3057 }
3058 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3059}
3060
3061TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3062 sp<SurfaceControl> layer;
3063 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3064
3065 Transaction transaction;
3066 CallbackHelper callback;
3067 std::vector<ExpectedResult> expectedResults(50);
3068 bool first = true;
3069 for (auto& expected : expectedResults) {
3070 expected.reset();
3071
3072 if (first) {
3073 fillTransaction(transaction, &callback, layer);
3074 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3075 first = false;
3076 } else {
3077 fillTransaction(transaction, &callback);
3078 }
3079
3080 transaction.apply();
3081 std::this_thread::sleep_for(200ms);
3082 }
3083 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3084}
3085
3086TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3087 sp<SurfaceControl> layer;
3088 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3089
3090 // Normal call to set up test
3091 Transaction transaction;
3092 CallbackHelper callback;
3093 fillTransaction(transaction, &callback, layer);
3094
Marissa Wall861616d2018-10-22 12:52:23 -07003095 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003096
3097 ExpectedResult expectedResult;
3098 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3099 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3100
3101 // Test
3102 std::vector<ExpectedResult> expectedResults(50);
3103 for (auto& expected : expectedResults) {
3104 expected.reset();
3105 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
3106
3107 fillTransaction(transaction, &callback);
3108
Marissa Wall861616d2018-10-22 12:52:23 -07003109 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003110
3111 std::this_thread::sleep_for(200ms);
3112 }
3113 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3114}
3115
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003116class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003117protected:
3118 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003119 LayerTransactionTest::SetUp();
3120 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003121
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003122 sp<IBinder> display(
3123 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003124 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07003125 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07003126
3127 ssize_t displayWidth = info.w;
3128 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003129
3130 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003131 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3132 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003133 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003134 ASSERT_TRUE(mBGSurfaceControl->isValid());
3135 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
3136
3137 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003138 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3139
Peiyong Lin566a3b42018-01-09 18:22:43 -08003140 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003141 ASSERT_TRUE(mFGSurfaceControl->isValid());
3142
3143 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3144
3145 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003146 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003147 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003148 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3149
3150 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3151
Robert Carr4cdc58f2017-08-23 14:22:20 -07003152 asTransaction([&](Transaction& t) {
3153 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003154
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003155 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003156
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003157 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3158 .setPosition(mFGSurfaceControl, 64, 64)
3159 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003160
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003161 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3162 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3163 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003164 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003165 }
3166
3167 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003168 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003169 mBGSurfaceControl = 0;
3170 mFGSurfaceControl = 0;
3171 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003172 }
3173
3174 void waitForPostedBuffers() {
3175 // Since the sync surface is in synchronous mode (i.e. double buffered)
3176 // posting three buffers to it should ensure that at least two
3177 // SurfaceFlinger::handlePageFlip calls have been made, which should
3178 // guaranteed that a buffer posted to another Surface has been retired.
3179 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3180 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3181 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3182 }
3183
Robert Carr4cdc58f2017-08-23 14:22:20 -07003184 void asTransaction(const std::function<void(Transaction&)>& exec) {
3185 Transaction t;
3186 exec(t);
3187 t.apply(true);
3188 }
3189
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003190 sp<SurfaceControl> mBGSurfaceControl;
3191 sp<SurfaceControl> mFGSurfaceControl;
3192
3193 // This surface is used to ensure that the buffers posted to
3194 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3195 sp<SurfaceControl> mSyncSurfaceControl;
3196};
3197
Robert Carr7f619b22017-11-06 12:56:35 -08003198TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003199
chaviw0e3479f2018-09-10 16:49:30 -07003200 std::unique_ptr<ScreenCapture> sc;
3201
3202 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003203 fillSurfaceRGBA8(relative, 10, 10, 10);
3204 waitForPostedBuffers();
3205
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003206 Transaction{}
3207 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003208 .setPosition(relative, 64, 64)
3209 .apply();
3210
3211 {
3212 // The relative should be on top of the FG control.
3213 ScreenCapture::captureScreen(&sc);
3214 sc->checkPixel(64, 64, 10, 10, 10);
3215 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003216 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003217
3218 {
3219 // Nothing should change at this point.
3220 ScreenCapture::captureScreen(&sc);
3221 sc->checkPixel(64, 64, 10, 10, 10);
3222 }
3223
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003224 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003225
3226 {
3227 // Ensure that the relative was actually hidden, rather than
3228 // being left in the detached but visible state.
3229 ScreenCapture::captureScreen(&sc);
3230 sc->expectFGColor(64, 64);
3231 }
3232}
3233
Robert Carr8d5227b2017-03-16 15:41:03 -07003234class GeometryLatchingTest : public LayerUpdateTest {
3235protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003236 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003237 SCOPED_TRACE(trace);
3238 ScreenCapture::captureScreen(&sc);
3239 // We find the leading edge of the FG surface.
3240 sc->expectFGColor(127, 127);
3241 sc->expectBGColor(128, 128);
3242 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003243
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003244 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07003245
3246 void unlockFGBuffer() {
3247 sp<Surface> s = mFGSurfaceControl->getSurface();
3248 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3249 waitForPostedBuffers();
3250 }
3251
Robert Carr8d5227b2017-03-16 15:41:03 -07003252 void completeFGResize() {
3253 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3254 waitForPostedBuffers();
3255 }
3256 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003257 asTransaction([&](Transaction& t) {
3258 t.setSize(mFGSurfaceControl, 64, 64);
3259 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003260 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003261 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003262
3263 EXPECT_INITIAL_STATE("After restoring initial state");
3264 }
chaviw0e3479f2018-09-10 16:49:30 -07003265 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003266};
3267
Robert Carr8d5227b2017-03-16 15:41:03 -07003268class CropLatchingTest : public GeometryLatchingTest {
3269protected:
3270 void EXPECT_CROPPED_STATE(const char* trace) {
3271 SCOPED_TRACE(trace);
3272 ScreenCapture::captureScreen(&sc);
3273 // The edge should be moved back one pixel by our crop.
3274 sc->expectFGColor(126, 126);
3275 sc->expectBGColor(127, 127);
3276 sc->expectBGColor(128, 128);
3277 }
chaviw59f5c562017-06-28 16:39:06 -07003278
3279 void EXPECT_RESIZE_STATE(const char* trace) {
3280 SCOPED_TRACE(trace);
3281 ScreenCapture::captureScreen(&sc);
3282 // The FG is now resized too 128,128 at 64,64
3283 sc->expectFGColor(64, 64);
3284 sc->expectFGColor(191, 191);
3285 sc->expectBGColor(192, 192);
3286 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003287};
3288
Pablo Ceballos05289c22016-04-14 15:49:55 -07003289TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003290 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003291 {
3292 SCOPED_TRACE("before anything");
3293 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003294 sc->expectBGColor(32, 32);
3295 sc->expectFGColor(96, 96);
3296 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003297 }
3298
3299 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003300 asTransaction([&](Transaction& t) {
3301 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003302 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3303 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003304 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003305
Robert Carr4cdc58f2017-08-23 14:22:20 -07003306 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003307 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003308 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3309 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003310 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003311
3312 {
3313 SCOPED_TRACE("before any trigger");
3314 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003315 sc->expectBGColor(32, 32);
3316 sc->expectFGColor(96, 96);
3317 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003318 }
3319
3320 // should trigger the first deferred transaction, but not the second one
3321 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3322 {
3323 SCOPED_TRACE("after first trigger");
3324 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003325 sc->expectBGColor(32, 32);
3326 sc->checkPixel(96, 96, 162, 63, 96);
3327 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003328 }
3329
3330 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003331 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003332
3333 // trigger the second deferred transaction
3334 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3335 {
3336 SCOPED_TRACE("after second trigger");
3337 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003338 sc->expectBGColor(32, 32);
3339 sc->expectBGColor(96, 96);
3340 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003341 }
3342}
3343
Robert Carre392b552017-09-19 12:16:05 -07003344TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003345 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003346
3347 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003348 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3349 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3350 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3351 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003352 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003353 SurfaceComposerClient::Transaction{}
3354 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3355 .show(childNoBuffer)
3356 .show(childBuffer)
3357 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003358 {
3359 ScreenCapture::captureScreen(&sc);
3360 sc->expectChildColor(73, 73);
3361 sc->expectFGColor(74, 74);
3362 }
Vishnu Nair60356342018-11-13 13:00:45 -08003363 SurfaceComposerClient::Transaction{}
3364 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3365 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003366 {
3367 ScreenCapture::captureScreen(&sc);
3368 sc->expectChildColor(73, 73);
3369 sc->expectChildColor(74, 74);
3370 }
3371}
3372
Robert Carr2c5f6d22017-09-26 12:30:35 -07003373TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003374 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003375 {
3376 SCOPED_TRACE("before move");
3377 ScreenCapture::captureScreen(&sc);
3378 sc->expectBGColor(0, 12);
3379 sc->expectFGColor(75, 75);
3380 sc->expectBGColor(145, 145);
3381 }
3382
3383 Transaction t1, t2;
3384 t1.setPosition(mFGSurfaceControl, 128, 128);
3385 t2.setPosition(mFGSurfaceControl, 0, 0);
3386 // We expect that the position update from t2 now
3387 // overwrites the position update from t1.
3388 t1.merge(std::move(t2));
3389 t1.apply();
3390
3391 {
3392 ScreenCapture::captureScreen(&sc);
3393 sc->expectFGColor(1, 1);
3394 }
3395}
3396
Robert Carr1f0a16a2016-10-24 16:27:39 -07003397class ChildLayerTest : public LayerUpdateTest {
3398protected:
3399 void SetUp() override {
3400 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003401 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3402 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003403 fillSurfaceRGBA8(mChild, 200, 200, 200);
3404
3405 {
3406 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003407 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003408 mCapture->expectChildColor(64, 64);
3409 }
3410 }
3411 void TearDown() override {
3412 LayerUpdateTest::TearDown();
3413 mChild = 0;
3414 }
3415
3416 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003417 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003418};
3419
3420TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003421 asTransaction([&](Transaction& t) {
3422 t.show(mChild);
3423 t.setPosition(mChild, 10, 10);
3424 t.setPosition(mFGSurfaceControl, 64, 64);
3425 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003426
3427 {
chaviw0e3479f2018-09-10 16:49:30 -07003428 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003429 // Top left of foreground must now be visible
3430 mCapture->expectFGColor(64, 64);
3431 // But 10 pixels in we should see the child surface
3432 mCapture->expectChildColor(74, 74);
3433 // And 10 more pixels we should be back to the foreground surface
3434 mCapture->expectFGColor(84, 84);
3435 }
3436
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003437 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003438
3439 {
chaviw0e3479f2018-09-10 16:49:30 -07003440 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003441 // Top left of foreground should now be at 0, 0
3442 mCapture->expectFGColor(0, 0);
3443 // But 10 pixels in we should see the child surface
3444 mCapture->expectChildColor(10, 10);
3445 // And 10 more pixels we should be back to the foreground surface
3446 mCapture->expectFGColor(20, 20);
3447 }
3448}
3449
Robert Carr41b08b52017-06-01 16:11:34 -07003450TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003451 asTransaction([&](Transaction& t) {
3452 t.show(mChild);
3453 t.setPosition(mChild, 0, 0);
3454 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003455 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003456 });
Robert Carr41b08b52017-06-01 16:11:34 -07003457
3458 {
chaviw0e3479f2018-09-10 16:49:30 -07003459 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003460 mCapture->expectChildColor(0, 0);
3461 mCapture->expectChildColor(4, 4);
3462 mCapture->expectBGColor(5, 5);
3463 }
3464}
3465
Robert Carr1f0a16a2016-10-24 16:27:39 -07003466TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003467 asTransaction([&](Transaction& t) {
3468 t.show(mChild);
3469 t.setPosition(mFGSurfaceControl, 0, 0);
3470 t.setPosition(mChild, 63, 63);
3471 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003472
3473 {
chaviw0e3479f2018-09-10 16:49:30 -07003474 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003475 mCapture->expectFGColor(0, 0);
3476 // Last pixel in foreground should now be the child.
3477 mCapture->expectChildColor(63, 63);
3478 // But the child should be constrained and the next pixel
3479 // must be the background
3480 mCapture->expectBGColor(64, 64);
3481 }
3482}
3483
3484TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003485 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003486
3487 // Find the boundary between the parent and child
3488 {
chaviw0e3479f2018-09-10 16:49:30 -07003489 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003490 mCapture->expectChildColor(9, 9);
3491 mCapture->expectFGColor(10, 10);
3492 }
3493
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003494 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003495
3496 // The boundary should be twice as far from the origin now.
3497 // The pixels from the last test should all be child now
3498 {
chaviw0e3479f2018-09-10 16:49:30 -07003499 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003500 mCapture->expectChildColor(9, 9);
3501 mCapture->expectChildColor(10, 10);
3502 mCapture->expectChildColor(19, 19);
3503 mCapture->expectFGColor(20, 20);
3504 }
3505}
Robert Carr9524cb32017-02-13 11:32:32 -08003506
Robert Carr6452f122017-03-21 10:41:29 -07003507TEST_F(ChildLayerTest, ChildLayerAlpha) {
3508 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3509 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3510 fillSurfaceRGBA8(mChild, 0, 254, 0);
3511 waitForPostedBuffers();
3512
Robert Carr4cdc58f2017-08-23 14:22:20 -07003513 asTransaction([&](Transaction& t) {
3514 t.show(mChild);
3515 t.setPosition(mChild, 0, 0);
3516 t.setPosition(mFGSurfaceControl, 0, 0);
3517 });
Robert Carr6452f122017-03-21 10:41:29 -07003518
3519 {
chaviw0e3479f2018-09-10 16:49:30 -07003520 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003521 // Unblended child color
3522 mCapture->checkPixel(0, 0, 0, 254, 0);
3523 }
3524
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003525 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003526
3527 {
chaviw0e3479f2018-09-10 16:49:30 -07003528 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003529 // Child and BG blended.
3530 mCapture->checkPixel(0, 0, 127, 127, 0);
3531 }
3532
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003533 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003534
3535 {
chaviw0e3479f2018-09-10 16:49:30 -07003536 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003537 // Child and BG blended.
3538 mCapture->checkPixel(0, 0, 95, 64, 95);
3539 }
3540}
3541
Robert Carr9524cb32017-02-13 11:32:32 -08003542TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003543 asTransaction([&](Transaction& t) {
3544 t.show(mChild);
3545 t.setPosition(mChild, 10, 10);
3546 t.setPosition(mFGSurfaceControl, 64, 64);
3547 });
Robert Carr9524cb32017-02-13 11:32:32 -08003548
3549 {
chaviw0e3479f2018-09-10 16:49:30 -07003550 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003551 // Top left of foreground must now be visible
3552 mCapture->expectFGColor(64, 64);
3553 // But 10 pixels in we should see the child surface
3554 mCapture->expectChildColor(74, 74);
3555 // And 10 more pixels we should be back to the foreground surface
3556 mCapture->expectFGColor(84, 84);
3557 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003558
3559 asTransaction([&](Transaction& t) {
3560 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
3561 });
3562
Robert Carr9524cb32017-02-13 11:32:32 -08003563 {
chaviw0e3479f2018-09-10 16:49:30 -07003564 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003565 mCapture->expectFGColor(64, 64);
3566 // In reparenting we should have exposed the entire foreground surface.
3567 mCapture->expectFGColor(74, 74);
3568 // And the child layer should now begin at 10, 10 (since the BG
3569 // layer is at (0, 0)).
3570 mCapture->expectBGColor(9, 9);
3571 mCapture->expectChildColor(10, 10);
3572 }
3573}
3574
Robert Carr2e102c92018-10-23 12:11:15 -07003575TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
3576 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003577 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07003578 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
3579
3580 {
3581 SCOPED_TRACE("Grandchild visible");
3582 ScreenCapture::captureScreen(&mCapture);
3583 mCapture->checkPixel(64, 64, 111, 111, 111);
3584 }
3585
3586 mChild->clear();
3587
3588 {
3589 SCOPED_TRACE("After destroying child");
3590 ScreenCapture::captureScreen(&mCapture);
3591 mCapture->expectFGColor(64, 64);
3592 }
3593
3594 asTransaction([&](Transaction& t) {
3595 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
3596 });
3597
3598 {
3599 SCOPED_TRACE("After reparenting grandchild");
3600 ScreenCapture::captureScreen(&mCapture);
3601 mCapture->checkPixel(64, 64, 111, 111, 111);
3602 }
3603}
3604
chaviw161410b02017-07-27 10:46:08 -07003605TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003606 asTransaction([&](Transaction& t) {
3607 t.show(mChild);
3608 t.setPosition(mChild, 10, 10);
3609 t.setPosition(mFGSurfaceControl, 64, 64);
3610 });
Robert Carr9524cb32017-02-13 11:32:32 -08003611
3612 {
chaviw0e3479f2018-09-10 16:49:30 -07003613 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003614 // Top left of foreground must now be visible
3615 mCapture->expectFGColor(64, 64);
3616 // But 10 pixels in we should see the child surface
3617 mCapture->expectChildColor(74, 74);
3618 // And 10 more pixels we should be back to the foreground surface
3619 mCapture->expectFGColor(84, 84);
3620 }
3621
chaviw0e3479f2018-09-10 16:49:30 -07003622
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003623 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08003624
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003625 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08003626
chaviw161410b02017-07-27 10:46:08 -07003627 // Since the child has the same client as the parent, it will not get
3628 // detached and will be hidden.
3629 {
chaviw0e3479f2018-09-10 16:49:30 -07003630 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003631 mCapture->expectFGColor(64, 64);
3632 mCapture->expectFGColor(74, 74);
3633 mCapture->expectFGColor(84, 84);
3634 }
3635}
3636
3637TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
3638 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003639 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003640 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
3641 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07003642
chaviw161410b02017-07-27 10:46:08 -07003643 ASSERT_TRUE(mChildNewClient->isValid());
3644
3645 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
3646
Robert Carr4cdc58f2017-08-23 14:22:20 -07003647 asTransaction([&](Transaction& t) {
3648 t.hide(mChild);
3649 t.show(mChildNewClient);
3650 t.setPosition(mChildNewClient, 10, 10);
3651 t.setPosition(mFGSurfaceControl, 64, 64);
3652 });
chaviw161410b02017-07-27 10:46:08 -07003653
3654 {
chaviw0e3479f2018-09-10 16:49:30 -07003655 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003656 // Top left of foreground must now be visible
3657 mCapture->expectFGColor(64, 64);
3658 // But 10 pixels in we should see the child surface
3659 mCapture->expectChildColor(74, 74);
3660 // And 10 more pixels we should be back to the foreground surface
3661 mCapture->expectFGColor(84, 84);
3662 }
3663
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003664 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07003665
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003666 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07003667
Robert Carr9524cb32017-02-13 11:32:32 -08003668 // Nothing should have changed.
3669 {
chaviw0e3479f2018-09-10 16:49:30 -07003670 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003671 mCapture->expectFGColor(64, 64);
3672 mCapture->expectChildColor(74, 74);
3673 mCapture->expectFGColor(84, 84);
3674 }
3675}
3676
chaviw5aedec92018-10-22 10:40:38 -07003677TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
3678 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
3679 sp<SurfaceControl> childNewClient =
3680 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
3681 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3682
3683 ASSERT_TRUE(childNewClient != nullptr);
3684 ASSERT_TRUE(childNewClient->isValid());
3685
3686 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
3687
3688 Transaction()
3689 .hide(mChild)
3690 .show(childNewClient)
3691 .setPosition(childNewClient, 10, 10)
3692 .setPosition(mFGSurfaceControl, 64, 64)
3693 .apply();
3694
3695 {
3696 mCapture = screenshot();
3697 // Top left of foreground must now be visible
3698 mCapture->expectFGColor(64, 64);
3699 // But 10 pixels in we should see the child surface
3700 mCapture->expectChildColor(74, 74);
3701 // And 10 more pixels we should be back to the foreground surface
3702 mCapture->expectFGColor(84, 84);
3703 }
3704
3705 Transaction().detachChildren(mFGSurfaceControl).apply();
3706 Transaction().hide(childNewClient).apply();
3707
3708 // Nothing should have changed.
3709 {
3710 mCapture = screenshot();
3711 mCapture->expectFGColor(64, 64);
3712 mCapture->expectChildColor(74, 74);
3713 mCapture->expectFGColor(84, 84);
3714 }
3715
3716 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
3717 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
3718 32);
3719 Transaction()
3720 .setLayer(newParentSurface, INT32_MAX - 1)
3721 .show(newParentSurface)
3722 .setPosition(newParentSurface, 20, 20)
3723 .reparent(childNewClient, newParentSurface->getHandle())
3724 .apply();
3725 {
3726 mCapture = screenshot();
3727 // Child is now hidden.
3728 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
3729 }
3730}
3731
Robert Carr9b429f42017-04-17 14:56:57 -07003732TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003733 asTransaction([&](Transaction& t) {
3734 t.show(mChild);
3735 t.setPosition(mChild, 0, 0);
3736 t.setPosition(mFGSurfaceControl, 0, 0);
3737 });
Robert Carr9b429f42017-04-17 14:56:57 -07003738
3739 {
chaviw0e3479f2018-09-10 16:49:30 -07003740 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003741 // We've positioned the child in the top left.
3742 mCapture->expectChildColor(0, 0);
3743 // But it's only 10x10.
3744 mCapture->expectFGColor(10, 10);
3745 }
3746
Robert Carr4cdc58f2017-08-23 14:22:20 -07003747 asTransaction([&](Transaction& t) {
3748 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3749 // We cause scaling by 2.
3750 t.setSize(mFGSurfaceControl, 128, 128);
3751 });
Robert Carr9b429f42017-04-17 14:56:57 -07003752
3753 {
chaviw0e3479f2018-09-10 16:49:30 -07003754 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003755 // We've positioned the child in the top left.
3756 mCapture->expectChildColor(0, 0);
3757 mCapture->expectChildColor(10, 10);
3758 mCapture->expectChildColor(19, 19);
3759 // And now it should be scaled all the way to 20x20
3760 mCapture->expectFGColor(20, 20);
3761 }
3762}
3763
Robert Carr1725eee2017-04-26 18:32:15 -07003764// Regression test for b/37673612
3765TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003766 asTransaction([&](Transaction& t) {
3767 t.show(mChild);
3768 t.setPosition(mChild, 0, 0);
3769 t.setPosition(mFGSurfaceControl, 0, 0);
3770 });
Robert Carr1725eee2017-04-26 18:32:15 -07003771
3772 {
chaviw0e3479f2018-09-10 16:49:30 -07003773 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003774 // We've positioned the child in the top left.
3775 mCapture->expectChildColor(0, 0);
3776 // But it's only 10x10.
3777 mCapture->expectFGColor(10, 10);
3778 }
Robert Carr1725eee2017-04-26 18:32:15 -07003779 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
3780 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003781 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07003782 sp<Surface> s = mFGSurfaceControl->getSurface();
3783 auto anw = static_cast<ANativeWindow*>(s.get());
3784 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
3785 native_window_set_buffers_dimensions(anw, 64, 128);
3786 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3787 waitForPostedBuffers();
3788
3789 {
3790 // The child should still be in the same place and not have any strange scaling as in
3791 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07003792 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003793 mCapture->expectChildColor(0, 0);
3794 mCapture->expectFGColor(10, 10);
3795 }
3796}
3797
Dan Stoza412903f2017-04-27 13:42:17 -07003798TEST_F(ChildLayerTest, Bug36858924) {
3799 // Destroy the child layer
3800 mChild.clear();
3801
3802 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08003803 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
3804 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07003805
3806 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07003807 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003808 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
3809 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003810 t.show(mChild);
3811 });
Dan Stoza412903f2017-04-27 13:42:17 -07003812
3813 // Render the foreground surface a few times
3814 //
3815 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
3816 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
3817 // never acquire/release the first buffer
3818 ALOGI("Filling 1");
3819 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3820 ALOGI("Filling 2");
3821 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
3822 ALOGI("Filling 3");
3823 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
3824 ALOGI("Filling 4");
3825 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3826}
3827
chaviwf1961f72017-09-18 16:41:07 -07003828TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003829 asTransaction([&](Transaction& t) {
3830 t.show(mChild);
3831 t.setPosition(mChild, 10, 10);
3832 t.setPosition(mFGSurfaceControl, 64, 64);
3833 });
chaviw06178942017-07-27 10:25:59 -07003834
3835 {
chaviw0e3479f2018-09-10 16:49:30 -07003836 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003837 // Top left of foreground must now be visible
3838 mCapture->expectFGColor(64, 64);
3839 // But 10 pixels in we should see the child surface
3840 mCapture->expectChildColor(74, 74);
3841 // And 10 more pixels we should be back to the foreground surface
3842 mCapture->expectFGColor(84, 84);
3843 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003844
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003845 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07003846
chaviw06178942017-07-27 10:25:59 -07003847 {
chaviw0e3479f2018-09-10 16:49:30 -07003848 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003849 mCapture->expectFGColor(64, 64);
3850 // In reparenting we should have exposed the entire foreground surface.
3851 mCapture->expectFGColor(74, 74);
3852 // And the child layer should now begin at 10, 10 (since the BG
3853 // layer is at (0, 0)).
3854 mCapture->expectBGColor(9, 9);
3855 mCapture->expectChildColor(10, 10);
3856 }
3857}
3858
chaviwf1961f72017-09-18 16:41:07 -07003859TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003860 asTransaction([&](Transaction& t) {
3861 t.show(mChild);
3862 t.setPosition(mChild, 10, 10);
3863 t.setPosition(mFGSurfaceControl, 64, 64);
3864 });
chaviwf1961f72017-09-18 16:41:07 -07003865
3866 {
chaviw0e3479f2018-09-10 16:49:30 -07003867 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003868 // Top left of foreground must now be visible
3869 mCapture->expectFGColor(64, 64);
3870 // But 10 pixels in we should see the child surface
3871 mCapture->expectChildColor(74, 74);
3872 // And 10 more pixels we should be back to the foreground surface
3873 mCapture->expectFGColor(84, 84);
3874 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003875 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07003876 {
chaviw0e3479f2018-09-10 16:49:30 -07003877 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003878 // Nothing should have changed.
3879 mCapture->expectFGColor(64, 64);
3880 mCapture->expectChildColor(74, 74);
3881 mCapture->expectFGColor(84, 84);
3882 }
3883}
3884
3885TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07003886 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003887 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07003888 ASSERT_TRUE(newSurface->isValid());
3889
3890 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003891 asTransaction([&](Transaction& t) {
3892 t.hide(mChild);
3893 t.show(newSurface);
3894 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003895 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003896 t.setPosition(mFGSurfaceControl, 64, 64);
3897 });
chaviwf1961f72017-09-18 16:41:07 -07003898
3899 {
chaviw0e3479f2018-09-10 16:49:30 -07003900 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003901 // Top left of foreground must now be visible
3902 mCapture->expectFGColor(64, 64);
3903 // At 10, 10 we should see the new surface
3904 mCapture->checkPixel(10, 10, 63, 195, 63);
3905 }
3906
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003907 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07003908
3909 {
chaviw0e3479f2018-09-10 16:49:30 -07003910 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003911 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
3912 // mFGSurface, putting it at 74, 74.
3913 mCapture->expectFGColor(64, 64);
3914 mCapture->checkPixel(74, 74, 63, 195, 63);
3915 mCapture->expectFGColor(84, 84);
3916 }
3917}
3918
chaviwc9674332017-08-28 12:32:18 -07003919TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003920 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
3921 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07003922 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3923
3924 {
chaviw0e3479f2018-09-10 16:49:30 -07003925 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07003926 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
3927 // which begins at 64, 64
3928 mCapture->checkPixel(64, 64, 50, 50, 50);
3929 }
3930}
3931
Robert Carr503c7042017-09-27 15:06:08 -07003932TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003933 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07003934 fillSurfaceRGBA8(relative, 255, 255, 255);
3935
3936 Transaction t;
3937 t.setLayer(relative, INT32_MAX)
3938 .setRelativeLayer(mChild, relative->getHandle(), 1)
3939 .setPosition(mFGSurfaceControl, 0, 0)
3940 .apply(true);
3941
3942 // We expect that the child should have been elevated above our
3943 // INT_MAX layer even though it's not a child of it.
3944 {
chaviw0e3479f2018-09-10 16:49:30 -07003945 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07003946 mCapture->expectChildColor(0, 0);
3947 mCapture->expectChildColor(9, 9);
3948 mCapture->checkPixel(10, 10, 255, 255, 255);
3949 }
3950}
Vishnu Nair60356342018-11-13 13:00:45 -08003951class BoundlessLayerTest : public LayerUpdateTest {
3952protected:
3953 std::unique_ptr<ScreenCapture> mCapture;
3954};
3955
3956// Verify setting a size on a buffer layer has no effect.
3957TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
3958 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003959 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
3960 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003961 ASSERT_TRUE(bufferLayer->isValid());
3962 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
3963 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
3964 {
3965 mCapture = screenshot();
3966 // Top left of background must now be visible
3967 mCapture->expectBGColor(0, 0);
3968 // Foreground Surface bounds must be color layer
3969 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
3970 // Buffer layer should not extend past buffer bounds
3971 mCapture->expectFGColor(95, 95);
3972 }
3973}
3974
3975// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
3976// which will crop the color layer.
3977TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
3978 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003979 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3980 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003981 ASSERT_TRUE(colorLayer->isValid());
3982 asTransaction([&](Transaction& t) {
3983 t.setColor(colorLayer, half3{0, 0, 0});
3984 t.show(colorLayer);
3985 });
3986 {
3987 mCapture = screenshot();
3988 // Top left of background must now be visible
3989 mCapture->expectBGColor(0, 0);
3990 // Foreground Surface bounds must be color layer
3991 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3992 // Color layer should not extend past foreground bounds
3993 mCapture->expectBGColor(129, 129);
3994 }
3995}
3996
3997// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
3998// a crop which will be used to crop the color layer.
3999TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004000 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4001 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004002 ASSERT_TRUE(cropLayer->isValid());
4003 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004004 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4005 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004006 ASSERT_TRUE(colorLayer->isValid());
4007 asTransaction([&](Transaction& t) {
4008 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4009 t.setColor(colorLayer, half3{0, 0, 0});
4010 t.show(cropLayer);
4011 t.show(colorLayer);
4012 });
4013 {
4014 mCapture = screenshot();
4015 // Top left of background must now be visible
4016 mCapture->expectBGColor(0, 0);
4017 // Top left of foreground must now be visible
4018 mCapture->expectFGColor(64, 64);
4019 // 5 pixels from the foreground we should see the child surface
4020 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4021 // 10 pixels from the foreground we should be back to the foreground surface
4022 mCapture->expectFGColor(74, 74);
4023 }
4024}
4025
4026// Verify for boundless layer with no children, their transforms have no effect.
4027TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4028 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004029 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4030 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004031 ASSERT_TRUE(colorLayer->isValid());
4032 asTransaction([&](Transaction& t) {
4033 t.setPosition(colorLayer, 320, 320);
4034 t.setMatrix(colorLayer, 2, 0, 0, 2);
4035 t.setColor(colorLayer, half3{0, 0, 0});
4036 t.show(colorLayer);
4037 });
4038 {
4039 mCapture = screenshot();
4040 // Top left of background must now be visible
4041 mCapture->expectBGColor(0, 0);
4042 // Foreground Surface bounds must be color layer
4043 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4044 // Color layer should not extend past foreground bounds
4045 mCapture->expectBGColor(129, 129);
4046 }
4047}
4048
4049// Verify for boundless layer with children, their transforms have an effect.
4050TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4051 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004052 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4053 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004054 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4055 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004056 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4057 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004058 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4059 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004060 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4061 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004062 ASSERT_TRUE(colorLayer->isValid());
4063 asTransaction([&](Transaction& t) {
4064 t.setPosition(boundlessLayerRightShift, 32, 0);
4065 t.show(boundlessLayerRightShift);
4066 t.setPosition(boundlessLayerDownShift, 0, 32);
4067 t.show(boundlessLayerDownShift);
4068 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4069 t.setColor(colorLayer, half3{0, 0, 0});
4070 t.show(colorLayer);
4071 });
4072 {
4073 mCapture = screenshot();
4074 // Top left of background must now be visible
4075 mCapture->expectBGColor(0, 0);
4076 // Top left of foreground must now be visible
4077 mCapture->expectFGColor(64, 64);
4078 // Foreground Surface bounds must be color layer
4079 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4080 // Color layer should not extend past foreground bounds
4081 mCapture->expectBGColor(129, 129);
4082 }
4083}
4084
4085// Verify child layers do not get clipped if they temporarily move into the negative
4086// coordinate space as the result of an intermediate transformation.
4087TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4088 sp<SurfaceControl> boundlessLayer =
4089 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4090 0 /* flags */, mFGSurfaceControl.get());
4091 ASSERT_TRUE(boundlessLayer != nullptr);
4092 ASSERT_TRUE(boundlessLayer->isValid());
4093 sp<SurfaceControl> colorLayer =
4094 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4095 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4096 ASSERT_TRUE(colorLayer != nullptr);
4097 ASSERT_TRUE(colorLayer->isValid());
4098 asTransaction([&](Transaction& t) {
4099 // shift child layer off bounds. If this layer was not boundless, we will
4100 // expect the child layer to be cropped.
4101 t.setPosition(boundlessLayer, 32, 32);
4102 t.show(boundlessLayer);
4103 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4104 // undo shift by parent
4105 t.setPosition(colorLayer, -32, -32);
4106 t.setColor(colorLayer, half3{0, 0, 0});
4107 t.show(colorLayer);
4108 });
4109 {
4110 mCapture = screenshot();
4111 // Top left of background must now be visible
4112 mCapture->expectBGColor(0, 0);
4113 // Foreground Surface bounds must be color layer
4114 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4115 // Color layer should not extend past foreground bounds
4116 mCapture->expectBGColor(129, 129);
4117 }
4118}
4119
4120// Verify for boundless root layers with children, their transforms have an effect.
4121TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004122 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4123 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004124 ASSERT_TRUE(rootBoundlessLayer->isValid());
4125 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004126 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4127 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4128
Vishnu Nair60356342018-11-13 13:00:45 -08004129 ASSERT_TRUE(colorLayer->isValid());
4130 asTransaction([&](Transaction& t) {
4131 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4132 t.setPosition(rootBoundlessLayer, 32, 32);
4133 t.show(rootBoundlessLayer);
4134 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4135 t.setColor(colorLayer, half3{0, 0, 0});
4136 t.show(colorLayer);
4137 t.hide(mFGSurfaceControl);
4138 });
4139 {
4140 mCapture = screenshot();
4141 // Top left of background must now be visible
4142 mCapture->expectBGColor(0, 0);
4143 // Top left of foreground must now be visible
4144 mCapture->expectBGColor(31, 31);
4145 // Foreground Surface bounds must be color layer
4146 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4147 // Color layer should not extend past foreground bounds
4148 mCapture->expectBGColor(97, 97);
4149 }
4150}
Robert Carr503c7042017-09-27 15:06:08 -07004151
chaviwa76b2712017-09-20 12:02:26 -07004152class ScreenCaptureTest : public LayerUpdateTest {
4153protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004154 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004155};
4156
4157TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4158 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004159 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004160 mCapture->expectBGColor(0, 0);
4161 // Doesn't capture FG layer which is at 64, 64
4162 mCapture->expectBGColor(64, 64);
4163}
4164
4165TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4166 auto fgHandle = mFGSurfaceControl->getHandle();
4167
Vishnu Nair88a11f22018-11-28 18:30:57 -08004168 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4169 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004170 fillSurfaceRGBA8(child, 200, 200, 200);
4171
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004172 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004173
4174 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004175 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004176 mCapture->expectFGColor(10, 10);
4177 mCapture->expectChildColor(0, 0);
4178}
4179
Robert Carr578038f2018-03-09 12:25:24 -08004180TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4181 auto fgHandle = mFGSurfaceControl->getHandle();
4182
Vishnu Nair88a11f22018-11-28 18:30:57 -08004183 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4184 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004185 fillSurfaceRGBA8(child, 200, 200, 200);
4186
4187 SurfaceComposerClient::Transaction().show(child).apply(true);
4188
4189 // Captures mFGSurfaceControl's child
4190 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4191 mCapture->checkPixel(10, 10, 0, 0, 0);
4192 mCapture->expectChildColor(0, 0);
4193}
4194
chaviw50da5042018-04-09 13:49:37 -07004195TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004196 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4197 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07004198
4199 fillSurfaceRGBA8(child, 200, 200, 200);
4200
4201 SurfaceComposerClient::Transaction().show(child).apply(true);
4202
4203 auto childHandle = child->getHandle();
4204
4205 // Captures child
4206 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
4207 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
4208 // Area outside of child's bounds is transparent.
4209 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
4210}
4211
chaviw4b129c22018-04-09 16:19:43 -07004212TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
4213 auto fgHandle = mFGSurfaceControl->getHandle();
4214
Vishnu Nair88a11f22018-11-28 18:30:57 -08004215 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4216 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4217 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07004218 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07004219 fillSurfaceRGBA8(child, 200, 200, 200);
4220 fillSurfaceRGBA8(relative, 100, 100, 100);
4221
4222 SurfaceComposerClient::Transaction()
4223 .show(child)
4224 // Set relative layer above fg layer so should be shown above when computing all layers.
4225 .setRelativeLayer(relative, fgHandle, 1)
4226 .show(relative)
4227 .apply(true);
4228
4229 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
4230 ScreenCapture::captureLayers(&mCapture, fgHandle);
4231 mCapture->expectFGColor(10, 10);
4232 mCapture->expectChildColor(0, 0);
4233}
4234
4235TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
4236 auto fgHandle = mFGSurfaceControl->getHandle();
4237
Vishnu Nair88a11f22018-11-28 18:30:57 -08004238 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4239 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4240 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
4241 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07004242 fillSurfaceRGBA8(child, 200, 200, 200);
4243 fillSurfaceRGBA8(relative, 100, 100, 100);
4244
4245 SurfaceComposerClient::Transaction()
4246 .show(child)
4247 // Set relative layer below fg layer but relative to child layer so it should be shown
4248 // above child layer.
4249 .setLayer(relative, -1)
4250 .setRelativeLayer(relative, child->getHandle(), 1)
4251 .show(relative)
4252 .apply(true);
4253
4254 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
4255 // relative value should be taken into account, placing it above child layer.
4256 ScreenCapture::captureLayers(&mCapture, fgHandle);
4257 mCapture->expectFGColor(10, 10);
4258 // Relative layer is showing on top of child layer
4259 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
4260}
Robert Carr578038f2018-03-09 12:25:24 -08004261
4262// In the following tests we verify successful skipping of a parent layer,
4263// so we use the same verification logic and only change how we mutate
4264// the parent layer to verify that various properties are ignored.
4265class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
4266public:
4267 void SetUp() override {
4268 LayerUpdateTest::SetUp();
4269
Vishnu Nair88a11f22018-11-28 18:30:57 -08004270 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4271 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004272 fillSurfaceRGBA8(mChild, 200, 200, 200);
4273
4274 SurfaceComposerClient::Transaction().show(mChild).apply(true);
4275 }
4276
4277 void verify() {
4278 auto fgHandle = mFGSurfaceControl->getHandle();
4279 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4280 mCapture->checkPixel(10, 10, 0, 0, 0);
4281 mCapture->expectChildColor(0, 0);
4282 }
4283
4284 std::unique_ptr<ScreenCapture> mCapture;
4285 sp<SurfaceControl> mChild;
4286};
4287
4288TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4289
4290 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4291
4292 // Even though the parent is hidden we should still capture the child.
4293 verify();
4294}
4295
4296TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004297 SurfaceComposerClient::Transaction()
4298 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
4299 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08004300
4301 // Even though the parent is cropped out we should still capture the child.
4302 verify();
4303}
4304
4305TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4306
4307 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4308
4309 // We should not inherit the parent scaling.
4310 verify();
4311}
4312
Robert Carr15eae092018-03-23 13:43:53 -07004313TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4314 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4315
4316 // Even though the parent is hidden we should still capture the child.
4317 verify();
4318
4319 // Verify everything was properly hidden when rendering the full-screen.
4320 screenshot()->expectBGColor(0,0);
4321}
4322
4323
chaviwa76b2712017-09-20 12:02:26 -07004324TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4325 auto fgHandle = mFGSurfaceControl->getHandle();
4326
Vishnu Nair88a11f22018-11-28 18:30:57 -08004327 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4328 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004329 fillSurfaceRGBA8(child, 200, 200, 200);
4330
Vishnu Nair88a11f22018-11-28 18:30:57 -08004331 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4332 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004333
4334 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4335 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004336 .show(child)
4337 .setPosition(grandchild, 5, 5)
4338 .show(grandchild)
4339 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004340
4341 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004342 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004343 mCapture->expectFGColor(10, 10);
4344 mCapture->expectChildColor(0, 0);
4345 mCapture->checkPixel(5, 5, 50, 50, 50);
4346}
4347
4348TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004349 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4350 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004351 fillSurfaceRGBA8(child, 200, 200, 200);
4352 auto childHandle = child->getHandle();
4353
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004354 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004355
4356 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004357 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004358 mCapture->expectChildColor(0, 0);
4359 mCapture->expectChildColor(9, 9);
4360}
4361
4362TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004363 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4364 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004365 fillSurfaceRGBA8(child, 200, 200, 200);
4366 auto childHandle = child->getHandle();
4367
Vishnu Nair88a11f22018-11-28 18:30:57 -08004368 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4369 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004370 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4371
4372 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004373 .show(child)
4374 .setPosition(grandchild, 5, 5)
4375 .show(grandchild)
4376 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004377
4378 auto grandchildHandle = grandchild->getHandle();
4379
4380 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004381 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004382 mCapture->checkPixel(0, 0, 50, 50, 50);
4383 mCapture->checkPixel(4, 4, 50, 50, 50);
4384}
4385
chaviw7206d492017-11-10 16:16:12 -08004386TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004387 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004388 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4389 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004390
Marissa Wall61c58622018-07-18 10:12:20 -07004391 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4392 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004393
4394 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004395 .setLayer(redLayer, INT32_MAX - 1)
4396 .show(redLayer)
4397 .show(blueLayer)
4398 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004399
4400 auto redLayerHandle = redLayer->getHandle();
4401
4402 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004403 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4404 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4405 // red area below the blue area
4406 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4407 // red area to the right of the blue area
4408 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004409
Marissa Wall861616d2018-10-22 12:52:23 -07004410 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004411 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004412 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4413 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004414 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004415 mCapture->checkPixel(30, 30, 0, 0, 0);
4416}
4417
4418TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004419 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004420 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4421 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004422
Marissa Wall61c58622018-07-18 10:12:20 -07004423 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4424 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004425
4426 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004427 .setLayer(redLayer, INT32_MAX - 1)
4428 .show(redLayer)
4429 .show(blueLayer)
4430 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004431
4432 auto redLayerHandle = redLayer->getHandle();
4433
4434 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004435 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4436 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4437 // red area below the blue area
4438 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4439 // red area to the right of the blue area
4440 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004441
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004442 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004443 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004444 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4445 // red area below the blue area
4446 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4447 // red area to the right of the blue area
4448 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004449 mCapture->checkPixel(30, 30, 0, 0, 0);
4450}
4451
4452TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004453 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004454
Marissa Wall61c58622018-07-18 10:12:20 -07004455 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004456
4457 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07004458 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08004459 SurfaceComposerClient::Transaction().apply(true);
4460
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004461 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004462
4463 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004464 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4465 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004466}
4467
chaviw8e3fe5d2018-02-22 10:55:42 -08004468
4469class DereferenceSurfaceControlTest : public LayerTransactionTest {
4470protected:
4471 void SetUp() override {
4472 LayerTransactionTest::SetUp();
4473 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004474 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004475 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004476 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004477 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4478 {
4479 SCOPED_TRACE("before anything");
4480 auto shot = screenshot();
4481 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4482 }
4483 }
4484 void TearDown() override {
4485 LayerTransactionTest::TearDown();
4486 bgLayer = 0;
4487 fgLayer = 0;
4488 }
4489
4490 sp<SurfaceControl> bgLayer;
4491 sp<SurfaceControl> fgLayer;
4492};
4493
4494TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4495 fgLayer = nullptr;
4496 {
4497 SCOPED_TRACE("after setting null");
4498 auto shot = screenshot();
4499 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4500 }
4501}
4502
4503TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4504 auto transaction = Transaction().show(fgLayer);
4505 fgLayer = nullptr;
4506 {
4507 SCOPED_TRACE("after setting null");
4508 auto shot = screenshot();
4509 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4510 }
4511}
4512
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004513} // namespace android