blob: 7e95d99512b231ca67d3a345300e1ab88f17c2ba [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
Chia-I Wue4ef6102017-11-01 15:16:35 -07001228TEST_F(LayerTransactionTest, SetColorBasic) {
1229 sp<SurfaceControl> bufferLayer;
1230 sp<SurfaceControl> colorLayer;
1231 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001232 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001233 ASSERT_NO_FATAL_FAILURE(colorLayer =
1234 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1235 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001236
Vishnu Nair88a11f22018-11-28 18:30:57 -08001237 Transaction()
1238 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1239 .setLayer(colorLayer, mLayerZBase + 1)
1240 .apply();
1241
Chia-I Wue4ef6102017-11-01 15:16:35 -07001242 {
1243 SCOPED_TRACE("default color");
1244 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1245 }
1246
1247 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1248 const Color expected = {15, 51, 85, 255};
1249 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1250 // channel) should be less than one
1251 const uint8_t tolerance = 1;
1252 Transaction().setColor(colorLayer, color).apply();
1253 {
1254 SCOPED_TRACE("new color");
1255 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1256 }
1257}
1258
1259TEST_F(LayerTransactionTest, SetColorClamped) {
1260 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001261 ASSERT_NO_FATAL_FAILURE(colorLayer =
1262 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1263 ISurfaceComposerClient::eFXSurfaceColor));
1264 Transaction()
1265 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1266 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1267 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001268
Chia-I Wue4ef6102017-11-01 15:16:35 -07001269 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1270}
1271
1272TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1273 sp<SurfaceControl> bufferLayer;
1274 sp<SurfaceControl> colorLayer;
1275 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001276 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001277 ASSERT_NO_FATAL_FAILURE(colorLayer =
1278 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1279 ISurfaceComposerClient::eFXSurfaceColor));
1280 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001281
1282 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1283 const float alpha = 0.25f;
1284 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1285 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1286 // channel) should be less than one
1287 const uint8_t tolerance = 1;
1288 Transaction()
1289 .setColor(colorLayer, color)
1290 .setAlpha(colorLayer, alpha)
1291 .setLayer(colorLayer, mLayerZBase + 1)
1292 .apply();
1293 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1294 tolerance);
1295}
1296
Adrian Roosb7a96502018-04-08 11:38:55 -07001297TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1298 sp<SurfaceControl> bufferLayer;
1299 sp<SurfaceControl> parentLayer;
1300 sp<SurfaceControl> colorLayer;
1301 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1302 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001303 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001304 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1305 0 /* buffer height */,
1306 ISurfaceComposerClient::eFXSurfaceColor));
1307 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001308 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1309 const float alpha = 0.25f;
1310 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1311 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1312 // channel) should be less than one
1313 const uint8_t tolerance = 1;
1314 Transaction()
1315 .reparent(colorLayer, parentLayer->getHandle())
1316 .setColor(colorLayer, color)
1317 .setAlpha(parentLayer, alpha)
1318 .setLayer(parentLayer, mLayerZBase + 1)
1319 .apply();
1320 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1321 tolerance);
1322}
1323
Marissa Wall61c58622018-07-18 10:12:20 -07001324TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001325 sp<SurfaceControl> bufferLayer;
1326 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001327 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001328
1329 // color is ignored
1330 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1331 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1332}
1333
Marissa Wall61c58622018-07-18 10:12:20 -07001334TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001335 sp<SurfaceControl> layer;
1336 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001337 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001338
1339 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1340 {
1341 SCOPED_TRACE("non-existing layer stack");
1342 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1343 }
1344
1345 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1346 {
1347 SCOPED_TRACE("original layer stack");
1348 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1349 }
1350}
1351
Marissa Wall61c58622018-07-18 10:12:20 -07001352TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001353 sp<SurfaceControl> layer;
1354 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1355 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001356 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001357
1358 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1359 {
1360 SCOPED_TRACE("IDENTITY");
1361 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1362 Color::WHITE);
1363 }
1364
1365 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1366 {
1367 SCOPED_TRACE("FLIP_H");
1368 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1369 Color::BLUE);
1370 }
1371
1372 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1373 {
1374 SCOPED_TRACE("FLIP_V");
1375 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1376 Color::GREEN);
1377 }
1378
1379 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1380 {
1381 SCOPED_TRACE("ROT_90");
1382 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1383 Color::GREEN);
1384 }
1385
1386 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1387 {
1388 SCOPED_TRACE("SCALE");
1389 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1390 Color::WHITE, true /* filtered */);
1391 }
1392}
1393
Marissa Wall61c58622018-07-18 10:12:20 -07001394TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001395 sp<SurfaceControl> layer;
1396 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1397 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001398 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001399
1400 const float rot = M_SQRT1_2; // 45 degrees
1401 const float trans = M_SQRT2 * 16.0f;
1402 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1403
1404 auto shot = screenshot();
1405 // check a 8x8 region inside each color
1406 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1407 const int32_t halfL = 4;
1408 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1409 };
1410 const int32_t unit = int32_t(trans / 2);
1411 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1412 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1413 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1414 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1415}
1416
Marissa Wall61c58622018-07-18 10:12:20 -07001417void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001418 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001419 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1420 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001421
1422 // setMatrix is applied after any pending resize, unlike setPosition
1423 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1424 {
1425 SCOPED_TRACE("resize pending");
1426 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001427 Rect rect;
1428 switch (layerType) {
1429 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1430 rect = {0, 0, 32, 32};
1431 break;
1432 case ISurfaceComposerClient::eFXSurfaceBufferState:
1433 rect = {0, 0, 128, 128};
1434 break;
1435 default:
1436 ASSERT_FALSE(true) << "Unsupported layer type";
1437 }
1438 shot->expectColor(rect, Color::RED);
1439 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001440 }
1441
Marissa Wall61c58622018-07-18 10:12:20 -07001442 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001443 {
1444 SCOPED_TRACE("resize applied");
1445 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1446 }
1447}
1448
Marissa Wall61c58622018-07-18 10:12:20 -07001449TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1450 ASSERT_NO_FATAL_FAILURE(
1451 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1452}
1453
1454TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1455 ASSERT_NO_FATAL_FAILURE(
1456 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1457}
1458
1459TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001460 sp<SurfaceControl> layer;
1461 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001462 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001463
1464 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1465 Transaction()
1466 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1467 .setSize(layer, 64, 64)
1468 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1469 .apply();
1470 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1471}
1472
Marissa Wall61c58622018-07-18 10:12:20 -07001473TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001474 sp<SurfaceControl> layer;
1475 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1476 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001477 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001478
1479 // XXX SCALE_CROP is not respected; calling setSize and
1480 // setOverrideScalingMode in separate transactions does not work
1481 // (b/69315456)
1482 Transaction()
1483 .setSize(layer, 64, 16)
1484 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1485 .apply();
1486 {
1487 SCOPED_TRACE("SCALE_TO_WINDOW");
1488 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1489 Color::WHITE, true /* filtered */);
1490 }
1491}
1492
Dan Stoza000dd012018-08-01 13:31:52 -07001493TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1494 sp<SurfaceControl> layer;
1495 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1496
1497 sp<IBinder> handle = layer->getHandle();
1498 ASSERT_TRUE(handle != nullptr);
1499
1500 FrameStats frameStats;
1501 mClient->getLayerFrameStats(handle, &frameStats);
1502
1503 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1504}
1505
Marissa Wall61c58622018-07-18 10:12:20 -07001506TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001507 sp<SurfaceControl> layer;
1508 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001509 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001510 const Rect crop(8, 8, 24, 24);
1511
Marissa Wallf58c14b2018-07-24 10:50:43 -07001512 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001513 auto shot = screenshot();
1514 shot->expectColor(crop, Color::RED);
1515 shot->expectBorder(crop, Color::BLACK);
1516}
1517
Marissa Wall61c58622018-07-18 10:12:20 -07001518TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1519 sp<SurfaceControl> layer;
1520 ASSERT_NO_FATAL_FAILURE(
1521 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1522 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1523 const Rect crop(8, 8, 24, 24);
1524
1525 Transaction().setCrop(layer, crop).apply();
1526 auto shot = screenshot();
1527 shot->expectColor(crop, Color::RED);
1528 shot->expectBorder(crop, Color::BLACK);
1529}
1530
1531TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001532 sp<SurfaceControl> layer;
1533 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001534 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001535
1536 {
1537 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001538 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001539 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1540 }
1541
1542 {
1543 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001544 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001545 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1546 }
1547}
1548
Marissa Wall61c58622018-07-18 10:12:20 -07001549TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1550 sp<SurfaceControl> layer;
1551 ASSERT_NO_FATAL_FAILURE(
1552 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1553 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1554
1555 {
1556 SCOPED_TRACE("empty rect");
1557 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1558 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1559 }
1560
1561 {
1562 SCOPED_TRACE("negative rect");
1563 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1564 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1565 }
1566}
1567
1568TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001569 sp<SurfaceControl> layer;
1570 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001571 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001572
Marissa Wallf58c14b2018-07-24 10:50:43 -07001573 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001574 auto shot = screenshot();
1575 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1576 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1577}
1578
Marissa Wall61c58622018-07-18 10:12:20 -07001579TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1580 sp<SurfaceControl> layer;
1581 ASSERT_NO_FATAL_FAILURE(
1582 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1583 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1584
1585 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1586 auto shot = screenshot();
1587 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1588 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1589}
1590
1591TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001592 sp<SurfaceControl> layer;
1593 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001594 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001595
1596 const Point position(32, 32);
1597 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001598 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001599 auto shot = screenshot();
1600 shot->expectColor(crop + position, Color::RED);
1601 shot->expectBorder(crop + position, Color::BLACK);
1602}
1603
Marissa Wall61c58622018-07-18 10:12:20 -07001604TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1605 sp<SurfaceControl> layer;
1606 ASSERT_NO_FATAL_FAILURE(
1607 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1608 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1609
1610 const Point position(32, 32);
1611 const Rect crop(8, 8, 24, 24);
1612 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1613 auto shot = screenshot();
1614 shot->expectColor(crop + position, Color::RED);
1615 shot->expectBorder(crop + position, Color::BLACK);
1616}
1617
1618TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001619 sp<SurfaceControl> layer;
1620 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001621 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001622
1623 // crop is affected by matrix
1624 Transaction()
1625 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001626 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001627 .apply();
1628 auto shot = screenshot();
1629 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1630 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1631}
1632
Marissa Wall61c58622018-07-18 10:12:20 -07001633TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1634 sp<SurfaceControl> layer;
1635 ASSERT_NO_FATAL_FAILURE(
1636 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1637 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1638
1639 // crop is affected by matrix
1640 Transaction()
1641 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1642 .setCrop(layer, Rect(8, 8, 24, 24))
1643 .apply();
1644 auto shot = screenshot();
1645 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1646 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1647}
1648
1649TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001650 sp<SurfaceControl> layer;
1651 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001652 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001653
Marissa Wallf58c14b2018-07-24 10:50:43 -07001654 // setCrop_legacy is applied immediately by default, with or without resize pending
1655 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001656 {
1657 SCOPED_TRACE("resize pending");
1658 auto shot = screenshot();
1659 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1660 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1661 }
1662
Marissa Wall61c58622018-07-18 10:12:20 -07001663 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001664 {
1665 SCOPED_TRACE("resize applied");
1666 auto shot = screenshot();
1667 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1668 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1669 }
1670}
1671
Marissa Wall61c58622018-07-18 10:12:20 -07001672TEST_F(LayerTransactionTest, SetCropWithResize_BufferState) {
1673 sp<SurfaceControl> layer;
1674 ASSERT_NO_FATAL_FAILURE(
1675 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1676 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1677
1678 // setCrop_legacy is applied immediately by default, with or without resize pending
1679 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1680 {
1681 SCOPED_TRACE("new buffer pending");
1682 auto shot = screenshot();
1683 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1684 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1685 }
1686
1687 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1688 {
1689 SCOPED_TRACE("new buffer");
1690 auto shot = screenshot();
1691 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1692 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1693 }
1694}
1695
1696TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001697 sp<SurfaceControl> layer;
1698 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001699 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001700
Marissa Wallf58c14b2018-07-24 10:50:43 -07001701 // request setCrop_legacy to be applied with the next resize
1702 Transaction()
1703 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1704 .setGeometryAppliesWithResize(layer)
1705 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001706 {
1707 SCOPED_TRACE("waiting for next resize");
1708 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1709 }
1710
Marissa Wallf58c14b2018-07-24 10:50:43 -07001711 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001712 {
1713 SCOPED_TRACE("pending crop modified");
1714 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1715 }
1716
1717 Transaction().setSize(layer, 16, 16).apply();
1718 {
1719 SCOPED_TRACE("resize pending");
1720 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1721 }
1722
1723 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001724 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001725 {
1726 SCOPED_TRACE("new crop applied");
1727 auto shot = screenshot();
1728 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1729 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1730 }
1731}
1732
Marissa Wall61c58622018-07-18 10:12:20 -07001733TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1734 sp<SurfaceControl> layer;
1735 ASSERT_NO_FATAL_FAILURE(
1736 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1737 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1738
1739 // request setCrop_legacy to be applied with the next resize
1740 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1741 {
1742 SCOPED_TRACE("set crop 1");
1743 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1744 }
1745
1746 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1747 {
1748 SCOPED_TRACE("set crop 2");
1749 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1750 }
1751
1752 Transaction().setSize(layer, 16, 16).apply();
1753 {
1754 SCOPED_TRACE("resize");
1755 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1756 }
1757
1758 // finally resize
1759 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1760 {
1761 SCOPED_TRACE("new buffer");
1762 auto shot = screenshot();
1763 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1764 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1765 }
1766}
1767
1768TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001769 sp<SurfaceControl> layer;
1770 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001771 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001772
Marissa Wallf58c14b2018-07-24 10:50:43 -07001773 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001774 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001775 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001776 .setSize(layer, 16, 16)
1777 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1778 .setGeometryAppliesWithResize(layer)
1779 .apply();
1780 {
1781 SCOPED_TRACE("new crop pending");
1782 auto shot = screenshot();
1783 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1784 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1785 }
1786
1787 // XXX crop is never latched without other geometry change (b/69315677)
1788 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001789 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001790 Transaction().setPosition(layer, 0, 0).apply();
1791 {
1792 SCOPED_TRACE("new crop applied");
1793 auto shot = screenshot();
1794 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1795 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1796 }
1797}
1798
Marissa Wall61c58622018-07-18 10:12:20 -07001799TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1800 sp<SurfaceControl> layer;
1801 ASSERT_NO_FATAL_FAILURE(
1802 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1803 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1804
1805 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1806 Transaction()
1807 .setCrop(layer, Rect(4, 4, 12, 12))
1808 .setSize(layer, 16, 16)
1809 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1810 .setGeometryAppliesWithResize(layer)
1811 .apply();
1812 {
1813 SCOPED_TRACE("new crop pending");
1814 auto shot = screenshot();
1815 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1816 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1817 }
1818
1819 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1820 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1821 Transaction().setPosition(layer, 0, 0).apply();
1822 {
1823 SCOPED_TRACE("new crop applied");
1824 auto shot = screenshot();
1825 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1826 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1827 }
1828}
1829
Marissa Wall61c58622018-07-18 10:12:20 -07001830TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1831 sp<SurfaceControl> layer;
1832 ASSERT_NO_FATAL_FAILURE(
1833 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1834
1835 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1836
1837 auto shot = screenshot();
1838 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1839 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1840}
1841
1842TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1843 sp<SurfaceControl> layer;
1844 ASSERT_NO_FATAL_FAILURE(
1845 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1846
1847 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1848
1849 {
1850 SCOPED_TRACE("set buffer 1");
1851 auto shot = screenshot();
1852 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1853 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1854 }
1855
1856 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1857
1858 {
1859 SCOPED_TRACE("set buffer 2");
1860 auto shot = screenshot();
1861 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1862 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1863 }
1864
1865 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1866
1867 {
1868 SCOPED_TRACE("set buffer 3");
1869 auto shot = screenshot();
1870 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1871 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1872 }
1873}
1874
1875TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1876 sp<SurfaceControl> layer1;
1877 ASSERT_NO_FATAL_FAILURE(
1878 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1879
1880 sp<SurfaceControl> layer2;
1881 ASSERT_NO_FATAL_FAILURE(
1882 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1883
1884 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1885
1886 {
1887 SCOPED_TRACE("set layer 1 buffer red");
1888 auto shot = screenshot();
1889 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1890 }
1891
1892 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1893
1894 {
1895 SCOPED_TRACE("set layer 2 buffer blue");
1896 auto shot = screenshot();
1897 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1898 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1899 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1900 }
1901
1902 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1903 {
1904 SCOPED_TRACE("set layer 1 buffer green");
1905 auto shot = screenshot();
1906 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1907 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1908 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1909 }
1910
1911 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1912
1913 {
1914 SCOPED_TRACE("set layer 2 buffer white");
1915 auto shot = screenshot();
1916 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1917 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1918 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1919 }
1920}
1921
1922TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1923 sp<SurfaceControl> layer;
1924 ASSERT_NO_FATAL_FAILURE(
1925 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1926
1927 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1928 Color::BLUE, Color::WHITE));
1929
1930 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1931
1932 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1933 Color::GREEN, true /* filtered */);
1934}
1935
1936TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1937 sp<SurfaceControl> layer;
1938 ASSERT_NO_FATAL_FAILURE(
1939 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1940
1941 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1942 Color::BLUE, Color::WHITE));
1943
1944 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1945
1946 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1947 Color::BLUE, true /* filtered */);
1948}
1949
1950TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
1951 sp<SurfaceControl> layer;
1952 ASSERT_NO_FATAL_FAILURE(
1953 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1954
1955 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1956 Color::BLUE, Color::WHITE));
1957
1958 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
1959
1960 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1961 Color::GREEN, true /* filtered */);
1962}
1963
1964TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
1965 sp<SurfaceControl> layer;
1966 ASSERT_NO_FATAL_FAILURE(
1967 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1968
1969 Transaction().setTransformToDisplayInverse(layer, false).apply();
1970
1971 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
1972
1973 Transaction().setTransformToDisplayInverse(layer, true).apply();
1974}
1975
1976TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
1977 sp<SurfaceControl> layer;
1978 ASSERT_NO_FATAL_FAILURE(
1979 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1980
1981 sp<GraphicBuffer> buffer =
1982 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1983 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1984 BufferUsage::COMPOSER_OVERLAY,
1985 "test");
1986 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1987
Marissa Wallfda30bb2018-10-12 11:34:28 -07001988 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07001989
1990 Transaction()
1991 .setBuffer(layer, buffer)
1992 .setAcquireFence(layer, fence)
1993 .setSize(layer, 32, 32)
1994 .apply();
1995
1996 auto shot = screenshot();
1997 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1998 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1999}
2000
2001TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2002 sp<SurfaceControl> layer;
2003 ASSERT_NO_FATAL_FAILURE(
2004 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2005
2006 sp<GraphicBuffer> buffer =
2007 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2008 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2009 BufferUsage::COMPOSER_OVERLAY,
2010 "test");
2011 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2012
2013 Transaction()
2014 .setBuffer(layer, buffer)
2015 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2016 .setSize(layer, 32, 32)
2017 .apply();
2018
2019 auto shot = screenshot();
2020 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2021 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2022}
2023
2024TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2025 sp<SurfaceControl> layer;
2026 ASSERT_NO_FATAL_FAILURE(
2027 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2028
2029 sp<GraphicBuffer> buffer =
2030 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2031 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2032 BufferUsage::COMPOSER_OVERLAY,
2033 "test");
2034 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2035
2036 HdrMetadata hdrMetadata;
2037 hdrMetadata.validTypes = 0;
2038 Transaction()
2039 .setBuffer(layer, buffer)
2040 .setHdrMetadata(layer, hdrMetadata)
2041 .setSize(layer, 32, 32)
2042 .apply();
2043
2044 auto shot = screenshot();
2045 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2046 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2047}
2048
2049TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2050 sp<SurfaceControl> layer;
2051 ASSERT_NO_FATAL_FAILURE(
2052 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2053
2054 sp<GraphicBuffer> buffer =
2055 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2056 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2057 BufferUsage::COMPOSER_OVERLAY,
2058 "test");
2059 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2060
2061 Region region;
2062 region.set(32, 32);
2063 Transaction()
2064 .setBuffer(layer, buffer)
2065 .setSurfaceDamageRegion(layer, region)
2066 .setSize(layer, 32, 32)
2067 .apply();
2068
2069 auto shot = screenshot();
2070 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2071 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2072}
2073
2074TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2075 sp<SurfaceControl> layer;
2076 ASSERT_NO_FATAL_FAILURE(
2077 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2078
2079 sp<GraphicBuffer> buffer =
2080 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2081 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2082 BufferUsage::COMPOSER_OVERLAY,
2083 "test");
2084 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2085
2086 Transaction()
2087 .setBuffer(layer, buffer)
2088 .setApi(layer, NATIVE_WINDOW_API_CPU)
2089 .setSize(layer, 32, 32)
2090 .apply();
2091
2092 auto shot = screenshot();
2093 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2094 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2095}
2096
2097TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2098 sp<SurfaceControl> layer;
2099 ASSERT_NO_FATAL_FAILURE(
2100 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2101
2102 // verify this doesn't cause a crash
2103 Transaction().setSidebandStream(layer, nullptr).apply();
2104}
2105
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002106class ColorTransformHelper {
2107public:
2108 static void DegammaColorSingle(half& s) {
2109 if (s <= 0.03928f)
2110 s = s / 12.92f;
2111 else
2112 s = pow((s + 0.055f) / 1.055f, 2.4f);
2113 }
2114
2115 static void DegammaColor(half3& color) {
2116 DegammaColorSingle(color.r);
2117 DegammaColorSingle(color.g);
2118 DegammaColorSingle(color.b);
2119 }
2120
2121 static void GammaColorSingle(half& s) {
2122 if (s <= 0.0031308f) {
2123 s = s * 12.92f;
2124 } else {
2125 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2126 }
2127 }
2128
2129 static void GammaColor(half3& color) {
2130 GammaColorSingle(color.r);
2131 GammaColorSingle(color.g);
2132 GammaColorSingle(color.b);
2133 }
2134
2135 static void applyMatrix(half3& color, const mat3& mat) {
2136 half3 ret = half3(0);
2137
2138 for (int i = 0; i < 3; i++) {
2139 for (int j = 0; j < 3; j++) {
2140 ret[i] = ret[i] + color[j] * mat[j][i];
2141 }
2142 }
2143 color = ret;
2144 }
2145};
2146
Peiyong Lind3788632018-09-18 16:01:31 -07002147TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2148 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002149 ASSERT_NO_FATAL_FAILURE(colorLayer =
2150 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2151 ISurfaceComposerClient::eFXSurfaceColor));
2152 Transaction()
2153 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2154 .setLayer(colorLayer, mLayerZBase + 1)
2155 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002156 {
2157 SCOPED_TRACE("default color");
2158 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2159 }
2160
2161 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002162 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002163 mat3 matrix;
2164 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2165 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2166 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002167
2168 // degamma before applying the matrix
2169 if (mColorManagementUsed) {
2170 ColorTransformHelper::DegammaColor(expected);
2171 }
2172
2173 ColorTransformHelper::applyMatrix(expected, matrix);
2174
2175 if (mColorManagementUsed) {
2176 ColorTransformHelper::GammaColor(expected);
2177 }
2178
2179 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2180 uint8_t(expected.b * 255), 255};
2181
2182 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2183 // channel) should be less than one
2184 const uint8_t tolerance = 1;
2185
Peiyong Lind3788632018-09-18 16:01:31 -07002186 Transaction().setColor(colorLayer, color)
2187 .setColorTransform(colorLayer, matrix, vec3()).apply();
2188 {
2189 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002190 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002191 }
2192}
2193
Marissa Wallfda30bb2018-10-12 11:34:28 -07002194class ExpectedResult {
2195public:
2196 enum Transaction {
2197 NOT_PRESENTED = 0,
2198 PRESENTED,
2199 };
2200
2201 enum Buffer {
2202 NOT_ACQUIRED = 0,
2203 ACQUIRED,
2204 };
2205
2206 enum PreviousBuffer {
2207 NOT_RELEASED = 0,
2208 RELEASED,
2209 };
2210
2211 void reset() {
2212 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2213 mExpectedSurfaceResults.clear();
2214 }
2215
2216 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
2217 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2218 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2219 mTransactionResult = transactionResult;
2220 mExpectedSurfaceResults.emplace(std::piecewise_construct,
2221 std::forward_as_tuple(layer->getHandle()),
2222 std::forward_as_tuple(bufferResult, previousBufferResult));
2223 }
2224
2225 void addSurfaces(ExpectedResult::Transaction transactionResult,
2226 const std::vector<sp<SurfaceControl>>& layers,
2227 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2228 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2229 for (const auto& layer : layers) {
2230 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2231 }
2232 }
2233
2234 void verifyTransactionStats(const TransactionStats& transactionStats) const {
2235 const auto& [latchTime, presentTime, surfaceStats] = transactionStats;
2236 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2237 ASSERT_GE(latchTime, 0) << "bad latch time";
2238 ASSERT_GE(presentTime, 0) << "bad present time";
2239 } else {
2240 ASSERT_EQ(presentTime, -1) << "transaction shouldn't have been presented";
2241 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2242 }
2243
2244 ASSERT_EQ(surfaceStats.size(), mExpectedSurfaceResults.size())
2245 << "wrong number of surfaces";
2246
2247 for (const auto& stats : surfaceStats) {
2248 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2249 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2250 << "unexpected surface control";
2251 expectedSurfaceResult->second.verifySurfaceStats(stats, latchTime);
2252 }
2253 }
2254
2255private:
2256 class ExpectedSurfaceResult {
2257 public:
2258 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2259 ExpectedResult::PreviousBuffer previousBufferResult)
2260 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2261
2262 void verifySurfaceStats(const SurfaceStats& surfaceStats, nsecs_t latchTime) const {
2263 const auto& [surfaceControl, acquireTime, releasePreviousBuffer] = surfaceStats;
2264
2265 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2266 << "bad acquire time";
2267 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
2268 ASSERT_EQ(releasePreviousBuffer,
2269 mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED)
2270 << "bad previous buffer released";
2271 }
2272
2273 private:
2274 ExpectedResult::Buffer mBufferResult;
2275 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2276 };
2277
2278 struct IBinderHash {
2279 std::size_t operator()(const sp<IBinder>& strongPointer) const {
2280 return std::hash<IBinder*>{}(strongPointer.get());
2281 }
2282 };
2283 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2284 std::unordered_map<sp<IBinder>, ExpectedSurfaceResult, IBinderHash> mExpectedSurfaceResults;
2285};
2286
2287class CallbackHelper {
2288public:
2289 static void function(void* callbackContext, const TransactionStats& transactionStats) {
2290 if (!callbackContext) {
2291 ALOGE("failed to get callback context");
2292 }
2293 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2294 std::lock_guard lock(helper->mMutex);
2295 helper->mTransactionStatsQueue.push(transactionStats);
2296 helper->mConditionVariable.notify_all();
2297 }
2298
2299 void getTransactionStats(TransactionStats* outStats) {
2300 std::unique_lock lock(mMutex);
2301
2302 if (mTransactionStatsQueue.empty()) {
2303 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2304 std::cv_status::timeout)
2305 << "did not receive callback";
2306 }
2307
2308 *outStats = std::move(mTransactionStatsQueue.front());
2309 mTransactionStatsQueue.pop();
2310 }
2311
2312 void verifyFinalState() {
2313 // Wait to see if there are extra callbacks
2314 std::this_thread::sleep_for(500ms);
2315
2316 std::lock_guard lock(mMutex);
2317 EXPECT_EQ(mTransactionStatsQueue.size(), 0) << "extra callbacks received";
2318 mTransactionStatsQueue = {};
2319 }
2320
2321 void* getContext() { return static_cast<void*>(this); }
2322
2323 std::mutex mMutex;
2324 std::condition_variable mConditionVariable;
2325 std::queue<TransactionStats> mTransactionStatsQueue;
2326};
2327
2328class LayerCallbackTest : public LayerTransactionTest {
2329protected:
2330 virtual sp<SurfaceControl> createBufferStateLayer() {
2331 return createLayer(mClient, "test", mWidth, mHeight,
2332 ISurfaceComposerClient::eFXSurfaceBufferState);
2333 }
2334
2335 virtual void fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2336 const sp<SurfaceControl>& layer = nullptr) {
2337 if (layer) {
2338 sp<GraphicBuffer> buffer =
2339 new GraphicBuffer(mWidth, mHeight, PIXEL_FORMAT_RGBA_8888, 1,
2340 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2341 BufferUsage::COMPOSER_OVERLAY |
2342 BufferUsage::GPU_TEXTURE,
2343 "test");
2344 fillGraphicBufferColor(buffer, Rect(0, 0, mWidth, mHeight), Color::RED);
2345
2346 sp<Fence> fence = new Fence(-1);
2347
2348 transaction.setBuffer(layer, buffer)
2349 .setAcquireFence(layer, fence)
2350 .setSize(layer, mWidth, mHeight);
2351 }
2352
2353 transaction.addTransactionCompletedCallback(callbackHelper->function,
2354 callbackHelper->getContext());
2355 }
2356
2357 void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2358 bool finalState = false) {
2359 TransactionStats transactionStats;
2360 ASSERT_NO_FATAL_FAILURE(helper.getTransactionStats(&transactionStats));
2361 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyTransactionStats(transactionStats));
2362
2363 if (finalState) {
2364 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2365 }
2366 }
2367
2368 void waitForCallbacks(CallbackHelper& helper,
2369 const std::vector<ExpectedResult>& expectedResults,
2370 bool finalState = false) {
2371 for (const auto& expectedResult : expectedResults) {
2372 waitForCallback(helper, expectedResult);
2373 }
2374 if (finalState) {
2375 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2376 }
2377 }
2378
2379 uint32_t mWidth = 32;
2380 uint32_t mHeight = 32;
2381};
2382
2383TEST_F(LayerCallbackTest, Basic) {
2384 sp<SurfaceControl> layer;
2385 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2386
2387 Transaction transaction;
2388 CallbackHelper callback;
2389 fillTransaction(transaction, &callback, layer);
2390
2391 transaction.apply();
2392
2393 ExpectedResult expected;
2394 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2395 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2396}
2397
2398TEST_F(LayerCallbackTest, NoBuffer) {
2399 sp<SurfaceControl> layer;
2400 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2401
2402 Transaction transaction;
2403 CallbackHelper callback;
2404 fillTransaction(transaction, &callback);
2405
2406 transaction.setPosition(layer, mWidth, mHeight).apply();
2407
2408 ExpectedResult expected;
2409 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2410 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2411}
2412
2413TEST_F(LayerCallbackTest, NoStateChange) {
2414 Transaction transaction;
2415 CallbackHelper callback;
2416 fillTransaction(transaction, &callback);
2417
2418 transaction.apply();
2419
2420 ExpectedResult expected;
2421 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2422}
2423
2424TEST_F(LayerCallbackTest, OffScreen) {
2425 sp<SurfaceControl> layer;
2426 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2427
2428 Transaction transaction;
2429 CallbackHelper callback;
2430 fillTransaction(transaction, &callback, layer);
2431
2432 transaction.setPosition(layer, -100, -100).apply();
2433
2434 ExpectedResult expected;
2435 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2436 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2437}
2438
2439TEST_F(LayerCallbackTest, Merge) {
2440 sp<SurfaceControl> layer1, layer2;
2441 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2442 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2443
2444 Transaction transaction1, transaction2;
2445 CallbackHelper callback1, callback2;
2446 fillTransaction(transaction1, &callback1, layer1);
2447 fillTransaction(transaction2, &callback2, layer2);
2448
2449 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2450
2451 ExpectedResult expected;
2452 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2453 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2454 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2455}
2456
2457TEST_F(LayerCallbackTest, Merge_SameCallback) {
2458 sp<SurfaceControl> layer1, layer2;
2459 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2460 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2461
2462 Transaction transaction1, transaction2;
2463 CallbackHelper callback;
2464 fillTransaction(transaction1, &callback, layer1);
2465 fillTransaction(transaction2, &callback, layer2);
2466
2467 transaction2.merge(std::move(transaction1)).apply();
2468
2469 ExpectedResult expected;
2470 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2471 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2472 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2473}
2474
2475TEST_F(LayerCallbackTest, Merge_SameLayer) {
2476 sp<SurfaceControl> layer;
2477 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2478
2479 Transaction transaction1, transaction2;
2480 CallbackHelper callback1, callback2;
2481 fillTransaction(transaction1, &callback1, layer);
2482 fillTransaction(transaction2, &callback2, layer);
2483
2484 transaction2.merge(std::move(transaction1)).apply();
2485
2486 ExpectedResult expected;
2487 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2488 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2489 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2490}
2491
2492TEST_F(LayerCallbackTest, Merge_SingleBuffer) {
2493 sp<SurfaceControl> layer1, layer2;
2494 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2495 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2496
2497 Transaction transaction1, transaction2;
2498 CallbackHelper callback1, callback2;
2499 fillTransaction(transaction1, &callback1, layer1);
2500 fillTransaction(transaction2, &callback2);
2501
2502 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2503
2504 ExpectedResult expected;
2505 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2506 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2507 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2508}
2509
2510TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2511 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2512 client2(new SurfaceComposerClient);
2513
2514 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2515 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2516
2517 sp<SurfaceControl> layer1, layer2;
2518 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2519 ISurfaceComposerClient::eFXSurfaceBufferState));
2520 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2521 ISurfaceComposerClient::eFXSurfaceBufferState));
2522
2523 Transaction transaction1, transaction2;
2524 CallbackHelper callback1, callback2;
2525 fillTransaction(transaction1, &callback1, layer1);
2526 fillTransaction(transaction2, &callback2, layer2);
2527
2528 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2529
2530 ExpectedResult expected;
2531 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2532 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2533 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2534}
2535
2536TEST_F(LayerCallbackTest, MultipleTransactions) {
2537 sp<SurfaceControl> layer;
2538 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2539
2540 Transaction transaction;
2541 CallbackHelper callback;
2542 for (size_t i = 0; i < 10; i++) {
2543 fillTransaction(transaction, &callback, layer);
2544
2545 transaction.apply();
2546
2547 ExpectedResult expected;
2548 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2549 ExpectedResult::Buffer::NOT_ACQUIRED,
2550 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2551 : ExpectedResult::PreviousBuffer::RELEASED);
2552 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2553 }
2554 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2555}
2556
2557TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
2558 sp<SurfaceControl> layer;
2559 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2560
2561 Transaction transaction;
2562 CallbackHelper callback;
2563 for (size_t i = 0; i < 10; i++) {
2564 ExpectedResult expected;
2565
2566 if (i == 0) {
2567 fillTransaction(transaction, &callback, layer);
2568 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2569 } else {
2570 fillTransaction(transaction, &callback);
2571 }
2572
2573 transaction.apply();
2574
2575 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2576 }
2577 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2578}
2579
2580TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
2581 sp<SurfaceControl> layer;
2582 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2583
2584 Transaction transaction;
2585 CallbackHelper callback;
2586 for (size_t i = 0; i < 10; i++) {
2587 if (i == 0) {
2588 fillTransaction(transaction, &callback, layer);
2589 } else {
2590 fillTransaction(transaction, &callback);
2591 }
2592
2593 transaction.setPosition(layer, mWidth, mHeight).apply();
2594
2595 ExpectedResult expected;
2596 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
2597 : ExpectedResult::Transaction::NOT_PRESENTED,
2598 layer);
2599 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
2600 }
2601 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2602}
2603
2604TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
2605 sp<SurfaceControl> layer1, layer2;
2606 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2607 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2608
2609 Transaction transaction1, transaction2;
2610 CallbackHelper callback1, callback2;
2611 for (size_t i = 0; i < 10; i++) {
2612 fillTransaction(transaction1, &callback1, layer1);
2613 fillTransaction(transaction2, &callback2, layer2);
2614
2615 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2616
2617 ExpectedResult expected;
2618 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2619 ExpectedResult::Buffer::NOT_ACQUIRED,
2620 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2621 : ExpectedResult::PreviousBuffer::RELEASED);
2622 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2623 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2624 }
2625 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2626 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2627}
2628
2629TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
2630 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2631 client2(new SurfaceComposerClient);
2632 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2633 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2634
2635 sp<SurfaceControl> layer1, layer2;
2636 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2637 ISurfaceComposerClient::eFXSurfaceBufferState));
2638 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2639 ISurfaceComposerClient::eFXSurfaceBufferState));
2640
2641 Transaction transaction1, transaction2;
2642 CallbackHelper callback1, callback2;
2643 for (size_t i = 0; i < 10; i++) {
2644 fillTransaction(transaction1, &callback1, layer1);
2645 fillTransaction(transaction2, &callback2, layer2);
2646
2647 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2648
2649 ExpectedResult expected;
2650 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2651 ExpectedResult::Buffer::NOT_ACQUIRED,
2652 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2653 : ExpectedResult::PreviousBuffer::RELEASED);
2654 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2655 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2656 }
2657 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2658 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2659}
2660
2661TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
2662 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2663 client2(new SurfaceComposerClient);
2664 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2665 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2666
2667 sp<SurfaceControl> layer1, layer2;
2668 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2669 ISurfaceComposerClient::eFXSurfaceBufferState));
2670 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2671 ISurfaceComposerClient::eFXSurfaceBufferState));
2672
2673 Transaction transaction1, transaction2;
2674 CallbackHelper callback1, callback2;
2675
2676 // Normal call to set up test
2677 fillTransaction(transaction1, &callback1, layer1);
2678 fillTransaction(transaction2, &callback2, layer2);
2679
2680 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2681
2682 ExpectedResult expected;
2683 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2684 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2685 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2686 expected.reset();
2687
2688 // Test
2689 fillTransaction(transaction1, &callback1);
2690 fillTransaction(transaction2, &callback2);
2691
2692 transaction2.merge(std::move(transaction1)).apply();
2693
2694 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2695 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2696}
2697
2698TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
2699 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2700 client2(new SurfaceComposerClient);
2701
2702 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2703 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2704
2705 sp<SurfaceControl> layer1, layer2;
2706 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2707 ISurfaceComposerClient::eFXSurfaceBufferState));
2708 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2709 ISurfaceComposerClient::eFXSurfaceBufferState));
2710
2711 Transaction transaction1, transaction2;
2712 CallbackHelper callback1, callback2;
2713
2714 // Normal call to set up test
2715 fillTransaction(transaction1, &callback1, layer1);
2716 fillTransaction(transaction2, &callback2, layer2);
2717
2718 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2719
2720 ExpectedResult expected;
2721 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2722 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2723 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2724 expected.reset();
2725
2726 // Test
2727 fillTransaction(transaction1, &callback1);
2728 fillTransaction(transaction2, &callback2);
2729
2730 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2731
2732 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2);
2733 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2734 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2735}
2736
2737TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
2738 sp<SurfaceControl> layer;
2739 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2740
2741 Transaction transaction;
2742 CallbackHelper callback;
2743 std::vector<ExpectedResult> expectedResults(50);
2744 ExpectedResult::PreviousBuffer previousBufferResult =
2745 ExpectedResult::PreviousBuffer::NOT_RELEASED;
2746 for (auto& expected : expectedResults) {
2747 expected.reset();
2748 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2749 ExpectedResult::Buffer::NOT_ACQUIRED, previousBufferResult);
2750 previousBufferResult = ExpectedResult::PreviousBuffer::RELEASED;
2751
2752 fillTransaction(transaction, &callback, layer);
2753
2754 transaction.apply();
2755 std::this_thread::sleep_for(200ms);
2756 }
2757 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2758}
2759
2760TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
2761 sp<SurfaceControl> layer;
2762 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2763
2764 Transaction transaction;
2765 CallbackHelper callback;
2766 std::vector<ExpectedResult> expectedResults(50);
2767 bool first = true;
2768 for (auto& expected : expectedResults) {
2769 expected.reset();
2770
2771 if (first) {
2772 fillTransaction(transaction, &callback, layer);
2773 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2774 first = false;
2775 } else {
2776 fillTransaction(transaction, &callback);
2777 }
2778
2779 transaction.apply();
2780 std::this_thread::sleep_for(200ms);
2781 }
2782 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2783}
2784
2785TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
2786 sp<SurfaceControl> layer;
2787 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2788
2789 // Normal call to set up test
2790 Transaction transaction;
2791 CallbackHelper callback;
2792 fillTransaction(transaction, &callback, layer);
2793
2794 transaction.setPosition(layer, mWidth, mHeight).apply();
2795
2796 ExpectedResult expectedResult;
2797 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2798 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
2799
2800 // Test
2801 std::vector<ExpectedResult> expectedResults(50);
2802 for (auto& expected : expectedResults) {
2803 expected.reset();
2804 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2805
2806 fillTransaction(transaction, &callback);
2807
2808 transaction.setPosition(layer, mWidth, mHeight).apply();
2809
2810 std::this_thread::sleep_for(200ms);
2811 }
2812 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2813}
2814
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002815class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002816protected:
2817 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002818 LayerTransactionTest::SetUp();
2819 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002820
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002821 sp<IBinder> display(
2822 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002823 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002824 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002825
2826 ssize_t displayWidth = info.w;
2827 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002828
2829 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002830 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2831 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002832 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002833 ASSERT_TRUE(mBGSurfaceControl->isValid());
2834 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2835
2836 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002837 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2838
Peiyong Lin566a3b42018-01-09 18:22:43 -08002839 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002840 ASSERT_TRUE(mFGSurfaceControl->isValid());
2841
2842 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2843
2844 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002845 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002846 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002847 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2848
2849 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2850
Robert Carr4cdc58f2017-08-23 14:22:20 -07002851 asTransaction([&](Transaction& t) {
2852 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002853
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002854 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002855
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002856 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2857 .setPosition(mFGSurfaceControl, 64, 64)
2858 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002859
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002860 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2861 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2862 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002863 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002864 }
2865
2866 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002867 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002868 mBGSurfaceControl = 0;
2869 mFGSurfaceControl = 0;
2870 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002871 }
2872
2873 void waitForPostedBuffers() {
2874 // Since the sync surface is in synchronous mode (i.e. double buffered)
2875 // posting three buffers to it should ensure that at least two
2876 // SurfaceFlinger::handlePageFlip calls have been made, which should
2877 // guaranteed that a buffer posted to another Surface has been retired.
2878 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2879 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2880 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2881 }
2882
Robert Carr4cdc58f2017-08-23 14:22:20 -07002883 void asTransaction(const std::function<void(Transaction&)>& exec) {
2884 Transaction t;
2885 exec(t);
2886 t.apply(true);
2887 }
2888
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002889 sp<SurfaceControl> mBGSurfaceControl;
2890 sp<SurfaceControl> mFGSurfaceControl;
2891
2892 // This surface is used to ensure that the buffers posted to
2893 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2894 sp<SurfaceControl> mSyncSurfaceControl;
2895};
2896
Robert Carr7f619b22017-11-06 12:56:35 -08002897TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08002898
chaviw0e3479f2018-09-10 16:49:30 -07002899 std::unique_ptr<ScreenCapture> sc;
2900
2901 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002902 fillSurfaceRGBA8(relative, 10, 10, 10);
2903 waitForPostedBuffers();
2904
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002905 Transaction{}
2906 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002907 .setPosition(relative, 64, 64)
2908 .apply();
2909
2910 {
2911 // The relative should be on top of the FG control.
2912 ScreenCapture::captureScreen(&sc);
2913 sc->checkPixel(64, 64, 10, 10, 10);
2914 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002915 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002916
2917 {
2918 // Nothing should change at this point.
2919 ScreenCapture::captureScreen(&sc);
2920 sc->checkPixel(64, 64, 10, 10, 10);
2921 }
2922
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002923 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002924
2925 {
2926 // Ensure that the relative was actually hidden, rather than
2927 // being left in the detached but visible state.
2928 ScreenCapture::captureScreen(&sc);
2929 sc->expectFGColor(64, 64);
2930 }
2931}
2932
Robert Carr8d5227b2017-03-16 15:41:03 -07002933class GeometryLatchingTest : public LayerUpdateTest {
2934protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002935 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002936 SCOPED_TRACE(trace);
2937 ScreenCapture::captureScreen(&sc);
2938 // We find the leading edge of the FG surface.
2939 sc->expectFGColor(127, 127);
2940 sc->expectBGColor(128, 128);
2941 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002942
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002943 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002944
2945 void unlockFGBuffer() {
2946 sp<Surface> s = mFGSurfaceControl->getSurface();
2947 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2948 waitForPostedBuffers();
2949 }
2950
Robert Carr8d5227b2017-03-16 15:41:03 -07002951 void completeFGResize() {
2952 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2953 waitForPostedBuffers();
2954 }
2955 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002956 asTransaction([&](Transaction& t) {
2957 t.setSize(mFGSurfaceControl, 64, 64);
2958 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002959 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002960 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002961
2962 EXPECT_INITIAL_STATE("After restoring initial state");
2963 }
chaviw0e3479f2018-09-10 16:49:30 -07002964 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07002965};
2966
Robert Carr8d5227b2017-03-16 15:41:03 -07002967class CropLatchingTest : public GeometryLatchingTest {
2968protected:
2969 void EXPECT_CROPPED_STATE(const char* trace) {
2970 SCOPED_TRACE(trace);
2971 ScreenCapture::captureScreen(&sc);
2972 // The edge should be moved back one pixel by our crop.
2973 sc->expectFGColor(126, 126);
2974 sc->expectBGColor(127, 127);
2975 sc->expectBGColor(128, 128);
2976 }
chaviw59f5c562017-06-28 16:39:06 -07002977
2978 void EXPECT_RESIZE_STATE(const char* trace) {
2979 SCOPED_TRACE(trace);
2980 ScreenCapture::captureScreen(&sc);
2981 // The FG is now resized too 128,128 at 64,64
2982 sc->expectFGColor(64, 64);
2983 sc->expectFGColor(191, 191);
2984 sc->expectBGColor(192, 192);
2985 }
Robert Carr8d5227b2017-03-16 15:41:03 -07002986};
2987
Pablo Ceballos05289c22016-04-14 15:49:55 -07002988TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07002989 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07002990 {
2991 SCOPED_TRACE("before anything");
2992 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002993 sc->expectBGColor(32, 32);
2994 sc->expectFGColor(96, 96);
2995 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002996 }
2997
2998 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002999 asTransaction([&](Transaction& t) {
3000 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003001 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3002 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003003 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003004
Robert Carr4cdc58f2017-08-23 14:22:20 -07003005 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003006 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003007 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3008 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003009 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003010
3011 {
3012 SCOPED_TRACE("before any trigger");
3013 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003014 sc->expectBGColor(32, 32);
3015 sc->expectFGColor(96, 96);
3016 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003017 }
3018
3019 // should trigger the first deferred transaction, but not the second one
3020 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3021 {
3022 SCOPED_TRACE("after first trigger");
3023 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003024 sc->expectBGColor(32, 32);
3025 sc->checkPixel(96, 96, 162, 63, 96);
3026 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003027 }
3028
3029 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003030 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003031
3032 // trigger the second deferred transaction
3033 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3034 {
3035 SCOPED_TRACE("after second trigger");
3036 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003037 sc->expectBGColor(32, 32);
3038 sc->expectBGColor(96, 96);
3039 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003040 }
3041}
3042
Robert Carre392b552017-09-19 12:16:05 -07003043TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003044 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003045
3046 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003047 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3048 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3049 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3050 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003051 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003052 SurfaceComposerClient::Transaction{}
3053 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3054 .show(childNoBuffer)
3055 .show(childBuffer)
3056 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003057 {
3058 ScreenCapture::captureScreen(&sc);
3059 sc->expectChildColor(73, 73);
3060 sc->expectFGColor(74, 74);
3061 }
Vishnu Nair60356342018-11-13 13:00:45 -08003062 SurfaceComposerClient::Transaction{}
3063 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3064 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003065 {
3066 ScreenCapture::captureScreen(&sc);
3067 sc->expectChildColor(73, 73);
3068 sc->expectChildColor(74, 74);
3069 }
3070}
3071
Robert Carr2c5f6d22017-09-26 12:30:35 -07003072TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003073 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003074 {
3075 SCOPED_TRACE("before move");
3076 ScreenCapture::captureScreen(&sc);
3077 sc->expectBGColor(0, 12);
3078 sc->expectFGColor(75, 75);
3079 sc->expectBGColor(145, 145);
3080 }
3081
3082 Transaction t1, t2;
3083 t1.setPosition(mFGSurfaceControl, 128, 128);
3084 t2.setPosition(mFGSurfaceControl, 0, 0);
3085 // We expect that the position update from t2 now
3086 // overwrites the position update from t1.
3087 t1.merge(std::move(t2));
3088 t1.apply();
3089
3090 {
3091 ScreenCapture::captureScreen(&sc);
3092 sc->expectFGColor(1, 1);
3093 }
3094}
3095
Robert Carr1f0a16a2016-10-24 16:27:39 -07003096class ChildLayerTest : public LayerUpdateTest {
3097protected:
3098 void SetUp() override {
3099 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003100 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3101 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003102 fillSurfaceRGBA8(mChild, 200, 200, 200);
3103
3104 {
3105 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003106 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003107 mCapture->expectChildColor(64, 64);
3108 }
3109 }
3110 void TearDown() override {
3111 LayerUpdateTest::TearDown();
3112 mChild = 0;
3113 }
3114
3115 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003116 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003117};
3118
3119TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003120 asTransaction([&](Transaction& t) {
3121 t.show(mChild);
3122 t.setPosition(mChild, 10, 10);
3123 t.setPosition(mFGSurfaceControl, 64, 64);
3124 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003125
3126 {
chaviw0e3479f2018-09-10 16:49:30 -07003127 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003128 // Top left of foreground must now be visible
3129 mCapture->expectFGColor(64, 64);
3130 // But 10 pixels in we should see the child surface
3131 mCapture->expectChildColor(74, 74);
3132 // And 10 more pixels we should be back to the foreground surface
3133 mCapture->expectFGColor(84, 84);
3134 }
3135
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003136 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003137
3138 {
chaviw0e3479f2018-09-10 16:49:30 -07003139 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003140 // Top left of foreground should now be at 0, 0
3141 mCapture->expectFGColor(0, 0);
3142 // But 10 pixels in we should see the child surface
3143 mCapture->expectChildColor(10, 10);
3144 // And 10 more pixels we should be back to the foreground surface
3145 mCapture->expectFGColor(20, 20);
3146 }
3147}
3148
Robert Carr41b08b52017-06-01 16:11:34 -07003149TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003150 asTransaction([&](Transaction& t) {
3151 t.show(mChild);
3152 t.setPosition(mChild, 0, 0);
3153 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003154 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003155 });
Robert Carr41b08b52017-06-01 16:11:34 -07003156
3157 {
chaviw0e3479f2018-09-10 16:49:30 -07003158 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003159 mCapture->expectChildColor(0, 0);
3160 mCapture->expectChildColor(4, 4);
3161 mCapture->expectBGColor(5, 5);
3162 }
3163}
3164
Robert Carr1f0a16a2016-10-24 16:27:39 -07003165TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003166 asTransaction([&](Transaction& t) {
3167 t.show(mChild);
3168 t.setPosition(mFGSurfaceControl, 0, 0);
3169 t.setPosition(mChild, 63, 63);
3170 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003171
3172 {
chaviw0e3479f2018-09-10 16:49:30 -07003173 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003174 mCapture->expectFGColor(0, 0);
3175 // Last pixel in foreground should now be the child.
3176 mCapture->expectChildColor(63, 63);
3177 // But the child should be constrained and the next pixel
3178 // must be the background
3179 mCapture->expectBGColor(64, 64);
3180 }
3181}
3182
3183TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003184 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003185
3186 // Find the boundary between the parent and child
3187 {
chaviw0e3479f2018-09-10 16:49:30 -07003188 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003189 mCapture->expectChildColor(9, 9);
3190 mCapture->expectFGColor(10, 10);
3191 }
3192
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003193 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003194
3195 // The boundary should be twice as far from the origin now.
3196 // The pixels from the last test should all be child now
3197 {
chaviw0e3479f2018-09-10 16:49:30 -07003198 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003199 mCapture->expectChildColor(9, 9);
3200 mCapture->expectChildColor(10, 10);
3201 mCapture->expectChildColor(19, 19);
3202 mCapture->expectFGColor(20, 20);
3203 }
3204}
Robert Carr9524cb32017-02-13 11:32:32 -08003205
Robert Carr6452f122017-03-21 10:41:29 -07003206TEST_F(ChildLayerTest, ChildLayerAlpha) {
3207 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3208 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3209 fillSurfaceRGBA8(mChild, 0, 254, 0);
3210 waitForPostedBuffers();
3211
Robert Carr4cdc58f2017-08-23 14:22:20 -07003212 asTransaction([&](Transaction& t) {
3213 t.show(mChild);
3214 t.setPosition(mChild, 0, 0);
3215 t.setPosition(mFGSurfaceControl, 0, 0);
3216 });
Robert Carr6452f122017-03-21 10:41:29 -07003217
3218 {
chaviw0e3479f2018-09-10 16:49:30 -07003219 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003220 // Unblended child color
3221 mCapture->checkPixel(0, 0, 0, 254, 0);
3222 }
3223
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003224 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003225
3226 {
chaviw0e3479f2018-09-10 16:49:30 -07003227 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003228 // Child and BG blended.
3229 mCapture->checkPixel(0, 0, 127, 127, 0);
3230 }
3231
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003232 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003233
3234 {
chaviw0e3479f2018-09-10 16:49:30 -07003235 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003236 // Child and BG blended.
3237 mCapture->checkPixel(0, 0, 95, 64, 95);
3238 }
3239}
3240
Robert Carr9524cb32017-02-13 11:32:32 -08003241TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003242 asTransaction([&](Transaction& t) {
3243 t.show(mChild);
3244 t.setPosition(mChild, 10, 10);
3245 t.setPosition(mFGSurfaceControl, 64, 64);
3246 });
Robert Carr9524cb32017-02-13 11:32:32 -08003247
3248 {
chaviw0e3479f2018-09-10 16:49:30 -07003249 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003250 // Top left of foreground must now be visible
3251 mCapture->expectFGColor(64, 64);
3252 // But 10 pixels in we should see the child surface
3253 mCapture->expectChildColor(74, 74);
3254 // And 10 more pixels we should be back to the foreground surface
3255 mCapture->expectFGColor(84, 84);
3256 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003257
3258 asTransaction([&](Transaction& t) {
3259 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
3260 });
3261
Robert Carr9524cb32017-02-13 11:32:32 -08003262 {
chaviw0e3479f2018-09-10 16:49:30 -07003263 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003264 mCapture->expectFGColor(64, 64);
3265 // In reparenting we should have exposed the entire foreground surface.
3266 mCapture->expectFGColor(74, 74);
3267 // And the child layer should now begin at 10, 10 (since the BG
3268 // layer is at (0, 0)).
3269 mCapture->expectBGColor(9, 9);
3270 mCapture->expectChildColor(10, 10);
3271 }
3272}
3273
Robert Carr2e102c92018-10-23 12:11:15 -07003274TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
3275 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003276 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07003277 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
3278
3279 {
3280 SCOPED_TRACE("Grandchild visible");
3281 ScreenCapture::captureScreen(&mCapture);
3282 mCapture->checkPixel(64, 64, 111, 111, 111);
3283 }
3284
3285 mChild->clear();
3286
3287 {
3288 SCOPED_TRACE("After destroying child");
3289 ScreenCapture::captureScreen(&mCapture);
3290 mCapture->expectFGColor(64, 64);
3291 }
3292
3293 asTransaction([&](Transaction& t) {
3294 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
3295 });
3296
3297 {
3298 SCOPED_TRACE("After reparenting grandchild");
3299 ScreenCapture::captureScreen(&mCapture);
3300 mCapture->checkPixel(64, 64, 111, 111, 111);
3301 }
3302}
3303
chaviw161410b02017-07-27 10:46:08 -07003304TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003305 asTransaction([&](Transaction& t) {
3306 t.show(mChild);
3307 t.setPosition(mChild, 10, 10);
3308 t.setPosition(mFGSurfaceControl, 64, 64);
3309 });
Robert Carr9524cb32017-02-13 11:32:32 -08003310
3311 {
chaviw0e3479f2018-09-10 16:49:30 -07003312 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003313 // Top left of foreground must now be visible
3314 mCapture->expectFGColor(64, 64);
3315 // But 10 pixels in we should see the child surface
3316 mCapture->expectChildColor(74, 74);
3317 // And 10 more pixels we should be back to the foreground surface
3318 mCapture->expectFGColor(84, 84);
3319 }
3320
chaviw0e3479f2018-09-10 16:49:30 -07003321
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003322 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08003323
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003324 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08003325
chaviw161410b02017-07-27 10:46:08 -07003326 // Since the child has the same client as the parent, it will not get
3327 // detached and will be hidden.
3328 {
chaviw0e3479f2018-09-10 16:49:30 -07003329 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003330 mCapture->expectFGColor(64, 64);
3331 mCapture->expectFGColor(74, 74);
3332 mCapture->expectFGColor(84, 84);
3333 }
3334}
3335
3336TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
3337 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003338 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003339 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
3340 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07003341
chaviw161410b02017-07-27 10:46:08 -07003342 ASSERT_TRUE(mChildNewClient->isValid());
3343
3344 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
3345
Robert Carr4cdc58f2017-08-23 14:22:20 -07003346 asTransaction([&](Transaction& t) {
3347 t.hide(mChild);
3348 t.show(mChildNewClient);
3349 t.setPosition(mChildNewClient, 10, 10);
3350 t.setPosition(mFGSurfaceControl, 64, 64);
3351 });
chaviw161410b02017-07-27 10:46:08 -07003352
3353 {
chaviw0e3479f2018-09-10 16:49:30 -07003354 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003355 // Top left of foreground must now be visible
3356 mCapture->expectFGColor(64, 64);
3357 // But 10 pixels in we should see the child surface
3358 mCapture->expectChildColor(74, 74);
3359 // And 10 more pixels we should be back to the foreground surface
3360 mCapture->expectFGColor(84, 84);
3361 }
3362
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003363 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07003364
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003365 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07003366
Robert Carr9524cb32017-02-13 11:32:32 -08003367 // Nothing should have changed.
3368 {
chaviw0e3479f2018-09-10 16:49:30 -07003369 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003370 mCapture->expectFGColor(64, 64);
3371 mCapture->expectChildColor(74, 74);
3372 mCapture->expectFGColor(84, 84);
3373 }
3374}
3375
chaviw5aedec92018-10-22 10:40:38 -07003376TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
3377 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
3378 sp<SurfaceControl> childNewClient =
3379 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
3380 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3381
3382 ASSERT_TRUE(childNewClient != nullptr);
3383 ASSERT_TRUE(childNewClient->isValid());
3384
3385 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
3386
3387 Transaction()
3388 .hide(mChild)
3389 .show(childNewClient)
3390 .setPosition(childNewClient, 10, 10)
3391 .setPosition(mFGSurfaceControl, 64, 64)
3392 .apply();
3393
3394 {
3395 mCapture = screenshot();
3396 // Top left of foreground must now be visible
3397 mCapture->expectFGColor(64, 64);
3398 // But 10 pixels in we should see the child surface
3399 mCapture->expectChildColor(74, 74);
3400 // And 10 more pixels we should be back to the foreground surface
3401 mCapture->expectFGColor(84, 84);
3402 }
3403
3404 Transaction().detachChildren(mFGSurfaceControl).apply();
3405 Transaction().hide(childNewClient).apply();
3406
3407 // Nothing should have changed.
3408 {
3409 mCapture = screenshot();
3410 mCapture->expectFGColor(64, 64);
3411 mCapture->expectChildColor(74, 74);
3412 mCapture->expectFGColor(84, 84);
3413 }
3414
3415 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
3416 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
3417 32);
3418 Transaction()
3419 .setLayer(newParentSurface, INT32_MAX - 1)
3420 .show(newParentSurface)
3421 .setPosition(newParentSurface, 20, 20)
3422 .reparent(childNewClient, newParentSurface->getHandle())
3423 .apply();
3424 {
3425 mCapture = screenshot();
3426 // Child is now hidden.
3427 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
3428 }
3429}
3430
Robert Carr9b429f42017-04-17 14:56:57 -07003431TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003432 asTransaction([&](Transaction& t) {
3433 t.show(mChild);
3434 t.setPosition(mChild, 0, 0);
3435 t.setPosition(mFGSurfaceControl, 0, 0);
3436 });
Robert Carr9b429f42017-04-17 14:56:57 -07003437
3438 {
chaviw0e3479f2018-09-10 16:49:30 -07003439 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003440 // We've positioned the child in the top left.
3441 mCapture->expectChildColor(0, 0);
3442 // But it's only 10x10.
3443 mCapture->expectFGColor(10, 10);
3444 }
3445
Robert Carr4cdc58f2017-08-23 14:22:20 -07003446 asTransaction([&](Transaction& t) {
3447 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3448 // We cause scaling by 2.
3449 t.setSize(mFGSurfaceControl, 128, 128);
3450 });
Robert Carr9b429f42017-04-17 14:56:57 -07003451
3452 {
chaviw0e3479f2018-09-10 16:49:30 -07003453 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003454 // We've positioned the child in the top left.
3455 mCapture->expectChildColor(0, 0);
3456 mCapture->expectChildColor(10, 10);
3457 mCapture->expectChildColor(19, 19);
3458 // And now it should be scaled all the way to 20x20
3459 mCapture->expectFGColor(20, 20);
3460 }
3461}
3462
Robert Carr1725eee2017-04-26 18:32:15 -07003463// Regression test for b/37673612
3464TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003465 asTransaction([&](Transaction& t) {
3466 t.show(mChild);
3467 t.setPosition(mChild, 0, 0);
3468 t.setPosition(mFGSurfaceControl, 0, 0);
3469 });
Robert Carr1725eee2017-04-26 18:32:15 -07003470
3471 {
chaviw0e3479f2018-09-10 16:49:30 -07003472 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003473 // We've positioned the child in the top left.
3474 mCapture->expectChildColor(0, 0);
3475 // But it's only 10x10.
3476 mCapture->expectFGColor(10, 10);
3477 }
Robert Carr1725eee2017-04-26 18:32:15 -07003478 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
3479 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003480 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07003481 sp<Surface> s = mFGSurfaceControl->getSurface();
3482 auto anw = static_cast<ANativeWindow*>(s.get());
3483 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
3484 native_window_set_buffers_dimensions(anw, 64, 128);
3485 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3486 waitForPostedBuffers();
3487
3488 {
3489 // The child should still be in the same place and not have any strange scaling as in
3490 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07003491 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003492 mCapture->expectChildColor(0, 0);
3493 mCapture->expectFGColor(10, 10);
3494 }
3495}
3496
Dan Stoza412903f2017-04-27 13:42:17 -07003497TEST_F(ChildLayerTest, Bug36858924) {
3498 // Destroy the child layer
3499 mChild.clear();
3500
3501 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08003502 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
3503 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07003504
3505 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07003506 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003507 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
3508 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003509 t.show(mChild);
3510 });
Dan Stoza412903f2017-04-27 13:42:17 -07003511
3512 // Render the foreground surface a few times
3513 //
3514 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
3515 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
3516 // never acquire/release the first buffer
3517 ALOGI("Filling 1");
3518 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3519 ALOGI("Filling 2");
3520 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
3521 ALOGI("Filling 3");
3522 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
3523 ALOGI("Filling 4");
3524 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3525}
3526
chaviwf1961f72017-09-18 16:41:07 -07003527TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003528 asTransaction([&](Transaction& t) {
3529 t.show(mChild);
3530 t.setPosition(mChild, 10, 10);
3531 t.setPosition(mFGSurfaceControl, 64, 64);
3532 });
chaviw06178942017-07-27 10:25:59 -07003533
3534 {
chaviw0e3479f2018-09-10 16:49:30 -07003535 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003536 // Top left of foreground must now be visible
3537 mCapture->expectFGColor(64, 64);
3538 // But 10 pixels in we should see the child surface
3539 mCapture->expectChildColor(74, 74);
3540 // And 10 more pixels we should be back to the foreground surface
3541 mCapture->expectFGColor(84, 84);
3542 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003543
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003544 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07003545
chaviw06178942017-07-27 10:25:59 -07003546 {
chaviw0e3479f2018-09-10 16:49:30 -07003547 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003548 mCapture->expectFGColor(64, 64);
3549 // In reparenting we should have exposed the entire foreground surface.
3550 mCapture->expectFGColor(74, 74);
3551 // And the child layer should now begin at 10, 10 (since the BG
3552 // layer is at (0, 0)).
3553 mCapture->expectBGColor(9, 9);
3554 mCapture->expectChildColor(10, 10);
3555 }
3556}
3557
chaviwf1961f72017-09-18 16:41:07 -07003558TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003559 asTransaction([&](Transaction& t) {
3560 t.show(mChild);
3561 t.setPosition(mChild, 10, 10);
3562 t.setPosition(mFGSurfaceControl, 64, 64);
3563 });
chaviwf1961f72017-09-18 16:41:07 -07003564
3565 {
chaviw0e3479f2018-09-10 16:49:30 -07003566 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003567 // Top left of foreground must now be visible
3568 mCapture->expectFGColor(64, 64);
3569 // But 10 pixels in we should see the child surface
3570 mCapture->expectChildColor(74, 74);
3571 // And 10 more pixels we should be back to the foreground surface
3572 mCapture->expectFGColor(84, 84);
3573 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003574 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07003575 {
chaviw0e3479f2018-09-10 16:49:30 -07003576 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003577 // Nothing should have changed.
3578 mCapture->expectFGColor(64, 64);
3579 mCapture->expectChildColor(74, 74);
3580 mCapture->expectFGColor(84, 84);
3581 }
3582}
3583
3584TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07003585 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003586 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07003587 ASSERT_TRUE(newSurface->isValid());
3588
3589 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003590 asTransaction([&](Transaction& t) {
3591 t.hide(mChild);
3592 t.show(newSurface);
3593 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003594 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003595 t.setPosition(mFGSurfaceControl, 64, 64);
3596 });
chaviwf1961f72017-09-18 16:41:07 -07003597
3598 {
chaviw0e3479f2018-09-10 16:49:30 -07003599 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003600 // Top left of foreground must now be visible
3601 mCapture->expectFGColor(64, 64);
3602 // At 10, 10 we should see the new surface
3603 mCapture->checkPixel(10, 10, 63, 195, 63);
3604 }
3605
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003606 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07003607
3608 {
chaviw0e3479f2018-09-10 16:49:30 -07003609 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003610 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
3611 // mFGSurface, putting it at 74, 74.
3612 mCapture->expectFGColor(64, 64);
3613 mCapture->checkPixel(74, 74, 63, 195, 63);
3614 mCapture->expectFGColor(84, 84);
3615 }
3616}
3617
chaviwc9674332017-08-28 12:32:18 -07003618TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003619 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
3620 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07003621 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3622
3623 {
chaviw0e3479f2018-09-10 16:49:30 -07003624 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07003625 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
3626 // which begins at 64, 64
3627 mCapture->checkPixel(64, 64, 50, 50, 50);
3628 }
3629}
3630
Robert Carr503c7042017-09-27 15:06:08 -07003631TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003632 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07003633 fillSurfaceRGBA8(relative, 255, 255, 255);
3634
3635 Transaction t;
3636 t.setLayer(relative, INT32_MAX)
3637 .setRelativeLayer(mChild, relative->getHandle(), 1)
3638 .setPosition(mFGSurfaceControl, 0, 0)
3639 .apply(true);
3640
3641 // We expect that the child should have been elevated above our
3642 // INT_MAX layer even though it's not a child of it.
3643 {
chaviw0e3479f2018-09-10 16:49:30 -07003644 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07003645 mCapture->expectChildColor(0, 0);
3646 mCapture->expectChildColor(9, 9);
3647 mCapture->checkPixel(10, 10, 255, 255, 255);
3648 }
3649}
Vishnu Nair60356342018-11-13 13:00:45 -08003650class BoundlessLayerTest : public LayerUpdateTest {
3651protected:
3652 std::unique_ptr<ScreenCapture> mCapture;
3653};
3654
3655// Verify setting a size on a buffer layer has no effect.
3656TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
3657 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003658 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
3659 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003660 ASSERT_TRUE(bufferLayer->isValid());
3661 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
3662 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
3663 {
3664 mCapture = screenshot();
3665 // Top left of background must now be visible
3666 mCapture->expectBGColor(0, 0);
3667 // Foreground Surface bounds must be color layer
3668 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
3669 // Buffer layer should not extend past buffer bounds
3670 mCapture->expectFGColor(95, 95);
3671 }
3672}
3673
3674// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
3675// which will crop the color layer.
3676TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
3677 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003678 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3679 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003680 ASSERT_TRUE(colorLayer->isValid());
3681 asTransaction([&](Transaction& t) {
3682 t.setColor(colorLayer, half3{0, 0, 0});
3683 t.show(colorLayer);
3684 });
3685 {
3686 mCapture = screenshot();
3687 // Top left of background must now be visible
3688 mCapture->expectBGColor(0, 0);
3689 // Foreground Surface bounds must be color layer
3690 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3691 // Color layer should not extend past foreground bounds
3692 mCapture->expectBGColor(129, 129);
3693 }
3694}
3695
3696// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
3697// a crop which will be used to crop the color layer.
3698TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003699 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3700 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003701 ASSERT_TRUE(cropLayer->isValid());
3702 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003703 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3704 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003705 ASSERT_TRUE(colorLayer->isValid());
3706 asTransaction([&](Transaction& t) {
3707 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
3708 t.setColor(colorLayer, half3{0, 0, 0});
3709 t.show(cropLayer);
3710 t.show(colorLayer);
3711 });
3712 {
3713 mCapture = screenshot();
3714 // Top left of background must now be visible
3715 mCapture->expectBGColor(0, 0);
3716 // Top left of foreground must now be visible
3717 mCapture->expectFGColor(64, 64);
3718 // 5 pixels from the foreground we should see the child surface
3719 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
3720 // 10 pixels from the foreground we should be back to the foreground surface
3721 mCapture->expectFGColor(74, 74);
3722 }
3723}
3724
3725// Verify for boundless layer with no children, their transforms have no effect.
3726TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
3727 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003728 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3729 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003730 ASSERT_TRUE(colorLayer->isValid());
3731 asTransaction([&](Transaction& t) {
3732 t.setPosition(colorLayer, 320, 320);
3733 t.setMatrix(colorLayer, 2, 0, 0, 2);
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 for boundless layer with children, their transforms have an effect.
3749TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
3750 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003751 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3752 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003753 ASSERT_TRUE(boundlessLayerRightShift->isValid());
3754 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003755 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3756 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003757 ASSERT_TRUE(boundlessLayerDownShift->isValid());
3758 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003759 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3760 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003761 ASSERT_TRUE(colorLayer->isValid());
3762 asTransaction([&](Transaction& t) {
3763 t.setPosition(boundlessLayerRightShift, 32, 0);
3764 t.show(boundlessLayerRightShift);
3765 t.setPosition(boundlessLayerDownShift, 0, 32);
3766 t.show(boundlessLayerDownShift);
3767 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3768 t.setColor(colorLayer, half3{0, 0, 0});
3769 t.show(colorLayer);
3770 });
3771 {
3772 mCapture = screenshot();
3773 // Top left of background must now be visible
3774 mCapture->expectBGColor(0, 0);
3775 // Top left of foreground must now be visible
3776 mCapture->expectFGColor(64, 64);
3777 // Foreground Surface bounds must be color layer
3778 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
3779 // Color layer should not extend past foreground bounds
3780 mCapture->expectBGColor(129, 129);
3781 }
3782}
3783
3784// Verify child layers do not get clipped if they temporarily move into the negative
3785// coordinate space as the result of an intermediate transformation.
3786TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
3787 sp<SurfaceControl> boundlessLayer =
3788 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3789 0 /* flags */, mFGSurfaceControl.get());
3790 ASSERT_TRUE(boundlessLayer != nullptr);
3791 ASSERT_TRUE(boundlessLayer->isValid());
3792 sp<SurfaceControl> colorLayer =
3793 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3794 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
3795 ASSERT_TRUE(colorLayer != nullptr);
3796 ASSERT_TRUE(colorLayer->isValid());
3797 asTransaction([&](Transaction& t) {
3798 // shift child layer off bounds. If this layer was not boundless, we will
3799 // expect the child layer to be cropped.
3800 t.setPosition(boundlessLayer, 32, 32);
3801 t.show(boundlessLayer);
3802 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3803 // undo shift by parent
3804 t.setPosition(colorLayer, -32, -32);
3805 t.setColor(colorLayer, half3{0, 0, 0});
3806 t.show(colorLayer);
3807 });
3808 {
3809 mCapture = screenshot();
3810 // Top left of background must now be visible
3811 mCapture->expectBGColor(0, 0);
3812 // Foreground Surface bounds must be color layer
3813 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3814 // Color layer should not extend past foreground bounds
3815 mCapture->expectBGColor(129, 129);
3816 }
3817}
3818
3819// Verify for boundless root layers with children, their transforms have an effect.
3820TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003821 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
3822 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08003823 ASSERT_TRUE(rootBoundlessLayer->isValid());
3824 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003825 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3826 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
3827
Vishnu Nair60356342018-11-13 13:00:45 -08003828 ASSERT_TRUE(colorLayer->isValid());
3829 asTransaction([&](Transaction& t) {
3830 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
3831 t.setPosition(rootBoundlessLayer, 32, 32);
3832 t.show(rootBoundlessLayer);
3833 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3834 t.setColor(colorLayer, half3{0, 0, 0});
3835 t.show(colorLayer);
3836 t.hide(mFGSurfaceControl);
3837 });
3838 {
3839 mCapture = screenshot();
3840 // Top left of background must now be visible
3841 mCapture->expectBGColor(0, 0);
3842 // Top left of foreground must now be visible
3843 mCapture->expectBGColor(31, 31);
3844 // Foreground Surface bounds must be color layer
3845 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
3846 // Color layer should not extend past foreground bounds
3847 mCapture->expectBGColor(97, 97);
3848 }
3849}
Robert Carr503c7042017-09-27 15:06:08 -07003850
chaviwa76b2712017-09-20 12:02:26 -07003851class ScreenCaptureTest : public LayerUpdateTest {
3852protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003853 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07003854};
3855
3856TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
3857 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003858 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003859 mCapture->expectBGColor(0, 0);
3860 // Doesn't capture FG layer which is at 64, 64
3861 mCapture->expectBGColor(64, 64);
3862}
3863
3864TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
3865 auto fgHandle = mFGSurfaceControl->getHandle();
3866
Vishnu Nair88a11f22018-11-28 18:30:57 -08003867 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3868 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003869 fillSurfaceRGBA8(child, 200, 200, 200);
3870
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003871 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003872
3873 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003874 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003875 mCapture->expectFGColor(10, 10);
3876 mCapture->expectChildColor(0, 0);
3877}
3878
Robert Carr578038f2018-03-09 12:25:24 -08003879TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
3880 auto fgHandle = mFGSurfaceControl->getHandle();
3881
Vishnu Nair88a11f22018-11-28 18:30:57 -08003882 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3883 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08003884 fillSurfaceRGBA8(child, 200, 200, 200);
3885
3886 SurfaceComposerClient::Transaction().show(child).apply(true);
3887
3888 // Captures mFGSurfaceControl's child
3889 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3890 mCapture->checkPixel(10, 10, 0, 0, 0);
3891 mCapture->expectChildColor(0, 0);
3892}
3893
chaviw50da5042018-04-09 13:49:37 -07003894TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003895 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3896 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07003897
3898 fillSurfaceRGBA8(child, 200, 200, 200);
3899
3900 SurfaceComposerClient::Transaction().show(child).apply(true);
3901
3902 auto childHandle = child->getHandle();
3903
3904 // Captures child
3905 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
3906 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
3907 // Area outside of child's bounds is transparent.
3908 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
3909}
3910
chaviw4b129c22018-04-09 16:19:43 -07003911TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
3912 auto fgHandle = mFGSurfaceControl->getHandle();
3913
Vishnu Nair88a11f22018-11-28 18:30:57 -08003914 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3915 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3916 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07003917 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07003918 fillSurfaceRGBA8(child, 200, 200, 200);
3919 fillSurfaceRGBA8(relative, 100, 100, 100);
3920
3921 SurfaceComposerClient::Transaction()
3922 .show(child)
3923 // Set relative layer above fg layer so should be shown above when computing all layers.
3924 .setRelativeLayer(relative, fgHandle, 1)
3925 .show(relative)
3926 .apply(true);
3927
3928 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
3929 ScreenCapture::captureLayers(&mCapture, fgHandle);
3930 mCapture->expectFGColor(10, 10);
3931 mCapture->expectChildColor(0, 0);
3932}
3933
3934TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
3935 auto fgHandle = mFGSurfaceControl->getHandle();
3936
Vishnu Nair88a11f22018-11-28 18:30:57 -08003937 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3938 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3939 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
3940 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07003941 fillSurfaceRGBA8(child, 200, 200, 200);
3942 fillSurfaceRGBA8(relative, 100, 100, 100);
3943
3944 SurfaceComposerClient::Transaction()
3945 .show(child)
3946 // Set relative layer below fg layer but relative to child layer so it should be shown
3947 // above child layer.
3948 .setLayer(relative, -1)
3949 .setRelativeLayer(relative, child->getHandle(), 1)
3950 .show(relative)
3951 .apply(true);
3952
3953 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
3954 // relative value should be taken into account, placing it above child layer.
3955 ScreenCapture::captureLayers(&mCapture, fgHandle);
3956 mCapture->expectFGColor(10, 10);
3957 // Relative layer is showing on top of child layer
3958 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
3959}
Robert Carr578038f2018-03-09 12:25:24 -08003960
3961// In the following tests we verify successful skipping of a parent layer,
3962// so we use the same verification logic and only change how we mutate
3963// the parent layer to verify that various properties are ignored.
3964class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
3965public:
3966 void SetUp() override {
3967 LayerUpdateTest::SetUp();
3968
Vishnu Nair88a11f22018-11-28 18:30:57 -08003969 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3970 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08003971 fillSurfaceRGBA8(mChild, 200, 200, 200);
3972
3973 SurfaceComposerClient::Transaction().show(mChild).apply(true);
3974 }
3975
3976 void verify() {
3977 auto fgHandle = mFGSurfaceControl->getHandle();
3978 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3979 mCapture->checkPixel(10, 10, 0, 0, 0);
3980 mCapture->expectChildColor(0, 0);
3981 }
3982
3983 std::unique_ptr<ScreenCapture> mCapture;
3984 sp<SurfaceControl> mChild;
3985};
3986
3987TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
3988
3989 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3990
3991 // Even though the parent is hidden we should still capture the child.
3992 verify();
3993}
3994
3995TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003996 SurfaceComposerClient::Transaction()
3997 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
3998 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08003999
4000 // Even though the parent is cropped out we should still capture the child.
4001 verify();
4002}
4003
4004TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4005
4006 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4007
4008 // We should not inherit the parent scaling.
4009 verify();
4010}
4011
Robert Carr15eae092018-03-23 13:43:53 -07004012TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4013 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4014
4015 // Even though the parent is hidden we should still capture the child.
4016 verify();
4017
4018 // Verify everything was properly hidden when rendering the full-screen.
4019 screenshot()->expectBGColor(0,0);
4020}
4021
4022
chaviwa76b2712017-09-20 12:02:26 -07004023TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4024 auto fgHandle = mFGSurfaceControl->getHandle();
4025
Vishnu Nair88a11f22018-11-28 18:30:57 -08004026 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4027 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004028 fillSurfaceRGBA8(child, 200, 200, 200);
4029
Vishnu Nair88a11f22018-11-28 18:30:57 -08004030 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4031 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004032
4033 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4034 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004035 .show(child)
4036 .setPosition(grandchild, 5, 5)
4037 .show(grandchild)
4038 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004039
4040 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004041 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004042 mCapture->expectFGColor(10, 10);
4043 mCapture->expectChildColor(0, 0);
4044 mCapture->checkPixel(5, 5, 50, 50, 50);
4045}
4046
4047TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004048 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4049 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004050 fillSurfaceRGBA8(child, 200, 200, 200);
4051 auto childHandle = child->getHandle();
4052
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004053 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004054
4055 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004056 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004057 mCapture->expectChildColor(0, 0);
4058 mCapture->expectChildColor(9, 9);
4059}
4060
4061TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004062 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4063 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004064 fillSurfaceRGBA8(child, 200, 200, 200);
4065 auto childHandle = child->getHandle();
4066
Vishnu Nair88a11f22018-11-28 18:30:57 -08004067 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4068 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004069 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4070
4071 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004072 .show(child)
4073 .setPosition(grandchild, 5, 5)
4074 .show(grandchild)
4075 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004076
4077 auto grandchildHandle = grandchild->getHandle();
4078
4079 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004080 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004081 mCapture->checkPixel(0, 0, 50, 50, 50);
4082 mCapture->checkPixel(4, 4, 50, 50, 50);
4083}
4084
chaviw7206d492017-11-10 16:16:12 -08004085TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004086 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004087 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4088 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004089
Marissa Wall61c58622018-07-18 10:12:20 -07004090 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4091 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004092
4093 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004094 .setLayer(redLayer, INT32_MAX - 1)
4095 .show(redLayer)
4096 .show(blueLayer)
4097 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004098
4099 auto redLayerHandle = redLayer->getHandle();
4100
4101 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004102 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4103 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4104 // red area below the blue area
4105 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4106 // red area to the right of the blue area
4107 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004108
4109 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004110 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004111 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4112 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004113 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004114 mCapture->checkPixel(30, 30, 0, 0, 0);
4115}
4116
4117TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004118 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004119 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4120 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004121
Marissa Wall61c58622018-07-18 10:12:20 -07004122 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4123 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004124
4125 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004126 .setLayer(redLayer, INT32_MAX - 1)
4127 .show(redLayer)
4128 .show(blueLayer)
4129 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004130
4131 auto redLayerHandle = redLayer->getHandle();
4132
4133 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004134 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4135 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4136 // red area below the blue area
4137 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4138 // red area to the right of the blue area
4139 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004140
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004141 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004142 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004143 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4144 // red area below the blue area
4145 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4146 // red area to the right of the blue area
4147 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004148 mCapture->checkPixel(30, 30, 0, 0, 0);
4149}
4150
4151TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004152 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004153
Marissa Wall61c58622018-07-18 10:12:20 -07004154 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004155
4156 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07004157 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08004158 SurfaceComposerClient::Transaction().apply(true);
4159
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004160 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004161
4162 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004163 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4164 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004165}
4166
chaviw8e3fe5d2018-02-22 10:55:42 -08004167
4168class DereferenceSurfaceControlTest : public LayerTransactionTest {
4169protected:
4170 void SetUp() override {
4171 LayerTransactionTest::SetUp();
4172 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004173 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004174 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004175 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004176 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4177 {
4178 SCOPED_TRACE("before anything");
4179 auto shot = screenshot();
4180 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4181 }
4182 }
4183 void TearDown() override {
4184 LayerTransactionTest::TearDown();
4185 bgLayer = 0;
4186 fgLayer = 0;
4187 }
4188
4189 sp<SurfaceControl> bgLayer;
4190 sp<SurfaceControl> fgLayer;
4191};
4192
4193TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4194 fgLayer = nullptr;
4195 {
4196 SCOPED_TRACE("after setting null");
4197 auto shot = screenshot();
4198 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4199 }
4200}
4201
4202TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4203 auto transaction = Transaction().show(fgLayer);
4204 fgLayer = nullptr;
4205 {
4206 SCOPED_TRACE("after setting null");
4207 auto shot = screenshot();
4208 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4209 }
4210}
4211
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004212} // namespace android