blob: 99de216fec68fa52587f68fb932f4e59be9d29d1 [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
Marissa Wallfda30bb2018-10-12 11:34:28 -070018#include <chrono>
19#include <cinttypes>
Chia-I Wu718daf82017-10-20 11:57:17 -070020#include <functional>
21#include <limits>
22#include <ostream>
Marissa Wallfda30bb2018-10-12 11:34:28 -070023#include <thread>
Chia-I Wu718daf82017-10-20 11:57:17 -070024
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070025#include <gtest/gtest.h>
26
Michael Lentine5a16a622015-05-21 13:48:24 -070027#include <android/native_window.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070030#include <gui/LayerState.h>
31
Mathias Agopian90ac7992012-02-25 18:48:35 -080032#include <gui/Surface.h>
33#include <gui/SurfaceComposerClient.h>
34#include <private/gui/ComposerService.h>
35
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070036#include <ui/ColorSpace.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070038#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070039#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070041#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070042#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070043
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070044namespace android {
45
Chia-I Wu718daf82017-10-20 11:57:17 -070046namespace {
47
48struct Color {
49 uint8_t r;
50 uint8_t g;
51 uint8_t b;
52 uint8_t a;
53
54 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070055 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070056 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070057 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070058 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070059 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070060};
61
62const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070063const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070064const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070065const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070066const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070067const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070068
Marissa Wall61c58622018-07-18 10:12:20 -070069using android::hardware::graphics::common::V1_1::BufferUsage;
Marissa Wallfda30bb2018-10-12 11:34:28 -070070using namespace std::chrono_literals;
Marissa Wall61c58622018-07-18 10:12:20 -070071
Chia-I Wu718daf82017-10-20 11:57:17 -070072std::ostream& operator<<(std::ostream& os, const Color& color) {
73 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
74 return os;
75}
76
77// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070078void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
79 const Color& color) {
80 Rect r(0, 0, buffer.width, buffer.height);
81 if (!r.intersect(rect, &r)) {
82 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070083 }
84
Marissa Wall61c58622018-07-18 10:12:20 -070085 int32_t width = r.right - r.left;
86 int32_t height = r.bottom - r.top;
87
88 for (int32_t row = 0; row < height; row++) {
89 uint8_t* dst =
90 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
91 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070092 dst[0] = color.r;
93 dst[1] = color.g;
94 dst[2] = color.b;
95 dst[3] = color.a;
96 dst += 4;
97 }
98 }
99}
100
Marissa Wall61c58622018-07-18 10:12:20 -0700101// Fill a region with the specified color.
102void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
103 Rect r(0, 0, buffer->width, buffer->height);
104 if (!r.intersect(rect, &r)) {
105 return;
106 }
107
108 int32_t width = r.right - r.left;
109 int32_t height = r.bottom - r.top;
110
111 uint8_t* pixels;
112 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
113 reinterpret_cast<void**>(&pixels));
114
115 for (int32_t row = 0; row < height; row++) {
116 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
117 for (int32_t column = 0; column < width; column++) {
118 dst[0] = color.r;
119 dst[1] = color.g;
120 dst[2] = color.b;
121 dst[3] = color.a;
122 dst += 4;
123 }
124 }
125 buffer->unlock();
126}
127
Chia-I Wu718daf82017-10-20 11:57:17 -0700128// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000129void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700130 const Color& color, uint8_t tolerance) {
131 int32_t x = rect.left;
132 int32_t y = rect.top;
133 int32_t width = rect.right - rect.left;
134 int32_t height = rect.bottom - rect.top;
135
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000136 int32_t bufferWidth = int32_t(outBuffer->getWidth());
137 int32_t bufferHeight = int32_t(outBuffer->getHeight());
138 if (x + width > bufferWidth) {
139 x = std::min(x, bufferWidth);
140 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700141 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000142 if (y + height > bufferHeight) {
143 y = std::min(y, bufferHeight);
144 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700145 }
146
147 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
148 uint8_t tmp = a >= b ? a - b : b - a;
149 return tmp <= tolerance;
150 };
151 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000152 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700153 for (int32_t i = 0; i < width; i++) {
154 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
155 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
156 << "pixel @ (" << x + i << ", " << y + j << "): "
157 << "expected (" << color << "), "
158 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
159 src += 4;
160 }
161 }
162}
163
164} // anonymous namespace
165
Robert Carr4cdc58f2017-08-23 14:22:20 -0700166using Transaction = SurfaceComposerClient::Transaction;
167
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700168// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700169static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
170 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800171 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700172 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800173 ASSERT_TRUE(s != nullptr);
174 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800175 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700176 for (int y = 0; y < outBuffer.height; y++) {
177 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700178 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700179 pixel[0] = r;
180 pixel[1] = g;
181 pixel[2] = b;
182 pixel[3] = 255;
183 }
184 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700185 if (unlock) {
186 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
187 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700188}
189
190// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
191// individual pixel values for testing purposes.
192class ScreenCapture : public RefBase {
193public:
chaviw0e3479f2018-09-10 16:49:30 -0700194 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700195 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700196 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700197 SurfaceComposerClient::Transaction().apply(true);
198
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000199 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700200 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700201 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
202 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000203 }
204
205 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
206 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
207 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
208 SurfaceComposerClient::Transaction().apply(true);
209
210 sp<GraphicBuffer> outBuffer;
211 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
212 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700213 }
214
Robert Carr578038f2018-03-09 12:25:24 -0800215 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
216 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
217 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
218 SurfaceComposerClient::Transaction().apply(true);
219
220 sp<GraphicBuffer> outBuffer;
221 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
222 *sc = std::make_unique<ScreenCapture>(outBuffer);
223 }
224
Chia-I Wu718daf82017-10-20 11:57:17 -0700225 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000226 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
227 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700228 }
229
230 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000231 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700232 const bool leftBorder = rect.left > 0;
233 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000234 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
235 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700236
237 if (topBorder) {
238 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
239 if (leftBorder) {
240 top.left -= 1;
241 }
242 if (rightBorder) {
243 top.right += 1;
244 }
245 expectColor(top, color, tolerance);
246 }
247 if (leftBorder) {
248 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
249 expectColor(left, color, tolerance);
250 }
251 if (rightBorder) {
252 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
253 expectColor(right, color, tolerance);
254 }
255 if (bottomBorder) {
256 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
257 if (leftBorder) {
258 bottom.left -= 1;
259 }
260 if (rightBorder) {
261 bottom.right += 1;
262 }
263 expectColor(bottom, color, tolerance);
264 }
265 }
266
Chia-I Wu93853fe2017-11-02 08:30:27 -0700267 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
268 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
269 uint8_t tolerance = 0) {
270 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
271
272 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
273 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
274 // avoid checking borders due to unspecified filtering behavior
275 const int32_t offsetX = filtered ? 2 : 0;
276 const int32_t offsetY = filtered ? 2 : 0;
277 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
278 tolerance);
279 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
280 tolerance);
281 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
282 tolerance);
283 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
284 bottomRight, tolerance);
285 }
286
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700287 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000288 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
289 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700290 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
291 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700292 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
293 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700294 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700295 }
296 }
297
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700298 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700299
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700300 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700301
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700302 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700303
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000304 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
305 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700306 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700307
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000308 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700309
310private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000311 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800312 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700313};
314
Chia-I Wu718daf82017-10-20 11:57:17 -0700315class LayerTransactionTest : public ::testing::Test {
316protected:
317 void SetUp() override {
318 mClient = new SurfaceComposerClient;
319 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
320
321 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700322
323 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
324 sp<IBinder> binder = sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
Ady Abraham37965d42018-11-01 13:43:32 -0700325 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
Chia-I Wu718daf82017-10-20 11:57:17 -0700326 }
327
chaviw0e3479f2018-09-10 16:49:30 -0700328 virtual void TearDown() {
329 mBlackBgSurface = 0;
330 mClient->dispose();
331 mClient = 0;
332 }
333
Marissa Wallfda30bb2018-10-12 11:34:28 -0700334 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
335 const char* name, uint32_t width, uint32_t height,
Marissa Wall61c58622018-07-18 10:12:20 -0700336 uint32_t flags = 0) {
Vishnu Nair88a11f22018-11-28 18:30:57 -0800337 auto layer = createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags);
Chia-I Wu718daf82017-10-20 11:57:17 -0700338
Vishnu Nair60356342018-11-13 13:00:45 -0800339 Transaction t;
340 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -0800341
342 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700343 if (error != NO_ERROR) {
344 ADD_FAILURE() << "failed to initialize SurfaceControl";
345 layer.clear();
346 }
347
348 return layer;
349 }
350
Vishnu Nair88a11f22018-11-28 18:30:57 -0800351 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
352 const char* name, uint32_t width, uint32_t height,
353 PixelFormat format, uint32_t flags,
354 SurfaceControl* parent = nullptr) {
355 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
356 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
357 return layer;
358 }
359
Marissa Wallfda30bb2018-10-12 11:34:28 -0700360 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
361 uint32_t flags = 0) {
362 return createLayer(mClient, name, width, height, flags);
363 }
364
Marissa Wall61c58622018-07-18 10:12:20 -0700365 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700366 // wait for previous transactions (such as setSize) to complete
367 Transaction().apply(true);
368
369 ANativeWindow_Buffer buffer = {};
370 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
371
372 return buffer;
373 }
374
Marissa Wall61c58622018-07-18 10:12:20 -0700375 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700376 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
377
378 // wait for the newly posted buffer to be latched
379 waitForLayerBuffers();
380 }
381
Marissa Wall61c58622018-07-18 10:12:20 -0700382 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
383 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700384 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700385 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
386 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
387 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700388 }
389
Marissa Wall61c58622018-07-18 10:12:20 -0700390 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
391 int32_t bufferWidth, int32_t bufferHeight) {
392 sp<GraphicBuffer> buffer =
393 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
394 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
395 BufferUsage::COMPOSER_OVERLAY,
396 "test");
397 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
398 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
399 }
400
401 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
402 int32_t bufferWidth, int32_t bufferHeight) {
403 switch (mLayerType) {
404 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
405 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
406 break;
407 case ISurfaceComposerClient::eFXSurfaceBufferState:
408 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
409 break;
410 default:
411 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
412 }
413 }
414
415 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
416 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700417 const Color& topRight, const Color& bottomLeft,
418 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700419 switch (mLayerType) {
420 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
421 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
422 bottomLeft, bottomRight);
423 break;
424 case ISurfaceComposerClient::eFXSurfaceBufferState:
425 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
426 bottomLeft, bottomRight);
427 break;
428 default:
429 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
430 }
431 }
432
433 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
434 int32_t bufferHeight, const Color& topLeft,
435 const Color& topRight, const Color& bottomLeft,
436 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700437 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700438 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
439 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700440
Marissa Wall61c58622018-07-18 10:12:20 -0700441 const int32_t halfW = bufferWidth / 2;
442 const int32_t halfH = bufferHeight / 2;
443 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
444 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
445 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
446 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
447 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700448
Marissa Wall61c58622018-07-18 10:12:20 -0700449 postBufferQueueLayerBuffer(layer);
450 }
451
452 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
453 int32_t bufferHeight, const Color& topLeft,
454 const Color& topRight, const Color& bottomLeft,
455 const Color& bottomRight) {
456 sp<GraphicBuffer> buffer =
457 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
458 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
459 BufferUsage::COMPOSER_OVERLAY,
460 "test");
461
462 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
463
464 const int32_t halfW = bufferWidth / 2;
465 const int32_t halfH = bufferHeight / 2;
466 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
467 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
468 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
469 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
470
471 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700472 }
473
chaviw0e3479f2018-09-10 16:49:30 -0700474 std::unique_ptr<ScreenCapture> screenshot() {
475 std::unique_ptr<ScreenCapture> screenshot;
476 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700477 return screenshot;
478 }
479
480 sp<SurfaceComposerClient> mClient;
481
482 sp<IBinder> mDisplay;
483 uint32_t mDisplayWidth;
484 uint32_t mDisplayHeight;
485 uint32_t mDisplayLayerStack;
486
487 // leave room for ~256 layers
488 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
489
Marissa Wall61c58622018-07-18 10:12:20 -0700490 void setPositionWithResizeHelper(uint32_t layerType);
491 void setSizeBasicHelper(uint32_t layerType);
492 void setMatrixWithResizeHelper(uint32_t layerType);
493
chaviw0e3479f2018-09-10 16:49:30 -0700494 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700495 bool mColorManagementUsed;
496
Chia-I Wu718daf82017-10-20 11:57:17 -0700497private:
498 void SetUpDisplay() {
499 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
500 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
501
502 // get display width/height
503 DisplayInfo info;
504 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
505 mDisplayWidth = info.w;
506 mDisplayHeight = info.h;
507
508 // After a new buffer is queued, SurfaceFlinger is notified and will
509 // latch the new buffer on next vsync. Let's heuristically wait for 3
510 // vsyncs.
511 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
512
513 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700514
Vishnu Nair88a11f22018-11-28 18:30:57 -0800515 mBlackBgSurface =
516 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
517 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700518
Chia-I Wu718daf82017-10-20 11:57:17 -0700519 // set layer stack (b/68888219)
520 Transaction t;
521 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800522 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700523 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
524 t.setColor(mBlackBgSurface, half3{0, 0, 0});
525 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700526 t.apply();
527 }
528
chaviw0e3479f2018-09-10 16:49:30 -0700529 void waitForLayerBuffers() {
530 // Request an empty transaction to get applied synchronously to ensure the buffer is
531 // latched.
532 Transaction().apply(true);
533 usleep(mBufferPostDelay);
534 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700535
536 int32_t mBufferPostDelay;
537};
538
Marissa Wall61c58622018-07-18 10:12:20 -0700539class LayerTypeTransactionTest : public LayerTransactionTest,
540 public ::testing::WithParamInterface<uint32_t> {
541public:
542 LayerTypeTransactionTest() { mLayerType = GetParam(); }
543
544 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
545 uint32_t flags = 0) override {
546 // if the flags already have a layer type specified, return an error
547 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
548 return nullptr;
549 }
550 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
551 }
552
553 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
554 int32_t bufferHeight) {
555 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
556 bufferWidth, bufferHeight));
557 }
558
559 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
560 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
561 const Color& bottomLeft, const Color& bottomRight) {
562 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
563 bufferWidth, bufferHeight,
564 topLeft, topRight,
565 bottomLeft, bottomRight));
566 }
567
568protected:
569 uint32_t mLayerType;
570};
571
572INSTANTIATE_TEST_CASE_P(
573 LayerTypeTransactionTests, LayerTypeTransactionTest,
574 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
575 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
576
577TEST_P(LayerTypeTransactionTest, SetPositionBasic) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700578 sp<SurfaceControl> layer;
579 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700580 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700581
582 {
583 SCOPED_TRACE("default position");
584 auto shot = screenshot();
585 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
586 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
587 }
588
589 Transaction().setPosition(layer, 5, 10).apply();
590 {
591 SCOPED_TRACE("new position");
592 auto shot = screenshot();
593 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
594 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
595 }
596}
597
Marissa Wall61c58622018-07-18 10:12:20 -0700598TEST_P(LayerTypeTransactionTest, SetPositionRounding) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700599 sp<SurfaceControl> layer;
600 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700601 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700602
603 // GLES requires only 4 bits of subpixel precision during rasterization
604 // XXX GLES composition does not match HWC composition due to precision
605 // loss (b/69315223)
606 const float epsilon = 1.0f / 16.0f;
607 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
608 {
609 SCOPED_TRACE("rounding down");
610 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
611 }
612
613 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
614 {
615 SCOPED_TRACE("rounding up");
616 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
617 }
618}
619
Marissa Wall61c58622018-07-18 10:12:20 -0700620TEST_P(LayerTypeTransactionTest, SetPositionOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700621 sp<SurfaceControl> layer;
622 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700623 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700624
625 Transaction().setPosition(layer, -32, -32).apply();
626 {
627 SCOPED_TRACE("negative coordinates");
628 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
629 }
630
631 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
632 {
633 SCOPED_TRACE("positive coordinates");
634 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
635 }
636}
637
Marissa Wall61c58622018-07-18 10:12:20 -0700638TEST_P(LayerTypeTransactionTest, SetPositionPartiallyOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700639 sp<SurfaceControl> layer;
640 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700641 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700642
643 // partially out of bounds
644 Transaction().setPosition(layer, -30, -30).apply();
645 {
646 SCOPED_TRACE("negative coordinates");
647 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
648 }
649
650 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
651 {
652 SCOPED_TRACE("positive coordinates");
653 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
654 mDisplayHeight),
655 Color::RED);
656 }
657}
658
Marissa Wall61c58622018-07-18 10:12:20 -0700659void LayerTransactionTest::setPositionWithResizeHelper(uint32_t layerType) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700660 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700661 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
662 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700663
664 // setPosition is applied immediately by default, with or without resize
665 // pending
666 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
667 {
668 SCOPED_TRACE("resize pending");
669 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700670 Rect rect;
671 switch (layerType) {
672 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
673 rect = {5, 10, 37, 42};
674 break;
675 case ISurfaceComposerClient::eFXSurfaceBufferState:
676 rect = {5, 10, 69, 74};
677 break;
678 default:
679 ASSERT_FALSE(true) << "Unsupported layer type";
680 }
681
682 shot->expectColor(rect, Color::RED);
683 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700684 }
685
Marissa Wall61c58622018-07-18 10:12:20 -0700686 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700687 {
688 SCOPED_TRACE("resize applied");
689 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
690 }
691}
692
Marissa Wall61c58622018-07-18 10:12:20 -0700693TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
694 ASSERT_NO_FATAL_FAILURE(
695 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
696}
697
698TEST_F(LayerTransactionTest, SetPositionWithResize_BufferState) {
699 ASSERT_NO_FATAL_FAILURE(
700 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
701}
702
703TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700704 sp<SurfaceControl> layer;
705 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700706 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700707
708 // request setPosition to be applied with the next resize
709 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
710 {
711 SCOPED_TRACE("new position pending");
712 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
713 }
714
715 Transaction().setPosition(layer, 15, 20).apply();
716 {
717 SCOPED_TRACE("pending new position modified");
718 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
719 }
720
721 Transaction().setSize(layer, 64, 64).apply();
722 {
723 SCOPED_TRACE("resize pending");
724 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
725 }
726
727 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700728 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700729 {
730 SCOPED_TRACE("new position applied");
731 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
732 }
733}
734
Marissa Wall61c58622018-07-18 10:12:20 -0700735TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700736 sp<SurfaceControl> layer;
737 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700738 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700739
740 // setPosition is not immediate even with SCALE_TO_WINDOW override
741 Transaction()
742 .setPosition(layer, 5, 10)
743 .setSize(layer, 64, 64)
744 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
745 .setGeometryAppliesWithResize(layer)
746 .apply();
747 {
748 SCOPED_TRACE("new position pending");
749 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
750 }
751
Marissa Wall61c58622018-07-18 10:12:20 -0700752 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700753 {
754 SCOPED_TRACE("new position applied");
755 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
756 }
757}
758
Marissa Wall61c58622018-07-18 10:12:20 -0700759void LayerTransactionTest::setSizeBasicHelper(uint32_t layerType) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700760 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700761 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
762 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700763
764 Transaction().setSize(layer, 64, 64).apply();
765 {
766 SCOPED_TRACE("resize pending");
767 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700768 Rect rect;
769 switch (layerType) {
770 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
771 rect = {0, 0, 32, 32};
772 break;
773 case ISurfaceComposerClient::eFXSurfaceBufferState:
774 rect = {0, 0, 64, 64};
775 break;
776 default:
777 ASSERT_FALSE(true) << "Unsupported layer type";
778 }
779 shot->expectColor(rect, Color::RED);
780 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700781 }
782
Marissa Wall61c58622018-07-18 10:12:20 -0700783 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700784 {
785 SCOPED_TRACE("resize applied");
786 auto shot = screenshot();
787 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
788 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
789 }
790}
791
Marissa Wall61c58622018-07-18 10:12:20 -0700792TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
793 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue);
794}
795
796TEST_F(LayerTransactionTest, SetSizeBasic_BufferState) {
797 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState);
798}
799
800TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700801 // cannot test robustness against invalid sizes (zero or really huge)
802}
803
Marissa Wall61c58622018-07-18 10:12:20 -0700804TEST_P(LayerTypeTransactionTest, SetSizeWithScaleToWindow) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700805 sp<SurfaceControl> layer;
806 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700807 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700808
809 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
810 Transaction()
811 .setSize(layer, 64, 64)
812 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
813 .apply();
814 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
815}
816
Marissa Wall61c58622018-07-18 10:12:20 -0700817TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700818 sp<SurfaceControl> layerR;
819 sp<SurfaceControl> layerG;
820 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700821 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700822 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700823 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700824
825 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
826 {
827 SCOPED_TRACE("layerR");
828 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
829 }
830
831 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
832 {
833 SCOPED_TRACE("layerG");
834 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
835 }
836}
837
Marissa Wall61c58622018-07-18 10:12:20 -0700838TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700839 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800840 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700841 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800842 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700843 sp<SurfaceControl> layerR;
844 sp<SurfaceControl> layerG;
845 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700846 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700847 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700848 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700849
chaviw0e3479f2018-09-10 16:49:30 -0700850 Transaction()
851 .reparent(layerR, parent->getHandle())
852 .reparent(layerG, parent->getHandle())
853 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700854 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
855 {
856 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700857 auto shot = screenshot();
858 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700859 }
860
861 Transaction().setLayer(layerR, -3).apply();
862 {
863 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700864 auto shot = screenshot();
865 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700866 }
867}
868
Marissa Wall61c58622018-07-18 10:12:20 -0700869TEST_P(LayerTypeTransactionTest, SetRelativeZBasic) {
Chia-I Wu49313302017-10-31 10:14:40 -0700870 sp<SurfaceControl> layerR;
871 sp<SurfaceControl> layerG;
872 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700873 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700874 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700875 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700876
877 Transaction()
878 .setPosition(layerG, 16, 16)
879 .setRelativeLayer(layerG, layerR->getHandle(), 1)
880 .apply();
881 {
882 SCOPED_TRACE("layerG above");
883 auto shot = screenshot();
884 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
885 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
886 }
887
888 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
889 {
890 SCOPED_TRACE("layerG below");
891 auto shot = screenshot();
892 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
893 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
894 }
895}
896
Marissa Wall61c58622018-07-18 10:12:20 -0700897TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700898 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800899 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700900 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800901 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800902 sp<SurfaceControl> layerR;
903 sp<SurfaceControl> layerG;
904 sp<SurfaceControl> layerB;
905 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700906 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800907 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700908 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800909 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700910 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800911
chaviw0e3479f2018-09-10 16:49:30 -0700912 Transaction()
913 .reparent(layerB, parent->getHandle())
914 .apply();
915
Chia-I Wuec2d9852017-11-21 09:21:01 -0800916 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
917 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
918
chaviw0e3479f2018-09-10 16:49:30 -0700919 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800920 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700921 sp<IBinder> parentHandle = parent->getHandle();
922 ScreenCapture::captureLayers(&screenshot, parentHandle);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800923 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
924}
925
Marissa Wall61c58622018-07-18 10:12:20 -0700926TEST_P(LayerTypeTransactionTest, SetRelativeZGroup) {
Chia-I Wu49313302017-10-31 10:14:40 -0700927 sp<SurfaceControl> layerR;
928 sp<SurfaceControl> layerG;
929 sp<SurfaceControl> layerB;
930 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700931 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700932 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700933 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700934 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700935 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700936
937 // layerR = 0, layerG = layerR + 3, layerB = 2
938 Transaction()
939 .setPosition(layerG, 8, 8)
940 .setRelativeLayer(layerG, layerR->getHandle(), 3)
941 .setPosition(layerB, 16, 16)
942 .setLayer(layerB, mLayerZBase + 2)
943 .apply();
944 {
945 SCOPED_TRACE("(layerR < layerG) < layerB");
946 auto shot = screenshot();
947 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
948 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
949 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
950 }
951
952 // layerR = 4, layerG = layerR + 3, layerB = 2
953 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
954 {
955 SCOPED_TRACE("layerB < (layerR < layerG)");
956 auto shot = screenshot();
957 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
958 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
959 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
960 }
961
962 // layerR = 4, layerG = layerR - 3, layerB = 2
963 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
964 {
965 SCOPED_TRACE("layerB < (layerG < layerR)");
966 auto shot = screenshot();
967 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
968 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
969 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
970 }
971
972 // restore to absolute z
973 // layerR = 4, layerG = 0, layerB = 2
974 Transaction().setLayer(layerG, mLayerZBase).apply();
975 {
976 SCOPED_TRACE("layerG < layerB < layerR");
977 auto shot = screenshot();
978 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
979 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
980 }
981
982 // layerR should not affect layerG anymore
983 // layerR = 1, layerG = 0, layerB = 2
984 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
985 {
986 SCOPED_TRACE("layerG < layerR < layerB");
987 auto shot = screenshot();
988 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
989 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
990 }
991}
992
Marissa Wall61c58622018-07-18 10:12:20 -0700993TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700994 sp<SurfaceControl> layerR;
995 sp<SurfaceControl> layerG;
996
997 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700998 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700999 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001000 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001001
1002 Transaction()
1003 .setPosition(layerG, 16, 16)
1004 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1005 .apply();
1006
1007 mClient->destroySurface(layerG->getHandle());
1008 // layerG should have been removed
1009 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1010}
1011
Marissa Wall61c58622018-07-18 10:12:20 -07001012TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001013 sp<SurfaceControl> layer;
1014 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001015 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001016
1017 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1018 {
1019 SCOPED_TRACE("layer hidden");
1020 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1021 }
1022
1023 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1024 {
1025 SCOPED_TRACE("layer shown");
1026 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1027 }
1028}
1029
Marissa Wall61c58622018-07-18 10:12:20 -07001030TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001031 const Color translucentRed = {100, 0, 0, 100};
1032 sp<SurfaceControl> layerR;
1033 sp<SurfaceControl> layerG;
1034 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001035 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001036 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001037 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001038
1039 Transaction()
1040 .setLayer(layerR, mLayerZBase + 1)
1041 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1042 .apply();
1043 {
1044 SCOPED_TRACE("layerR opaque");
1045 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1046 }
1047
1048 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1049 {
1050 SCOPED_TRACE("layerR translucent");
1051 const uint8_t g = uint8_t(255 - translucentRed.a);
1052 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1053 }
1054}
1055
Marissa Wall61c58622018-07-18 10:12:20 -07001056TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001057 sp<SurfaceControl> layer;
1058 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001059 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001060
1061 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001062 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001063 Transaction()
1064 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1065 .apply(true);
1066 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001067 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001068
1069 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1070 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001071 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001072}
1073
Marissa Wall61c58622018-07-18 10:12:20 -07001074TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001075 const Rect top(0, 0, 32, 16);
1076 const Rect bottom(0, 16, 32, 32);
1077 sp<SurfaceControl> layer;
1078 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1079
1080 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001081 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1082 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1083 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001084 // setTransparentRegionHint always applies to the following buffer
1085 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001086 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001087 {
1088 SCOPED_TRACE("top transparent");
1089 auto shot = screenshot();
1090 shot->expectColor(top, Color::BLACK);
1091 shot->expectColor(bottom, Color::RED);
1092 }
1093
1094 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1095 {
1096 SCOPED_TRACE("transparent region hint pending");
1097 auto shot = screenshot();
1098 shot->expectColor(top, Color::BLACK);
1099 shot->expectColor(bottom, Color::RED);
1100 }
1101
Marissa Wall61c58622018-07-18 10:12:20 -07001102 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1103 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1104 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1105 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001106 {
1107 SCOPED_TRACE("bottom transparent");
1108 auto shot = screenshot();
1109 shot->expectColor(top, Color::RED);
1110 shot->expectColor(bottom, Color::BLACK);
1111 }
1112}
1113
Marissa Wall61c58622018-07-18 10:12:20 -07001114TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1115 const Rect top(0, 0, 32, 16);
1116 const Rect bottom(0, 16, 32, 32);
1117 sp<SurfaceControl> layer;
1118 ASSERT_NO_FATAL_FAILURE(
1119 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1120
1121 sp<GraphicBuffer> buffer =
1122 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1123 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1124 BufferUsage::COMPOSER_OVERLAY,
1125 "test");
1126
1127 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1128 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1129 Transaction()
1130 .setTransparentRegionHint(layer, Region(top))
1131 .setBuffer(layer, buffer)
1132 .setSize(layer, 32, 32)
1133 .apply();
1134 {
1135 SCOPED_TRACE("top transparent");
1136 auto shot = screenshot();
1137 shot->expectColor(top, Color::BLACK);
1138 shot->expectColor(bottom, Color::RED);
1139 }
1140
1141 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1142 {
1143 SCOPED_TRACE("transparent region hint intermediate");
1144 auto shot = screenshot();
1145 shot->expectColor(top, Color::BLACK);
1146 shot->expectColor(bottom, Color::BLACK);
1147 }
1148
1149 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1150 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1151 BufferUsage::COMPOSER_OVERLAY,
1152 "test");
1153
1154 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1155 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1156 Transaction().setBuffer(layer, buffer).setSize(layer, 32, 32).apply();
1157 {
1158 SCOPED_TRACE("bottom transparent");
1159 auto shot = screenshot();
1160 shot->expectColor(top, Color::RED);
1161 shot->expectColor(bottom, Color::BLACK);
1162 }
1163}
1164
1165TEST_P(LayerTypeTransactionTest, SetTransparentRegionHintOutOfBounds) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001166 sp<SurfaceControl> layerTransparent;
1167 sp<SurfaceControl> layerR;
1168 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1169 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1170
1171 // check that transparent region hint is bound by the layer size
1172 Transaction()
1173 .setTransparentRegionHint(layerTransparent,
1174 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1175 .setPosition(layerR, 16, 16)
1176 .setLayer(layerR, mLayerZBase + 1)
1177 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001178 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1179 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001180 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1181}
1182
Marissa Wall61c58622018-07-18 10:12:20 -07001183TEST_P(LayerTypeTransactionTest, SetAlphaBasic) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001184 sp<SurfaceControl> layer1;
1185 sp<SurfaceControl> layer2;
1186 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1187 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001188 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}, 32, 32));
1189 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001190
1191 Transaction()
1192 .setAlpha(layer1, 0.25f)
1193 .setAlpha(layer2, 0.75f)
1194 .setPosition(layer2, 16, 0)
1195 .setLayer(layer2, mLayerZBase + 1)
1196 .apply();
1197 {
1198 auto shot = screenshot();
1199 uint8_t r = 16; // 64 * 0.25f
1200 uint8_t g = 48; // 64 * 0.75f
1201 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1202 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1203
1204 r /= 4; // r * (1.0f - 0.75f)
1205 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1206 }
1207}
1208
Marissa Wall61c58622018-07-18 10:12:20 -07001209TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001210 const Color color = {64, 0, 0, 255};
1211 sp<SurfaceControl> layer;
1212 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001213 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001214
1215 Transaction().setAlpha(layer, 2.0f).apply();
1216 {
1217 SCOPED_TRACE("clamped to 1.0f");
1218 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1219 }
1220
1221 Transaction().setAlpha(layer, -1.0f).apply();
1222 {
1223 SCOPED_TRACE("clamped to 0.0f");
1224 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1225 }
1226}
1227
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001228TEST_P(LayerTypeTransactionTest, SetCornerRadius) {
1229 sp<SurfaceControl> layer;
1230 const uint8_t size = 64;
1231 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001232 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001233 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1234 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1235
1236 Transaction()
1237 .setCornerRadius(layer, cornerRadius)
1238 .apply();
1239 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001240 const uint8_t bottom = size - 1;
1241 const uint8_t right = size - 1;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001242 auto shot = screenshot();
1243 // Transparent corners
1244 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001245 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1246 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1247 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1248 }
1249}
1250
1251TEST_P(LayerTypeTransactionTest, SetCornerRadiusChildCrop) {
1252 sp<SurfaceControl> parent;
1253 sp<SurfaceControl> child;
1254 const uint8_t size = 64;
1255 const uint8_t testArea = 4;
1256 const float cornerRadius = 20.0f;
1257 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1258 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1259 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1260 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1261
1262 Transaction()
1263 .setCornerRadius(parent, cornerRadius)
1264 .reparent(child, parent->getHandle())
1265 .setPosition(child, 0, size / 2)
1266 .apply();
1267 {
1268 const uint8_t bottom = size - 1;
1269 const uint8_t right = size - 1;
1270 auto shot = screenshot();
1271 // Top edge of child should not have rounded corners because it's translated in the parent
1272 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1273 Color::GREEN);
1274 // But bottom edges should have been clipped according to parent bounds
1275 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1276 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001277 }
1278}
1279
Chia-I Wue4ef6102017-11-01 15:16:35 -07001280TEST_F(LayerTransactionTest, SetColorBasic) {
1281 sp<SurfaceControl> bufferLayer;
1282 sp<SurfaceControl> colorLayer;
1283 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001284 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001285 ASSERT_NO_FATAL_FAILURE(colorLayer =
1286 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1287 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001288
Vishnu Nair88a11f22018-11-28 18:30:57 -08001289 Transaction()
1290 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1291 .setLayer(colorLayer, mLayerZBase + 1)
1292 .apply();
1293
Chia-I Wue4ef6102017-11-01 15:16:35 -07001294 {
1295 SCOPED_TRACE("default color");
1296 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1297 }
1298
1299 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1300 const Color expected = {15, 51, 85, 255};
1301 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1302 // channel) should be less than one
1303 const uint8_t tolerance = 1;
1304 Transaction().setColor(colorLayer, color).apply();
1305 {
1306 SCOPED_TRACE("new color");
1307 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1308 }
1309}
1310
1311TEST_F(LayerTransactionTest, SetColorClamped) {
1312 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001313 ASSERT_NO_FATAL_FAILURE(colorLayer =
1314 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1315 ISurfaceComposerClient::eFXSurfaceColor));
1316 Transaction()
1317 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1318 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1319 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001320
Chia-I Wue4ef6102017-11-01 15:16:35 -07001321 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1322}
1323
1324TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1325 sp<SurfaceControl> bufferLayer;
1326 sp<SurfaceControl> colorLayer;
1327 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001328 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001329 ASSERT_NO_FATAL_FAILURE(colorLayer =
1330 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1331 ISurfaceComposerClient::eFXSurfaceColor));
1332 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001333
1334 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1335 const float alpha = 0.25f;
1336 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1337 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1338 // channel) should be less than one
1339 const uint8_t tolerance = 1;
1340 Transaction()
1341 .setColor(colorLayer, color)
1342 .setAlpha(colorLayer, alpha)
1343 .setLayer(colorLayer, mLayerZBase + 1)
1344 .apply();
1345 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1346 tolerance);
1347}
1348
Adrian Roosb7a96502018-04-08 11:38:55 -07001349TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1350 sp<SurfaceControl> bufferLayer;
1351 sp<SurfaceControl> parentLayer;
1352 sp<SurfaceControl> colorLayer;
1353 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1354 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001355 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001356 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1357 0 /* buffer height */,
1358 ISurfaceComposerClient::eFXSurfaceColor));
1359 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001360 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1361 const float alpha = 0.25f;
1362 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1363 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1364 // channel) should be less than one
1365 const uint8_t tolerance = 1;
1366 Transaction()
1367 .reparent(colorLayer, parentLayer->getHandle())
1368 .setColor(colorLayer, color)
1369 .setAlpha(parentLayer, alpha)
1370 .setLayer(parentLayer, mLayerZBase + 1)
1371 .apply();
1372 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1373 tolerance);
1374}
1375
Marissa Wall61c58622018-07-18 10:12:20 -07001376TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001377 sp<SurfaceControl> bufferLayer;
1378 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001379 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001380
1381 // color is ignored
1382 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1383 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1384}
1385
Marissa Wall61c58622018-07-18 10:12:20 -07001386TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001387 sp<SurfaceControl> layer;
1388 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001389 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001390
1391 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1392 {
1393 SCOPED_TRACE("non-existing layer stack");
1394 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1395 }
1396
1397 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1398 {
1399 SCOPED_TRACE("original layer stack");
1400 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1401 }
1402}
1403
Marissa Wall61c58622018-07-18 10:12:20 -07001404TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001405 sp<SurfaceControl> layer;
1406 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1407 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001408 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001409
1410 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1411 {
1412 SCOPED_TRACE("IDENTITY");
1413 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1414 Color::WHITE);
1415 }
1416
1417 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1418 {
1419 SCOPED_TRACE("FLIP_H");
1420 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1421 Color::BLUE);
1422 }
1423
1424 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1425 {
1426 SCOPED_TRACE("FLIP_V");
1427 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1428 Color::GREEN);
1429 }
1430
1431 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1432 {
1433 SCOPED_TRACE("ROT_90");
1434 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1435 Color::GREEN);
1436 }
1437
1438 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1439 {
1440 SCOPED_TRACE("SCALE");
1441 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1442 Color::WHITE, true /* filtered */);
1443 }
1444}
1445
Marissa Wall61c58622018-07-18 10:12:20 -07001446TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001447 sp<SurfaceControl> layer;
1448 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1449 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001450 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001451
1452 const float rot = M_SQRT1_2; // 45 degrees
1453 const float trans = M_SQRT2 * 16.0f;
1454 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1455
1456 auto shot = screenshot();
1457 // check a 8x8 region inside each color
1458 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1459 const int32_t halfL = 4;
1460 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1461 };
1462 const int32_t unit = int32_t(trans / 2);
1463 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1464 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1465 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1466 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1467}
1468
Marissa Wall61c58622018-07-18 10:12:20 -07001469void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001470 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001471 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1472 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001473
1474 // setMatrix is applied after any pending resize, unlike setPosition
1475 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1476 {
1477 SCOPED_TRACE("resize pending");
1478 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001479 Rect rect;
1480 switch (layerType) {
1481 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1482 rect = {0, 0, 32, 32};
1483 break;
1484 case ISurfaceComposerClient::eFXSurfaceBufferState:
1485 rect = {0, 0, 128, 128};
1486 break;
1487 default:
1488 ASSERT_FALSE(true) << "Unsupported layer type";
1489 }
1490 shot->expectColor(rect, Color::RED);
1491 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001492 }
1493
Marissa Wall61c58622018-07-18 10:12:20 -07001494 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001495 {
1496 SCOPED_TRACE("resize applied");
1497 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1498 }
1499}
1500
Marissa Wall61c58622018-07-18 10:12:20 -07001501TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1502 ASSERT_NO_FATAL_FAILURE(
1503 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1504}
1505
1506TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1507 ASSERT_NO_FATAL_FAILURE(
1508 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1509}
1510
1511TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001512 sp<SurfaceControl> layer;
1513 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001514 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001515
1516 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1517 Transaction()
1518 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1519 .setSize(layer, 64, 64)
1520 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1521 .apply();
1522 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1523}
1524
Marissa Wall61c58622018-07-18 10:12:20 -07001525TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001526 sp<SurfaceControl> layer;
1527 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1528 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001529 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001530
1531 // XXX SCALE_CROP is not respected; calling setSize and
1532 // setOverrideScalingMode in separate transactions does not work
1533 // (b/69315456)
1534 Transaction()
1535 .setSize(layer, 64, 16)
1536 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1537 .apply();
1538 {
1539 SCOPED_TRACE("SCALE_TO_WINDOW");
1540 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1541 Color::WHITE, true /* filtered */);
1542 }
1543}
1544
Dan Stoza000dd012018-08-01 13:31:52 -07001545TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1546 sp<SurfaceControl> layer;
1547 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1548
1549 sp<IBinder> handle = layer->getHandle();
1550 ASSERT_TRUE(handle != nullptr);
1551
1552 FrameStats frameStats;
1553 mClient->getLayerFrameStats(handle, &frameStats);
1554
1555 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1556}
1557
Marissa Wall61c58622018-07-18 10:12:20 -07001558TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001559 sp<SurfaceControl> layer;
1560 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001561 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001562 const Rect crop(8, 8, 24, 24);
1563
Marissa Wallf58c14b2018-07-24 10:50:43 -07001564 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001565 auto shot = screenshot();
1566 shot->expectColor(crop, Color::RED);
1567 shot->expectBorder(crop, Color::BLACK);
1568}
1569
Marissa Wall61c58622018-07-18 10:12:20 -07001570TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1571 sp<SurfaceControl> layer;
1572 ASSERT_NO_FATAL_FAILURE(
1573 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1574 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1575 const Rect crop(8, 8, 24, 24);
1576
1577 Transaction().setCrop(layer, crop).apply();
1578 auto shot = screenshot();
1579 shot->expectColor(crop, Color::RED);
1580 shot->expectBorder(crop, Color::BLACK);
1581}
1582
1583TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001584 sp<SurfaceControl> layer;
1585 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001586 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001587
1588 {
1589 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001590 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001591 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1592 }
1593
1594 {
1595 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001596 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001597 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1598 }
1599}
1600
Marissa Wall61c58622018-07-18 10:12:20 -07001601TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1602 sp<SurfaceControl> layer;
1603 ASSERT_NO_FATAL_FAILURE(
1604 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1605 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1606
1607 {
1608 SCOPED_TRACE("empty rect");
1609 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1610 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1611 }
1612
1613 {
1614 SCOPED_TRACE("negative rect");
1615 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1616 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1617 }
1618}
1619
1620TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001621 sp<SurfaceControl> layer;
1622 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001623 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001624
Marissa Wallf58c14b2018-07-24 10:50:43 -07001625 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001626 auto shot = screenshot();
1627 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1628 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1629}
1630
Marissa Wall61c58622018-07-18 10:12:20 -07001631TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1632 sp<SurfaceControl> layer;
1633 ASSERT_NO_FATAL_FAILURE(
1634 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1635 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1636
1637 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1638 auto shot = screenshot();
1639 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1640 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1641}
1642
1643TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001644 sp<SurfaceControl> layer;
1645 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001646 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001647
1648 const Point position(32, 32);
1649 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001650 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001651 auto shot = screenshot();
1652 shot->expectColor(crop + position, Color::RED);
1653 shot->expectBorder(crop + position, Color::BLACK);
1654}
1655
Marissa Wall61c58622018-07-18 10:12:20 -07001656TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1657 sp<SurfaceControl> layer;
1658 ASSERT_NO_FATAL_FAILURE(
1659 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1660 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1661
1662 const Point position(32, 32);
1663 const Rect crop(8, 8, 24, 24);
1664 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1665 auto shot = screenshot();
1666 shot->expectColor(crop + position, Color::RED);
1667 shot->expectBorder(crop + position, Color::BLACK);
1668}
1669
1670TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001671 sp<SurfaceControl> layer;
1672 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001673 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001674
1675 // crop is affected by matrix
1676 Transaction()
1677 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001678 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001679 .apply();
1680 auto shot = screenshot();
1681 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1682 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1683}
1684
Marissa Wall61c58622018-07-18 10:12:20 -07001685TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1686 sp<SurfaceControl> layer;
1687 ASSERT_NO_FATAL_FAILURE(
1688 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1689 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1690
1691 // crop is affected by matrix
1692 Transaction()
1693 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1694 .setCrop(layer, Rect(8, 8, 24, 24))
1695 .apply();
1696 auto shot = screenshot();
1697 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1698 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1699}
1700
1701TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001702 sp<SurfaceControl> layer;
1703 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001704 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001705
Marissa Wallf58c14b2018-07-24 10:50:43 -07001706 // setCrop_legacy is applied immediately by default, with or without resize pending
1707 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001708 {
1709 SCOPED_TRACE("resize pending");
1710 auto shot = screenshot();
1711 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1712 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1713 }
1714
Marissa Wall61c58622018-07-18 10:12:20 -07001715 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001716 {
1717 SCOPED_TRACE("resize applied");
1718 auto shot = screenshot();
1719 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1720 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1721 }
1722}
1723
Marissa Wall61c58622018-07-18 10:12:20 -07001724TEST_F(LayerTransactionTest, SetCropWithResize_BufferState) {
1725 sp<SurfaceControl> layer;
1726 ASSERT_NO_FATAL_FAILURE(
1727 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1728 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1729
1730 // setCrop_legacy is applied immediately by default, with or without resize pending
1731 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1732 {
1733 SCOPED_TRACE("new buffer pending");
1734 auto shot = screenshot();
1735 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1736 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1737 }
1738
1739 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1740 {
1741 SCOPED_TRACE("new buffer");
1742 auto shot = screenshot();
1743 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1744 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1745 }
1746}
1747
1748TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001749 sp<SurfaceControl> layer;
1750 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001751 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001752
Marissa Wallf58c14b2018-07-24 10:50:43 -07001753 // request setCrop_legacy to be applied with the next resize
1754 Transaction()
1755 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1756 .setGeometryAppliesWithResize(layer)
1757 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001758 {
1759 SCOPED_TRACE("waiting for next resize");
1760 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1761 }
1762
Marissa Wallf58c14b2018-07-24 10:50:43 -07001763 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001764 {
1765 SCOPED_TRACE("pending crop modified");
1766 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1767 }
1768
1769 Transaction().setSize(layer, 16, 16).apply();
1770 {
1771 SCOPED_TRACE("resize pending");
1772 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1773 }
1774
1775 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001776 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001777 {
1778 SCOPED_TRACE("new crop applied");
1779 auto shot = screenshot();
1780 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1781 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1782 }
1783}
1784
Marissa Wall61c58622018-07-18 10:12:20 -07001785TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1786 sp<SurfaceControl> layer;
1787 ASSERT_NO_FATAL_FAILURE(
1788 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1789 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1790
1791 // request setCrop_legacy to be applied with the next resize
1792 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1793 {
1794 SCOPED_TRACE("set crop 1");
1795 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1796 }
1797
1798 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1799 {
1800 SCOPED_TRACE("set crop 2");
1801 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1802 }
1803
1804 Transaction().setSize(layer, 16, 16).apply();
1805 {
1806 SCOPED_TRACE("resize");
1807 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1808 }
1809
1810 // finally resize
1811 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1812 {
1813 SCOPED_TRACE("new buffer");
1814 auto shot = screenshot();
1815 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1816 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1817 }
1818}
1819
1820TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001821 sp<SurfaceControl> layer;
1822 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001823 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001824
Marissa Wallf58c14b2018-07-24 10:50:43 -07001825 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001826 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001827 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001828 .setSize(layer, 16, 16)
1829 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1830 .setGeometryAppliesWithResize(layer)
1831 .apply();
1832 {
1833 SCOPED_TRACE("new crop pending");
1834 auto shot = screenshot();
1835 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1836 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1837 }
1838
1839 // XXX crop is never latched without other geometry change (b/69315677)
1840 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001841 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001842 Transaction().setPosition(layer, 0, 0).apply();
1843 {
1844 SCOPED_TRACE("new crop applied");
1845 auto shot = screenshot();
1846 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1847 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1848 }
1849}
1850
Marissa Wall61c58622018-07-18 10:12:20 -07001851TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1852 sp<SurfaceControl> layer;
1853 ASSERT_NO_FATAL_FAILURE(
1854 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1855 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1856
1857 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1858 Transaction()
1859 .setCrop(layer, Rect(4, 4, 12, 12))
1860 .setSize(layer, 16, 16)
1861 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1862 .setGeometryAppliesWithResize(layer)
1863 .apply();
1864 {
1865 SCOPED_TRACE("new crop pending");
1866 auto shot = screenshot();
1867 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1868 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1869 }
1870
1871 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1872 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1873 Transaction().setPosition(layer, 0, 0).apply();
1874 {
1875 SCOPED_TRACE("new crop applied");
1876 auto shot = screenshot();
1877 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1878 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1879 }
1880}
1881
Marissa Wall61c58622018-07-18 10:12:20 -07001882TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1883 sp<SurfaceControl> layer;
1884 ASSERT_NO_FATAL_FAILURE(
1885 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1886
1887 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1888
1889 auto shot = screenshot();
1890 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1891 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1892}
1893
1894TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1895 sp<SurfaceControl> layer;
1896 ASSERT_NO_FATAL_FAILURE(
1897 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1898
1899 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1900
1901 {
1902 SCOPED_TRACE("set buffer 1");
1903 auto shot = screenshot();
1904 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1905 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1906 }
1907
1908 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1909
1910 {
1911 SCOPED_TRACE("set buffer 2");
1912 auto shot = screenshot();
1913 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1914 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1915 }
1916
1917 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1918
1919 {
1920 SCOPED_TRACE("set buffer 3");
1921 auto shot = screenshot();
1922 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1923 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1924 }
1925}
1926
1927TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1928 sp<SurfaceControl> layer1;
1929 ASSERT_NO_FATAL_FAILURE(
1930 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1931
1932 sp<SurfaceControl> layer2;
1933 ASSERT_NO_FATAL_FAILURE(
1934 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1935
1936 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1937
1938 {
1939 SCOPED_TRACE("set layer 1 buffer red");
1940 auto shot = screenshot();
1941 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1942 }
1943
1944 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1945
1946 {
1947 SCOPED_TRACE("set layer 2 buffer blue");
1948 auto shot = screenshot();
1949 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1950 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1951 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1952 }
1953
1954 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1955 {
1956 SCOPED_TRACE("set layer 1 buffer green");
1957 auto shot = screenshot();
1958 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1959 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1960 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1961 }
1962
1963 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1964
1965 {
1966 SCOPED_TRACE("set layer 2 buffer white");
1967 auto shot = screenshot();
1968 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1969 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1970 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1971 }
1972}
1973
1974TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1975 sp<SurfaceControl> layer;
1976 ASSERT_NO_FATAL_FAILURE(
1977 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1978
1979 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1980 Color::BLUE, Color::WHITE));
1981
1982 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1983
1984 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1985 Color::GREEN, true /* filtered */);
1986}
1987
1988TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1989 sp<SurfaceControl> layer;
1990 ASSERT_NO_FATAL_FAILURE(
1991 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1992
1993 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1994 Color::BLUE, Color::WHITE));
1995
1996 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1997
1998 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1999 Color::BLUE, true /* filtered */);
2000}
2001
2002TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
2003 sp<SurfaceControl> layer;
2004 ASSERT_NO_FATAL_FAILURE(
2005 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2006
2007 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2008 Color::BLUE, Color::WHITE));
2009
2010 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
2011
2012 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2013 Color::GREEN, true /* filtered */);
2014}
2015
2016TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2017 sp<SurfaceControl> layer;
2018 ASSERT_NO_FATAL_FAILURE(
2019 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2020
2021 Transaction().setTransformToDisplayInverse(layer, false).apply();
2022
2023 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2024
2025 Transaction().setTransformToDisplayInverse(layer, true).apply();
2026}
2027
2028TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
2029 sp<SurfaceControl> layer;
2030 ASSERT_NO_FATAL_FAILURE(
2031 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2032
2033 sp<GraphicBuffer> buffer =
2034 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2035 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2036 BufferUsage::COMPOSER_OVERLAY,
2037 "test");
2038 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2039
Marissa Wallfda30bb2018-10-12 11:34:28 -07002040 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002041
2042 Transaction()
2043 .setBuffer(layer, buffer)
2044 .setAcquireFence(layer, fence)
2045 .setSize(layer, 32, 32)
2046 .apply();
2047
2048 auto shot = screenshot();
2049 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2050 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2051}
2052
2053TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2054 sp<SurfaceControl> layer;
2055 ASSERT_NO_FATAL_FAILURE(
2056 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2057
2058 sp<GraphicBuffer> buffer =
2059 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2060 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2061 BufferUsage::COMPOSER_OVERLAY,
2062 "test");
2063 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2064
2065 Transaction()
2066 .setBuffer(layer, buffer)
2067 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2068 .setSize(layer, 32, 32)
2069 .apply();
2070
2071 auto shot = screenshot();
2072 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2073 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2074}
2075
2076TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2077 sp<SurfaceControl> layer;
2078 ASSERT_NO_FATAL_FAILURE(
2079 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2080
2081 sp<GraphicBuffer> buffer =
2082 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2083 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2084 BufferUsage::COMPOSER_OVERLAY,
2085 "test");
2086 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2087
2088 HdrMetadata hdrMetadata;
2089 hdrMetadata.validTypes = 0;
2090 Transaction()
2091 .setBuffer(layer, buffer)
2092 .setHdrMetadata(layer, hdrMetadata)
2093 .setSize(layer, 32, 32)
2094 .apply();
2095
2096 auto shot = screenshot();
2097 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2098 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2099}
2100
2101TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2102 sp<SurfaceControl> layer;
2103 ASSERT_NO_FATAL_FAILURE(
2104 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2105
2106 sp<GraphicBuffer> buffer =
2107 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2108 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2109 BufferUsage::COMPOSER_OVERLAY,
2110 "test");
2111 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2112
2113 Region region;
2114 region.set(32, 32);
2115 Transaction()
2116 .setBuffer(layer, buffer)
2117 .setSurfaceDamageRegion(layer, region)
2118 .setSize(layer, 32, 32)
2119 .apply();
2120
2121 auto shot = screenshot();
2122 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2123 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2124}
2125
2126TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2127 sp<SurfaceControl> layer;
2128 ASSERT_NO_FATAL_FAILURE(
2129 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2130
2131 sp<GraphicBuffer> buffer =
2132 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2133 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2134 BufferUsage::COMPOSER_OVERLAY,
2135 "test");
2136 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2137
2138 Transaction()
2139 .setBuffer(layer, buffer)
2140 .setApi(layer, NATIVE_WINDOW_API_CPU)
2141 .setSize(layer, 32, 32)
2142 .apply();
2143
2144 auto shot = screenshot();
2145 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2146 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2147}
2148
2149TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2150 sp<SurfaceControl> layer;
2151 ASSERT_NO_FATAL_FAILURE(
2152 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2153
2154 // verify this doesn't cause a crash
2155 Transaction().setSidebandStream(layer, nullptr).apply();
2156}
2157
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002158class ColorTransformHelper {
2159public:
2160 static void DegammaColorSingle(half& s) {
2161 if (s <= 0.03928f)
2162 s = s / 12.92f;
2163 else
2164 s = pow((s + 0.055f) / 1.055f, 2.4f);
2165 }
2166
2167 static void DegammaColor(half3& color) {
2168 DegammaColorSingle(color.r);
2169 DegammaColorSingle(color.g);
2170 DegammaColorSingle(color.b);
2171 }
2172
2173 static void GammaColorSingle(half& s) {
2174 if (s <= 0.0031308f) {
2175 s = s * 12.92f;
2176 } else {
2177 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2178 }
2179 }
2180
2181 static void GammaColor(half3& color) {
2182 GammaColorSingle(color.r);
2183 GammaColorSingle(color.g);
2184 GammaColorSingle(color.b);
2185 }
2186
2187 static void applyMatrix(half3& color, const mat3& mat) {
2188 half3 ret = half3(0);
2189
2190 for (int i = 0; i < 3; i++) {
2191 for (int j = 0; j < 3; j++) {
2192 ret[i] = ret[i] + color[j] * mat[j][i];
2193 }
2194 }
2195 color = ret;
2196 }
2197};
2198
Peiyong Lind3788632018-09-18 16:01:31 -07002199TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2200 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002201 ASSERT_NO_FATAL_FAILURE(colorLayer =
2202 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2203 ISurfaceComposerClient::eFXSurfaceColor));
2204 Transaction()
2205 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2206 .setLayer(colorLayer, mLayerZBase + 1)
2207 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002208 {
2209 SCOPED_TRACE("default color");
2210 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2211 }
2212
2213 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002214 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002215 mat3 matrix;
2216 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2217 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2218 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002219
2220 // degamma before applying the matrix
2221 if (mColorManagementUsed) {
2222 ColorTransformHelper::DegammaColor(expected);
2223 }
2224
2225 ColorTransformHelper::applyMatrix(expected, matrix);
2226
2227 if (mColorManagementUsed) {
2228 ColorTransformHelper::GammaColor(expected);
2229 }
2230
2231 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2232 uint8_t(expected.b * 255), 255};
2233
2234 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2235 // channel) should be less than one
2236 const uint8_t tolerance = 1;
2237
Peiyong Lind3788632018-09-18 16:01:31 -07002238 Transaction().setColor(colorLayer, color)
2239 .setColorTransform(colorLayer, matrix, vec3()).apply();
2240 {
2241 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002242 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002243 }
2244}
2245
Marissa Wallfda30bb2018-10-12 11:34:28 -07002246class ExpectedResult {
2247public:
2248 enum Transaction {
2249 NOT_PRESENTED = 0,
2250 PRESENTED,
2251 };
2252
2253 enum Buffer {
2254 NOT_ACQUIRED = 0,
2255 ACQUIRED,
2256 };
2257
2258 enum PreviousBuffer {
2259 NOT_RELEASED = 0,
2260 RELEASED,
2261 };
2262
2263 void reset() {
2264 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2265 mExpectedSurfaceResults.clear();
2266 }
2267
2268 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
2269 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2270 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2271 mTransactionResult = transactionResult;
2272 mExpectedSurfaceResults.emplace(std::piecewise_construct,
2273 std::forward_as_tuple(layer->getHandle()),
2274 std::forward_as_tuple(bufferResult, previousBufferResult));
2275 }
2276
2277 void addSurfaces(ExpectedResult::Transaction transactionResult,
2278 const std::vector<sp<SurfaceControl>>& layers,
2279 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2280 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2281 for (const auto& layer : layers) {
2282 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2283 }
2284 }
2285
2286 void verifyTransactionStats(const TransactionStats& transactionStats) const {
2287 const auto& [latchTime, presentTime, surfaceStats] = transactionStats;
2288 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2289 ASSERT_GE(latchTime, 0) << "bad latch time";
2290 ASSERT_GE(presentTime, 0) << "bad present time";
2291 } else {
2292 ASSERT_EQ(presentTime, -1) << "transaction shouldn't have been presented";
2293 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2294 }
2295
2296 ASSERT_EQ(surfaceStats.size(), mExpectedSurfaceResults.size())
2297 << "wrong number of surfaces";
2298
2299 for (const auto& stats : surfaceStats) {
2300 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2301 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2302 << "unexpected surface control";
2303 expectedSurfaceResult->second.verifySurfaceStats(stats, latchTime);
2304 }
2305 }
2306
2307private:
2308 class ExpectedSurfaceResult {
2309 public:
2310 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2311 ExpectedResult::PreviousBuffer previousBufferResult)
2312 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2313
2314 void verifySurfaceStats(const SurfaceStats& surfaceStats, nsecs_t latchTime) const {
2315 const auto& [surfaceControl, acquireTime, releasePreviousBuffer] = surfaceStats;
2316
2317 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2318 << "bad acquire time";
2319 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
2320 ASSERT_EQ(releasePreviousBuffer,
2321 mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED)
2322 << "bad previous buffer released";
2323 }
2324
2325 private:
2326 ExpectedResult::Buffer mBufferResult;
2327 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2328 };
2329
2330 struct IBinderHash {
2331 std::size_t operator()(const sp<IBinder>& strongPointer) const {
2332 return std::hash<IBinder*>{}(strongPointer.get());
2333 }
2334 };
2335 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2336 std::unordered_map<sp<IBinder>, ExpectedSurfaceResult, IBinderHash> mExpectedSurfaceResults;
2337};
2338
2339class CallbackHelper {
2340public:
2341 static void function(void* callbackContext, const TransactionStats& transactionStats) {
2342 if (!callbackContext) {
2343 ALOGE("failed to get callback context");
2344 }
2345 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2346 std::lock_guard lock(helper->mMutex);
2347 helper->mTransactionStatsQueue.push(transactionStats);
2348 helper->mConditionVariable.notify_all();
2349 }
2350
2351 void getTransactionStats(TransactionStats* outStats) {
2352 std::unique_lock lock(mMutex);
2353
2354 if (mTransactionStatsQueue.empty()) {
2355 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2356 std::cv_status::timeout)
2357 << "did not receive callback";
2358 }
2359
2360 *outStats = std::move(mTransactionStatsQueue.front());
2361 mTransactionStatsQueue.pop();
2362 }
2363
2364 void verifyFinalState() {
2365 // Wait to see if there are extra callbacks
2366 std::this_thread::sleep_for(500ms);
2367
2368 std::lock_guard lock(mMutex);
2369 EXPECT_EQ(mTransactionStatsQueue.size(), 0) << "extra callbacks received";
2370 mTransactionStatsQueue = {};
2371 }
2372
2373 void* getContext() { return static_cast<void*>(this); }
2374
2375 std::mutex mMutex;
2376 std::condition_variable mConditionVariable;
2377 std::queue<TransactionStats> mTransactionStatsQueue;
2378};
2379
2380class LayerCallbackTest : public LayerTransactionTest {
2381protected:
2382 virtual sp<SurfaceControl> createBufferStateLayer() {
2383 return createLayer(mClient, "test", mWidth, mHeight,
2384 ISurfaceComposerClient::eFXSurfaceBufferState);
2385 }
2386
2387 virtual void fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2388 const sp<SurfaceControl>& layer = nullptr) {
2389 if (layer) {
2390 sp<GraphicBuffer> buffer =
2391 new GraphicBuffer(mWidth, mHeight, PIXEL_FORMAT_RGBA_8888, 1,
2392 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2393 BufferUsage::COMPOSER_OVERLAY |
2394 BufferUsage::GPU_TEXTURE,
2395 "test");
2396 fillGraphicBufferColor(buffer, Rect(0, 0, mWidth, mHeight), Color::RED);
2397
2398 sp<Fence> fence = new Fence(-1);
2399
2400 transaction.setBuffer(layer, buffer)
2401 .setAcquireFence(layer, fence)
2402 .setSize(layer, mWidth, mHeight);
2403 }
2404
2405 transaction.addTransactionCompletedCallback(callbackHelper->function,
2406 callbackHelper->getContext());
2407 }
2408
2409 void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2410 bool finalState = false) {
2411 TransactionStats transactionStats;
2412 ASSERT_NO_FATAL_FAILURE(helper.getTransactionStats(&transactionStats));
2413 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyTransactionStats(transactionStats));
2414
2415 if (finalState) {
2416 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2417 }
2418 }
2419
2420 void waitForCallbacks(CallbackHelper& helper,
2421 const std::vector<ExpectedResult>& expectedResults,
2422 bool finalState = false) {
2423 for (const auto& expectedResult : expectedResults) {
2424 waitForCallback(helper, expectedResult);
2425 }
2426 if (finalState) {
2427 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2428 }
2429 }
2430
2431 uint32_t mWidth = 32;
2432 uint32_t mHeight = 32;
2433};
2434
2435TEST_F(LayerCallbackTest, Basic) {
2436 sp<SurfaceControl> layer;
2437 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2438
2439 Transaction transaction;
2440 CallbackHelper callback;
2441 fillTransaction(transaction, &callback, layer);
2442
2443 transaction.apply();
2444
2445 ExpectedResult expected;
2446 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2447 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2448}
2449
2450TEST_F(LayerCallbackTest, NoBuffer) {
2451 sp<SurfaceControl> layer;
2452 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2453
2454 Transaction transaction;
2455 CallbackHelper callback;
2456 fillTransaction(transaction, &callback);
2457
2458 transaction.setPosition(layer, mWidth, mHeight).apply();
2459
2460 ExpectedResult expected;
2461 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2462 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2463}
2464
2465TEST_F(LayerCallbackTest, NoStateChange) {
2466 Transaction transaction;
2467 CallbackHelper callback;
2468 fillTransaction(transaction, &callback);
2469
2470 transaction.apply();
2471
2472 ExpectedResult expected;
2473 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2474}
2475
2476TEST_F(LayerCallbackTest, OffScreen) {
2477 sp<SurfaceControl> layer;
2478 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2479
2480 Transaction transaction;
2481 CallbackHelper callback;
2482 fillTransaction(transaction, &callback, layer);
2483
2484 transaction.setPosition(layer, -100, -100).apply();
2485
2486 ExpectedResult expected;
2487 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2488 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2489}
2490
2491TEST_F(LayerCallbackTest, Merge) {
2492 sp<SurfaceControl> layer1, layer2;
2493 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2494 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2495
2496 Transaction transaction1, transaction2;
2497 CallbackHelper callback1, callback2;
2498 fillTransaction(transaction1, &callback1, layer1);
2499 fillTransaction(transaction2, &callback2, layer2);
2500
2501 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2502
2503 ExpectedResult expected;
2504 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2505 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2506 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2507}
2508
2509TEST_F(LayerCallbackTest, Merge_SameCallback) {
2510 sp<SurfaceControl> layer1, layer2;
2511 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2512 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2513
2514 Transaction transaction1, transaction2;
2515 CallbackHelper callback;
2516 fillTransaction(transaction1, &callback, layer1);
2517 fillTransaction(transaction2, &callback, layer2);
2518
2519 transaction2.merge(std::move(transaction1)).apply();
2520
2521 ExpectedResult expected;
2522 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2523 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2524 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2525}
2526
2527TEST_F(LayerCallbackTest, Merge_SameLayer) {
2528 sp<SurfaceControl> layer;
2529 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2530
2531 Transaction transaction1, transaction2;
2532 CallbackHelper callback1, callback2;
2533 fillTransaction(transaction1, &callback1, layer);
2534 fillTransaction(transaction2, &callback2, layer);
2535
2536 transaction2.merge(std::move(transaction1)).apply();
2537
2538 ExpectedResult expected;
2539 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2540 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2541 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2542}
2543
2544TEST_F(LayerCallbackTest, Merge_SingleBuffer) {
2545 sp<SurfaceControl> layer1, layer2;
2546 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2547 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2548
2549 Transaction transaction1, transaction2;
2550 CallbackHelper callback1, callback2;
2551 fillTransaction(transaction1, &callback1, layer1);
2552 fillTransaction(transaction2, &callback2);
2553
2554 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2555
2556 ExpectedResult expected;
2557 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2558 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2559 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2560}
2561
2562TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2563 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2564 client2(new SurfaceComposerClient);
2565
2566 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2567 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2568
2569 sp<SurfaceControl> layer1, layer2;
2570 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2571 ISurfaceComposerClient::eFXSurfaceBufferState));
2572 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2573 ISurfaceComposerClient::eFXSurfaceBufferState));
2574
2575 Transaction transaction1, transaction2;
2576 CallbackHelper callback1, callback2;
2577 fillTransaction(transaction1, &callback1, layer1);
2578 fillTransaction(transaction2, &callback2, layer2);
2579
2580 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2581
2582 ExpectedResult expected;
2583 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2584 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2585 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2586}
2587
2588TEST_F(LayerCallbackTest, MultipleTransactions) {
2589 sp<SurfaceControl> layer;
2590 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2591
2592 Transaction transaction;
2593 CallbackHelper callback;
2594 for (size_t i = 0; i < 10; i++) {
2595 fillTransaction(transaction, &callback, layer);
2596
2597 transaction.apply();
2598
2599 ExpectedResult expected;
2600 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2601 ExpectedResult::Buffer::NOT_ACQUIRED,
2602 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2603 : ExpectedResult::PreviousBuffer::RELEASED);
2604 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2605 }
2606 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2607}
2608
2609TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
2610 sp<SurfaceControl> layer;
2611 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2612
2613 Transaction transaction;
2614 CallbackHelper callback;
2615 for (size_t i = 0; i < 10; i++) {
2616 ExpectedResult expected;
2617
2618 if (i == 0) {
2619 fillTransaction(transaction, &callback, layer);
2620 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2621 } else {
2622 fillTransaction(transaction, &callback);
2623 }
2624
2625 transaction.apply();
2626
2627 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2628 }
2629 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2630}
2631
2632TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
2633 sp<SurfaceControl> layer;
2634 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2635
2636 Transaction transaction;
2637 CallbackHelper callback;
2638 for (size_t i = 0; i < 10; i++) {
2639 if (i == 0) {
2640 fillTransaction(transaction, &callback, layer);
2641 } else {
2642 fillTransaction(transaction, &callback);
2643 }
2644
2645 transaction.setPosition(layer, mWidth, mHeight).apply();
2646
2647 ExpectedResult expected;
2648 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
2649 : ExpectedResult::Transaction::NOT_PRESENTED,
2650 layer);
2651 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
2652 }
2653 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2654}
2655
2656TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
2657 sp<SurfaceControl> layer1, layer2;
2658 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2659 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2660
2661 Transaction transaction1, transaction2;
2662 CallbackHelper callback1, callback2;
2663 for (size_t i = 0; i < 10; i++) {
2664 fillTransaction(transaction1, &callback1, layer1);
2665 fillTransaction(transaction2, &callback2, layer2);
2666
2667 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2668
2669 ExpectedResult expected;
2670 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2671 ExpectedResult::Buffer::NOT_ACQUIRED,
2672 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2673 : ExpectedResult::PreviousBuffer::RELEASED);
2674 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2675 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2676 }
2677 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2678 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2679}
2680
2681TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
2682 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2683 client2(new SurfaceComposerClient);
2684 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2685 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2686
2687 sp<SurfaceControl> layer1, layer2;
2688 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2689 ISurfaceComposerClient::eFXSurfaceBufferState));
2690 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2691 ISurfaceComposerClient::eFXSurfaceBufferState));
2692
2693 Transaction transaction1, transaction2;
2694 CallbackHelper callback1, callback2;
2695 for (size_t i = 0; i < 10; i++) {
2696 fillTransaction(transaction1, &callback1, layer1);
2697 fillTransaction(transaction2, &callback2, layer2);
2698
2699 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2700
2701 ExpectedResult expected;
2702 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2703 ExpectedResult::Buffer::NOT_ACQUIRED,
2704 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2705 : ExpectedResult::PreviousBuffer::RELEASED);
2706 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2707 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2708 }
2709 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2710 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2711}
2712
2713TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
2714 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2715 client2(new SurfaceComposerClient);
2716 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2717 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2718
2719 sp<SurfaceControl> layer1, layer2;
2720 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2721 ISurfaceComposerClient::eFXSurfaceBufferState));
2722 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2723 ISurfaceComposerClient::eFXSurfaceBufferState));
2724
2725 Transaction transaction1, transaction2;
2726 CallbackHelper callback1, callback2;
2727
2728 // Normal call to set up test
2729 fillTransaction(transaction1, &callback1, layer1);
2730 fillTransaction(transaction2, &callback2, layer2);
2731
2732 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2733
2734 ExpectedResult expected;
2735 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2736 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2737 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2738 expected.reset();
2739
2740 // Test
2741 fillTransaction(transaction1, &callback1);
2742 fillTransaction(transaction2, &callback2);
2743
2744 transaction2.merge(std::move(transaction1)).apply();
2745
2746 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2747 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2748}
2749
2750TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
2751 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2752 client2(new SurfaceComposerClient);
2753
2754 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2755 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2756
2757 sp<SurfaceControl> layer1, layer2;
2758 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2759 ISurfaceComposerClient::eFXSurfaceBufferState));
2760 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2761 ISurfaceComposerClient::eFXSurfaceBufferState));
2762
2763 Transaction transaction1, transaction2;
2764 CallbackHelper callback1, callback2;
2765
2766 // Normal call to set up test
2767 fillTransaction(transaction1, &callback1, layer1);
2768 fillTransaction(transaction2, &callback2, layer2);
2769
2770 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2771
2772 ExpectedResult expected;
2773 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2774 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2775 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2776 expected.reset();
2777
2778 // Test
2779 fillTransaction(transaction1, &callback1);
2780 fillTransaction(transaction2, &callback2);
2781
2782 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2783
2784 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2);
2785 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2786 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2787}
2788
2789TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
2790 sp<SurfaceControl> layer;
2791 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2792
2793 Transaction transaction;
2794 CallbackHelper callback;
2795 std::vector<ExpectedResult> expectedResults(50);
2796 ExpectedResult::PreviousBuffer previousBufferResult =
2797 ExpectedResult::PreviousBuffer::NOT_RELEASED;
2798 for (auto& expected : expectedResults) {
2799 expected.reset();
2800 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2801 ExpectedResult::Buffer::NOT_ACQUIRED, previousBufferResult);
2802 previousBufferResult = ExpectedResult::PreviousBuffer::RELEASED;
2803
2804 fillTransaction(transaction, &callback, layer);
2805
2806 transaction.apply();
2807 std::this_thread::sleep_for(200ms);
2808 }
2809 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2810}
2811
2812TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
2813 sp<SurfaceControl> layer;
2814 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2815
2816 Transaction transaction;
2817 CallbackHelper callback;
2818 std::vector<ExpectedResult> expectedResults(50);
2819 bool first = true;
2820 for (auto& expected : expectedResults) {
2821 expected.reset();
2822
2823 if (first) {
2824 fillTransaction(transaction, &callback, layer);
2825 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2826 first = false;
2827 } else {
2828 fillTransaction(transaction, &callback);
2829 }
2830
2831 transaction.apply();
2832 std::this_thread::sleep_for(200ms);
2833 }
2834 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2835}
2836
2837TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
2838 sp<SurfaceControl> layer;
2839 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2840
2841 // Normal call to set up test
2842 Transaction transaction;
2843 CallbackHelper callback;
2844 fillTransaction(transaction, &callback, layer);
2845
2846 transaction.setPosition(layer, mWidth, mHeight).apply();
2847
2848 ExpectedResult expectedResult;
2849 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2850 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
2851
2852 // Test
2853 std::vector<ExpectedResult> expectedResults(50);
2854 for (auto& expected : expectedResults) {
2855 expected.reset();
2856 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2857
2858 fillTransaction(transaction, &callback);
2859
2860 transaction.setPosition(layer, mWidth, mHeight).apply();
2861
2862 std::this_thread::sleep_for(200ms);
2863 }
2864 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2865}
2866
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002867class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002868protected:
2869 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002870 LayerTransactionTest::SetUp();
2871 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002872
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002873 sp<IBinder> display(
2874 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002875 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002876 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002877
2878 ssize_t displayWidth = info.w;
2879 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002880
2881 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002882 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2883 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002884 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002885 ASSERT_TRUE(mBGSurfaceControl->isValid());
2886 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2887
2888 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002889 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2890
Peiyong Lin566a3b42018-01-09 18:22:43 -08002891 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002892 ASSERT_TRUE(mFGSurfaceControl->isValid());
2893
2894 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2895
2896 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002897 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002898 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002899 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2900
2901 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2902
Robert Carr4cdc58f2017-08-23 14:22:20 -07002903 asTransaction([&](Transaction& t) {
2904 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002905
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002906 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002907
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002908 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2909 .setPosition(mFGSurfaceControl, 64, 64)
2910 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002911
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002912 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2913 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2914 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002915 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002916 }
2917
2918 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002919 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002920 mBGSurfaceControl = 0;
2921 mFGSurfaceControl = 0;
2922 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002923 }
2924
2925 void waitForPostedBuffers() {
2926 // Since the sync surface is in synchronous mode (i.e. double buffered)
2927 // posting three buffers to it should ensure that at least two
2928 // SurfaceFlinger::handlePageFlip calls have been made, which should
2929 // guaranteed that a buffer posted to another Surface has been retired.
2930 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2931 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2932 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2933 }
2934
Robert Carr4cdc58f2017-08-23 14:22:20 -07002935 void asTransaction(const std::function<void(Transaction&)>& exec) {
2936 Transaction t;
2937 exec(t);
2938 t.apply(true);
2939 }
2940
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002941 sp<SurfaceControl> mBGSurfaceControl;
2942 sp<SurfaceControl> mFGSurfaceControl;
2943
2944 // This surface is used to ensure that the buffers posted to
2945 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2946 sp<SurfaceControl> mSyncSurfaceControl;
2947};
2948
Robert Carr7f619b22017-11-06 12:56:35 -08002949TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08002950
chaviw0e3479f2018-09-10 16:49:30 -07002951 std::unique_ptr<ScreenCapture> sc;
2952
2953 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002954 fillSurfaceRGBA8(relative, 10, 10, 10);
2955 waitForPostedBuffers();
2956
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002957 Transaction{}
2958 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002959 .setPosition(relative, 64, 64)
2960 .apply();
2961
2962 {
2963 // The relative should be on top of the FG control.
2964 ScreenCapture::captureScreen(&sc);
2965 sc->checkPixel(64, 64, 10, 10, 10);
2966 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002967 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002968
2969 {
2970 // Nothing should change at this point.
2971 ScreenCapture::captureScreen(&sc);
2972 sc->checkPixel(64, 64, 10, 10, 10);
2973 }
2974
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002975 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002976
2977 {
2978 // Ensure that the relative was actually hidden, rather than
2979 // being left in the detached but visible state.
2980 ScreenCapture::captureScreen(&sc);
2981 sc->expectFGColor(64, 64);
2982 }
2983}
2984
Robert Carr8d5227b2017-03-16 15:41:03 -07002985class GeometryLatchingTest : public LayerUpdateTest {
2986protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002987 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002988 SCOPED_TRACE(trace);
2989 ScreenCapture::captureScreen(&sc);
2990 // We find the leading edge of the FG surface.
2991 sc->expectFGColor(127, 127);
2992 sc->expectBGColor(128, 128);
2993 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002994
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002995 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002996
2997 void unlockFGBuffer() {
2998 sp<Surface> s = mFGSurfaceControl->getSurface();
2999 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3000 waitForPostedBuffers();
3001 }
3002
Robert Carr8d5227b2017-03-16 15:41:03 -07003003 void completeFGResize() {
3004 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3005 waitForPostedBuffers();
3006 }
3007 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003008 asTransaction([&](Transaction& t) {
3009 t.setSize(mFGSurfaceControl, 64, 64);
3010 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003011 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003012 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003013
3014 EXPECT_INITIAL_STATE("After restoring initial state");
3015 }
chaviw0e3479f2018-09-10 16:49:30 -07003016 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003017};
3018
Robert Carr8d5227b2017-03-16 15:41:03 -07003019class CropLatchingTest : public GeometryLatchingTest {
3020protected:
3021 void EXPECT_CROPPED_STATE(const char* trace) {
3022 SCOPED_TRACE(trace);
3023 ScreenCapture::captureScreen(&sc);
3024 // The edge should be moved back one pixel by our crop.
3025 sc->expectFGColor(126, 126);
3026 sc->expectBGColor(127, 127);
3027 sc->expectBGColor(128, 128);
3028 }
chaviw59f5c562017-06-28 16:39:06 -07003029
3030 void EXPECT_RESIZE_STATE(const char* trace) {
3031 SCOPED_TRACE(trace);
3032 ScreenCapture::captureScreen(&sc);
3033 // The FG is now resized too 128,128 at 64,64
3034 sc->expectFGColor(64, 64);
3035 sc->expectFGColor(191, 191);
3036 sc->expectBGColor(192, 192);
3037 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003038};
3039
Pablo Ceballos05289c22016-04-14 15:49:55 -07003040TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003041 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003042 {
3043 SCOPED_TRACE("before anything");
3044 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003045 sc->expectBGColor(32, 32);
3046 sc->expectFGColor(96, 96);
3047 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003048 }
3049
3050 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003051 asTransaction([&](Transaction& t) {
3052 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003053 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3054 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003055 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003056
Robert Carr4cdc58f2017-08-23 14:22:20 -07003057 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003058 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003059 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3060 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003061 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003062
3063 {
3064 SCOPED_TRACE("before any trigger");
3065 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003066 sc->expectBGColor(32, 32);
3067 sc->expectFGColor(96, 96);
3068 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003069 }
3070
3071 // should trigger the first deferred transaction, but not the second one
3072 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3073 {
3074 SCOPED_TRACE("after first trigger");
3075 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003076 sc->expectBGColor(32, 32);
3077 sc->checkPixel(96, 96, 162, 63, 96);
3078 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003079 }
3080
3081 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003082 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003083
3084 // trigger the second deferred transaction
3085 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3086 {
3087 SCOPED_TRACE("after second trigger");
3088 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003089 sc->expectBGColor(32, 32);
3090 sc->expectBGColor(96, 96);
3091 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003092 }
3093}
3094
Robert Carre392b552017-09-19 12:16:05 -07003095TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003096 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003097
3098 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003099 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3100 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3101 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3102 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003103 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003104 SurfaceComposerClient::Transaction{}
3105 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3106 .show(childNoBuffer)
3107 .show(childBuffer)
3108 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003109 {
3110 ScreenCapture::captureScreen(&sc);
3111 sc->expectChildColor(73, 73);
3112 sc->expectFGColor(74, 74);
3113 }
Vishnu Nair60356342018-11-13 13:00:45 -08003114 SurfaceComposerClient::Transaction{}
3115 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3116 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003117 {
3118 ScreenCapture::captureScreen(&sc);
3119 sc->expectChildColor(73, 73);
3120 sc->expectChildColor(74, 74);
3121 }
3122}
3123
Robert Carr2c5f6d22017-09-26 12:30:35 -07003124TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003125 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003126 {
3127 SCOPED_TRACE("before move");
3128 ScreenCapture::captureScreen(&sc);
3129 sc->expectBGColor(0, 12);
3130 sc->expectFGColor(75, 75);
3131 sc->expectBGColor(145, 145);
3132 }
3133
3134 Transaction t1, t2;
3135 t1.setPosition(mFGSurfaceControl, 128, 128);
3136 t2.setPosition(mFGSurfaceControl, 0, 0);
3137 // We expect that the position update from t2 now
3138 // overwrites the position update from t1.
3139 t1.merge(std::move(t2));
3140 t1.apply();
3141
3142 {
3143 ScreenCapture::captureScreen(&sc);
3144 sc->expectFGColor(1, 1);
3145 }
3146}
3147
Robert Carr1f0a16a2016-10-24 16:27:39 -07003148class ChildLayerTest : public LayerUpdateTest {
3149protected:
3150 void SetUp() override {
3151 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003152 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3153 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003154 fillSurfaceRGBA8(mChild, 200, 200, 200);
3155
3156 {
3157 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003158 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003159 mCapture->expectChildColor(64, 64);
3160 }
3161 }
3162 void TearDown() override {
3163 LayerUpdateTest::TearDown();
3164 mChild = 0;
3165 }
3166
3167 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003168 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003169};
3170
3171TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003172 asTransaction([&](Transaction& t) {
3173 t.show(mChild);
3174 t.setPosition(mChild, 10, 10);
3175 t.setPosition(mFGSurfaceControl, 64, 64);
3176 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003177
3178 {
chaviw0e3479f2018-09-10 16:49:30 -07003179 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003180 // Top left of foreground must now be visible
3181 mCapture->expectFGColor(64, 64);
3182 // But 10 pixels in we should see the child surface
3183 mCapture->expectChildColor(74, 74);
3184 // And 10 more pixels we should be back to the foreground surface
3185 mCapture->expectFGColor(84, 84);
3186 }
3187
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003188 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003189
3190 {
chaviw0e3479f2018-09-10 16:49:30 -07003191 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003192 // Top left of foreground should now be at 0, 0
3193 mCapture->expectFGColor(0, 0);
3194 // But 10 pixels in we should see the child surface
3195 mCapture->expectChildColor(10, 10);
3196 // And 10 more pixels we should be back to the foreground surface
3197 mCapture->expectFGColor(20, 20);
3198 }
3199}
3200
Robert Carr41b08b52017-06-01 16:11:34 -07003201TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003202 asTransaction([&](Transaction& t) {
3203 t.show(mChild);
3204 t.setPosition(mChild, 0, 0);
3205 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003206 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003207 });
Robert Carr41b08b52017-06-01 16:11:34 -07003208
3209 {
chaviw0e3479f2018-09-10 16:49:30 -07003210 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003211 mCapture->expectChildColor(0, 0);
3212 mCapture->expectChildColor(4, 4);
3213 mCapture->expectBGColor(5, 5);
3214 }
3215}
3216
Robert Carr1f0a16a2016-10-24 16:27:39 -07003217TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003218 asTransaction([&](Transaction& t) {
3219 t.show(mChild);
3220 t.setPosition(mFGSurfaceControl, 0, 0);
3221 t.setPosition(mChild, 63, 63);
3222 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003223
3224 {
chaviw0e3479f2018-09-10 16:49:30 -07003225 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003226 mCapture->expectFGColor(0, 0);
3227 // Last pixel in foreground should now be the child.
3228 mCapture->expectChildColor(63, 63);
3229 // But the child should be constrained and the next pixel
3230 // must be the background
3231 mCapture->expectBGColor(64, 64);
3232 }
3233}
3234
3235TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003236 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003237
3238 // Find the boundary between the parent and child
3239 {
chaviw0e3479f2018-09-10 16:49:30 -07003240 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003241 mCapture->expectChildColor(9, 9);
3242 mCapture->expectFGColor(10, 10);
3243 }
3244
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003245 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003246
3247 // The boundary should be twice as far from the origin now.
3248 // The pixels from the last test should all be child now
3249 {
chaviw0e3479f2018-09-10 16:49:30 -07003250 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003251 mCapture->expectChildColor(9, 9);
3252 mCapture->expectChildColor(10, 10);
3253 mCapture->expectChildColor(19, 19);
3254 mCapture->expectFGColor(20, 20);
3255 }
3256}
Robert Carr9524cb32017-02-13 11:32:32 -08003257
Robert Carr6452f122017-03-21 10:41:29 -07003258TEST_F(ChildLayerTest, ChildLayerAlpha) {
3259 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3260 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3261 fillSurfaceRGBA8(mChild, 0, 254, 0);
3262 waitForPostedBuffers();
3263
Robert Carr4cdc58f2017-08-23 14:22:20 -07003264 asTransaction([&](Transaction& t) {
3265 t.show(mChild);
3266 t.setPosition(mChild, 0, 0);
3267 t.setPosition(mFGSurfaceControl, 0, 0);
3268 });
Robert Carr6452f122017-03-21 10:41:29 -07003269
3270 {
chaviw0e3479f2018-09-10 16:49:30 -07003271 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003272 // Unblended child color
3273 mCapture->checkPixel(0, 0, 0, 254, 0);
3274 }
3275
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003276 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003277
3278 {
chaviw0e3479f2018-09-10 16:49:30 -07003279 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003280 // Child and BG blended.
3281 mCapture->checkPixel(0, 0, 127, 127, 0);
3282 }
3283
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003284 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003285
3286 {
chaviw0e3479f2018-09-10 16:49:30 -07003287 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003288 // Child and BG blended.
3289 mCapture->checkPixel(0, 0, 95, 64, 95);
3290 }
3291}
3292
Robert Carr9524cb32017-02-13 11:32:32 -08003293TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003294 asTransaction([&](Transaction& t) {
3295 t.show(mChild);
3296 t.setPosition(mChild, 10, 10);
3297 t.setPosition(mFGSurfaceControl, 64, 64);
3298 });
Robert Carr9524cb32017-02-13 11:32:32 -08003299
3300 {
chaviw0e3479f2018-09-10 16:49:30 -07003301 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003302 // Top left of foreground must now be visible
3303 mCapture->expectFGColor(64, 64);
3304 // But 10 pixels in we should see the child surface
3305 mCapture->expectChildColor(74, 74);
3306 // And 10 more pixels we should be back to the foreground surface
3307 mCapture->expectFGColor(84, 84);
3308 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003309
3310 asTransaction([&](Transaction& t) {
3311 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
3312 });
3313
Robert Carr9524cb32017-02-13 11:32:32 -08003314 {
chaviw0e3479f2018-09-10 16:49:30 -07003315 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003316 mCapture->expectFGColor(64, 64);
3317 // In reparenting we should have exposed the entire foreground surface.
3318 mCapture->expectFGColor(74, 74);
3319 // And the child layer should now begin at 10, 10 (since the BG
3320 // layer is at (0, 0)).
3321 mCapture->expectBGColor(9, 9);
3322 mCapture->expectChildColor(10, 10);
3323 }
3324}
3325
Robert Carr2e102c92018-10-23 12:11:15 -07003326TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
3327 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003328 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07003329 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
3330
3331 {
3332 SCOPED_TRACE("Grandchild visible");
3333 ScreenCapture::captureScreen(&mCapture);
3334 mCapture->checkPixel(64, 64, 111, 111, 111);
3335 }
3336
3337 mChild->clear();
3338
3339 {
3340 SCOPED_TRACE("After destroying child");
3341 ScreenCapture::captureScreen(&mCapture);
3342 mCapture->expectFGColor(64, 64);
3343 }
3344
3345 asTransaction([&](Transaction& t) {
3346 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
3347 });
3348
3349 {
3350 SCOPED_TRACE("After reparenting grandchild");
3351 ScreenCapture::captureScreen(&mCapture);
3352 mCapture->checkPixel(64, 64, 111, 111, 111);
3353 }
3354}
3355
chaviw161410b02017-07-27 10:46:08 -07003356TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003357 asTransaction([&](Transaction& t) {
3358 t.show(mChild);
3359 t.setPosition(mChild, 10, 10);
3360 t.setPosition(mFGSurfaceControl, 64, 64);
3361 });
Robert Carr9524cb32017-02-13 11:32:32 -08003362
3363 {
chaviw0e3479f2018-09-10 16:49:30 -07003364 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003365 // Top left of foreground must now be visible
3366 mCapture->expectFGColor(64, 64);
3367 // But 10 pixels in we should see the child surface
3368 mCapture->expectChildColor(74, 74);
3369 // And 10 more pixels we should be back to the foreground surface
3370 mCapture->expectFGColor(84, 84);
3371 }
3372
chaviw0e3479f2018-09-10 16:49:30 -07003373
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003374 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08003375
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003376 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08003377
chaviw161410b02017-07-27 10:46:08 -07003378 // Since the child has the same client as the parent, it will not get
3379 // detached and will be hidden.
3380 {
chaviw0e3479f2018-09-10 16:49:30 -07003381 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003382 mCapture->expectFGColor(64, 64);
3383 mCapture->expectFGColor(74, 74);
3384 mCapture->expectFGColor(84, 84);
3385 }
3386}
3387
3388TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
3389 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003390 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003391 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
3392 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07003393
chaviw161410b02017-07-27 10:46:08 -07003394 ASSERT_TRUE(mChildNewClient->isValid());
3395
3396 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
3397
Robert Carr4cdc58f2017-08-23 14:22:20 -07003398 asTransaction([&](Transaction& t) {
3399 t.hide(mChild);
3400 t.show(mChildNewClient);
3401 t.setPosition(mChildNewClient, 10, 10);
3402 t.setPosition(mFGSurfaceControl, 64, 64);
3403 });
chaviw161410b02017-07-27 10:46:08 -07003404
3405 {
chaviw0e3479f2018-09-10 16:49:30 -07003406 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003407 // Top left of foreground must now be visible
3408 mCapture->expectFGColor(64, 64);
3409 // But 10 pixels in we should see the child surface
3410 mCapture->expectChildColor(74, 74);
3411 // And 10 more pixels we should be back to the foreground surface
3412 mCapture->expectFGColor(84, 84);
3413 }
3414
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003415 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07003416
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003417 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07003418
Robert Carr9524cb32017-02-13 11:32:32 -08003419 // Nothing should have changed.
3420 {
chaviw0e3479f2018-09-10 16:49:30 -07003421 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003422 mCapture->expectFGColor(64, 64);
3423 mCapture->expectChildColor(74, 74);
3424 mCapture->expectFGColor(84, 84);
3425 }
3426}
3427
chaviw5aedec92018-10-22 10:40:38 -07003428TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
3429 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
3430 sp<SurfaceControl> childNewClient =
3431 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
3432 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3433
3434 ASSERT_TRUE(childNewClient != nullptr);
3435 ASSERT_TRUE(childNewClient->isValid());
3436
3437 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
3438
3439 Transaction()
3440 .hide(mChild)
3441 .show(childNewClient)
3442 .setPosition(childNewClient, 10, 10)
3443 .setPosition(mFGSurfaceControl, 64, 64)
3444 .apply();
3445
3446 {
3447 mCapture = screenshot();
3448 // Top left of foreground must now be visible
3449 mCapture->expectFGColor(64, 64);
3450 // But 10 pixels in we should see the child surface
3451 mCapture->expectChildColor(74, 74);
3452 // And 10 more pixels we should be back to the foreground surface
3453 mCapture->expectFGColor(84, 84);
3454 }
3455
3456 Transaction().detachChildren(mFGSurfaceControl).apply();
3457 Transaction().hide(childNewClient).apply();
3458
3459 // Nothing should have changed.
3460 {
3461 mCapture = screenshot();
3462 mCapture->expectFGColor(64, 64);
3463 mCapture->expectChildColor(74, 74);
3464 mCapture->expectFGColor(84, 84);
3465 }
3466
3467 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
3468 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
3469 32);
3470 Transaction()
3471 .setLayer(newParentSurface, INT32_MAX - 1)
3472 .show(newParentSurface)
3473 .setPosition(newParentSurface, 20, 20)
3474 .reparent(childNewClient, newParentSurface->getHandle())
3475 .apply();
3476 {
3477 mCapture = screenshot();
3478 // Child is now hidden.
3479 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
3480 }
3481}
3482
Robert Carr9b429f42017-04-17 14:56:57 -07003483TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003484 asTransaction([&](Transaction& t) {
3485 t.show(mChild);
3486 t.setPosition(mChild, 0, 0);
3487 t.setPosition(mFGSurfaceControl, 0, 0);
3488 });
Robert Carr9b429f42017-04-17 14:56:57 -07003489
3490 {
chaviw0e3479f2018-09-10 16:49:30 -07003491 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003492 // We've positioned the child in the top left.
3493 mCapture->expectChildColor(0, 0);
3494 // But it's only 10x10.
3495 mCapture->expectFGColor(10, 10);
3496 }
3497
Robert Carr4cdc58f2017-08-23 14:22:20 -07003498 asTransaction([&](Transaction& t) {
3499 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3500 // We cause scaling by 2.
3501 t.setSize(mFGSurfaceControl, 128, 128);
3502 });
Robert Carr9b429f42017-04-17 14:56:57 -07003503
3504 {
chaviw0e3479f2018-09-10 16:49:30 -07003505 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003506 // We've positioned the child in the top left.
3507 mCapture->expectChildColor(0, 0);
3508 mCapture->expectChildColor(10, 10);
3509 mCapture->expectChildColor(19, 19);
3510 // And now it should be scaled all the way to 20x20
3511 mCapture->expectFGColor(20, 20);
3512 }
3513}
3514
Robert Carr1725eee2017-04-26 18:32:15 -07003515// Regression test for b/37673612
3516TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003517 asTransaction([&](Transaction& t) {
3518 t.show(mChild);
3519 t.setPosition(mChild, 0, 0);
3520 t.setPosition(mFGSurfaceControl, 0, 0);
3521 });
Robert Carr1725eee2017-04-26 18:32:15 -07003522
3523 {
chaviw0e3479f2018-09-10 16:49:30 -07003524 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003525 // We've positioned the child in the top left.
3526 mCapture->expectChildColor(0, 0);
3527 // But it's only 10x10.
3528 mCapture->expectFGColor(10, 10);
3529 }
Robert Carr1725eee2017-04-26 18:32:15 -07003530 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
3531 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003532 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07003533 sp<Surface> s = mFGSurfaceControl->getSurface();
3534 auto anw = static_cast<ANativeWindow*>(s.get());
3535 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
3536 native_window_set_buffers_dimensions(anw, 64, 128);
3537 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3538 waitForPostedBuffers();
3539
3540 {
3541 // The child should still be in the same place and not have any strange scaling as in
3542 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07003543 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003544 mCapture->expectChildColor(0, 0);
3545 mCapture->expectFGColor(10, 10);
3546 }
3547}
3548
Dan Stoza412903f2017-04-27 13:42:17 -07003549TEST_F(ChildLayerTest, Bug36858924) {
3550 // Destroy the child layer
3551 mChild.clear();
3552
3553 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08003554 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
3555 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07003556
3557 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07003558 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003559 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
3560 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003561 t.show(mChild);
3562 });
Dan Stoza412903f2017-04-27 13:42:17 -07003563
3564 // Render the foreground surface a few times
3565 //
3566 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
3567 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
3568 // never acquire/release the first buffer
3569 ALOGI("Filling 1");
3570 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3571 ALOGI("Filling 2");
3572 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
3573 ALOGI("Filling 3");
3574 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
3575 ALOGI("Filling 4");
3576 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3577}
3578
chaviwf1961f72017-09-18 16:41:07 -07003579TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003580 asTransaction([&](Transaction& t) {
3581 t.show(mChild);
3582 t.setPosition(mChild, 10, 10);
3583 t.setPosition(mFGSurfaceControl, 64, 64);
3584 });
chaviw06178942017-07-27 10:25:59 -07003585
3586 {
chaviw0e3479f2018-09-10 16:49:30 -07003587 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003588 // Top left of foreground must now be visible
3589 mCapture->expectFGColor(64, 64);
3590 // But 10 pixels in we should see the child surface
3591 mCapture->expectChildColor(74, 74);
3592 // And 10 more pixels we should be back to the foreground surface
3593 mCapture->expectFGColor(84, 84);
3594 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003595
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003596 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07003597
chaviw06178942017-07-27 10:25:59 -07003598 {
chaviw0e3479f2018-09-10 16:49:30 -07003599 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003600 mCapture->expectFGColor(64, 64);
3601 // In reparenting we should have exposed the entire foreground surface.
3602 mCapture->expectFGColor(74, 74);
3603 // And the child layer should now begin at 10, 10 (since the BG
3604 // layer is at (0, 0)).
3605 mCapture->expectBGColor(9, 9);
3606 mCapture->expectChildColor(10, 10);
3607 }
3608}
3609
chaviwf1961f72017-09-18 16:41:07 -07003610TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003611 asTransaction([&](Transaction& t) {
3612 t.show(mChild);
3613 t.setPosition(mChild, 10, 10);
3614 t.setPosition(mFGSurfaceControl, 64, 64);
3615 });
chaviwf1961f72017-09-18 16:41:07 -07003616
3617 {
chaviw0e3479f2018-09-10 16:49:30 -07003618 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003619 // Top left of foreground must now be visible
3620 mCapture->expectFGColor(64, 64);
3621 // But 10 pixels in we should see the child surface
3622 mCapture->expectChildColor(74, 74);
3623 // And 10 more pixels we should be back to the foreground surface
3624 mCapture->expectFGColor(84, 84);
3625 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003626 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07003627 {
chaviw0e3479f2018-09-10 16:49:30 -07003628 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003629 // Nothing should have changed.
3630 mCapture->expectFGColor(64, 64);
3631 mCapture->expectChildColor(74, 74);
3632 mCapture->expectFGColor(84, 84);
3633 }
3634}
3635
3636TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07003637 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003638 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07003639 ASSERT_TRUE(newSurface->isValid());
3640
3641 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003642 asTransaction([&](Transaction& t) {
3643 t.hide(mChild);
3644 t.show(newSurface);
3645 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003646 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003647 t.setPosition(mFGSurfaceControl, 64, 64);
3648 });
chaviwf1961f72017-09-18 16:41:07 -07003649
3650 {
chaviw0e3479f2018-09-10 16:49:30 -07003651 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003652 // Top left of foreground must now be visible
3653 mCapture->expectFGColor(64, 64);
3654 // At 10, 10 we should see the new surface
3655 mCapture->checkPixel(10, 10, 63, 195, 63);
3656 }
3657
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003658 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07003659
3660 {
chaviw0e3479f2018-09-10 16:49:30 -07003661 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003662 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
3663 // mFGSurface, putting it at 74, 74.
3664 mCapture->expectFGColor(64, 64);
3665 mCapture->checkPixel(74, 74, 63, 195, 63);
3666 mCapture->expectFGColor(84, 84);
3667 }
3668}
3669
chaviwc9674332017-08-28 12:32:18 -07003670TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003671 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
3672 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07003673 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3674
3675 {
chaviw0e3479f2018-09-10 16:49:30 -07003676 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07003677 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
3678 // which begins at 64, 64
3679 mCapture->checkPixel(64, 64, 50, 50, 50);
3680 }
3681}
3682
Robert Carr503c7042017-09-27 15:06:08 -07003683TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003684 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07003685 fillSurfaceRGBA8(relative, 255, 255, 255);
3686
3687 Transaction t;
3688 t.setLayer(relative, INT32_MAX)
3689 .setRelativeLayer(mChild, relative->getHandle(), 1)
3690 .setPosition(mFGSurfaceControl, 0, 0)
3691 .apply(true);
3692
3693 // We expect that the child should have been elevated above our
3694 // INT_MAX layer even though it's not a child of it.
3695 {
chaviw0e3479f2018-09-10 16:49:30 -07003696 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07003697 mCapture->expectChildColor(0, 0);
3698 mCapture->expectChildColor(9, 9);
3699 mCapture->checkPixel(10, 10, 255, 255, 255);
3700 }
3701}
Vishnu Nair60356342018-11-13 13:00:45 -08003702class BoundlessLayerTest : public LayerUpdateTest {
3703protected:
3704 std::unique_ptr<ScreenCapture> mCapture;
3705};
3706
3707// Verify setting a size on a buffer layer has no effect.
3708TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
3709 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003710 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
3711 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003712 ASSERT_TRUE(bufferLayer->isValid());
3713 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
3714 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
3715 {
3716 mCapture = screenshot();
3717 // Top left of background must now be visible
3718 mCapture->expectBGColor(0, 0);
3719 // Foreground Surface bounds must be color layer
3720 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
3721 // Buffer layer should not extend past buffer bounds
3722 mCapture->expectFGColor(95, 95);
3723 }
3724}
3725
3726// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
3727// which will crop the color layer.
3728TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
3729 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003730 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3731 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003732 ASSERT_TRUE(colorLayer->isValid());
3733 asTransaction([&](Transaction& t) {
3734 t.setColor(colorLayer, half3{0, 0, 0});
3735 t.show(colorLayer);
3736 });
3737 {
3738 mCapture = screenshot();
3739 // Top left of background must now be visible
3740 mCapture->expectBGColor(0, 0);
3741 // Foreground Surface bounds must be color layer
3742 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3743 // Color layer should not extend past foreground bounds
3744 mCapture->expectBGColor(129, 129);
3745 }
3746}
3747
3748// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
3749// a crop which will be used to crop the color layer.
3750TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003751 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3752 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003753 ASSERT_TRUE(cropLayer->isValid());
3754 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003755 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3756 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003757 ASSERT_TRUE(colorLayer->isValid());
3758 asTransaction([&](Transaction& t) {
3759 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
3760 t.setColor(colorLayer, half3{0, 0, 0});
3761 t.show(cropLayer);
3762 t.show(colorLayer);
3763 });
3764 {
3765 mCapture = screenshot();
3766 // Top left of background must now be visible
3767 mCapture->expectBGColor(0, 0);
3768 // Top left of foreground must now be visible
3769 mCapture->expectFGColor(64, 64);
3770 // 5 pixels from the foreground we should see the child surface
3771 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
3772 // 10 pixels from the foreground we should be back to the foreground surface
3773 mCapture->expectFGColor(74, 74);
3774 }
3775}
3776
3777// Verify for boundless layer with no children, their transforms have no effect.
3778TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
3779 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003780 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3781 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003782 ASSERT_TRUE(colorLayer->isValid());
3783 asTransaction([&](Transaction& t) {
3784 t.setPosition(colorLayer, 320, 320);
3785 t.setMatrix(colorLayer, 2, 0, 0, 2);
3786 t.setColor(colorLayer, half3{0, 0, 0});
3787 t.show(colorLayer);
3788 });
3789 {
3790 mCapture = screenshot();
3791 // Top left of background must now be visible
3792 mCapture->expectBGColor(0, 0);
3793 // Foreground Surface bounds must be color layer
3794 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3795 // Color layer should not extend past foreground bounds
3796 mCapture->expectBGColor(129, 129);
3797 }
3798}
3799
3800// Verify for boundless layer with children, their transforms have an effect.
3801TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
3802 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003803 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3804 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003805 ASSERT_TRUE(boundlessLayerRightShift->isValid());
3806 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003807 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3808 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003809 ASSERT_TRUE(boundlessLayerDownShift->isValid());
3810 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003811 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3812 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003813 ASSERT_TRUE(colorLayer->isValid());
3814 asTransaction([&](Transaction& t) {
3815 t.setPosition(boundlessLayerRightShift, 32, 0);
3816 t.show(boundlessLayerRightShift);
3817 t.setPosition(boundlessLayerDownShift, 0, 32);
3818 t.show(boundlessLayerDownShift);
3819 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3820 t.setColor(colorLayer, half3{0, 0, 0});
3821 t.show(colorLayer);
3822 });
3823 {
3824 mCapture = screenshot();
3825 // Top left of background must now be visible
3826 mCapture->expectBGColor(0, 0);
3827 // Top left of foreground must now be visible
3828 mCapture->expectFGColor(64, 64);
3829 // Foreground Surface bounds must be color layer
3830 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
3831 // Color layer should not extend past foreground bounds
3832 mCapture->expectBGColor(129, 129);
3833 }
3834}
3835
3836// Verify child layers do not get clipped if they temporarily move into the negative
3837// coordinate space as the result of an intermediate transformation.
3838TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
3839 sp<SurfaceControl> boundlessLayer =
3840 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3841 0 /* flags */, mFGSurfaceControl.get());
3842 ASSERT_TRUE(boundlessLayer != nullptr);
3843 ASSERT_TRUE(boundlessLayer->isValid());
3844 sp<SurfaceControl> colorLayer =
3845 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3846 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
3847 ASSERT_TRUE(colorLayer != nullptr);
3848 ASSERT_TRUE(colorLayer->isValid());
3849 asTransaction([&](Transaction& t) {
3850 // shift child layer off bounds. If this layer was not boundless, we will
3851 // expect the child layer to be cropped.
3852 t.setPosition(boundlessLayer, 32, 32);
3853 t.show(boundlessLayer);
3854 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3855 // undo shift by parent
3856 t.setPosition(colorLayer, -32, -32);
3857 t.setColor(colorLayer, half3{0, 0, 0});
3858 t.show(colorLayer);
3859 });
3860 {
3861 mCapture = screenshot();
3862 // Top left of background must now be visible
3863 mCapture->expectBGColor(0, 0);
3864 // Foreground Surface bounds must be color layer
3865 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3866 // Color layer should not extend past foreground bounds
3867 mCapture->expectBGColor(129, 129);
3868 }
3869}
3870
3871// Verify for boundless root layers with children, their transforms have an effect.
3872TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003873 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
3874 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08003875 ASSERT_TRUE(rootBoundlessLayer->isValid());
3876 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003877 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3878 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
3879
Vishnu Nair60356342018-11-13 13:00:45 -08003880 ASSERT_TRUE(colorLayer->isValid());
3881 asTransaction([&](Transaction& t) {
3882 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
3883 t.setPosition(rootBoundlessLayer, 32, 32);
3884 t.show(rootBoundlessLayer);
3885 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3886 t.setColor(colorLayer, half3{0, 0, 0});
3887 t.show(colorLayer);
3888 t.hide(mFGSurfaceControl);
3889 });
3890 {
3891 mCapture = screenshot();
3892 // Top left of background must now be visible
3893 mCapture->expectBGColor(0, 0);
3894 // Top left of foreground must now be visible
3895 mCapture->expectBGColor(31, 31);
3896 // Foreground Surface bounds must be color layer
3897 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
3898 // Color layer should not extend past foreground bounds
3899 mCapture->expectBGColor(97, 97);
3900 }
3901}
Robert Carr503c7042017-09-27 15:06:08 -07003902
chaviwa76b2712017-09-20 12:02:26 -07003903class ScreenCaptureTest : public LayerUpdateTest {
3904protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003905 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07003906};
3907
3908TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
3909 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003910 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003911 mCapture->expectBGColor(0, 0);
3912 // Doesn't capture FG layer which is at 64, 64
3913 mCapture->expectBGColor(64, 64);
3914}
3915
3916TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
3917 auto fgHandle = mFGSurfaceControl->getHandle();
3918
Vishnu Nair88a11f22018-11-28 18:30:57 -08003919 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3920 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003921 fillSurfaceRGBA8(child, 200, 200, 200);
3922
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003923 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003924
3925 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003926 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003927 mCapture->expectFGColor(10, 10);
3928 mCapture->expectChildColor(0, 0);
3929}
3930
Robert Carr578038f2018-03-09 12:25:24 -08003931TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
3932 auto fgHandle = mFGSurfaceControl->getHandle();
3933
Vishnu Nair88a11f22018-11-28 18:30:57 -08003934 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3935 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08003936 fillSurfaceRGBA8(child, 200, 200, 200);
3937
3938 SurfaceComposerClient::Transaction().show(child).apply(true);
3939
3940 // Captures mFGSurfaceControl's child
3941 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3942 mCapture->checkPixel(10, 10, 0, 0, 0);
3943 mCapture->expectChildColor(0, 0);
3944}
3945
chaviw50da5042018-04-09 13:49:37 -07003946TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003947 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3948 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07003949
3950 fillSurfaceRGBA8(child, 200, 200, 200);
3951
3952 SurfaceComposerClient::Transaction().show(child).apply(true);
3953
3954 auto childHandle = child->getHandle();
3955
3956 // Captures child
3957 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
3958 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
3959 // Area outside of child's bounds is transparent.
3960 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
3961}
3962
chaviw4b129c22018-04-09 16:19:43 -07003963TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
3964 auto fgHandle = mFGSurfaceControl->getHandle();
3965
Vishnu Nair88a11f22018-11-28 18:30:57 -08003966 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3967 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3968 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07003969 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07003970 fillSurfaceRGBA8(child, 200, 200, 200);
3971 fillSurfaceRGBA8(relative, 100, 100, 100);
3972
3973 SurfaceComposerClient::Transaction()
3974 .show(child)
3975 // Set relative layer above fg layer so should be shown above when computing all layers.
3976 .setRelativeLayer(relative, fgHandle, 1)
3977 .show(relative)
3978 .apply(true);
3979
3980 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
3981 ScreenCapture::captureLayers(&mCapture, fgHandle);
3982 mCapture->expectFGColor(10, 10);
3983 mCapture->expectChildColor(0, 0);
3984}
3985
3986TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
3987 auto fgHandle = mFGSurfaceControl->getHandle();
3988
Vishnu Nair88a11f22018-11-28 18:30:57 -08003989 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3990 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3991 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
3992 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07003993 fillSurfaceRGBA8(child, 200, 200, 200);
3994 fillSurfaceRGBA8(relative, 100, 100, 100);
3995
3996 SurfaceComposerClient::Transaction()
3997 .show(child)
3998 // Set relative layer below fg layer but relative to child layer so it should be shown
3999 // above child layer.
4000 .setLayer(relative, -1)
4001 .setRelativeLayer(relative, child->getHandle(), 1)
4002 .show(relative)
4003 .apply(true);
4004
4005 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
4006 // relative value should be taken into account, placing it above child layer.
4007 ScreenCapture::captureLayers(&mCapture, fgHandle);
4008 mCapture->expectFGColor(10, 10);
4009 // Relative layer is showing on top of child layer
4010 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
4011}
Robert Carr578038f2018-03-09 12:25:24 -08004012
4013// In the following tests we verify successful skipping of a parent layer,
4014// so we use the same verification logic and only change how we mutate
4015// the parent layer to verify that various properties are ignored.
4016class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
4017public:
4018 void SetUp() override {
4019 LayerUpdateTest::SetUp();
4020
Vishnu Nair88a11f22018-11-28 18:30:57 -08004021 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4022 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004023 fillSurfaceRGBA8(mChild, 200, 200, 200);
4024
4025 SurfaceComposerClient::Transaction().show(mChild).apply(true);
4026 }
4027
4028 void verify() {
4029 auto fgHandle = mFGSurfaceControl->getHandle();
4030 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4031 mCapture->checkPixel(10, 10, 0, 0, 0);
4032 mCapture->expectChildColor(0, 0);
4033 }
4034
4035 std::unique_ptr<ScreenCapture> mCapture;
4036 sp<SurfaceControl> mChild;
4037};
4038
4039TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4040
4041 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4042
4043 // Even though the parent is hidden we should still capture the child.
4044 verify();
4045}
4046
4047TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004048 SurfaceComposerClient::Transaction()
4049 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
4050 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08004051
4052 // Even though the parent is cropped out we should still capture the child.
4053 verify();
4054}
4055
4056TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4057
4058 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4059
4060 // We should not inherit the parent scaling.
4061 verify();
4062}
4063
Robert Carr15eae092018-03-23 13:43:53 -07004064TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4065 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4066
4067 // Even though the parent is hidden we should still capture the child.
4068 verify();
4069
4070 // Verify everything was properly hidden when rendering the full-screen.
4071 screenshot()->expectBGColor(0,0);
4072}
4073
4074
chaviwa76b2712017-09-20 12:02:26 -07004075TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4076 auto fgHandle = mFGSurfaceControl->getHandle();
4077
Vishnu Nair88a11f22018-11-28 18:30:57 -08004078 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4079 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004080 fillSurfaceRGBA8(child, 200, 200, 200);
4081
Vishnu Nair88a11f22018-11-28 18:30:57 -08004082 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4083 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004084
4085 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4086 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004087 .show(child)
4088 .setPosition(grandchild, 5, 5)
4089 .show(grandchild)
4090 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004091
4092 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004093 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004094 mCapture->expectFGColor(10, 10);
4095 mCapture->expectChildColor(0, 0);
4096 mCapture->checkPixel(5, 5, 50, 50, 50);
4097}
4098
4099TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004100 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4101 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004102 fillSurfaceRGBA8(child, 200, 200, 200);
4103 auto childHandle = child->getHandle();
4104
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004105 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004106
4107 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004108 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004109 mCapture->expectChildColor(0, 0);
4110 mCapture->expectChildColor(9, 9);
4111}
4112
4113TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004114 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4115 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004116 fillSurfaceRGBA8(child, 200, 200, 200);
4117 auto childHandle = child->getHandle();
4118
Vishnu Nair88a11f22018-11-28 18:30:57 -08004119 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4120 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004121 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4122
4123 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004124 .show(child)
4125 .setPosition(grandchild, 5, 5)
4126 .show(grandchild)
4127 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004128
4129 auto grandchildHandle = grandchild->getHandle();
4130
4131 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004132 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004133 mCapture->checkPixel(0, 0, 50, 50, 50);
4134 mCapture->checkPixel(4, 4, 50, 50, 50);
4135}
4136
chaviw7206d492017-11-10 16:16:12 -08004137TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004138 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004139 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4140 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004141
Marissa Wall61c58622018-07-18 10:12:20 -07004142 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4143 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004144
4145 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004146 .setLayer(redLayer, INT32_MAX - 1)
4147 .show(redLayer)
4148 .show(blueLayer)
4149 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004150
4151 auto redLayerHandle = redLayer->getHandle();
4152
4153 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004154 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4155 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4156 // red area below the blue area
4157 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4158 // red area to the right of the blue area
4159 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004160
4161 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004162 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004163 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4164 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004165 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004166 mCapture->checkPixel(30, 30, 0, 0, 0);
4167}
4168
4169TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004170 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004171 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4172 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004173
Marissa Wall61c58622018-07-18 10:12:20 -07004174 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4175 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004176
4177 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004178 .setLayer(redLayer, INT32_MAX - 1)
4179 .show(redLayer)
4180 .show(blueLayer)
4181 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004182
4183 auto redLayerHandle = redLayer->getHandle();
4184
4185 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004186 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4187 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4188 // red area below the blue area
4189 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4190 // red area to the right of the blue area
4191 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004192
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004193 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004194 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004195 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4196 // red area below the blue area
4197 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4198 // red area to the right of the blue area
4199 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004200 mCapture->checkPixel(30, 30, 0, 0, 0);
4201}
4202
4203TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004204 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004205
Marissa Wall61c58622018-07-18 10:12:20 -07004206 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004207
4208 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07004209 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08004210 SurfaceComposerClient::Transaction().apply(true);
4211
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004212 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004213
4214 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004215 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4216 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004217}
4218
chaviw8e3fe5d2018-02-22 10:55:42 -08004219
4220class DereferenceSurfaceControlTest : public LayerTransactionTest {
4221protected:
4222 void SetUp() override {
4223 LayerTransactionTest::SetUp();
4224 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004225 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004226 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004227 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004228 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4229 {
4230 SCOPED_TRACE("before anything");
4231 auto shot = screenshot();
4232 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4233 }
4234 }
4235 void TearDown() override {
4236 LayerTransactionTest::TearDown();
4237 bgLayer = 0;
4238 fgLayer = 0;
4239 }
4240
4241 sp<SurfaceControl> bgLayer;
4242 sp<SurfaceControl> fgLayer;
4243};
4244
4245TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4246 fgLayer = nullptr;
4247 {
4248 SCOPED_TRACE("after setting null");
4249 auto shot = screenshot();
4250 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4251 }
4252}
4253
4254TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4255 auto transaction = Transaction().show(fgLayer);
4256 fgLayer = nullptr;
4257 {
4258 SCOPED_TRACE("after setting null");
4259 auto shot = screenshot();
4260 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4261 }
4262}
4263
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004264} // namespace android