blob: e62fc6e6c8720d99704aca2a3bdd538cddfd3b32 [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
Marissa Wall861616d2018-10-22 12:52:23 -07001713// TODO (marissaw): change Layer to make crop to be in bounds instead of passing a bad crop to hwc
1714// TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1715// sp<SurfaceControl> layer;
1716// ASSERT_NO_FATAL_FAILURE(
1717// layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1718// ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1719//
1720// Transaction()
1721// .setCrop(layer, Rect(-128, -64, 128, 64))
1722// .setFrame(layer, Rect(0, 0, 32, 32))
1723// .apply();
1724// auto shot = screenshot();
1725// shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1726// shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1727//}
1728//
Marissa Wall61c58622018-07-18 10:12:20 -07001729TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001730 sp<SurfaceControl> layer;
1731 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001732 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001733
1734 const Point position(32, 32);
1735 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001736 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001737 auto shot = screenshot();
1738 shot->expectColor(crop + position, Color::RED);
1739 shot->expectBorder(crop + position, Color::BLACK);
1740}
1741
Marissa Wall61c58622018-07-18 10:12:20 -07001742TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1743 sp<SurfaceControl> layer;
1744 ASSERT_NO_FATAL_FAILURE(
1745 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1746 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1747
Marissa Wall861616d2018-10-22 12:52:23 -07001748 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001749 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001750 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001751 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001752 shot->expectColor(frame, Color::RED);
1753 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001754}
1755
1756TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001757 sp<SurfaceControl> layer;
1758 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001759 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001760
Marissa Wall861616d2018-10-22 12:52:23 -07001761 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001762 Transaction()
1763 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001764 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001765 .apply();
1766 auto shot = screenshot();
1767 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1768 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1769}
1770
Marissa Wall61c58622018-07-18 10:12:20 -07001771TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001772 sp<SurfaceControl> layer;
1773 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001774 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001775
Marissa Wallf58c14b2018-07-24 10:50:43 -07001776 // setCrop_legacy is applied immediately by default, with or without resize pending
1777 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001778 {
1779 SCOPED_TRACE("resize pending");
1780 auto shot = screenshot();
1781 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1782 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1783 }
1784
Marissa Wall61c58622018-07-18 10:12:20 -07001785 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001786 {
1787 SCOPED_TRACE("resize applied");
1788 auto shot = screenshot();
1789 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1790 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1791 }
1792}
1793
Marissa Wall61c58622018-07-18 10:12:20 -07001794TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001795 sp<SurfaceControl> layer;
1796 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001797 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001798
Marissa Wallf58c14b2018-07-24 10:50:43 -07001799 // request setCrop_legacy to be applied with the next resize
1800 Transaction()
1801 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1802 .setGeometryAppliesWithResize(layer)
1803 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001804 {
1805 SCOPED_TRACE("waiting for next resize");
1806 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1807 }
1808
Marissa Wallf58c14b2018-07-24 10:50:43 -07001809 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001810 {
1811 SCOPED_TRACE("pending crop modified");
1812 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1813 }
1814
1815 Transaction().setSize(layer, 16, 16).apply();
1816 {
1817 SCOPED_TRACE("resize pending");
1818 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1819 }
1820
1821 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001822 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001823 {
1824 SCOPED_TRACE("new crop applied");
1825 auto shot = screenshot();
1826 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1827 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1828 }
1829}
1830
Marissa Wall61c58622018-07-18 10:12:20 -07001831TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001832 sp<SurfaceControl> layer;
1833 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001834 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001835
Marissa Wallf58c14b2018-07-24 10:50:43 -07001836 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001837 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001838 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001839 .setSize(layer, 16, 16)
1840 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1841 .setGeometryAppliesWithResize(layer)
1842 .apply();
1843 {
1844 SCOPED_TRACE("new crop pending");
1845 auto shot = screenshot();
1846 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1847 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1848 }
1849
1850 // XXX crop is never latched without other geometry change (b/69315677)
1851 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
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 Transaction().setPosition(layer, 0, 0).apply();
1854 {
1855 SCOPED_TRACE("new crop applied");
1856 auto shot = screenshot();
1857 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1858 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1859 }
1860}
1861
Marissa Wall861616d2018-10-22 12:52:23 -07001862TEST_F(LayerTransactionTest, SetFrameBasic_BufferState) {
1863 sp<SurfaceControl> layer;
1864 ASSERT_NO_FATAL_FAILURE(
1865 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1866 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1867 const Rect frame(8, 8, 24, 24);
1868
1869 Transaction().setFrame(layer, frame).apply();
1870 auto shot = screenshot();
1871 shot->expectColor(frame, Color::RED);
1872 shot->expectBorder(frame, Color::BLACK);
1873}
1874
1875TEST_F(LayerTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001876 sp<SurfaceControl> layer;
1877 ASSERT_NO_FATAL_FAILURE(
1878 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1879 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1880
Marissa Wall61c58622018-07-18 10:12:20 -07001881 {
Marissa Wall861616d2018-10-22 12:52:23 -07001882 SCOPED_TRACE("empty rect");
1883 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
1884 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001885 }
1886
Marissa Wall61c58622018-07-18 10:12:20 -07001887 {
Marissa Wall861616d2018-10-22 12:52:23 -07001888 SCOPED_TRACE("negative rect");
1889 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
1890 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001891 }
1892}
1893
Marissa Wall861616d2018-10-22 12:52:23 -07001894TEST_F(LayerTransactionTest, SetFrameDefaultParentless_BufferState) {
1895 sp<SurfaceControl> layer;
1896 ASSERT_NO_FATAL_FAILURE(
1897 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1898 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
1899
1900 // A parentless layer will default to a frame with the same size as the buffer
1901 auto shot = screenshot();
1902 shot->expectColor(Rect(0, 0, 10, 10), Color::RED);
1903 shot->expectBorder(Rect(0, 0, 10, 10), Color::BLACK);
1904}
1905
1906TEST_F(LayerTransactionTest, SetFrameDefaultBSParent_BufferState) {
1907 sp<SurfaceControl> parent, child;
1908 ASSERT_NO_FATAL_FAILURE(
1909 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1910 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
1911 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
1912
1913 ASSERT_NO_FATAL_FAILURE(
1914 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1915 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1916
1917 Transaction().reparent(child, parent->getHandle()).apply();
1918
1919 // A layer will default to the frame of its parent
1920 auto shot = screenshot();
1921 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1922 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1923}
1924
1925TEST_F(LayerTransactionTest, SetFrameDefaultBQParent_BufferState) {
1926 sp<SurfaceControl> parent, child;
1927 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
1928 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
1929
1930 ASSERT_NO_FATAL_FAILURE(
1931 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1932 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1933
1934 Transaction().reparent(child, parent->getHandle()).apply();
1935
1936 // A layer will default to the frame of its parent
1937 auto shot = screenshot();
1938 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1939 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1940}
1941
1942TEST_F(LayerTransactionTest, SetFrameUpdate_BufferState) {
1943 sp<SurfaceControl> layer;
1944 ASSERT_NO_FATAL_FAILURE(
1945 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1946 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1947 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
1948
1949 std::this_thread::sleep_for(500ms);
1950
1951 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
1952
1953 auto shot = screenshot();
1954 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1955 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1956}
1957
1958TEST_F(LayerTransactionTest, SetFrameOutsideBounds_BufferState) {
1959 sp<SurfaceControl> parent, child;
1960 ASSERT_NO_FATAL_FAILURE(
1961 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1962 ASSERT_NO_FATAL_FAILURE(
1963 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1964 Transaction().reparent(child, parent->getHandle()).apply();
1965
1966 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
1967 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
1968
1969 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1970 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
1971
1972 auto shot = screenshot();
1973 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
1974 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
1975 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1976}
1977
Marissa Wall61c58622018-07-18 10:12:20 -07001978TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1979 sp<SurfaceControl> layer;
1980 ASSERT_NO_FATAL_FAILURE(
1981 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1982
1983 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1984
1985 auto shot = screenshot();
1986 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1987 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1988}
1989
1990TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1991 sp<SurfaceControl> layer;
1992 ASSERT_NO_FATAL_FAILURE(
1993 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1994
1995 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1996
1997 {
1998 SCOPED_TRACE("set buffer 1");
1999 auto shot = screenshot();
2000 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2001 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2002 }
2003
2004 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2005
2006 {
2007 SCOPED_TRACE("set buffer 2");
2008 auto shot = screenshot();
2009 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2010 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2011 }
2012
2013 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2014
2015 {
2016 SCOPED_TRACE("set buffer 3");
2017 auto shot = screenshot();
2018 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2019 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2020 }
2021}
2022
2023TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
2024 sp<SurfaceControl> layer1;
2025 ASSERT_NO_FATAL_FAILURE(
2026 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2027
2028 sp<SurfaceControl> layer2;
2029 ASSERT_NO_FATAL_FAILURE(
2030 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2031
2032 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2033
Marissa Wall861616d2018-10-22 12:52:23 -07002034 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002035 {
2036 SCOPED_TRACE("set layer 1 buffer red");
2037 auto shot = screenshot();
2038 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2039 }
2040
2041 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2042
Marissa Wall861616d2018-10-22 12:52:23 -07002043 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002044 {
2045 SCOPED_TRACE("set layer 2 buffer blue");
2046 auto shot = screenshot();
2047 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2048 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2049 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2050 }
2051
2052 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2053 {
2054 SCOPED_TRACE("set layer 1 buffer green");
2055 auto shot = screenshot();
2056 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2057 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2058 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2059 }
2060
2061 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2062
2063 {
2064 SCOPED_TRACE("set layer 2 buffer white");
2065 auto shot = screenshot();
2066 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2067 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2068 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2069 }
2070}
2071
2072TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
2073 sp<SurfaceControl> layer;
2074 ASSERT_NO_FATAL_FAILURE(
2075 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2076
2077 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2078 Color::BLUE, Color::WHITE));
2079
Marissa Wall861616d2018-10-22 12:52:23 -07002080 Transaction()
2081 .setFrame(layer, Rect(0, 0, 32, 32))
2082 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2083 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002084
2085 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2086 Color::GREEN, true /* filtered */);
2087}
2088
2089TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
2090 sp<SurfaceControl> layer;
2091 ASSERT_NO_FATAL_FAILURE(
2092 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2093
2094 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2095 Color::BLUE, Color::WHITE));
2096
Marissa Wall861616d2018-10-22 12:52:23 -07002097 Transaction()
2098 .setFrame(layer, Rect(0, 0, 32, 32))
2099 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2100 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002101
2102 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2103 Color::BLUE, true /* filtered */);
2104}
2105
2106TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
2107 sp<SurfaceControl> layer;
2108 ASSERT_NO_FATAL_FAILURE(
2109 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2110
2111 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2112 Color::BLUE, Color::WHITE));
2113
Marissa Wall861616d2018-10-22 12:52:23 -07002114 Transaction()
2115 .setFrame(layer, Rect(0, 0, 32, 32))
2116 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2117 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002118
2119 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2120 Color::GREEN, true /* filtered */);
2121}
2122
2123TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2124 sp<SurfaceControl> layer;
2125 ASSERT_NO_FATAL_FAILURE(
2126 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2127
2128 Transaction().setTransformToDisplayInverse(layer, false).apply();
2129
2130 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2131
2132 Transaction().setTransformToDisplayInverse(layer, true).apply();
2133}
2134
2135TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
2136 sp<SurfaceControl> layer;
2137 ASSERT_NO_FATAL_FAILURE(
2138 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2139
2140 sp<GraphicBuffer> buffer =
2141 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2142 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2143 BufferUsage::COMPOSER_OVERLAY,
2144 "test");
2145 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2146
Marissa Wallfda30bb2018-10-12 11:34:28 -07002147 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002148
2149 Transaction()
2150 .setBuffer(layer, buffer)
2151 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002152 .apply();
2153
2154 auto shot = screenshot();
2155 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2156 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2157}
2158
2159TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2160 sp<SurfaceControl> layer;
2161 ASSERT_NO_FATAL_FAILURE(
2162 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2163
2164 sp<GraphicBuffer> buffer =
2165 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2166 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2167 BufferUsage::COMPOSER_OVERLAY,
2168 "test");
2169 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2170
2171 Transaction()
2172 .setBuffer(layer, buffer)
2173 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002174 .apply();
2175
2176 auto shot = screenshot();
2177 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2178 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2179}
2180
2181TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2182 sp<SurfaceControl> layer;
2183 ASSERT_NO_FATAL_FAILURE(
2184 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2185
2186 sp<GraphicBuffer> buffer =
2187 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2188 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2189 BufferUsage::COMPOSER_OVERLAY,
2190 "test");
2191 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2192
2193 HdrMetadata hdrMetadata;
2194 hdrMetadata.validTypes = 0;
2195 Transaction()
2196 .setBuffer(layer, buffer)
2197 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002198 .apply();
2199
2200 auto shot = screenshot();
2201 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2202 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2203}
2204
2205TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2206 sp<SurfaceControl> layer;
2207 ASSERT_NO_FATAL_FAILURE(
2208 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2209
2210 sp<GraphicBuffer> buffer =
2211 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2212 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2213 BufferUsage::COMPOSER_OVERLAY,
2214 "test");
2215 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2216
2217 Region region;
2218 region.set(32, 32);
2219 Transaction()
2220 .setBuffer(layer, buffer)
2221 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002222 .apply();
2223
2224 auto shot = screenshot();
2225 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2226 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2227}
2228
2229TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2230 sp<SurfaceControl> layer;
2231 ASSERT_NO_FATAL_FAILURE(
2232 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2233
2234 sp<GraphicBuffer> buffer =
2235 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2236 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2237 BufferUsage::COMPOSER_OVERLAY,
2238 "test");
2239 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2240
2241 Transaction()
2242 .setBuffer(layer, buffer)
2243 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002244 .apply();
2245
2246 auto shot = screenshot();
2247 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2248 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2249}
2250
2251TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2252 sp<SurfaceControl> layer;
2253 ASSERT_NO_FATAL_FAILURE(
2254 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2255
2256 // verify this doesn't cause a crash
2257 Transaction().setSidebandStream(layer, nullptr).apply();
2258}
2259
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002260class ColorTransformHelper {
2261public:
2262 static void DegammaColorSingle(half& s) {
2263 if (s <= 0.03928f)
2264 s = s / 12.92f;
2265 else
2266 s = pow((s + 0.055f) / 1.055f, 2.4f);
2267 }
2268
2269 static void DegammaColor(half3& color) {
2270 DegammaColorSingle(color.r);
2271 DegammaColorSingle(color.g);
2272 DegammaColorSingle(color.b);
2273 }
2274
2275 static void GammaColorSingle(half& s) {
2276 if (s <= 0.0031308f) {
2277 s = s * 12.92f;
2278 } else {
2279 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2280 }
2281 }
2282
2283 static void GammaColor(half3& color) {
2284 GammaColorSingle(color.r);
2285 GammaColorSingle(color.g);
2286 GammaColorSingle(color.b);
2287 }
2288
2289 static void applyMatrix(half3& color, const mat3& mat) {
2290 half3 ret = half3(0);
2291
2292 for (int i = 0; i < 3; i++) {
2293 for (int j = 0; j < 3; j++) {
2294 ret[i] = ret[i] + color[j] * mat[j][i];
2295 }
2296 }
2297 color = ret;
2298 }
2299};
2300
Peiyong Lind3788632018-09-18 16:01:31 -07002301TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2302 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002303 ASSERT_NO_FATAL_FAILURE(colorLayer =
2304 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2305 ISurfaceComposerClient::eFXSurfaceColor));
2306 Transaction()
2307 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2308 .setLayer(colorLayer, mLayerZBase + 1)
2309 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002310 {
2311 SCOPED_TRACE("default color");
2312 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2313 }
2314
2315 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002316 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002317 mat3 matrix;
2318 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2319 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2320 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002321
2322 // degamma before applying the matrix
2323 if (mColorManagementUsed) {
2324 ColorTransformHelper::DegammaColor(expected);
2325 }
2326
2327 ColorTransformHelper::applyMatrix(expected, matrix);
2328
2329 if (mColorManagementUsed) {
2330 ColorTransformHelper::GammaColor(expected);
2331 }
2332
2333 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2334 uint8_t(expected.b * 255), 255};
2335
2336 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2337 // channel) should be less than one
2338 const uint8_t tolerance = 1;
2339
Peiyong Lind3788632018-09-18 16:01:31 -07002340 Transaction().setColor(colorLayer, color)
2341 .setColorTransform(colorLayer, matrix, vec3()).apply();
2342 {
2343 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002344 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002345 }
2346}
2347
chaviwf66724d2018-11-28 16:35:21 -08002348TEST_F(LayerTransactionTest, SetColorTransformOnParent) {
2349 sp<SurfaceControl> parentLayer;
2350 sp<SurfaceControl> colorLayer;
2351 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2352 0 /* buffer height */,
2353 ISurfaceComposerClient::eFXSurfaceContainer));
2354 ASSERT_NO_FATAL_FAILURE(
2355 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2356 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2357
2358 Transaction()
2359 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2360 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2361 .setLayer(parentLayer, mLayerZBase + 1)
2362 .apply();
2363 {
2364 SCOPED_TRACE("default color");
2365 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2366 }
2367
2368 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2369 half3 expected = color;
2370 mat3 matrix;
2371 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2372 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2373 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2374
2375 // degamma before applying the matrix
2376 if (mColorManagementUsed) {
2377 ColorTransformHelper::DegammaColor(expected);
2378 }
2379
2380 ColorTransformHelper::applyMatrix(expected, matrix);
2381
2382 if (mColorManagementUsed) {
2383 ColorTransformHelper::GammaColor(expected);
2384 }
2385
2386 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2387 uint8_t(expected.b * 255), 255};
2388
2389 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2390 // channel) should be less than one
2391 const uint8_t tolerance = 1;
2392
2393 Transaction()
2394 .setColor(colorLayer, color)
2395 .setColorTransform(parentLayer, matrix, vec3())
2396 .apply();
2397 {
2398 SCOPED_TRACE("new color");
2399 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2400 }
2401}
2402
2403TEST_F(LayerTransactionTest, SetColorTransformOnChildAndParent) {
2404 sp<SurfaceControl> parentLayer;
2405 sp<SurfaceControl> colorLayer;
2406 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2407 0 /* buffer height */,
2408 ISurfaceComposerClient::eFXSurfaceContainer));
2409 ASSERT_NO_FATAL_FAILURE(
2410 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2411 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2412
2413 Transaction()
2414 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2415 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2416 .setLayer(parentLayer, mLayerZBase + 1)
2417 .apply();
2418 {
2419 SCOPED_TRACE("default color");
2420 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2421 }
2422
2423 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2424 half3 expected = color;
2425 mat3 matrixChild;
2426 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2427 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2428 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2429 mat3 matrixParent;
2430 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2431 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2432 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2433
2434 // degamma before applying the matrix
2435 if (mColorManagementUsed) {
2436 ColorTransformHelper::DegammaColor(expected);
2437 }
2438
2439 ColorTransformHelper::applyMatrix(expected, matrixChild);
2440 ColorTransformHelper::applyMatrix(expected, matrixParent);
2441
2442 if (mColorManagementUsed) {
2443 ColorTransformHelper::GammaColor(expected);
2444 }
2445
2446 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2447 uint8_t(expected.b * 255), 255};
2448
2449 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2450 // channel) should be less than one
2451 const uint8_t tolerance = 1;
2452
2453 Transaction()
2454 .setColor(colorLayer, color)
2455 .setColorTransform(parentLayer, matrixParent, vec3())
2456 .setColorTransform(colorLayer, matrixChild, vec3())
2457 .apply();
2458 {
2459 SCOPED_TRACE("new color");
2460 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2461 }
2462}
2463
Marissa Wallfda30bb2018-10-12 11:34:28 -07002464class ExpectedResult {
2465public:
2466 enum Transaction {
2467 NOT_PRESENTED = 0,
2468 PRESENTED,
2469 };
2470
2471 enum Buffer {
2472 NOT_ACQUIRED = 0,
2473 ACQUIRED,
2474 };
2475
2476 enum PreviousBuffer {
2477 NOT_RELEASED = 0,
2478 RELEASED,
2479 };
2480
2481 void reset() {
2482 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2483 mExpectedSurfaceResults.clear();
2484 }
2485
2486 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
2487 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2488 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2489 mTransactionResult = transactionResult;
2490 mExpectedSurfaceResults.emplace(std::piecewise_construct,
2491 std::forward_as_tuple(layer->getHandle()),
2492 std::forward_as_tuple(bufferResult, previousBufferResult));
2493 }
2494
2495 void addSurfaces(ExpectedResult::Transaction transactionResult,
2496 const std::vector<sp<SurfaceControl>>& layers,
2497 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2498 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2499 for (const auto& layer : layers) {
2500 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2501 }
2502 }
2503
2504 void verifyTransactionStats(const TransactionStats& transactionStats) const {
Valerie Hau63258a12018-12-14 14:31:48 -08002505 const auto& [latchTime, presentFence, surfaceStats] = transactionStats;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002506 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2507 ASSERT_GE(latchTime, 0) << "bad latch time";
Valerie Hau63258a12018-12-14 14:31:48 -08002508 ASSERT_NE(presentFence, nullptr);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002509 } else {
Valerie Hau63258a12018-12-14 14:31:48 -08002510 ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
Marissa Wallfda30bb2018-10-12 11:34:28 -07002511 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2512 }
2513
2514 ASSERT_EQ(surfaceStats.size(), mExpectedSurfaceResults.size())
2515 << "wrong number of surfaces";
2516
2517 for (const auto& stats : surfaceStats) {
2518 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2519 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2520 << "unexpected surface control";
2521 expectedSurfaceResult->second.verifySurfaceStats(stats, latchTime);
2522 }
2523 }
2524
2525private:
2526 class ExpectedSurfaceResult {
2527 public:
2528 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2529 ExpectedResult::PreviousBuffer previousBufferResult)
2530 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2531
2532 void verifySurfaceStats(const SurfaceStats& surfaceStats, nsecs_t latchTime) const {
2533 const auto& [surfaceControl, acquireTime, releasePreviousBuffer] = surfaceStats;
2534
2535 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2536 << "bad acquire time";
2537 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
2538 ASSERT_EQ(releasePreviousBuffer,
2539 mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED)
2540 << "bad previous buffer released";
2541 }
2542
2543 private:
2544 ExpectedResult::Buffer mBufferResult;
2545 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2546 };
2547
2548 struct IBinderHash {
2549 std::size_t operator()(const sp<IBinder>& strongPointer) const {
2550 return std::hash<IBinder*>{}(strongPointer.get());
2551 }
2552 };
2553 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2554 std::unordered_map<sp<IBinder>, ExpectedSurfaceResult, IBinderHash> mExpectedSurfaceResults;
2555};
2556
2557class CallbackHelper {
2558public:
2559 static void function(void* callbackContext, const TransactionStats& transactionStats) {
2560 if (!callbackContext) {
2561 ALOGE("failed to get callback context");
2562 }
2563 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2564 std::lock_guard lock(helper->mMutex);
2565 helper->mTransactionStatsQueue.push(transactionStats);
2566 helper->mConditionVariable.notify_all();
2567 }
2568
2569 void getTransactionStats(TransactionStats* outStats) {
2570 std::unique_lock lock(mMutex);
2571
2572 if (mTransactionStatsQueue.empty()) {
2573 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2574 std::cv_status::timeout)
2575 << "did not receive callback";
2576 }
2577
2578 *outStats = std::move(mTransactionStatsQueue.front());
2579 mTransactionStatsQueue.pop();
2580 }
2581
2582 void verifyFinalState() {
2583 // Wait to see if there are extra callbacks
2584 std::this_thread::sleep_for(500ms);
2585
2586 std::lock_guard lock(mMutex);
2587 EXPECT_EQ(mTransactionStatsQueue.size(), 0) << "extra callbacks received";
2588 mTransactionStatsQueue = {};
2589 }
2590
2591 void* getContext() { return static_cast<void*>(this); }
2592
2593 std::mutex mMutex;
2594 std::condition_variable mConditionVariable;
2595 std::queue<TransactionStats> mTransactionStatsQueue;
2596};
2597
2598class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002599public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002600 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002601 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002602 }
2603
Marissa Wall861616d2018-10-22 12:52:23 -07002604 static void fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2605 const sp<SurfaceControl>& layer = nullptr) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002606 if (layer) {
2607 sp<GraphicBuffer> buffer =
Marissa Wall861616d2018-10-22 12:52:23 -07002608 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002609 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2610 BufferUsage::COMPOSER_OVERLAY |
2611 BufferUsage::GPU_TEXTURE,
2612 "test");
Marissa Wall861616d2018-10-22 12:52:23 -07002613 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002614
2615 sp<Fence> fence = new Fence(-1);
2616
Marissa Wall861616d2018-10-22 12:52:23 -07002617 transaction.setBuffer(layer, buffer).setAcquireFence(layer, fence);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002618 }
2619
2620 transaction.addTransactionCompletedCallback(callbackHelper->function,
2621 callbackHelper->getContext());
2622 }
2623
Marissa Wall861616d2018-10-22 12:52:23 -07002624 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2625 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002626 TransactionStats transactionStats;
2627 ASSERT_NO_FATAL_FAILURE(helper.getTransactionStats(&transactionStats));
2628 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyTransactionStats(transactionStats));
2629
2630 if (finalState) {
2631 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2632 }
2633 }
2634
Marissa Wall861616d2018-10-22 12:52:23 -07002635 static void waitForCallbacks(CallbackHelper& helper,
2636 const std::vector<ExpectedResult>& expectedResults,
2637 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002638 for (const auto& expectedResult : expectedResults) {
2639 waitForCallback(helper, expectedResult);
2640 }
2641 if (finalState) {
2642 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2643 }
2644 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002645};
2646
2647TEST_F(LayerCallbackTest, Basic) {
2648 sp<SurfaceControl> layer;
2649 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2650
2651 Transaction transaction;
2652 CallbackHelper callback;
2653 fillTransaction(transaction, &callback, layer);
2654
2655 transaction.apply();
2656
2657 ExpectedResult expected;
2658 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2659 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2660}
2661
2662TEST_F(LayerCallbackTest, NoBuffer) {
2663 sp<SurfaceControl> layer;
2664 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2665
2666 Transaction transaction;
2667 CallbackHelper callback;
2668 fillTransaction(transaction, &callback);
2669
Marissa Wall861616d2018-10-22 12:52:23 -07002670 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002671
2672 ExpectedResult expected;
2673 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2674 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2675}
2676
2677TEST_F(LayerCallbackTest, NoStateChange) {
2678 Transaction transaction;
2679 CallbackHelper callback;
2680 fillTransaction(transaction, &callback);
2681
2682 transaction.apply();
2683
2684 ExpectedResult expected;
2685 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2686}
2687
2688TEST_F(LayerCallbackTest, OffScreen) {
2689 sp<SurfaceControl> layer;
2690 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2691
2692 Transaction transaction;
2693 CallbackHelper callback;
2694 fillTransaction(transaction, &callback, layer);
2695
Marissa Wall861616d2018-10-22 12:52:23 -07002696 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002697
2698 ExpectedResult expected;
2699 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2700 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2701}
2702
2703TEST_F(LayerCallbackTest, Merge) {
2704 sp<SurfaceControl> layer1, layer2;
2705 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2706 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2707
2708 Transaction transaction1, transaction2;
2709 CallbackHelper callback1, callback2;
2710 fillTransaction(transaction1, &callback1, layer1);
2711 fillTransaction(transaction2, &callback2, layer2);
2712
Marissa Wall861616d2018-10-22 12:52:23 -07002713 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2714 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002715
2716 ExpectedResult expected;
2717 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2718 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2719 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2720}
2721
2722TEST_F(LayerCallbackTest, Merge_SameCallback) {
2723 sp<SurfaceControl> layer1, layer2;
2724 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2725 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2726
2727 Transaction transaction1, transaction2;
2728 CallbackHelper callback;
2729 fillTransaction(transaction1, &callback, layer1);
2730 fillTransaction(transaction2, &callback, layer2);
2731
2732 transaction2.merge(std::move(transaction1)).apply();
2733
2734 ExpectedResult expected;
2735 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2736 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2737 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2738}
2739
2740TEST_F(LayerCallbackTest, Merge_SameLayer) {
2741 sp<SurfaceControl> layer;
2742 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2743
2744 Transaction transaction1, transaction2;
2745 CallbackHelper callback1, callback2;
2746 fillTransaction(transaction1, &callback1, layer);
2747 fillTransaction(transaction2, &callback2, layer);
2748
2749 transaction2.merge(std::move(transaction1)).apply();
2750
2751 ExpectedResult expected;
2752 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2753 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2754 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2755}
2756
2757TEST_F(LayerCallbackTest, Merge_SingleBuffer) {
2758 sp<SurfaceControl> layer1, layer2;
2759 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2760 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2761
2762 Transaction transaction1, transaction2;
2763 CallbackHelper callback1, callback2;
2764 fillTransaction(transaction1, &callback1, layer1);
2765 fillTransaction(transaction2, &callback2);
2766
Marissa Wall861616d2018-10-22 12:52:23 -07002767 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2768 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002769
2770 ExpectedResult expected;
2771 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2772 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2773 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2774}
2775
2776TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2777 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2778 client2(new SurfaceComposerClient);
2779
2780 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2781 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2782
2783 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002784 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002785 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002786 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002787 ISurfaceComposerClient::eFXSurfaceBufferState));
2788
2789 Transaction transaction1, transaction2;
2790 CallbackHelper callback1, callback2;
2791 fillTransaction(transaction1, &callback1, layer1);
2792 fillTransaction(transaction2, &callback2, layer2);
2793
Marissa Wall861616d2018-10-22 12:52:23 -07002794 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2795 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002796
2797 ExpectedResult expected;
2798 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2799 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2800 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2801}
2802
2803TEST_F(LayerCallbackTest, MultipleTransactions) {
2804 sp<SurfaceControl> layer;
2805 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2806
2807 Transaction transaction;
2808 CallbackHelper callback;
2809 for (size_t i = 0; i < 10; i++) {
2810 fillTransaction(transaction, &callback, layer);
2811
2812 transaction.apply();
2813
2814 ExpectedResult expected;
2815 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2816 ExpectedResult::Buffer::NOT_ACQUIRED,
2817 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2818 : ExpectedResult::PreviousBuffer::RELEASED);
2819 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2820 }
2821 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2822}
2823
2824TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
2825 sp<SurfaceControl> layer;
2826 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2827
2828 Transaction transaction;
2829 CallbackHelper callback;
2830 for (size_t i = 0; i < 10; i++) {
2831 ExpectedResult expected;
2832
2833 if (i == 0) {
2834 fillTransaction(transaction, &callback, layer);
2835 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2836 } else {
2837 fillTransaction(transaction, &callback);
2838 }
2839
2840 transaction.apply();
2841
2842 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2843 }
2844 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2845}
2846
2847TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
2848 sp<SurfaceControl> layer;
2849 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2850
2851 Transaction transaction;
2852 CallbackHelper callback;
2853 for (size_t i = 0; i < 10; i++) {
2854 if (i == 0) {
2855 fillTransaction(transaction, &callback, layer);
2856 } else {
2857 fillTransaction(transaction, &callback);
2858 }
2859
Marissa Wall861616d2018-10-22 12:52:23 -07002860 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002861
2862 ExpectedResult expected;
2863 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
2864 : ExpectedResult::Transaction::NOT_PRESENTED,
2865 layer);
2866 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
2867 }
2868 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2869}
2870
2871TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
2872 sp<SurfaceControl> layer1, layer2;
2873 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2874 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2875
2876 Transaction transaction1, transaction2;
2877 CallbackHelper callback1, callback2;
2878 for (size_t i = 0; i < 10; i++) {
2879 fillTransaction(transaction1, &callback1, layer1);
2880 fillTransaction(transaction2, &callback2, layer2);
2881
Marissa Wall861616d2018-10-22 12:52:23 -07002882 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2883 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002884
2885 ExpectedResult expected;
2886 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2887 ExpectedResult::Buffer::NOT_ACQUIRED,
2888 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2889 : ExpectedResult::PreviousBuffer::RELEASED);
2890 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2891 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2892 }
2893 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2894 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2895}
2896
2897TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
2898 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2899 client2(new SurfaceComposerClient);
2900 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2901 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2902
2903 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002904 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002905 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002906 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002907 ISurfaceComposerClient::eFXSurfaceBufferState));
2908
2909 Transaction transaction1, transaction2;
2910 CallbackHelper callback1, callback2;
2911 for (size_t i = 0; i < 10; i++) {
2912 fillTransaction(transaction1, &callback1, layer1);
2913 fillTransaction(transaction2, &callback2, layer2);
2914
Marissa Wall861616d2018-10-22 12:52:23 -07002915 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2916 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002917
2918 ExpectedResult expected;
2919 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2920 ExpectedResult::Buffer::NOT_ACQUIRED,
2921 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2922 : ExpectedResult::PreviousBuffer::RELEASED);
2923 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2924 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2925 }
2926 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2927 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2928}
2929
2930TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
2931 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2932 client2(new SurfaceComposerClient);
2933 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2934 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2935
2936 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002937 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002938 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002939 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002940 ISurfaceComposerClient::eFXSurfaceBufferState));
2941
2942 Transaction transaction1, transaction2;
2943 CallbackHelper callback1, callback2;
2944
2945 // Normal call to set up test
2946 fillTransaction(transaction1, &callback1, layer1);
2947 fillTransaction(transaction2, &callback2, layer2);
2948
Marissa Wall861616d2018-10-22 12:52:23 -07002949 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2950 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002951
2952 ExpectedResult expected;
2953 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2954 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2955 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2956 expected.reset();
2957
2958 // Test
2959 fillTransaction(transaction1, &callback1);
2960 fillTransaction(transaction2, &callback2);
2961
2962 transaction2.merge(std::move(transaction1)).apply();
2963
2964 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2965 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2966}
2967
2968TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
2969 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2970 client2(new SurfaceComposerClient);
2971
2972 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2973 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2974
2975 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07002976 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002977 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07002978 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002979 ISurfaceComposerClient::eFXSurfaceBufferState));
2980
2981 Transaction transaction1, transaction2;
2982 CallbackHelper callback1, callback2;
2983
2984 // Normal call to set up test
2985 fillTransaction(transaction1, &callback1, layer1);
2986 fillTransaction(transaction2, &callback2, layer2);
2987
Marissa Wall861616d2018-10-22 12:52:23 -07002988 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2989 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002990
2991 ExpectedResult expected;
2992 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2993 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2994 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2995 expected.reset();
2996
2997 // Test
2998 fillTransaction(transaction1, &callback1);
2999 fillTransaction(transaction2, &callback2);
3000
Marissa Wall861616d2018-10-22 12:52:23 -07003001 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003002
3003 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2);
3004 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3005 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3006}
3007
3008TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3009 sp<SurfaceControl> layer;
3010 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3011
3012 Transaction transaction;
3013 CallbackHelper callback;
3014 std::vector<ExpectedResult> expectedResults(50);
3015 ExpectedResult::PreviousBuffer previousBufferResult =
3016 ExpectedResult::PreviousBuffer::NOT_RELEASED;
3017 for (auto& expected : expectedResults) {
3018 expected.reset();
3019 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3020 ExpectedResult::Buffer::NOT_ACQUIRED, previousBufferResult);
3021 previousBufferResult = ExpectedResult::PreviousBuffer::RELEASED;
3022
3023 fillTransaction(transaction, &callback, layer);
3024
3025 transaction.apply();
3026 std::this_thread::sleep_for(200ms);
3027 }
3028 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3029}
3030
3031TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3032 sp<SurfaceControl> layer;
3033 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3034
3035 Transaction transaction;
3036 CallbackHelper callback;
3037 std::vector<ExpectedResult> expectedResults(50);
3038 bool first = true;
3039 for (auto& expected : expectedResults) {
3040 expected.reset();
3041
3042 if (first) {
3043 fillTransaction(transaction, &callback, layer);
3044 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3045 first = false;
3046 } else {
3047 fillTransaction(transaction, &callback);
3048 }
3049
3050 transaction.apply();
3051 std::this_thread::sleep_for(200ms);
3052 }
3053 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3054}
3055
3056TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3057 sp<SurfaceControl> layer;
3058 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3059
3060 // Normal call to set up test
3061 Transaction transaction;
3062 CallbackHelper callback;
3063 fillTransaction(transaction, &callback, layer);
3064
Marissa Wall861616d2018-10-22 12:52:23 -07003065 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003066
3067 ExpectedResult expectedResult;
3068 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3069 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3070
3071 // Test
3072 std::vector<ExpectedResult> expectedResults(50);
3073 for (auto& expected : expectedResults) {
3074 expected.reset();
3075 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
3076
3077 fillTransaction(transaction, &callback);
3078
Marissa Wall861616d2018-10-22 12:52:23 -07003079 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003080
3081 std::this_thread::sleep_for(200ms);
3082 }
3083 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3084}
3085
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003086class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003087protected:
3088 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003089 LayerTransactionTest::SetUp();
3090 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003091
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003092 sp<IBinder> display(
3093 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003094 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07003095 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07003096
3097 ssize_t displayWidth = info.w;
3098 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003099
3100 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003101 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3102 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003103 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003104 ASSERT_TRUE(mBGSurfaceControl->isValid());
3105 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
3106
3107 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003108 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3109
Peiyong Lin566a3b42018-01-09 18:22:43 -08003110 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003111 ASSERT_TRUE(mFGSurfaceControl->isValid());
3112
3113 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3114
3115 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003116 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003117 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003118 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3119
3120 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3121
Robert Carr4cdc58f2017-08-23 14:22:20 -07003122 asTransaction([&](Transaction& t) {
3123 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003124
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003125 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003126
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003127 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3128 .setPosition(mFGSurfaceControl, 64, 64)
3129 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003130
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003131 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3132 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3133 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003134 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003135 }
3136
3137 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003138 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003139 mBGSurfaceControl = 0;
3140 mFGSurfaceControl = 0;
3141 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003142 }
3143
3144 void waitForPostedBuffers() {
3145 // Since the sync surface is in synchronous mode (i.e. double buffered)
3146 // posting three buffers to it should ensure that at least two
3147 // SurfaceFlinger::handlePageFlip calls have been made, which should
3148 // guaranteed that a buffer posted to another Surface has been retired.
3149 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3150 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3151 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3152 }
3153
Robert Carr4cdc58f2017-08-23 14:22:20 -07003154 void asTransaction(const std::function<void(Transaction&)>& exec) {
3155 Transaction t;
3156 exec(t);
3157 t.apply(true);
3158 }
3159
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003160 sp<SurfaceControl> mBGSurfaceControl;
3161 sp<SurfaceControl> mFGSurfaceControl;
3162
3163 // This surface is used to ensure that the buffers posted to
3164 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3165 sp<SurfaceControl> mSyncSurfaceControl;
3166};
3167
Robert Carr7f619b22017-11-06 12:56:35 -08003168TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003169
chaviw0e3479f2018-09-10 16:49:30 -07003170 std::unique_ptr<ScreenCapture> sc;
3171
3172 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003173 fillSurfaceRGBA8(relative, 10, 10, 10);
3174 waitForPostedBuffers();
3175
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003176 Transaction{}
3177 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003178 .setPosition(relative, 64, 64)
3179 .apply();
3180
3181 {
3182 // The relative should be on top of the FG control.
3183 ScreenCapture::captureScreen(&sc);
3184 sc->checkPixel(64, 64, 10, 10, 10);
3185 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003186 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003187
3188 {
3189 // Nothing should change at this point.
3190 ScreenCapture::captureScreen(&sc);
3191 sc->checkPixel(64, 64, 10, 10, 10);
3192 }
3193
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003194 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003195
3196 {
3197 // Ensure that the relative was actually hidden, rather than
3198 // being left in the detached but visible state.
3199 ScreenCapture::captureScreen(&sc);
3200 sc->expectFGColor(64, 64);
3201 }
3202}
3203
Robert Carr8d5227b2017-03-16 15:41:03 -07003204class GeometryLatchingTest : public LayerUpdateTest {
3205protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003206 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003207 SCOPED_TRACE(trace);
3208 ScreenCapture::captureScreen(&sc);
3209 // We find the leading edge of the FG surface.
3210 sc->expectFGColor(127, 127);
3211 sc->expectBGColor(128, 128);
3212 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003213
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003214 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07003215
3216 void unlockFGBuffer() {
3217 sp<Surface> s = mFGSurfaceControl->getSurface();
3218 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3219 waitForPostedBuffers();
3220 }
3221
Robert Carr8d5227b2017-03-16 15:41:03 -07003222 void completeFGResize() {
3223 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3224 waitForPostedBuffers();
3225 }
3226 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003227 asTransaction([&](Transaction& t) {
3228 t.setSize(mFGSurfaceControl, 64, 64);
3229 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003230 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003231 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003232
3233 EXPECT_INITIAL_STATE("After restoring initial state");
3234 }
chaviw0e3479f2018-09-10 16:49:30 -07003235 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003236};
3237
Robert Carr8d5227b2017-03-16 15:41:03 -07003238class CropLatchingTest : public GeometryLatchingTest {
3239protected:
3240 void EXPECT_CROPPED_STATE(const char* trace) {
3241 SCOPED_TRACE(trace);
3242 ScreenCapture::captureScreen(&sc);
3243 // The edge should be moved back one pixel by our crop.
3244 sc->expectFGColor(126, 126);
3245 sc->expectBGColor(127, 127);
3246 sc->expectBGColor(128, 128);
3247 }
chaviw59f5c562017-06-28 16:39:06 -07003248
3249 void EXPECT_RESIZE_STATE(const char* trace) {
3250 SCOPED_TRACE(trace);
3251 ScreenCapture::captureScreen(&sc);
3252 // The FG is now resized too 128,128 at 64,64
3253 sc->expectFGColor(64, 64);
3254 sc->expectFGColor(191, 191);
3255 sc->expectBGColor(192, 192);
3256 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003257};
3258
Pablo Ceballos05289c22016-04-14 15:49:55 -07003259TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003260 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003261 {
3262 SCOPED_TRACE("before anything");
3263 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003264 sc->expectBGColor(32, 32);
3265 sc->expectFGColor(96, 96);
3266 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003267 }
3268
3269 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003270 asTransaction([&](Transaction& t) {
3271 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003272 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3273 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003274 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003275
Robert Carr4cdc58f2017-08-23 14:22:20 -07003276 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003277 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003278 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3279 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003280 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003281
3282 {
3283 SCOPED_TRACE("before any trigger");
3284 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003285 sc->expectBGColor(32, 32);
3286 sc->expectFGColor(96, 96);
3287 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003288 }
3289
3290 // should trigger the first deferred transaction, but not the second one
3291 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3292 {
3293 SCOPED_TRACE("after first trigger");
3294 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003295 sc->expectBGColor(32, 32);
3296 sc->checkPixel(96, 96, 162, 63, 96);
3297 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003298 }
3299
3300 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003301 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003302
3303 // trigger the second deferred transaction
3304 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3305 {
3306 SCOPED_TRACE("after second trigger");
3307 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003308 sc->expectBGColor(32, 32);
3309 sc->expectBGColor(96, 96);
3310 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003311 }
3312}
3313
Robert Carre392b552017-09-19 12:16:05 -07003314TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003315 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003316
3317 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003318 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3319 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3320 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3321 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003322 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003323 SurfaceComposerClient::Transaction{}
3324 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3325 .show(childNoBuffer)
3326 .show(childBuffer)
3327 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003328 {
3329 ScreenCapture::captureScreen(&sc);
3330 sc->expectChildColor(73, 73);
3331 sc->expectFGColor(74, 74);
3332 }
Vishnu Nair60356342018-11-13 13:00:45 -08003333 SurfaceComposerClient::Transaction{}
3334 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3335 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003336 {
3337 ScreenCapture::captureScreen(&sc);
3338 sc->expectChildColor(73, 73);
3339 sc->expectChildColor(74, 74);
3340 }
3341}
3342
Robert Carr2c5f6d22017-09-26 12:30:35 -07003343TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003344 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003345 {
3346 SCOPED_TRACE("before move");
3347 ScreenCapture::captureScreen(&sc);
3348 sc->expectBGColor(0, 12);
3349 sc->expectFGColor(75, 75);
3350 sc->expectBGColor(145, 145);
3351 }
3352
3353 Transaction t1, t2;
3354 t1.setPosition(mFGSurfaceControl, 128, 128);
3355 t2.setPosition(mFGSurfaceControl, 0, 0);
3356 // We expect that the position update from t2 now
3357 // overwrites the position update from t1.
3358 t1.merge(std::move(t2));
3359 t1.apply();
3360
3361 {
3362 ScreenCapture::captureScreen(&sc);
3363 sc->expectFGColor(1, 1);
3364 }
3365}
3366
Robert Carr1f0a16a2016-10-24 16:27:39 -07003367class ChildLayerTest : public LayerUpdateTest {
3368protected:
3369 void SetUp() override {
3370 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003371 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3372 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003373 fillSurfaceRGBA8(mChild, 200, 200, 200);
3374
3375 {
3376 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003377 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003378 mCapture->expectChildColor(64, 64);
3379 }
3380 }
3381 void TearDown() override {
3382 LayerUpdateTest::TearDown();
3383 mChild = 0;
3384 }
3385
3386 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003387 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003388};
3389
3390TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003391 asTransaction([&](Transaction& t) {
3392 t.show(mChild);
3393 t.setPosition(mChild, 10, 10);
3394 t.setPosition(mFGSurfaceControl, 64, 64);
3395 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003396
3397 {
chaviw0e3479f2018-09-10 16:49:30 -07003398 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003399 // Top left of foreground must now be visible
3400 mCapture->expectFGColor(64, 64);
3401 // But 10 pixels in we should see the child surface
3402 mCapture->expectChildColor(74, 74);
3403 // And 10 more pixels we should be back to the foreground surface
3404 mCapture->expectFGColor(84, 84);
3405 }
3406
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003407 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003408
3409 {
chaviw0e3479f2018-09-10 16:49:30 -07003410 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003411 // Top left of foreground should now be at 0, 0
3412 mCapture->expectFGColor(0, 0);
3413 // But 10 pixels in we should see the child surface
3414 mCapture->expectChildColor(10, 10);
3415 // And 10 more pixels we should be back to the foreground surface
3416 mCapture->expectFGColor(20, 20);
3417 }
3418}
3419
Robert Carr41b08b52017-06-01 16:11:34 -07003420TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003421 asTransaction([&](Transaction& t) {
3422 t.show(mChild);
3423 t.setPosition(mChild, 0, 0);
3424 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003425 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003426 });
Robert Carr41b08b52017-06-01 16:11:34 -07003427
3428 {
chaviw0e3479f2018-09-10 16:49:30 -07003429 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003430 mCapture->expectChildColor(0, 0);
3431 mCapture->expectChildColor(4, 4);
3432 mCapture->expectBGColor(5, 5);
3433 }
3434}
3435
Robert Carr1f0a16a2016-10-24 16:27:39 -07003436TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003437 asTransaction([&](Transaction& t) {
3438 t.show(mChild);
3439 t.setPosition(mFGSurfaceControl, 0, 0);
3440 t.setPosition(mChild, 63, 63);
3441 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003442
3443 {
chaviw0e3479f2018-09-10 16:49:30 -07003444 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003445 mCapture->expectFGColor(0, 0);
3446 // Last pixel in foreground should now be the child.
3447 mCapture->expectChildColor(63, 63);
3448 // But the child should be constrained and the next pixel
3449 // must be the background
3450 mCapture->expectBGColor(64, 64);
3451 }
3452}
3453
3454TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003455 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003456
3457 // Find the boundary between the parent and child
3458 {
chaviw0e3479f2018-09-10 16:49:30 -07003459 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003460 mCapture->expectChildColor(9, 9);
3461 mCapture->expectFGColor(10, 10);
3462 }
3463
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003464 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003465
3466 // The boundary should be twice as far from the origin now.
3467 // The pixels from the last test should all be child now
3468 {
chaviw0e3479f2018-09-10 16:49:30 -07003469 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003470 mCapture->expectChildColor(9, 9);
3471 mCapture->expectChildColor(10, 10);
3472 mCapture->expectChildColor(19, 19);
3473 mCapture->expectFGColor(20, 20);
3474 }
3475}
Robert Carr9524cb32017-02-13 11:32:32 -08003476
Robert Carr6452f122017-03-21 10:41:29 -07003477TEST_F(ChildLayerTest, ChildLayerAlpha) {
3478 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3479 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3480 fillSurfaceRGBA8(mChild, 0, 254, 0);
3481 waitForPostedBuffers();
3482
Robert Carr4cdc58f2017-08-23 14:22:20 -07003483 asTransaction([&](Transaction& t) {
3484 t.show(mChild);
3485 t.setPosition(mChild, 0, 0);
3486 t.setPosition(mFGSurfaceControl, 0, 0);
3487 });
Robert Carr6452f122017-03-21 10:41:29 -07003488
3489 {
chaviw0e3479f2018-09-10 16:49:30 -07003490 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003491 // Unblended child color
3492 mCapture->checkPixel(0, 0, 0, 254, 0);
3493 }
3494
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003495 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003496
3497 {
chaviw0e3479f2018-09-10 16:49:30 -07003498 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003499 // Child and BG blended.
3500 mCapture->checkPixel(0, 0, 127, 127, 0);
3501 }
3502
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003503 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003504
3505 {
chaviw0e3479f2018-09-10 16:49:30 -07003506 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003507 // Child and BG blended.
3508 mCapture->checkPixel(0, 0, 95, 64, 95);
3509 }
3510}
3511
Robert Carr9524cb32017-02-13 11:32:32 -08003512TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003513 asTransaction([&](Transaction& t) {
3514 t.show(mChild);
3515 t.setPosition(mChild, 10, 10);
3516 t.setPosition(mFGSurfaceControl, 64, 64);
3517 });
Robert Carr9524cb32017-02-13 11:32:32 -08003518
3519 {
chaviw0e3479f2018-09-10 16:49:30 -07003520 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003521 // Top left of foreground must now be visible
3522 mCapture->expectFGColor(64, 64);
3523 // But 10 pixels in we should see the child surface
3524 mCapture->expectChildColor(74, 74);
3525 // And 10 more pixels we should be back to the foreground surface
3526 mCapture->expectFGColor(84, 84);
3527 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003528
3529 asTransaction([&](Transaction& t) {
3530 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
3531 });
3532
Robert Carr9524cb32017-02-13 11:32:32 -08003533 {
chaviw0e3479f2018-09-10 16:49:30 -07003534 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003535 mCapture->expectFGColor(64, 64);
3536 // In reparenting we should have exposed the entire foreground surface.
3537 mCapture->expectFGColor(74, 74);
3538 // And the child layer should now begin at 10, 10 (since the BG
3539 // layer is at (0, 0)).
3540 mCapture->expectBGColor(9, 9);
3541 mCapture->expectChildColor(10, 10);
3542 }
3543}
3544
Robert Carr2e102c92018-10-23 12:11:15 -07003545TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
3546 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003547 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07003548 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
3549
3550 {
3551 SCOPED_TRACE("Grandchild visible");
3552 ScreenCapture::captureScreen(&mCapture);
3553 mCapture->checkPixel(64, 64, 111, 111, 111);
3554 }
3555
3556 mChild->clear();
3557
3558 {
3559 SCOPED_TRACE("After destroying child");
3560 ScreenCapture::captureScreen(&mCapture);
3561 mCapture->expectFGColor(64, 64);
3562 }
3563
3564 asTransaction([&](Transaction& t) {
3565 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
3566 });
3567
3568 {
3569 SCOPED_TRACE("After reparenting grandchild");
3570 ScreenCapture::captureScreen(&mCapture);
3571 mCapture->checkPixel(64, 64, 111, 111, 111);
3572 }
3573}
3574
chaviw161410b02017-07-27 10:46:08 -07003575TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003576 asTransaction([&](Transaction& t) {
3577 t.show(mChild);
3578 t.setPosition(mChild, 10, 10);
3579 t.setPosition(mFGSurfaceControl, 64, 64);
3580 });
Robert Carr9524cb32017-02-13 11:32:32 -08003581
3582 {
chaviw0e3479f2018-09-10 16:49:30 -07003583 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003584 // Top left of foreground must now be visible
3585 mCapture->expectFGColor(64, 64);
3586 // But 10 pixels in we should see the child surface
3587 mCapture->expectChildColor(74, 74);
3588 // And 10 more pixels we should be back to the foreground surface
3589 mCapture->expectFGColor(84, 84);
3590 }
3591
chaviw0e3479f2018-09-10 16:49:30 -07003592
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003593 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08003594
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003595 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08003596
chaviw161410b02017-07-27 10:46:08 -07003597 // Since the child has the same client as the parent, it will not get
3598 // detached and will be hidden.
3599 {
chaviw0e3479f2018-09-10 16:49:30 -07003600 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003601 mCapture->expectFGColor(64, 64);
3602 mCapture->expectFGColor(74, 74);
3603 mCapture->expectFGColor(84, 84);
3604 }
3605}
3606
3607TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
3608 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003609 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003610 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
3611 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07003612
chaviw161410b02017-07-27 10:46:08 -07003613 ASSERT_TRUE(mChildNewClient->isValid());
3614
3615 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
3616
Robert Carr4cdc58f2017-08-23 14:22:20 -07003617 asTransaction([&](Transaction& t) {
3618 t.hide(mChild);
3619 t.show(mChildNewClient);
3620 t.setPosition(mChildNewClient, 10, 10);
3621 t.setPosition(mFGSurfaceControl, 64, 64);
3622 });
chaviw161410b02017-07-27 10:46:08 -07003623
3624 {
chaviw0e3479f2018-09-10 16:49:30 -07003625 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003626 // Top left of foreground must now be visible
3627 mCapture->expectFGColor(64, 64);
3628 // But 10 pixels in we should see the child surface
3629 mCapture->expectChildColor(74, 74);
3630 // And 10 more pixels we should be back to the foreground surface
3631 mCapture->expectFGColor(84, 84);
3632 }
3633
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003634 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07003635
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003636 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07003637
Robert Carr9524cb32017-02-13 11:32:32 -08003638 // Nothing should have changed.
3639 {
chaviw0e3479f2018-09-10 16:49:30 -07003640 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003641 mCapture->expectFGColor(64, 64);
3642 mCapture->expectChildColor(74, 74);
3643 mCapture->expectFGColor(84, 84);
3644 }
3645}
3646
chaviw5aedec92018-10-22 10:40:38 -07003647TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
3648 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
3649 sp<SurfaceControl> childNewClient =
3650 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
3651 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3652
3653 ASSERT_TRUE(childNewClient != nullptr);
3654 ASSERT_TRUE(childNewClient->isValid());
3655
3656 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
3657
3658 Transaction()
3659 .hide(mChild)
3660 .show(childNewClient)
3661 .setPosition(childNewClient, 10, 10)
3662 .setPosition(mFGSurfaceControl, 64, 64)
3663 .apply();
3664
3665 {
3666 mCapture = screenshot();
3667 // Top left of foreground must now be visible
3668 mCapture->expectFGColor(64, 64);
3669 // But 10 pixels in we should see the child surface
3670 mCapture->expectChildColor(74, 74);
3671 // And 10 more pixels we should be back to the foreground surface
3672 mCapture->expectFGColor(84, 84);
3673 }
3674
3675 Transaction().detachChildren(mFGSurfaceControl).apply();
3676 Transaction().hide(childNewClient).apply();
3677
3678 // Nothing should have changed.
3679 {
3680 mCapture = screenshot();
3681 mCapture->expectFGColor(64, 64);
3682 mCapture->expectChildColor(74, 74);
3683 mCapture->expectFGColor(84, 84);
3684 }
3685
3686 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
3687 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
3688 32);
3689 Transaction()
3690 .setLayer(newParentSurface, INT32_MAX - 1)
3691 .show(newParentSurface)
3692 .setPosition(newParentSurface, 20, 20)
3693 .reparent(childNewClient, newParentSurface->getHandle())
3694 .apply();
3695 {
3696 mCapture = screenshot();
3697 // Child is now hidden.
3698 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
3699 }
3700}
3701
Robert Carr9b429f42017-04-17 14:56:57 -07003702TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003703 asTransaction([&](Transaction& t) {
3704 t.show(mChild);
3705 t.setPosition(mChild, 0, 0);
3706 t.setPosition(mFGSurfaceControl, 0, 0);
3707 });
Robert Carr9b429f42017-04-17 14:56:57 -07003708
3709 {
chaviw0e3479f2018-09-10 16:49:30 -07003710 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003711 // We've positioned the child in the top left.
3712 mCapture->expectChildColor(0, 0);
3713 // But it's only 10x10.
3714 mCapture->expectFGColor(10, 10);
3715 }
3716
Robert Carr4cdc58f2017-08-23 14:22:20 -07003717 asTransaction([&](Transaction& t) {
3718 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3719 // We cause scaling by 2.
3720 t.setSize(mFGSurfaceControl, 128, 128);
3721 });
Robert Carr9b429f42017-04-17 14:56:57 -07003722
3723 {
chaviw0e3479f2018-09-10 16:49:30 -07003724 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003725 // We've positioned the child in the top left.
3726 mCapture->expectChildColor(0, 0);
3727 mCapture->expectChildColor(10, 10);
3728 mCapture->expectChildColor(19, 19);
3729 // And now it should be scaled all the way to 20x20
3730 mCapture->expectFGColor(20, 20);
3731 }
3732}
3733
Robert Carr1725eee2017-04-26 18:32:15 -07003734// Regression test for b/37673612
3735TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003736 asTransaction([&](Transaction& t) {
3737 t.show(mChild);
3738 t.setPosition(mChild, 0, 0);
3739 t.setPosition(mFGSurfaceControl, 0, 0);
3740 });
Robert Carr1725eee2017-04-26 18:32:15 -07003741
3742 {
chaviw0e3479f2018-09-10 16:49:30 -07003743 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003744 // We've positioned the child in the top left.
3745 mCapture->expectChildColor(0, 0);
3746 // But it's only 10x10.
3747 mCapture->expectFGColor(10, 10);
3748 }
Robert Carr1725eee2017-04-26 18:32:15 -07003749 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
3750 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003751 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07003752 sp<Surface> s = mFGSurfaceControl->getSurface();
3753 auto anw = static_cast<ANativeWindow*>(s.get());
3754 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
3755 native_window_set_buffers_dimensions(anw, 64, 128);
3756 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3757 waitForPostedBuffers();
3758
3759 {
3760 // The child should still be in the same place and not have any strange scaling as in
3761 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07003762 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003763 mCapture->expectChildColor(0, 0);
3764 mCapture->expectFGColor(10, 10);
3765 }
3766}
3767
Dan Stoza412903f2017-04-27 13:42:17 -07003768TEST_F(ChildLayerTest, Bug36858924) {
3769 // Destroy the child layer
3770 mChild.clear();
3771
3772 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08003773 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
3774 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07003775
3776 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07003777 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003778 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
3779 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003780 t.show(mChild);
3781 });
Dan Stoza412903f2017-04-27 13:42:17 -07003782
3783 // Render the foreground surface a few times
3784 //
3785 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
3786 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
3787 // never acquire/release the first buffer
3788 ALOGI("Filling 1");
3789 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3790 ALOGI("Filling 2");
3791 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
3792 ALOGI("Filling 3");
3793 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
3794 ALOGI("Filling 4");
3795 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3796}
3797
chaviwf1961f72017-09-18 16:41:07 -07003798TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003799 asTransaction([&](Transaction& t) {
3800 t.show(mChild);
3801 t.setPosition(mChild, 10, 10);
3802 t.setPosition(mFGSurfaceControl, 64, 64);
3803 });
chaviw06178942017-07-27 10:25:59 -07003804
3805 {
chaviw0e3479f2018-09-10 16:49:30 -07003806 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003807 // Top left of foreground must now be visible
3808 mCapture->expectFGColor(64, 64);
3809 // But 10 pixels in we should see the child surface
3810 mCapture->expectChildColor(74, 74);
3811 // And 10 more pixels we should be back to the foreground surface
3812 mCapture->expectFGColor(84, 84);
3813 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003814
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003815 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07003816
chaviw06178942017-07-27 10:25:59 -07003817 {
chaviw0e3479f2018-09-10 16:49:30 -07003818 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003819 mCapture->expectFGColor(64, 64);
3820 // In reparenting we should have exposed the entire foreground surface.
3821 mCapture->expectFGColor(74, 74);
3822 // And the child layer should now begin at 10, 10 (since the BG
3823 // layer is at (0, 0)).
3824 mCapture->expectBGColor(9, 9);
3825 mCapture->expectChildColor(10, 10);
3826 }
3827}
3828
chaviwf1961f72017-09-18 16:41:07 -07003829TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003830 asTransaction([&](Transaction& t) {
3831 t.show(mChild);
3832 t.setPosition(mChild, 10, 10);
3833 t.setPosition(mFGSurfaceControl, 64, 64);
3834 });
chaviwf1961f72017-09-18 16:41:07 -07003835
3836 {
chaviw0e3479f2018-09-10 16:49:30 -07003837 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003838 // Top left of foreground must now be visible
3839 mCapture->expectFGColor(64, 64);
3840 // But 10 pixels in we should see the child surface
3841 mCapture->expectChildColor(74, 74);
3842 // And 10 more pixels we should be back to the foreground surface
3843 mCapture->expectFGColor(84, 84);
3844 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003845 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07003846 {
chaviw0e3479f2018-09-10 16:49:30 -07003847 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003848 // Nothing should have changed.
3849 mCapture->expectFGColor(64, 64);
3850 mCapture->expectChildColor(74, 74);
3851 mCapture->expectFGColor(84, 84);
3852 }
3853}
3854
3855TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07003856 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003857 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07003858 ASSERT_TRUE(newSurface->isValid());
3859
3860 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003861 asTransaction([&](Transaction& t) {
3862 t.hide(mChild);
3863 t.show(newSurface);
3864 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003865 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003866 t.setPosition(mFGSurfaceControl, 64, 64);
3867 });
chaviwf1961f72017-09-18 16:41:07 -07003868
3869 {
chaviw0e3479f2018-09-10 16:49:30 -07003870 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003871 // Top left of foreground must now be visible
3872 mCapture->expectFGColor(64, 64);
3873 // At 10, 10 we should see the new surface
3874 mCapture->checkPixel(10, 10, 63, 195, 63);
3875 }
3876
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003877 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07003878
3879 {
chaviw0e3479f2018-09-10 16:49:30 -07003880 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003881 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
3882 // mFGSurface, putting it at 74, 74.
3883 mCapture->expectFGColor(64, 64);
3884 mCapture->checkPixel(74, 74, 63, 195, 63);
3885 mCapture->expectFGColor(84, 84);
3886 }
3887}
3888
chaviwc9674332017-08-28 12:32:18 -07003889TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003890 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
3891 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07003892 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3893
3894 {
chaviw0e3479f2018-09-10 16:49:30 -07003895 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07003896 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
3897 // which begins at 64, 64
3898 mCapture->checkPixel(64, 64, 50, 50, 50);
3899 }
3900}
3901
Robert Carr503c7042017-09-27 15:06:08 -07003902TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003903 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07003904 fillSurfaceRGBA8(relative, 255, 255, 255);
3905
3906 Transaction t;
3907 t.setLayer(relative, INT32_MAX)
3908 .setRelativeLayer(mChild, relative->getHandle(), 1)
3909 .setPosition(mFGSurfaceControl, 0, 0)
3910 .apply(true);
3911
3912 // We expect that the child should have been elevated above our
3913 // INT_MAX layer even though it's not a child of it.
3914 {
chaviw0e3479f2018-09-10 16:49:30 -07003915 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07003916 mCapture->expectChildColor(0, 0);
3917 mCapture->expectChildColor(9, 9);
3918 mCapture->checkPixel(10, 10, 255, 255, 255);
3919 }
3920}
Vishnu Nair60356342018-11-13 13:00:45 -08003921class BoundlessLayerTest : public LayerUpdateTest {
3922protected:
3923 std::unique_ptr<ScreenCapture> mCapture;
3924};
3925
3926// Verify setting a size on a buffer layer has no effect.
3927TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
3928 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003929 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
3930 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003931 ASSERT_TRUE(bufferLayer->isValid());
3932 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
3933 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
3934 {
3935 mCapture = screenshot();
3936 // Top left of background must now be visible
3937 mCapture->expectBGColor(0, 0);
3938 // Foreground Surface bounds must be color layer
3939 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
3940 // Buffer layer should not extend past buffer bounds
3941 mCapture->expectFGColor(95, 95);
3942 }
3943}
3944
3945// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
3946// which will crop the color layer.
3947TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
3948 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003949 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3950 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003951 ASSERT_TRUE(colorLayer->isValid());
3952 asTransaction([&](Transaction& t) {
3953 t.setColor(colorLayer, half3{0, 0, 0});
3954 t.show(colorLayer);
3955 });
3956 {
3957 mCapture = screenshot();
3958 // Top left of background must now be visible
3959 mCapture->expectBGColor(0, 0);
3960 // Foreground Surface bounds must be color layer
3961 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3962 // Color layer should not extend past foreground bounds
3963 mCapture->expectBGColor(129, 129);
3964 }
3965}
3966
3967// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
3968// a crop which will be used to crop the color layer.
3969TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003970 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3971 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003972 ASSERT_TRUE(cropLayer->isValid());
3973 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003974 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3975 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003976 ASSERT_TRUE(colorLayer->isValid());
3977 asTransaction([&](Transaction& t) {
3978 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
3979 t.setColor(colorLayer, half3{0, 0, 0});
3980 t.show(cropLayer);
3981 t.show(colorLayer);
3982 });
3983 {
3984 mCapture = screenshot();
3985 // Top left of background must now be visible
3986 mCapture->expectBGColor(0, 0);
3987 // Top left of foreground must now be visible
3988 mCapture->expectFGColor(64, 64);
3989 // 5 pixels from the foreground we should see the child surface
3990 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
3991 // 10 pixels from the foreground we should be back to the foreground surface
3992 mCapture->expectFGColor(74, 74);
3993 }
3994}
3995
3996// Verify for boundless layer with no children, their transforms have no effect.
3997TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
3998 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003999 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4000 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004001 ASSERT_TRUE(colorLayer->isValid());
4002 asTransaction([&](Transaction& t) {
4003 t.setPosition(colorLayer, 320, 320);
4004 t.setMatrix(colorLayer, 2, 0, 0, 2);
4005 t.setColor(colorLayer, half3{0, 0, 0});
4006 t.show(colorLayer);
4007 });
4008 {
4009 mCapture = screenshot();
4010 // Top left of background must now be visible
4011 mCapture->expectBGColor(0, 0);
4012 // Foreground Surface bounds must be color layer
4013 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4014 // Color layer should not extend past foreground bounds
4015 mCapture->expectBGColor(129, 129);
4016 }
4017}
4018
4019// Verify for boundless layer with children, their transforms have an effect.
4020TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4021 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004022 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4023 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004024 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4025 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004026 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4027 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004028 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4029 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004030 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4031 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004032 ASSERT_TRUE(colorLayer->isValid());
4033 asTransaction([&](Transaction& t) {
4034 t.setPosition(boundlessLayerRightShift, 32, 0);
4035 t.show(boundlessLayerRightShift);
4036 t.setPosition(boundlessLayerDownShift, 0, 32);
4037 t.show(boundlessLayerDownShift);
4038 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4039 t.setColor(colorLayer, half3{0, 0, 0});
4040 t.show(colorLayer);
4041 });
4042 {
4043 mCapture = screenshot();
4044 // Top left of background must now be visible
4045 mCapture->expectBGColor(0, 0);
4046 // Top left of foreground must now be visible
4047 mCapture->expectFGColor(64, 64);
4048 // Foreground Surface bounds must be color layer
4049 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4050 // Color layer should not extend past foreground bounds
4051 mCapture->expectBGColor(129, 129);
4052 }
4053}
4054
4055// Verify child layers do not get clipped if they temporarily move into the negative
4056// coordinate space as the result of an intermediate transformation.
4057TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4058 sp<SurfaceControl> boundlessLayer =
4059 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4060 0 /* flags */, mFGSurfaceControl.get());
4061 ASSERT_TRUE(boundlessLayer != nullptr);
4062 ASSERT_TRUE(boundlessLayer->isValid());
4063 sp<SurfaceControl> colorLayer =
4064 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4065 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4066 ASSERT_TRUE(colorLayer != nullptr);
4067 ASSERT_TRUE(colorLayer->isValid());
4068 asTransaction([&](Transaction& t) {
4069 // shift child layer off bounds. If this layer was not boundless, we will
4070 // expect the child layer to be cropped.
4071 t.setPosition(boundlessLayer, 32, 32);
4072 t.show(boundlessLayer);
4073 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4074 // undo shift by parent
4075 t.setPosition(colorLayer, -32, -32);
4076 t.setColor(colorLayer, half3{0, 0, 0});
4077 t.show(colorLayer);
4078 });
4079 {
4080 mCapture = screenshot();
4081 // Top left of background must now be visible
4082 mCapture->expectBGColor(0, 0);
4083 // Foreground Surface bounds must be color layer
4084 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4085 // Color layer should not extend past foreground bounds
4086 mCapture->expectBGColor(129, 129);
4087 }
4088}
4089
4090// Verify for boundless root layers with children, their transforms have an effect.
4091TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004092 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4093 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004094 ASSERT_TRUE(rootBoundlessLayer->isValid());
4095 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004096 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4097 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4098
Vishnu Nair60356342018-11-13 13:00:45 -08004099 ASSERT_TRUE(colorLayer->isValid());
4100 asTransaction([&](Transaction& t) {
4101 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4102 t.setPosition(rootBoundlessLayer, 32, 32);
4103 t.show(rootBoundlessLayer);
4104 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4105 t.setColor(colorLayer, half3{0, 0, 0});
4106 t.show(colorLayer);
4107 t.hide(mFGSurfaceControl);
4108 });
4109 {
4110 mCapture = screenshot();
4111 // Top left of background must now be visible
4112 mCapture->expectBGColor(0, 0);
4113 // Top left of foreground must now be visible
4114 mCapture->expectBGColor(31, 31);
4115 // Foreground Surface bounds must be color layer
4116 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4117 // Color layer should not extend past foreground bounds
4118 mCapture->expectBGColor(97, 97);
4119 }
4120}
Robert Carr503c7042017-09-27 15:06:08 -07004121
chaviwa76b2712017-09-20 12:02:26 -07004122class ScreenCaptureTest : public LayerUpdateTest {
4123protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004124 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004125};
4126
4127TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4128 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004129 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004130 mCapture->expectBGColor(0, 0);
4131 // Doesn't capture FG layer which is at 64, 64
4132 mCapture->expectBGColor(64, 64);
4133}
4134
4135TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4136 auto fgHandle = mFGSurfaceControl->getHandle();
4137
Vishnu Nair88a11f22018-11-28 18:30:57 -08004138 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4139 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004140 fillSurfaceRGBA8(child, 200, 200, 200);
4141
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004142 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004143
4144 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004145 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004146 mCapture->expectFGColor(10, 10);
4147 mCapture->expectChildColor(0, 0);
4148}
4149
Robert Carr578038f2018-03-09 12:25:24 -08004150TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4151 auto fgHandle = mFGSurfaceControl->getHandle();
4152
Vishnu Nair88a11f22018-11-28 18:30:57 -08004153 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4154 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004155 fillSurfaceRGBA8(child, 200, 200, 200);
4156
4157 SurfaceComposerClient::Transaction().show(child).apply(true);
4158
4159 // Captures mFGSurfaceControl's child
4160 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4161 mCapture->checkPixel(10, 10, 0, 0, 0);
4162 mCapture->expectChildColor(0, 0);
4163}
4164
chaviw50da5042018-04-09 13:49:37 -07004165TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004166 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4167 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07004168
4169 fillSurfaceRGBA8(child, 200, 200, 200);
4170
4171 SurfaceComposerClient::Transaction().show(child).apply(true);
4172
4173 auto childHandle = child->getHandle();
4174
4175 // Captures child
4176 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
4177 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
4178 // Area outside of child's bounds is transparent.
4179 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
4180}
4181
chaviw4b129c22018-04-09 16:19:43 -07004182TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
4183 auto fgHandle = mFGSurfaceControl->getHandle();
4184
Vishnu Nair88a11f22018-11-28 18:30:57 -08004185 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4186 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4187 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07004188 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07004189 fillSurfaceRGBA8(child, 200, 200, 200);
4190 fillSurfaceRGBA8(relative, 100, 100, 100);
4191
4192 SurfaceComposerClient::Transaction()
4193 .show(child)
4194 // Set relative layer above fg layer so should be shown above when computing all layers.
4195 .setRelativeLayer(relative, fgHandle, 1)
4196 .show(relative)
4197 .apply(true);
4198
4199 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
4200 ScreenCapture::captureLayers(&mCapture, fgHandle);
4201 mCapture->expectFGColor(10, 10);
4202 mCapture->expectChildColor(0, 0);
4203}
4204
4205TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
4206 auto fgHandle = mFGSurfaceControl->getHandle();
4207
Vishnu Nair88a11f22018-11-28 18:30:57 -08004208 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4209 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4210 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
4211 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07004212 fillSurfaceRGBA8(child, 200, 200, 200);
4213 fillSurfaceRGBA8(relative, 100, 100, 100);
4214
4215 SurfaceComposerClient::Transaction()
4216 .show(child)
4217 // Set relative layer below fg layer but relative to child layer so it should be shown
4218 // above child layer.
4219 .setLayer(relative, -1)
4220 .setRelativeLayer(relative, child->getHandle(), 1)
4221 .show(relative)
4222 .apply(true);
4223
4224 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
4225 // relative value should be taken into account, placing it above child layer.
4226 ScreenCapture::captureLayers(&mCapture, fgHandle);
4227 mCapture->expectFGColor(10, 10);
4228 // Relative layer is showing on top of child layer
4229 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
4230}
Robert Carr578038f2018-03-09 12:25:24 -08004231
4232// In the following tests we verify successful skipping of a parent layer,
4233// so we use the same verification logic and only change how we mutate
4234// the parent layer to verify that various properties are ignored.
4235class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
4236public:
4237 void SetUp() override {
4238 LayerUpdateTest::SetUp();
4239
Vishnu Nair88a11f22018-11-28 18:30:57 -08004240 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4241 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004242 fillSurfaceRGBA8(mChild, 200, 200, 200);
4243
4244 SurfaceComposerClient::Transaction().show(mChild).apply(true);
4245 }
4246
4247 void verify() {
4248 auto fgHandle = mFGSurfaceControl->getHandle();
4249 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4250 mCapture->checkPixel(10, 10, 0, 0, 0);
4251 mCapture->expectChildColor(0, 0);
4252 }
4253
4254 std::unique_ptr<ScreenCapture> mCapture;
4255 sp<SurfaceControl> mChild;
4256};
4257
4258TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4259
4260 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4261
4262 // Even though the parent is hidden we should still capture the child.
4263 verify();
4264}
4265
4266TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004267 SurfaceComposerClient::Transaction()
4268 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
4269 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08004270
4271 // Even though the parent is cropped out we should still capture the child.
4272 verify();
4273}
4274
4275TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4276
4277 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4278
4279 // We should not inherit the parent scaling.
4280 verify();
4281}
4282
Robert Carr15eae092018-03-23 13:43:53 -07004283TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4284 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4285
4286 // Even though the parent is hidden we should still capture the child.
4287 verify();
4288
4289 // Verify everything was properly hidden when rendering the full-screen.
4290 screenshot()->expectBGColor(0,0);
4291}
4292
4293
chaviwa76b2712017-09-20 12:02:26 -07004294TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4295 auto fgHandle = mFGSurfaceControl->getHandle();
4296
Vishnu Nair88a11f22018-11-28 18:30:57 -08004297 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4298 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004299 fillSurfaceRGBA8(child, 200, 200, 200);
4300
Vishnu Nair88a11f22018-11-28 18:30:57 -08004301 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4302 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004303
4304 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4305 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004306 .show(child)
4307 .setPosition(grandchild, 5, 5)
4308 .show(grandchild)
4309 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004310
4311 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004312 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004313 mCapture->expectFGColor(10, 10);
4314 mCapture->expectChildColor(0, 0);
4315 mCapture->checkPixel(5, 5, 50, 50, 50);
4316}
4317
4318TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004319 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4320 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004321 fillSurfaceRGBA8(child, 200, 200, 200);
4322 auto childHandle = child->getHandle();
4323
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004324 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004325
4326 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004327 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004328 mCapture->expectChildColor(0, 0);
4329 mCapture->expectChildColor(9, 9);
4330}
4331
4332TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004333 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4334 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004335 fillSurfaceRGBA8(child, 200, 200, 200);
4336 auto childHandle = child->getHandle();
4337
Vishnu Nair88a11f22018-11-28 18:30:57 -08004338 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4339 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004340 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4341
4342 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004343 .show(child)
4344 .setPosition(grandchild, 5, 5)
4345 .show(grandchild)
4346 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004347
4348 auto grandchildHandle = grandchild->getHandle();
4349
4350 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004351 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004352 mCapture->checkPixel(0, 0, 50, 50, 50);
4353 mCapture->checkPixel(4, 4, 50, 50, 50);
4354}
4355
chaviw7206d492017-11-10 16:16:12 -08004356TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004357 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004358 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4359 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004360
Marissa Wall61c58622018-07-18 10:12:20 -07004361 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4362 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004363
4364 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004365 .setLayer(redLayer, INT32_MAX - 1)
4366 .show(redLayer)
4367 .show(blueLayer)
4368 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004369
4370 auto redLayerHandle = redLayer->getHandle();
4371
4372 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004373 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4374 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4375 // red area below the blue area
4376 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4377 // red area to the right of the blue area
4378 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004379
Marissa Wall861616d2018-10-22 12:52:23 -07004380 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004381 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004382 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4383 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004384 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004385 mCapture->checkPixel(30, 30, 0, 0, 0);
4386}
4387
4388TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004389 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004390 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4391 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004392
Marissa Wall61c58622018-07-18 10:12:20 -07004393 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4394 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004395
4396 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004397 .setLayer(redLayer, INT32_MAX - 1)
4398 .show(redLayer)
4399 .show(blueLayer)
4400 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004401
4402 auto redLayerHandle = redLayer->getHandle();
4403
4404 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004405 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4406 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4407 // red area below the blue area
4408 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4409 // red area to the right of the blue area
4410 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004411
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004412 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004413 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004414 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4415 // red area below the blue area
4416 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4417 // red area to the right of the blue area
4418 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004419 mCapture->checkPixel(30, 30, 0, 0, 0);
4420}
4421
4422TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004423 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004424
Marissa Wall61c58622018-07-18 10:12:20 -07004425 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004426
4427 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07004428 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08004429 SurfaceComposerClient::Transaction().apply(true);
4430
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004431 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004432
4433 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004434 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4435 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004436}
4437
chaviw8e3fe5d2018-02-22 10:55:42 -08004438
4439class DereferenceSurfaceControlTest : public LayerTransactionTest {
4440protected:
4441 void SetUp() override {
4442 LayerTransactionTest::SetUp();
4443 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004444 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004445 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004446 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004447 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4448 {
4449 SCOPED_TRACE("before anything");
4450 auto shot = screenshot();
4451 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4452 }
4453 }
4454 void TearDown() override {
4455 LayerTransactionTest::TearDown();
4456 bgLayer = 0;
4457 fgLayer = 0;
4458 }
4459
4460 sp<SurfaceControl> bgLayer;
4461 sp<SurfaceControl> fgLayer;
4462};
4463
4464TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4465 fgLayer = nullptr;
4466 {
4467 SCOPED_TRACE("after setting null");
4468 auto shot = screenshot();
4469 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4470 }
4471}
4472
4473TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4474 auto transaction = Transaction().show(fgLayer);
4475 fgLayer = nullptr;
4476 {
4477 SCOPED_TRACE("after setting null");
4478 auto shot = screenshot();
4479 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4480 }
4481}
4482
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004483} // namespace android