blob: e4149916205b0939057abca07a2e37f8ebdaf1f0 [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
Marissa Wallfda30bb2018-10-12 11:34:28 -070018#include <chrono>
19#include <cinttypes>
Chia-I Wu718daf82017-10-20 11:57:17 -070020#include <functional>
21#include <limits>
22#include <ostream>
Marissa Wallfda30bb2018-10-12 11:34:28 -070023#include <thread>
Chia-I Wu718daf82017-10-20 11:57:17 -070024
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070025#include <gtest/gtest.h>
26
Michael Lentine5a16a622015-05-21 13:48:24 -070027#include <android/native_window.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070030#include <gui/LayerState.h>
31
Mathias Agopian90ac7992012-02-25 18:48:35 -080032#include <gui/Surface.h>
33#include <gui/SurfaceComposerClient.h>
34#include <private/gui/ComposerService.h>
35
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070036#include <ui/ColorSpace.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070038#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070039#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070041#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070042#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070043
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070044namespace android {
45
Chia-I Wu718daf82017-10-20 11:57:17 -070046namespace {
47
48struct Color {
49 uint8_t r;
50 uint8_t g;
51 uint8_t b;
52 uint8_t a;
53
54 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070055 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070056 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070057 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070058 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070059 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070060};
61
62const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070063const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070064const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070065const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070066const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070067const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070068
Marissa Wall61c58622018-07-18 10:12:20 -070069using android::hardware::graphics::common::V1_1::BufferUsage;
Marissa Wallfda30bb2018-10-12 11:34:28 -070070using namespace std::chrono_literals;
Marissa Wall61c58622018-07-18 10:12:20 -070071
Chia-I Wu718daf82017-10-20 11:57:17 -070072std::ostream& operator<<(std::ostream& os, const Color& color) {
73 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
74 return os;
75}
76
77// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070078void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
79 const Color& color) {
80 Rect r(0, 0, buffer.width, buffer.height);
81 if (!r.intersect(rect, &r)) {
82 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070083 }
84
Marissa Wall61c58622018-07-18 10:12:20 -070085 int32_t width = r.right - r.left;
86 int32_t height = r.bottom - r.top;
87
88 for (int32_t row = 0; row < height; row++) {
89 uint8_t* dst =
90 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
91 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070092 dst[0] = color.r;
93 dst[1] = color.g;
94 dst[2] = color.b;
95 dst[3] = color.a;
96 dst += 4;
97 }
98 }
99}
100
Marissa Wall61c58622018-07-18 10:12:20 -0700101// Fill a region with the specified color.
102void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
103 Rect r(0, 0, buffer->width, buffer->height);
104 if (!r.intersect(rect, &r)) {
105 return;
106 }
107
108 int32_t width = r.right - r.left;
109 int32_t height = r.bottom - r.top;
110
111 uint8_t* pixels;
112 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
113 reinterpret_cast<void**>(&pixels));
114
115 for (int32_t row = 0; row < height; row++) {
116 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
117 for (int32_t column = 0; column < width; column++) {
118 dst[0] = color.r;
119 dst[1] = color.g;
120 dst[2] = color.b;
121 dst[3] = color.a;
122 dst += 4;
123 }
124 }
125 buffer->unlock();
126}
127
Chia-I Wu718daf82017-10-20 11:57:17 -0700128// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000129void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700130 const Color& color, uint8_t tolerance) {
131 int32_t x = rect.left;
132 int32_t y = rect.top;
133 int32_t width = rect.right - rect.left;
134 int32_t height = rect.bottom - rect.top;
135
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000136 int32_t bufferWidth = int32_t(outBuffer->getWidth());
137 int32_t bufferHeight = int32_t(outBuffer->getHeight());
138 if (x + width > bufferWidth) {
139 x = std::min(x, bufferWidth);
140 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700141 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000142 if (y + height > bufferHeight) {
143 y = std::min(y, bufferHeight);
144 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700145 }
146
147 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
148 uint8_t tmp = a >= b ? a - b : b - a;
149 return tmp <= tolerance;
150 };
151 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000152 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700153 for (int32_t i = 0; i < width; i++) {
154 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
155 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
156 << "pixel @ (" << x + i << ", " << y + j << "): "
157 << "expected (" << color << "), "
158 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
159 src += 4;
160 }
161 }
162}
163
164} // anonymous namespace
165
Robert Carr4cdc58f2017-08-23 14:22:20 -0700166using Transaction = SurfaceComposerClient::Transaction;
167
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700168// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700169static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
170 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800171 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700172 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800173 ASSERT_TRUE(s != nullptr);
174 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800175 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700176 for (int y = 0; y < outBuffer.height; y++) {
177 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700178 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700179 pixel[0] = r;
180 pixel[1] = g;
181 pixel[2] = b;
182 pixel[3] = 255;
183 }
184 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700185 if (unlock) {
186 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
187 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700188}
189
190// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
191// individual pixel values for testing purposes.
192class ScreenCapture : public RefBase {
193public:
chaviw0e3479f2018-09-10 16:49:30 -0700194 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700195 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700196 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700197 SurfaceComposerClient::Transaction().apply(true);
198
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000199 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700200 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700201 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
202 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000203 }
204
205 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
206 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
207 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
208 SurfaceComposerClient::Transaction().apply(true);
209
210 sp<GraphicBuffer> outBuffer;
211 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
212 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700213 }
214
Robert Carr578038f2018-03-09 12:25:24 -0800215 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
216 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
217 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
218 SurfaceComposerClient::Transaction().apply(true);
219
220 sp<GraphicBuffer> outBuffer;
221 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
222 *sc = std::make_unique<ScreenCapture>(outBuffer);
223 }
224
Chia-I Wu718daf82017-10-20 11:57:17 -0700225 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000226 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
227 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700228 }
229
230 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000231 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700232 const bool leftBorder = rect.left > 0;
233 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000234 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
235 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700236
237 if (topBorder) {
238 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
239 if (leftBorder) {
240 top.left -= 1;
241 }
242 if (rightBorder) {
243 top.right += 1;
244 }
245 expectColor(top, color, tolerance);
246 }
247 if (leftBorder) {
248 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
249 expectColor(left, color, tolerance);
250 }
251 if (rightBorder) {
252 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
253 expectColor(right, color, tolerance);
254 }
255 if (bottomBorder) {
256 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
257 if (leftBorder) {
258 bottom.left -= 1;
259 }
260 if (rightBorder) {
261 bottom.right += 1;
262 }
263 expectColor(bottom, color, tolerance);
264 }
265 }
266
Chia-I Wu93853fe2017-11-02 08:30:27 -0700267 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
268 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
269 uint8_t tolerance = 0) {
270 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
271
272 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
273 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
274 // avoid checking borders due to unspecified filtering behavior
275 const int32_t offsetX = filtered ? 2 : 0;
276 const int32_t offsetY = filtered ? 2 : 0;
277 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
278 tolerance);
279 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
280 tolerance);
281 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
282 tolerance);
283 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
284 bottomRight, tolerance);
285 }
286
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700287 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000288 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
289 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700290 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
291 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700292 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
293 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700294 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700295 }
296 }
297
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700298 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700299
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700300 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700301
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700302 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700303
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000304 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
305 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700306 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700307
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000308 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700309
310private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000311 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800312 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700313};
314
Chia-I Wu718daf82017-10-20 11:57:17 -0700315class LayerTransactionTest : public ::testing::Test {
316protected:
317 void SetUp() override {
318 mClient = new SurfaceComposerClient;
319 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
320
321 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700322
323 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
324 sp<IBinder> binder = sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
Ady Abraham37965d42018-11-01 13:43:32 -0700325 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
Chia-I Wu718daf82017-10-20 11:57:17 -0700326 }
327
chaviw0e3479f2018-09-10 16:49:30 -0700328 virtual void TearDown() {
329 mBlackBgSurface = 0;
330 mClient->dispose();
331 mClient = 0;
332 }
333
Marissa Wallfda30bb2018-10-12 11:34:28 -0700334 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
335 const char* name, uint32_t width, uint32_t height,
Marissa Wall61c58622018-07-18 10:12:20 -0700336 uint32_t flags = 0) {
Vishnu Nair88a11f22018-11-28 18:30:57 -0800337 auto layer = createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags);
Chia-I Wu718daf82017-10-20 11:57:17 -0700338
Vishnu Nair60356342018-11-13 13:00:45 -0800339 Transaction t;
340 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -0800341
342 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700343 if (error != NO_ERROR) {
344 ADD_FAILURE() << "failed to initialize SurfaceControl";
345 layer.clear();
346 }
347
348 return layer;
349 }
350
Vishnu Nair88a11f22018-11-28 18:30:57 -0800351 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
352 const char* name, uint32_t width, uint32_t height,
353 PixelFormat format, uint32_t flags,
354 SurfaceControl* parent = nullptr) {
355 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
356 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
357 return layer;
358 }
359
Marissa Wallfda30bb2018-10-12 11:34:28 -0700360 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
361 uint32_t flags = 0) {
362 return createLayer(mClient, name, width, height, flags);
363 }
364
Marissa Wall61c58622018-07-18 10:12:20 -0700365 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700366 // wait for previous transactions (such as setSize) to complete
367 Transaction().apply(true);
368
369 ANativeWindow_Buffer buffer = {};
370 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
371
372 return buffer;
373 }
374
Marissa Wall61c58622018-07-18 10:12:20 -0700375 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700376 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
377
378 // wait for the newly posted buffer to be latched
379 waitForLayerBuffers();
380 }
381
Marissa Wall61c58622018-07-18 10:12:20 -0700382 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
383 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700384 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700385 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
386 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
387 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700388 }
389
Marissa Wall61c58622018-07-18 10:12:20 -0700390 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
391 int32_t bufferWidth, int32_t bufferHeight) {
392 sp<GraphicBuffer> buffer =
393 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
394 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
395 BufferUsage::COMPOSER_OVERLAY,
396 "test");
397 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
398 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
399 }
400
401 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
402 int32_t bufferWidth, int32_t bufferHeight) {
403 switch (mLayerType) {
404 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
405 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
406 break;
407 case ISurfaceComposerClient::eFXSurfaceBufferState:
408 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
409 break;
410 default:
411 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
412 }
413 }
414
415 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
416 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700417 const Color& topRight, const Color& bottomLeft,
418 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700419 switch (mLayerType) {
420 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
421 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
422 bottomLeft, bottomRight);
423 break;
424 case ISurfaceComposerClient::eFXSurfaceBufferState:
425 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
426 bottomLeft, bottomRight);
427 break;
428 default:
429 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
430 }
431 }
432
433 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
434 int32_t bufferHeight, const Color& topLeft,
435 const Color& topRight, const Color& bottomLeft,
436 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700437 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700438 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
439 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700440
Marissa Wall61c58622018-07-18 10:12:20 -0700441 const int32_t halfW = bufferWidth / 2;
442 const int32_t halfH = bufferHeight / 2;
443 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
444 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
445 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
446 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
447 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700448
Marissa Wall61c58622018-07-18 10:12:20 -0700449 postBufferQueueLayerBuffer(layer);
450 }
451
452 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
453 int32_t bufferHeight, const Color& topLeft,
454 const Color& topRight, const Color& bottomLeft,
455 const Color& bottomRight) {
456 sp<GraphicBuffer> buffer =
457 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
458 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
459 BufferUsage::COMPOSER_OVERLAY,
460 "test");
461
462 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
463
464 const int32_t halfW = bufferWidth / 2;
465 const int32_t halfH = bufferHeight / 2;
466 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
467 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
468 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
469 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
470
471 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700472 }
473
chaviw0e3479f2018-09-10 16:49:30 -0700474 std::unique_ptr<ScreenCapture> screenshot() {
475 std::unique_ptr<ScreenCapture> screenshot;
476 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700477 return screenshot;
478 }
479
480 sp<SurfaceComposerClient> mClient;
481
482 sp<IBinder> mDisplay;
483 uint32_t mDisplayWidth;
484 uint32_t mDisplayHeight;
485 uint32_t mDisplayLayerStack;
486
487 // leave room for ~256 layers
488 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
489
Marissa Wall61c58622018-07-18 10:12:20 -0700490 void setPositionWithResizeHelper(uint32_t layerType);
491 void setSizeBasicHelper(uint32_t layerType);
492 void setMatrixWithResizeHelper(uint32_t layerType);
493
chaviw0e3479f2018-09-10 16:49:30 -0700494 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700495 bool mColorManagementUsed;
496
Chia-I Wu718daf82017-10-20 11:57:17 -0700497private:
498 void SetUpDisplay() {
499 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
500 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
501
502 // get display width/height
503 DisplayInfo info;
504 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
505 mDisplayWidth = info.w;
506 mDisplayHeight = info.h;
507
508 // After a new buffer is queued, SurfaceFlinger is notified and will
509 // latch the new buffer on next vsync. Let's heuristically wait for 3
510 // vsyncs.
511 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
512
513 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700514
Vishnu Nair88a11f22018-11-28 18:30:57 -0800515 mBlackBgSurface =
516 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
517 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700518
Chia-I Wu718daf82017-10-20 11:57:17 -0700519 // set layer stack (b/68888219)
520 Transaction t;
521 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800522 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700523 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
524 t.setColor(mBlackBgSurface, half3{0, 0, 0});
525 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700526 t.apply();
527 }
528
chaviw0e3479f2018-09-10 16:49:30 -0700529 void waitForLayerBuffers() {
530 // Request an empty transaction to get applied synchronously to ensure the buffer is
531 // latched.
532 Transaction().apply(true);
533 usleep(mBufferPostDelay);
534 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700535
536 int32_t mBufferPostDelay;
537};
538
Marissa Wall61c58622018-07-18 10:12:20 -0700539class LayerTypeTransactionTest : public LayerTransactionTest,
540 public ::testing::WithParamInterface<uint32_t> {
541public:
542 LayerTypeTransactionTest() { mLayerType = GetParam(); }
543
544 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
545 uint32_t flags = 0) override {
546 // if the flags already have a layer type specified, return an error
547 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
548 return nullptr;
549 }
550 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
551 }
552
553 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
554 int32_t bufferHeight) {
555 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
556 bufferWidth, bufferHeight));
557 }
558
559 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
560 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
561 const Color& bottomLeft, const Color& bottomRight) {
562 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
563 bufferWidth, bufferHeight,
564 topLeft, topRight,
565 bottomLeft, bottomRight));
566 }
567
568protected:
569 uint32_t mLayerType;
570};
571
572INSTANTIATE_TEST_CASE_P(
573 LayerTypeTransactionTests, LayerTypeTransactionTest,
574 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
575 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
576
577TEST_P(LayerTypeTransactionTest, SetPositionBasic) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700578 sp<SurfaceControl> layer;
579 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700580 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700581
582 {
583 SCOPED_TRACE("default position");
584 auto shot = screenshot();
585 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
586 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
587 }
588
589 Transaction().setPosition(layer, 5, 10).apply();
590 {
591 SCOPED_TRACE("new position");
592 auto shot = screenshot();
593 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
594 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
595 }
596}
597
Marissa Wall61c58622018-07-18 10:12:20 -0700598TEST_P(LayerTypeTransactionTest, SetPositionRounding) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700599 sp<SurfaceControl> layer;
600 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700601 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700602
603 // GLES requires only 4 bits of subpixel precision during rasterization
604 // XXX GLES composition does not match HWC composition due to precision
605 // loss (b/69315223)
606 const float epsilon = 1.0f / 16.0f;
607 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
608 {
609 SCOPED_TRACE("rounding down");
610 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
611 }
612
613 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
614 {
615 SCOPED_TRACE("rounding up");
616 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
617 }
618}
619
Marissa Wall61c58622018-07-18 10:12:20 -0700620TEST_P(LayerTypeTransactionTest, SetPositionOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700621 sp<SurfaceControl> layer;
622 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700623 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700624
625 Transaction().setPosition(layer, -32, -32).apply();
626 {
627 SCOPED_TRACE("negative coordinates");
628 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
629 }
630
631 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
632 {
633 SCOPED_TRACE("positive coordinates");
634 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
635 }
636}
637
Marissa Wall61c58622018-07-18 10:12:20 -0700638TEST_P(LayerTypeTransactionTest, SetPositionPartiallyOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700639 sp<SurfaceControl> layer;
640 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700641 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700642
643 // partially out of bounds
644 Transaction().setPosition(layer, -30, -30).apply();
645 {
646 SCOPED_TRACE("negative coordinates");
647 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
648 }
649
650 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
651 {
652 SCOPED_TRACE("positive coordinates");
653 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
654 mDisplayHeight),
655 Color::RED);
656 }
657}
658
Marissa Wall61c58622018-07-18 10:12:20 -0700659void LayerTransactionTest::setPositionWithResizeHelper(uint32_t layerType) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700660 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700661 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
662 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700663
664 // setPosition is applied immediately by default, with or without resize
665 // pending
666 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
667 {
668 SCOPED_TRACE("resize pending");
669 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700670 Rect rect;
671 switch (layerType) {
672 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
673 rect = {5, 10, 37, 42};
674 break;
675 case ISurfaceComposerClient::eFXSurfaceBufferState:
676 rect = {5, 10, 69, 74};
677 break;
678 default:
679 ASSERT_FALSE(true) << "Unsupported layer type";
680 }
681
682 shot->expectColor(rect, Color::RED);
683 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700684 }
685
Marissa Wall61c58622018-07-18 10:12:20 -0700686 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700687 {
688 SCOPED_TRACE("resize applied");
689 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
690 }
691}
692
Marissa Wall61c58622018-07-18 10:12:20 -0700693TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
694 ASSERT_NO_FATAL_FAILURE(
695 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
696}
697
698TEST_F(LayerTransactionTest, SetPositionWithResize_BufferState) {
699 ASSERT_NO_FATAL_FAILURE(
700 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
701}
702
703TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700704 sp<SurfaceControl> layer;
705 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700706 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700707
708 // request setPosition to be applied with the next resize
709 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
710 {
711 SCOPED_TRACE("new position pending");
712 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
713 }
714
715 Transaction().setPosition(layer, 15, 20).apply();
716 {
717 SCOPED_TRACE("pending new position modified");
718 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
719 }
720
721 Transaction().setSize(layer, 64, 64).apply();
722 {
723 SCOPED_TRACE("resize pending");
724 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
725 }
726
727 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700728 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700729 {
730 SCOPED_TRACE("new position applied");
731 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
732 }
733}
734
Marissa Wall61c58622018-07-18 10:12:20 -0700735TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700736 sp<SurfaceControl> layer;
737 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700738 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700739
740 // setPosition is not immediate even with SCALE_TO_WINDOW override
741 Transaction()
742 .setPosition(layer, 5, 10)
743 .setSize(layer, 64, 64)
744 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
745 .setGeometryAppliesWithResize(layer)
746 .apply();
747 {
748 SCOPED_TRACE("new position pending");
749 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
750 }
751
Marissa Wall61c58622018-07-18 10:12:20 -0700752 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700753 {
754 SCOPED_TRACE("new position applied");
755 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
756 }
757}
758
Marissa Wall61c58622018-07-18 10:12:20 -0700759void LayerTransactionTest::setSizeBasicHelper(uint32_t layerType) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700760 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700761 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
762 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700763
764 Transaction().setSize(layer, 64, 64).apply();
765 {
766 SCOPED_TRACE("resize pending");
767 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700768 Rect rect;
769 switch (layerType) {
770 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
771 rect = {0, 0, 32, 32};
772 break;
773 case ISurfaceComposerClient::eFXSurfaceBufferState:
774 rect = {0, 0, 64, 64};
775 break;
776 default:
777 ASSERT_FALSE(true) << "Unsupported layer type";
778 }
779 shot->expectColor(rect, Color::RED);
780 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700781 }
782
Marissa Wall61c58622018-07-18 10:12:20 -0700783 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700784 {
785 SCOPED_TRACE("resize applied");
786 auto shot = screenshot();
787 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
788 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
789 }
790}
791
Marissa Wall61c58622018-07-18 10:12:20 -0700792TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
793 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue);
794}
795
796TEST_F(LayerTransactionTest, SetSizeBasic_BufferState) {
797 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState);
798}
799
800TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700801 // cannot test robustness against invalid sizes (zero or really huge)
802}
803
Marissa Wall61c58622018-07-18 10:12:20 -0700804TEST_P(LayerTypeTransactionTest, SetSizeWithScaleToWindow) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700805 sp<SurfaceControl> layer;
806 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700807 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700808
809 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
810 Transaction()
811 .setSize(layer, 64, 64)
812 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
813 .apply();
814 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
815}
816
Marissa Wall61c58622018-07-18 10:12:20 -0700817TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700818 sp<SurfaceControl> layerR;
819 sp<SurfaceControl> layerG;
820 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700821 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700822 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700823 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700824
825 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
826 {
827 SCOPED_TRACE("layerR");
828 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
829 }
830
831 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
832 {
833 SCOPED_TRACE("layerG");
834 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
835 }
836}
837
Marissa Wall61c58622018-07-18 10:12:20 -0700838TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700839 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800840 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700841 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800842 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700843 sp<SurfaceControl> layerR;
844 sp<SurfaceControl> layerG;
845 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700846 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700847 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700848 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700849
chaviw0e3479f2018-09-10 16:49:30 -0700850 Transaction()
851 .reparent(layerR, parent->getHandle())
852 .reparent(layerG, parent->getHandle())
853 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700854 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
855 {
856 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700857 auto shot = screenshot();
858 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700859 }
860
861 Transaction().setLayer(layerR, -3).apply();
862 {
863 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700864 auto shot = screenshot();
865 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700866 }
867}
868
Marissa Wall61c58622018-07-18 10:12:20 -0700869TEST_P(LayerTypeTransactionTest, SetRelativeZBasic) {
Chia-I Wu49313302017-10-31 10:14:40 -0700870 sp<SurfaceControl> layerR;
871 sp<SurfaceControl> layerG;
872 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700873 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700874 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700875 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700876
877 Transaction()
878 .setPosition(layerG, 16, 16)
879 .setRelativeLayer(layerG, layerR->getHandle(), 1)
880 .apply();
881 {
882 SCOPED_TRACE("layerG above");
883 auto shot = screenshot();
884 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
885 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
886 }
887
888 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
889 {
890 SCOPED_TRACE("layerG below");
891 auto shot = screenshot();
892 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
893 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
894 }
895}
896
Marissa Wall61c58622018-07-18 10:12:20 -0700897TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700898 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800899 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700900 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800901 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800902 sp<SurfaceControl> layerR;
903 sp<SurfaceControl> layerG;
904 sp<SurfaceControl> layerB;
905 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700906 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800907 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700908 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800909 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700910 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800911
chaviw0e3479f2018-09-10 16:49:30 -0700912 Transaction()
913 .reparent(layerB, parent->getHandle())
914 .apply();
915
Chia-I Wuec2d9852017-11-21 09:21:01 -0800916 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
917 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
918
chaviw0e3479f2018-09-10 16:49:30 -0700919 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800920 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700921 sp<IBinder> parentHandle = parent->getHandle();
922 ScreenCapture::captureLayers(&screenshot, parentHandle);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800923 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
924}
925
Marissa Wall61c58622018-07-18 10:12:20 -0700926TEST_P(LayerTypeTransactionTest, SetRelativeZGroup) {
Chia-I Wu49313302017-10-31 10:14:40 -0700927 sp<SurfaceControl> layerR;
928 sp<SurfaceControl> layerG;
929 sp<SurfaceControl> layerB;
930 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700931 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700932 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700933 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700934 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700935 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700936
937 // layerR = 0, layerG = layerR + 3, layerB = 2
938 Transaction()
939 .setPosition(layerG, 8, 8)
940 .setRelativeLayer(layerG, layerR->getHandle(), 3)
941 .setPosition(layerB, 16, 16)
942 .setLayer(layerB, mLayerZBase + 2)
943 .apply();
944 {
945 SCOPED_TRACE("(layerR < layerG) < layerB");
946 auto shot = screenshot();
947 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
948 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
949 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
950 }
951
952 // layerR = 4, layerG = layerR + 3, layerB = 2
953 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
954 {
955 SCOPED_TRACE("layerB < (layerR < layerG)");
956 auto shot = screenshot();
957 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
958 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
959 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
960 }
961
962 // layerR = 4, layerG = layerR - 3, layerB = 2
963 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
964 {
965 SCOPED_TRACE("layerB < (layerG < layerR)");
966 auto shot = screenshot();
967 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
968 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
969 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
970 }
971
972 // restore to absolute z
973 // layerR = 4, layerG = 0, layerB = 2
974 Transaction().setLayer(layerG, mLayerZBase).apply();
975 {
976 SCOPED_TRACE("layerG < layerB < layerR");
977 auto shot = screenshot();
978 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
979 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
980 }
981
982 // layerR should not affect layerG anymore
983 // layerR = 1, layerG = 0, layerB = 2
984 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
985 {
986 SCOPED_TRACE("layerG < layerR < layerB");
987 auto shot = screenshot();
988 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
989 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
990 }
991}
992
Marissa Wall61c58622018-07-18 10:12:20 -0700993TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700994 sp<SurfaceControl> layerR;
995 sp<SurfaceControl> layerG;
996
997 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700998 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700999 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001000 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001001
1002 Transaction()
1003 .setPosition(layerG, 16, 16)
1004 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1005 .apply();
1006
1007 mClient->destroySurface(layerG->getHandle());
1008 // layerG should have been removed
1009 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1010}
1011
Marissa Wall61c58622018-07-18 10:12:20 -07001012TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001013 sp<SurfaceControl> layer;
1014 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001015 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001016
1017 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1018 {
1019 SCOPED_TRACE("layer hidden");
1020 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1021 }
1022
1023 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1024 {
1025 SCOPED_TRACE("layer shown");
1026 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1027 }
1028}
1029
Marissa Wall61c58622018-07-18 10:12:20 -07001030TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001031 const Color translucentRed = {100, 0, 0, 100};
1032 sp<SurfaceControl> layerR;
1033 sp<SurfaceControl> layerG;
1034 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001035 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001036 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001037 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001038
1039 Transaction()
1040 .setLayer(layerR, mLayerZBase + 1)
1041 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1042 .apply();
1043 {
1044 SCOPED_TRACE("layerR opaque");
1045 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1046 }
1047
1048 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1049 {
1050 SCOPED_TRACE("layerR translucent");
1051 const uint8_t g = uint8_t(255 - translucentRed.a);
1052 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1053 }
1054}
1055
Marissa Wall61c58622018-07-18 10:12:20 -07001056TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001057 sp<SurfaceControl> layer;
1058 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001059 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001060
1061 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001062 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001063 Transaction()
1064 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1065 .apply(true);
1066 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001067 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001068
1069 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1070 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001071 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001072}
1073
Marissa Wall61c58622018-07-18 10:12:20 -07001074TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001075 const Rect top(0, 0, 32, 16);
1076 const Rect bottom(0, 16, 32, 32);
1077 sp<SurfaceControl> layer;
1078 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1079
1080 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001081 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1082 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1083 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001084 // setTransparentRegionHint always applies to the following buffer
1085 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001086 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001087 {
1088 SCOPED_TRACE("top transparent");
1089 auto shot = screenshot();
1090 shot->expectColor(top, Color::BLACK);
1091 shot->expectColor(bottom, Color::RED);
1092 }
1093
1094 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1095 {
1096 SCOPED_TRACE("transparent region hint pending");
1097 auto shot = screenshot();
1098 shot->expectColor(top, Color::BLACK);
1099 shot->expectColor(bottom, Color::RED);
1100 }
1101
Marissa Wall61c58622018-07-18 10:12:20 -07001102 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1103 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1104 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1105 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001106 {
1107 SCOPED_TRACE("bottom transparent");
1108 auto shot = screenshot();
1109 shot->expectColor(top, Color::RED);
1110 shot->expectColor(bottom, Color::BLACK);
1111 }
1112}
1113
Marissa Wall61c58622018-07-18 10:12:20 -07001114TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1115 const Rect top(0, 0, 32, 16);
1116 const Rect bottom(0, 16, 32, 32);
1117 sp<SurfaceControl> layer;
1118 ASSERT_NO_FATAL_FAILURE(
1119 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1120
1121 sp<GraphicBuffer> buffer =
1122 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1123 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1124 BufferUsage::COMPOSER_OVERLAY,
1125 "test");
1126
1127 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1128 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1129 Transaction()
1130 .setTransparentRegionHint(layer, Region(top))
1131 .setBuffer(layer, buffer)
1132 .setSize(layer, 32, 32)
1133 .apply();
1134 {
1135 SCOPED_TRACE("top transparent");
1136 auto shot = screenshot();
1137 shot->expectColor(top, Color::BLACK);
1138 shot->expectColor(bottom, Color::RED);
1139 }
1140
1141 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1142 {
1143 SCOPED_TRACE("transparent region hint intermediate");
1144 auto shot = screenshot();
1145 shot->expectColor(top, Color::BLACK);
1146 shot->expectColor(bottom, Color::BLACK);
1147 }
1148
1149 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1150 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1151 BufferUsage::COMPOSER_OVERLAY,
1152 "test");
1153
1154 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1155 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1156 Transaction().setBuffer(layer, buffer).setSize(layer, 32, 32).apply();
1157 {
1158 SCOPED_TRACE("bottom transparent");
1159 auto shot = screenshot();
1160 shot->expectColor(top, Color::RED);
1161 shot->expectColor(bottom, Color::BLACK);
1162 }
1163}
1164
1165TEST_P(LayerTypeTransactionTest, SetTransparentRegionHintOutOfBounds) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001166 sp<SurfaceControl> layerTransparent;
1167 sp<SurfaceControl> layerR;
1168 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1169 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1170
1171 // check that transparent region hint is bound by the layer size
1172 Transaction()
1173 .setTransparentRegionHint(layerTransparent,
1174 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1175 .setPosition(layerR, 16, 16)
1176 .setLayer(layerR, mLayerZBase + 1)
1177 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001178 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1179 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001180 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1181}
1182
Marissa Wall61c58622018-07-18 10:12:20 -07001183TEST_P(LayerTypeTransactionTest, SetAlphaBasic) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001184 sp<SurfaceControl> layer1;
1185 sp<SurfaceControl> layer2;
1186 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1187 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001188 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}, 32, 32));
1189 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001190
1191 Transaction()
1192 .setAlpha(layer1, 0.25f)
1193 .setAlpha(layer2, 0.75f)
1194 .setPosition(layer2, 16, 0)
1195 .setLayer(layer2, mLayerZBase + 1)
1196 .apply();
1197 {
1198 auto shot = screenshot();
1199 uint8_t r = 16; // 64 * 0.25f
1200 uint8_t g = 48; // 64 * 0.75f
1201 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1202 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1203
1204 r /= 4; // r * (1.0f - 0.75f)
1205 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1206 }
1207}
1208
Marissa Wall61c58622018-07-18 10:12:20 -07001209TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001210 const Color color = {64, 0, 0, 255};
1211 sp<SurfaceControl> layer;
1212 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001213 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001214
1215 Transaction().setAlpha(layer, 2.0f).apply();
1216 {
1217 SCOPED_TRACE("clamped to 1.0f");
1218 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1219 }
1220
1221 Transaction().setAlpha(layer, -1.0f).apply();
1222 {
1223 SCOPED_TRACE("clamped to 0.0f");
1224 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1225 }
1226}
1227
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001228TEST_P(LayerTypeTransactionTest, SetCornerRadius) {
1229 sp<SurfaceControl> layer;
1230 const uint8_t size = 64;
1231 const uint8_t testArea = 4;
1232 const float cornerRadius = 16.0f;
1233 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1234 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1235
1236 Transaction()
1237 .setCornerRadius(layer, cornerRadius)
1238 .apply();
1239 {
1240 auto shot = screenshot();
1241 // Transparent corners
1242 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
1243 shot->expectColor(Rect(0, size - testArea, testArea, testArea), Color::BLACK);
1244 shot->expectColor(Rect(size - testArea, 0, testArea, testArea), Color::BLACK);
1245 shot->expectColor(Rect(size - testArea, size - testArea, testArea, testArea),
1246 Color::BLACK);
1247 }
1248}
1249
Chia-I Wue4ef6102017-11-01 15:16:35 -07001250TEST_F(LayerTransactionTest, SetColorBasic) {
1251 sp<SurfaceControl> bufferLayer;
1252 sp<SurfaceControl> colorLayer;
1253 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001254 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001255 ASSERT_NO_FATAL_FAILURE(colorLayer =
1256 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1257 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001258
Vishnu Nair88a11f22018-11-28 18:30:57 -08001259 Transaction()
1260 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1261 .setLayer(colorLayer, mLayerZBase + 1)
1262 .apply();
1263
Chia-I Wue4ef6102017-11-01 15:16:35 -07001264 {
1265 SCOPED_TRACE("default color");
1266 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1267 }
1268
1269 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1270 const Color expected = {15, 51, 85, 255};
1271 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1272 // channel) should be less than one
1273 const uint8_t tolerance = 1;
1274 Transaction().setColor(colorLayer, color).apply();
1275 {
1276 SCOPED_TRACE("new color");
1277 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1278 }
1279}
1280
1281TEST_F(LayerTransactionTest, SetColorClamped) {
1282 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001283 ASSERT_NO_FATAL_FAILURE(colorLayer =
1284 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1285 ISurfaceComposerClient::eFXSurfaceColor));
1286 Transaction()
1287 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1288 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1289 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001290
Chia-I Wue4ef6102017-11-01 15:16:35 -07001291 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1292}
1293
1294TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1295 sp<SurfaceControl> bufferLayer;
1296 sp<SurfaceControl> colorLayer;
1297 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001298 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001299 ASSERT_NO_FATAL_FAILURE(colorLayer =
1300 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1301 ISurfaceComposerClient::eFXSurfaceColor));
1302 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001303
1304 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1305 const float alpha = 0.25f;
1306 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1307 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1308 // channel) should be less than one
1309 const uint8_t tolerance = 1;
1310 Transaction()
1311 .setColor(colorLayer, color)
1312 .setAlpha(colorLayer, alpha)
1313 .setLayer(colorLayer, mLayerZBase + 1)
1314 .apply();
1315 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1316 tolerance);
1317}
1318
Adrian Roosb7a96502018-04-08 11:38:55 -07001319TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1320 sp<SurfaceControl> bufferLayer;
1321 sp<SurfaceControl> parentLayer;
1322 sp<SurfaceControl> colorLayer;
1323 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1324 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001325 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001326 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1327 0 /* buffer height */,
1328 ISurfaceComposerClient::eFXSurfaceColor));
1329 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001330 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1331 const float alpha = 0.25f;
1332 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1333 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1334 // channel) should be less than one
1335 const uint8_t tolerance = 1;
1336 Transaction()
1337 .reparent(colorLayer, parentLayer->getHandle())
1338 .setColor(colorLayer, color)
1339 .setAlpha(parentLayer, alpha)
1340 .setLayer(parentLayer, mLayerZBase + 1)
1341 .apply();
1342 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1343 tolerance);
1344}
1345
Marissa Wall61c58622018-07-18 10:12:20 -07001346TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001347 sp<SurfaceControl> bufferLayer;
1348 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001349 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001350
1351 // color is ignored
1352 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1353 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1354}
1355
Marissa Wall61c58622018-07-18 10:12:20 -07001356TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001357 sp<SurfaceControl> layer;
1358 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001359 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001360
1361 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1362 {
1363 SCOPED_TRACE("non-existing layer stack");
1364 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1365 }
1366
1367 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1368 {
1369 SCOPED_TRACE("original layer stack");
1370 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1371 }
1372}
1373
Marissa Wall61c58622018-07-18 10:12:20 -07001374TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001375 sp<SurfaceControl> layer;
1376 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1377 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001378 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001379
1380 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1381 {
1382 SCOPED_TRACE("IDENTITY");
1383 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1384 Color::WHITE);
1385 }
1386
1387 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1388 {
1389 SCOPED_TRACE("FLIP_H");
1390 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1391 Color::BLUE);
1392 }
1393
1394 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1395 {
1396 SCOPED_TRACE("FLIP_V");
1397 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1398 Color::GREEN);
1399 }
1400
1401 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1402 {
1403 SCOPED_TRACE("ROT_90");
1404 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1405 Color::GREEN);
1406 }
1407
1408 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1409 {
1410 SCOPED_TRACE("SCALE");
1411 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1412 Color::WHITE, true /* filtered */);
1413 }
1414}
1415
Marissa Wall61c58622018-07-18 10:12:20 -07001416TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001417 sp<SurfaceControl> layer;
1418 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1419 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001420 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001421
1422 const float rot = M_SQRT1_2; // 45 degrees
1423 const float trans = M_SQRT2 * 16.0f;
1424 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1425
1426 auto shot = screenshot();
1427 // check a 8x8 region inside each color
1428 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1429 const int32_t halfL = 4;
1430 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1431 };
1432 const int32_t unit = int32_t(trans / 2);
1433 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1434 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1435 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1436 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1437}
1438
Marissa Wall61c58622018-07-18 10:12:20 -07001439void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001440 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001441 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1442 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001443
1444 // setMatrix is applied after any pending resize, unlike setPosition
1445 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1446 {
1447 SCOPED_TRACE("resize pending");
1448 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001449 Rect rect;
1450 switch (layerType) {
1451 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1452 rect = {0, 0, 32, 32};
1453 break;
1454 case ISurfaceComposerClient::eFXSurfaceBufferState:
1455 rect = {0, 0, 128, 128};
1456 break;
1457 default:
1458 ASSERT_FALSE(true) << "Unsupported layer type";
1459 }
1460 shot->expectColor(rect, Color::RED);
1461 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001462 }
1463
Marissa Wall61c58622018-07-18 10:12:20 -07001464 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001465 {
1466 SCOPED_TRACE("resize applied");
1467 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1468 }
1469}
1470
Marissa Wall61c58622018-07-18 10:12:20 -07001471TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1472 ASSERT_NO_FATAL_FAILURE(
1473 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1474}
1475
1476TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1477 ASSERT_NO_FATAL_FAILURE(
1478 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1479}
1480
1481TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001482 sp<SurfaceControl> layer;
1483 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001484 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001485
1486 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1487 Transaction()
1488 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1489 .setSize(layer, 64, 64)
1490 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1491 .apply();
1492 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1493}
1494
Marissa Wall61c58622018-07-18 10:12:20 -07001495TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001496 sp<SurfaceControl> layer;
1497 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1498 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001499 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001500
1501 // XXX SCALE_CROP is not respected; calling setSize and
1502 // setOverrideScalingMode in separate transactions does not work
1503 // (b/69315456)
1504 Transaction()
1505 .setSize(layer, 64, 16)
1506 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1507 .apply();
1508 {
1509 SCOPED_TRACE("SCALE_TO_WINDOW");
1510 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1511 Color::WHITE, true /* filtered */);
1512 }
1513}
1514
Dan Stoza000dd012018-08-01 13:31:52 -07001515TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1516 sp<SurfaceControl> layer;
1517 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1518
1519 sp<IBinder> handle = layer->getHandle();
1520 ASSERT_TRUE(handle != nullptr);
1521
1522 FrameStats frameStats;
1523 mClient->getLayerFrameStats(handle, &frameStats);
1524
1525 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1526}
1527
Marissa Wall61c58622018-07-18 10:12:20 -07001528TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001529 sp<SurfaceControl> layer;
1530 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001531 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001532 const Rect crop(8, 8, 24, 24);
1533
Marissa Wallf58c14b2018-07-24 10:50:43 -07001534 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001535 auto shot = screenshot();
1536 shot->expectColor(crop, Color::RED);
1537 shot->expectBorder(crop, Color::BLACK);
1538}
1539
Marissa Wall61c58622018-07-18 10:12:20 -07001540TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1541 sp<SurfaceControl> layer;
1542 ASSERT_NO_FATAL_FAILURE(
1543 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1544 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1545 const Rect crop(8, 8, 24, 24);
1546
1547 Transaction().setCrop(layer, crop).apply();
1548 auto shot = screenshot();
1549 shot->expectColor(crop, Color::RED);
1550 shot->expectBorder(crop, Color::BLACK);
1551}
1552
1553TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001554 sp<SurfaceControl> layer;
1555 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001556 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001557
1558 {
1559 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001560 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001561 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1562 }
1563
1564 {
1565 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001566 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001567 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1568 }
1569}
1570
Marissa Wall61c58622018-07-18 10:12:20 -07001571TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1572 sp<SurfaceControl> layer;
1573 ASSERT_NO_FATAL_FAILURE(
1574 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1575 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1576
1577 {
1578 SCOPED_TRACE("empty rect");
1579 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1580 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1581 }
1582
1583 {
1584 SCOPED_TRACE("negative rect");
1585 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1586 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1587 }
1588}
1589
1590TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001591 sp<SurfaceControl> layer;
1592 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001593 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001594
Marissa Wallf58c14b2018-07-24 10:50:43 -07001595 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001596 auto shot = screenshot();
1597 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1598 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1599}
1600
Marissa Wall61c58622018-07-18 10:12:20 -07001601TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1602 sp<SurfaceControl> layer;
1603 ASSERT_NO_FATAL_FAILURE(
1604 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1605 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1606
1607 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1608 auto shot = screenshot();
1609 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1610 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1611}
1612
1613TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001614 sp<SurfaceControl> layer;
1615 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001616 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001617
1618 const Point position(32, 32);
1619 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001620 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001621 auto shot = screenshot();
1622 shot->expectColor(crop + position, Color::RED);
1623 shot->expectBorder(crop + position, Color::BLACK);
1624}
1625
Marissa Wall61c58622018-07-18 10:12:20 -07001626TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1627 sp<SurfaceControl> layer;
1628 ASSERT_NO_FATAL_FAILURE(
1629 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1630 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1631
1632 const Point position(32, 32);
1633 const Rect crop(8, 8, 24, 24);
1634 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1635 auto shot = screenshot();
1636 shot->expectColor(crop + position, Color::RED);
1637 shot->expectBorder(crop + position, Color::BLACK);
1638}
1639
1640TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001641 sp<SurfaceControl> layer;
1642 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001643 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001644
1645 // crop is affected by matrix
1646 Transaction()
1647 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001648 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001649 .apply();
1650 auto shot = screenshot();
1651 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1652 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1653}
1654
Marissa Wall61c58622018-07-18 10:12:20 -07001655TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1656 sp<SurfaceControl> layer;
1657 ASSERT_NO_FATAL_FAILURE(
1658 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1659 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1660
1661 // crop is affected by matrix
1662 Transaction()
1663 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1664 .setCrop(layer, Rect(8, 8, 24, 24))
1665 .apply();
1666 auto shot = screenshot();
1667 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1668 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1669}
1670
1671TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001672 sp<SurfaceControl> layer;
1673 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001674 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001675
Marissa Wallf58c14b2018-07-24 10:50:43 -07001676 // setCrop_legacy is applied immediately by default, with or without resize pending
1677 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001678 {
1679 SCOPED_TRACE("resize pending");
1680 auto shot = screenshot();
1681 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1682 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1683 }
1684
Marissa Wall61c58622018-07-18 10:12:20 -07001685 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001686 {
1687 SCOPED_TRACE("resize applied");
1688 auto shot = screenshot();
1689 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1690 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1691 }
1692}
1693
Marissa Wall61c58622018-07-18 10:12:20 -07001694TEST_F(LayerTransactionTest, SetCropWithResize_BufferState) {
1695 sp<SurfaceControl> layer;
1696 ASSERT_NO_FATAL_FAILURE(
1697 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1698 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1699
1700 // setCrop_legacy is applied immediately by default, with or without resize pending
1701 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1702 {
1703 SCOPED_TRACE("new buffer pending");
1704 auto shot = screenshot();
1705 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1706 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1707 }
1708
1709 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1710 {
1711 SCOPED_TRACE("new buffer");
1712 auto shot = screenshot();
1713 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1714 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1715 }
1716}
1717
1718TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001719 sp<SurfaceControl> layer;
1720 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001721 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001722
Marissa Wallf58c14b2018-07-24 10:50:43 -07001723 // request setCrop_legacy to be applied with the next resize
1724 Transaction()
1725 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1726 .setGeometryAppliesWithResize(layer)
1727 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001728 {
1729 SCOPED_TRACE("waiting for next resize");
1730 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1731 }
1732
Marissa Wallf58c14b2018-07-24 10:50:43 -07001733 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001734 {
1735 SCOPED_TRACE("pending crop modified");
1736 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1737 }
1738
1739 Transaction().setSize(layer, 16, 16).apply();
1740 {
1741 SCOPED_TRACE("resize pending");
1742 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1743 }
1744
1745 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001746 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001747 {
1748 SCOPED_TRACE("new crop applied");
1749 auto shot = screenshot();
1750 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1751 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1752 }
1753}
1754
Marissa Wall61c58622018-07-18 10:12:20 -07001755TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1756 sp<SurfaceControl> layer;
1757 ASSERT_NO_FATAL_FAILURE(
1758 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1759 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1760
1761 // request setCrop_legacy to be applied with the next resize
1762 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1763 {
1764 SCOPED_TRACE("set crop 1");
1765 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1766 }
1767
1768 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1769 {
1770 SCOPED_TRACE("set crop 2");
1771 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1772 }
1773
1774 Transaction().setSize(layer, 16, 16).apply();
1775 {
1776 SCOPED_TRACE("resize");
1777 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1778 }
1779
1780 // finally resize
1781 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1782 {
1783 SCOPED_TRACE("new buffer");
1784 auto shot = screenshot();
1785 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1786 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1787 }
1788}
1789
1790TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001791 sp<SurfaceControl> layer;
1792 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001793 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001794
Marissa Wallf58c14b2018-07-24 10:50:43 -07001795 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001796 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001797 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001798 .setSize(layer, 16, 16)
1799 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1800 .setGeometryAppliesWithResize(layer)
1801 .apply();
1802 {
1803 SCOPED_TRACE("new crop pending");
1804 auto shot = screenshot();
1805 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1806 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1807 }
1808
1809 // XXX crop is never latched without other geometry change (b/69315677)
1810 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001811 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001812 Transaction().setPosition(layer, 0, 0).apply();
1813 {
1814 SCOPED_TRACE("new crop applied");
1815 auto shot = screenshot();
1816 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1817 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1818 }
1819}
1820
Marissa Wall61c58622018-07-18 10:12:20 -07001821TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1822 sp<SurfaceControl> layer;
1823 ASSERT_NO_FATAL_FAILURE(
1824 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1825 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1826
1827 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1828 Transaction()
1829 .setCrop(layer, Rect(4, 4, 12, 12))
1830 .setSize(layer, 16, 16)
1831 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1832 .setGeometryAppliesWithResize(layer)
1833 .apply();
1834 {
1835 SCOPED_TRACE("new crop pending");
1836 auto shot = screenshot();
1837 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1838 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1839 }
1840
1841 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1842 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1843 Transaction().setPosition(layer, 0, 0).apply();
1844 {
1845 SCOPED_TRACE("new crop applied");
1846 auto shot = screenshot();
1847 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1848 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1849 }
1850}
1851
Marissa Wall61c58622018-07-18 10:12:20 -07001852TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1853 sp<SurfaceControl> layer;
1854 ASSERT_NO_FATAL_FAILURE(
1855 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1856
1857 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1858
1859 auto shot = screenshot();
1860 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1861 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1862}
1863
1864TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1865 sp<SurfaceControl> layer;
1866 ASSERT_NO_FATAL_FAILURE(
1867 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1868
1869 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1870
1871 {
1872 SCOPED_TRACE("set buffer 1");
1873 auto shot = screenshot();
1874 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1875 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1876 }
1877
1878 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1879
1880 {
1881 SCOPED_TRACE("set buffer 2");
1882 auto shot = screenshot();
1883 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1884 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1885 }
1886
1887 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1888
1889 {
1890 SCOPED_TRACE("set buffer 3");
1891 auto shot = screenshot();
1892 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1893 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1894 }
1895}
1896
1897TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1898 sp<SurfaceControl> layer1;
1899 ASSERT_NO_FATAL_FAILURE(
1900 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1901
1902 sp<SurfaceControl> layer2;
1903 ASSERT_NO_FATAL_FAILURE(
1904 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1905
1906 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1907
1908 {
1909 SCOPED_TRACE("set layer 1 buffer red");
1910 auto shot = screenshot();
1911 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1912 }
1913
1914 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1915
1916 {
1917 SCOPED_TRACE("set layer 2 buffer blue");
1918 auto shot = screenshot();
1919 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1920 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1921 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1922 }
1923
1924 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1925 {
1926 SCOPED_TRACE("set layer 1 buffer green");
1927 auto shot = screenshot();
1928 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1929 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1930 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1931 }
1932
1933 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1934
1935 {
1936 SCOPED_TRACE("set layer 2 buffer white");
1937 auto shot = screenshot();
1938 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1939 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1940 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1941 }
1942}
1943
1944TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1945 sp<SurfaceControl> layer;
1946 ASSERT_NO_FATAL_FAILURE(
1947 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1948
1949 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1950 Color::BLUE, Color::WHITE));
1951
1952 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1953
1954 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1955 Color::GREEN, true /* filtered */);
1956}
1957
1958TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1959 sp<SurfaceControl> layer;
1960 ASSERT_NO_FATAL_FAILURE(
1961 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1962
1963 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1964 Color::BLUE, Color::WHITE));
1965
1966 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1967
1968 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1969 Color::BLUE, true /* filtered */);
1970}
1971
1972TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
1973 sp<SurfaceControl> layer;
1974 ASSERT_NO_FATAL_FAILURE(
1975 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1976
1977 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1978 Color::BLUE, Color::WHITE));
1979
1980 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
1981
1982 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1983 Color::GREEN, true /* filtered */);
1984}
1985
1986TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
1987 sp<SurfaceControl> layer;
1988 ASSERT_NO_FATAL_FAILURE(
1989 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1990
1991 Transaction().setTransformToDisplayInverse(layer, false).apply();
1992
1993 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
1994
1995 Transaction().setTransformToDisplayInverse(layer, true).apply();
1996}
1997
1998TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
1999 sp<SurfaceControl> layer;
2000 ASSERT_NO_FATAL_FAILURE(
2001 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2002
2003 sp<GraphicBuffer> buffer =
2004 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2005 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2006 BufferUsage::COMPOSER_OVERLAY,
2007 "test");
2008 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2009
Marissa Wallfda30bb2018-10-12 11:34:28 -07002010 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002011
2012 Transaction()
2013 .setBuffer(layer, buffer)
2014 .setAcquireFence(layer, fence)
2015 .setSize(layer, 32, 32)
2016 .apply();
2017
2018 auto shot = screenshot();
2019 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2020 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2021}
2022
2023TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2024 sp<SurfaceControl> layer;
2025 ASSERT_NO_FATAL_FAILURE(
2026 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2027
2028 sp<GraphicBuffer> buffer =
2029 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2030 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2031 BufferUsage::COMPOSER_OVERLAY,
2032 "test");
2033 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2034
2035 Transaction()
2036 .setBuffer(layer, buffer)
2037 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2038 .setSize(layer, 32, 32)
2039 .apply();
2040
2041 auto shot = screenshot();
2042 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2043 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2044}
2045
2046TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2047 sp<SurfaceControl> layer;
2048 ASSERT_NO_FATAL_FAILURE(
2049 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2050
2051 sp<GraphicBuffer> buffer =
2052 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2053 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2054 BufferUsage::COMPOSER_OVERLAY,
2055 "test");
2056 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2057
2058 HdrMetadata hdrMetadata;
2059 hdrMetadata.validTypes = 0;
2060 Transaction()
2061 .setBuffer(layer, buffer)
2062 .setHdrMetadata(layer, hdrMetadata)
2063 .setSize(layer, 32, 32)
2064 .apply();
2065
2066 auto shot = screenshot();
2067 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2068 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2069}
2070
2071TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2072 sp<SurfaceControl> layer;
2073 ASSERT_NO_FATAL_FAILURE(
2074 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2075
2076 sp<GraphicBuffer> buffer =
2077 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2078 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2079 BufferUsage::COMPOSER_OVERLAY,
2080 "test");
2081 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2082
2083 Region region;
2084 region.set(32, 32);
2085 Transaction()
2086 .setBuffer(layer, buffer)
2087 .setSurfaceDamageRegion(layer, region)
2088 .setSize(layer, 32, 32)
2089 .apply();
2090
2091 auto shot = screenshot();
2092 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2093 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2094}
2095
2096TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2097 sp<SurfaceControl> layer;
2098 ASSERT_NO_FATAL_FAILURE(
2099 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2100
2101 sp<GraphicBuffer> buffer =
2102 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2103 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2104 BufferUsage::COMPOSER_OVERLAY,
2105 "test");
2106 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2107
2108 Transaction()
2109 .setBuffer(layer, buffer)
2110 .setApi(layer, NATIVE_WINDOW_API_CPU)
2111 .setSize(layer, 32, 32)
2112 .apply();
2113
2114 auto shot = screenshot();
2115 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2116 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2117}
2118
2119TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2120 sp<SurfaceControl> layer;
2121 ASSERT_NO_FATAL_FAILURE(
2122 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2123
2124 // verify this doesn't cause a crash
2125 Transaction().setSidebandStream(layer, nullptr).apply();
2126}
2127
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002128class ColorTransformHelper {
2129public:
2130 static void DegammaColorSingle(half& s) {
2131 if (s <= 0.03928f)
2132 s = s / 12.92f;
2133 else
2134 s = pow((s + 0.055f) / 1.055f, 2.4f);
2135 }
2136
2137 static void DegammaColor(half3& color) {
2138 DegammaColorSingle(color.r);
2139 DegammaColorSingle(color.g);
2140 DegammaColorSingle(color.b);
2141 }
2142
2143 static void GammaColorSingle(half& s) {
2144 if (s <= 0.0031308f) {
2145 s = s * 12.92f;
2146 } else {
2147 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2148 }
2149 }
2150
2151 static void GammaColor(half3& color) {
2152 GammaColorSingle(color.r);
2153 GammaColorSingle(color.g);
2154 GammaColorSingle(color.b);
2155 }
2156
2157 static void applyMatrix(half3& color, const mat3& mat) {
2158 half3 ret = half3(0);
2159
2160 for (int i = 0; i < 3; i++) {
2161 for (int j = 0; j < 3; j++) {
2162 ret[i] = ret[i] + color[j] * mat[j][i];
2163 }
2164 }
2165 color = ret;
2166 }
2167};
2168
Peiyong Lind3788632018-09-18 16:01:31 -07002169TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2170 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002171 ASSERT_NO_FATAL_FAILURE(colorLayer =
2172 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2173 ISurfaceComposerClient::eFXSurfaceColor));
2174 Transaction()
2175 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2176 .setLayer(colorLayer, mLayerZBase + 1)
2177 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002178 {
2179 SCOPED_TRACE("default color");
2180 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2181 }
2182
2183 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002184 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002185 mat3 matrix;
2186 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2187 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2188 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002189
2190 // degamma before applying the matrix
2191 if (mColorManagementUsed) {
2192 ColorTransformHelper::DegammaColor(expected);
2193 }
2194
2195 ColorTransformHelper::applyMatrix(expected, matrix);
2196
2197 if (mColorManagementUsed) {
2198 ColorTransformHelper::GammaColor(expected);
2199 }
2200
2201 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2202 uint8_t(expected.b * 255), 255};
2203
2204 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2205 // channel) should be less than one
2206 const uint8_t tolerance = 1;
2207
Peiyong Lind3788632018-09-18 16:01:31 -07002208 Transaction().setColor(colorLayer, color)
2209 .setColorTransform(colorLayer, matrix, vec3()).apply();
2210 {
2211 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002212 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002213 }
2214}
2215
Marissa Wallfda30bb2018-10-12 11:34:28 -07002216class ExpectedResult {
2217public:
2218 enum Transaction {
2219 NOT_PRESENTED = 0,
2220 PRESENTED,
2221 };
2222
2223 enum Buffer {
2224 NOT_ACQUIRED = 0,
2225 ACQUIRED,
2226 };
2227
2228 enum PreviousBuffer {
2229 NOT_RELEASED = 0,
2230 RELEASED,
2231 };
2232
2233 void reset() {
2234 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2235 mExpectedSurfaceResults.clear();
2236 }
2237
2238 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
2239 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2240 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2241 mTransactionResult = transactionResult;
2242 mExpectedSurfaceResults.emplace(std::piecewise_construct,
2243 std::forward_as_tuple(layer->getHandle()),
2244 std::forward_as_tuple(bufferResult, previousBufferResult));
2245 }
2246
2247 void addSurfaces(ExpectedResult::Transaction transactionResult,
2248 const std::vector<sp<SurfaceControl>>& layers,
2249 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2250 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2251 for (const auto& layer : layers) {
2252 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2253 }
2254 }
2255
2256 void verifyTransactionStats(const TransactionStats& transactionStats) const {
2257 const auto& [latchTime, presentTime, surfaceStats] = transactionStats;
2258 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2259 ASSERT_GE(latchTime, 0) << "bad latch time";
2260 ASSERT_GE(presentTime, 0) << "bad present time";
2261 } else {
2262 ASSERT_EQ(presentTime, -1) << "transaction shouldn't have been presented";
2263 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2264 }
2265
2266 ASSERT_EQ(surfaceStats.size(), mExpectedSurfaceResults.size())
2267 << "wrong number of surfaces";
2268
2269 for (const auto& stats : surfaceStats) {
2270 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2271 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2272 << "unexpected surface control";
2273 expectedSurfaceResult->second.verifySurfaceStats(stats, latchTime);
2274 }
2275 }
2276
2277private:
2278 class ExpectedSurfaceResult {
2279 public:
2280 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2281 ExpectedResult::PreviousBuffer previousBufferResult)
2282 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2283
2284 void verifySurfaceStats(const SurfaceStats& surfaceStats, nsecs_t latchTime) const {
2285 const auto& [surfaceControl, acquireTime, releasePreviousBuffer] = surfaceStats;
2286
2287 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2288 << "bad acquire time";
2289 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
2290 ASSERT_EQ(releasePreviousBuffer,
2291 mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED)
2292 << "bad previous buffer released";
2293 }
2294
2295 private:
2296 ExpectedResult::Buffer mBufferResult;
2297 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2298 };
2299
2300 struct IBinderHash {
2301 std::size_t operator()(const sp<IBinder>& strongPointer) const {
2302 return std::hash<IBinder*>{}(strongPointer.get());
2303 }
2304 };
2305 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2306 std::unordered_map<sp<IBinder>, ExpectedSurfaceResult, IBinderHash> mExpectedSurfaceResults;
2307};
2308
2309class CallbackHelper {
2310public:
2311 static void function(void* callbackContext, const TransactionStats& transactionStats) {
2312 if (!callbackContext) {
2313 ALOGE("failed to get callback context");
2314 }
2315 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2316 std::lock_guard lock(helper->mMutex);
2317 helper->mTransactionStatsQueue.push(transactionStats);
2318 helper->mConditionVariable.notify_all();
2319 }
2320
2321 void getTransactionStats(TransactionStats* outStats) {
2322 std::unique_lock lock(mMutex);
2323
2324 if (mTransactionStatsQueue.empty()) {
2325 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2326 std::cv_status::timeout)
2327 << "did not receive callback";
2328 }
2329
2330 *outStats = std::move(mTransactionStatsQueue.front());
2331 mTransactionStatsQueue.pop();
2332 }
2333
2334 void verifyFinalState() {
2335 // Wait to see if there are extra callbacks
2336 std::this_thread::sleep_for(500ms);
2337
2338 std::lock_guard lock(mMutex);
2339 EXPECT_EQ(mTransactionStatsQueue.size(), 0) << "extra callbacks received";
2340 mTransactionStatsQueue = {};
2341 }
2342
2343 void* getContext() { return static_cast<void*>(this); }
2344
2345 std::mutex mMutex;
2346 std::condition_variable mConditionVariable;
2347 std::queue<TransactionStats> mTransactionStatsQueue;
2348};
2349
2350class LayerCallbackTest : public LayerTransactionTest {
2351protected:
2352 virtual sp<SurfaceControl> createBufferStateLayer() {
2353 return createLayer(mClient, "test", mWidth, mHeight,
2354 ISurfaceComposerClient::eFXSurfaceBufferState);
2355 }
2356
2357 virtual void fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2358 const sp<SurfaceControl>& layer = nullptr) {
2359 if (layer) {
2360 sp<GraphicBuffer> buffer =
2361 new GraphicBuffer(mWidth, mHeight, PIXEL_FORMAT_RGBA_8888, 1,
2362 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2363 BufferUsage::COMPOSER_OVERLAY |
2364 BufferUsage::GPU_TEXTURE,
2365 "test");
2366 fillGraphicBufferColor(buffer, Rect(0, 0, mWidth, mHeight), Color::RED);
2367
2368 sp<Fence> fence = new Fence(-1);
2369
2370 transaction.setBuffer(layer, buffer)
2371 .setAcquireFence(layer, fence)
2372 .setSize(layer, mWidth, mHeight);
2373 }
2374
2375 transaction.addTransactionCompletedCallback(callbackHelper->function,
2376 callbackHelper->getContext());
2377 }
2378
2379 void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2380 bool finalState = false) {
2381 TransactionStats transactionStats;
2382 ASSERT_NO_FATAL_FAILURE(helper.getTransactionStats(&transactionStats));
2383 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyTransactionStats(transactionStats));
2384
2385 if (finalState) {
2386 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2387 }
2388 }
2389
2390 void waitForCallbacks(CallbackHelper& helper,
2391 const std::vector<ExpectedResult>& expectedResults,
2392 bool finalState = false) {
2393 for (const auto& expectedResult : expectedResults) {
2394 waitForCallback(helper, expectedResult);
2395 }
2396 if (finalState) {
2397 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2398 }
2399 }
2400
2401 uint32_t mWidth = 32;
2402 uint32_t mHeight = 32;
2403};
2404
2405TEST_F(LayerCallbackTest, Basic) {
2406 sp<SurfaceControl> layer;
2407 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2408
2409 Transaction transaction;
2410 CallbackHelper callback;
2411 fillTransaction(transaction, &callback, layer);
2412
2413 transaction.apply();
2414
2415 ExpectedResult expected;
2416 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2417 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2418}
2419
2420TEST_F(LayerCallbackTest, NoBuffer) {
2421 sp<SurfaceControl> layer;
2422 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2423
2424 Transaction transaction;
2425 CallbackHelper callback;
2426 fillTransaction(transaction, &callback);
2427
2428 transaction.setPosition(layer, mWidth, mHeight).apply();
2429
2430 ExpectedResult expected;
2431 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2432 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2433}
2434
2435TEST_F(LayerCallbackTest, NoStateChange) {
2436 Transaction transaction;
2437 CallbackHelper callback;
2438 fillTransaction(transaction, &callback);
2439
2440 transaction.apply();
2441
2442 ExpectedResult expected;
2443 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2444}
2445
2446TEST_F(LayerCallbackTest, OffScreen) {
2447 sp<SurfaceControl> layer;
2448 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2449
2450 Transaction transaction;
2451 CallbackHelper callback;
2452 fillTransaction(transaction, &callback, layer);
2453
2454 transaction.setPosition(layer, -100, -100).apply();
2455
2456 ExpectedResult expected;
2457 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2458 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2459}
2460
2461TEST_F(LayerCallbackTest, Merge) {
2462 sp<SurfaceControl> layer1, layer2;
2463 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2464 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2465
2466 Transaction transaction1, transaction2;
2467 CallbackHelper callback1, callback2;
2468 fillTransaction(transaction1, &callback1, layer1);
2469 fillTransaction(transaction2, &callback2, layer2);
2470
2471 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2472
2473 ExpectedResult expected;
2474 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2475 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2476 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2477}
2478
2479TEST_F(LayerCallbackTest, Merge_SameCallback) {
2480 sp<SurfaceControl> layer1, layer2;
2481 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2482 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2483
2484 Transaction transaction1, transaction2;
2485 CallbackHelper callback;
2486 fillTransaction(transaction1, &callback, layer1);
2487 fillTransaction(transaction2, &callback, layer2);
2488
2489 transaction2.merge(std::move(transaction1)).apply();
2490
2491 ExpectedResult expected;
2492 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2493 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2494 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2495}
2496
2497TEST_F(LayerCallbackTest, Merge_SameLayer) {
2498 sp<SurfaceControl> layer;
2499 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2500
2501 Transaction transaction1, transaction2;
2502 CallbackHelper callback1, callback2;
2503 fillTransaction(transaction1, &callback1, layer);
2504 fillTransaction(transaction2, &callback2, layer);
2505
2506 transaction2.merge(std::move(transaction1)).apply();
2507
2508 ExpectedResult expected;
2509 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2510 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2511 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2512}
2513
2514TEST_F(LayerCallbackTest, Merge_SingleBuffer) {
2515 sp<SurfaceControl> layer1, layer2;
2516 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2517 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2518
2519 Transaction transaction1, transaction2;
2520 CallbackHelper callback1, callback2;
2521 fillTransaction(transaction1, &callback1, layer1);
2522 fillTransaction(transaction2, &callback2);
2523
2524 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2525
2526 ExpectedResult expected;
2527 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2528 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2529 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2530}
2531
2532TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2533 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2534 client2(new SurfaceComposerClient);
2535
2536 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2537 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2538
2539 sp<SurfaceControl> layer1, layer2;
2540 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2541 ISurfaceComposerClient::eFXSurfaceBufferState));
2542 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2543 ISurfaceComposerClient::eFXSurfaceBufferState));
2544
2545 Transaction transaction1, transaction2;
2546 CallbackHelper callback1, callback2;
2547 fillTransaction(transaction1, &callback1, layer1);
2548 fillTransaction(transaction2, &callback2, layer2);
2549
2550 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2551
2552 ExpectedResult expected;
2553 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2554 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2555 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2556}
2557
2558TEST_F(LayerCallbackTest, MultipleTransactions) {
2559 sp<SurfaceControl> layer;
2560 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2561
2562 Transaction transaction;
2563 CallbackHelper callback;
2564 for (size_t i = 0; i < 10; i++) {
2565 fillTransaction(transaction, &callback, layer);
2566
2567 transaction.apply();
2568
2569 ExpectedResult expected;
2570 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2571 ExpectedResult::Buffer::NOT_ACQUIRED,
2572 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2573 : ExpectedResult::PreviousBuffer::RELEASED);
2574 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2575 }
2576 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2577}
2578
2579TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
2580 sp<SurfaceControl> layer;
2581 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2582
2583 Transaction transaction;
2584 CallbackHelper callback;
2585 for (size_t i = 0; i < 10; i++) {
2586 ExpectedResult expected;
2587
2588 if (i == 0) {
2589 fillTransaction(transaction, &callback, layer);
2590 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2591 } else {
2592 fillTransaction(transaction, &callback);
2593 }
2594
2595 transaction.apply();
2596
2597 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2598 }
2599 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2600}
2601
2602TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
2603 sp<SurfaceControl> layer;
2604 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2605
2606 Transaction transaction;
2607 CallbackHelper callback;
2608 for (size_t i = 0; i < 10; i++) {
2609 if (i == 0) {
2610 fillTransaction(transaction, &callback, layer);
2611 } else {
2612 fillTransaction(transaction, &callback);
2613 }
2614
2615 transaction.setPosition(layer, mWidth, mHeight).apply();
2616
2617 ExpectedResult expected;
2618 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
2619 : ExpectedResult::Transaction::NOT_PRESENTED,
2620 layer);
2621 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
2622 }
2623 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2624}
2625
2626TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
2627 sp<SurfaceControl> layer1, layer2;
2628 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2629 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2630
2631 Transaction transaction1, transaction2;
2632 CallbackHelper callback1, callback2;
2633 for (size_t i = 0; i < 10; i++) {
2634 fillTransaction(transaction1, &callback1, layer1);
2635 fillTransaction(transaction2, &callback2, layer2);
2636
2637 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2638
2639 ExpectedResult expected;
2640 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2641 ExpectedResult::Buffer::NOT_ACQUIRED,
2642 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2643 : ExpectedResult::PreviousBuffer::RELEASED);
2644 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2645 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2646 }
2647 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2648 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2649}
2650
2651TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
2652 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2653 client2(new SurfaceComposerClient);
2654 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2655 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2656
2657 sp<SurfaceControl> layer1, layer2;
2658 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2659 ISurfaceComposerClient::eFXSurfaceBufferState));
2660 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2661 ISurfaceComposerClient::eFXSurfaceBufferState));
2662
2663 Transaction transaction1, transaction2;
2664 CallbackHelper callback1, callback2;
2665 for (size_t i = 0; i < 10; i++) {
2666 fillTransaction(transaction1, &callback1, layer1);
2667 fillTransaction(transaction2, &callback2, layer2);
2668
2669 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2670
2671 ExpectedResult expected;
2672 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2673 ExpectedResult::Buffer::NOT_ACQUIRED,
2674 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2675 : ExpectedResult::PreviousBuffer::RELEASED);
2676 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2677 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2678 }
2679 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2680 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2681}
2682
2683TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
2684 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2685 client2(new SurfaceComposerClient);
2686 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2687 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2688
2689 sp<SurfaceControl> layer1, layer2;
2690 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2691 ISurfaceComposerClient::eFXSurfaceBufferState));
2692 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2693 ISurfaceComposerClient::eFXSurfaceBufferState));
2694
2695 Transaction transaction1, transaction2;
2696 CallbackHelper callback1, callback2;
2697
2698 // Normal call to set up test
2699 fillTransaction(transaction1, &callback1, layer1);
2700 fillTransaction(transaction2, &callback2, layer2);
2701
2702 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2703
2704 ExpectedResult expected;
2705 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2706 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2707 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2708 expected.reset();
2709
2710 // Test
2711 fillTransaction(transaction1, &callback1);
2712 fillTransaction(transaction2, &callback2);
2713
2714 transaction2.merge(std::move(transaction1)).apply();
2715
2716 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2717 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2718}
2719
2720TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
2721 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2722 client2(new SurfaceComposerClient);
2723
2724 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2725 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2726
2727 sp<SurfaceControl> layer1, layer2;
2728 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2729 ISurfaceComposerClient::eFXSurfaceBufferState));
2730 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2731 ISurfaceComposerClient::eFXSurfaceBufferState));
2732
2733 Transaction transaction1, transaction2;
2734 CallbackHelper callback1, callback2;
2735
2736 // Normal call to set up test
2737 fillTransaction(transaction1, &callback1, layer1);
2738 fillTransaction(transaction2, &callback2, layer2);
2739
2740 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2741
2742 ExpectedResult expected;
2743 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2744 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2745 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2746 expected.reset();
2747
2748 // Test
2749 fillTransaction(transaction1, &callback1);
2750 fillTransaction(transaction2, &callback2);
2751
2752 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2753
2754 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2);
2755 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2756 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2757}
2758
2759TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
2760 sp<SurfaceControl> layer;
2761 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2762
2763 Transaction transaction;
2764 CallbackHelper callback;
2765 std::vector<ExpectedResult> expectedResults(50);
2766 ExpectedResult::PreviousBuffer previousBufferResult =
2767 ExpectedResult::PreviousBuffer::NOT_RELEASED;
2768 for (auto& expected : expectedResults) {
2769 expected.reset();
2770 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2771 ExpectedResult::Buffer::NOT_ACQUIRED, previousBufferResult);
2772 previousBufferResult = ExpectedResult::PreviousBuffer::RELEASED;
2773
2774 fillTransaction(transaction, &callback, layer);
2775
2776 transaction.apply();
2777 std::this_thread::sleep_for(200ms);
2778 }
2779 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2780}
2781
2782TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
2783 sp<SurfaceControl> layer;
2784 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2785
2786 Transaction transaction;
2787 CallbackHelper callback;
2788 std::vector<ExpectedResult> expectedResults(50);
2789 bool first = true;
2790 for (auto& expected : expectedResults) {
2791 expected.reset();
2792
2793 if (first) {
2794 fillTransaction(transaction, &callback, layer);
2795 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2796 first = false;
2797 } else {
2798 fillTransaction(transaction, &callback);
2799 }
2800
2801 transaction.apply();
2802 std::this_thread::sleep_for(200ms);
2803 }
2804 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2805}
2806
2807TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
2808 sp<SurfaceControl> layer;
2809 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2810
2811 // Normal call to set up test
2812 Transaction transaction;
2813 CallbackHelper callback;
2814 fillTransaction(transaction, &callback, layer);
2815
2816 transaction.setPosition(layer, mWidth, mHeight).apply();
2817
2818 ExpectedResult expectedResult;
2819 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2820 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
2821
2822 // Test
2823 std::vector<ExpectedResult> expectedResults(50);
2824 for (auto& expected : expectedResults) {
2825 expected.reset();
2826 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2827
2828 fillTransaction(transaction, &callback);
2829
2830 transaction.setPosition(layer, mWidth, mHeight).apply();
2831
2832 std::this_thread::sleep_for(200ms);
2833 }
2834 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2835}
2836
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002837class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002838protected:
2839 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002840 LayerTransactionTest::SetUp();
2841 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002842
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002843 sp<IBinder> display(
2844 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002845 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002846 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002847
2848 ssize_t displayWidth = info.w;
2849 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002850
2851 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002852 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2853 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002854 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002855 ASSERT_TRUE(mBGSurfaceControl->isValid());
2856 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2857
2858 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002859 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2860
Peiyong Lin566a3b42018-01-09 18:22:43 -08002861 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002862 ASSERT_TRUE(mFGSurfaceControl->isValid());
2863
2864 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2865
2866 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002867 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002868 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002869 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2870
2871 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2872
Robert Carr4cdc58f2017-08-23 14:22:20 -07002873 asTransaction([&](Transaction& t) {
2874 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002875
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002876 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002877
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002878 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2879 .setPosition(mFGSurfaceControl, 64, 64)
2880 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002881
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002882 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2883 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2884 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002885 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002886 }
2887
2888 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002889 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002890 mBGSurfaceControl = 0;
2891 mFGSurfaceControl = 0;
2892 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002893 }
2894
2895 void waitForPostedBuffers() {
2896 // Since the sync surface is in synchronous mode (i.e. double buffered)
2897 // posting three buffers to it should ensure that at least two
2898 // SurfaceFlinger::handlePageFlip calls have been made, which should
2899 // guaranteed that a buffer posted to another Surface has been retired.
2900 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2901 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2902 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2903 }
2904
Robert Carr4cdc58f2017-08-23 14:22:20 -07002905 void asTransaction(const std::function<void(Transaction&)>& exec) {
2906 Transaction t;
2907 exec(t);
2908 t.apply(true);
2909 }
2910
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002911 sp<SurfaceControl> mBGSurfaceControl;
2912 sp<SurfaceControl> mFGSurfaceControl;
2913
2914 // This surface is used to ensure that the buffers posted to
2915 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2916 sp<SurfaceControl> mSyncSurfaceControl;
2917};
2918
Robert Carr7f619b22017-11-06 12:56:35 -08002919TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08002920
chaviw0e3479f2018-09-10 16:49:30 -07002921 std::unique_ptr<ScreenCapture> sc;
2922
2923 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002924 fillSurfaceRGBA8(relative, 10, 10, 10);
2925 waitForPostedBuffers();
2926
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002927 Transaction{}
2928 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002929 .setPosition(relative, 64, 64)
2930 .apply();
2931
2932 {
2933 // The relative should be on top of the FG control.
2934 ScreenCapture::captureScreen(&sc);
2935 sc->checkPixel(64, 64, 10, 10, 10);
2936 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002937 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002938
2939 {
2940 // Nothing should change at this point.
2941 ScreenCapture::captureScreen(&sc);
2942 sc->checkPixel(64, 64, 10, 10, 10);
2943 }
2944
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002945 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002946
2947 {
2948 // Ensure that the relative was actually hidden, rather than
2949 // being left in the detached but visible state.
2950 ScreenCapture::captureScreen(&sc);
2951 sc->expectFGColor(64, 64);
2952 }
2953}
2954
Robert Carr8d5227b2017-03-16 15:41:03 -07002955class GeometryLatchingTest : public LayerUpdateTest {
2956protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002957 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002958 SCOPED_TRACE(trace);
2959 ScreenCapture::captureScreen(&sc);
2960 // We find the leading edge of the FG surface.
2961 sc->expectFGColor(127, 127);
2962 sc->expectBGColor(128, 128);
2963 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002964
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002965 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002966
2967 void unlockFGBuffer() {
2968 sp<Surface> s = mFGSurfaceControl->getSurface();
2969 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2970 waitForPostedBuffers();
2971 }
2972
Robert Carr8d5227b2017-03-16 15:41:03 -07002973 void completeFGResize() {
2974 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2975 waitForPostedBuffers();
2976 }
2977 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002978 asTransaction([&](Transaction& t) {
2979 t.setSize(mFGSurfaceControl, 64, 64);
2980 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002981 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002982 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002983
2984 EXPECT_INITIAL_STATE("After restoring initial state");
2985 }
chaviw0e3479f2018-09-10 16:49:30 -07002986 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07002987};
2988
Robert Carr8d5227b2017-03-16 15:41:03 -07002989class CropLatchingTest : public GeometryLatchingTest {
2990protected:
2991 void EXPECT_CROPPED_STATE(const char* trace) {
2992 SCOPED_TRACE(trace);
2993 ScreenCapture::captureScreen(&sc);
2994 // The edge should be moved back one pixel by our crop.
2995 sc->expectFGColor(126, 126);
2996 sc->expectBGColor(127, 127);
2997 sc->expectBGColor(128, 128);
2998 }
chaviw59f5c562017-06-28 16:39:06 -07002999
3000 void EXPECT_RESIZE_STATE(const char* trace) {
3001 SCOPED_TRACE(trace);
3002 ScreenCapture::captureScreen(&sc);
3003 // The FG is now resized too 128,128 at 64,64
3004 sc->expectFGColor(64, 64);
3005 sc->expectFGColor(191, 191);
3006 sc->expectBGColor(192, 192);
3007 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003008};
3009
Pablo Ceballos05289c22016-04-14 15:49:55 -07003010TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003011 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003012 {
3013 SCOPED_TRACE("before anything");
3014 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003015 sc->expectBGColor(32, 32);
3016 sc->expectFGColor(96, 96);
3017 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003018 }
3019
3020 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003021 asTransaction([&](Transaction& t) {
3022 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003023 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3024 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003025 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003026
Robert Carr4cdc58f2017-08-23 14:22:20 -07003027 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003028 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003029 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3030 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003031 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003032
3033 {
3034 SCOPED_TRACE("before any trigger");
3035 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003036 sc->expectBGColor(32, 32);
3037 sc->expectFGColor(96, 96);
3038 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003039 }
3040
3041 // should trigger the first deferred transaction, but not the second one
3042 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3043 {
3044 SCOPED_TRACE("after first trigger");
3045 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003046 sc->expectBGColor(32, 32);
3047 sc->checkPixel(96, 96, 162, 63, 96);
3048 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003049 }
3050
3051 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003052 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003053
3054 // trigger the second deferred transaction
3055 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3056 {
3057 SCOPED_TRACE("after second trigger");
3058 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003059 sc->expectBGColor(32, 32);
3060 sc->expectBGColor(96, 96);
3061 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003062 }
3063}
3064
Robert Carre392b552017-09-19 12:16:05 -07003065TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003066 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003067
3068 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003069 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3070 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3071 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3072 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003073 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003074 SurfaceComposerClient::Transaction{}
3075 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3076 .show(childNoBuffer)
3077 .show(childBuffer)
3078 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003079 {
3080 ScreenCapture::captureScreen(&sc);
3081 sc->expectChildColor(73, 73);
3082 sc->expectFGColor(74, 74);
3083 }
Vishnu Nair60356342018-11-13 13:00:45 -08003084 SurfaceComposerClient::Transaction{}
3085 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3086 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003087 {
3088 ScreenCapture::captureScreen(&sc);
3089 sc->expectChildColor(73, 73);
3090 sc->expectChildColor(74, 74);
3091 }
3092}
3093
Robert Carr2c5f6d22017-09-26 12:30:35 -07003094TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003095 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003096 {
3097 SCOPED_TRACE("before move");
3098 ScreenCapture::captureScreen(&sc);
3099 sc->expectBGColor(0, 12);
3100 sc->expectFGColor(75, 75);
3101 sc->expectBGColor(145, 145);
3102 }
3103
3104 Transaction t1, t2;
3105 t1.setPosition(mFGSurfaceControl, 128, 128);
3106 t2.setPosition(mFGSurfaceControl, 0, 0);
3107 // We expect that the position update from t2 now
3108 // overwrites the position update from t1.
3109 t1.merge(std::move(t2));
3110 t1.apply();
3111
3112 {
3113 ScreenCapture::captureScreen(&sc);
3114 sc->expectFGColor(1, 1);
3115 }
3116}
3117
Robert Carr1f0a16a2016-10-24 16:27:39 -07003118class ChildLayerTest : public LayerUpdateTest {
3119protected:
3120 void SetUp() override {
3121 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003122 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3123 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003124 fillSurfaceRGBA8(mChild, 200, 200, 200);
3125
3126 {
3127 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003128 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003129 mCapture->expectChildColor(64, 64);
3130 }
3131 }
3132 void TearDown() override {
3133 LayerUpdateTest::TearDown();
3134 mChild = 0;
3135 }
3136
3137 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003138 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003139};
3140
3141TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003142 asTransaction([&](Transaction& t) {
3143 t.show(mChild);
3144 t.setPosition(mChild, 10, 10);
3145 t.setPosition(mFGSurfaceControl, 64, 64);
3146 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003147
3148 {
chaviw0e3479f2018-09-10 16:49:30 -07003149 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003150 // Top left of foreground must now be visible
3151 mCapture->expectFGColor(64, 64);
3152 // But 10 pixels in we should see the child surface
3153 mCapture->expectChildColor(74, 74);
3154 // And 10 more pixels we should be back to the foreground surface
3155 mCapture->expectFGColor(84, 84);
3156 }
3157
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003158 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003159
3160 {
chaviw0e3479f2018-09-10 16:49:30 -07003161 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003162 // Top left of foreground should now be at 0, 0
3163 mCapture->expectFGColor(0, 0);
3164 // But 10 pixels in we should see the child surface
3165 mCapture->expectChildColor(10, 10);
3166 // And 10 more pixels we should be back to the foreground surface
3167 mCapture->expectFGColor(20, 20);
3168 }
3169}
3170
Robert Carr41b08b52017-06-01 16:11:34 -07003171TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003172 asTransaction([&](Transaction& t) {
3173 t.show(mChild);
3174 t.setPosition(mChild, 0, 0);
3175 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003176 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003177 });
Robert Carr41b08b52017-06-01 16:11:34 -07003178
3179 {
chaviw0e3479f2018-09-10 16:49:30 -07003180 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003181 mCapture->expectChildColor(0, 0);
3182 mCapture->expectChildColor(4, 4);
3183 mCapture->expectBGColor(5, 5);
3184 }
3185}
3186
Robert Carr1f0a16a2016-10-24 16:27:39 -07003187TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003188 asTransaction([&](Transaction& t) {
3189 t.show(mChild);
3190 t.setPosition(mFGSurfaceControl, 0, 0);
3191 t.setPosition(mChild, 63, 63);
3192 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003193
3194 {
chaviw0e3479f2018-09-10 16:49:30 -07003195 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003196 mCapture->expectFGColor(0, 0);
3197 // Last pixel in foreground should now be the child.
3198 mCapture->expectChildColor(63, 63);
3199 // But the child should be constrained and the next pixel
3200 // must be the background
3201 mCapture->expectBGColor(64, 64);
3202 }
3203}
3204
3205TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003206 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003207
3208 // Find the boundary between the parent and child
3209 {
chaviw0e3479f2018-09-10 16:49:30 -07003210 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003211 mCapture->expectChildColor(9, 9);
3212 mCapture->expectFGColor(10, 10);
3213 }
3214
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003215 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003216
3217 // The boundary should be twice as far from the origin now.
3218 // The pixels from the last test should all be child now
3219 {
chaviw0e3479f2018-09-10 16:49:30 -07003220 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003221 mCapture->expectChildColor(9, 9);
3222 mCapture->expectChildColor(10, 10);
3223 mCapture->expectChildColor(19, 19);
3224 mCapture->expectFGColor(20, 20);
3225 }
3226}
Robert Carr9524cb32017-02-13 11:32:32 -08003227
Robert Carr6452f122017-03-21 10:41:29 -07003228TEST_F(ChildLayerTest, ChildLayerAlpha) {
3229 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3230 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3231 fillSurfaceRGBA8(mChild, 0, 254, 0);
3232 waitForPostedBuffers();
3233
Robert Carr4cdc58f2017-08-23 14:22:20 -07003234 asTransaction([&](Transaction& t) {
3235 t.show(mChild);
3236 t.setPosition(mChild, 0, 0);
3237 t.setPosition(mFGSurfaceControl, 0, 0);
3238 });
Robert Carr6452f122017-03-21 10:41:29 -07003239
3240 {
chaviw0e3479f2018-09-10 16:49:30 -07003241 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003242 // Unblended child color
3243 mCapture->checkPixel(0, 0, 0, 254, 0);
3244 }
3245
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003246 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003247
3248 {
chaviw0e3479f2018-09-10 16:49:30 -07003249 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003250 // Child and BG blended.
3251 mCapture->checkPixel(0, 0, 127, 127, 0);
3252 }
3253
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003254 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003255
3256 {
chaviw0e3479f2018-09-10 16:49:30 -07003257 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003258 // Child and BG blended.
3259 mCapture->checkPixel(0, 0, 95, 64, 95);
3260 }
3261}
3262
Robert Carr9524cb32017-02-13 11:32:32 -08003263TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003264 asTransaction([&](Transaction& t) {
3265 t.show(mChild);
3266 t.setPosition(mChild, 10, 10);
3267 t.setPosition(mFGSurfaceControl, 64, 64);
3268 });
Robert Carr9524cb32017-02-13 11:32:32 -08003269
3270 {
chaviw0e3479f2018-09-10 16:49:30 -07003271 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003272 // Top left of foreground must now be visible
3273 mCapture->expectFGColor(64, 64);
3274 // But 10 pixels in we should see the child surface
3275 mCapture->expectChildColor(74, 74);
3276 // And 10 more pixels we should be back to the foreground surface
3277 mCapture->expectFGColor(84, 84);
3278 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003279
3280 asTransaction([&](Transaction& t) {
3281 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
3282 });
3283
Robert Carr9524cb32017-02-13 11:32:32 -08003284 {
chaviw0e3479f2018-09-10 16:49:30 -07003285 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003286 mCapture->expectFGColor(64, 64);
3287 // In reparenting we should have exposed the entire foreground surface.
3288 mCapture->expectFGColor(74, 74);
3289 // And the child layer should now begin at 10, 10 (since the BG
3290 // layer is at (0, 0)).
3291 mCapture->expectBGColor(9, 9);
3292 mCapture->expectChildColor(10, 10);
3293 }
3294}
3295
Robert Carr2e102c92018-10-23 12:11:15 -07003296TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
3297 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003298 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07003299 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
3300
3301 {
3302 SCOPED_TRACE("Grandchild visible");
3303 ScreenCapture::captureScreen(&mCapture);
3304 mCapture->checkPixel(64, 64, 111, 111, 111);
3305 }
3306
3307 mChild->clear();
3308
3309 {
3310 SCOPED_TRACE("After destroying child");
3311 ScreenCapture::captureScreen(&mCapture);
3312 mCapture->expectFGColor(64, 64);
3313 }
3314
3315 asTransaction([&](Transaction& t) {
3316 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
3317 });
3318
3319 {
3320 SCOPED_TRACE("After reparenting grandchild");
3321 ScreenCapture::captureScreen(&mCapture);
3322 mCapture->checkPixel(64, 64, 111, 111, 111);
3323 }
3324}
3325
chaviw161410b02017-07-27 10:46:08 -07003326TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003327 asTransaction([&](Transaction& t) {
3328 t.show(mChild);
3329 t.setPosition(mChild, 10, 10);
3330 t.setPosition(mFGSurfaceControl, 64, 64);
3331 });
Robert Carr9524cb32017-02-13 11:32:32 -08003332
3333 {
chaviw0e3479f2018-09-10 16:49:30 -07003334 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003335 // Top left of foreground must now be visible
3336 mCapture->expectFGColor(64, 64);
3337 // But 10 pixels in we should see the child surface
3338 mCapture->expectChildColor(74, 74);
3339 // And 10 more pixels we should be back to the foreground surface
3340 mCapture->expectFGColor(84, 84);
3341 }
3342
chaviw0e3479f2018-09-10 16:49:30 -07003343
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003344 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08003345
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003346 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08003347
chaviw161410b02017-07-27 10:46:08 -07003348 // Since the child has the same client as the parent, it will not get
3349 // detached and will be hidden.
3350 {
chaviw0e3479f2018-09-10 16:49:30 -07003351 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003352 mCapture->expectFGColor(64, 64);
3353 mCapture->expectFGColor(74, 74);
3354 mCapture->expectFGColor(84, 84);
3355 }
3356}
3357
3358TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
3359 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003360 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003361 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
3362 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07003363
chaviw161410b02017-07-27 10:46:08 -07003364 ASSERT_TRUE(mChildNewClient->isValid());
3365
3366 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
3367
Robert Carr4cdc58f2017-08-23 14:22:20 -07003368 asTransaction([&](Transaction& t) {
3369 t.hide(mChild);
3370 t.show(mChildNewClient);
3371 t.setPosition(mChildNewClient, 10, 10);
3372 t.setPosition(mFGSurfaceControl, 64, 64);
3373 });
chaviw161410b02017-07-27 10:46:08 -07003374
3375 {
chaviw0e3479f2018-09-10 16:49:30 -07003376 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003377 // Top left of foreground must now be visible
3378 mCapture->expectFGColor(64, 64);
3379 // But 10 pixels in we should see the child surface
3380 mCapture->expectChildColor(74, 74);
3381 // And 10 more pixels we should be back to the foreground surface
3382 mCapture->expectFGColor(84, 84);
3383 }
3384
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003385 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07003386
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003387 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07003388
Robert Carr9524cb32017-02-13 11:32:32 -08003389 // Nothing should have changed.
3390 {
chaviw0e3479f2018-09-10 16:49:30 -07003391 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003392 mCapture->expectFGColor(64, 64);
3393 mCapture->expectChildColor(74, 74);
3394 mCapture->expectFGColor(84, 84);
3395 }
3396}
3397
chaviw5aedec92018-10-22 10:40:38 -07003398TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
3399 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
3400 sp<SurfaceControl> childNewClient =
3401 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
3402 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3403
3404 ASSERT_TRUE(childNewClient != nullptr);
3405 ASSERT_TRUE(childNewClient->isValid());
3406
3407 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
3408
3409 Transaction()
3410 .hide(mChild)
3411 .show(childNewClient)
3412 .setPosition(childNewClient, 10, 10)
3413 .setPosition(mFGSurfaceControl, 64, 64)
3414 .apply();
3415
3416 {
3417 mCapture = screenshot();
3418 // Top left of foreground must now be visible
3419 mCapture->expectFGColor(64, 64);
3420 // But 10 pixels in we should see the child surface
3421 mCapture->expectChildColor(74, 74);
3422 // And 10 more pixels we should be back to the foreground surface
3423 mCapture->expectFGColor(84, 84);
3424 }
3425
3426 Transaction().detachChildren(mFGSurfaceControl).apply();
3427 Transaction().hide(childNewClient).apply();
3428
3429 // Nothing should have changed.
3430 {
3431 mCapture = screenshot();
3432 mCapture->expectFGColor(64, 64);
3433 mCapture->expectChildColor(74, 74);
3434 mCapture->expectFGColor(84, 84);
3435 }
3436
3437 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
3438 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
3439 32);
3440 Transaction()
3441 .setLayer(newParentSurface, INT32_MAX - 1)
3442 .show(newParentSurface)
3443 .setPosition(newParentSurface, 20, 20)
3444 .reparent(childNewClient, newParentSurface->getHandle())
3445 .apply();
3446 {
3447 mCapture = screenshot();
3448 // Child is now hidden.
3449 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
3450 }
3451}
3452
Robert Carr9b429f42017-04-17 14:56:57 -07003453TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003454 asTransaction([&](Transaction& t) {
3455 t.show(mChild);
3456 t.setPosition(mChild, 0, 0);
3457 t.setPosition(mFGSurfaceControl, 0, 0);
3458 });
Robert Carr9b429f42017-04-17 14:56:57 -07003459
3460 {
chaviw0e3479f2018-09-10 16:49:30 -07003461 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003462 // We've positioned the child in the top left.
3463 mCapture->expectChildColor(0, 0);
3464 // But it's only 10x10.
3465 mCapture->expectFGColor(10, 10);
3466 }
3467
Robert Carr4cdc58f2017-08-23 14:22:20 -07003468 asTransaction([&](Transaction& t) {
3469 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3470 // We cause scaling by 2.
3471 t.setSize(mFGSurfaceControl, 128, 128);
3472 });
Robert Carr9b429f42017-04-17 14:56:57 -07003473
3474 {
chaviw0e3479f2018-09-10 16:49:30 -07003475 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003476 // We've positioned the child in the top left.
3477 mCapture->expectChildColor(0, 0);
3478 mCapture->expectChildColor(10, 10);
3479 mCapture->expectChildColor(19, 19);
3480 // And now it should be scaled all the way to 20x20
3481 mCapture->expectFGColor(20, 20);
3482 }
3483}
3484
Robert Carr1725eee2017-04-26 18:32:15 -07003485// Regression test for b/37673612
3486TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003487 asTransaction([&](Transaction& t) {
3488 t.show(mChild);
3489 t.setPosition(mChild, 0, 0);
3490 t.setPosition(mFGSurfaceControl, 0, 0);
3491 });
Robert Carr1725eee2017-04-26 18:32:15 -07003492
3493 {
chaviw0e3479f2018-09-10 16:49:30 -07003494 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003495 // We've positioned the child in the top left.
3496 mCapture->expectChildColor(0, 0);
3497 // But it's only 10x10.
3498 mCapture->expectFGColor(10, 10);
3499 }
Robert Carr1725eee2017-04-26 18:32:15 -07003500 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
3501 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003502 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07003503 sp<Surface> s = mFGSurfaceControl->getSurface();
3504 auto anw = static_cast<ANativeWindow*>(s.get());
3505 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
3506 native_window_set_buffers_dimensions(anw, 64, 128);
3507 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3508 waitForPostedBuffers();
3509
3510 {
3511 // The child should still be in the same place and not have any strange scaling as in
3512 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07003513 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003514 mCapture->expectChildColor(0, 0);
3515 mCapture->expectFGColor(10, 10);
3516 }
3517}
3518
Dan Stoza412903f2017-04-27 13:42:17 -07003519TEST_F(ChildLayerTest, Bug36858924) {
3520 // Destroy the child layer
3521 mChild.clear();
3522
3523 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08003524 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
3525 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07003526
3527 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07003528 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003529 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
3530 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003531 t.show(mChild);
3532 });
Dan Stoza412903f2017-04-27 13:42:17 -07003533
3534 // Render the foreground surface a few times
3535 //
3536 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
3537 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
3538 // never acquire/release the first buffer
3539 ALOGI("Filling 1");
3540 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3541 ALOGI("Filling 2");
3542 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
3543 ALOGI("Filling 3");
3544 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
3545 ALOGI("Filling 4");
3546 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3547}
3548
chaviwf1961f72017-09-18 16:41:07 -07003549TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003550 asTransaction([&](Transaction& t) {
3551 t.show(mChild);
3552 t.setPosition(mChild, 10, 10);
3553 t.setPosition(mFGSurfaceControl, 64, 64);
3554 });
chaviw06178942017-07-27 10:25:59 -07003555
3556 {
chaviw0e3479f2018-09-10 16:49:30 -07003557 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003558 // Top left of foreground must now be visible
3559 mCapture->expectFGColor(64, 64);
3560 // But 10 pixels in we should see the child surface
3561 mCapture->expectChildColor(74, 74);
3562 // And 10 more pixels we should be back to the foreground surface
3563 mCapture->expectFGColor(84, 84);
3564 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003565
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003566 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07003567
chaviw06178942017-07-27 10:25:59 -07003568 {
chaviw0e3479f2018-09-10 16:49:30 -07003569 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003570 mCapture->expectFGColor(64, 64);
3571 // In reparenting we should have exposed the entire foreground surface.
3572 mCapture->expectFGColor(74, 74);
3573 // And the child layer should now begin at 10, 10 (since the BG
3574 // layer is at (0, 0)).
3575 mCapture->expectBGColor(9, 9);
3576 mCapture->expectChildColor(10, 10);
3577 }
3578}
3579
chaviwf1961f72017-09-18 16:41:07 -07003580TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003581 asTransaction([&](Transaction& t) {
3582 t.show(mChild);
3583 t.setPosition(mChild, 10, 10);
3584 t.setPosition(mFGSurfaceControl, 64, 64);
3585 });
chaviwf1961f72017-09-18 16:41:07 -07003586
3587 {
chaviw0e3479f2018-09-10 16:49:30 -07003588 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003589 // Top left of foreground must now be visible
3590 mCapture->expectFGColor(64, 64);
3591 // But 10 pixels in we should see the child surface
3592 mCapture->expectChildColor(74, 74);
3593 // And 10 more pixels we should be back to the foreground surface
3594 mCapture->expectFGColor(84, 84);
3595 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003596 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07003597 {
chaviw0e3479f2018-09-10 16:49:30 -07003598 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003599 // Nothing should have changed.
3600 mCapture->expectFGColor(64, 64);
3601 mCapture->expectChildColor(74, 74);
3602 mCapture->expectFGColor(84, 84);
3603 }
3604}
3605
3606TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07003607 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003608 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07003609 ASSERT_TRUE(newSurface->isValid());
3610
3611 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003612 asTransaction([&](Transaction& t) {
3613 t.hide(mChild);
3614 t.show(newSurface);
3615 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003616 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003617 t.setPosition(mFGSurfaceControl, 64, 64);
3618 });
chaviwf1961f72017-09-18 16:41:07 -07003619
3620 {
chaviw0e3479f2018-09-10 16:49:30 -07003621 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003622 // Top left of foreground must now be visible
3623 mCapture->expectFGColor(64, 64);
3624 // At 10, 10 we should see the new surface
3625 mCapture->checkPixel(10, 10, 63, 195, 63);
3626 }
3627
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003628 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07003629
3630 {
chaviw0e3479f2018-09-10 16:49:30 -07003631 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003632 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
3633 // mFGSurface, putting it at 74, 74.
3634 mCapture->expectFGColor(64, 64);
3635 mCapture->checkPixel(74, 74, 63, 195, 63);
3636 mCapture->expectFGColor(84, 84);
3637 }
3638}
3639
chaviwc9674332017-08-28 12:32:18 -07003640TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003641 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
3642 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07003643 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3644
3645 {
chaviw0e3479f2018-09-10 16:49:30 -07003646 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07003647 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
3648 // which begins at 64, 64
3649 mCapture->checkPixel(64, 64, 50, 50, 50);
3650 }
3651}
3652
Robert Carr503c7042017-09-27 15:06:08 -07003653TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003654 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07003655 fillSurfaceRGBA8(relative, 255, 255, 255);
3656
3657 Transaction t;
3658 t.setLayer(relative, INT32_MAX)
3659 .setRelativeLayer(mChild, relative->getHandle(), 1)
3660 .setPosition(mFGSurfaceControl, 0, 0)
3661 .apply(true);
3662
3663 // We expect that the child should have been elevated above our
3664 // INT_MAX layer even though it's not a child of it.
3665 {
chaviw0e3479f2018-09-10 16:49:30 -07003666 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07003667 mCapture->expectChildColor(0, 0);
3668 mCapture->expectChildColor(9, 9);
3669 mCapture->checkPixel(10, 10, 255, 255, 255);
3670 }
3671}
Vishnu Nair60356342018-11-13 13:00:45 -08003672class BoundlessLayerTest : public LayerUpdateTest {
3673protected:
3674 std::unique_ptr<ScreenCapture> mCapture;
3675};
3676
3677// Verify setting a size on a buffer layer has no effect.
3678TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
3679 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003680 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
3681 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003682 ASSERT_TRUE(bufferLayer->isValid());
3683 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
3684 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
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, 94, 94), Color::BLACK);
3691 // Buffer layer should not extend past buffer bounds
3692 mCapture->expectFGColor(95, 95);
3693 }
3694}
3695
3696// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
3697// which will crop the color layer.
3698TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
3699 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003700 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3701 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003702 ASSERT_TRUE(colorLayer->isValid());
3703 asTransaction([&](Transaction& t) {
3704 t.setColor(colorLayer, half3{0, 0, 0});
3705 t.show(colorLayer);
3706 });
3707 {
3708 mCapture = screenshot();
3709 // Top left of background must now be visible
3710 mCapture->expectBGColor(0, 0);
3711 // Foreground Surface bounds must be color layer
3712 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3713 // Color layer should not extend past foreground bounds
3714 mCapture->expectBGColor(129, 129);
3715 }
3716}
3717
3718// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
3719// a crop which will be used to crop the color layer.
3720TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003721 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3722 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003723 ASSERT_TRUE(cropLayer->isValid());
3724 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003725 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3726 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003727 ASSERT_TRUE(colorLayer->isValid());
3728 asTransaction([&](Transaction& t) {
3729 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
3730 t.setColor(colorLayer, half3{0, 0, 0});
3731 t.show(cropLayer);
3732 t.show(colorLayer);
3733 });
3734 {
3735 mCapture = screenshot();
3736 // Top left of background must now be visible
3737 mCapture->expectBGColor(0, 0);
3738 // Top left of foreground must now be visible
3739 mCapture->expectFGColor(64, 64);
3740 // 5 pixels from the foreground we should see the child surface
3741 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
3742 // 10 pixels from the foreground we should be back to the foreground surface
3743 mCapture->expectFGColor(74, 74);
3744 }
3745}
3746
3747// Verify for boundless layer with no children, their transforms have no effect.
3748TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
3749 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003750 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3751 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003752 ASSERT_TRUE(colorLayer->isValid());
3753 asTransaction([&](Transaction& t) {
3754 t.setPosition(colorLayer, 320, 320);
3755 t.setMatrix(colorLayer, 2, 0, 0, 2);
3756 t.setColor(colorLayer, half3{0, 0, 0});
3757 t.show(colorLayer);
3758 });
3759 {
3760 mCapture = screenshot();
3761 // Top left of background must now be visible
3762 mCapture->expectBGColor(0, 0);
3763 // Foreground Surface bounds must be color layer
3764 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3765 // Color layer should not extend past foreground bounds
3766 mCapture->expectBGColor(129, 129);
3767 }
3768}
3769
3770// Verify for boundless layer with children, their transforms have an effect.
3771TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
3772 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003773 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3774 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003775 ASSERT_TRUE(boundlessLayerRightShift->isValid());
3776 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003777 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
3778 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003779 ASSERT_TRUE(boundlessLayerDownShift->isValid());
3780 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003781 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3782 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08003783 ASSERT_TRUE(colorLayer->isValid());
3784 asTransaction([&](Transaction& t) {
3785 t.setPosition(boundlessLayerRightShift, 32, 0);
3786 t.show(boundlessLayerRightShift);
3787 t.setPosition(boundlessLayerDownShift, 0, 32);
3788 t.show(boundlessLayerDownShift);
3789 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3790 t.setColor(colorLayer, half3{0, 0, 0});
3791 t.show(colorLayer);
3792 });
3793 {
3794 mCapture = screenshot();
3795 // Top left of background must now be visible
3796 mCapture->expectBGColor(0, 0);
3797 // Top left of foreground must now be visible
3798 mCapture->expectFGColor(64, 64);
3799 // Foreground Surface bounds must be color layer
3800 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
3801 // Color layer should not extend past foreground bounds
3802 mCapture->expectBGColor(129, 129);
3803 }
3804}
3805
3806// Verify child layers do not get clipped if they temporarily move into the negative
3807// coordinate space as the result of an intermediate transformation.
3808TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
3809 sp<SurfaceControl> boundlessLayer =
3810 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3811 0 /* flags */, mFGSurfaceControl.get());
3812 ASSERT_TRUE(boundlessLayer != nullptr);
3813 ASSERT_TRUE(boundlessLayer->isValid());
3814 sp<SurfaceControl> colorLayer =
3815 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3816 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
3817 ASSERT_TRUE(colorLayer != nullptr);
3818 ASSERT_TRUE(colorLayer->isValid());
3819 asTransaction([&](Transaction& t) {
3820 // shift child layer off bounds. If this layer was not boundless, we will
3821 // expect the child layer to be cropped.
3822 t.setPosition(boundlessLayer, 32, 32);
3823 t.show(boundlessLayer);
3824 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3825 // undo shift by parent
3826 t.setPosition(colorLayer, -32, -32);
3827 t.setColor(colorLayer, half3{0, 0, 0});
3828 t.show(colorLayer);
3829 });
3830 {
3831 mCapture = screenshot();
3832 // Top left of background must now be visible
3833 mCapture->expectBGColor(0, 0);
3834 // Foreground Surface bounds must be color layer
3835 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3836 // Color layer should not extend past foreground bounds
3837 mCapture->expectBGColor(129, 129);
3838 }
3839}
3840
3841// Verify for boundless root layers with children, their transforms have an effect.
3842TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003843 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
3844 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08003845 ASSERT_TRUE(rootBoundlessLayer->isValid());
3846 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003847 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
3848 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
3849
Vishnu Nair60356342018-11-13 13:00:45 -08003850 ASSERT_TRUE(colorLayer->isValid());
3851 asTransaction([&](Transaction& t) {
3852 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
3853 t.setPosition(rootBoundlessLayer, 32, 32);
3854 t.show(rootBoundlessLayer);
3855 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3856 t.setColor(colorLayer, half3{0, 0, 0});
3857 t.show(colorLayer);
3858 t.hide(mFGSurfaceControl);
3859 });
3860 {
3861 mCapture = screenshot();
3862 // Top left of background must now be visible
3863 mCapture->expectBGColor(0, 0);
3864 // Top left of foreground must now be visible
3865 mCapture->expectBGColor(31, 31);
3866 // Foreground Surface bounds must be color layer
3867 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
3868 // Color layer should not extend past foreground bounds
3869 mCapture->expectBGColor(97, 97);
3870 }
3871}
Robert Carr503c7042017-09-27 15:06:08 -07003872
chaviwa76b2712017-09-20 12:02:26 -07003873class ScreenCaptureTest : public LayerUpdateTest {
3874protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003875 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07003876};
3877
3878TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
3879 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003880 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003881 mCapture->expectBGColor(0, 0);
3882 // Doesn't capture FG layer which is at 64, 64
3883 mCapture->expectBGColor(64, 64);
3884}
3885
3886TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
3887 auto fgHandle = mFGSurfaceControl->getHandle();
3888
Vishnu Nair88a11f22018-11-28 18:30:57 -08003889 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3890 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003891 fillSurfaceRGBA8(child, 200, 200, 200);
3892
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003893 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003894
3895 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003896 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003897 mCapture->expectFGColor(10, 10);
3898 mCapture->expectChildColor(0, 0);
3899}
3900
Robert Carr578038f2018-03-09 12:25:24 -08003901TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
3902 auto fgHandle = mFGSurfaceControl->getHandle();
3903
Vishnu Nair88a11f22018-11-28 18:30:57 -08003904 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3905 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08003906 fillSurfaceRGBA8(child, 200, 200, 200);
3907
3908 SurfaceComposerClient::Transaction().show(child).apply(true);
3909
3910 // Captures mFGSurfaceControl's child
3911 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3912 mCapture->checkPixel(10, 10, 0, 0, 0);
3913 mCapture->expectChildColor(0, 0);
3914}
3915
chaviw50da5042018-04-09 13:49:37 -07003916TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08003917 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3918 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07003919
3920 fillSurfaceRGBA8(child, 200, 200, 200);
3921
3922 SurfaceComposerClient::Transaction().show(child).apply(true);
3923
3924 auto childHandle = child->getHandle();
3925
3926 // Captures child
3927 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
3928 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
3929 // Area outside of child's bounds is transparent.
3930 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
3931}
3932
chaviw4b129c22018-04-09 16:19:43 -07003933TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
3934 auto fgHandle = mFGSurfaceControl->getHandle();
3935
Vishnu Nair88a11f22018-11-28 18:30:57 -08003936 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3937 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3938 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07003939 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07003940 fillSurfaceRGBA8(child, 200, 200, 200);
3941 fillSurfaceRGBA8(relative, 100, 100, 100);
3942
3943 SurfaceComposerClient::Transaction()
3944 .show(child)
3945 // Set relative layer above fg layer so should be shown above when computing all layers.
3946 .setRelativeLayer(relative, fgHandle, 1)
3947 .show(relative)
3948 .apply(true);
3949
3950 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
3951 ScreenCapture::captureLayers(&mCapture, fgHandle);
3952 mCapture->expectFGColor(10, 10);
3953 mCapture->expectChildColor(0, 0);
3954}
3955
3956TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
3957 auto fgHandle = mFGSurfaceControl->getHandle();
3958
Vishnu Nair88a11f22018-11-28 18:30:57 -08003959 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
3960 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3961 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
3962 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07003963 fillSurfaceRGBA8(child, 200, 200, 200);
3964 fillSurfaceRGBA8(relative, 100, 100, 100);
3965
3966 SurfaceComposerClient::Transaction()
3967 .show(child)
3968 // Set relative layer below fg layer but relative to child layer so it should be shown
3969 // above child layer.
3970 .setLayer(relative, -1)
3971 .setRelativeLayer(relative, child->getHandle(), 1)
3972 .show(relative)
3973 .apply(true);
3974
3975 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
3976 // relative value should be taken into account, placing it above child layer.
3977 ScreenCapture::captureLayers(&mCapture, fgHandle);
3978 mCapture->expectFGColor(10, 10);
3979 // Relative layer is showing on top of child layer
3980 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
3981}
Robert Carr578038f2018-03-09 12:25:24 -08003982
3983// In the following tests we verify successful skipping of a parent layer,
3984// so we use the same verification logic and only change how we mutate
3985// the parent layer to verify that various properties are ignored.
3986class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
3987public:
3988 void SetUp() override {
3989 LayerUpdateTest::SetUp();
3990
Vishnu Nair88a11f22018-11-28 18:30:57 -08003991 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3992 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08003993 fillSurfaceRGBA8(mChild, 200, 200, 200);
3994
3995 SurfaceComposerClient::Transaction().show(mChild).apply(true);
3996 }
3997
3998 void verify() {
3999 auto fgHandle = mFGSurfaceControl->getHandle();
4000 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4001 mCapture->checkPixel(10, 10, 0, 0, 0);
4002 mCapture->expectChildColor(0, 0);
4003 }
4004
4005 std::unique_ptr<ScreenCapture> mCapture;
4006 sp<SurfaceControl> mChild;
4007};
4008
4009TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4010
4011 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4012
4013 // Even though the parent is hidden we should still capture the child.
4014 verify();
4015}
4016
4017TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004018 SurfaceComposerClient::Transaction()
4019 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
4020 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08004021
4022 // Even though the parent is cropped out we should still capture the child.
4023 verify();
4024}
4025
4026TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4027
4028 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4029
4030 // We should not inherit the parent scaling.
4031 verify();
4032}
4033
Robert Carr15eae092018-03-23 13:43:53 -07004034TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4035 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4036
4037 // Even though the parent is hidden we should still capture the child.
4038 verify();
4039
4040 // Verify everything was properly hidden when rendering the full-screen.
4041 screenshot()->expectBGColor(0,0);
4042}
4043
4044
chaviwa76b2712017-09-20 12:02:26 -07004045TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4046 auto fgHandle = mFGSurfaceControl->getHandle();
4047
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
Vishnu Nair88a11f22018-11-28 18:30:57 -08004052 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4053 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004054
4055 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4056 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004057 .show(child)
4058 .setPosition(grandchild, 5, 5)
4059 .show(grandchild)
4060 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004061
4062 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004063 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004064 mCapture->expectFGColor(10, 10);
4065 mCapture->expectChildColor(0, 0);
4066 mCapture->checkPixel(5, 5, 50, 50, 50);
4067}
4068
4069TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004070 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4071 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004072 fillSurfaceRGBA8(child, 200, 200, 200);
4073 auto childHandle = child->getHandle();
4074
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004075 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004076
4077 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004078 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004079 mCapture->expectChildColor(0, 0);
4080 mCapture->expectChildColor(9, 9);
4081}
4082
4083TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004084 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4085 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004086 fillSurfaceRGBA8(child, 200, 200, 200);
4087 auto childHandle = child->getHandle();
4088
Vishnu Nair88a11f22018-11-28 18:30:57 -08004089 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4090 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004091 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4092
4093 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004094 .show(child)
4095 .setPosition(grandchild, 5, 5)
4096 .show(grandchild)
4097 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004098
4099 auto grandchildHandle = grandchild->getHandle();
4100
4101 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004102 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004103 mCapture->checkPixel(0, 0, 50, 50, 50);
4104 mCapture->checkPixel(4, 4, 50, 50, 50);
4105}
4106
chaviw7206d492017-11-10 16:16:12 -08004107TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004108 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004109 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4110 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004111
Marissa Wall61c58622018-07-18 10:12:20 -07004112 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4113 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004114
4115 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004116 .setLayer(redLayer, INT32_MAX - 1)
4117 .show(redLayer)
4118 .show(blueLayer)
4119 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004120
4121 auto redLayerHandle = redLayer->getHandle();
4122
4123 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004124 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4125 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4126 // red area below the blue area
4127 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4128 // red area to the right of the blue area
4129 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004130
4131 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004132 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004133 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4134 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004135 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004136 mCapture->checkPixel(30, 30, 0, 0, 0);
4137}
4138
4139TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004140 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004141 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4142 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004143
Marissa Wall61c58622018-07-18 10:12:20 -07004144 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4145 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004146
4147 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004148 .setLayer(redLayer, INT32_MAX - 1)
4149 .show(redLayer)
4150 .show(blueLayer)
4151 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004152
4153 auto redLayerHandle = redLayer->getHandle();
4154
4155 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004156 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4157 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4158 // red area below the blue area
4159 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4160 // red area to the right of the blue area
4161 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004162
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004163 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004164 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004165 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4166 // red area below the blue area
4167 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4168 // red area to the right of the blue area
4169 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004170 mCapture->checkPixel(30, 30, 0, 0, 0);
4171}
4172
4173TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004174 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004175
Marissa Wall61c58622018-07-18 10:12:20 -07004176 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004177
4178 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07004179 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08004180 SurfaceComposerClient::Transaction().apply(true);
4181
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004182 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004183
4184 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004185 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4186 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004187}
4188
chaviw8e3fe5d2018-02-22 10:55:42 -08004189
4190class DereferenceSurfaceControlTest : public LayerTransactionTest {
4191protected:
4192 void SetUp() override {
4193 LayerTransactionTest::SetUp();
4194 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004195 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004196 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004197 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004198 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4199 {
4200 SCOPED_TRACE("before anything");
4201 auto shot = screenshot();
4202 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4203 }
4204 }
4205 void TearDown() override {
4206 LayerTransactionTest::TearDown();
4207 bgLayer = 0;
4208 fgLayer = 0;
4209 }
4210
4211 sp<SurfaceControl> bgLayer;
4212 sp<SurfaceControl> fgLayer;
4213};
4214
4215TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4216 fgLayer = nullptr;
4217 {
4218 SCOPED_TRACE("after setting null");
4219 auto shot = screenshot();
4220 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4221 }
4222}
4223
4224TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4225 auto transaction = Transaction().show(fgLayer);
4226 fgLayer = nullptr;
4227 {
4228 SCOPED_TRACE("after setting null");
4229 auto shot = screenshot();
4230 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4231 }
4232}
4233
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004234} // namespace android